blob: f09032622672d4bf2775a6c2b719eb1b8559a194 [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
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07007#include "../../include/javascript/IJavaScript.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07008#include "../../include/javascript/JS_Context.h"
9#include "../../include/javascript/JS_EventHandler.h"
10#include "../../include/javascript/JS_Runtime.h"
Tom Sepez9ed941a2015-09-01 09:51:10 -070011#include "../../include/javascript/JavaScript.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070012#include "../../include/javascript/resource.h"
13
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,
36 CFX_WideString& info) {
37 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) {
44 info = JSGetStringFromID(this, IDS_STRING_JSBUSY);
45 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)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 info = JSGetStringFromID(this, IDS_STRING_JSEVENT);
54 return FALSE;
55 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 FXJSErr error = {NULL, NULL, 0};
58 int nRet = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 if (script.GetLength() > 0) {
Tom Sepez39bfe122015-09-17 15:25:23 -070060 nRet = FXJS_Execute(m_pRuntime->GetIsolate(), this, script.c_str(),
61 script.GetLength(), &error);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 }
Bo Xud4e406e2014-08-13 11:03:19 -070063
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 if (nRet < 0) {
65 CFX_WideString sLine;
66 sLine.Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline,
67 error.message);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 info += sLine;
69 } else {
70 info = JSGetStringFromID(this, IDS_STRING_RUN);
71 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070072
Tom Sepez5d0e8432015-09-22 15:50:03 -070073 m_pRuntime->RemoveEventFromSet(event);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 m_pEventHandler->Destroy();
75 m_bBusy = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 return nRet >= 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078}
79
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080void CJS_Context::OnApp_Init() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 m_pEventHandler->OnApp_Init();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082}
83
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084void CJS_Context::OnDoc_Open(CPDFSDK_Document* pDoc,
85 const CFX_WideString& strTargetName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 m_pEventHandler->OnDoc_Open(pDoc, strTargetName);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087}
88
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089void CJS_Context::OnDoc_WillPrint(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 m_pEventHandler->OnDoc_WillPrint(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091}
92
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093void CJS_Context::OnDoc_DidPrint(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 m_pEventHandler->OnDoc_DidPrint(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095}
96
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097void CJS_Context::OnDoc_WillSave(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 m_pEventHandler->OnDoc_WillSave(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099}
100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101void CJS_Context::OnDoc_DidSave(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 m_pEventHandler->OnDoc_DidSave(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700103}
104
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105void CJS_Context::OnDoc_WillClose(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 m_pEventHandler->OnDoc_WillClose(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700107}
108
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109void CJS_Context::OnPage_Open(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 m_pEventHandler->OnPage_Open(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700111}
112
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113void CJS_Context::OnPage_Close(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 m_pEventHandler->OnPage_Close(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115}
116
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117void CJS_Context::OnPage_InView(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 m_pEventHandler->OnPage_InView(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700119}
120
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121void CJS_Context::OnPage_OutView(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 m_pEventHandler->OnPage_OutView(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700123}
124
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125void CJS_Context::OnField_MouseDown(FX_BOOL bModifier,
126 FX_BOOL bShift,
127 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 m_pEventHandler->OnField_MouseDown(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129}
130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131void CJS_Context::OnField_MouseEnter(FX_BOOL bModifier,
132 FX_BOOL bShift,
133 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 m_pEventHandler->OnField_MouseEnter(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700135}
136
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700137void CJS_Context::OnField_MouseExit(FX_BOOL bModifier,
138 FX_BOOL bShift,
139 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 m_pEventHandler->OnField_MouseExit(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141}
142
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143void CJS_Context::OnField_MouseUp(FX_BOOL bModifier,
144 FX_BOOL bShift,
145 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 m_pEventHandler->OnField_MouseUp(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147}
148
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149void CJS_Context::OnField_Focus(FX_BOOL bModifier,
150 FX_BOOL bShift,
151 CPDF_FormField* pTarget,
152 const CFX_WideString& Value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154}
155
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700156void CJS_Context::OnField_Blur(FX_BOOL bModifier,
157 FX_BOOL bShift,
158 CPDF_FormField* pTarget,
159 const CFX_WideString& Value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161}
162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163void CJS_Context::OnField_Calculate(CPDF_FormField* pSource,
164 CPDF_FormField* pTarget,
165 CFX_WideString& Value,
166 FX_BOOL& bRc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168}
169
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170void CJS_Context::OnField_Format(CPDF_FormField* pTarget,
171 CFX_WideString& Value,
172 FX_BOOL bWillCommit) {
173 m_pEventHandler->OnField_Format(pTarget, Value, bWillCommit);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174}
175
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176void CJS_Context::OnField_Keystroke(CFX_WideString& strChange,
177 const CFX_WideString& strChangeEx,
178 FX_BOOL bKeyDown,
179 FX_BOOL bModifier,
180 int& nSelEnd,
181 int& nSelStart,
182 FX_BOOL bShift,
183 CPDF_FormField* pTarget,
184 CFX_WideString& Value,
185 FX_BOOL bWillCommit,
186 FX_BOOL bFieldFull,
187 FX_BOOL& bRc) {
188 m_pEventHandler->OnField_Keystroke(
189 strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart, bShift,
190 pTarget, Value, bWillCommit, bFieldFull, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191}
192
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193void CJS_Context::OnField_Validate(CFX_WideString& strChange,
194 const CFX_WideString& strChangeEx,
195 FX_BOOL bKeyDown,
196 FX_BOOL bModifier,
197 FX_BOOL bShift,
198 CPDF_FormField* pTarget,
199 CFX_WideString& Value,
200 FX_BOOL& bRc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier,
202 bShift, pTarget, Value, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203}
204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205void CJS_Context::OnScreen_Focus(FX_BOOL bModifier,
206 FX_BOOL bShift,
207 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 m_pEventHandler->OnScreen_Focus(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209}
210
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211void CJS_Context::OnScreen_Blur(FX_BOOL bModifier,
212 FX_BOOL bShift,
213 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 m_pEventHandler->OnScreen_Blur(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700215}
216
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217void CJS_Context::OnScreen_Open(FX_BOOL bModifier,
218 FX_BOOL bShift,
219 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 m_pEventHandler->OnScreen_Open(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700221}
222
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223void CJS_Context::OnScreen_Close(FX_BOOL bModifier,
224 FX_BOOL bShift,
225 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 m_pEventHandler->OnScreen_Close(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700227}
228
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700229void CJS_Context::OnScreen_MouseDown(FX_BOOL bModifier,
230 FX_BOOL bShift,
231 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700232 m_pEventHandler->OnScreen_MouseDown(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233}
234
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235void CJS_Context::OnScreen_MouseUp(FX_BOOL bModifier,
236 FX_BOOL bShift,
237 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 m_pEventHandler->OnScreen_MouseUp(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239}
240
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700241void CJS_Context::OnScreen_MouseEnter(FX_BOOL bModifier,
242 FX_BOOL bShift,
243 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 m_pEventHandler->OnScreen_MouseEnter(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700245}
246
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247void CJS_Context::OnScreen_MouseExit(FX_BOOL bModifier,
248 FX_BOOL bShift,
249 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 m_pEventHandler->OnScreen_MouseExit(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700251}
252
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253void CJS_Context::OnScreen_InView(FX_BOOL bModifier,
254 FX_BOOL bShift,
255 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700256 m_pEventHandler->OnScreen_InView(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700257}
258
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700259void CJS_Context::OnScreen_OutView(FX_BOOL bModifier,
260 FX_BOOL bShift,
261 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 m_pEventHandler->OnScreen_OutView(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263}
264
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265void CJS_Context::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 m_pEventHandler->OnBookmark_MouseUp(pBookMark);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700267}
268
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269void CJS_Context::OnLink_MouseUp(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 m_pEventHandler->OnLink_MouseUp(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700271}
272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273void CJS_Context::OnConsole_Exec() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 m_pEventHandler->OnConsole_Exec();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700275}
276
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277void CJS_Context::OnExternal_Exec() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 m_pEventHandler->OnExternal_Exec();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279}
280
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281void CJS_Context::OnBatchExec(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282 m_pEventHandler->OnBatchExec(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283}
284
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285void CJS_Context::OnMenu_Exec(CPDFSDK_Document* pTarget,
286 const CFX_WideString& strTargetName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700287 m_pEventHandler->OnMenu_Exec(pTarget, strTargetName);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700288}