blob: 7898f4851691944615c6f760822e562fc2a1372b [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"
Lei Zhangd607f5b2015-10-05 17:06:09 -07008#include "../../include/javascript/JS_Context.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009#include "../../include/javascript/JS_Define.h"
10#include "../../include/javascript/JS_Object.h"
Lei Zhangd607f5b2015-10-05 17:06:09 -070011
12namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013
Nico Weber9d8ec5a2015-08-04 13:00:21 -070014int FXJS_MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070015 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
Lei Zhangd607f5b2015-10-05 17:06:09 -070028} // namespace
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {}
31
32CJS_EmbedObj::~CJS_EmbedObj() {
33 m_pJSObject = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034}
35
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036int CJS_EmbedObj::MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037 const FX_WCHAR* swMsg,
38 const FX_WCHAR* swTitle,
39 FX_UINT nType,
40 FX_UINT nIcon) {
Lei Zhangd607f5b2015-10-05 17:06:09 -070041 return FXJS_MsgBox(pApp, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042}
43
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
45 CJS_Object::Alert(pContext, swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046}
47
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
49 CJS_Object* pJSObj = data.GetParameter();
50 pJSObj->ExitInstance();
51 delete pJSObj;
Tom Sepez39bfe122015-09-17 15:25:23 -070052 FXJS_FreePrivate(data.GetInternalField(0));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053}
54
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
56 CJS_Object* pJSObj = data.GetParameter();
57 pJSObj->Dispose();
58 data.SetSecondPassCallback(FreeObject);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020059}
60
Tom Sepez116e4ad2015-09-21 09:22:05 -070061CJS_Object::CJS_Object(v8::Local<v8::Object> pObject) {
62 m_pIsolate = pObject->CreationContext()->GetIsolate();
63 m_pV8Object.Reset(m_pIsolate, pObject);
64}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065
Lei Zhang2b1a2d52015-08-14 22:16:22 -070066CJS_Object::~CJS_Object() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070067}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069void CJS_Object::MakeWeak() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070070 m_pV8Object.SetWeak(this, DisposeObject,
71 v8::WeakCallbackType::kInternalFields);
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020072}
73
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074void CJS_Object::Dispose() {
Tom Sepez116e4ad2015-09-21 09:22:05 -070075 m_pV8Object.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076}
77
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078int CJS_Object::MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 const FX_WCHAR* swMsg,
80 const FX_WCHAR* swTitle,
81 FX_UINT nType,
82 FX_UINT nIcon) {
Lei Zhangd607f5b2015-10-05 17:06:09 -070083 return FXJS_MsgBox(pApp, swMsg, swTitle, nType, nIcon);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084}
85
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
87 ASSERT(pContext != NULL);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 if (pContext->IsMsgBoxEnabled()) {
90 CPDFDoc_Environment* pApp = pContext->GetReaderApp();
91 if (pApp)
92 pApp->JS_appAlert(swMsg, NULL, 0, 3);
93 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094}
Tom Sepez371c87f2015-08-13 16:56:19 -070095
Lei Zhang2d5a0e12015-10-05 17:00:03 -070096CJS_Timer::CJS_Timer(CJS_EmbedObj* pObj,
97 CPDFDoc_Environment* pApp,
98 CJS_Runtime* pRuntime,
99 int nType,
100 const CFX_WideString& script,
101 FX_DWORD dwElapse,
102 FX_DWORD dwTimeOut)
103 : m_nTimerID(0),
104 m_pEmbedObj(pObj),
105 m_bProcessing(false),
106 m_bValid(true),
107 m_nType(nType),
108 m_dwTimeOut(dwTimeOut),
109 m_pRuntime(pRuntime),
110 m_pApp(pApp) {
Tom Sepez371c87f2015-08-13 16:56:19 -0700111 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700112 m_nTimerID = pHandler->SetTimer(dwElapse, TimerProc);
Tom Sepez371c87f2015-08-13 16:56:19 -0700113 (*GetGlobalTimerMap())[m_nTimerID] = this;
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700114 m_pRuntime->AddObserver(this);
115}
116
117CJS_Timer::~CJS_Timer() {
118 CJS_Runtime* pRuntime = GetRuntime();
119 if (pRuntime)
120 pRuntime->RemoveObserver(this);
121 KillJSTimer();
Tom Sepez371c87f2015-08-13 16:56:19 -0700122}
123
124void CJS_Timer::KillJSTimer() {
125 if (m_nTimerID) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700126 if (m_bValid) {
127 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
128 pHandler->KillTimer(m_nTimerID);
129 }
Tom Sepez371c87f2015-08-13 16:56:19 -0700130 GetGlobalTimerMap()->erase(m_nTimerID);
131 m_nTimerID = 0;
132 }
133}
134
135// static
136void CJS_Timer::TimerProc(int idEvent) {
137 const auto it = GetGlobalTimerMap()->find(idEvent);
138 if (it != GetGlobalTimerMap()->end()) {
139 CJS_Timer* pTimer = it->second;
140 if (!pTimer->m_bProcessing) {
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700141 CFX_AutoRestorer<bool> scoped_processing(&pTimer->m_bProcessing);
142 pTimer->m_bProcessing = true;
Tom Sepez371c87f2015-08-13 16:56:19 -0700143 if (pTimer->m_pEmbedObj)
144 pTimer->m_pEmbedObj->TimerProc(pTimer);
Tom Sepez371c87f2015-08-13 16:56:19 -0700145 }
146 }
147}
148
149// static
150CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() {
151 // Leak the timer array at shutdown.
152 static auto* s_TimerMap = new TimerMap;
153 return s_TimerMap;
154}
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700155
156void CJS_Timer::OnDestroyed() {
157 m_bValid = false;
158}