blob: db77eeb8eddfae3b5e601dc59e01a9c48d7e1a59 [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 Sepez37458412015-10-06 11:33:46 -07007#ifndef FPDFSDK_SRC_JAVASCRIPT_JS_RUNTIME_H_
8#define FPDFSDK_SRC_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 Sepez37458412015-10-06 11:33:46 -070013#include "../../include/javascript/IJavaScript.h"
14#include "../../include/jsapi/fxjs_v8.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070015#include "JS_EventHandler.h"
Lei Zhanga688a042015-11-09 13:57:49 -080016#include "core/include/fxcrt/fx_basic.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070017
18class CJS_Context;
19
Tom Sepezba038bc2015-10-08 12:03:00 -070020class CJS_Runtime : public IJS_Runtime {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070021 public:
Lei Zhang2d5a0e12015-10-05 17:00:03 -070022 class Observer {
23 public:
24 virtual void OnDestroyed() = 0;
25
26 protected:
27 virtual ~Observer() {}
28 };
29
Tom Sepez5d0e8432015-09-22 15:50:03 -070030 using FieldEvent = std::pair<CFX_WideString, JS_EVENT_T>;
31
Tom Sepez67fd5df2015-10-08 12:24:19 -070032 static CJS_Runtime* FromContext(const IJS_Context* cc);
33
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070034 explicit CJS_Runtime(CPDFDoc_Environment* pApp);
Lei Zhang2b1a2d52015-08-14 22:16:22 -070035 ~CJS_Runtime() override;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036
Tom Sepezba038bc2015-10-08 12:03:00 -070037 // IJS_Runtime
38 IJS_Context* NewContext() override;
39 void ReleaseContext(IJS_Context* pContext) override;
40 IJS_Context* GetCurrentContext() override;
Lei Zhang2b1a2d52015-08-14 22:16:22 -070041 void SetReaderDocument(CPDFSDK_Document* pReaderDoc) override;
42 CPDFSDK_Document* GetReaderDocument() override { return m_pDocument; }
Tom Sepez33420902015-10-13 15:00:10 -070043 int Execute(IJS_Context* cc,
44 const wchar_t* script,
45 CFX_WideString* info) override;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046
Tom Sepez808a99e2015-09-10 12:28:37 -070047 CPDFDoc_Environment* GetReaderApp() const { return m_pApp; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048
Tom Sepez5d0e8432015-09-22 15:50:03 -070049 // Returns true if the event isn't already found in the set.
50 bool AddEventToSet(const FieldEvent& event);
51 void RemoveEventFromSet(const FieldEvent& event);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 void BeginBlock() { m_bBlocking = TRUE; }
54 void EndBlock() { m_bBlocking = FALSE; }
Tom Sepez808a99e2015-09-10 12:28:37 -070055 FX_BOOL IsBlocking() const { return m_bBlocking; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070056
Tom Sepez808a99e2015-09-10 12:28:37 -070057 v8::Isolate* GetIsolate() const { return m_isolate; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 v8::Local<v8::Context> NewJSContext();
Bo Xufdc00a72014-10-28 23:03:33 -070059
Tom Sepez3343d142015-11-02 09:54:54 -080060 // IJS_Runtime:
61 FX_BOOL GetHValueByName(const CFX_ByteStringC& utf8Name,
62 FXJSE_HVALUE hValue) override;
63 FX_BOOL SetHValueByName(const CFX_ByteStringC& utf8Name,
64 FXJSE_HVALUE hValue) override;
Bo Xufdc00a72014-10-28 23:03:33 -070065
Lei Zhang2d5a0e12015-10-05 17:00:03 -070066 void AddObserver(Observer* observer);
67 void RemoveObserver(Observer* observer);
68
Tom Sepez570875c2015-09-11 08:35:03 -070069 private:
Tom Sepez142165e2015-09-11 13:21:50 -070070 void DefineJSObjects();
Tom Sepez570875c2015-09-11 08:35:03 -070071
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 CFX_ArrayTemplate<CJS_Context*> m_ContextArray;
73 CPDFDoc_Environment* m_pApp;
74 CPDFSDK_Document* m_pDocument;
75 FX_BOOL m_bBlocking;
Tom Sepez5d0e8432015-09-22 15:50:03 -070076 std::set<FieldEvent> m_FieldEventSet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 v8::Isolate* m_isolate;
Jochen Eisinger29007842015-08-05 09:02:13 +020078 bool m_isolateManaged;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 v8::Global<v8::Context> m_context;
Lei Zhang2d5a0e12015-10-05 17:00:03 -070080 std::set<Observer*> m_observers;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081};
82
Tom Sepez37458412015-10-06 11:33:46 -070083#endif // FPDFSDK_SRC_JAVASCRIPT_JS_RUNTIME_H_