blob: b3f8ef68f2aa6db990dc2494d8bf6122fd63b9dc [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclaire0345a42017-10-30 20:20:42 +00007#ifndef FXJS_CJS_RUNTIME_H_
8#define FXJS_CJS_RUNTIME_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
tsepez41a53ad2016-03-28 16:59:30 -070010#include <memory>
Dan Sinclairf766ad22016-03-14 13:51:24 -040011#include <set>
Tom Sepez5d0e8432015-09-22 15:50:03 -070012#include <utility>
Tom Sepez4237aed2015-11-10 15:19:17 -080013#include <vector>
Tom Sepez5d0e8432015-09-22 15:50:03 -070014
Tom Sepez27c350f2019-06-11 23:52:35 +000015#include "core/fxcrt/observed_ptr.h"
Tom Sepez24374a62019-08-07 21:26:02 +000016#include "core/fxcrt/timerhandler_iface.h"
Tom Sepezb7c7df62018-02-09 19:08:59 +000017#include "fxjs/cfxjs_engine.h"
Tom Sepez7dbb85b2019-06-11 19:49:00 +000018#include "fxjs/cjs_eventrecorder.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000019#include "fxjs/ijs_runtime.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070020
Tom Sepezd6ae2af2017-02-16 11:49:55 -080021class CJS_EventContext;
Tom Sepez24374a62019-08-07 21:26:02 +000022class CPDFSDK_FormFillEnvironment;
Tom Sepez9a3f8122015-04-07 15:35:48 -070023
Tom Sepez55865452018-08-27 20:18:04 +000024class CJS_Runtime final : public IJS_Runtime,
25 public CFXJS_Engine,
Tom Sepezd8ae8f82019-06-12 17:58:33 +000026 public Observable {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 public:
Ryan Harrison275e2602017-09-18 14:23:18 -040028 using FieldEvent = std::pair<WideString, JS_EVENT_T>;
Tom Sepez5d0e8432015-09-22 15:50:03 -070029
dsinclair82e17672016-10-11 12:38:01 -070030 explicit CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv);
Lei Zhang2b1a2d52015-08-14 22:16:22 -070031 ~CJS_Runtime() override;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032
Tom Sepezfe8d4e32019-08-15 20:15:08 +000033 // IJS_Runtime:
34 CJS_Runtime* AsCJSRuntime() override;
Tom Sepezd6ae2af2017-02-16 11:49:55 -080035 IJS_EventContext* NewEventContext() override;
36 void ReleaseEventContext(IJS_EventContext* pContext) override;
dsinclair82e17672016-10-11 12:38:01 -070037 CPDFSDK_FormFillEnvironment* GetFormFillEnv() const override;
Dan Sinclairdc5d88b2018-05-17 13:53:52 +000038 Optional<IJS_Runtime::JS_Error> ExecuteScript(
39 const WideString& script) override;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040
Tom Sepezb1670b52017-02-16 17:01:00 -080041 CJS_EventContext* GetCurrentEventContext() const;
Tom Sepez24374a62019-08-07 21:26:02 +000042 TimerHandlerIface* GetTimerHandler() const;
Tom Sepezb1670b52017-02-16 17:01:00 -080043
Tom Sepez5d0e8432015-09-22 15:50:03 -070044 // Returns true if the event isn't already found in the set.
45 bool AddEventToSet(const FieldEvent& event);
46 void RemoveEventFromSet(const FieldEvent& event);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047
tsepez4cf55152016-11-02 14:37:54 -070048 void BeginBlock() { m_bBlocking = true; }
49 void EndBlock() { m_bBlocking = false; }
50 bool IsBlocking() const { return m_bBlocking; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
Dan Sinclaire85107b2017-10-24 15:29:22 -040052 // Attempt to convert the |value| into a number. If successful the number
53 // value will be returned, otherwise |value| is returned.
dan sinclair80435cb2017-10-24 21:40:24 -040054 v8::Local<v8::Value> MaybeCoerceToNumber(v8::Local<v8::Value> value);
Dan Sinclaire85107b2017-10-24 15:29:22 -040055
Tom Sepezc839ac72018-12-14 20:34:11 +000056 bool GetValueByNameFromGlobalObject(ByteStringView utf8Name,
Tom Sepezfe8d4e32019-08-15 20:15:08 +000057 v8::Local<v8::Value>* pValue);
Tom Sepezc839ac72018-12-14 20:34:11 +000058 bool SetValueByNameInGlobalObject(ByteStringView utf8Name,
Tom Sepezfe8d4e32019-08-15 20:15:08 +000059 v8::Local<v8::Value> pValue);
Bo Xufdc00a72014-10-28 23:03:33 -070060
Tom Sepez570875c2015-09-11 08:35:03 -070061 private:
Tom Sepez142165e2015-09-11 13:21:50 -070062 void DefineJSObjects();
dsinclair82e17672016-10-11 12:38:01 -070063 void SetFormFillEnvToDocument();
Tom Sepez570875c2015-09-11 08:35:03 -070064
Tom Sepezd6ae2af2017-02-16 11:49:55 -080065 std::vector<std::unique_ptr<CJS_EventContext>> m_EventContextArray;
Tom Sepezd8ae8f82019-06-12 17:58:33 +000066 ObservedPtr<CPDFSDK_FormFillEnvironment> m_pFormFillEnv;
Lei Zhangd5f42792018-08-29 23:18:08 +000067 bool m_bBlocking = false;
68 bool m_isolateManaged = false;
tsepeza4941912016-08-15 11:40:12 -070069 std::set<FieldEvent> m_FieldEventSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070};
71
Dan Sinclaire0345a42017-10-30 20:20:42 +000072#endif // FXJS_CJS_RUNTIME_H_