John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 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. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 7 | #include "../../include/javascript/IJavaScript.h" |
| 8 | #include "../../include/javascript/JS_Define.h" |
| 9 | #include "../../include/javascript/JS_Object.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 10 | #include "../../include/javascript/JS_Context.h" |
| 11 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 12 | int FXJS_MsgBox(CPDFDoc_Environment* pApp, |
| 13 | CPDFSDK_PageView* pPageView, |
| 14 | const FX_WCHAR* swMsg, |
| 15 | const FX_WCHAR* swTitle, |
| 16 | FX_UINT nType, |
| 17 | FX_UINT nIcon) { |
| 18 | if (!pApp) |
| 19 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 20 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 21 | if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument()) |
| 22 | pDoc->KillFocusAnnot(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 23 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 24 | return pApp->JS_appAlert(swMsg, swTitle, nType, nIcon); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 27 | CPDFSDK_PageView* FXJS_GetPageView(IFXJS_Context* cc) { |
| 28 | if (CJS_Context* pContext = (CJS_Context*)cc) { |
| 29 | if (pContext->GetReaderDocument()) |
| 30 | return NULL; |
| 31 | } |
| 32 | return NULL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 35 | CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {} |
| 36 | |
| 37 | CJS_EmbedObj::~CJS_EmbedObj() { |
| 38 | m_pJSObject = NULL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 41 | CPDFSDK_PageView* CJS_EmbedObj::JSGetPageView(IFXJS_Context* cc) { |
| 42 | return FXJS_GetPageView(cc); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 45 | int CJS_EmbedObj::MsgBox(CPDFDoc_Environment* pApp, |
| 46 | CPDFSDK_PageView* pPageView, |
| 47 | const FX_WCHAR* swMsg, |
| 48 | const FX_WCHAR* swTitle, |
| 49 | FX_UINT nType, |
| 50 | FX_UINT nIcon) { |
| 51 | return FXJS_MsgBox(pApp, pPageView, swMsg, swTitle, nType, nIcon); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 54 | void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) { |
| 55 | CJS_Object::Alert(pContext, swMsg); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 58 | void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data) { |
| 59 | CJS_Object* pJSObj = data.GetParameter(); |
| 60 | pJSObj->ExitInstance(); |
| 61 | delete pJSObj; |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 62 | FXJS_FreePrivate(data.GetInternalField(0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 65 | void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) { |
| 66 | CJS_Object* pJSObj = data.GetParameter(); |
| 67 | pJSObj->Dispose(); |
| 68 | data.SetSecondPassCallback(FreeObject); |
Jochen Eisinger | dfa2c99 | 2015-05-19 00:38:00 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Tom Sepez | 116e4ad | 2015-09-21 09:22:05 -0700 | [diff] [blame] | 71 | CJS_Object::CJS_Object(v8::Local<v8::Object> pObject) { |
| 72 | m_pIsolate = pObject->CreationContext()->GetIsolate(); |
| 73 | m_pV8Object.Reset(m_pIsolate, pObject); |
| 74 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 75 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 76 | CJS_Object::~CJS_Object() { |
Tom Sepez | 116e4ad | 2015-09-21 09:22:05 -0700 | [diff] [blame] | 77 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 78 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 79 | void CJS_Object::MakeWeak() { |
Tom Sepez | 116e4ad | 2015-09-21 09:22:05 -0700 | [diff] [blame] | 80 | m_pV8Object.SetWeak(this, DisposeObject, |
| 81 | v8::WeakCallbackType::kInternalFields); |
Jochen Eisinger | dfa2c99 | 2015-05-19 00:38:00 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 84 | void CJS_Object::Dispose() { |
Tom Sepez | 116e4ad | 2015-09-21 09:22:05 -0700 | [diff] [blame] | 85 | m_pV8Object.Reset(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 88 | CPDFSDK_PageView* CJS_Object::JSGetPageView(IFXJS_Context* cc) { |
| 89 | return FXJS_GetPageView(cc); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 92 | int CJS_Object::MsgBox(CPDFDoc_Environment* pApp, |
| 93 | CPDFSDK_PageView* pPageView, |
| 94 | const FX_WCHAR* swMsg, |
| 95 | const FX_WCHAR* swTitle, |
| 96 | FX_UINT nType, |
| 97 | FX_UINT nIcon) { |
| 98 | return FXJS_MsgBox(pApp, pPageView, swMsg, swTitle, nType, nIcon); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 101 | void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) { |
| 102 | ASSERT(pContext != NULL); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 103 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 104 | if (pContext->IsMsgBoxEnabled()) { |
| 105 | CPDFDoc_Environment* pApp = pContext->GetReaderApp(); |
| 106 | if (pApp) |
| 107 | pApp->JS_appAlert(swMsg, NULL, 0, 3); |
| 108 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 109 | } |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 110 | |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame^] | 111 | CJS_Timer::CJS_Timer(CJS_EmbedObj* pObj, |
| 112 | CPDFDoc_Environment* pApp, |
| 113 | CJS_Runtime* pRuntime, |
| 114 | int nType, |
| 115 | const CFX_WideString& script, |
| 116 | FX_DWORD dwElapse, |
| 117 | FX_DWORD dwTimeOut) |
| 118 | : m_nTimerID(0), |
| 119 | m_pEmbedObj(pObj), |
| 120 | m_bProcessing(false), |
| 121 | m_bValid(true), |
| 122 | m_nType(nType), |
| 123 | m_dwTimeOut(dwTimeOut), |
| 124 | m_pRuntime(pRuntime), |
| 125 | m_pApp(pApp) { |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 126 | IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame^] | 127 | m_nTimerID = pHandler->SetTimer(dwElapse, TimerProc); |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 128 | (*GetGlobalTimerMap())[m_nTimerID] = this; |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame^] | 129 | m_pRuntime->AddObserver(this); |
| 130 | } |
| 131 | |
| 132 | CJS_Timer::~CJS_Timer() { |
| 133 | CJS_Runtime* pRuntime = GetRuntime(); |
| 134 | if (pRuntime) |
| 135 | pRuntime->RemoveObserver(this); |
| 136 | KillJSTimer(); |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void CJS_Timer::KillJSTimer() { |
| 140 | if (m_nTimerID) { |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame^] | 141 | if (m_bValid) { |
| 142 | IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); |
| 143 | pHandler->KillTimer(m_nTimerID); |
| 144 | } |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 145 | GetGlobalTimerMap()->erase(m_nTimerID); |
| 146 | m_nTimerID = 0; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // static |
| 151 | void CJS_Timer::TimerProc(int idEvent) { |
| 152 | const auto it = GetGlobalTimerMap()->find(idEvent); |
| 153 | if (it != GetGlobalTimerMap()->end()) { |
| 154 | CJS_Timer* pTimer = it->second; |
| 155 | if (!pTimer->m_bProcessing) { |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame^] | 156 | CFX_AutoRestorer<bool> scoped_processing(&pTimer->m_bProcessing); |
| 157 | pTimer->m_bProcessing = true; |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 158 | if (pTimer->m_pEmbedObj) |
| 159 | pTimer->m_pEmbedObj->TimerProc(pTimer); |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // static |
| 165 | CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() { |
| 166 | // Leak the timer array at shutdown. |
| 167 | static auto* s_TimerMap = new TimerMap; |
| 168 | return s_TimerMap; |
| 169 | } |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame^] | 170 | |
| 171 | void CJS_Timer::OnDestroyed() { |
| 172 | m_bValid = false; |
| 173 | } |