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