blob: 02978982d89d4da07ffdef47e8ffc5bdbf376087 [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
Nico Weber9d8ec5a2015-08-04 13:00:21 -070013int 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-Malek3f3b45c2014-05-23 17:28:10 -070021
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022 if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument())
23 pDoc->KillFocusAnnot();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025 return pApp->JS_appAlert(swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026}
27
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028CPDFSDK_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-Malek3f3b45c2014-05-23 17:28:10 -070034}
35
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {}
37
38CJS_EmbedObj::~CJS_EmbedObj() {
39 m_pJSObject = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040}
41
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042CPDFSDK_PageView* CJS_EmbedObj::JSGetPageView(IFXJS_Context* cc) {
43 return FXJS_GetPageView(cc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044}
45
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046int 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-Malek3f3b45c2014-05-23 17:28:10 -070053}
54
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
56 CJS_Object::Alert(pContext, swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057}
58
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059CJS_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-Malek3f3b45c2014-05-23 17:28:10 -070065}
66
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067void CJS_EmbedObj::EndTimer(CJS_Timer* pTimer) {
68 ASSERT(pTimer != NULL);
69 pTimer->KillJSTimer();
70 delete pTimer;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070071}
72
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073void 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-Malek3f3b45c2014-05-23 17:28:10 -070078}
79
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
81 CJS_Object* pJSObj = data.GetParameter();
82 pJSObj->Dispose();
83 data.SetSecondPassCallback(FreeObject);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020084}
85
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086CJS_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-Malek3f3b45c2014-05-23 17:28:10 -070090};
91
Lei Zhang2b1a2d52015-08-14 22:16:22 -070092CJS_Object::~CJS_Object() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 m_pObject.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094};
95
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096void CJS_Object::MakeWeak() {
97 m_pObject.SetWeak(this, DisposeObject, v8::WeakCallbackType::kInternalFields);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020098}
99
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100void CJS_Object::Dispose() {
101 m_pObject.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102}
103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104CPDFSDK_PageView* CJS_Object::JSGetPageView(IFXJS_Context* cc) {
105 return FXJS_GetPageView(cc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106}
107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108int 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-Malek3f3b45c2014-05-23 17:28:10 -0700115}
116
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
118 ASSERT(pContext != NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700119
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 if (pContext->IsMsgBoxEnabled()) {
121 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
122 if (pApp)
123 pApp->JS_appAlert(swMsg, NULL, 0, 3);
124 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125}
Tom Sepez371c87f2015-08-13 16:56:19 -0700126
127FX_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
137void 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
147void 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
161CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() {
162 // Leak the timer array at shutdown.
163 static auto* s_TimerMap = new TimerMap;
164 return s_TimerMap;
165}