blob: 429d2682d85026f8123f4c0537fdcc8d8922bfcb [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
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07007#include "../../include/javascript/IJavaScript.h"
8#include "../../include/javascript/JS_Define.h"
9#include "../../include/javascript/JS_Object.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070010#include "../../include/javascript/JS_Context.h"
11
Nico Weber9d8ec5a2015-08-04 13:00:21 -070012int FXJS_MsgBox(CPDFDoc_Environment* pApp,
13 CPDFSDK_PageView* pPageView,
14 const FX_WCHAR* swMsg,
15 const FX_WCHAR* swTitle,
16 FX_UINT nType,
17 FX_UINT nIcon) {
18 if (!pApp)
19 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021 if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument())
22 pDoc->KillFocusAnnot();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 return pApp->JS_appAlert(swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025}
26
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027CPDFSDK_PageView* FXJS_GetPageView(IFXJS_Context* cc) {
28 if (CJS_Context* pContext = (CJS_Context*)cc) {
29 if (pContext->GetReaderDocument())
30 return NULL;
31 }
32 return NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033}
34
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {}
36
37CJS_EmbedObj::~CJS_EmbedObj() {
38 m_pJSObject = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039}
40
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041CPDFSDK_PageView* CJS_EmbedObj::JSGetPageView(IFXJS_Context* cc) {
42 return FXJS_GetPageView(cc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043}
44
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045int CJS_EmbedObj::MsgBox(CPDFDoc_Environment* pApp,
46 CPDFSDK_PageView* pPageView,
47 const FX_WCHAR* swMsg,
48 const FX_WCHAR* swTitle,
49 FX_UINT nType,
50 FX_UINT nIcon) {
51 return FXJS_MsgBox(pApp, pPageView, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052}
53
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
55 CJS_Object::Alert(pContext, swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056}
57
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058CJS_Timer* CJS_EmbedObj::BeginTimer(CPDFDoc_Environment* pApp,
59 FX_UINT nElapse) {
60 CJS_Timer* pTimer = new CJS_Timer(this, pApp);
61 pTimer->SetJSTimer(nElapse);
62
63 return pTimer;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064}
65
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066void CJS_EmbedObj::EndTimer(CJS_Timer* pTimer) {
67 ASSERT(pTimer != NULL);
68 pTimer->KillJSTimer();
69 delete pTimer;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070}
71
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
73 CJS_Object* pJSObj = data.GetParameter();
74 pJSObj->ExitInstance();
75 delete pJSObj;
Tom Sepez39bfe122015-09-17 15:25:23 -070076 FXJS_FreePrivate(data.GetInternalField(0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077}
78
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
80 CJS_Object* pJSObj = data.GetParameter();
81 pJSObj->Dispose();
82 data.SetSecondPassCallback(FreeObject);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020083}
84
Tom Sepez116e4ad2015-09-21 09:22:05 -070085CJS_Object::CJS_Object(v8::Local<v8::Object> pObject) {
86 m_pIsolate = pObject->CreationContext()->GetIsolate();
87 m_pV8Object.Reset(m_pIsolate, pObject);
88}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089
Lei Zhang2b1a2d52015-08-14 22:16:22 -070090CJS_Object::~CJS_Object() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070091}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093void CJS_Object::MakeWeak() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070094 m_pV8Object.SetWeak(this, DisposeObject,
95 v8::WeakCallbackType::kInternalFields);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020096}
97
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098void CJS_Object::Dispose() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070099 m_pV8Object.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100}
101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102CPDFSDK_PageView* CJS_Object::JSGetPageView(IFXJS_Context* cc) {
103 return FXJS_GetPageView(cc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700104}
105
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106int CJS_Object::MsgBox(CPDFDoc_Environment* pApp,
107 CPDFSDK_PageView* pPageView,
108 const FX_WCHAR* swMsg,
109 const FX_WCHAR* swTitle,
110 FX_UINT nType,
111 FX_UINT nIcon) {
112 return FXJS_MsgBox(pApp, pPageView, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700113}
114
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
116 ASSERT(pContext != NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 if (pContext->IsMsgBoxEnabled()) {
119 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
120 if (pApp)
121 pApp->JS_appAlert(swMsg, NULL, 0, 3);
122 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700123}
Tom Sepez371c87f2015-08-13 16:56:19 -0700124
125FX_UINT CJS_Timer::SetJSTimer(FX_UINT nElapse) {
126 if (m_nTimerID)
127 KillJSTimer();
128 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
129 m_nTimerID = pHandler->SetTimer(nElapse, TimerProc);
130 (*GetGlobalTimerMap())[m_nTimerID] = this;
131 m_dwElapse = nElapse;
132 return m_nTimerID;
133}
134
135void CJS_Timer::KillJSTimer() {
136 if (m_nTimerID) {
137 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
138 pHandler->KillTimer(m_nTimerID);
139 GetGlobalTimerMap()->erase(m_nTimerID);
140 m_nTimerID = 0;
141 }
142}
143
144// static
145void CJS_Timer::TimerProc(int idEvent) {
146 const auto it = GetGlobalTimerMap()->find(idEvent);
147 if (it != GetGlobalTimerMap()->end()) {
148 CJS_Timer* pTimer = it->second;
149 if (!pTimer->m_bProcessing) {
150 pTimer->m_bProcessing = TRUE;
151 if (pTimer->m_pEmbedObj)
152 pTimer->m_pEmbedObj->TimerProc(pTimer);
153 pTimer->m_bProcessing = FALSE;
154 }
155 }
156}
157
158// static
159CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() {
160 // Leak the timer array at shutdown.
161 static auto* s_TimerMap = new TimerMap;
162 return s_TimerMap;
163}