blob: fe8a91f4712bc8dbc9885b26896e871af4a451a6 [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.
Tom Sepezc6ab1722015-02-05 15:27:25 -08004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepez19922bb2015-05-28 13:23:12 -07007#ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_
8#define FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
Nico Weber9d8ec5a2015-08-04 13:00:21 -070010#include "../fsdk_define.h" // For FX_UINT
11#include "../fsdk_mgr.h" // For CPDFDoc_Environment
Tom Sepez3a832662015-03-02 12:59:05 -080012#include "../fx_systemhandler.h" // For IFX_SystemHandler
Tom Sepez9a3f8122015-04-07 15:35:48 -070013#include "../jsapi/fxjs_v8.h"
Tom Sepez3a832662015-03-02 12:59:05 -080014
15class CPDFSDK_PageView;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016class CJS_Object;
17class CJS_Timer;
18class CJS_Context;
19
Nico Weber9d8ec5a2015-08-04 13:00:21 -070020class CJS_EmbedObj {
21 public:
22 CJS_EmbedObj(CJS_Object* pJSObject);
23 virtual ~CJS_EmbedObj();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025 virtual void TimerProc(CJS_Timer* pTimer){};
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 CJS_Timer* BeginTimer(CPDFDoc_Environment* pApp, FX_UINT nElapse);
28 void EndTimer(CJS_Timer* pTimer);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030 CJS_Object* GetJSObject() { return m_pJSObject; };
31 operator CJS_Object*() { return m_pJSObject; };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 CPDFSDK_PageView* JSGetPageView(IFXJS_Context* cc);
34 int MsgBox(CPDFDoc_Environment* pApp,
35 CPDFSDK_PageView* pPageView,
36 const FX_WCHAR* swMsg,
37 const FX_WCHAR* swTitle = NULL,
38 FX_UINT nType = 0,
39 FX_UINT nIcon = 0);
40 void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 protected:
43 CJS_Object* m_pJSObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044};
45
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046class CJS_Object {
47 public:
48 CJS_Object(JSFXObject pObject);
49 virtual ~CJS_Object(void);
Tom Sepezc6ab1722015-02-05 15:27:25 -080050
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 void MakeWeak();
52 void Dispose();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 virtual FX_BOOL IsType(const FX_CHAR* sClassName) { return TRUE; };
55 virtual CFX_ByteString GetClassName() { return ""; };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 virtual FX_BOOL InitInstance(IFXJS_Context* cc) { return TRUE; };
58 virtual FX_BOOL ExitInstance() { return TRUE; };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 operator JSFXObject() {
61 return v8::Local<v8::Object>::New(m_pIsolate, m_pObject);
62 }
63 operator CJS_EmbedObj*() { return m_pEmbedObj; };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 void SetEmbedObject(CJS_EmbedObj* pObj) { m_pEmbedObj = pObj; };
66 CJS_EmbedObj* GetEmbedObject() { return m_pEmbedObj; };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 static CPDFSDK_PageView* JSGetPageView(IFXJS_Context* cc);
69 static int MsgBox(CPDFDoc_Environment* pApp,
70 CPDFSDK_PageView* pPageView,
71 const FX_WCHAR* swMsg,
72 const FX_WCHAR* swTitle = NULL,
73 FX_UINT nType = 0,
74 FX_UINT nIcon = 0);
75 static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 v8::Isolate* GetIsolate() { return m_pIsolate; }
78
79 protected:
80 CJS_EmbedObj* m_pEmbedObj;
81 v8::Global<v8::Object> m_pObject;
82 v8::Isolate* m_pIsolate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070083};
84
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085struct JS_TIMER_MAP {
86 FX_UINT nID;
87 CJS_Timer* pTimer;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088};
89
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090typedef CFX_ArrayTemplate<JS_TIMER_MAP*> CTimerMapArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092struct JS_TIMER_MAPARRAY {
93 public:
94 JS_TIMER_MAPARRAY() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 ~JS_TIMER_MAPARRAY() { Reset(); }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 void Reset() {
99 for (int i = 0, sz = m_Array.GetSize(); i < sz; i++)
100 delete m_Array.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 m_Array.RemoveAll();
103 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700104
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 void SetAt(FX_UINT nIndex, CJS_Timer* pTimer) {
106 int i = Find(nIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 if (i >= 0) {
109 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i))
110 pMap->pTimer = pTimer;
111 } else {
112 if (JS_TIMER_MAP* pMap = new JS_TIMER_MAP) {
113 pMap->nID = nIndex;
114 pMap->pTimer = pTimer;
115 m_Array.Add(pMap);
116 }
117 }
118 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700119
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 CJS_Timer* GetAt(FX_UINT nIndex) {
121 int i = Find(nIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123 if (i >= 0) {
124 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i))
125 return pMap->pTimer;
126 }
127 return NULL;
128 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 void RemoveAt(FX_UINT nIndex) {
131 int i = Find(nIndex);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 if (i >= 0) {
134 delete m_Array.GetAt(i);
135 m_Array.RemoveAt(i);
136 }
137 // To prevent potential fake memory leak reported by vc6.
138 if (m_Array.GetSize() == 0)
139 m_Array.RemoveAll();
140 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142 int Find(FX_UINT nIndex) {
143 for (int i = 0, sz = m_Array.GetSize(); i < sz; i++) {
144 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i)) {
145 if (pMap->nID == nIndex)
146 return i;
147 }
148 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700149
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 return -1;
151 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 CTimerMapArray m_Array;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700154};
155
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800156JS_TIMER_MAPARRAY& GetTimeMap();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700157
158class CJS_Runtime;
159
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160class CJS_Timer {
161 public:
162 CJS_Timer(CJS_EmbedObj* pObj, CPDFDoc_Environment* pApp)
163 : m_nTimerID(0),
164 m_pEmbedObj(pObj),
165 m_bProcessing(FALSE),
166 m_dwStartTime(0),
167 m_dwTimeOut(0),
168 m_dwElapse(0),
169 m_pRuntime(NULL),
170 m_nType(0),
171 m_pApp(pApp) {}
Tom Sepezc6ab1722015-02-05 15:27:25 -0800172
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173 virtual ~CJS_Timer() { KillJSTimer(); }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700175 public:
176 FX_UINT SetJSTimer(FX_UINT nElapse) {
177 if (m_nTimerID)
178 KillJSTimer();
179 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
180 m_nTimerID = pHandler->SetTimer(nElapse, TimerProc);
181 GetTimeMap().SetAt(m_nTimerID, this);
182 m_dwElapse = nElapse;
183 return m_nTimerID;
184 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 void KillJSTimer() {
187 if (m_nTimerID) {
188 if (m_pApp == NULL) {
189 GetTimeMap().RemoveAt(m_nTimerID);
190 m_nTimerID = 0;
191 return;
192 }
193 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
194 pHandler->KillTimer(m_nTimerID);
195 GetTimeMap().RemoveAt(m_nTimerID);
196 m_nTimerID = 0;
197 }
198 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 void SetType(int nType) { m_nType = nType; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700201
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 int GetType() const { return m_nType; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700204 void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700205
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 FX_DWORD GetStartTime() const { return m_dwStartTime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 FX_DWORD GetTimeOut() const { return m_dwTimeOut; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; }
Tom Sepezc6ab1722015-02-05 15:27:25 -0800213
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 CJS_Runtime* GetRuntime() const { return m_pRuntime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700215
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 void SetJScript(const CFX_WideString& script) { m_swJScript = script; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 CFX_WideString GetJScript() const { return m_swJScript; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700219
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 static void TimerProc(int idEvent) {
221 if (CJS_Timer* pTimer = GetTimeMap().GetAt(idEvent)) {
222 if (!pTimer->m_bProcessing) {
223 pTimer->m_bProcessing = TRUE;
224 if (pTimer->m_pEmbedObj)
225 pTimer->m_pEmbedObj->TimerProc(pTimer);
226 pTimer->m_bProcessing = FALSE;
227 } else {
228 // TRACE(L"BUSY!\n");
229 }
230 }
231 };
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700232
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 private:
234 FX_UINT m_nTimerID;
235 CJS_EmbedObj* m_pEmbedObj;
236 FX_BOOL m_bProcessing;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 // data
239 FX_DWORD m_dwStartTime;
240 FX_DWORD m_dwTimeOut;
241 FX_DWORD m_dwElapse;
242 CJS_Runtime* m_pRuntime;
243 CFX_WideString m_swJScript;
244 int m_nType; // 0:Interval; 1:TimeOut
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700245
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 CPDFDoc_Environment* m_pApp;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700247};
Tom Sepez19922bb2015-05-28 13:23:12 -0700248
249#endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_