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