blob: ac1a6883695db305310fcb4e30540f463b76c51e [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 Sepezd2cc1b92015-04-30 15:19:03 -070010#include "../../../third_party/base/nonstd_unique_ptr.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070011#include "../../../core/include/fxcrt/fx_basic.h"
12#include "../jsapi/fxjs_v8.h"
13#include "IJavaScript.h"
14#include "JS_EventHandler.h"
15
16class CJS_Context;
17
Tom Sepezd2cc1b92015-04-30 15:19:03 -070018class CJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019 void* Allocate(size_t length) override;
20 void* AllocateUninitialized(size_t length) override;
21 void Free(void* data, size_t length) override;
Tom Sepezd2cc1b92015-04-30 15:19:03 -070022};
23
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024class CJS_FieldEvent {
25 public:
26 CFX_WideString sTargetName;
27 JS_EVENT_T eEventType;
28 CJS_FieldEvent* pNext;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070029};
30
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031class CJS_Runtime : public IFXJS_Runtime {
32 public:
33 CJS_Runtime(CPDFDoc_Environment* pApp);
Lei Zhang2b1a2d52015-08-14 22:16:22 -070034 ~CJS_Runtime() override;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035
Lei Zhang2b1a2d52015-08-14 22:16:22 -070036 // IFXJS_Runtime
37 IFXJS_Context* NewContext() override;
38 void ReleaseContext(IFXJS_Context* pContext) override;
39 IFXJS_Context* GetCurrentContext() override;
40 void SetReaderDocument(CPDFSDK_Document* pReaderDoc) override;
41 CPDFSDK_Document* GetReaderDocument() override { return m_pDocument; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042
Tom Sepez808a99e2015-09-10 12:28:37 -070043 CPDFDoc_Environment* GetReaderApp() const { return m_pApp; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 FX_BOOL AddEventToLoop(const CFX_WideString& sTargetName,
46 JS_EVENT_T eEventType);
47 void RemoveEventInLoop(const CFX_WideString& sTargetName,
48 JS_EVENT_T eEventType);
49 void RemoveEventsInLoop(CJS_FieldEvent* pStart);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 void BeginBlock() { m_bBlocking = TRUE; }
52 void EndBlock() { m_bBlocking = FALSE; }
Tom Sepez808a99e2015-09-10 12:28:37 -070053 FX_BOOL IsBlocking() const { return m_bBlocking; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070054
Tom Sepez808a99e2015-09-10 12:28:37 -070055 v8::Isolate* GetIsolate() const { return m_isolate; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 v8::Local<v8::Context> NewJSContext();
Bo Xufdc00a72014-10-28 23:03:33 -070057
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 virtual FX_BOOL GetHValueByName(const CFX_ByteStringC& utf8Name,
59 FXJSE_HVALUE hValue);
60 virtual FX_BOOL SetHValueByName(const CFX_ByteStringC& utf8Name,
61 FXJSE_HVALUE hValue);
Bo Xufdc00a72014-10-28 23:03:33 -070062
Tom Sepez570875c2015-09-11 08:35:03 -070063 private:
Tom Sepez142165e2015-09-11 13:21:50 -070064 void DefineJSObjects();
Tom Sepez570875c2015-09-11 08:35:03 -070065
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 CFX_ArrayTemplate<CJS_Context*> m_ContextArray;
67 CPDFDoc_Environment* m_pApp;
68 CPDFSDK_Document* m_pDocument;
69 FX_BOOL m_bBlocking;
70 CJS_FieldEvent* m_pFieldEventPath;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 v8::Isolate* m_isolate;
Jochen Eisinger29007842015-08-05 09:02:13 +020072 bool m_isolateManaged;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 nonstd::unique_ptr<CJS_ArrayBufferAllocator> m_pArrayBufferAllocator;
74 v8::Global<v8::Context> m_context;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075};
76
Tom Sepez19922bb2015-05-28 13:23:12 -070077#endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_RUNTIME_H_