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