blob: 50f9f3cbb0f42acc45179b9d88ededc9c3b7bece [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
Bo Xud4e406e2014-08-13 11:03:19 -070071 if (script.GetLength() > 0)
72 {
73 if (nMode == 0)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074 {
Tom Sepez4f7bc042015-04-27 12:06:58 -070075 nRet = JS_Execute(*m_pRuntime, this, script.c_str(), script.GetLength(), &error);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076 }
Bo Xud4e406e2014-08-13 11:03:19 -070077 else
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078 {
Tom Sepez4f7bc042015-04-27 12:06:58 -070079 nRet = JS_Parse(*m_pRuntime, this, script.c_str(), script.GetLength(), &error);
Bo Xud4e406e2014-08-13 11:03:19 -070080 }
81 }
82
83 if (nRet < 0)
84 {
85 CFX_WideString sLine;
Bo Xuf13c1f32014-11-14 17:03:50 -080086 sLine.Format(L"[ Line: %05d { %s } ] : %s",error.linnum-1,error.srcline,error.message);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087
88// TRACE(L"/* -------------- JS Error -------------- */\n");
89// TRACE(sLine);
90// TRACE(L"\n");
Bo Xud4e406e2014-08-13 11:03:19 -070091 //CFX_ByteString sTemp = CFX_ByteString::FromUnicode(error.message);
92 info += sLine;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093 }
Bo Xud4e406e2014-08-13 11:03:19 -070094 else
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095 {
Bo Xud4e406e2014-08-13 11:03:19 -070096 info = JSGetStringFromID(this, IDS_STRING_RUN);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097 }
98
99 m_pRuntime->RemoveEventInLoop(m_pEventHandler->TargetName(), m_pEventHandler->EventType());
100
101 m_pEventHandler->Destroy();
102 m_bBusy = FALSE;
103
104 return nRet >= 0;
105}
106
107FX_BOOL CJS_Context::RunScript(const CFX_WideString& script, CFX_WideString& info)
108{
109 v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
Bo Xufdc00a72014-10-28 23:03:33 -0700110 v8::Locker locker(m_pRuntime->GetIsolate());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700111 v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
112 v8::Local<v8::Context> context = m_pRuntime->NewJSContext();
113 v8::Context::Scope context_scope(context);
114
115 return DoJob(0, script, info);
116}
117
118FX_BOOL CJS_Context::Compile(const CFX_WideString& script, CFX_WideString& info)
119{
120 v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
121 v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
122 v8::Local<v8::Context> context = m_pRuntime->NewJSContext();
123 v8::Context::Scope context_scope(context);
124
125 return DoJob(1, script, info);
126}
127
128void CJS_Context::OnApp_Init()
129{
130 ASSERT(m_pEventHandler != NULL);
131 m_pEventHandler->OnApp_Init();
132}
133
134void CJS_Context::OnDoc_Open(CPDFSDK_Document* pDoc, const CFX_WideString &strTargetName)
135{
136 ASSERT(m_pEventHandler != NULL);
137 m_pEventHandler->OnDoc_Open(pDoc,strTargetName);
138}
139
140void CJS_Context::OnDoc_WillPrint(CPDFSDK_Document* pDoc)
141{
142 ASSERT(m_pEventHandler != NULL);
143 m_pEventHandler->OnDoc_WillPrint(pDoc);
144}
145
146void CJS_Context::OnDoc_DidPrint(CPDFSDK_Document* pDoc)
147{
148 ASSERT(m_pEventHandler != NULL);
149 m_pEventHandler->OnDoc_DidPrint(pDoc);
150}
151
152void CJS_Context::OnDoc_WillSave(CPDFSDK_Document* pDoc)
153{
154 ASSERT(m_pEventHandler != NULL);
155 m_pEventHandler->OnDoc_WillSave(pDoc);
156}
157
158void CJS_Context::OnDoc_DidSave(CPDFSDK_Document* pDoc)
159{
160 ASSERT(m_pEventHandler != NULL);
161 m_pEventHandler->OnDoc_DidSave(pDoc);
162}
163
164void CJS_Context::OnDoc_WillClose(CPDFSDK_Document* pDoc)
165{
166 ASSERT(m_pEventHandler != NULL);
167 m_pEventHandler->OnDoc_WillClose(pDoc);
168}
169
170void CJS_Context::OnPage_Open(CPDFSDK_Document* pTarget)
171{
172 ASSERT(m_pEventHandler != NULL);
173 m_pEventHandler->OnPage_Open(pTarget);
174}
175
176void CJS_Context::OnPage_Close(CPDFSDK_Document* pTarget)
177{
178 ASSERT(m_pEventHandler != NULL);
179 m_pEventHandler->OnPage_Close(pTarget);
180}
181
182void CJS_Context::OnPage_InView(CPDFSDK_Document* pTarget)
183{
184 ASSERT(m_pEventHandler != NULL);
185 m_pEventHandler->OnPage_InView(pTarget);
186}
187
188void CJS_Context::OnPage_OutView(CPDFSDK_Document* pTarget)
189{
190 ASSERT(m_pEventHandler != NULL);
191 m_pEventHandler->OnPage_OutView(pTarget);
192}
193
194void CJS_Context::OnField_MouseDown(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField *pTarget)
195{
196 ASSERT(m_pEventHandler != NULL);
197 m_pEventHandler->OnField_MouseDown(bModifier, bShift, pTarget);
198}
199
200void CJS_Context::OnField_MouseEnter(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField *pTarget)
201{
202 ASSERT(m_pEventHandler != NULL);
203 m_pEventHandler->OnField_MouseEnter(bModifier, bShift, pTarget);
204}
205
206void CJS_Context::OnField_MouseExit(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField *pTarget)
207{
208 ASSERT(m_pEventHandler != NULL);
209 m_pEventHandler->OnField_MouseExit(bModifier, bShift, pTarget);
210}
211
212void CJS_Context::OnField_MouseUp(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField *pTarget)
213{
214 ASSERT(m_pEventHandler != NULL);
215 m_pEventHandler->OnField_MouseUp(bModifier, bShift, pTarget);
216}
217
218void CJS_Context::OnField_Focus(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField* pTarget, const CFX_WideString& Value)
219{
220 ASSERT(m_pEventHandler != NULL);
221 m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value);
222}
223
224void CJS_Context::OnField_Blur(FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField* pTarget, const CFX_WideString& Value)
225{
226 ASSERT(m_pEventHandler != NULL);
227 m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value);
228}
229
230void CJS_Context::OnField_Calculate(CPDF_FormField* pSource, CPDF_FormField* pTarget, CFX_WideString& Value, FX_BOOL& bRc)
231{
232 ASSERT(m_pEventHandler != NULL);
233 m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc);
234}
235
236void CJS_Context::OnField_Format(int nCommitKey, CPDF_FormField* pTarget, CFX_WideString& Value, FX_BOOL bWillCommit)
237{
238 ASSERT(m_pEventHandler != NULL);
239 m_pEventHandler->OnField_Format(nCommitKey, pTarget, Value, bWillCommit);
240}
241
242
243void CJS_Context::OnField_Keystroke(int nCommitKey, CFX_WideString& strChange, const CFX_WideString& strChangeEx,
244 FX_BOOL bKeyDown, FX_BOOL bModifier, int &nSelEnd,int &nSelStart,
245 FX_BOOL bShift, CPDF_FormField* pTarget, CFX_WideString& Value,
246 FX_BOOL bWillCommit, FX_BOOL bFieldFull, FX_BOOL& bRc)
247{
248 ASSERT(m_pEventHandler != NULL);
249 m_pEventHandler->OnField_Keystroke(nCommitKey, strChange, strChangeEx, bKeyDown,
250 bModifier, nSelEnd, nSelStart, bShift, pTarget, Value, bWillCommit, bFieldFull, bRc);
251}
252
253void CJS_Context::OnField_Validate(CFX_WideString& strChange,const CFX_WideString& strChangeEx,
254 FX_BOOL bKeyDown, FX_BOOL bModifier, FX_BOOL bShift, CPDF_FormField* pTarget,
255 CFX_WideString& Value, FX_BOOL& bRc)
256{
257 ASSERT(m_pEventHandler != NULL);
258 m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier, bShift, pTarget, Value, bRc);
259}
260
261void CJS_Context::OnScreen_Focus(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
262{
263 ASSERT(m_pEventHandler != NULL);
264 m_pEventHandler->OnScreen_Focus(bModifier, bShift, pScreen);
265}
266
267void CJS_Context::OnScreen_Blur(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
268{
269 ASSERT(m_pEventHandler != NULL);
270 m_pEventHandler->OnScreen_Blur(bModifier, bShift, pScreen);
271}
272
273void CJS_Context::OnScreen_Open(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
274{
275 ASSERT(m_pEventHandler != NULL);
276 m_pEventHandler->OnScreen_Open(bModifier, bShift, pScreen);
277}
278
279void CJS_Context::OnScreen_Close(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
280{
281 ASSERT(m_pEventHandler != NULL);
282 m_pEventHandler->OnScreen_Close(bModifier, bShift, pScreen);
283}
284
285void CJS_Context::OnScreen_MouseDown(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
286{
287 ASSERT(m_pEventHandler != NULL);
288 m_pEventHandler->OnScreen_MouseDown(bModifier, bShift, pScreen);
289}
290
291void CJS_Context::OnScreen_MouseUp(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
292{
293 ASSERT(m_pEventHandler != NULL);
294 m_pEventHandler->OnScreen_MouseUp(bModifier, bShift, pScreen);
295}
296
297void CJS_Context::OnScreen_MouseEnter(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
298{
299 ASSERT(m_pEventHandler != NULL);
300 m_pEventHandler->OnScreen_MouseEnter(bModifier, bShift, pScreen);
301}
302
303void CJS_Context::OnScreen_MouseExit(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
304{
305 ASSERT(m_pEventHandler != NULL);
306 m_pEventHandler->OnScreen_MouseExit(bModifier, bShift, pScreen);
307}
308
309void CJS_Context::OnScreen_InView(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
310{
311 ASSERT(m_pEventHandler != NULL);
312 m_pEventHandler->OnScreen_InView(bModifier, bShift, pScreen);
313}
314
315void CJS_Context::OnScreen_OutView(FX_BOOL bModifier, FX_BOOL bShift, CPDFSDK_Annot* pScreen)
316{
317 ASSERT(m_pEventHandler != NULL);
318 m_pEventHandler->OnScreen_OutView(bModifier, bShift, pScreen);
319}
320
321void CJS_Context::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark)
322{
323 ASSERT(m_pEventHandler != NULL);
324 m_pEventHandler->OnBookmark_MouseUp(pBookMark);
325}
326
327void CJS_Context::OnLink_MouseUp(CPDFSDK_Document* pTarget)
328{
329 ASSERT(m_pEventHandler != NULL);
330 m_pEventHandler->OnLink_MouseUp(pTarget);
331}
332
333void CJS_Context::OnConsole_Exec()
334{
335 ASSERT(m_pEventHandler != NULL);
336 m_pEventHandler->OnConsole_Exec();
337}
338
339void CJS_Context::OnExternal_Exec()
340{
341 ASSERT(m_pEventHandler != NULL);
342 m_pEventHandler->OnExternal_Exec();
343}
344
345void CJS_Context::OnBatchExec(CPDFSDK_Document* pTarget)
346{
347 ASSERT(m_pEventHandler != NULL);
348 m_pEventHandler->OnBatchExec(pTarget);
349}
350
351void CJS_Context::OnMenu_Exec(CPDFSDK_Document* pTarget,const CFX_WideString& strTargetName)
352{
353 ASSERT(m_pEventHandler != NULL);
354 m_pEventHandler->OnMenu_Exec(pTarget, strTargetName);
355}
356