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 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 22 CJS_EventContext::~CJS_EventContext() {} 23 24 CPDFSDK_FormFillEnvironment* CJS_EventContext::GetFormFillEnv() { 25 return m_pRuntime->GetFormFillEnv(); 26 } 27 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 65 void CJS_EventContext::OnApp_Init() { 66 m_pEventHandler->OnApp_Init(); 67 } 68 69 void CJS_EventContext::OnDoc_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv, 70 const WideString& strTargetName) { 71 m_pEventHandler->OnDoc_Open(pFormFillEnv, strTargetName); 72 } 73 74 void CJS_EventContext::OnDoc_WillPrint( 75 CPDFSDK_FormFillEnvironment* pFormFillEnv) { 76 m_pEventHandler->OnDoc_WillPrint(pFormFillEnv); 77 } 78 79 void CJS_EventContext::OnDoc_DidPrint( 80 CPDFSDK_FormFillEnvironment* pFormFillEnv) { 81 m_pEventHandler->OnDoc_DidPrint(pFormFillEnv); 82 } 83 84 void CJS_EventContext::OnDoc_WillSave( 85 CPDFSDK_FormFillEnvironment* pFormFillEnv) { 86 m_pEventHandler->OnDoc_WillSave(pFormFillEnv); 87 } 88 89 void CJS_EventContext::OnDoc_DidSave( 90 CPDFSDK_FormFillEnvironment* pFormFillEnv) { 91 m_pEventHandler->OnDoc_DidSave(pFormFillEnv); 92 } 93 94 void CJS_EventContext::OnDoc_WillClose( 95 CPDFSDK_FormFillEnvironment* pFormFillEnv) { 96 m_pEventHandler->OnDoc_WillClose(pFormFillEnv); 97 } 98 99 void CJS_EventContext::OnPage_Open(CPDFSDK_FormFillEnvironment* pFormFillEnv) { 100 m_pEventHandler->OnPage_Open(pFormFillEnv); 101 } 102 103 void CJS_EventContext::OnPage_Close(CPDFSDK_FormFillEnvironment* pFormFillEnv) { 104 m_pEventHandler->OnPage_Close(pFormFillEnv); 105 } 106 107 void CJS_EventContext::OnPage_InView( 108 CPDFSDK_FormFillEnvironment* pFormFillEnv) { 109 m_pEventHandler->OnPage_InView(pFormFillEnv); 110 } 111 112 void CJS_EventContext::OnPage_OutView( 113 CPDFSDK_FormFillEnvironment* pFormFillEnv) { 114 m_pEventHandler->OnPage_OutView(pFormFillEnv); 115 } 116 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 257 void CJS_EventContext::OnBookmark_MouseUp(CPDF_Bookmark* pBookMark) { 258 m_pEventHandler->OnBookmark_MouseUp(pBookMark); 259 } 260 261 void CJS_EventContext::OnLink_MouseUp( 262 CPDFSDK_FormFillEnvironment* pFormFillEnv) { 263 m_pEventHandler->OnLink_MouseUp(pFormFillEnv); 264 } 265 266 void CJS_EventContext::OnConsole_Exec() { 267 m_pEventHandler->OnConsole_Exec(); 268 } 269 270 void CJS_EventContext::OnExternal_Exec() { 271 m_pEventHandler->OnExternal_Exec(); 272 } 273 274 void CJS_EventContext::OnBatchExec(CPDFSDK_FormFillEnvironment* pFormFillEnv) { 275 m_pEventHandler->OnBatchExec(pFormFillEnv); 276 } 277 278 void CJS_EventContext::OnMenu_Exec(CPDFSDK_FormFillEnvironment* pFormFillEnv, 279 const WideString& strTargetName) { 280 m_pEventHandler->OnMenu_Exec(pFormFillEnv, strTargetName); 281 } 282