John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 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. |
Tom Sepez | c6ab172 | 2015-02-05 15:27:25 -0800 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Tom Sepez | 19922bb | 2015-05-28 13:23:12 -0700 | [diff] [blame] | 7 | #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ |
| 8 | #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 9 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 10 | #include "../fsdk_define.h" // For FX_UINT |
| 11 | #include "../fsdk_mgr.h" // For CPDFDoc_Environment |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 12 | #include "../fx_systemhandler.h" // For IFX_SystemHandler |
Tom Sepez | 9a3f812 | 2015-04-07 15:35:48 -0700 | [diff] [blame] | 13 | #include "../jsapi/fxjs_v8.h" |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 14 | |
| 15 | class CPDFSDK_PageView; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 16 | class CJS_Object; |
| 17 | class CJS_Timer; |
| 18 | class CJS_Context; |
| 19 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 20 | class CJS_EmbedObj { |
| 21 | public: |
| 22 | CJS_EmbedObj(CJS_Object* pJSObject); |
| 23 | virtual ~CJS_EmbedObj(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 24 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 25 | virtual void TimerProc(CJS_Timer* pTimer){}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 26 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 27 | CJS_Timer* BeginTimer(CPDFDoc_Environment* pApp, FX_UINT nElapse); |
| 28 | void EndTimer(CJS_Timer* pTimer); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 29 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 30 | CJS_Object* GetJSObject() { return m_pJSObject; }; |
| 31 | operator CJS_Object*() { return m_pJSObject; }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 32 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 33 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 41 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 42 | protected: |
| 43 | CJS_Object* m_pJSObject; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 46 | class CJS_Object { |
| 47 | public: |
| 48 | CJS_Object(JSFXObject pObject); |
| 49 | virtual ~CJS_Object(void); |
Tom Sepez | c6ab172 | 2015-02-05 15:27:25 -0800 | [diff] [blame] | 50 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 51 | void MakeWeak(); |
| 52 | void Dispose(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 53 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 54 | virtual FX_BOOL IsType(const FX_CHAR* sClassName) { return TRUE; }; |
| 55 | virtual CFX_ByteString GetClassName() { return ""; }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 56 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 57 | virtual FX_BOOL InitInstance(IFXJS_Context* cc) { return TRUE; }; |
| 58 | virtual FX_BOOL ExitInstance() { return TRUE; }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 59 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 60 | operator JSFXObject() { |
| 61 | return v8::Local<v8::Object>::New(m_pIsolate, m_pObject); |
| 62 | } |
| 63 | operator CJS_EmbedObj*() { return m_pEmbedObj; }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 64 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 65 | void SetEmbedObject(CJS_EmbedObj* pObj) { m_pEmbedObj = pObj; }; |
| 66 | CJS_EmbedObj* GetEmbedObject() { return m_pEmbedObj; }; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 67 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 68 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 76 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 77 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 85 | struct JS_TIMER_MAP { |
| 86 | FX_UINT nID; |
| 87 | CJS_Timer* pTimer; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 90 | typedef CFX_ArrayTemplate<JS_TIMER_MAP*> CTimerMapArray; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 91 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 92 | struct JS_TIMER_MAPARRAY { |
| 93 | public: |
| 94 | JS_TIMER_MAPARRAY() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 95 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 96 | ~JS_TIMER_MAPARRAY() { Reset(); } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 97 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 98 | void Reset() { |
| 99 | for (int i = 0, sz = m_Array.GetSize(); i < sz; i++) |
| 100 | delete m_Array.GetAt(i); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 101 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 102 | m_Array.RemoveAll(); |
| 103 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 104 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 105 | void SetAt(FX_UINT nIndex, CJS_Timer* pTimer) { |
| 106 | int i = Find(nIndex); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 107 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 108 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 119 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 120 | CJS_Timer* GetAt(FX_UINT nIndex) { |
| 121 | int i = Find(nIndex); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 122 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 123 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 129 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 130 | void RemoveAt(FX_UINT nIndex) { |
| 131 | int i = Find(nIndex); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 132 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 133 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 141 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 142 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 149 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 150 | return -1; |
| 151 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 152 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 153 | CTimerMapArray m_Array; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Bruce Dawson | b4649dc | 2015-01-05 13:26:17 -0800 | [diff] [blame] | 156 | JS_TIMER_MAPARRAY& GetTimeMap(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 157 | |
| 158 | class CJS_Runtime; |
| 159 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 160 | class 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 Sepez | c6ab172 | 2015-02-05 15:27:25 -0800 | [diff] [blame] | 172 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 173 | virtual ~CJS_Timer() { KillJSTimer(); } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 174 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 175 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 185 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 199 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 200 | void SetType(int nType) { m_nType = nType; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 201 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 202 | int GetType() const { return m_nType; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 203 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 204 | void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 205 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 206 | FX_DWORD GetStartTime() const { return m_dwStartTime; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 207 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 208 | void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 209 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 210 | FX_DWORD GetTimeOut() const { return m_dwTimeOut; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 211 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 212 | void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; } |
Tom Sepez | c6ab172 | 2015-02-05 15:27:25 -0800 | [diff] [blame] | 213 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 214 | CJS_Runtime* GetRuntime() const { return m_pRuntime; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 215 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 216 | void SetJScript(const CFX_WideString& script) { m_swJScript = script; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 217 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 218 | CFX_WideString GetJScript() const { return m_swJScript; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 219 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 220 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 232 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 233 | private: |
| 234 | FX_UINT m_nTimerID; |
| 235 | CJS_EmbedObj* m_pEmbedObj; |
| 236 | FX_BOOL m_bProcessing; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 237 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 238 | // 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 245 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 246 | CPDFDoc_Environment* m_pApp; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 247 | }; |
Tom Sepez | 19922bb | 2015-05-28 13:23:12 -0700 | [diff] [blame] | 248 | |
| 249 | #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ |