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