blob: ef9bab971d52447dfc0c390969a9db1e9a1a77ad [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// 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-Malek3f3b45c2014-05-23 17:28:10 -070011#include "../../include/javascript/JS_Context.h"
12
Tom Sepez2ebc33e2015-08-13 16:07:29 -070013JSTimerMap* GetGlobalTimerMap() {
Bruce Dawsonb4649dc2015-01-05 13:26:17 -080014 // Leak the timer array at shutdown.
Tom Sepez2ebc33e2015-08-13 16:07:29 -070015 static auto* timeMap = new JSTimerMap;
16 return timeMap;
Bruce Dawsonb4649dc2015-01-05 13:26:17 -080017}
18
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019int 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-Malek3f3b45c2014-05-23 17:28:10 -070027
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument())
29 pDoc->KillFocusAnnot();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070030
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031 return pApp->JS_appAlert(swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032}
33
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034CPDFSDK_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-Malek3f3b45c2014-05-23 17:28:10 -070040}
41
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042/* --------------------------------- CJS_EmbedObj
43 * --------------------------------- */
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {}
46
47CJS_EmbedObj::~CJS_EmbedObj() {
48 m_pJSObject = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070049}
50
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051CPDFSDK_PageView* CJS_EmbedObj::JSGetPageView(IFXJS_Context* cc) {
52 return FXJS_GetPageView(cc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053}
54
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055int 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-Malek3f3b45c2014-05-23 17:28:10 -070062}
63
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
65 CJS_Object::Alert(pContext, swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066}
67
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068CJS_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-Malek3f3b45c2014-05-23 17:28:10 -070074}
75
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076void CJS_EmbedObj::EndTimer(CJS_Timer* pTimer) {
77 ASSERT(pTimer != NULL);
78 pTimer->KillJSTimer();
79 delete pTimer;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080}
81
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082/* --------------------------------- CJS_Object
83 * --------------------------------- */
84void 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-Malek3f3b45c2014-05-23 17:28:10 -070089}
90
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
92 CJS_Object* pJSObj = data.GetParameter();
93 pJSObj->Dispose();
94 data.SetSecondPassCallback(FreeObject);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020095}
96
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097CJS_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-Malek3f3b45c2014-05-23 17:28:10 -0700101};
102
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103CJS_Object::~CJS_Object(void) {
104 delete m_pEmbedObj;
105 m_pEmbedObj = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 m_pObject.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108};
109
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110void CJS_Object::MakeWeak() {
111 m_pObject.SetWeak(this, DisposeObject, v8::WeakCallbackType::kInternalFields);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +0200112}
113
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114void CJS_Object::Dispose() {
115 m_pObject.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116}
117
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118CPDFSDK_PageView* CJS_Object::JSGetPageView(IFXJS_Context* cc) {
119 return FXJS_GetPageView(cc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120}
121
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122int 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-Malek3f3b45c2014-05-23 17:28:10 -0700129}
130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
132 ASSERT(pContext != NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 if (pContext->IsMsgBoxEnabled()) {
135 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
136 if (pApp)
137 pApp->JS_appAlert(swMsg, NULL, 0, 3);
138 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700139}