Home | History | Annotate | Download | only in fxjs
      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 FXJS_CJS_RUNTIME_H_
      8 #define FXJS_CJS_RUNTIME_H_
      9 
     10 #include <map>
     11 #include <memory>
     12 #include <set>
     13 #include <utility>
     14 #include <vector>
     15 
     16 #include "core/fxcrt/observable.h"
     17 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
     18 #include "fxjs/cjs_eventhandler.h"
     19 #include "fxjs/fxjs_v8.h"
     20 #include "fxjs/ijs_runtime.h"
     21 
     22 class CJS_EventContext;
     23 
     24 class CJS_Runtime : public IJS_Runtime,
     25                     public CFXJS_Engine,
     26                     public Observable<CJS_Runtime> {
     27  public:
     28   using FieldEvent = std::pair<WideString, JS_EVENT_T>;
     29 
     30   static CJS_Runtime* CurrentRuntimeFromIsolate(v8::Isolate* pIsolate);
     31 
     32   explicit CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv);
     33   ~CJS_Runtime() override;
     34 
     35   // IJS_Runtime
     36   IJS_EventContext* NewEventContext() override;
     37   void ReleaseEventContext(IJS_EventContext* pContext) override;
     38   CPDFSDK_FormFillEnvironment* GetFormFillEnv() const override;
     39   int ExecuteScript(const WideString& script, WideString* info) override;
     40 
     41   CJS_EventContext* GetCurrentEventContext() const;
     42 
     43   // Returns true if the event isn't already found in the set.
     44   bool AddEventToSet(const FieldEvent& event);
     45   void RemoveEventFromSet(const FieldEvent& event);
     46 
     47   void BeginBlock() { m_bBlocking = true; }
     48   void EndBlock() { m_bBlocking = false; }
     49   bool IsBlocking() const { return m_bBlocking; }
     50 
     51   // Attempt to convert the |value| into a number. If successful the number
     52   // value will be returned, otherwise |value| is returned.
     53   v8::Local<v8::Value> MaybeCoerceToNumber(v8::Local<v8::Value> value);
     54 
     55 #ifdef PDF_ENABLE_XFA
     56   bool GetValueByName(const ByteStringView& utf8Name,
     57                       CFXJSE_Value* pValue) override;
     58   bool SetValueByName(const ByteStringView& utf8Name,
     59                       CFXJSE_Value* pValue) override;
     60 #endif  // PDF_ENABLE_XFA
     61 
     62  private:
     63   void DefineJSObjects();
     64   void SetFormFillEnvToDocument();
     65 
     66   std::vector<std::unique_ptr<CJS_EventContext>> m_EventContextArray;
     67   CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv;
     68   bool m_bBlocking;
     69   bool m_isolateManaged;
     70   std::set<FieldEvent> m_FieldEventSet;
     71 };
     72 
     73 #endif  // FXJS_CJS_RUNTIME_H_
     74