From 2d5a0e173f3bb8335cf302f1194305c2f9e65bed Mon Sep 17 00:00:00 2001
From: Lei Zhang <thestig@chromium.org>
Date: Mon, 5 Oct 2015 17:00:03 -0700
Subject: Merge to XFA: CJS_Timer should observe CJS_Runtime destruction.

Also remove dead CJS_EmbedObj::{Begin,End}Timer code.

BUG=539107
R=tsepez@chromium.org

Review URL: https://codereview.chromium.org/1384883002 .

(cherry picked from commit 794c9b67d3d519342aa7e15052766f7d4a99f551)

Review URL: https://codereview.chromium.org/1386053002 .
---
 fpdfsdk/include/javascript/JS_Object.h  | 63 ++++++++++++---------------------
 fpdfsdk/include/javascript/JS_Runtime.h | 12 +++++++
 2 files changed, 35 insertions(+), 40 deletions(-)

(limited to 'fpdfsdk/include/javascript')

diff --git a/fpdfsdk/include/javascript/JS_Object.h b/fpdfsdk/include/javascript/JS_Object.h
index c7f92ac61d..337da7ba20 100644
--- a/fpdfsdk/include/javascript/JS_Object.h
+++ b/fpdfsdk/include/javascript/JS_Object.h
@@ -15,11 +15,11 @@
 #include "../fsdk_mgr.h"          // For CPDFDoc_Environment
 #include "../fx_systemhandler.h"  // For IFX_SystemHandler
 #include "../jsapi/fxjs_v8.h"
+#include "JS_Runtime.h"
 
 class CPDFSDK_PageView;
 class CJS_Context;
 class CJS_Object;
-class CJS_Runtime;
 class CJS_Timer;
 
 class CJS_EmbedObj {
@@ -28,8 +28,6 @@ class CJS_EmbedObj {
   virtual ~CJS_EmbedObj();
 
   virtual void TimerProc(CJS_Timer* pTimer) {}
-  CJS_Timer* BeginTimer(CPDFDoc_Environment* pApp, FX_UINT nElapse);
-  void EndTimer(CJS_Timer* pTimer);
 
   CJS_Object* GetJSObject() const { return m_pJSObject; }
 
@@ -83,38 +81,22 @@ class CJS_Object {
   v8::Isolate* m_pIsolate;
 };
 
-class CJS_Timer {
+class CJS_Timer : public CJS_Runtime::Observer {
  public:
-  CJS_Timer(CJS_EmbedObj* pObj, CPDFDoc_Environment* pApp)
-      : m_nTimerID(0),
-        m_pEmbedObj(pObj),
-        m_bProcessing(FALSE),
-        m_dwStartTime(0),
-        m_dwTimeOut(0),
-        m_dwElapse(0),
-        m_pRuntime(NULL),
-        m_nType(0),
-        m_pApp(pApp) {}
-
-  virtual ~CJS_Timer() { KillJSTimer(); }
+  CJS_Timer(CJS_EmbedObj* pObj,
+            CPDFDoc_Environment* pApp,
+            CJS_Runtime* pRuntime,
+            int nType,
+            const CFX_WideString& script,
+            FX_DWORD dwElapse,
+            FX_DWORD dwTimeOut);
+  ~CJS_Timer() override;
 
- public:
-  FX_UINT SetJSTimer(FX_UINT nElapse);
   void KillJSTimer();
 
-  void SetType(int nType) { m_nType = nType; }
   int GetType() const { return m_nType; }
-
-  void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; }
-  FX_DWORD GetStartTime() const { return m_dwStartTime; }
-
-  void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; }
   FX_DWORD GetTimeOut() const { return m_dwTimeOut; }
-
-  void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; }
-  CJS_Runtime* GetRuntime() const { return m_pRuntime; }
-
-  void SetJScript(const CFX_WideString& script) { m_swJScript = script; }
+  CJS_Runtime* GetRuntime() const { return m_bValid ? m_pRuntime : nullptr; }
   CFX_WideString GetJScript() const { return m_swJScript; }
 
   static void TimerProc(int idEvent);
@@ -123,19 +105,20 @@ class CJS_Timer {
   using TimerMap = std::map<FX_UINT, CJS_Timer*>;
   static TimerMap* GetGlobalTimerMap();
 
-  FX_UINT m_nTimerID;
-  CJS_EmbedObj* m_pEmbedObj;
-  FX_BOOL m_bProcessing;
+  // CJS_Runtime::Observer
+  void OnDestroyed() override;
+
+  FX_DWORD m_nTimerID;
+  CJS_EmbedObj* const m_pEmbedObj;
+  bool m_bProcessing;
+  bool m_bValid;
 
   // data
-  FX_DWORD m_dwStartTime;
-  FX_DWORD m_dwTimeOut;
-  FX_DWORD m_dwElapse;
-  CJS_Runtime* m_pRuntime;
-  CFX_WideString m_swJScript;
-  int m_nType;  // 0:Interval; 1:TimeOut
-
-  CPDFDoc_Environment* m_pApp;
+  const int m_nType;  // 0:Interval; 1:TimeOut
+  const FX_DWORD m_dwTimeOut;
+  const CFX_WideString m_swJScript;
+  CJS_Runtime* const m_pRuntime;
+  CPDFDoc_Environment* const m_pApp;
 };
 
 #endif  // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_
diff --git a/fpdfsdk/include/javascript/JS_Runtime.h b/fpdfsdk/include/javascript/JS_Runtime.h
index 042bba9dcb..578da2e092 100644
--- a/fpdfsdk/include/javascript/JS_Runtime.h
+++ b/fpdfsdk/include/javascript/JS_Runtime.h
@@ -20,6 +20,14 @@ class CJS_Context;
 
 class CJS_Runtime : public IFXJS_Runtime {
  public:
+  class Observer {
+   public:
+    virtual void OnDestroyed() = 0;
+
+   protected:
+    virtual ~Observer() {}
+  };
+
   using FieldEvent = std::pair<CFX_WideString, JS_EVENT_T>;
 
   explicit CJS_Runtime(CPDFDoc_Environment* pApp);
@@ -50,6 +58,9 @@ class CJS_Runtime : public IFXJS_Runtime {
   virtual FX_BOOL SetHValueByName(const CFX_ByteStringC& utf8Name,
                                   FXJSE_HVALUE hValue);
 
+  void AddObserver(Observer* observer);
+  void RemoveObserver(Observer* observer);
+
  private:
   void DefineJSObjects();
 
@@ -62,6 +73,7 @@ class CJS_Runtime : public IFXJS_Runtime {
   bool m_isolateManaged;
   nonstd::unique_ptr<FXJS_ArrayBufferAllocator> m_pArrayBufferAllocator;
   v8::Global<v8::Context> m_context;
+  std::set<Observer*> m_observers;
 };
 
 #endif  // FPDFSDK_INCLUDE_JAVASCRIPT_JS_RUNTIME_H_
-- 
cgit v1.2.3