blob: 337da7ba203fc3b499dd4c245a882a8dbd4a3adb [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"
Lei Zhang2d5a0e12015-10-05 17:00:03 -070018#include "JS_Runtime.h"
Tom Sepez3a832662015-03-02 12:59:05 -080019
20class CPDFSDK_PageView;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070021class CJS_Context;
Tom Sepez371c87f2015-08-13 16:56:19 -070022class CJS_Object;
Tom Sepez371c87f2015-08-13 16:56:19 -070023class 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) {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
Tom Sepez371c87f2015-08-13 16:56:19 -070032 CJS_Object* GetJSObject() const { return m_pJSObject; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070033
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 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-Malek3f3b45c2014-05-23 17:28:10 -070042
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 protected:
44 CJS_Object* m_pJSObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045};
46
Nico Weber9d8ec5a2015-08-04 13:00:21 -070047class CJS_Object {
48 public:
Tom Sepez808a99e2015-09-10 12:28:37 -070049 explicit CJS_Object(v8::Local<v8::Object> pObject);
Lei Zhang2b1a2d52015-08-14 22:16:22 -070050 virtual ~CJS_Object();
Tom Sepezc6ab1722015-02-05 15:27:25 -080051
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 void MakeWeak();
53 void Dispose();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070054
Tom Sepez371c87f2015-08-13 16:56:19 -070055 virtual FX_BOOL IsType(const FX_CHAR* sClassName) { return TRUE; }
56 virtual CFX_ByteString GetClassName() { return ""; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057
Tom Sepez371c87f2015-08-13 16:56:19 -070058 virtual FX_BOOL InitInstance(IFXJS_Context* cc) { return TRUE; }
59 virtual FX_BOOL ExitInstance() { return TRUE; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060
Tom Sepez116e4ad2015-09-21 09:22:05 -070061 v8::Local<v8::Object> ToV8Object() { return m_pV8Object.Get(m_pIsolate); }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062
Tom Sepez371c87f2015-08-13 16:56:19 -070063 // 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-Malek3f3b45c2014-05-23 17:28:10 -070066
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 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-Malek3f3b45c2014-05-23 17:28:10 -070075
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 v8::Isolate* GetIsolate() { return m_pIsolate; }
77
78 protected:
Tom Sepez371c87f2015-08-13 16:56:19 -070079 nonstd::unique_ptr<CJS_EmbedObj> m_pEmbedObj;
Tom Sepez116e4ad2015-09-21 09:22:05 -070080 v8::Global<v8::Object> m_pV8Object;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 v8::Isolate* m_pIsolate;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082};
83
Lei Zhang2d5a0e12015-10-05 17:00:03 -070084class CJS_Timer : public CJS_Runtime::Observer {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 public:
Lei Zhang2d5a0e12015-10-05 17:00:03 -070086 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 Sepezc6ab1722015-02-05 15:27:25 -080094
Tom Sepez371c87f2015-08-13 16:56:19 -070095 void KillJSTimer();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 int GetType() const { return m_nType; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 FX_DWORD GetTimeOut() const { return m_dwTimeOut; }
Lei Zhang2d5a0e12015-10-05 17:00:03 -070099 CJS_Runtime* GetRuntime() const { return m_bValid ? m_pRuntime : nullptr; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 CFX_WideString GetJScript() const { return m_swJScript; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
Tom Sepez371c87f2015-08-13 16:56:19 -0700102 static void TimerProc(int idEvent);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700103
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700104 private:
Tom Sepez371c87f2015-08-13 16:56:19 -0700105 using TimerMap = std::map<FX_UINT, CJS_Timer*>;
106 static TimerMap* GetGlobalTimerMap();
107
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700108 // 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-Malek3f3b45c2014-05-23 17:28:10 -0700115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 // data
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700117 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-Malek3f3b45c2014-05-23 17:28:10 -0700122};
Tom Sepez19922bb2015-05-28 13:23:12 -0700123
124#endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_