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 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 7 | #ifndef FPDFSDK_JAVASCRIPT_JS_OBJECT_H_ |
| 8 | #define FPDFSDK_JAVASCRIPT_JS_OBJECT_H_ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 9 | |
Tom Sepez | 2ebc33e | 2015-08-13 16:07:29 -0700 | [diff] [blame] | 10 | #include <map> |
Lei Zhang | aa8bf7e | 2015-12-24 19:13:32 -0800 | [diff] [blame] | 11 | #include <memory> |
Tom Sepez | 2ebc33e | 2015-08-13 16:07:29 -0700 | [diff] [blame] | 12 | |
dsinclair | 64376be | 2016-03-31 20:03:24 -0700 | [diff] [blame^] | 13 | #include "fpdfsdk/include/fsdk_define.h" |
Lei Zhang | bde53d2 | 2015-11-12 22:21:30 -0800 | [diff] [blame] | 14 | #include "fpdfsdk/include/jsapi/fxjs_v8.h" |
dsinclair | 64376be | 2016-03-31 20:03:24 -0700 | [diff] [blame^] | 15 | #include "fpdfsdk/javascript/cjs_runtime.h" |
Tom Sepez | 3a83266 | 2015-03-02 12:59:05 -0800 | [diff] [blame] | 16 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 17 | class CJS_Context; |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 18 | class CJS_Object; |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 19 | class CJS_Timer; |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 20 | class CPDFDoc_Environment; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 21 | class CJS_EmbedObj { |
| 22 | public: |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 23 | explicit CJS_EmbedObj(CJS_Object* pJSObject); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 24 | virtual ~CJS_EmbedObj(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 25 | |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 26 | virtual void TimerProc(CJS_Timer* pTimer) {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 27 | |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 28 | CJS_Object* GetJSObject() const { return m_pJSObject; } |
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 | int MsgBox(CPDFDoc_Environment* pApp, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 31 | const FX_WCHAR* swMsg, |
Lei Zhang | d607f5b | 2015-10-05 17:06:09 -0700 | [diff] [blame] | 32 | const FX_WCHAR* swTitle, |
| 33 | FX_UINT nType, |
| 34 | FX_UINT nIcon); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 35 | void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 36 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 37 | protected: |
| 38 | CJS_Object* m_pJSObject; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 41 | class CJS_Object { |
| 42 | public: |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 43 | explicit CJS_Object(v8::Local<v8::Object> pObject); |
Lei Zhang | 2b1a2d5 | 2015-08-14 22:16:22 -0700 | [diff] [blame] | 44 | virtual ~CJS_Object(); |
Tom Sepez | c6ab172 | 2015-02-05 15:27:25 -0800 | [diff] [blame] | 45 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 46 | void MakeWeak(); |
| 47 | void Dispose(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 48 | |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 49 | virtual FX_BOOL IsType(const FX_CHAR* sClassName) { return TRUE; } |
| 50 | virtual CFX_ByteString GetClassName() { return ""; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 51 | |
Tom Sepez | 3342090 | 2015-10-13 15:00:10 -0700 | [diff] [blame] | 52 | virtual void InitInstance(IJS_Runtime* pIRuntime) {} |
| 53 | virtual void ExitInstance() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 54 | |
Tom Sepez | 116e4ad | 2015-09-21 09:22:05 -0700 | [diff] [blame] | 55 | v8::Local<v8::Object> ToV8Object() { return m_pV8Object.Get(m_pIsolate); } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 56 | |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 57 | // Takes ownership of |pObj|. |
| 58 | void SetEmbedObject(CJS_EmbedObj* pObj) { m_pEmbedObj.reset(pObj); } |
| 59 | CJS_EmbedObj* GetEmbedObject() const { return m_pEmbedObj.get(); } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 60 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 61 | static int MsgBox(CPDFDoc_Environment* pApp, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 62 | const FX_WCHAR* swMsg, |
Lei Zhang | d607f5b | 2015-10-05 17:06:09 -0700 | [diff] [blame] | 63 | const FX_WCHAR* swTitle, |
| 64 | FX_UINT nType, |
| 65 | FX_UINT nIcon); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 66 | static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg); |
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 | v8::Isolate* GetIsolate() { return m_pIsolate; } |
| 69 | |
| 70 | protected: |
Lei Zhang | aa8bf7e | 2015-12-24 19:13:32 -0800 | [diff] [blame] | 71 | std::unique_ptr<CJS_EmbedObj> m_pEmbedObj; |
Tom Sepez | 116e4ad | 2015-09-21 09:22:05 -0700 | [diff] [blame] | 72 | v8::Global<v8::Object> m_pV8Object; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 73 | v8::Isolate* m_pIsolate; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 76 | class CJS_Timer : public CJS_Runtime::Observer { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 77 | public: |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 78 | CJS_Timer(CJS_EmbedObj* pObj, |
| 79 | CPDFDoc_Environment* pApp, |
| 80 | CJS_Runtime* pRuntime, |
| 81 | int nType, |
| 82 | const CFX_WideString& script, |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 83 | uint32_t dwElapse, |
| 84 | uint32_t dwTimeOut); |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 85 | ~CJS_Timer() override; |
Tom Sepez | c6ab172 | 2015-02-05 15:27:25 -0800 | [diff] [blame] | 86 | |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 87 | void KillJSTimer(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 88 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 89 | int GetType() const { return m_nType; } |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 90 | uint32_t GetTimeOut() const { return m_dwTimeOut; } |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 91 | CJS_Runtime* GetRuntime() const { return m_bValid ? m_pRuntime : nullptr; } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 92 | CFX_WideString GetJScript() const { return m_swJScript; } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 93 | |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 94 | static void TimerProc(int idEvent); |
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 | private: |
Tom Sepez | 371c87f | 2015-08-13 16:56:19 -0700 | [diff] [blame] | 97 | using TimerMap = std::map<FX_UINT, CJS_Timer*>; |
| 98 | static TimerMap* GetGlobalTimerMap(); |
| 99 | |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 100 | // CJS_Runtime::Observer |
| 101 | void OnDestroyed() override; |
| 102 | |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 103 | uint32_t m_nTimerID; |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 104 | CJS_EmbedObj* const m_pEmbedObj; |
| 105 | bool m_bProcessing; |
| 106 | bool m_bValid; |
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 | // data |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 109 | const int m_nType; // 0:Interval; 1:TimeOut |
tsepez | c3255f5 | 2016-03-25 14:52:27 -0700 | [diff] [blame] | 110 | const uint32_t m_dwTimeOut; |
Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 111 | const CFX_WideString m_swJScript; |
| 112 | CJS_Runtime* const m_pRuntime; |
| 113 | CPDFDoc_Environment* const m_pApp; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 114 | }; |
Tom Sepez | 19922bb | 2015-05-28 13:23:12 -0700 | [diff] [blame] | 115 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 116 | #endif // FPDFSDK_JAVASCRIPT_JS_OBJECT_H_ |