blob: e7896574693fbf0bb842fc42138632b507380bdf [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
7#include "../../include/javascript/JavaScript.h"
8#include "../../include/javascript/IJavaScript.h"
9//#include "../../include/javascript/JS_ResMgr.h"
10#include "../../include/javascript/JS_Context.h"
11#include "../../include/javascript/JS_EventHandler.h"
12#include "../../include/javascript/JS_Runtime.h"
13#include "../../include/javascript/resource.h"
14
15/* -------------------------- CJS_Context -------------------------- */
16
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070017CJS_Context::CJS_Context(CJS_Runtime* pRuntime) :
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018 m_pRuntime(pRuntime),
19 m_bBusy(FALSE),
20 m_bMsgBoxEnable(TRUE)
21{
22 m_pEventHandler = new CJS_EventHandler(this);
23}
24
25CJS_Context::~CJS_Context(void)
26{
Lei Zhang796c9082015-07-16 18:45:22 -070027 delete m_pEventHandler;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028}
29
30CPDFSDK_Document* CJS_Context::GetReaderDocument()
31{
32 ASSERT(m_pRuntime != NULL);
33
34 return m_pRuntime->GetReaderDocument();
35}
36
37CPDFDoc_Environment* CJS_Context::GetReaderApp()
38{
39 ASSERT(m_pRuntime != NULL);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070040
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041 return m_pRuntime->GetReaderApp();
42}
43
44FX_BOOL CJS_Context::DoJob(int nMode, const CFX_WideString& script, CFX_WideString& info)
45{
46 if (m_bBusy)
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070047 {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048 info = JSGetStringFromID(this, IDS_STRING_JSBUSY);
49 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070050 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
52 m_bBusy = TRUE;
53
54 ASSERT(m_pRuntime != NULL);
55 ASSERT(m_pEventHandler != NULL);
56 ASSERT(m_pEventHandler->IsValid());
57
58 if (!m_pRuntime->AddEventToLoop(m_pEventHandler->TargetName(), m_pEventHandler->EventType()))
59 {
60 info = JSGetStringFromID(this, IDS_STRING_JSEVENT);
61 return FALSE;
62 }
63
64 FXJSErr error ={NULL,NULL, 0};
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070065 int nRet = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066
Bo Xud4e406e2014-08-13 11:03:19 -070067 if (script.GetLength() > 0)
68 {
69 if (nMode == 0)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070 {
Tom Sepez4f7bc042015-04-27 12:06:58 -070071 nRet = JS_Execute(*m_pRuntime, this, script.c_str(), script.GetLength(), &error);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070072 }
Bo Xud4e406e2014-08-13 11:03:19 -070073 else
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074 {
Tom Sepez4f7bc042015-04-27 12:06:58 -070075 nRet = JS_Parse(*m_pRuntime, this, script.c_str(), script.GetLength(), &error);
Bo Xud4e406e2014-08-13 11:03:19 -070076 }
77 }
78
79 if (nRet < 0)
80 {
81 CFX_WideString sLine;
Bo Xuf13c1f32014-11-14 17:03:50 -080082 sLine.Format(L"[ Line: %05d { %s } ] : %s",error.linnum-1,error.srcline,error.message);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070083
84// TRACE(L"/* -------------- JS Error -------------- */\n");
85// TRACE(sLine);
86// TRACE(L"\n");
Bo Xud4e406e2014-08-13 11:03:19 -070087 //CFX_ByteString sTemp = CFX_ByteString::FromUnicode(error.message);
88 info += sLine;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089 }
Bo Xud4e406e2014-08-13 11:03:19 -070090 else
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091 {
Bo Xud4e406e2014-08-13 11:03:19 -070092 info = JSGetStringFromID(this, IDS_STRING_RUN);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093 }
94
95 m_pRuntime->RemoveEventInLoop(m_pEventHandler->TargetName(), m_pEventHandler->EventType());
96
97 m_pEventHandler->Destroy();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070098 m_bBusy = FALSE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099
100 return nRet >= 0;
101}
102
103FX_BOOL CJS_Context::RunScript(const CFX_WideString& script, CFX_WideString& info)
104{
105 v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
Bo Xufdc00a72014-10-28 23:03:33 -0700106 v8::Locker locker(m_pRuntime->GetIsolate());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700107 v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
108 v8::Local<v8::Context> context = m_pRuntime->NewJSContext();
109 v8::Context::Scope context_scope(context);
110
111 return DoJob(0, script, info);
112}
113
114FX_BOOL CJS_Context::Compile(const CFX_WideString& script, CFX_WideString& info)
115{
116 v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
117 v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
118 v8::Local<v8::Context> context = m_pRuntime->NewJSContext();
119 v8::Context::Scope context_scope(context);
120
121 return DoJob(1, script, info);
122}
123
124void CJS_Context::OnApp_Init()
125{
126 ASSERT(m_pEventHandler != NULL);
127 m_pEventHandler->OnApp_Init();
128}
129
130void CJS_Context::OnDoc_Open(CPDFSDK_Document* pDoc, const CFX_WideString &strTargetName)
131{
132 ASSERT(m_pEventHandler != NULL);
133 m_pEventHandler->OnDoc_Open(pDoc,strTargetName);
134}
135
136void CJS_Context::OnDoc_WillPrint(CPDFSDK_Document* pDoc)
137{
138 ASSERT(m_pEventHandler != NULL);
139 m_pEventHandler->OnDoc_WillPrint(pDoc);
140}
141
142void CJS_Context::OnDoc_DidPrint(CPDFSDK_Document* pDoc)
143{
144 ASSERT(m_pEventHandler != NULL);
145 m_pEventHandler->OnDoc_DidPrint(pDoc);
146}
147
148void CJS_Context::OnDoc_WillSave(CPDFSDK_Document* pDoc)
149{
150 ASSERT(m_pEventHandler != NULL);
151 m_pEventHandler->OnDoc_WillSave(pDoc);
152}
153
154void CJS_Context::OnDoc_DidSave(CPDFSDK_Document* pDoc)
155{
156 ASSERT(m_pEventHandler != NULL);
157 m_pEventHandler->OnDoc_DidSave(pDoc);
158}
159
160void CJS_Context::OnDoc_WillClose(CPDFSDK_Document* pDoc)
161{
162 ASSERT(m_pEventHandler != NULL);
163 m_pEventHandler->OnDoc_WillClose(pDoc);
164}
165
166void CJS_Context::OnPage_Open(CPDFSDK_Document* pTarget)
167{
168 ASSERT(m_pEventHandler != NULL);
169 m_pEventHandler->OnPage_Open(pTarget);
170}
171
172void CJS_Context::OnPage_Close(CPDFSDK_Document* pTarget)
173{
174 ASSERT(m_pEventHandler != NULL);
175 m_pEventHandler->OnPage_Close(pTarget);
176}
177
178void CJS_Context::OnPage_InView(CPDFSDK_Document* pTarget)
179{
180 ASSERT(m_pEventHandler != NULL);
181 m_pEventHandler->OnPage_InView(pTarget);
182}
183
184void CJS_Context::OnPage_OutView(CPDFSDK_Document* pTarget)
185{
186 ASSERT(m_pEventHandler != NULL);
187 m_pEventHandler->OnPage_OutView(pTarget);
188}
189
190void CJS_Context::OnField_MouseDown(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField *pTarget)
191{
192 ASSERT(m_pEventHandler != NULL);
193 m_pEventHandler->OnField_MouseDown(bModifier, bShift, pTarget);
194}
195
196void CJS_Context::OnField_MouseEnter(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField *pTarget)
197{
198 ASSERT(m_pEventHandler != NULL);
199 m_pEventHandler->OnField_MouseEnter(bModifier, bShift, pTarget);
200}
201
202void CJS_Context::OnField_MouseExit(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField *pTarget)
203{
204 ASSERT(m_pEventHandler != NULL);
205 m_pEventHandler->OnField_MouseExit(bModifier, bShift, pTarget);
206}
207
208void CJS_Context::OnField_MouseUp(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField *pTarget)
209{
210 ASSERT(m_pEventHandler != NULL);
211 m_pEventHandler->OnField_MouseUp(bModifier, bShift, pTarget);
212}
213
214void CJS_Context::OnField_Focus(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField* pTarget, const CFX_WideString& Value)
215{
216 ASSERT(m_pEventHandler != NULL);
217 m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value);
218}
219
220void CJS_Context::OnField_Blur(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField* pTarget, const CFX_WideString& Value)
221{
222 ASSERT(m_pEventHandler != NULL);
223 m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value);
224}
225
226void CJS_Context::OnField_Calculate(CPDF_FormField* pSource, CPDF_FormField* pTarget, CFX_WideString& Value, FX_BOOL& bRc)
227{
228 ASSERT(m_pEventHandler != NULL);
229 m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc);
230}
231
Lei Zhang0aa0e732015-06-10 15:23:23 -0700232void CJS_Context::OnField_Format(CPDF_FormField* pTarget, CFX_WideString& Value, FX_BOOL bWillCommit)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233{
Lei Zhang0aa0e732015-06-10 15:23:23 -0700234 m_pEventHandler->OnField_Format(pTarget, Value, bWillCommit);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235}
236
237
Lei Zhang0aa0e732015-06-10 15:23:23 -0700238void CJS_Context::OnField_Keystroke(CFX_WideString& strChange, const CFX_WideString& strChangeEx,
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239 FX_BOOL bKeyDown, FX_BOOL bModifier, int &nSelEnd,int &nSelStart,
240 FX_BOOL bShift, CPDF_FormField* pTarget, CFX_WideString& Value,
241 FX_BOOL bWillCommit, FX_BOOL bFieldFull, FX_BOOL& bRc)
242{
Lei Zhang0aa0e732015-06-10 15:23:23 -0700243 m_pEventHandler->OnField_Keystroke(
244 strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart,
245 bShift, pTarget, Value, bWillCommit, bFieldFull, bRc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246}
247
248void CJS_Context::OnField_Validate(CFX_WideString& strChange,const CFX_WideString& strChangeEx,
249 FX_BOOL bKeyDown, FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField* pTarget,
250 CFX_WideString& Value, FX_BOOL& bRc)
251{
252 ASSERT(m_pEventHandler != NULL);
253 m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier, bShift, pTarget, Value, bRc);
254}
255
256void CJS_Context::OnScreen_Focus(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
257{
258 ASSERT(m_pEventHandler != NULL);
259 m_pEventHandler->OnScreen_Focus(bModifier, bShift, pScreen);
260}
261
262void CJS_Context::OnScreen_Blur(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
263{
264 ASSERT(m_pEventHandler != NULL);
265 m_pEventHandler->OnScreen_Blur(bModifier, bShift, pScreen);
266}
267
268void CJS_Context::OnScreen_Open(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
269{
270 ASSERT(m_pEventHandler != NULL);
271 m_pEventHandler->OnScreen_Open(bModifier, bShift, pScreen);
272}
273
274void CJS_Context::OnScreen_Close(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
275{
276 ASSERT(m_pEventHandler != NULL);
277 m_pEventHandler->OnScreen_Close(bModifier, bShift, pScreen);
278}
279
280void CJS_Context::OnScreen_MouseDown(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
281{
282 ASSERT(m_pEventHandler != NULL);
283 m_pEventHandler->OnScreen_MouseDown(bModifier, bShift, pScreen);
284}
285
286void CJS_Context::OnScreen_MouseUp(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
287{
288 ASSERT(m_pEventHandler != NULL);
289 m_pEventHandler->OnScreen_MouseUp(bModifier, bShift, pScreen);
290}
291
292void CJS_Context::OnScreen_MouseEnter(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
293{
294 ASSERT(m_pEventHandler != NULL);
295 m_pEventHandler->OnScreen_MouseEnter(bModifier, bShift, pScreen);
296}
297
298void CJS_Context::OnScreen_MouseExit(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
299{
300 ASSERT(m_pEventHandler != NULL);
301 m_pEventHandler->OnScreen_MouseExit(bModifier, bShift, pScreen);
302}
303
304void CJS_Context::OnScreen_InView(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
305{
306 ASSERT(m_pEventHandler != NULL);
307 m_pEventHandler->OnScreen_InView(bModifier, bShift, pScreen);
308}
309
310void CJS_Context::OnScreen_OutView(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
311{
312 ASSERT(m_pEventHandler != NULL);
313 m_pEventHandler->OnScreen_OutView(bModifier, bShift, pScreen);
314}
315
316void CJS_Context::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark)
317{
318 ASSERT(m_pEventHandler != NULL);
319 m_pEventHandler->OnBookmark_MouseUp(pBookMark);
320}
321
322void CJS_Context::OnLink_MouseUp(CPDFSDK_Document* pTarget)
323{
324 ASSERT(m_pEventHandler != NULL);
325 m_pEventHandler->OnLink_MouseUp(pTarget);
326}
327
328void CJS_Context::OnConsole_Exec()
329{
330 ASSERT(m_pEventHandler != NULL);
331 m_pEventHandler->OnConsole_Exec();
332}
333
334void CJS_Context::OnExternal_Exec()
335{
336 ASSERT(m_pEventHandler != NULL);
337 m_pEventHandler->OnExternal_Exec();
338}
339
340void CJS_Context::OnBatchExec(CPDFSDK_Document* pTarget)
341{
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700342 ASSERT(m_pEventHandler != NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700343 m_pEventHandler->OnBatchExec(pTarget);
344}
345
346void CJS_Context::OnMenu_Exec(CPDFSDK_Document* pTarget,const CFX_WideString& strTargetName)
347{
348 ASSERT(m_pEventHandler != NULL);
349 m_pEventHandler->OnMenu_Exec(pTarget, strTargetName);
350}
351