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