Remove JavaScript.h
[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
12 namespace {
13
14 int FXJS_MsgBox(CPDFDoc_Environment* pApp,
15                 const FX_WCHAR* swMsg,
16                 const FX_WCHAR* swTitle,
17                 FX_UINT nType,
18                 FX_UINT nIcon) {
19   if (!pApp)
20     return 0;
21
22   if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument())
23     pDoc->KillFocusAnnot();
24
25   return pApp->JS_appAlert(swMsg, swTitle, nType, nIcon);
26 }
27
28 }  // namespace
29
30 CJS_EmbedObj::CJS_EmbedObj(CJS_Object* pJSObject) : m_pJSObject(pJSObject) {}
31
32 CJS_EmbedObj::~CJS_EmbedObj() {
33   m_pJSObject = NULL;
34 }
35
36 int CJS_EmbedObj::MsgBox(CPDFDoc_Environment* pApp,
37                          const FX_WCHAR* swMsg,
38                          const FX_WCHAR* swTitle,
39                          FX_UINT nType,
40                          FX_UINT nIcon) {
41   return FXJS_MsgBox(pApp, swMsg, swTitle, nType, nIcon);
42 }
43
44 void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
45   CJS_Object::Alert(pContext, swMsg);
46 }
47
48 void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
49   CJS_Object* pJSObj = data.GetParameter();
50   pJSObj->ExitInstance();
51   delete pJSObj;
52   FXJS_FreePrivate(data.GetInternalField(0));
53 }
54
55 void DisposeObject(const v8::WeakCallbackInfo<CJS_Object>& data) {
56   CJS_Object* pJSObj = data.GetParameter();
57   pJSObj->Dispose();
58   data.SetSecondPassCallback(FreeObject);
59 }
60
61 CJS_Object::CJS_Object(v8::Local<v8::Object> pObject) {
62   m_pIsolate = pObject->CreationContext()->GetIsolate();
63   m_pV8Object.Reset(m_pIsolate, pObject);
64 }
65
66 CJS_Object::~CJS_Object() {
67 }
68
69 void CJS_Object::MakeWeak() {
70   m_pV8Object.SetWeak(this, DisposeObject,
71                       v8::WeakCallbackType::kInternalFields);
72 }
73
74 void CJS_Object::Dispose() {
75   m_pV8Object.Reset();
76 }
77
78 int CJS_Object::MsgBox(CPDFDoc_Environment* pApp,
79                        const FX_WCHAR* swMsg,
80                        const FX_WCHAR* swTitle,
81                        FX_UINT nType,
82                        FX_UINT nIcon) {
83   return FXJS_MsgBox(pApp, swMsg, swTitle, nType, nIcon);
84 }
85
86 void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) {
87   ASSERT(pContext != NULL);
88
89   if (pContext->IsMsgBoxEnabled()) {
90     CPDFDoc_Environment* pApp = pContext->GetReaderApp();
91     if (pApp)
92       pApp->JS_appAlert(swMsg, NULL, 0, 3);
93   }
94 }
95
96 CJS_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) {
111   IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
112   m_nTimerID = pHandler->SetTimer(dwElapse, TimerProc);
113   (*GetGlobalTimerMap())[m_nTimerID] = this;
114   m_pRuntime->AddObserver(this);
115 }
116
117 CJS_Timer::~CJS_Timer() {
118   CJS_Runtime* pRuntime = GetRuntime();
119   if (pRuntime)
120     pRuntime->RemoveObserver(this);
121   KillJSTimer();
122 }
123
124 void CJS_Timer::KillJSTimer() {
125   if (m_nTimerID) {
126     if (m_bValid) {
127       IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
128       pHandler->KillTimer(m_nTimerID);
129     }
130     GetGlobalTimerMap()->erase(m_nTimerID);
131     m_nTimerID = 0;
132   }
133 }
134
135 // static
136 void 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) {
141       CFX_AutoRestorer<bool> scoped_processing(&pTimer->m_bProcessing);
142       pTimer->m_bProcessing = true;
143       if (pTimer->m_pEmbedObj)
144         pTimer->m_pEmbedObj->TimerProc(pTimer);
145     }
146   }
147 }
148
149 // static
150 CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() {
151   // Leak the timer array at shutdown.
152   static auto* s_TimerMap = new TimerMap;
153   return s_TimerMap;
154 }
155
156 void CJS_Timer::OnDestroyed() {
157   m_bValid = false;
158 }