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