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