blob: a53e745786a51a052116956a7ad55450be64884f [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_Context.h"
8
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009#include "../../include/javascript/IJavaScript.h"
Tom Sepez37458412015-10-06 11:33:46 -070010#include "JS_EventHandler.h"
11#include "JS_Runtime.h"
12#include "resource.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013
14/* -------------------------- CJS_Context -------------------------- */
15
Nico Weber9d8ec5a2015-08-04 13:00:21 -070016CJS_Context::CJS_Context(CJS_Runtime* pRuntime)
17 : m_pRuntime(pRuntime), m_bBusy(FALSE), m_bMsgBoxEnable(TRUE) {
18 m_pEventHandler = new CJS_EventHandler(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019}
20
Lei Zhang2b1a2d52015-08-14 22:16:22 -070021CJS_Context::~CJS_Context() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022 delete m_pEventHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023}
24
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025CPDFSDK_Document* CJS_Context::GetReaderDocument() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 return m_pRuntime->GetReaderDocument();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027}
28
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029CPDFDoc_Environment* CJS_Context::GetReaderApp() {
30 ASSERT(m_pRuntime != NULL);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070031
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 return m_pRuntime->GetReaderApp();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033}
34
Tom Sepez9ed941a2015-09-01 09:51:10 -070035FX_BOOL CJS_Context::RunScript(const CFX_WideString& script,
Tom Sepez33420902015-10-13 15:00:10 -070036 CFX_WideString* info) {
Tom Sepez9ed941a2015-09-01 09:51:10 -070037 v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
38 v8::Locker locker(m_pRuntime->GetIsolate());
39 v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
40 v8::Local<v8::Context> context = m_pRuntime->NewJSContext();
41 v8::Context::Scope context_scope(context);
42
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 if (m_bBusy) {
Tom Sepez33420902015-10-13 15:00:10 -070044 *info = JSGetStringFromID(this, IDS_STRING_JSBUSY);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 return FALSE;
46 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047 m_bBusy = TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049 ASSERT(m_pEventHandler->IsValid());
Tom Sepez5d0e8432015-09-22 15:50:03 -070050 CJS_Runtime::FieldEvent event(m_pEventHandler->TargetName(),
51 m_pEventHandler->EventType());
52 if (!m_pRuntime->AddEventToSet(event)) {
Tom Sepez33420902015-10-13 15:00:10 -070053 *info = JSGetStringFromID(this, IDS_STRING_JSEVENT);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 return FALSE;
55 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
Tom Sepez33420902015-10-13 15:00:10 -070057 CFX_WideString sErrorMessage;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 int nRet = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 if (script.GetLength() > 0) {
Tom Sepez33420902015-10-13 15:00:10 -070060 nRet = m_pRuntime->Execute(this, script.c_str(), &sErrorMessage);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 }
Bo Xud4e406e2014-08-13 11:03:19 -070062
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 if (nRet < 0) {
Tom Sepez33420902015-10-13 15:00:10 -070064 *info += sErrorMessage;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 } else {
Tom Sepez33420902015-10-13 15:00:10 -070066 *info = JSGetStringFromID(this, IDS_STRING_RUN);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068
Tom Sepez5d0e8432015-09-22 15:50:03 -070069 m_pRuntime->RemoveEventFromSet(event);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 m_pEventHandler->Destroy();
71 m_bBusy = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070072
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 return nRet >= 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074}
75
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076void CJS_Context::OnApp_Init() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 m_pEventHandler->OnApp_Init();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078}
79
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080void CJS_Context::OnDoc_Open(CPDFSDK_Document* pDoc,
81 const CFX_WideString& strTargetName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 m_pEventHandler->OnDoc_Open(pDoc, strTargetName);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070083}
84
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085void CJS_Context::OnDoc_WillPrint(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 m_pEventHandler->OnDoc_WillPrint(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087}
88
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089void CJS_Context::OnDoc_DidPrint(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 m_pEventHandler->OnDoc_DidPrint(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091}
92
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093void CJS_Context::OnDoc_WillSave(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 m_pEventHandler->OnDoc_WillSave(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095}
96
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097void CJS_Context::OnDoc_DidSave(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 m_pEventHandler->OnDoc_DidSave(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099}
100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101void CJS_Context::OnDoc_WillClose(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 m_pEventHandler->OnDoc_WillClose(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700103}
104
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105void CJS_Context::OnPage_Open(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 m_pEventHandler->OnPage_Open(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700107}
108
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109void CJS_Context::OnPage_Close(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 m_pEventHandler->OnPage_Close(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700111}
112
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113void CJS_Context::OnPage_InView(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 m_pEventHandler->OnPage_InView(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115}
116
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117void CJS_Context::OnPage_OutView(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 m_pEventHandler->OnPage_OutView(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700119}
120
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121void CJS_Context::OnField_MouseDown(FX_BOOL bModifier,
122 FX_BOOL bShift,
123 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 m_pEventHandler->OnField_MouseDown(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125}
126
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127void CJS_Context::OnField_MouseEnter(FX_BOOL bModifier,
128 FX_BOOL bShift,
129 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 m_pEventHandler->OnField_MouseEnter(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131}
132
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133void CJS_Context::OnField_MouseExit(FX_BOOL bModifier,
134 FX_BOOL bShift,
135 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 m_pEventHandler->OnField_MouseExit(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137}
138
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139void CJS_Context::OnField_MouseUp(FX_BOOL bModifier,
140 FX_BOOL bShift,
141 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142 m_pEventHandler->OnField_MouseUp(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700143}
144
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145void CJS_Context::OnField_Focus(FX_BOOL bModifier,
146 FX_BOOL bShift,
147 CPDF_FormField* pTarget,
148 const CFX_WideString& Value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149 m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700150}
151
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152void CJS_Context::OnField_Blur(FX_BOOL bModifier,
153 FX_BOOL bShift,
154 CPDF_FormField* pTarget,
155 const CFX_WideString& Value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156 m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700157}
158
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159void CJS_Context::OnField_Calculate(CPDF_FormField* pSource,
160 CPDF_FormField* pTarget,
161 CFX_WideString& Value,
162 FX_BOOL& bRc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700164}
165
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166void CJS_Context::OnField_Format(CPDF_FormField* pTarget,
167 CFX_WideString& Value,
168 FX_BOOL bWillCommit) {
169 m_pEventHandler->OnField_Format(pTarget, Value, bWillCommit);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700170}
171
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172void CJS_Context::OnField_Keystroke(CFX_WideString& strChange,
173 const CFX_WideString& strChangeEx,
174 FX_BOOL bKeyDown,
175 FX_BOOL bModifier,
176 int& nSelEnd,
177 int& nSelStart,
178 FX_BOOL bShift,
179 CPDF_FormField* pTarget,
180 CFX_WideString& Value,
181 FX_BOOL bWillCommit,
182 FX_BOOL bFieldFull,
183 FX_BOOL& bRc) {
184 m_pEventHandler->OnField_Keystroke(
185 strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart, bShift,
186 pTarget, Value, bWillCommit, bFieldFull, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700187}
188
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700189void CJS_Context::OnField_Validate(CFX_WideString& strChange,
190 const CFX_WideString& strChangeEx,
191 FX_BOOL bKeyDown,
192 FX_BOOL bModifier,
193 FX_BOOL bShift,
194 CPDF_FormField* pTarget,
195 CFX_WideString& Value,
196 FX_BOOL& bRc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700197 m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier,
198 bShift, pTarget, Value, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}
200
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201void CJS_Context::OnScreen_Focus(FX_BOOL bModifier,
202 FX_BOOL bShift,
203 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 m_pEventHandler->OnScreen_Focus(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700205}
206
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207void CJS_Context::OnScreen_Blur(FX_BOOL bModifier,
208 FX_BOOL bShift,
209 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 m_pEventHandler->OnScreen_Blur(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211}
212
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213void CJS_Context::OnScreen_Open(FX_BOOL bModifier,
214 FX_BOOL bShift,
215 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 m_pEventHandler->OnScreen_Open(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217}
218
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219void CJS_Context::OnScreen_Close(FX_BOOL bModifier,
220 FX_BOOL bShift,
221 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 m_pEventHandler->OnScreen_Close(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700223}
224
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225void CJS_Context::OnScreen_MouseDown(FX_BOOL bModifier,
226 FX_BOOL bShift,
227 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 m_pEventHandler->OnScreen_MouseDown(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700229}
230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231void CJS_Context::OnScreen_MouseUp(FX_BOOL bModifier,
232 FX_BOOL bShift,
233 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 m_pEventHandler->OnScreen_MouseUp(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235}
236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237void CJS_Context::OnScreen_MouseEnter(FX_BOOL bModifier,
238 FX_BOOL bShift,
239 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 m_pEventHandler->OnScreen_MouseEnter(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700241}
242
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243void CJS_Context::OnScreen_MouseExit(FX_BOOL bModifier,
244 FX_BOOL bShift,
245 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 m_pEventHandler->OnScreen_MouseExit(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247}
248
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249void CJS_Context::OnScreen_InView(FX_BOOL bModifier,
250 FX_BOOL bShift,
251 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 m_pEventHandler->OnScreen_InView(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700253}
254
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255void CJS_Context::OnScreen_OutView(FX_BOOL bModifier,
256 FX_BOOL bShift,
257 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 m_pEventHandler->OnScreen_OutView(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259}
260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261void CJS_Context::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 m_pEventHandler->OnBookmark_MouseUp(pBookMark);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263}
264
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265void CJS_Context::OnLink_MouseUp(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 m_pEventHandler->OnLink_MouseUp(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700267}
268
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269void CJS_Context::OnConsole_Exec() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 m_pEventHandler->OnConsole_Exec();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271}
272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273void CJS_Context::OnExternal_Exec() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 m_pEventHandler->OnExternal_Exec();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275}
276
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277void CJS_Context::OnBatchExec(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 m_pEventHandler->OnBatchExec(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279}
280
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281void CJS_Context::OnMenu_Exec(CPDFSDK_Document* pTarget,
282 const CFX_WideString& strTargetName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 m_pEventHandler->OnMenu_Exec(pTarget, strTargetName);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700284}