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