OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ | 7 #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ |
8 #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ | 8 #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ |
9 | 9 |
10 #include "../fsdk_define.h" // For FX_UINT | 10 #include "../fsdk_define.h" // For FX_UINT |
11 #include "../fsdk_mgr.h" // For CPDFDoc_Environment | 11 #include "../fsdk_mgr.h" // For CPDFDoc_Environment |
12 #include "../fx_systemhandler.h" // For IFX_SystemHandler | 12 #include "../fx_systemhandler.h" // For IFX_SystemHandler |
13 #include "../jsapi/fxjs_v8.h" | 13 #include "../jsapi/fxjs_v8.h" |
14 | 14 |
15 class CPDFSDK_PageView; | 15 class CPDFSDK_PageView; |
16 class CJS_Object; | 16 class CJS_Object; |
17 class CJS_Timer; | 17 class CJS_Timer; |
18 class CJS_Context; | 18 class CJS_Context; |
19 | 19 |
20 class CJS_EmbedObj | 20 class CJS_EmbedObj { |
21 { | 21 public: |
22 public: | 22 CJS_EmbedObj(CJS_Object* pJSObject); |
23 » CJS_EmbedObj(CJS_Object* pJSObject); | 23 virtual ~CJS_EmbedObj(); |
24 » virtual ~CJS_EmbedObj(); | 24 |
25 | 25 virtual void TimerProc(CJS_Timer* pTimer){}; |
26 » virtual void» » » » TimerProc(CJS_Timer* pTimer){}; | 26 |
27 | 27 CJS_Timer* BeginTimer(CPDFDoc_Environment* pApp, FX_UINT nElapse); |
28 » CJS_Timer*» » » » » BeginTimer(CPDFDoc_Envir
onment * pApp, FX_UINT nElapse); | 28 void EndTimer(CJS_Timer* pTimer); |
29 » void» » » » » » EndTimer(CJS_Timer* pTim
er); | 29 |
30 | 30 CJS_Object* GetJSObject() { return m_pJSObject; }; |
31 » CJS_Object*» » » » » GetJSObject(){return m_p
JSObject;}; | 31 operator CJS_Object*() { return m_pJSObject; }; |
32 » operator» » » » » CJS_Object* (){return m_
pJSObject;}; | 32 |
33 | 33 CPDFSDK_PageView* JSGetPageView(IFXJS_Context* cc); |
34 » CPDFSDK_PageView *» » » JSGetPageView(IFXJS_Context* cc)
; | 34 int MsgBox(CPDFDoc_Environment* pApp, |
35 » int» » » » » » » MsgBox(CPDFDoc_E
nvironment* pApp, CPDFSDK_PageView* pPageView, const FX_WCHAR* swMsg, const FX_W
CHAR* swTitle = NULL, FX_UINT nType = 0, FX_UINT nIcon = 0); | 35 CPDFSDK_PageView* pPageView, |
36 » void» » » » » » Alert(CJS_Context* pCont
ext, const FX_WCHAR* swMsg); | 36 const FX_WCHAR* swMsg, |
37 | 37 const FX_WCHAR* swTitle = NULL, |
38 protected: | 38 FX_UINT nType = 0, |
39 » CJS_Object*» » » » » m_pJSObject; | 39 FX_UINT nIcon = 0); |
40 }; | 40 void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg); |
41 | 41 |
42 class CJS_Object | 42 protected: |
43 { | 43 CJS_Object* m_pJSObject; |
44 public: | 44 }; |
45 » CJS_Object(JSFXObject pObject); | 45 |
46 » virtual ~CJS_Object(void); | 46 class CJS_Object { |
47 | 47 public: |
48 » void» » » » » » MakeWeak(); | 48 CJS_Object(JSFXObject pObject); |
49 void Dispose(); | 49 virtual ~CJS_Object(void); |
50 | 50 |
51 » virtual FX_BOOL»» » » IsType(const FX_CHAR* sClassName
){return TRUE;}; | 51 void MakeWeak(); |
52 » virtual CFX_ByteString» » GetClassName(){return "";}; | 52 void Dispose(); |
53 | 53 |
54 » virtual FX_BOOL»» » » InitInstance(IFXJS_Context* cc){
return TRUE;}; | 54 virtual FX_BOOL IsType(const FX_CHAR* sClassName) { return TRUE; }; |
55 » virtual FX_BOOL»» » » ExitInstance(){return TRUE;}; | 55 virtual CFX_ByteString GetClassName() { return ""; }; |
56 | 56 |
57 » operator» » » » » JSFXObject () {return v8
::Local<v8::Object>::New(m_pIsolate, m_pObject);} | 57 virtual FX_BOOL InitInstance(IFXJS_Context* cc) { return TRUE; }; |
58 » operator» » » » » CJS_EmbedObj* (){return
m_pEmbedObj;}; | 58 virtual FX_BOOL ExitInstance() { return TRUE; }; |
59 | 59 |
60 » void» » » » » » SetEmbedObject(CJS_Embed
Obj* pObj){m_pEmbedObj = pObj;}; | 60 operator JSFXObject() { |
61 » CJS_EmbedObj *» » » » GetEmbedObject(){return m_pEmbed
Obj;}; | 61 return v8::Local<v8::Object>::New(m_pIsolate, m_pObject); |
62 | 62 } |
63 » static CPDFSDK_PageView *» JSGetPageView(IFXJS_Context* cc); | 63 operator CJS_EmbedObj*() { return m_pEmbedObj; }; |
64 » static int» » » » » MsgBox(CPDFDoc_Environme
nt* pApp, CPDFSDK_PageView* pPageView, const FX_WCHAR* swMsg, const FX_WCHAR* sw
Title = NULL, FX_UINT nType = 0,FX_UINT nIcon = 0); | 64 |
65 » static void» » » » » Alert(CJS_Context* pCont
ext, const FX_WCHAR* swMsg); | 65 void SetEmbedObject(CJS_EmbedObj* pObj) { m_pEmbedObj = pObj; }; |
66 | 66 CJS_EmbedObj* GetEmbedObject() { return m_pEmbedObj; }; |
67 » v8::Isolate*» » » » » GetIsolate() {return m_p
Isolate;} | 67 |
68 protected: | 68 static CPDFSDK_PageView* JSGetPageView(IFXJS_Context* cc); |
69 » CJS_EmbedObj *» » » » m_pEmbedObj; | 69 static int MsgBox(CPDFDoc_Environment* pApp, |
70 » v8::Global<v8::Object>» » » m_pObject; | 70 CPDFSDK_PageView* pPageView, |
71 » v8::Isolate*» » » » » m_pIsolate; | 71 const FX_WCHAR* swMsg, |
72 }; | 72 const FX_WCHAR* swTitle = NULL, |
73 | 73 FX_UINT nType = 0, |
74 struct JS_TIMER_MAP | 74 FX_UINT nIcon = 0); |
75 { | 75 static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg); |
76 » FX_UINT nID; | 76 |
77 » CJS_Timer * pTimer; | 77 v8::Isolate* GetIsolate() { return m_pIsolate; } |
78 }; | 78 |
79 | 79 protected: |
80 typedef CFX_ArrayTemplate<JS_TIMER_MAP*>» CTimerMapArray; | 80 CJS_EmbedObj* m_pEmbedObj; |
81 | 81 v8::Global<v8::Object> m_pObject; |
82 struct JS_TIMER_MAPARRAY | 82 v8::Isolate* m_pIsolate; |
83 { | 83 }; |
84 public: | 84 |
85 » JS_TIMER_MAPARRAY() | 85 struct JS_TIMER_MAP { |
86 » { | 86 FX_UINT nID; |
87 » } | 87 CJS_Timer* pTimer; |
88 | 88 }; |
89 » ~JS_TIMER_MAPARRAY() | 89 |
90 » { | 90 typedef CFX_ArrayTemplate<JS_TIMER_MAP*> CTimerMapArray; |
91 » » Reset(); | 91 |
92 » } | 92 struct JS_TIMER_MAPARRAY { |
93 | 93 public: |
94 » void Reset() | 94 JS_TIMER_MAPARRAY() {} |
95 » { | 95 |
96 » » for (int i=0,sz=m_Array.GetSize(); i<sz; i++) | 96 ~JS_TIMER_MAPARRAY() { Reset(); } |
97 » » » delete m_Array.GetAt(i); | 97 |
98 | 98 void Reset() { |
99 » » m_Array.RemoveAll(); | 99 for (int i = 0, sz = m_Array.GetSize(); i < sz; i++) |
100 » } | 100 delete m_Array.GetAt(i); |
101 | 101 |
102 » void SetAt(FX_UINT nIndex,CJS_Timer * pTimer) | 102 m_Array.RemoveAll(); |
103 » { | 103 } |
104 » » int i = Find(nIndex); | 104 |
105 | 105 void SetAt(FX_UINT nIndex, CJS_Timer* pTimer) { |
106 » » if (i>=0) | 106 int i = Find(nIndex); |
107 » » { | 107 |
108 » » » if (JS_TIMER_MAP * pMap = m_Array.GetAt(i)) | 108 if (i >= 0) { |
109 » » » » pMap->pTimer = pTimer; | 109 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i)) |
110 » » } | 110 pMap->pTimer = pTimer; |
111 » » else | 111 } else { |
112 » » { | 112 if (JS_TIMER_MAP* pMap = new JS_TIMER_MAP) { |
113 » » » if (JS_TIMER_MAP * pMap = new JS_TIMER_MAP) | 113 pMap->nID = nIndex; |
114 » » » { | 114 pMap->pTimer = pTimer; |
115 » » » » pMap->nID = nIndex; | 115 m_Array.Add(pMap); |
116 » » » » pMap->pTimer = pTimer; | 116 } |
117 » » » » m_Array.Add(pMap); | 117 } |
118 » » » } | 118 } |
119 » » } | 119 |
120 » } | 120 CJS_Timer* GetAt(FX_UINT nIndex) { |
121 | 121 int i = Find(nIndex); |
122 » CJS_Timer * GetAt(FX_UINT nIndex) | 122 |
123 » { | 123 if (i >= 0) { |
124 » » int i = Find(nIndex); | 124 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i)) |
125 | 125 return pMap->pTimer; |
126 » » if (i>=0) | 126 } |
127 » » { | 127 return NULL; |
128 » » » if (JS_TIMER_MAP * pMap = m_Array.GetAt(i)) | 128 } |
129 » » » » return pMap->pTimer; | 129 |
130 » » } | 130 void RemoveAt(FX_UINT nIndex) { |
131 » » return NULL; | 131 int i = Find(nIndex); |
132 » } | 132 |
133 | 133 if (i >= 0) { |
134 » void RemoveAt(FX_UINT nIndex) | 134 delete m_Array.GetAt(i); |
135 » { | 135 m_Array.RemoveAt(i); |
136 » » int i = Find(nIndex); | 136 } |
137 | 137 // To prevent potential fake memory leak reported by vc6. |
138 » » if (i>=0) | 138 if (m_Array.GetSize() == 0) |
139 » » { | 139 m_Array.RemoveAll(); |
140 » » » delete m_Array.GetAt(i); | 140 } |
141 » » » m_Array.RemoveAt(i); | 141 |
142 » » } | 142 int Find(FX_UINT nIndex) { |
143 » » //To prevent potential fake memory leak reported by vc6. | 143 for (int i = 0, sz = m_Array.GetSize(); i < sz; i++) { |
144 » » if(m_Array.GetSize() == 0) | 144 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i)) { |
145 » » » m_Array.RemoveAll(); | 145 if (pMap->nID == nIndex) |
146 » } | 146 return i; |
147 | 147 } |
148 » int Find(FX_UINT nIndex) | 148 } |
149 » { | 149 |
150 » » for (int i=0,sz=m_Array.GetSize(); i<sz; i++) | 150 return -1; |
151 » » { | 151 } |
152 » » » if (JS_TIMER_MAP * pMap = m_Array.GetAt(i)) | 152 |
153 » » » { | 153 CTimerMapArray m_Array; |
154 » » » » if (pMap->nID == nIndex) | |
155 » » » » » return i; | |
156 » » » } | |
157 » » } | |
158 | |
159 » » return -1; | |
160 » } | |
161 | |
162 » CTimerMapArray» » m_Array; | |
163 }; | 154 }; |
164 | 155 |
165 JS_TIMER_MAPARRAY& GetTimeMap(); | 156 JS_TIMER_MAPARRAY& GetTimeMap(); |
166 | 157 |
167 class CJS_Runtime; | 158 class CJS_Runtime; |
168 | 159 |
169 class CJS_Timer | 160 class CJS_Timer { |
170 { | 161 public: |
171 public: | 162 CJS_Timer(CJS_EmbedObj* pObj, CPDFDoc_Environment* pApp) |
172 » CJS_Timer(CJS_EmbedObj * pObj,CPDFDoc_Environment* pApp): | 163 : m_nTimerID(0), |
173 » » m_nTimerID(0), | 164 m_pEmbedObj(pObj), |
174 » » m_pEmbedObj(pObj), | 165 m_bProcessing(FALSE), |
175 » » m_bProcessing(FALSE), | 166 m_dwStartTime(0), |
176 » » m_dwStartTime(0), | 167 m_dwTimeOut(0), |
177 » » m_dwTimeOut(0), | 168 m_dwElapse(0), |
178 » » m_dwElapse(0), | 169 m_pRuntime(NULL), |
179 » » m_pRuntime(NULL), | 170 m_nType(0), |
180 » » m_nType(0), | 171 m_pApp(pApp) {} |
181 » » m_pApp(pApp) | 172 |
182 » { | 173 virtual ~CJS_Timer() { KillJSTimer(); } |
183 » } | 174 |
184 | 175 public: |
185 » virtual ~CJS_Timer() | 176 FX_UINT SetJSTimer(FX_UINT nElapse) { |
186 » { | 177 if (m_nTimerID) |
187 » » KillJSTimer(); | 178 KillJSTimer(); |
188 » } | 179 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); |
189 | 180 m_nTimerID = pHandler->SetTimer(nElapse, TimerProc); |
190 public: | 181 GetTimeMap().SetAt(m_nTimerID, this); |
191 » FX_UINT SetJSTimer(FX_UINT nElapse) | 182 m_dwElapse = nElapse; |
192 » { | 183 return m_nTimerID; |
193 » » if (m_nTimerID)KillJSTimer(); | 184 }; |
194 » » IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); | 185 |
195 » » m_nTimerID = pHandler->SetTimer(nElapse,TimerProc); | 186 void KillJSTimer() { |
196 » » GetTimeMap().SetAt(m_nTimerID,this); | 187 if (m_nTimerID) { |
197 » » m_dwElapse = nElapse; | 188 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); |
198 » » return m_nTimerID; | 189 pHandler->KillTimer(m_nTimerID); |
199 » }; | 190 GetTimeMap().RemoveAt(m_nTimerID); |
200 | 191 m_nTimerID = 0; |
201 » void KillJSTimer() | 192 } |
202 » { | 193 }; |
203 » » if (m_nTimerID) | 194 |
204 » » { | 195 void SetType(int nType) { m_nType = nType; } |
205 » » » IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); | 196 |
206 » » » pHandler->KillTimer(m_nTimerID); | 197 int GetType() const { return m_nType; } |
207 » » » GetTimeMap().RemoveAt(m_nTimerID); | 198 |
208 » » » m_nTimerID = 0; | 199 void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; } |
209 » » } | 200 |
210 » }; | 201 FX_DWORD GetStartTime() const { return m_dwStartTime; } |
211 | 202 |
212 » void SetType(int nType) | 203 void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; } |
213 » { | 204 |
214 » » m_nType = nType; | 205 FX_DWORD GetTimeOut() const { return m_dwTimeOut; } |
215 » } | 206 |
216 | 207 void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; } |
217 » int GetType() const | 208 |
218 » { | 209 CJS_Runtime* GetRuntime() const { return m_pRuntime; } |
219 » » return m_nType; | 210 |
220 » } | 211 void SetJScript(const CFX_WideString& script) { m_swJScript = script; } |
221 | 212 |
222 » void SetStartTime(FX_DWORD dwStartTime) | 213 CFX_WideString GetJScript() const { return m_swJScript; } |
223 » { | 214 |
224 » » m_dwStartTime = dwStartTime; | 215 static void TimerProc(int idEvent) { |
225 » } | 216 if (CJS_Timer* pTimer = GetTimeMap().GetAt(idEvent)) { |
226 | 217 if (!pTimer->m_bProcessing) { |
227 » FX_DWORD GetStartTime() const | 218 pTimer->m_bProcessing = TRUE; |
228 » { | 219 if (pTimer->m_pEmbedObj) |
229 » » return m_dwStartTime; | 220 pTimer->m_pEmbedObj->TimerProc(pTimer); |
230 » } | 221 pTimer->m_bProcessing = FALSE; |
231 | 222 } else { |
232 » void SetTimeOut(FX_DWORD dwTimeOut) | 223 //» TRACE(L"BUSY!\n"); |
233 » { | 224 } |
234 » » m_dwTimeOut = dwTimeOut; | 225 } |
235 » } | 226 }; |
236 | 227 |
237 » FX_DWORD GetTimeOut() const | 228 private: |
238 » { | 229 FX_UINT m_nTimerID; |
239 » » return m_dwTimeOut; | 230 CJS_EmbedObj* m_pEmbedObj; |
240 » } | 231 FX_BOOL m_bProcessing; |
241 | 232 |
242 » void SetRuntime(CJS_Runtime* pRuntime) | 233 // data |
243 » { | 234 FX_DWORD m_dwStartTime; |
244 » » m_pRuntime = pRuntime; | 235 FX_DWORD m_dwTimeOut; |
245 » } | 236 FX_DWORD m_dwElapse; |
246 | 237 CJS_Runtime* m_pRuntime; |
247 » CJS_Runtime* GetRuntime() const | 238 CFX_WideString m_swJScript; |
248 » { | 239 int m_nType; // 0:Interval; 1:TimeOut |
249 » » return m_pRuntime; | 240 |
250 » } | 241 CPDFDoc_Environment* m_pApp; |
251 | |
252 » void SetJScript(const CFX_WideString& script) | |
253 » { | |
254 » » m_swJScript = script; | |
255 » } | |
256 | |
257 » CFX_WideString GetJScript() const | |
258 » { | |
259 » » return m_swJScript; | |
260 » } | |
261 | |
262 » static void TimerProc(int idEvent) | |
263 » { | |
264 » » if (CJS_Timer * pTimer = GetTimeMap().GetAt(idEvent)) | |
265 » » { | |
266 » » » if (!pTimer->m_bProcessing) | |
267 » » » { | |
268 » » » » pTimer->m_bProcessing = TRUE; | |
269 » » » » if (pTimer->m_pEmbedObj) pTimer->m_pEmbedObj->Ti
merProc(pTimer); | |
270 » » » » pTimer->m_bProcessing = FALSE; | |
271 » » » } | |
272 » » » else | |
273 » » » { | |
274 » » » //» TRACE(L"BUSY!\n"); | |
275 » » » } | |
276 » » } | |
277 » }; | |
278 | |
279 private: | |
280 » FX_UINT»» » » » » » m_nTimerID; | |
281 » CJS_EmbedObj*» » » » » m_pEmbedObj; | |
282 » FX_BOOL»» » » » » » m_bProcessing; | |
283 | |
284 » //data | |
285 » FX_DWORD» » » » » » » m_dwStar
tTime; | |
286 » FX_DWORD» » » » » » » m_dwTime
Out; | |
287 » FX_DWORD» » » » » » m_dwElapse; | |
288 » CJS_Runtime*» » » » » m_pRuntime; | |
289 » CFX_WideString» » » » » m_swJScript; | |
290 » int» » » » » » » » m_nType;
//0:Interval; 1:TimeOut | |
291 | |
292 » CPDFDoc_Environment*» » » m_pApp; | |
293 }; | 242 }; |
294 | 243 |
295 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ | 244 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ |
OLD | NEW |