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