blob: c7f92ac61d4094f3158d75952b926753988cf731 [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 Sepez116e4ad2015-09-21 09:22:05 -070063 v8::Local<v8::Object> ToV8Object() { return m_pV8Object.Get(m_pIsolate); }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064
Tom Sepez371c87f2015-08-13 16:56:19 -070065 // Takes ownership of |pObj|.
66 void SetEmbedObject(CJS_EmbedObj* pObj) { m_pEmbedObj.reset(pObj); }
67 CJS_EmbedObj* GetEmbedObject() const { return m_pEmbedObj.get(); }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 static CPDFSDK_PageView* JSGetPageView(IFXJS_Context* cc);
70 static int MsgBox(CPDFDoc_Environment* pApp,
71 CPDFSDK_PageView* pPageView,
72 const FX_WCHAR* swMsg,
73 const FX_WCHAR* swTitle = NULL,
74 FX_UINT nType = 0,
75 FX_UINT nIcon = 0);
76 static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 v8::Isolate* GetIsolate() { return m_pIsolate; }
79
80 protected:
Tom Sepez371c87f2015-08-13 16:56:19 -070081 nonstd::unique_ptr<CJS_EmbedObj> m_pEmbedObj;
Tom Sepez116e4ad2015-09-21 09:22:05 -070082 v8::Global<v8::Object> m_pV8Object;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 v8::Isolate* m_pIsolate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084};
85
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086class CJS_Timer {
87 public:
88 CJS_Timer(CJS_EmbedObj* pObj, CPDFDoc_Environment* pApp)
89 : m_nTimerID(0),
90 m_pEmbedObj(pObj),
91 m_bProcessing(FALSE),
92 m_dwStartTime(0),
93 m_dwTimeOut(0),
94 m_dwElapse(0),
95 m_pRuntime(NULL),
96 m_nType(0),
97 m_pApp(pApp) {}
Tom Sepezc6ab1722015-02-05 15:27:25 -080098
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 virtual ~CJS_Timer() { KillJSTimer(); }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 public:
Tom Sepez371c87f2015-08-13 16:56:19 -0700102 FX_UINT SetJSTimer(FX_UINT nElapse);
103 void KillJSTimer();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700104
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 void SetType(int nType) { m_nType = nType; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700106 int GetType() const { return m_nType; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700107
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700108 void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109 FX_DWORD GetStartTime() const { return m_dwStartTime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700110
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 FX_DWORD GetTimeOut() const { return m_dwTimeOut; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700113
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 CJS_Runtime* GetRuntime() const { return m_pRuntime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700116
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 void SetJScript(const CFX_WideString& script) { m_swJScript = script; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 CFX_WideString GetJScript() const { return m_swJScript; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700119
Tom Sepez371c87f2015-08-13 16:56:19 -0700120 static void TimerProc(int idEvent);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700121
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 private:
Tom Sepez371c87f2015-08-13 16:56:19 -0700123 using TimerMap = std::map<FX_UINT, CJS_Timer*>;
124 static TimerMap* GetGlobalTimerMap();
125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126 FX_UINT m_nTimerID;
127 CJS_EmbedObj* m_pEmbedObj;
128 FX_BOOL m_bProcessing;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130 // data
131 FX_DWORD m_dwStartTime;
132 FX_DWORD m_dwTimeOut;
133 FX_DWORD m_dwElapse;
134 CJS_Runtime* m_pRuntime;
135 CFX_WideString m_swJScript;
136 int m_nType; // 0:Interval; 1:TimeOut
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700138 CPDFDoc_Environment* m_pApp;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700139};
Tom Sepez19922bb2015-05-28 13:23:12 -0700140
141#endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_