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 <map> | 10 #include <map> |
11 | 11 |
12 #include "../../../third_party/base/nonstd_unique_ptr.h" | |
13 | |
12 #include "../fsdk_define.h" // For FX_UINT | 14 #include "../fsdk_define.h" // For FX_UINT |
13 #include "../fsdk_mgr.h" // For CPDFDoc_Environment | 15 #include "../fsdk_mgr.h" // For CPDFDoc_Environment |
14 #include "../fx_systemhandler.h" // For IFX_SystemHandler | 16 #include "../fx_systemhandler.h" // For IFX_SystemHandler |
15 #include "../jsapi/fxjs_v8.h" | 17 #include "../jsapi/fxjs_v8.h" |
16 | 18 |
17 class CPDFSDK_PageView; | 19 class CPDFSDK_PageView; |
20 class CJS_Context; | |
18 class CJS_Object; | 21 class CJS_Object; |
22 class CJS_Runtime; | |
19 class CJS_Timer; | 23 class CJS_Timer; |
20 class CJS_Context; | |
21 | 24 |
22 class CJS_EmbedObj { | 25 class CJS_EmbedObj { |
23 public: | 26 public: |
24 CJS_EmbedObj(CJS_Object* pJSObject); | 27 explicit CJS_EmbedObj(CJS_Object* pJSObject); |
25 virtual ~CJS_EmbedObj(); | 28 virtual ~CJS_EmbedObj(); |
26 | 29 |
27 virtual void TimerProc(CJS_Timer* pTimer){}; | 30 virtual void TimerProc(CJS_Timer* pTimer) {} |
28 | |
29 CJS_Timer* BeginTimer(CPDFDoc_Environment* pApp, FX_UINT nElapse); | 31 CJS_Timer* BeginTimer(CPDFDoc_Environment* pApp, FX_UINT nElapse); |
30 void EndTimer(CJS_Timer* pTimer); | 32 void EndTimer(CJS_Timer* pTimer); |
31 | 33 |
32 CJS_Object* GetJSObject() { return m_pJSObject; }; | 34 CJS_Object* GetJSObject() const { return m_pJSObject; } |
33 operator CJS_Object*() { return m_pJSObject; }; | |
34 | 35 |
35 CPDFSDK_PageView* JSGetPageView(IFXJS_Context* cc); | 36 CPDFSDK_PageView* JSGetPageView(IFXJS_Context* cc); |
36 int MsgBox(CPDFDoc_Environment* pApp, | 37 int MsgBox(CPDFDoc_Environment* pApp, |
37 CPDFSDK_PageView* pPageView, | 38 CPDFSDK_PageView* pPageView, |
38 const FX_WCHAR* swMsg, | 39 const FX_WCHAR* swMsg, |
39 const FX_WCHAR* swTitle = NULL, | 40 const FX_WCHAR* swTitle = NULL, |
40 FX_UINT nType = 0, | 41 FX_UINT nType = 0, |
41 FX_UINT nIcon = 0); | 42 FX_UINT nIcon = 0); |
42 void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg); | 43 void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg); |
43 | 44 |
44 protected: | 45 protected: |
45 CJS_Object* m_pJSObject; | 46 CJS_Object* m_pJSObject; |
46 }; | 47 }; |
47 | 48 |
48 class CJS_Object { | 49 class CJS_Object { |
49 public: | 50 public: |
50 CJS_Object(JSFXObject pObject); | 51 explicit CJS_Object(JSFXObject pObject); |
51 virtual ~CJS_Object(void); | 52 virtual ~CJS_Object(void); |
52 | 53 |
53 void MakeWeak(); | 54 void MakeWeak(); |
54 void Dispose(); | 55 void Dispose(); |
55 | 56 |
56 virtual FX_BOOL IsType(const FX_CHAR* sClassName) { return TRUE; }; | 57 virtual FX_BOOL IsType(const FX_CHAR* sClassName) { return TRUE; } |
57 virtual CFX_ByteString GetClassName() { return ""; }; | 58 virtual CFX_ByteString GetClassName() { return ""; } |
58 | 59 |
59 virtual FX_BOOL InitInstance(IFXJS_Context* cc) { return TRUE; }; | 60 virtual FX_BOOL InitInstance(IFXJS_Context* cc) { return TRUE; } |
60 virtual FX_BOOL ExitInstance() { return TRUE; }; | 61 virtual FX_BOOL ExitInstance() { return TRUE; } |
61 | 62 |
62 operator JSFXObject() { | 63 operator JSFXObject() { |
63 return v8::Local<v8::Object>::New(m_pIsolate, m_pObject); | 64 return v8::Local<v8::Object>::New(m_pIsolate, m_pObject); |
64 } | 65 } |
65 operator CJS_EmbedObj*() { return m_pEmbedObj; }; | |
66 | 66 |
67 void SetEmbedObject(CJS_EmbedObj* pObj) { m_pEmbedObj = pObj; }; | 67 // Takes ownership of |pObj|. |
68 CJS_EmbedObj* GetEmbedObject() { return m_pEmbedObj; }; | 68 void SetEmbedObject(CJS_EmbedObj* pObj) { m_pEmbedObj.reset(pObj); } |
69 CJS_EmbedObj* GetEmbedObject() const { return m_pEmbedObj.get(); } | |
69 | 70 |
70 static CPDFSDK_PageView* JSGetPageView(IFXJS_Context* cc); | 71 static CPDFSDK_PageView* JSGetPageView(IFXJS_Context* cc); |
71 static int MsgBox(CPDFDoc_Environment* pApp, | 72 static int MsgBox(CPDFDoc_Environment* pApp, |
72 CPDFSDK_PageView* pPageView, | 73 CPDFSDK_PageView* pPageView, |
73 const FX_WCHAR* swMsg, | 74 const FX_WCHAR* swMsg, |
74 const FX_WCHAR* swTitle = NULL, | 75 const FX_WCHAR* swTitle = NULL, |
75 FX_UINT nType = 0, | 76 FX_UINT nType = 0, |
76 FX_UINT nIcon = 0); | 77 FX_UINT nIcon = 0); |
77 static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg); | 78 static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg); |
78 | 79 |
79 v8::Isolate* GetIsolate() { return m_pIsolate; } | 80 v8::Isolate* GetIsolate() { return m_pIsolate; } |
80 | 81 |
81 protected: | 82 protected: |
82 CJS_EmbedObj* m_pEmbedObj; | 83 nonstd::unique_ptr<CJS_EmbedObj> m_pEmbedObj; |
83 v8::Global<v8::Object> m_pObject; | 84 v8::Global<v8::Object> m_pObject; |
84 v8::Isolate* m_pIsolate; | 85 v8::Isolate* m_pIsolate; |
85 }; | 86 }; |
86 | 87 |
87 using JSTimerMap = std::map<FX_UINT, CJS_Timer*>; | |
88 JSTimerMap* GetGlobalTimerMap(); | |
89 | |
90 class CJS_Runtime; | |
91 | |
92 class CJS_Timer { | 88 class CJS_Timer { |
93 public: | 89 public: |
94 CJS_Timer(CJS_EmbedObj* pObj, CPDFDoc_Environment* pApp) | 90 CJS_Timer(CJS_EmbedObj* pObj, CPDFDoc_Environment* pApp) |
95 : m_nTimerID(0), | 91 : m_nTimerID(0), |
96 m_pEmbedObj(pObj), | 92 m_pEmbedObj(pObj), |
97 m_bProcessing(FALSE), | 93 m_bProcessing(FALSE), |
98 m_dwStartTime(0), | 94 m_dwStartTime(0), |
99 m_dwTimeOut(0), | 95 m_dwTimeOut(0), |
100 m_dwElapse(0), | 96 m_dwElapse(0), |
101 m_pRuntime(NULL), | 97 m_pRuntime(NULL), |
102 m_nType(0), | 98 m_nType(0), |
103 m_pApp(pApp) {} | 99 m_pApp(pApp) {} |
104 | 100 |
105 virtual ~CJS_Timer() { KillJSTimer(); } | 101 virtual ~CJS_Timer() { KillJSTimer(); } |
106 | 102 |
107 public: | 103 public: |
108 FX_UINT SetJSTimer(FX_UINT nElapse) { | 104 FX_UINT SetJSTimer(FX_UINT nElapse); |
109 if (m_nTimerID) | 105 void KillJSTimer(); |
110 KillJSTimer(); | |
111 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); | |
112 m_nTimerID = pHandler->SetTimer(nElapse, TimerProc); | |
113 (*GetGlobalTimerMap())[m_nTimerID] = this; | |
114 m_dwElapse = nElapse; | |
115 return m_nTimerID; | |
116 }; | |
117 | |
118 void KillJSTimer() { | |
119 if (m_nTimerID) { | |
120 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); | |
121 pHandler->KillTimer(m_nTimerID); | |
122 GetGlobalTimerMap()->erase(m_nTimerID); | |
123 m_nTimerID = 0; | |
124 } | |
125 }; | |
126 | 106 |
127 void SetType(int nType) { m_nType = nType; } | 107 void SetType(int nType) { m_nType = nType; } |
128 | |
129 int GetType() const { return m_nType; } | 108 int GetType() const { return m_nType; } |
130 | 109 |
131 void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; } | 110 void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; } |
132 | |
133 FX_DWORD GetStartTime() const { return m_dwStartTime; } | 111 FX_DWORD GetStartTime() const { return m_dwStartTime; } |
134 | 112 |
135 void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; } | 113 void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; } |
136 | |
137 FX_DWORD GetTimeOut() const { return m_dwTimeOut; } | 114 FX_DWORD GetTimeOut() const { return m_dwTimeOut; } |
138 | 115 |
139 void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; } | 116 void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; } |
140 | |
141 CJS_Runtime* GetRuntime() const { return m_pRuntime; } | 117 CJS_Runtime* GetRuntime() const { return m_pRuntime; } |
142 | 118 |
143 void SetJScript(const CFX_WideString& script) { m_swJScript = script; } | 119 void SetJScript(const CFX_WideString& script) { m_swJScript = script; } |
144 | |
145 CFX_WideString GetJScript() const { return m_swJScript; } | 120 CFX_WideString GetJScript() const { return m_swJScript; } |
146 | 121 |
147 static void TimerProc(int idEvent) { | 122 static void TimerProc(int idEvent); |
148 const auto it = GetGlobalTimerMap()->find(idEvent); | |
149 if (it != GetGlobalTimerMap()->end()) { | |
150 CJS_Timer* pTimer = it->second; | |
151 if (!pTimer->m_bProcessing) { | |
152 pTimer->m_bProcessing = TRUE; | |
153 if (pTimer->m_pEmbedObj) | |
154 pTimer->m_pEmbedObj->TimerProc(pTimer); | |
155 pTimer->m_bProcessing = FALSE; | |
156 } | |
157 } | |
158 }; | |
159 | 123 |
160 private: | 124 private: |
125 using TimerMap = std::map<FX_UINT, CJS_Timer*>; | |
126 static TimerMap* GetGlobalTimerMap(); | |
Lei Zhang
2015/08/13 23:22:24
Just make this a function in an anonymous namespac
Tom Sepez
2015/08/13 23:28:31
Nah, I like restricting callers to this class, and
Lei Zhang
2015/08/13 23:36:48
Oh, I'm too used to having just 1 class per file.
| |
127 | |
161 FX_UINT m_nTimerID; | 128 FX_UINT m_nTimerID; |
162 CJS_EmbedObj* m_pEmbedObj; | 129 CJS_EmbedObj* m_pEmbedObj; |
163 FX_BOOL m_bProcessing; | 130 FX_BOOL m_bProcessing; |
164 | 131 |
165 // data | 132 // data |
166 FX_DWORD m_dwStartTime; | 133 FX_DWORD m_dwStartTime; |
167 FX_DWORD m_dwTimeOut; | 134 FX_DWORD m_dwTimeOut; |
168 FX_DWORD m_dwElapse; | 135 FX_DWORD m_dwElapse; |
169 CJS_Runtime* m_pRuntime; | 136 CJS_Runtime* m_pRuntime; |
170 CFX_WideString m_swJScript; | 137 CFX_WideString m_swJScript; |
171 int m_nType; // 0:Interval; 1:TimeOut | 138 int m_nType; // 0:Interval; 1:TimeOut |
172 | 139 |
173 CPDFDoc_Environment* m_pApp; | 140 CPDFDoc_Environment* m_pApp; |
174 }; | 141 }; |
175 | 142 |
176 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ | 143 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ |
OLD | NEW |