blob: 042bba9dcb1e780e08c18ca9d9326c97f6421c89 [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
Tom Sepez19922bb2015-05-28 13:23:12 -07007#ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_RUNTIME_H_
8#define FPDFSDK_INCLUDE_JAVASCRIPT_JS_RUNTIME_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
Tom Sepez5d0e8432015-09-22 15:50:03 -070010#include <set>
11#include <utility>
12
Tom Sepezd2cc1b92015-04-30 15:19:03 -070013#include "../../../third_party/base/nonstd_unique_ptr.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070014#include "../../../core/include/fxcrt/fx_basic.h"
15#include "../jsapi/fxjs_v8.h"
16#include "IJavaScript.h"
17#include "JS_EventHandler.h"
18
19class CJS_Context;
20
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021class CJS_Runtime : public IFXJS_Runtime {
22 public:
Tom Sepez5d0e8432015-09-22 15:50:03 -070023 using FieldEvent = std::pair<CFX_WideString, JS_EVENT_T>;
24
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070025 explicit CJS_Runtime(CPDFDoc_Environment* pApp);
Lei Zhang2b1a2d52015-08-14 22:16:22 -070026 ~CJS_Runtime() override;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070027
Lei Zhang2b1a2d52015-08-14 22:16:22 -070028 // IFXJS_Runtime
29 IFXJS_Context* NewContext() override;
30 void ReleaseContext(IFXJS_Context* pContext) override;
31 IFXJS_Context* GetCurrentContext() override;
32 void SetReaderDocument(CPDFSDK_Document* pReaderDoc) override;
33 CPDFSDK_Document* GetReaderDocument() override { return m_pDocument; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034
Tom Sepez808a99e2015-09-10 12:28:37 -070035 CPDFDoc_Environment* GetReaderApp() const { return m_pApp; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036
Tom Sepez5d0e8432015-09-22 15:50:03 -070037 // Returns true if the event isn't already found in the set.
38 bool AddEventToSet(const FieldEvent& event);
39 void RemoveEventFromSet(const FieldEvent& event);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 void BeginBlock() { m_bBlocking = TRUE; }
42 void EndBlock() { m_bBlocking = FALSE; }
Tom Sepez808a99e2015-09-10 12:28:37 -070043 FX_BOOL IsBlocking() const { return m_bBlocking; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Tom Sepez808a99e2015-09-10 12:28:37 -070045 v8::Isolate* GetIsolate() const { return m_isolate; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 v8::Local<v8::Context> NewJSContext();
Bo Xufdc00a72014-10-28 23:03:33 -070047
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 virtual FX_BOOL GetHValueByName(const CFX_ByteStringC& utf8Name,
49 FXJSE_HVALUE hValue);
50 virtual FX_BOOL SetHValueByName(const CFX_ByteStringC& utf8Name,
51 FXJSE_HVALUE hValue);
Bo Xufdc00a72014-10-28 23:03:33 -070052
Tom Sepez570875c2015-09-11 08:35:03 -070053 private:
Tom Sepez142165e2015-09-11 13:21:50 -070054 void DefineJSObjects();
Tom Sepez570875c2015-09-11 08:35:03 -070055
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 CFX_ArrayTemplate<CJS_Context*> m_ContextArray;
57 CPDFDoc_Environment* m_pApp;
58 CPDFSDK_Document* m_pDocument;
59 FX_BOOL m_bBlocking;
Tom Sepez5d0e8432015-09-22 15:50:03 -070060 std::set<FieldEvent> m_FieldEventSet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 v8::Isolate* m_isolate;
Jochen Eisinger29007842015-08-05 09:02:13 +020062 bool m_isolateManaged;
Tom Sepez39bfe122015-09-17 15:25:23 -070063 nonstd::unique_ptr<FXJS_ArrayBufferAllocator> m_pArrayBufferAllocator;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 v8::Global<v8::Context> m_context;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070065};
66
Tom Sepez19922bb2015-05-28 13:23:12 -070067#endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_RUNTIME_H_