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