blob: 883ecc82ae9cb910df65acc09cb3fdeb68fa675b [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepez37458412015-10-06 11:33:46 -07007#include "JS_Object.h"
8
9#include "../../include/fsdk_mgr.h" // For CPDFDoc_Environment.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070010#include "../../include/javascript/IJavaScript.h"
Tom Sepez37458412015-10-06 11:33:46 -070011#include "JS_Context.h"
12#include "JS_Define.h"
Lei Zhangd607f5b2015-10-05 17:06:09 -070013
14namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015
Nico Weber9d8ec5a2015-08-04 13:00:21 -070016int FXJS_MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070017 const FX_WCHAR* swMsg,
18 const FX_WCHAR* swTitle,
19 FX_UINT nType,
20 FX_UINT nIcon) {
21 if (!pApp)
22 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument())
25 pDoc->KillFocusAnnot();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 return pApp->JS_appAlert(swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028}
29
Lei Zhangd607f5b2015-10-05 17:06:09 -070030} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {}
33
34CJS_EmbedObj::~CJS_EmbedObj() {
35 m_pJSObject = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036}
37
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038int CJS_EmbedObj::MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 const FX_WCHAR* swMsg,
40 const FX_WCHAR* swTitle,
41 FX_UINT nType,
42 FX_UINT nIcon) {
Lei Zhangd607f5b2015-10-05 17:06:09 -070043 return FXJS_MsgBox(pApp, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044}
45
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
47 CJS_Object::Alert(pContext, swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048}
49
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
51 CJS_Object* pJSObj = data.GetParameter();
52 pJSObj->ExitInstance();
53 delete pJSObj;
Tom Sepez39bfe122015-09-17 15:25:23 -070054 FXJS_FreePrivate(data.GetInternalField(0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055}
56
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
58 CJS_Object* pJSObj = data.GetParameter();
59 pJSObj->Dispose();
60 data.SetSecondPassCallback(FreeObject);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020061}
62
Tom Sepez116e4ad2015-09-21 09:22:05 -070063CJS_Object::CJS_Object(v8::Local<v8::Object> pObject) {
64 m_pIsolate = pObject->CreationContext()->GetIsolate();
65 m_pV8Object.Reset(m_pIsolate, pObject);
66}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067
Lei Zhang2b1a2d52015-08-14 22:16:22 -070068CJS_Object::~CJS_Object() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070069}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071void CJS_Object::MakeWeak() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070072 m_pV8Object.SetWeak(this, DisposeObject,
73 v8::WeakCallbackType::kInternalFields);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020074}
75
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076void CJS_Object::Dispose() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070077 m_pV8Object.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078}
79
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080int CJS_Object::MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 const FX_WCHAR* swMsg,
82 const FX_WCHAR* swTitle,
83 FX_UINT nType,
84 FX_UINT nIcon) {
Lei Zhangd607f5b2015-10-05 17:06:09 -070085 return FXJS_MsgBox(pApp, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086}
87
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
89 ASSERT(pContext != NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 if (pContext->IsMsgBoxEnabled()) {
92 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
93 if (pApp)
94 pApp->JS_appAlert(swMsg, NULL, 0, 3);
95 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096}
Tom Sepez371c87f2015-08-13 16:56:19 -070097
Lei Zhang2d5a0e12015-10-05 17:00:03 -070098CJS_Timer::CJS_Timer(CJS_EmbedObj* pObj,
99 CPDFDoc_Environment* pApp,
100 CJS_Runtime* pRuntime,
101 int nType,
102 const CFX_WideString& script,
103 FX_DWORD dwElapse,
104 FX_DWORD dwTimeOut)
105 : m_nTimerID(0),
106 m_pEmbedObj(pObj),
107 m_bProcessing(false),
108 m_bValid(true),
109 m_nType(nType),
110 m_dwTimeOut(dwTimeOut),
Lei Zhang79e893a2015-11-04 16:02:47 -0800111 m_swJScript(script),
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700112 m_pRuntime(pRuntime),
113 m_pApp(pApp) {
Tom Sepez371c87f2015-08-13 16:56:19 -0700114 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700115 m_nTimerID = pHandler->SetTimer(dwElapse, TimerProc);
Tom Sepez371c87f2015-08-13 16:56:19 -0700116 (*GetGlobalTimerMap())[m_nTimerID] = this;
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700117 m_pRuntime->AddObserver(this);
118}
119
120CJS_Timer::~CJS_Timer() {
121 CJS_Runtime* pRuntime = GetRuntime();
122 if (pRuntime)
123 pRuntime->RemoveObserver(this);
124 KillJSTimer();
Tom Sepez371c87f2015-08-13 16:56:19 -0700125}
126
127void CJS_Timer::KillJSTimer() {
128 if (m_nTimerID) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700129 if (m_bValid) {
130 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
131 pHandler->KillTimer(m_nTimerID);
132 }
Tom Sepez371c87f2015-08-13 16:56:19 -0700133 GetGlobalTimerMap()->erase(m_nTimerID);
134 m_nTimerID = 0;
135 }
136}
137
138// static
139void CJS_Timer::TimerProc(int idEvent) {
140 const auto it = GetGlobalTimerMap()->find(idEvent);
141 if (it != GetGlobalTimerMap()->end()) {
142 CJS_Timer* pTimer = it->second;
143 if (!pTimer->m_bProcessing) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700144 CFX_AutoRestorer<bool> scoped_processing(&pTimer->m_bProcessing);
145 pTimer->m_bProcessing = true;
Tom Sepez371c87f2015-08-13 16:56:19 -0700146 if (pTimer->m_pEmbedObj)
147 pTimer->m_pEmbedObj->TimerProc(pTimer);
Tom Sepez371c87f2015-08-13 16:56:19 -0700148 }
149 }
150}
151
152// static
153CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() {
154 // Leak the timer array at shutdown.
155 static auto* s_TimerMap = new TimerMap;
156 return s_TimerMap;
157}
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700158
159void CJS_Timer::OnDestroyed() {
160 m_bValid = false;
161}