1 // 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.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef FPDFSDK_SRC_JAVASCRIPT_JS_OBJECT_H_
8 #define FPDFSDK_SRC_JAVASCRIPT_JS_OBJECT_H_
9 
10 #include <map>
11 #include <memory>
12 
13 #include "JS_Runtime.h"
14 #include "fpdfsdk/include/fsdk_define.h"  // For FX_UINT
15 #include "fpdfsdk/include/jsapi/fxjs_v8.h"
16 
17 class CJS_Context;
18 class CJS_Object;
19 class CJS_Timer;
20 class CPDFDoc_Environment;
21 class CJS_EmbedObj {
22  public:
23   explicit CJS_EmbedObj(CJS_Object* pJSObject);
24   virtual ~CJS_EmbedObj();
25 
TimerProc(CJS_Timer * pTimer)26   virtual void TimerProc(CJS_Timer* pTimer) {}
27 
GetJSObject()28   CJS_Object* GetJSObject() const { return m_pJSObject; }
29 
30   int MsgBox(CPDFDoc_Environment* pApp,
31              const FX_WCHAR* swMsg,
32              const FX_WCHAR* swTitle,
33              FX_UINT nType,
34              FX_UINT nIcon);
35   void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
36 
37  protected:
38   CJS_Object* m_pJSObject;
39 };
40 
41 class CJS_Object {
42  public:
43   explicit CJS_Object(v8::Local<v8::Object> pObject);
44   virtual ~CJS_Object();
45 
46   void MakeWeak();
47   void Dispose();
48 
IsType(const FX_CHAR * sClassName)49   virtual FX_BOOL IsType(const FX_CHAR* sClassName) { return TRUE; }
GetClassName()50   virtual CFX_ByteString GetClassName() { return ""; }
51 
InitInstance(IJS_Runtime * pIRuntime)52   virtual void InitInstance(IJS_Runtime* pIRuntime) {}
ExitInstance()53   virtual void ExitInstance() {}
54 
ToV8Object()55   v8::Local<v8::Object> ToV8Object() { return m_pV8Object.Get(m_pIsolate); }
56 
57   // Takes ownership of |pObj|.
SetEmbedObject(CJS_EmbedObj * pObj)58   void SetEmbedObject(CJS_EmbedObj* pObj) { m_pEmbedObj.reset(pObj); }
GetEmbedObject()59   CJS_EmbedObj* GetEmbedObject() const { return m_pEmbedObj.get(); }
60 
61   static int MsgBox(CPDFDoc_Environment* pApp,
62                     const FX_WCHAR* swMsg,
63                     const FX_WCHAR* swTitle,
64                     FX_UINT nType,
65                     FX_UINT nIcon);
66   static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
67 
GetIsolate()68   v8::Isolate* GetIsolate() { return m_pIsolate; }
69 
70  protected:
71   std::unique_ptr<CJS_EmbedObj> m_pEmbedObj;
72   v8::Global<v8::Object> m_pV8Object;
73   v8::Isolate* m_pIsolate;
74 };
75 
76 class CJS_Timer : public CJS_Runtime::Observer {
77  public:
78   CJS_Timer(CJS_EmbedObj* pObj,
79             CPDFDoc_Environment* pApp,
80             CJS_Runtime* pRuntime,
81             int nType,
82             const CFX_WideString& script,
83             FX_DWORD dwElapse,
84             FX_DWORD dwTimeOut);
85   ~CJS_Timer() override;
86 
87   void KillJSTimer();
88 
GetType()89   int GetType() const { return m_nType; }
GetTimeOut()90   FX_DWORD GetTimeOut() const { return m_dwTimeOut; }
GetRuntime()91   CJS_Runtime* GetRuntime() const { return m_bValid ? m_pRuntime : nullptr; }
GetJScript()92   CFX_WideString GetJScript() const { return m_swJScript; }
93 
94   static void TimerProc(int idEvent);
95 
96  private:
97   using TimerMap = std::map<FX_UINT, CJS_Timer*>;
98   static TimerMap* GetGlobalTimerMap();
99 
100   // CJS_Runtime::Observer
101   void OnDestroyed() override;
102 
103   FX_DWORD m_nTimerID;
104   CJS_EmbedObj* const m_pEmbedObj;
105   bool m_bProcessing;
106   bool m_bValid;
107 
108   // data
109   const int m_nType;  // 0:Interval; 1:TimeOut
110   const FX_DWORD m_dwTimeOut;
111   const CFX_WideString m_swJScript;
112   CJS_Runtime* const m_pRuntime;
113   CPDFDoc_Environment* const m_pApp;
114 };
115 
116 #endif  // FPDFSDK_SRC_JAVASCRIPT_JS_OBJECT_H_
117