blob: 3c9be58013b130495456e12865aedb6ae608d1d0 [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());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 if (!m_pRuntime->AddEventToLoop(m_pEventHandler->TargetName(),
51 m_pEventHandler->EventType())) {
52 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 Sepez808a99e2015-09-10 12:28:37 -070059 nRet = JS_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
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 m_pRuntime->RemoveEventInLoop(m_pEventHandler->TargetName(),
73 m_pEventHandler->EventType());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 m_pEventHandler->Destroy();
76 m_bBusy = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 return nRet >= 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079}
80
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081void CJS_Context::OnApp_Init() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 m_pEventHandler->OnApp_Init();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070083}
84
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085void CJS_Context::OnDoc_Open(CPDFSDK_Document* pDoc,
86 const CFX_WideString& strTargetName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 m_pEventHandler->OnDoc_Open(pDoc, strTargetName);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088}
89
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090void CJS_Context::OnDoc_WillPrint(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 m_pEventHandler->OnDoc_WillPrint(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092}
93
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094void CJS_Context::OnDoc_DidPrint(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 m_pEventHandler->OnDoc_DidPrint(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096}
97
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098void CJS_Context::OnDoc_WillSave(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 m_pEventHandler->OnDoc_WillSave(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100}
101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102void CJS_Context::OnDoc_DidSave(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 m_pEventHandler->OnDoc_DidSave(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700104}
105
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106void CJS_Context::OnDoc_WillClose(CPDFSDK_Document* pDoc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 m_pEventHandler->OnDoc_WillClose(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108}
109
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110void CJS_Context::OnPage_Open(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 m_pEventHandler->OnPage_Open(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112}
113
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114void CJS_Context::OnPage_Close(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 m_pEventHandler->OnPage_Close(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}
117
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118void CJS_Context::OnPage_InView(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 m_pEventHandler->OnPage_InView(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120}
121
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122void CJS_Context::OnPage_OutView(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123 m_pEventHandler->OnPage_OutView(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124}
125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126void CJS_Context::OnField_MouseDown(FX_BOOL bModifier,
127 FX_BOOL bShift,
128 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129 m_pEventHandler->OnField_MouseDown(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700130}
131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132void CJS_Context::OnField_MouseEnter(FX_BOOL bModifier,
133 FX_BOOL bShift,
134 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135 m_pEventHandler->OnField_MouseEnter(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700136}
137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138void CJS_Context::OnField_MouseExit(FX_BOOL bModifier,
139 FX_BOOL bShift,
140 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700141 m_pEventHandler->OnField_MouseExit(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700142}
143
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144void CJS_Context::OnField_MouseUp(FX_BOOL bModifier,
145 FX_BOOL bShift,
146 CPDF_FormField* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 m_pEventHandler->OnField_MouseUp(bModifier, bShift, pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700148}
149
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150void CJS_Context::OnField_Focus(FX_BOOL bModifier,
151 FX_BOOL bShift,
152 CPDF_FormField* pTarget,
153 const CFX_WideString& Value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700155}
156
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157void CJS_Context::OnField_Blur(FX_BOOL bModifier,
158 FX_BOOL bShift,
159 CPDF_FormField* pTarget,
160 const CFX_WideString& Value) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162}
163
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164void CJS_Context::OnField_Calculate(CPDF_FormField* pSource,
165 CPDF_FormField* pTarget,
166 CFX_WideString& Value,
167 FX_BOOL& bRc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700169}
170
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171void CJS_Context::OnField_Format(CPDF_FormField* pTarget,
172 CFX_WideString& Value,
173 FX_BOOL bWillCommit) {
174 m_pEventHandler->OnField_Format(pTarget, Value, bWillCommit);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175}
176
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177void CJS_Context::OnField_Keystroke(CFX_WideString& strChange,
178 const CFX_WideString& strChangeEx,
179 FX_BOOL bKeyDown,
180 FX_BOOL bModifier,
181 int& nSelEnd,
182 int& nSelStart,
183 FX_BOOL bShift,
184 CPDF_FormField* pTarget,
185 CFX_WideString& Value,
186 FX_BOOL bWillCommit,
187 FX_BOOL bFieldFull,
188 FX_BOOL& bRc) {
189 m_pEventHandler->OnField_Keystroke(
190 strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart, bShift,
191 pTarget, Value, bWillCommit, bFieldFull, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192}
193
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194void CJS_Context::OnField_Validate(CFX_WideString& strChange,
195 const CFX_WideString& strChangeEx,
196 FX_BOOL bKeyDown,
197 FX_BOOL bModifier,
198 FX_BOOL bShift,
199 CPDF_FormField* pTarget,
200 CFX_WideString& Value,
201 FX_BOOL& bRc) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier,
203 bShift, pTarget, Value, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700204}
205
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206void CJS_Context::OnScreen_Focus(FX_BOOL bModifier,
207 FX_BOOL bShift,
208 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 m_pEventHandler->OnScreen_Focus(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210}
211
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212void CJS_Context::OnScreen_Blur(FX_BOOL bModifier,
213 FX_BOOL bShift,
214 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 m_pEventHandler->OnScreen_Blur(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700216}
217
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218void CJS_Context::OnScreen_Open(FX_BOOL bModifier,
219 FX_BOOL bShift,
220 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 m_pEventHandler->OnScreen_Open(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700222}
223
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700224void CJS_Context::OnScreen_Close(FX_BOOL bModifier,
225 FX_BOOL bShift,
226 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 m_pEventHandler->OnScreen_Close(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700228}
229
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700230void CJS_Context::OnScreen_MouseDown(FX_BOOL bModifier,
231 FX_BOOL bShift,
232 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 m_pEventHandler->OnScreen_MouseDown(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700234}
235
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700236void CJS_Context::OnScreen_MouseUp(FX_BOOL bModifier,
237 FX_BOOL bShift,
238 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239 m_pEventHandler->OnScreen_MouseUp(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700240}
241
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242void CJS_Context::OnScreen_MouseEnter(FX_BOOL bModifier,
243 FX_BOOL bShift,
244 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 m_pEventHandler->OnScreen_MouseEnter(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246}
247
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248void CJS_Context::OnScreen_MouseExit(FX_BOOL bModifier,
249 FX_BOOL bShift,
250 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700251 m_pEventHandler->OnScreen_MouseExit(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252}
253
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254void CJS_Context::OnScreen_InView(FX_BOOL bModifier,
255 FX_BOOL bShift,
256 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 m_pEventHandler->OnScreen_InView(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700258}
259
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700260void CJS_Context::OnScreen_OutView(FX_BOOL bModifier,
261 FX_BOOL bShift,
262 CPDFSDK_Annot* pScreen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700263 m_pEventHandler->OnScreen_OutView(bModifier, bShift, pScreen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264}
265
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266void CJS_Context::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 m_pEventHandler->OnBookmark_MouseUp(pBookMark);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268}
269
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270void CJS_Context::OnLink_MouseUp(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 m_pEventHandler->OnLink_MouseUp(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700272}
273
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274void CJS_Context::OnConsole_Exec() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275 m_pEventHandler->OnConsole_Exec();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700276}
277
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278void CJS_Context::OnExternal_Exec() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700279 m_pEventHandler->OnExternal_Exec();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700280}
281
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700282void CJS_Context::OnBatchExec(CPDFSDK_Document* pTarget) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 m_pEventHandler->OnBatchExec(pTarget);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700284}
285
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286void CJS_Context::OnMenu_Exec(CPDFSDK_Document* pTarget,
287 const CFX_WideString& strTargetName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 m_pEventHandler->OnMenu_Exec(pTarget, strTargetName);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700289}