blob: e0ec23e79aba85e9b65081e62b691a09ee1587ce [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"
Lei Zhangbde53d22015-11-12 22:21:30 -080014#include "fpdfsdk/include/jsapi/fxjs_v8.h"
dsinclair64376be2016-03-31 20:03:24 -070015#include "fpdfsdk/javascript/cjs_runtime.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;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021class CJS_EmbedObj {
22 public:
Tom Sepez371c87f2015-08-13 16:56:19 -070023 explicit CJS_EmbedObj(CJS_Object* pJSObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 virtual ~CJS_EmbedObj();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070025
Tom Sepez371c87f2015-08-13 16:56:19 -070026 virtual void TimerProc(CJS_Timer* pTimer) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Tom Sepez371c87f2015-08-13 16:56:19 -070028 CJS_Object* GetJSObject() const { return m_pJSObject; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
Nico Weber9d8ec5a2015-08-04 13:00:21 -070030 int MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031 const FX_WCHAR* swMsg,
Lei Zhangd607f5b2015-10-05 17:06:09 -070032 const FX_WCHAR* swTitle,
33 FX_UINT nType,
34 FX_UINT nIcon);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070035 void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037 protected:
38 CJS_Object* m_pJSObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039};
40
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041class CJS_Object {
42 public:
Tom Sepez808a99e2015-09-10 12:28:37 -070043 explicit CJS_Object(v8::Local<v8::Object> pObject);
Lei Zhang2b1a2d52015-08-14 22:16:22 -070044 virtual ~CJS_Object();
Tom Sepezc6ab1722015-02-05 15:27:25 -080045
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 void MakeWeak();
47 void Dispose();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048
Tom Sepez371c87f2015-08-13 16:56:19 -070049 virtual FX_BOOL IsType(const FX_CHAR* sClassName) { return TRUE; }
50 virtual CFX_ByteString GetClassName() { return ""; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
Tom Sepez33420902015-10-13 15:00:10 -070052 virtual void InitInstance(IJS_Runtime* pIRuntime) {}
53 virtual void ExitInstance() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070054
Tom Sepez116e4ad2015-09-21 09:22:05 -070055 v8::Local<v8::Object> ToV8Object() { return m_pV8Object.Get(m_pIsolate); }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
Tom Sepez371c87f2015-08-13 16:56:19 -070057 // 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-Malek3f3b45c2014-05-23 17:28:10 -070060
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 static int MsgBox(CPDFDoc_Environment* pApp,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 const FX_WCHAR* swMsg,
Lei Zhangd607f5b2015-10-05 17:06:09 -070063 const FX_WCHAR* swTitle,
64 FX_UINT nType,
65 FX_UINT nIcon);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 v8::Isolate* GetIsolate() { return m_pIsolate; }
69
70 protected:
Lei Zhangaa8bf7e2015-12-24 19:13:32 -080071 std::unique_ptr<CJS_EmbedObj> m_pEmbedObj;
Tom Sepez116e4ad2015-09-21 09:22:05 -070072 v8::Global<v8::Object> m_pV8Object;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 v8::Isolate* m_pIsolate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074};
75
Lei Zhang2d5a0e12015-10-05 17:00:03 -070076class CJS_Timer : public CJS_Runtime::Observer {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 public:
Lei Zhang2d5a0e12015-10-05 17:00:03 -070078 CJS_Timer(CJS_EmbedObj* pObj,
79 CPDFDoc_Environment* pApp,
80 CJS_Runtime* pRuntime,
81 int nType,
82 const CFX_WideString& script,
tsepezc3255f52016-03-25 14:52:27 -070083 uint32_t dwElapse,
84 uint32_t dwTimeOut);
Lei Zhang2d5a0e12015-10-05 17:00:03 -070085 ~CJS_Timer() override;
Tom Sepezc6ab1722015-02-05 15:27:25 -080086
Tom Sepez371c87f2015-08-13 16:56:19 -070087 void KillJSTimer();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 int GetType() const { return m_nType; }
tsepezc3255f52016-03-25 14:52:27 -070090 uint32_t GetTimeOut() const { return m_dwTimeOut; }
Lei Zhang2d5a0e12015-10-05 17:00:03 -070091 CJS_Runtime* GetRuntime() const { return m_bValid ? m_pRuntime : nullptr; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 CFX_WideString GetJScript() const { return m_swJScript; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093
Tom Sepez371c87f2015-08-13 16:56:19 -070094 static void TimerProc(int idEvent);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 private:
Tom Sepez371c87f2015-08-13 16:56:19 -070097 using TimerMap = std::map<FX_UINT, CJS_Timer*>;
98 static TimerMap* GetGlobalTimerMap();
99
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700100 // CJS_Runtime::Observer
101 void OnDestroyed() override;
102
tsepezc3255f52016-03-25 14:52:27 -0700103 uint32_t m_nTimerID;
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700104 CJS_EmbedObj* const m_pEmbedObj;
105 bool m_bProcessing;
106 bool m_bValid;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 // data
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700109 const int m_nType; // 0:Interval; 1:TimeOut
tsepezc3255f52016-03-25 14:52:27 -0700110 const uint32_t m_dwTimeOut;
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700111 const CFX_WideString m_swJScript;
112 CJS_Runtime* const m_pRuntime;
113 CPDFDoc_Environment* const m_pApp;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700114};
Tom Sepez19922bb2015-05-28 13:23:12 -0700115
Dan Sinclairf766ad22016-03-14 13:51:24 -0400116#endif // FPDFSDK_JAVASCRIPT_JS_OBJECT_H_