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 | CJS_Timer* CJS_EmbedObj::BeginTimer(CPDFDoc_Environment* pApp, |
| 59 | FX_UINT nElapse) { |
| 60 | CJS_Timer* pTimer = new CJS_Timer(this, pApp); |
| 61 | pTimer->SetJSTimer(nElapse); |
| 62 | |
| 63 | return pTimer; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 66 | void CJS_EmbedObj::EndTimer(CJS_Timer* pTimer) { |
| 67 | ASSERT(pTimer != NULL); |
| 68 | pTimer->KillJSTimer(); |
| 69 | delete pTimer; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 72 | void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data) { |
| 73 | CJS_Object* pJSObj = data.GetParameter(); |
| 74 | pJSObj->ExitInstance(); |
| 75 | delete pJSObj; |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 76 | FXJS_FreePrivate(data.GetInternalField(0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 79 | void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) { |
| 80 | CJS_Object* pJSObj = data.GetParameter(); |
| 81 | pJSObj->Dispose(); |
| 82 | data.SetSecondPassCallback(FreeObject); |
Jochen Eisinger | dfa2c99 | 2015-05-19 00:38:00 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Tom Sepez | 116e4ad | 2015-09-21 09:22:05 -0700 | [diff] [blame] | 85 | CJS_Object::CJS_Object(v8::Local<v8::Object> pObject) { |
| 86 | m_pIsolate = pObject->CreationContext()->GetIsolate(); |
| 87 | m_pV8Object.Reset(m_pIsolate, pObject); |
| 88 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 89 | |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 90 | CJS_Object::~CJS_Object() { |
Tom Sepez | 116e4ad | 2015-09-21 09:22:05 -0700 | [diff] [blame] | 91 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 92 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 93 | void CJS_Object::MakeWeak() { |
Tom Sepez | 116e4ad | 2015-09-21 09:22:05 -0700 | [diff] [blame] | 94 | m_pV8Object.SetWeak(this, DisposeObject, |
| 95 | v8::WeakCallbackType::kInternalFields); |
Jochen Eisinger | dfa2c99 | 2015-05-19 00:38:00 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 98 | void CJS_Object::Dispose() { |
Tom Sepez | 116e4ad | 2015-09-21 09:22:05 -0700 | [diff] [blame] | 99 | m_pV8Object.Reset(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 102 | CPDFSDK_PageView* CJS_Object::JSGetPageView(IFXJS_Context* cc) { |
| 103 | return FXJS_GetPageView(cc); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 106 | int CJS_Object::MsgBox(CPDFDoc_Environment* pApp, |
| 107 | CPDFSDK_PageView* pPageView, |
| 108 | const FX_WCHAR* swMsg, |
| 109 | const FX_WCHAR* swTitle, |
| 110 | FX_UINT nType, |
| 111 | FX_UINT nIcon) { |
| 112 | return FXJS_MsgBox(pApp, pPageView, swMsg, swTitle, nType, nIcon); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 115 | void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) { |
| 116 | ASSERT(pContext != NULL); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 117 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 118 | if (pContext->IsMsgBoxEnabled()) { |
| 119 | CPDFDoc_Environment* pApp = pContext->GetReaderApp(); |
| 120 | if (pApp) |
| 121 | pApp->JS_appAlert(swMsg, NULL, 0, 3); |
| 122 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 123 | } |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 124 | |
| 125 | FX_UINT CJS_Timer::SetJSTimer(FX_UINT nElapse) { |
| 126 | if (m_nTimerID) |
| 127 | KillJSTimer(); |
| 128 | IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); |
| 129 | m_nTimerID = pHandler->SetTimer(nElapse, TimerProc); |
| 130 | (*GetGlobalTimerMap())[m_nTimerID] = this; |
| 131 | m_dwElapse = nElapse; |
| 132 | return m_nTimerID; |
| 133 | } |
| 134 | |
| 135 | void CJS_Timer::KillJSTimer() { |
| 136 | if (m_nTimerID) { |
| 137 | IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); |
| 138 | pHandler->KillTimer(m_nTimerID); |
| 139 | GetGlobalTimerMap()->erase(m_nTimerID); |
| 140 | m_nTimerID = 0; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // static |
| 145 | void CJS_Timer::TimerProc(int idEvent) { |
| 146 | const auto it = GetGlobalTimerMap()->find(idEvent); |
| 147 | if (it != GetGlobalTimerMap()->end()) { |
| 148 | CJS_Timer* pTimer = it->second; |
| 149 | if (!pTimer->m_bProcessing) { |
| 150 | pTimer->m_bProcessing = TRUE; |
| 151 | if (pTimer->m_pEmbedObj) |
| 152 | pTimer->m_pEmbedObj->TimerProc(pTimer); |
| 153 | pTimer->m_bProcessing = FALSE; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // static |
| 159 | CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() { |
| 160 | // Leak the timer array at shutdown. |
| 161 | static auto* s_TimerMap = new TimerMap; |
| 162 | return s_TimerMap; |
| 163 | } |