Use std::set<> to track active event handlers.
[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/IJavaScript.h"
8 #include "../../include/javascript/JS_Context.h"
9 #include "../../include/javascript/JS_EventHandler.h"
10 #include "../../include/javascript/JS_Runtime.h"
11 #include "../../include/javascript/JavaScript.h"
12 #include "../../include/javascript/resource.h"
13
14 /* -------------------------- CJS_Context -------------------------- */
15
16 CJS_Context::CJS_Context(CJS_Runtime* pRuntime)
17     : m_pRuntime(pRuntime), m_bBusy(FALSE), m_bMsgBoxEnable(TRUE) {
18   m_pEventHandler = new CJS_EventHandler(this);
19 }
20
21 CJS_Context::~CJS_Context() {
22   delete m_pEventHandler;
23 }
24
25 CPDFSDK_Document* CJS_Context::GetReaderDocument() {
26   return m_pRuntime->GetReaderDocument();
27 }
28
29 CPDFDoc_Environment* CJS_Context::GetReaderApp() {
30   ASSERT(m_pRuntime != NULL);
31
32   return m_pRuntime->GetReaderApp();
33 }
34
35 FX_BOOL CJS_Context::RunScript(const CFX_WideString& script,
36                                CFX_WideString& info) {
37   v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate());
38   v8::HandleScope handle_scope(m_pRuntime->GetIsolate());
39   v8::Local<v8::Context> context = m_pRuntime->NewJSContext();
40   v8::Context::Scope context_scope(context);
41
42   if (m_bBusy) {
43     info = JSGetStringFromID(this, IDS_STRING_JSBUSY);
44     return FALSE;
45   }
46   m_bBusy = TRUE;
47
48   ASSERT(m_pEventHandler->IsValid());
49   CJS_Runtime::FieldEvent event(m_pEventHandler->TargetName(),
50                                 m_pEventHandler->EventType());
51   if (!m_pRuntime->AddEventToSet(event)) {
52     info = JSGetStringFromID(this, IDS_STRING_JSEVENT);
53     return FALSE;
54   }
55
56   FXJSErr error = {NULL, NULL, 0};
57   int nRet = 0;
58   if (script.GetLength() > 0) {
59     nRet = FXJS_Execute(m_pRuntime->GetIsolate(), this, script.c_str(),
60                         script.GetLength(), &error);
61   }
62
63   if (nRet < 0) {
64     CFX_WideString sLine;
65     sLine.Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline,
66                  error.message);
67     info += sLine;
68   } else {
69     info = JSGetStringFromID(this, IDS_STRING_RUN);
70   }
71
72   m_pRuntime->RemoveEventFromSet(event);
73   m_pEventHandler->Destroy();
74   m_bBusy = FALSE;
75
76   return nRet >= 0;
77 }
78
79 void CJS_Context::OnApp_Init() {
80   m_pEventHandler->OnApp_Init();
81 }
82
83 void CJS_Context::OnDoc_Open(CPDFSDK_Document* pDoc,
84                              const CFX_WideString& strTargetName) {
85   m_pEventHandler->OnDoc_Open(pDoc, strTargetName);
86 }
87
88 void CJS_Context::OnDoc_WillPrint(CPDFSDK_Document* pDoc) {
89   m_pEventHandler->OnDoc_WillPrint(pDoc);
90 }
91
92 void CJS_Context::OnDoc_DidPrint(CPDFSDK_Document* pDoc) {
93   m_pEventHandler->OnDoc_DidPrint(pDoc);
94 }
95
96 void CJS_Context::OnDoc_WillSave(CPDFSDK_Document* pDoc) {
97   m_pEventHandler->OnDoc_WillSave(pDoc);
98 }
99
100 void CJS_Context::OnDoc_DidSave(CPDFSDK_Document* pDoc) {
101   m_pEventHandler->OnDoc_DidSave(pDoc);
102 }
103
104 void CJS_Context::OnDoc_WillClose(CPDFSDK_Document* pDoc) {
105   m_pEventHandler->OnDoc_WillClose(pDoc);
106 }
107
108 void CJS_Context::OnPage_Open(CPDFSDK_Document* pTarget) {
109   m_pEventHandler->OnPage_Open(pTarget);
110 }
111
112 void CJS_Context::OnPage_Close(CPDFSDK_Document* pTarget) {
113   m_pEventHandler->OnPage_Close(pTarget);
114 }
115
116 void CJS_Context::OnPage_InView(CPDFSDK_Document* pTarget) {
117   m_pEventHandler->OnPage_InView(pTarget);
118 }
119
120 void CJS_Context::OnPage_OutView(CPDFSDK_Document* pTarget) {
121   m_pEventHandler->OnPage_OutView(pTarget);
122 }
123
124 void CJS_Context::OnField_MouseDown(FX_BOOL bModifier,
125                                     FX_BOOL bShift,
126                                     CPDF_FormField* pTarget) {
127   m_pEventHandler->OnField_MouseDown(bModifier, bShift, pTarget);
128 }
129
130 void CJS_Context::OnField_MouseEnter(FX_BOOL bModifier,
131                                      FX_BOOL bShift,
132                                      CPDF_FormField* pTarget) {
133   m_pEventHandler->OnField_MouseEnter(bModifier, bShift, pTarget);
134 }
135
136 void CJS_Context::OnField_MouseExit(FX_BOOL bModifier,
137                                     FX_BOOL bShift,
138                                     CPDF_FormField* pTarget) {
139   m_pEventHandler->OnField_MouseExit(bModifier, bShift, pTarget);
140 }
141
142 void CJS_Context::OnField_MouseUp(FX_BOOL bModifier,
143                                   FX_BOOL bShift,
144                                   CPDF_FormField* pTarget) {
145   m_pEventHandler->OnField_MouseUp(bModifier, bShift, pTarget);
146 }
147
148 void CJS_Context::OnField_Focus(FX_BOOL bModifier,
149                                 FX_BOOL bShift,
150                                 CPDF_FormField* pTarget,
151                                 const CFX_WideString& Value) {
152   m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value);
153 }
154
155 void CJS_Context::OnField_Blur(FX_BOOL bModifier,
156                                FX_BOOL bShift,
157                                CPDF_FormField* pTarget,
158                                const CFX_WideString& Value) {
159   m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value);
160 }
161
162 void CJS_Context::OnField_Calculate(CPDF_FormField* pSource,
163                                     CPDF_FormField* pTarget,
164                                     CFX_WideString& Value,
165                                     FX_BOOL& bRc) {
166   m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc);
167 }
168
169 void CJS_Context::OnField_Format(CPDF_FormField* pTarget,
170                                  CFX_WideString& Value,
171                                  FX_BOOL bWillCommit) {
172   m_pEventHandler->OnField_Format(pTarget, Value, bWillCommit);
173 }
174
175 void CJS_Context::OnField_Keystroke(CFX_WideString& strChange,
176                                     const CFX_WideString& strChangeEx,
177                                     FX_BOOL bKeyDown,
178                                     FX_BOOL bModifier,
179                                     int& nSelEnd,
180                                     int& nSelStart,
181                                     FX_BOOL bShift,
182                                     CPDF_FormField* pTarget,
183                                     CFX_WideString& Value,
184                                     FX_BOOL bWillCommit,
185                                     FX_BOOL bFieldFull,
186                                     FX_BOOL& bRc) {
187   m_pEventHandler->OnField_Keystroke(
188       strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart, bShift,
189       pTarget, Value, bWillCommit, bFieldFull, bRc);
190 }
191
192 void CJS_Context::OnField_Validate(CFX_WideString& strChange,
193                                    const CFX_WideString& strChangeEx,
194                                    FX_BOOL bKeyDown,
195                                    FX_BOOL bModifier,
196                                    FX_BOOL bShift,
197                                    CPDF_FormField* pTarget,
198                                    CFX_WideString& Value,
199                                    FX_BOOL& bRc) {
200   m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier,
201                                     bShift, pTarget, Value, bRc);
202 }
203
204 void CJS_Context::OnScreen_Focus(FX_BOOL bModifier,
205                                  FX_BOOL bShift,
206                                  CPDFSDK_Annot* pScreen) {
207   m_pEventHandler->OnScreen_Focus(bModifier, bShift, pScreen);
208 }
209
210 void CJS_Context::OnScreen_Blur(FX_BOOL bModifier,
211                                 FX_BOOL bShift,
212                                 CPDFSDK_Annot* pScreen) {
213   m_pEventHandler->OnScreen_Blur(bModifier, bShift, pScreen);
214 }
215
216 void CJS_Context::OnScreen_Open(FX_BOOL bModifier,
217                                 FX_BOOL bShift,
218                                 CPDFSDK_Annot* pScreen) {
219   m_pEventHandler->OnScreen_Open(bModifier, bShift, pScreen);
220 }
221
222 void CJS_Context::OnScreen_Close(FX_BOOL bModifier,
223                                  FX_BOOL bShift,
224                                  CPDFSDK_Annot* pScreen) {
225   m_pEventHandler->OnScreen_Close(bModifier, bShift, pScreen);
226 }
227
228 void CJS_Context::OnScreen_MouseDown(FX_BOOL bModifier,
229                                      FX_BOOL bShift,
230                                      CPDFSDK_Annot* pScreen) {
231   m_pEventHandler->OnScreen_MouseDown(bModifier, bShift, pScreen);
232 }
233
234 void CJS_Context::OnScreen_MouseUp(FX_BOOL bModifier,
235                                    FX_BOOL bShift,
236                                    CPDFSDK_Annot* pScreen) {
237   m_pEventHandler->OnScreen_MouseUp(bModifier, bShift, pScreen);
238 }
239
240 void CJS_Context::OnScreen_MouseEnter(FX_BOOL bModifier,
241                                       FX_BOOL bShift,
242                                       CPDFSDK_Annot* pScreen) {
243   m_pEventHandler->OnScreen_MouseEnter(bModifier, bShift, pScreen);
244 }
245
246 void CJS_Context::OnScreen_MouseExit(FX_BOOL bModifier,
247                                      FX_BOOL bShift,
248                                      CPDFSDK_Annot* pScreen) {
249   m_pEventHandler->OnScreen_MouseExit(bModifier, bShift, pScreen);
250 }
251
252 void CJS_Context::OnScreen_InView(FX_BOOL bModifier,
253                                   FX_BOOL bShift,
254                                   CPDFSDK_Annot* pScreen) {
255   m_pEventHandler->OnScreen_InView(bModifier, bShift, pScreen);
256 }
257
258 void CJS_Context::OnScreen_OutView(FX_BOOL bModifier,
259                                    FX_BOOL bShift,
260                                    CPDFSDK_Annot* pScreen) {
261   m_pEventHandler->OnScreen_OutView(bModifier, bShift, pScreen);
262 }
263
264 void CJS_Context::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) {
265   m_pEventHandler->OnBookmark_MouseUp(pBookMark);
266 }
267
268 void CJS_Context::OnLink_MouseUp(CPDFSDK_Document* pTarget) {
269   m_pEventHandler->OnLink_MouseUp(pTarget);
270 }
271
272 void CJS_Context::OnConsole_Exec() {
273   m_pEventHandler->OnConsole_Exec();
274 }
275
276 void CJS_Context::OnExternal_Exec() {
277   m_pEventHandler->OnExternal_Exec();
278 }
279
280 void CJS_Context::OnBatchExec(CPDFSDK_Document* pTarget) {
281   m_pEventHandler->OnBatchExec(pTarget);
282 }
283
284 void CJS_Context::OnMenu_Exec(CPDFSDK_Document* pTarget,
285                               const CFX_WideString& strTargetName) {
286   m_pEventHandler->OnMenu_Exec(pTarget, strTargetName);
287 }