Home | History | Annotate | Download | only in javascript
      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_RUNTIME_H_
      8 #define FPDFSDK_SRC_JAVASCRIPT_JS_RUNTIME_H_
      9 
     10 #include <set>
     11 #include <utility>
     12 #include <vector>
     13 
     14 #include "JS_EventHandler.h"
     15 #include "core/include/fxcrt/fx_basic.h"
     16 #include "fpdfsdk/include/javascript/IJavaScript.h"
     17 #include "fpdfsdk/include/jsapi/fxjs_v8.h"
     18 
     19 class CJS_Context;
     20 
     21 class CJS_Runtime : public IJS_Runtime {
     22  public:
     23   class Observer {
     24    public:
     25     virtual void OnDestroyed() = 0;
     26 
     27    protected:
     28     virtual ~Observer() {}
     29   };
     30 
     31   using FieldEvent = std::pair<CFX_WideString, JS_EVENT_T>;
     32 
     33   static CJS_Runtime* FromContext(const IJS_Context* cc);
     34 
     35   explicit CJS_Runtime(CPDFDoc_Environment* pApp);
     36   ~CJS_Runtime() override;
     37 
     38   // IJS_Runtime
     39   IJS_Context* NewContext() override;
     40   void ReleaseContext(IJS_Context* pContext) override;
     41   IJS_Context* GetCurrentContext() override;
     42   void SetReaderDocument(CPDFSDK_Document* pReaderDoc) override;
     43   CPDFSDK_Document* GetReaderDocument() override { return m_pDocument; }
     44   int Execute(IJS_Context* cc,
     45               const wchar_t* script,
     46               CFX_WideString* info) override;
     47 
     48   CPDFDoc_Environment* GetReaderApp() const { return m_pApp; }
     49 
     50   // Returns true if the event isn't already found in the set.
     51   bool AddEventToSet(const FieldEvent& event);
     52   void RemoveEventFromSet(const FieldEvent& event);
     53 
     54   void BeginBlock() { m_bBlocking = TRUE; }
     55   void EndBlock() { m_bBlocking = FALSE; }
     56   FX_BOOL IsBlocking() const { return m_bBlocking; }
     57 
     58   v8::Isolate* GetIsolate() const { return m_isolate; }
     59   v8::Local<v8::Context> NewJSContext();
     60 
     61 #ifdef PDF_ENABLE_XFA
     62   FX_BOOL GetHValueByName(const CFX_ByteStringC& utf8Name,
     63                           FXJSE_HVALUE hValue) override;
     64   FX_BOOL SetHValueByName(const CFX_ByteStringC& utf8Name,
     65                           FXJSE_HVALUE hValue) override;
     66 #endif  // PDF_ENABLE_XFA
     67 
     68   void AddObserver(Observer* observer);
     69   void RemoveObserver(Observer* observer);
     70 
     71  private:
     72   void DefineJSObjects();
     73 
     74   CFX_ArrayTemplate<CJS_Context*> m_ContextArray;
     75   CPDFDoc_Environment* m_pApp;
     76   CPDFSDK_Document* m_pDocument;
     77   FX_BOOL m_bBlocking;
     78   std::set<FieldEvent> m_FieldEventSet;
     79   v8::Isolate* m_isolate;
     80   bool m_isolateManaged;
     81   v8::Global<v8::Context> m_context;
     82   std::vector<v8::Global<v8::Object>*> m_StaticObjects;
     83   std::set<Observer*> m_observers;
     84 };
     85 
     86 #endif  // FPDFSDK_SRC_JAVASCRIPT_JS_RUNTIME_H_
     87