Remove pointless CPDFSDK_PageView usage in CJS_Object / CJS_EmbedObj.
[pdfium.git] / fpdfsdk / src / javascript / JS_Object.cpp
1 // 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.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "../../include/javascript/IJavaScript.h"
8 #include "../../include/javascript/JS_Context.h"
9 #include "../../include/javascript/JS_Define.h"
10 #include "../../include/javascript/JS_Object.h"
11 #include "../../include/javascript/JavaScript.h"
12
13 namespace {
14
15 int FXJS_MsgBox(CPDFDoc_Environment* pApp,
16                 const FX_WCHAR* swMsg,
17                 const FX_WCHAR* swTitle,
18                 FX_UINT nType,
19                 FX_UINT nIcon) {
20   if (!pApp)
21     return 0;
22
23   if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument())
24     pDoc->KillFocusAnnot();
25
26   return pApp->JS_appAlert(swMsg, swTitle, nType, nIcon);
27 }
28
29 }  // namespace
30
31 CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {}
32
33 CJS_EmbedObj::~CJS_EmbedObj() {
34   m_pJSObject = NULL;
35 }
36
37 int CJS_EmbedObj::MsgBox(CPDFDoc_Environment* pApp,
38                          const FX_WCHAR* swMsg,
39                          const FX_WCHAR* swTitle,
40                          FX_UINT nType,
41                          FX_UINT nIcon) {
42   return FXJS_MsgBox(pApp, swMsg, swTitle, nType, nIcon);
43 }
44
45 void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
46   CJS_Object::Alert(pContext, swMsg);
47 }
48
49 void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
50   CJS_Object* pJSObj = data.GetParameter();
51   pJSObj->ExitInstance();
52   delete pJSObj;
53   FXJS_FreePrivate(data.GetInternalField(0));
54 }
55
56 void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
57   CJS_Object* pJSObj = data.GetParameter();
58   pJSObj->Dispose();
59   data.SetSecondPassCallback(FreeObject);
60 }
61
62 CJS_Object::CJS_Object(v8::Local<v8::Object> pObject) {
63   m_pIsolate = pObject->CreationContext()->GetIsolate();
64   m_pV8Object.Reset(m_pIsolate, pObject);
65 }
66
67 CJS_Object::~CJS_Object() {
68 }
69
70 void CJS_Object::MakeWeak() {
71   m_pV8Object.SetWeak(this, DisposeObject,
72                       v8::WeakCallbackType::kInternalFields);
73 }
74
75 void CJS_Object::Dispose() {
76   m_pV8Object.Reset();
77 }
78
79 int CJS_Object::MsgBox(CPDFDoc_Environment* pApp,
80                        const FX_WCHAR* swMsg,
81                        const FX_WCHAR* swTitle,
82                        FX_UINT nType,
83                        FX_UINT nIcon) {
84   return FXJS_MsgBox(pApp, swMsg, swTitle, nType, nIcon);
85 }
86
87 void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
88   ASSERT(pContext != NULL);
89
90   if (pContext->IsMsgBoxEnabled()) {
91     CPDFDoc_Environment* pApp = pContext->GetReaderApp();
92     if (pApp)
93       pApp->JS_appAlert(swMsg, NULL, 0, 3);
94   }
95 }
96
97 CJS_Timer::CJS_Timer(CJS_EmbedObj* pObj,
98                      CPDFDoc_Environment* pApp,
99                      CJS_Runtime* pRuntime,
100                      int nType,
101                      const CFX_WideString& script,
102                      FX_DWORD dwElapse,
103                      FX_DWORD dwTimeOut)
104     : m_nTimerID(0),
105       m_pEmbedObj(pObj),
106       m_bProcessing(false),
107       m_bValid(true),
108       m_nType(nType),
109       m_dwTimeOut(dwTimeOut),
110       m_pRuntime(pRuntime),
111       m_pApp(pApp) {
112   IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
113   m_nTimerID = pHandler->SetTimer(dwElapse, TimerProc);
114   (*GetGlobalTimerMap())[m_nTimerID] = this;
115   m_pRuntime->AddObserver(this);
116 }
117
118 CJS_Timer::~CJS_Timer() {
119   CJS_Runtime* pRuntime = GetRuntime();
120   if (pRuntime)
121     pRuntime->RemoveObserver(this);
122   KillJSTimer();
123 }
124
125 void CJS_Timer::KillJSTimer() {
126   if (m_nTimerID) {
127     if (m_bValid) {
128       IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
129       pHandler->KillTimer(m_nTimerID);
130     }
131     GetGlobalTimerMap()->erase(m_nTimerID);
132     m_nTimerID = 0;
133   }
134 }
135
136 // static
137 void CJS_Timer::TimerProc(int idEvent) {
138   const auto it = GetGlobalTimerMap()->find(idEvent);
139   if (it != GetGlobalTimerMap()->end()) {
140     CJS_Timer* pTimer = it->second;
141     if (!pTimer->m_bProcessing) {
142       CFX_AutoRestorer<bool> scoped_processing(&pTimer->m_bProcessing);
143       pTimer->m_bProcessing = true;
144       if (pTimer->m_pEmbedObj)
145         pTimer->m_pEmbedObj->TimerProc(pTimer);
146     }
147   }
148 }
149
150 // static
151 CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() {
152   // Leak the timer array at shutdown.
153   static auto* s_TimerMap = new TimerMap;
154   return s_TimerMap;
155 }
156
157 void CJS_Timer::OnDestroyed() {
158   m_bValid = false;
159 }