blob: aec94c0362020f7b1a6bb29d462a3d4d2cd4bba2 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Sepezc6ab1722015-02-05 15:27:25 -08004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#ifndef FPDFSDK_JAVASCRIPT_JS_OBJECT_H_
8#define FPDFSDK_JAVASCRIPT_JS_OBJECT_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
Tom Sepez2ebc33e2015-08-13 16:07:29 -070010#include <map>
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080011#include <memory>
Tom Sepez2ebc33e2015-08-13 16:07:29 -070012
dsinclair64376be2016-03-31 20:03:24 -070013#include "fpdfsdk/include/fsdk_define.h"
dsinclair64376be2016-03-31 20:03:24 -070014#include "fpdfsdk/javascript/cjs_runtime.h"
dsinclairb3f24672016-07-12 10:42:14 -070015#include "fxjs/include/fxjs_v8.h"
Tom Sepez3a832662015-03-02 12:59:05 -080016
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017class CJS_Context;
Tom Sepez371c87f2015-08-13 16:56:19 -070018class CJS_Object;
Tom Sepez371c87f2015-08-13 16:56:19 -070019class CJS_Timer;
Tom Sepez37458412015-10-06 11:33:46 -070020class CPDFDoc_Environment;
tsepez8ca63de2016-08-05 17:12:27 -070021
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022class CJS_EmbedObj {
23 public:
Tom Sepez371c87f2015-08-13 16:56:19 -070024 explicit CJS_EmbedObj(CJS_Object* pJSObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025 virtual ~CJS_EmbedObj();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026
Tom Sepez371c87f2015-08-13 16:56:19 -070027 virtual void TimerProc(CJS_Timer* pTimer) {}
tsepez8ca63de2016-08-05 17:12:27 -070028 virtual void CancelProc(CJS_Timer* pTimer) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
Tom Sepez371c87f2015-08-13 16:56:19 -070030 CJS_Object* GetJSObject() const { return m_pJSObject; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
Nico Weber9d8ec5a2015-08-04 13:00:21 -070032 int MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033 const FX_WCHAR* swMsg,
Lei Zhangd607f5b2015-10-05 17:06:09 -070034 const FX_WCHAR* swTitle,
35 FX_UINT nType,
36 FX_UINT nIcon);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037 void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 protected:
40 CJS_Object* m_pJSObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041};
42
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043class CJS_Object {
44 public:
Tom Sepez808a99e2015-09-10 12:28:37 -070045 explicit CJS_Object(v8::Local<v8::Object> pObject);
Lei Zhang2b1a2d52015-08-14 22:16:22 -070046 virtual ~CJS_Object();
Tom Sepezc6ab1722015-02-05 15:27:25 -080047
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 void MakeWeak();
49 void Dispose();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
weili625ad662016-06-15 11:21:33 -070051 virtual void InitInstance(IJS_Runtime* pIRuntime);
52 virtual void ExitInstance();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070053
Tom Sepez116e4ad2015-09-21 09:22:05 -070054 v8::Local<v8::Object> ToV8Object() { return m_pV8Object.Get(m_pIsolate); }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055
Tom Sepez371c87f2015-08-13 16:56:19 -070056 // 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-Malek3f3b45c2014-05-23 17:28:10 -070059
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 v8::Isolate* GetIsolate() { return m_pIsolate; }
63
64 protected:
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080065 std::unique_ptr<CJS_EmbedObj> m_pEmbedObj;
Tom Sepez116e4ad2015-09-21 09:22:05 -070066 v8::Global<v8::Object> m_pV8Object;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 v8::Isolate* m_pIsolate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068};
69
Lei Zhang2d5a0e12015-10-05 17:00:03 -070070class CJS_Timer : public CJS_Runtime::Observer {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 public:
Lei Zhang2d5a0e12015-10-05 17:00:03 -070072 CJS_Timer(CJS_EmbedObj* pObj,
73 CPDFDoc_Environment* pApp,
74 CJS_Runtime* pRuntime,
75 int nType,
76 const CFX_WideString& script,
tsepezc3255f52016-03-25 14:52:27 -070077 uint32_t dwElapse,
78 uint32_t dwTimeOut);
Lei Zhang2d5a0e12015-10-05 17:00:03 -070079 ~CJS_Timer() override;
Tom Sepezc6ab1722015-02-05 15:27:25 -080080
tsepez8ca63de2016-08-05 17:12:27 -070081 static void Trigger(int nTimerID);
82 static void Cancel(int nTimerID);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070083
tsepez8ca63de2016-08-05 17:12:27 -070084 bool IsOneShot() const { return m_nType == 1; }
tsepezc3255f52016-03-25 14:52:27 -070085 uint32_t GetTimeOut() const { return m_dwTimeOut; }
tsepez8ca63de2016-08-05 17:12:27 -070086 int GetTimerID() const { return m_nTimerID; }
Lei Zhang2d5a0e12015-10-05 17:00:03 -070087 CJS_Runtime* GetRuntime() const { return m_bValid ? m_pRuntime : nullptr; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 CFX_WideString GetJScript() const { return m_swJScript; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089
Nico Weber9d8ec5a2015-08-04 13:00:21 -070090 private:
Tom Sepez371c87f2015-08-13 16:56:19 -070091 using TimerMap = std::map<FX_UINT, CJS_Timer*>;
92 static TimerMap* GetGlobalTimerMap();
93
Lei Zhang2d5a0e12015-10-05 17:00:03 -070094 // CJS_Runtime::Observer
95 void OnDestroyed() override;
96
tsepezc3255f52016-03-25 14:52:27 -070097 uint32_t m_nTimerID;
Lei Zhang2d5a0e12015-10-05 17:00:03 -070098 CJS_EmbedObj* const m_pEmbedObj;
99 bool m_bProcessing;
100 bool m_bValid;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700102 // data
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700103 const int m_nType; // 0:Interval; 1:TimeOut
tsepezc3255f52016-03-25 14:52:27 -0700104 const uint32_t m_dwTimeOut;
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700105 const CFX_WideString m_swJScript;
106 CJS_Runtime* const m_pRuntime;
107 CPDFDoc_Environment* const m_pApp;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108};
Tom Sepez19922bb2015-05-28 13:23:12 -0700109
Dan Sinclairf766ad22016-03-14 13:51:24 -0400110#endif // FPDFSDK_JAVASCRIPT_JS_OBJECT_H_