Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(515)

Side by Side Diff: fpdfsdk/include/javascript/JS_Object.h

Issue 1286383004: Kill JS_TIMER_MAPARRAY (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Clang-format Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | fpdfsdk/src/javascript/JS_Object.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <map>
11
10 #include "../fsdk_define.h" // For FX_UINT 12 #include "../fsdk_define.h" // For FX_UINT
11 #include "../fsdk_mgr.h" // For CPDFDoc_Environment 13 #include "../fsdk_mgr.h" // For CPDFDoc_Environment
12 #include "../fx_systemhandler.h" // For IFX_SystemHandler 14 #include "../fx_systemhandler.h" // For IFX_SystemHandler
13 #include "../jsapi/fxjs_v8.h" 15 #include "../jsapi/fxjs_v8.h"
14 16
15 class CPDFSDK_PageView; 17 class CPDFSDK_PageView;
16 class CJS_Object; 18 class CJS_Object;
17 class CJS_Timer; 19 class CJS_Timer;
18 class CJS_Context; 20 class CJS_Context;
19 21
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg); 77 static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
76 78
77 v8::Isolate* GetIsolate() { return m_pIsolate; } 79 v8::Isolate* GetIsolate() { return m_pIsolate; }
78 80
79 protected: 81 protected:
80 CJS_EmbedObj* m_pEmbedObj; 82 CJS_EmbedObj* m_pEmbedObj;
81 v8::Global<v8::Object> m_pObject; 83 v8::Global<v8::Object> m_pObject;
82 v8::Isolate* m_pIsolate; 84 v8::Isolate* m_pIsolate;
83 }; 85 };
84 86
85 struct JS_TIMER_MAP { 87 using JSTimerMap = std::map<FX_UINT, CJS_Timer*>;
86 FX_UINT nID; 88 JSTimerMap* GetGlobalTimerMap();
87 CJS_Timer* pTimer;
88 };
89
90 typedef CFX_ArrayTemplate<JS_TIMER_MAP*> CTimerMapArray;
91
92 struct JS_TIMER_MAPARRAY {
93 public:
94 JS_TIMER_MAPARRAY() {}
95
96 ~JS_TIMER_MAPARRAY() { Reset(); }
97
98 void Reset() {
99 for (int i = 0, sz = m_Array.GetSize(); i < sz; i++)
100 delete m_Array.GetAt(i);
101
102 m_Array.RemoveAll();
103 }
104
105 void SetAt(FX_UINT nIndex, CJS_Timer* pTimer) {
106 int i = Find(nIndex);
107
108 if (i >= 0) {
109 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i))
110 pMap->pTimer = pTimer;
111 } else {
112 JS_TIMER_MAP* pMap = new JS_TIMER_MAP;
113 pMap->nID = nIndex;
114 pMap->pTimer = pTimer;
115 m_Array.Add(pMap);
116 }
117 }
118
119 CJS_Timer* GetAt(FX_UINT nIndex) {
120 int i = Find(nIndex);
121
122 if (i >= 0) {
123 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i))
124 return pMap->pTimer;
125 }
126 return NULL;
127 }
128
129 void RemoveAt(FX_UINT nIndex) {
130 int i = Find(nIndex);
131
132 if (i >= 0) {
133 delete m_Array.GetAt(i);
134 m_Array.RemoveAt(i);
135 }
136 // To prevent potential fake memory leak reported by vc6.
137 if (m_Array.GetSize() == 0)
138 m_Array.RemoveAll();
139 }
140
141 int Find(FX_UINT nIndex) {
142 for (int i = 0, sz = m_Array.GetSize(); i < sz; i++) {
143 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i)) {
144 if (pMap->nID == nIndex)
145 return i;
146 }
147 }
148
149 return -1;
150 }
151
152 CTimerMapArray m_Array;
153 };
154
155 JS_TIMER_MAPARRAY& GetTimeMap();
156 89
157 class CJS_Runtime; 90 class CJS_Runtime;
158 91
159 class CJS_Timer { 92 class CJS_Timer {
160 public: 93 public:
161 CJS_Timer(CJS_EmbedObj* pObj, CPDFDoc_Environment* pApp) 94 CJS_Timer(CJS_EmbedObj* pObj, CPDFDoc_Environment* pApp)
162 : m_nTimerID(0), 95 : m_nTimerID(0),
163 m_pEmbedObj(pObj), 96 m_pEmbedObj(pObj),
164 m_bProcessing(FALSE), 97 m_bProcessing(FALSE),
165 m_dwStartTime(0), 98 m_dwStartTime(0),
166 m_dwTimeOut(0), 99 m_dwTimeOut(0),
167 m_dwElapse(0), 100 m_dwElapse(0),
168 m_pRuntime(NULL), 101 m_pRuntime(NULL),
169 m_nType(0), 102 m_nType(0),
170 m_pApp(pApp) {} 103 m_pApp(pApp) {}
171 104
172 virtual ~CJS_Timer() { KillJSTimer(); } 105 virtual ~CJS_Timer() { KillJSTimer(); }
173 106
174 public: 107 public:
175 FX_UINT SetJSTimer(FX_UINT nElapse) { 108 FX_UINT SetJSTimer(FX_UINT nElapse) {
176 if (m_nTimerID) 109 if (m_nTimerID)
177 KillJSTimer(); 110 KillJSTimer();
178 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); 111 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
179 m_nTimerID = pHandler->SetTimer(nElapse, TimerProc); 112 m_nTimerID = pHandler->SetTimer(nElapse, TimerProc);
180 GetTimeMap().SetAt(m_nTimerID, this); 113 (*GetGlobalTimerMap())[m_nTimerID] = this;
181 m_dwElapse = nElapse; 114 m_dwElapse = nElapse;
182 return m_nTimerID; 115 return m_nTimerID;
183 }; 116 };
184 117
185 void KillJSTimer() { 118 void KillJSTimer() {
186 if (m_nTimerID) { 119 if (m_nTimerID) {
187 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); 120 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
188 pHandler->KillTimer(m_nTimerID); 121 pHandler->KillTimer(m_nTimerID);
189 GetTimeMap().RemoveAt(m_nTimerID); 122 GetGlobalTimerMap()->erase(m_nTimerID);
190 m_nTimerID = 0; 123 m_nTimerID = 0;
191 } 124 }
192 }; 125 };
193 126
194 void SetType(int nType) { m_nType = nType; } 127 void SetType(int nType) { m_nType = nType; }
195 128
196 int GetType() const { return m_nType; } 129 int GetType() const { return m_nType; }
197 130
198 void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; } 131 void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; }
199 132
200 FX_DWORD GetStartTime() const { return m_dwStartTime; } 133 FX_DWORD GetStartTime() const { return m_dwStartTime; }
201 134
202 void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; } 135 void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; }
203 136
204 FX_DWORD GetTimeOut() const { return m_dwTimeOut; } 137 FX_DWORD GetTimeOut() const { return m_dwTimeOut; }
205 138
206 void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; } 139 void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; }
207 140
208 CJS_Runtime* GetRuntime() const { return m_pRuntime; } 141 CJS_Runtime* GetRuntime() const { return m_pRuntime; }
209 142
210 void SetJScript(const CFX_WideString& script) { m_swJScript = script; } 143 void SetJScript(const CFX_WideString& script) { m_swJScript = script; }
211 144
212 CFX_WideString GetJScript() const { return m_swJScript; } 145 CFX_WideString GetJScript() const { return m_swJScript; }
213 146
214 static void TimerProc(int idEvent) { 147 static void TimerProc(int idEvent) {
215 if (CJS_Timer* pTimer = GetTimeMap().GetAt(idEvent)) { 148 const auto it = GetGlobalTimerMap()->find(idEvent);
149 if (it != GetGlobalTimerMap()->end()) {
150 CJS_Timer* pTimer = it->second;
216 if (!pTimer->m_bProcessing) { 151 if (!pTimer->m_bProcessing) {
217 pTimer->m_bProcessing = TRUE; 152 pTimer->m_bProcessing = TRUE;
218 if (pTimer->m_pEmbedObj) 153 if (pTimer->m_pEmbedObj)
219 pTimer->m_pEmbedObj->TimerProc(pTimer); 154 pTimer->m_pEmbedObj->TimerProc(pTimer);
220 pTimer->m_bProcessing = FALSE; 155 pTimer->m_bProcessing = FALSE;
221 } else {
222 // TRACE(L"BUSY!\n");
223 } 156 }
224 } 157 }
225 }; 158 };
226 159
227 private: 160 private:
228 FX_UINT m_nTimerID; 161 FX_UINT m_nTimerID;
229 CJS_EmbedObj* m_pEmbedObj; 162 CJS_EmbedObj* m_pEmbedObj;
230 FX_BOOL m_bProcessing; 163 FX_BOOL m_bProcessing;
231 164
232 // data 165 // data
233 FX_DWORD m_dwStartTime; 166 FX_DWORD m_dwStartTime;
234 FX_DWORD m_dwTimeOut; 167 FX_DWORD m_dwTimeOut;
235 FX_DWORD m_dwElapse; 168 FX_DWORD m_dwElapse;
236 CJS_Runtime* m_pRuntime; 169 CJS_Runtime* m_pRuntime;
237 CFX_WideString m_swJScript; 170 CFX_WideString m_swJScript;
238 int m_nType; // 0:Interval; 1:TimeOut 171 int m_nType; // 0:Interval; 1:TimeOut
239 172
240 CPDFDoc_Environment* m_pApp; 173 CPDFDoc_Environment* m_pApp;
241 }; 174 };
242 175
243 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ 176 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_
OLDNEW
« no previous file with comments | « no previous file | fpdfsdk/src/javascript/JS_Object.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698