blob: 2ee23f8877f4ccc718f30a5297098771af0ed02a [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
Bruce Dawsonb4649dc2015-01-05 13:26:17 -080013JS_TIMER_MAPARRAY& GetTimeMap()
14{
15 // Leak the timer array at shutdown.
16 static auto* timeMap = new JS_TIMER_MAPARRAY;
17 return *timeMap;
18}
19
Tom Sepezd7e5cc72015-06-10 14:33:37 -070020int FXJS_MsgBox(CPDFDoc_Environment* pApp, CPDFSDK_PageView* pPageView, const FX_WCHAR* swMsg, const FX_WCHAR* swTitle, FX_UINT nType, FX_UINT nIcon)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070021{
Tom Sepezdcbc02f2015-07-17 09:16:17 -070022 if (!pApp)
23 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024
Tom Sepezdcbc02f2015-07-17 09:16:17 -070025 if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument())
26 pDoc->KillFocusAnnot();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Tom Sepezdcbc02f2015-07-17 09:16:17 -070028 return pApp->JS_appAlert(swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029}
30
31CPDFSDK_PageView* FXJS_GetPageView(IFXJS_Context* cc)
32{
Tom Sepezdcbc02f2015-07-17 09:16:17 -070033 if (CJS_Context* pContext = (CJS_Context *)cc)
34 {
35 if (pContext->GetReaderDocument())
36 return NULL;
37 }
38 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039}
40
41/* --------------------------------- CJS_EmbedObj --------------------------------- */
42
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070043CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) :
Tom Sepezdcbc02f2015-07-17 09:16:17 -070044 m_pJSObject(pJSObject)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045{
46}
47
48CJS_EmbedObj::~CJS_EmbedObj()
49{
Tom Sepezdcbc02f2015-07-17 09:16:17 -070050 m_pJSObject = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
52}
53
54CPDFSDK_PageView* CJS_EmbedObj::JSGetPageView(IFXJS_Context* cc)
55{
Tom Sepezdcbc02f2015-07-17 09:16:17 -070056 return FXJS_GetPageView(cc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057}
58
Tom Sepezd7e5cc72015-06-10 14:33:37 -070059int CJS_EmbedObj::MsgBox(CPDFDoc_Environment* pApp, CPDFSDK_PageView* pPageView,const FX_WCHAR* swMsg,const FX_WCHAR* swTitle,FX_UINT nType,FX_UINT nIcon)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060{
Tom Sepezdcbc02f2015-07-17 09:16:17 -070061 return FXJS_MsgBox(pApp, pPageView, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062}
63
Tom Sepezd7e5cc72015-06-10 14:33:37 -070064void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065{
Tom Sepezdcbc02f2015-07-17 09:16:17 -070066 CJS_Object::Alert(pContext, swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067}
68
Bo Xufdc00a72014-10-28 23:03:33 -070069CJS_Timer* CJS_EmbedObj::BeginTimer(CPDFDoc_Environment* pApp, FX_UINT nElapse)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070{
Tom Sepezdcbc02f2015-07-17 09:16:17 -070071 CJS_Timer* pTimer = new CJS_Timer(this,pApp);
72 pTimer->SetJSTimer(nElapse);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070073
Tom Sepezdcbc02f2015-07-17 09:16:17 -070074 return pTimer;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075}
76
77void CJS_EmbedObj::EndTimer(CJS_Timer* pTimer)
78{
Tom Sepezdcbc02f2015-07-17 09:16:17 -070079 ASSERT(pTimer != NULL);
80 pTimer->KillJSTimer();
81 delete pTimer;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082}
83
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084/* --------------------------------- CJS_Object --------------------------------- */
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020085void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086{
Tom Sepezdcbc02f2015-07-17 09:16:17 -070087 CJS_Object* pJSObj = data.GetParameter();
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020088 pJSObj->ExitInstance();
89 delete pJSObj;
Tom Sepezdcbc02f2015-07-17 09:16:17 -070090 JS_FreePrivate(data.GetInternalField(0));
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020091}
92
93void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data)
94{
Tom Sepezdcbc02f2015-07-17 09:16:17 -070095 CJS_Object* pJSObj = data.GetParameter();
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020096 pJSObj->Dispose();
97 data.SetSecondPassCallback(FreeObject);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098}
99
100CJS_Object::CJS_Object(JSFXObject pObject) :m_pEmbedObj(NULL)
101{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700102 v8::Local<v8::Context> context = pObject->CreationContext();
103 m_pIsolate = context->GetIsolate();
104 m_pObject.Reset(m_pIsolate, pObject);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700105};
106
107CJS_Object::~CJS_Object(void)
108{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700109 delete m_pEmbedObj;
110 m_pEmbedObj = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700111
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700112 m_pObject.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700113};
114
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700115void CJS_Object::MakeWeak()
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700117 m_pObject.SetWeak(
Jochen Eisingerdfa2c992015-05-19 00:38:00 +0200118 this, DisposeObject, v8::WeakCallbackType::kInternalFields);
119}
120
121void CJS_Object::Dispose()
122{
123 m_pObject.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124}
125
126CPDFSDK_PageView* CJS_Object::JSGetPageView(IFXJS_Context* cc)
127{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700128 return FXJS_GetPageView(cc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129}
130
Tom Sepezd7e5cc72015-06-10 14:33:37 -0700131int CJS_Object::MsgBox(CPDFDoc_Environment* pApp, CPDFSDK_PageView* pPageView, const FX_WCHAR* swMsg, const FX_WCHAR* swTitle, FX_UINT nType, FX_UINT nIcon)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700133 return FXJS_MsgBox(pApp, pPageView, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700134}
135
Tom Sepezd7e5cc72015-06-10 14:33:37 -0700136void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg)
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137{
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700138 ASSERT(pContext != NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700139
Tom Sepezdcbc02f2015-07-17 09:16:17 -0700140 if (pContext->IsMsgBoxEnabled())
141 {
142 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
143 if(pApp)
144 pApp->JS_appAlert(swMsg, NULL, 0, 3);
145 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146}