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 | |
Tom Sepez | 2ebc33e | 2015-08-13 16:07:29 -0700 | [diff] [blame^] | 13 | JSTimerMap* GetGlobalTimerMap() { |
Bruce Dawson | b4649dc | 2015-01-05 13:26:17 -0800 | [diff] [blame] | 14 | // Leak the timer array at shutdown. |
Tom Sepez | 2ebc33e | 2015-08-13 16:07:29 -0700 | [diff] [blame^] | 15 | static auto* timeMap = new JSTimerMap; |
| 16 | return timeMap; |
Bruce Dawson | b4649dc | 2015-01-05 13:26:17 -0800 | [diff] [blame] | 17 | } |
| 18 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 19 | int FXJS_MsgBox(CPDFDoc_Environment* pApp, |
| 20 | CPDFSDK_PageView* pPageView, |
| 21 | const FX_WCHAR* swMsg, |
| 22 | const FX_WCHAR* swTitle, |
| 23 | FX_UINT nType, |
| 24 | FX_UINT nIcon) { |
| 25 | if (!pApp) |
| 26 | return 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 27 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 28 | if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument()) |
| 29 | pDoc->KillFocusAnnot(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 30 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 31 | return pApp->JS_appAlert(swMsg, swTitle, nType, nIcon); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 34 | CPDFSDK_PageView* FXJS_GetPageView(IFXJS_Context* cc) { |
| 35 | if (CJS_Context* pContext = (CJS_Context*)cc) { |
| 36 | if (pContext->GetReaderDocument()) |
| 37 | return NULL; |
| 38 | } |
| 39 | return 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 | /* --------------------------------- CJS_EmbedObj |
| 43 | * --------------------------------- */ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 44 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 45 | CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {} |
| 46 | |
| 47 | CJS_EmbedObj::~CJS_EmbedObj() { |
| 48 | m_pJSObject = NULL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 51 | CPDFSDK_PageView* CJS_EmbedObj::JSGetPageView(IFXJS_Context* cc) { |
| 52 | return FXJS_GetPageView(cc); |
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 | int CJS_EmbedObj::MsgBox(CPDFDoc_Environment* pApp, |
| 56 | CPDFSDK_PageView* pPageView, |
| 57 | const FX_WCHAR* swMsg, |
| 58 | const FX_WCHAR* swTitle, |
| 59 | FX_UINT nType, |
| 60 | FX_UINT nIcon) { |
| 61 | return FXJS_MsgBox(pApp, pPageView, swMsg, swTitle, nType, nIcon); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 64 | void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) { |
| 65 | CJS_Object::Alert(pContext, swMsg); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 68 | CJS_Timer* CJS_EmbedObj::BeginTimer(CPDFDoc_Environment* pApp, |
| 69 | FX_UINT nElapse) { |
| 70 | CJS_Timer* pTimer = new CJS_Timer(this, pApp); |
| 71 | pTimer->SetJSTimer(nElapse); |
| 72 | |
| 73 | return pTimer; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 76 | void CJS_EmbedObj::EndTimer(CJS_Timer* pTimer) { |
| 77 | ASSERT(pTimer != NULL); |
| 78 | pTimer->KillJSTimer(); |
| 79 | delete pTimer; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 82 | /* --------------------------------- CJS_Object |
| 83 | * --------------------------------- */ |
| 84 | void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data) { |
| 85 | CJS_Object* pJSObj = data.GetParameter(); |
| 86 | pJSObj->ExitInstance(); |
| 87 | delete pJSObj; |
| 88 | JS_FreePrivate(data.GetInternalField(0)); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 91 | void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) { |
| 92 | CJS_Object* pJSObj = data.GetParameter(); |
| 93 | pJSObj->Dispose(); |
| 94 | data.SetSecondPassCallback(FreeObject); |
Jochen Eisinger | dfa2c99 | 2015-05-19 00:38:00 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 97 | CJS_Object::CJS_Object(JSFXObject pObject) : m_pEmbedObj(NULL) { |
| 98 | v8::Local<v8::Context> context = pObject->CreationContext(); |
| 99 | m_pIsolate = context->GetIsolate(); |
| 100 | m_pObject.Reset(m_pIsolate, pObject); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 103 | CJS_Object::~CJS_Object(void) { |
| 104 | delete m_pEmbedObj; |
| 105 | m_pEmbedObj = NULL; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 106 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 107 | m_pObject.Reset(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 110 | void CJS_Object::MakeWeak() { |
| 111 | m_pObject.SetWeak(this, DisposeObject, v8::WeakCallbackType::kInternalFields); |
Jochen Eisinger | dfa2c99 | 2015-05-19 00:38:00 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 114 | void CJS_Object::Dispose() { |
| 115 | m_pObject.Reset(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 118 | CPDFSDK_PageView* CJS_Object::JSGetPageView(IFXJS_Context* cc) { |
| 119 | return FXJS_GetPageView(cc); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 122 | int CJS_Object::MsgBox(CPDFDoc_Environment* pApp, |
| 123 | CPDFSDK_PageView* pPageView, |
| 124 | const FX_WCHAR* swMsg, |
| 125 | const FX_WCHAR* swTitle, |
| 126 | FX_UINT nType, |
| 127 | FX_UINT nIcon) { |
| 128 | return FXJS_MsgBox(pApp, pPageView, swMsg, swTitle, nType, nIcon); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 131 | void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) { |
| 132 | ASSERT(pContext != NULL); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 133 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 134 | if (pContext->IsMsgBoxEnabled()) { |
| 135 | CPDFDoc_Environment* pApp = pContext->GetReaderApp(); |
| 136 | if (pApp) |
| 137 | pApp->JS_appAlert(swMsg, NULL, 0, 3); |
| 138 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 139 | } |