blob: e34ffb51baa82db2c82841d7215a79ff36dc12fe [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.
4
5// 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
17CJS_Context::CJS_Context(CJS_Runtime* pRuntime) :
18 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{
27 if (m_pEventHandler)
28 {
29 delete m_pEventHandler;
30 m_pEventHandler = NULL;
31 }
32}
33
34CPDFSDK_Document* CJS_Context::GetReaderDocument()
35{
36 ASSERT(m_pRuntime != NULL);
37
38 return m_pRuntime->GetReaderDocument();
39}
40
41CPDFDoc_Environment* CJS_Context::GetReaderApp()
42{
43 ASSERT(m_pRuntime != NULL);
44
45 return m_pRuntime->GetReaderApp();
46}
47
48FX_BOOL CJS_Context::DoJob(int nMode, const CFX_WideString& script, CFX_WideString& info)
49{
50 if (m_bBusy)
51 {
52 info = JSGetStringFromID(this, IDS_STRING_JSBUSY);
53 return FALSE;
54 }
55
56 m_bBusy = TRUE;
57
58 ASSERT(m_pRuntime != NULL);
59 ASSERT(m_pEventHandler != NULL);
60 ASSERT(m_pEventHandler->IsValid());
61
62 if (!m_pRuntime->AddEventToLoop(m_pEventHandler->TargetName(), m_pEventHandler->EventType()))
63 {
64 info = JSGetStringFromID(this, IDS_STRING_JSEVENT);
65 return FALSE;
66 }
67
68 FXJSErr error ={NULL,NULL, 0};
69 int nRet = 0;
70
71 try
72 {
73 if (script.GetLength() > 0)
74 {
75 if (nMode == 0)
76 {
77 nRet = JS_Execute(*m_pRuntime, this, script, script.GetLength(), &error);
78 }
79 else
80 {
81 nRet = JS_Parse(*m_pRuntime, this, script, script.GetLength(), &error);
82 }
83 }
84
85 if (nRet < 0)
86 {
87 CFX_WideString sLine;
88 sLine.Format((FX_LPCWSTR)L"[ Line: %05d { %s } ] : %s",error.linnum-1,error.srcline,error.message);
89
90// TRACE(L"/* -------------- JS Error -------------- */\n");
91// TRACE(sLine);
92// TRACE(L"\n");
93 //CFX_ByteString sTemp = CFX_ByteString::FromUnicode(error.message);
94 info += sLine;
95 }
96 else
97 {
98 info = JSGetStringFromID(this, IDS_STRING_RUN);
99 }
100
101 }
102 catch (...)
103 {
104 info = JSGetStringFromID(this, IDS_STRING_UNHANDLED);
105 nRet = -1;
106 }
107
108 m_pRuntime->RemoveEventInLoop(m_pEventHandler->TargetName(), m_pEventHandler->EventType());
109
110 m_pEventHandler->Destroy();
111 m_bBusy = FALSE;
112
113 return nRet >= 0;
114}
115
116FX_BOOL CJS_Context::RunScript(const CFX_WideString& script, CFX_WideString& info)
117{
118 v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
119 v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
120 v8::Local<v8::Context> context = m_pRuntime->NewJSContext();
121 v8::Context::Scope context_scope(context);
122
123 return DoJob(0, script, info);
124}
125
126FX_BOOL CJS_Context::Compile(const CFX_WideString& script, CFX_WideString& info)
127{
128 v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
129 v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
130 v8::Local<v8::Context> context = m_pRuntime->NewJSContext();
131 v8::Context::Scope context_scope(context);
132
133 return DoJob(1, script, info);
134}
135
136void CJS_Context::OnApp_Init()
137{
138 ASSERT(m_pEventHandler != NULL);
139 m_pEventHandler->OnApp_Init();
140}
141
142void CJS_Context::OnDoc_Open(CPDFSDK_Document* pDoc, const CFX_WideString &strTargetName)
143{
144 ASSERT(m_pEventHandler != NULL);
145 m_pEventHandler->OnDoc_Open(pDoc,strTargetName);
146}
147
148void CJS_Context::OnDoc_WillPrint(CPDFSDK_Document* pDoc)
149{
150 ASSERT(m_pEventHandler != NULL);
151 m_pEventHandler->OnDoc_WillPrint(pDoc);
152}
153
154void CJS_Context::OnDoc_DidPrint(CPDFSDK_Document* pDoc)
155{
156 ASSERT(m_pEventHandler != NULL);
157 m_pEventHandler->OnDoc_DidPrint(pDoc);
158}
159
160void CJS_Context::OnDoc_WillSave(CPDFSDK_Document* pDoc)
161{
162 ASSERT(m_pEventHandler != NULL);
163 m_pEventHandler->OnDoc_WillSave(pDoc);
164}
165
166void CJS_Context::OnDoc_DidSave(CPDFSDK_Document* pDoc)
167{
168 ASSERT(m_pEventHandler != NULL);
169 m_pEventHandler->OnDoc_DidSave(pDoc);
170}
171
172void CJS_Context::OnDoc_WillClose(CPDFSDK_Document* pDoc)
173{
174 ASSERT(m_pEventHandler != NULL);
175 m_pEventHandler->OnDoc_WillClose(pDoc);
176}
177
178void CJS_Context::OnPage_Open(CPDFSDK_Document* pTarget)
179{
180 ASSERT(m_pEventHandler != NULL);
181 m_pEventHandler->OnPage_Open(pTarget);
182}
183
184void CJS_Context::OnPage_Close(CPDFSDK_Document* pTarget)
185{
186 ASSERT(m_pEventHandler != NULL);
187 m_pEventHandler->OnPage_Close(pTarget);
188}
189
190void CJS_Context::OnPage_InView(CPDFSDK_Document* pTarget)
191{
192 ASSERT(m_pEventHandler != NULL);
193 m_pEventHandler->OnPage_InView(pTarget);
194}
195
196void CJS_Context::OnPage_OutView(CPDFSDK_Document* pTarget)
197{
198 ASSERT(m_pEventHandler != NULL);
199 m_pEventHandler->OnPage_OutView(pTarget);
200}
201
202void CJS_Context::OnField_MouseDown(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField *pTarget)
203{
204 ASSERT(m_pEventHandler != NULL);
205 m_pEventHandler->OnField_MouseDown(bModifier, bShift, pTarget);
206}
207
208void CJS_Context::OnField_MouseEnter(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField *pTarget)
209{
210 ASSERT(m_pEventHandler != NULL);
211 m_pEventHandler->OnField_MouseEnter(bModifier, bShift, pTarget);
212}
213
214void CJS_Context::OnField_MouseExit(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField *pTarget)
215{
216 ASSERT(m_pEventHandler != NULL);
217 m_pEventHandler->OnField_MouseExit(bModifier, bShift, pTarget);
218}
219
220void CJS_Context::OnField_MouseUp(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField *pTarget)
221{
222 ASSERT(m_pEventHandler != NULL);
223 m_pEventHandler->OnField_MouseUp(bModifier, bShift, pTarget);
224}
225
226void CJS_Context::OnField_Focus(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField* pTarget, const CFX_WideString& Value)
227{
228 ASSERT(m_pEventHandler != NULL);
229 m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value);
230}
231
232void CJS_Context::OnField_Blur(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField* pTarget, const CFX_WideString& Value)
233{
234 ASSERT(m_pEventHandler != NULL);
235 m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value);
236}
237
238void CJS_Context::OnField_Calculate(CPDF_FormField* pSource, CPDF_FormField* pTarget, CFX_WideString& Value, FX_BOOL& bRc)
239{
240 ASSERT(m_pEventHandler != NULL);
241 m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc);
242}
243
244void CJS_Context::OnField_Format(int nCommitKey, CPDF_FormField* pTarget, CFX_WideString& Value, FX_BOOL bWillCommit)
245{
246 ASSERT(m_pEventHandler != NULL);
247 m_pEventHandler->OnField_Format(nCommitKey, pTarget, Value, bWillCommit);
248}
249
250
251void CJS_Context::OnField_Keystroke(int nCommitKey, CFX_WideString& strChange, const CFX_WideString& strChangeEx,
252 FX_BOOL bKeyDown, FX_BOOL bModifier, int &nSelEnd,int &nSelStart,
253 FX_BOOL bShift, CPDF_FormField* pTarget, CFX_WideString& Value,
254 FX_BOOL bWillCommit, FX_BOOL bFieldFull, FX_BOOL& bRc)
255{
256 ASSERT(m_pEventHandler != NULL);
257 m_pEventHandler->OnField_Keystroke(nCommitKey, strChange, strChangeEx, bKeyDown,
258 bModifier, nSelEnd, nSelStart, bShift, pTarget, Value, bWillCommit, bFieldFull, bRc);
259}
260
261void CJS_Context::OnField_Validate(CFX_WideString& strChange,const CFX_WideString& strChangeEx,
262 FX_BOOL bKeyDown, FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField* pTarget,
263 CFX_WideString& Value, FX_BOOL& bRc)
264{
265 ASSERT(m_pEventHandler != NULL);
266 m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier, bShift, pTarget, Value, bRc);
267}
268
269void CJS_Context::OnScreen_Focus(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
270{
271 ASSERT(m_pEventHandler != NULL);
272 m_pEventHandler->OnScreen_Focus(bModifier, bShift, pScreen);
273}
274
275void CJS_Context::OnScreen_Blur(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
276{
277 ASSERT(m_pEventHandler != NULL);
278 m_pEventHandler->OnScreen_Blur(bModifier, bShift, pScreen);
279}
280
281void CJS_Context::OnScreen_Open(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
282{
283 ASSERT(m_pEventHandler != NULL);
284 m_pEventHandler->OnScreen_Open(bModifier, bShift, pScreen);
285}
286
287void CJS_Context::OnScreen_Close(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
288{
289 ASSERT(m_pEventHandler != NULL);
290 m_pEventHandler->OnScreen_Close(bModifier, bShift, pScreen);
291}
292
293void CJS_Context::OnScreen_MouseDown(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
294{
295 ASSERT(m_pEventHandler != NULL);
296 m_pEventHandler->OnScreen_MouseDown(bModifier, bShift, pScreen);
297}
298
299void CJS_Context::OnScreen_MouseUp(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
300{
301 ASSERT(m_pEventHandler != NULL);
302 m_pEventHandler->OnScreen_MouseUp(bModifier, bShift, pScreen);
303}
304
305void CJS_Context::OnScreen_MouseEnter(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
306{
307 ASSERT(m_pEventHandler != NULL);
308 m_pEventHandler->OnScreen_MouseEnter(bModifier, bShift, pScreen);
309}
310
311void CJS_Context::OnScreen_MouseExit(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
312{
313 ASSERT(m_pEventHandler != NULL);
314 m_pEventHandler->OnScreen_MouseExit(bModifier, bShift, pScreen);
315}
316
317void CJS_Context::OnScreen_InView(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
318{
319 ASSERT(m_pEventHandler != NULL);
320 m_pEventHandler->OnScreen_InView(bModifier, bShift, pScreen);
321}
322
323void CJS_Context::OnScreen_OutView(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
324{
325 ASSERT(m_pEventHandler != NULL);
326 m_pEventHandler->OnScreen_OutView(bModifier, bShift, pScreen);
327}
328
329void CJS_Context::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark)
330{
331 ASSERT(m_pEventHandler != NULL);
332 m_pEventHandler->OnBookmark_MouseUp(pBookMark);
333}
334
335void CJS_Context::OnLink_MouseUp(CPDFSDK_Document* pTarget)
336{
337 ASSERT(m_pEventHandler != NULL);
338 m_pEventHandler->OnLink_MouseUp(pTarget);
339}
340
341void CJS_Context::OnConsole_Exec()
342{
343 ASSERT(m_pEventHandler != NULL);
344 m_pEventHandler->OnConsole_Exec();
345}
346
347void CJS_Context::OnExternal_Exec()
348{
349 ASSERT(m_pEventHandler != NULL);
350 m_pEventHandler->OnExternal_Exec();
351}
352
353void CJS_Context::OnBatchExec(CPDFSDK_Document* pTarget)
354{
355 ASSERT(m_pEventHandler != NULL);
356 m_pEventHandler->OnBatchExec(pTarget);
357}
358
359void CJS_Context::OnMenu_Exec(CPDFSDK_Document* pTarget,const CFX_WideString& strTargetName)
360{
361 ASSERT(m_pEventHandler != NULL);
362 m_pEventHandler->OnMenu_Exec(pTarget, strTargetName);
363}
364