blob: 44756b628d2fa18ec074954f6733e3e056468179 [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"
11#include "../../include/javascript/resource.h"
12
13/* -------------------------- CJS_Context -------------------------- */
14
Nico Weber9d8ec5a2015-08-04 13:00:21 -070015CJS_Context::CJS_Context(CJS_Runtime* pRuntime)
16 : m_pRuntime(pRuntime), m_bBusy(FALSE), m_bMsgBoxEnable(TRUE) {
17 m_pEventHandler = new CJS_EventHandler(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018}
19
Lei Zhang2b1a2d52015-08-14 22:16:22 -070020CJS_Context::~CJS_Context() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021 delete m_pEventHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022}
23
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024CPDFSDK_Document* CJS_Context::GetReaderDocument() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025 return m_pRuntime->GetReaderDocument();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026}
27
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028CPDFDoc_Environment* CJS_Context::GetReaderApp() {
29 ASSERT(m_pRuntime != NULL);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070030
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031 return m_pRuntime->GetReaderApp();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032}
33
Tom Sepez9ed941a2015-09-01 09:51:10 -070034FX_BOOL CJS_Context::RunScript(const CFX_WideString& script,
35 CFX_WideString& info) {
36 v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
37 v8::Locker locker(m_pRuntime->GetIsolate());
38 v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
39 v8::Local<v8::Context> context = m_pRuntime->NewJSContext();
40 v8::Context::Scope context_scope(context);
41
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 if (m_bBusy) {
43 info = JSGetStringFromID(this, IDS_STRING_JSBUSY);
44 return FALSE;
45 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 m_bBusy = TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 ASSERT(m_pEventHandler->IsValid());
Tom Sepez5d0e8432015-09-22 15:50:03 -070049 CJS_Runtime::FieldEvent event(m_pEventHandler->TargetName(),
50 m_pEventHandler->EventType());
51 if (!m_pRuntime->AddEventToSet(event)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 info = JSGetStringFromID(this, IDS_STRING_JSEVENT);
53 return FALSE;
54 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 FXJSErr error = {NULL, NULL, 0};
57 int nRet = 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 if (script.GetLength() > 0) {
Tom Sepez39bfe122015-09-17 15:25:23 -070059 nRet = FXJS_Execute(m_pRuntime->GetIsolate(), this, script.c_str(),
60 script.GetLength(), &error);
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) {
64 CFX_WideString sLine;
65 sLine.Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline,
66 error.message);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 info += sLine;
68 } else {
69 info = JSGetStringFromID(this, IDS_STRING_RUN);
70 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070071
Tom Sepez5d0e8432015-09-22 15:50:03 -070072 m_pRuntime->RemoveEventFromSet(event);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 m_pEventHandler->Destroy();
74 m_bBusy = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 return nRet >= 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077}
78
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079void CJS_Context::OnApp_Init() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 m_pEventHandler->OnApp_Init();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081}
82
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083void CJS_Context::OnDoc_Open(CPDFSDK_Document* pDoc,
84 const CFX_WideString& strTargetName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 m_pEventHandler->OnDoc_Open(pDoc, strTargetName);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086}
87
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088void CJS_Context::OnDoc_WillPrint(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 m_pEventHandler->OnDoc_WillPrint(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090}
91
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092void CJS_Context::OnDoc_DidPrint(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 m_pEventHandler->OnDoc_DidPrint(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094}
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096void CJS_Context::OnDoc_WillSave(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 m_pEventHandler->OnDoc_WillSave(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098}
99
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100void CJS_Context::OnDoc_DidSave(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 m_pEventHandler->OnDoc_DidSave(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102}
103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104void CJS_Context::OnDoc_WillClose(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 m_pEventHandler->OnDoc_WillClose(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106}
107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108void CJS_Context::OnPage_Open(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 m_pEventHandler->OnPage_Open(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700110}
111
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112void CJS_Context::OnPage_Close(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 m_pEventHandler->OnPage_Close(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700114}
115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116void CJS_Context::OnPage_InView(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 m_pEventHandler->OnPage_InView(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700118}
119
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120void CJS_Context::OnPage_OutView(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700121 m_pEventHandler->OnPage_OutView(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700122}
123
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124void CJS_Context::OnField_MouseDown(FX_BOOL bModifier,
125 FX_BOOL bShift,
126 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700127 m_pEventHandler->OnField_MouseDown(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700128}
129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130void CJS_Context::OnField_MouseEnter(FX_BOOL bModifier,
131 FX_BOOL bShift,
132 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 m_pEventHandler->OnField_MouseEnter(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700134}
135
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136void CJS_Context::OnField_MouseExit(FX_BOOL bModifier,
137 FX_BOOL bShift,
138 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 m_pEventHandler->OnField_MouseExit(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140}
141
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142void CJS_Context::OnField_MouseUp(FX_BOOL bModifier,
143 FX_BOOL bShift,
144 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700145 m_pEventHandler->OnField_MouseUp(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146}
147
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148void CJS_Context::OnField_Focus(FX_BOOL bModifier,
149 FX_BOOL bShift,
150 CPDF_FormField* pTarget,
151 const CFX_WideString& Value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700153}
154
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155void CJS_Context::OnField_Blur(FX_BOOL bModifier,
156 FX_BOOL bShift,
157 CPDF_FormField* pTarget,
158 const CFX_WideString& Value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700159 m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160}
161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162void CJS_Context::OnField_Calculate(CPDF_FormField* pSource,
163 CPDF_FormField* pTarget,
164 CFX_WideString& Value,
165 FX_BOOL& bRc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700166 m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700167}
168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169void CJS_Context::OnField_Format(CPDF_FormField* pTarget,
170 CFX_WideString& Value,
171 FX_BOOL bWillCommit) {
172 m_pEventHandler->OnField_Format(pTarget, Value, bWillCommit);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700173}
174
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175void CJS_Context::OnField_Keystroke(CFX_WideString& strChange,
176 const CFX_WideString& strChangeEx,
177 FX_BOOL bKeyDown,
178 FX_BOOL bModifier,
179 int& nSelEnd,
180 int& nSelStart,
181 FX_BOOL bShift,
182 CPDF_FormField* pTarget,
183 CFX_WideString& Value,
184 FX_BOOL bWillCommit,
185 FX_BOOL bFieldFull,
186 FX_BOOL& bRc) {
187 m_pEventHandler->OnField_Keystroke(
188 strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart, bShift,
189 pTarget, Value, bWillCommit, bFieldFull, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700190}
191
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192void CJS_Context::OnField_Validate(CFX_WideString& strChange,
193 const CFX_WideString& strChangeEx,
194 FX_BOOL bKeyDown,
195 FX_BOOL bModifier,
196 FX_BOOL bShift,
197 CPDF_FormField* pTarget,
198 CFX_WideString& Value,
199 FX_BOOL& bRc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier,
201 bShift, pTarget, Value, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700202}
203
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204void CJS_Context::OnScreen_Focus(FX_BOOL bModifier,
205 FX_BOOL bShift,
206 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700207 m_pEventHandler->OnScreen_Focus(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700208}
209
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210void CJS_Context::OnScreen_Blur(FX_BOOL bModifier,
211 FX_BOOL bShift,
212 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700213 m_pEventHandler->OnScreen_Blur(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700214}
215
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216void CJS_Context::OnScreen_Open(FX_BOOL bModifier,
217 FX_BOOL bShift,
218 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219 m_pEventHandler->OnScreen_Open(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700220}
221
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222void CJS_Context::OnScreen_Close(FX_BOOL bModifier,
223 FX_BOOL bShift,
224 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 m_pEventHandler->OnScreen_Close(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700226}
227
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228void CJS_Context::OnScreen_MouseDown(FX_BOOL bModifier,
229 FX_BOOL bShift,
230 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 m_pEventHandler->OnScreen_MouseDown(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700232}
233
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234void CJS_Context::OnScreen_MouseUp(FX_BOOL bModifier,
235 FX_BOOL bShift,
236 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 m_pEventHandler->OnScreen_MouseUp(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700238}
239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240void CJS_Context::OnScreen_MouseEnter(FX_BOOL bModifier,
241 FX_BOOL bShift,
242 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 m_pEventHandler->OnScreen_MouseEnter(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700244}
245
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246void CJS_Context::OnScreen_MouseExit(FX_BOOL bModifier,
247 FX_BOOL bShift,
248 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 m_pEventHandler->OnScreen_MouseExit(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250}
251
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252void CJS_Context::OnScreen_InView(FX_BOOL bModifier,
253 FX_BOOL bShift,
254 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255 m_pEventHandler->OnScreen_InView(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700256}
257
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258void CJS_Context::OnScreen_OutView(FX_BOOL bModifier,
259 FX_BOOL bShift,
260 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 m_pEventHandler->OnScreen_OutView(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262}
263
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264void CJS_Context::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 m_pEventHandler->OnBookmark_MouseUp(pBookMark);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700266}
267
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268void CJS_Context::OnLink_MouseUp(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 m_pEventHandler->OnLink_MouseUp(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700270}
271
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272void CJS_Context::OnConsole_Exec() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 m_pEventHandler->OnConsole_Exec();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700274}
275
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276void CJS_Context::OnExternal_Exec() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 m_pEventHandler->OnExternal_Exec();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700278}
279
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280void CJS_Context::OnBatchExec(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281 m_pEventHandler->OnBatchExec(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282}
283
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284void CJS_Context::OnMenu_Exec(CPDFSDK_Document* pTarget,
285 const CFX_WideString& strTargetName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 m_pEventHandler->OnMenu_Exec(pTarget, strTargetName);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287}