1 // Copyright 2014 The PDFium Authors 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 <memory> 11 #include <set> 12 #include <utility> 13 #include <vector> 14 15 #include "core/fxcrt/cfx_timer.h" 16 #include "core/fxcrt/observed_ptr.h" 17 #include "fxjs/cfxjs_engine.h" 18 #include "fxjs/cjs_event_context.h" 19 #include "fxjs/ijs_runtime.h" 20 21 class CPDFSDK_FormFillEnvironment; 22 23 class CJS_Runtime final : public IJS_Runtime, 24 public CFXJS_Engine, 25 public Observable { 26 public: 27 using FieldEvent = std::pair<WideString, CJS_EventContext::Kind>; 28 29 explicit CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv); 30 ~CJS_Runtime() override; 31 32 // IJS_Runtime: 33 CJS_Runtime* AsCJSRuntime() override; 34 IJS_EventContext* NewEventContext() override; 35 void ReleaseEventContext(IJS_EventContext* pContext) override; 36 CPDFSDK_FormFillEnvironment* GetFormFillEnv() const override; 37 absl::optional<IJS_Runtime::JS_Error> ExecuteScript( 38 const WideString& script) override; 39 40 CJS_EventContext* GetCurrentEventContext() const; 41 CFX_Timer::HandlerIface* GetTimerHandler() 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 BeginBlock()47 void BeginBlock() { m_bBlocking = true; } EndBlock()48 void EndBlock() { m_bBlocking = false; } IsBlocking()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 v8::Local<v8::Value> GetValueByNameFromGlobalObject(ByteStringView utf8Name); 56 bool SetValueByNameInGlobalObject(ByteStringView utf8Name, 57 v8::Local<v8::Value> pValue); 58 59 private: 60 void DefineJSObjects(); 61 void SetFormFillEnvToDocument(); 62 63 std::vector<std::unique_ptr<CJS_EventContext>> m_EventContextArray; 64 ObservedPtr<CPDFSDK_FormFillEnvironment> m_pFormFillEnv; 65 bool m_bBlocking = false; 66 bool m_isolateManaged = false; 67 std::set<FieldEvent> m_FieldEventSet; 68 }; 69 70 #endif // FXJS_CJS_RUNTIME_H_ 71