blob: a7772f1cf9503ce3024e67d10060bbe02b863691 [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
Tom Sepez19922bb2015-05-28 13:23:12 -07007#ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_
8#define FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
Tom Sepez2ebc33e2015-08-13 16:07:29 -070010#include <map>
11
Tom Sepez371c87f2015-08-13 16:56:19 -070012#include "../../../third_party/base/nonstd_unique_ptr.h"
13
Nico Weber9d8ec5a2015-08-04 13:00:21 -070014#include "../fsdk_define.h" // For FX_UINT
15#include "../fsdk_mgr.h" // For CPDFDoc_Environment
Tom Sepez3a832662015-03-02 12:59:05 -080016#include "../fx_systemhandler.h" // For IFX_SystemHandler
Tom Sepez9a3f8122015-04-07 15:35:48 -070017#include "../jsapi/fxjs_v8.h"
Tom Sepez3a832662015-03-02 12:59:05 -080018
19class CPDFSDK_PageView;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020class CJS_Context;
Tom Sepez371c87f2015-08-13 16:56:19 -070021class CJS_Object;
22class CJS_Runtime;
23class CJS_Timer;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024
Nico Weber9d8ec5a2015-08-04 13:00:21 -070025class CJS_EmbedObj {
26 public:
Tom Sepez371c87f2015-08-13 16:56:19 -070027 explicit CJS_EmbedObj(CJS_Object* pJSObject);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070028 virtual ~CJS_EmbedObj();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029
Tom Sepez371c87f2015-08-13 16:56:19 -070030 virtual void TimerProc(CJS_Timer* pTimer) {}
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031 CJS_Timer* BeginTimer(CPDFDoc_Environment* pApp, FX_UINT nElapse);
32 void EndTimer(CJS_Timer* pTimer);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033
Tom Sepez371c87f2015-08-13 16:56:19 -070034 CJS_Object* GetJSObject() const { return m_pJSObject; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 CPDFSDK_PageView* JSGetPageView(IFXJS_Context* cc);
37 int MsgBox(CPDFDoc_Environment* pApp,
38 CPDFSDK_PageView* pPageView,
39 const FX_WCHAR* swMsg,
40 const FX_WCHAR* swTitle = NULL,
41 FX_UINT nType = 0,
42 FX_UINT nIcon = 0);
43 void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 protected:
46 CJS_Object* m_pJSObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047};
48
Nico Weber9d8ec5a2015-08-04 13:00:21 -070049class CJS_Object {
50 public:
Tom Sepez808a99e2015-09-10 12:28:37 -070051 explicit CJS_Object(v8::Local<v8::Object> pObject);
Lei Zhang2b1a2d52015-08-14 22:16:22 -070052 virtual ~CJS_Object();
Tom Sepezc6ab1722015-02-05 15:27:25 -080053
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 void MakeWeak();
55 void Dispose();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
Tom Sepez371c87f2015-08-13 16:56:19 -070057 virtual FX_BOOL IsType(const FX_CHAR* sClassName) { return TRUE; }
58 virtual CFX_ByteString GetClassName() { return ""; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059
Tom Sepez371c87f2015-08-13 16:56:19 -070060 virtual FX_BOOL InitInstance(IFXJS_Context* cc) { return TRUE; }
61 virtual FX_BOOL ExitInstance() { return TRUE; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062
Tom Sepez808a99e2015-09-10 12:28:37 -070063 operator v8::Local<v8::Object>() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 return v8::Local<v8::Object>::New(m_pIsolate, m_pObject);
65 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066
Tom Sepez371c87f2015-08-13 16:56:19 -070067 // Takes ownership of |pObj|.
68 void SetEmbedObject(CJS_EmbedObj* pObj) { m_pEmbedObj.reset(pObj); }
69 CJS_EmbedObj* GetEmbedObject() const { return m_pEmbedObj.get(); }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 static CPDFSDK_PageView* JSGetPageView(IFXJS_Context* cc);
72 static int MsgBox(CPDFDoc_Environment* pApp,
73 CPDFSDK_PageView* pPageView,
74 const FX_WCHAR* swMsg,
75 const FX_WCHAR* swTitle = NULL,
76 FX_UINT nType = 0,
77 FX_UINT nIcon = 0);
78 static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 v8::Isolate* GetIsolate() { return m_pIsolate; }
81
82 protected:
Tom Sepez371c87f2015-08-13 16:56:19 -070083 nonstd::unique_ptr<CJS_EmbedObj> m_pEmbedObj;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 v8::Global<v8::Object> m_pObject;
85 v8::Isolate* m_pIsolate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086};
87
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088class CJS_Timer {
89 public:
90 CJS_Timer(CJS_EmbedObj* pObj, CPDFDoc_Environment* pApp)
91 : m_nTimerID(0),
92 m_pEmbedObj(pObj),
93 m_bProcessing(FALSE),
94 m_dwStartTime(0),
95 m_dwTimeOut(0),
96 m_dwElapse(0),
97 m_pRuntime(NULL),
98 m_nType(0),
99 m_pApp(pApp) {}
Tom Sepezc6ab1722015-02-05 15:27:25 -0800100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 virtual ~CJS_Timer() { KillJSTimer(); }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 public:
Tom Sepez371c87f2015-08-13 16:56:19 -0700104 FX_UINT SetJSTimer(FX_UINT nElapse);
105 void KillJSTimer();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 void SetType(int nType) { m_nType = nType; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 int GetType() const { return m_nType; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700109
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700110 void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 FX_DWORD GetStartTime() const { return m_dwStartTime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700113 void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 FX_DWORD GetTimeOut() const { return m_dwTimeOut; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 CJS_Runtime* GetRuntime() const { return m_pRuntime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700118
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700119 void SetJScript(const CFX_WideString& script) { m_swJScript = script; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 CFX_WideString GetJScript() const { return m_swJScript; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700121
Tom Sepez371c87f2015-08-13 16:56:19 -0700122 static void TimerProc(int idEvent);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700123
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700124 private:
Tom Sepez371c87f2015-08-13 16:56:19 -0700125 using TimerMap = std::map<FX_UINT, CJS_Timer*>;
126 static TimerMap* GetGlobalTimerMap();
127
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 FX_UINT m_nTimerID;
129 CJS_EmbedObj* m_pEmbedObj;
130 FX_BOOL m_bProcessing;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 // data
133 FX_DWORD m_dwStartTime;
134 FX_DWORD m_dwTimeOut;
135 FX_DWORD m_dwElapse;
136 CJS_Runtime* m_pRuntime;
137 CFX_WideString m_swJScript;
138 int m_nType; // 0:Interval; 1:TimeOut
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700139
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 CPDFDoc_Environment* m_pApp;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141};
Tom Sepez19922bb2015-05-28 13:23:12 -0700142
143#endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_