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