1 // 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.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_RUNTIME_H_
8 #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_RUNTIME_H_
9 
10 #include "../../../third_party/base/nonstd_unique_ptr.h"
11 #include "../../../core/include/fxcrt/fx_basic.h"
12 #include "../jsapi/fxjs_v8.h"
13 #include "IJavaScript.h"
14 #include "JS_EventHandler.h"
15 
16 class CJS_Context;
17 
18 class CJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
19     void* Allocate(size_t length) override;
20     void* AllocateUninitialized(size_t length) override;
21     void Free(void* data, size_t length) override;
22 };
23 
24 class CJS_FieldEvent
25 {
26 public:
27 	CFX_WideString		sTargetName;
28 	JS_EVENT_T			eEventType;
29 	CJS_FieldEvent*		pNext;
30 };
31 
32 class CJS_Runtime : public IFXJS_Runtime
33 {
34 public:
35 	CJS_Runtime(CPDFDoc_Environment * pApp);
36 	virtual ~CJS_Runtime();
37 
38 	virtual IFXJS_Context *					NewContext();
39 	virtual void							ReleaseContext(IFXJS_Context * pContext);
40 	virtual IFXJS_Context*					GetCurrentContext();
41 
42 	virtual void							SetReaderDocument(CPDFSDK_Document *pReaderDoc);
GetReaderDocument()43 	virtual CPDFSDK_Document *				GetReaderDocument(){return m_pDocument;}
44 
GetReaderApp()45 	CPDFDoc_Environment *							GetReaderApp(){return m_pApp;}
46 
47 	FX_BOOL									InitJSObjects();
48 
49 	FX_BOOL									AddEventToLoop(const CFX_WideString& sTargetName, JS_EVENT_T eEventType);
50 	void									RemoveEventInLoop(const CFX_WideString& sTargetName, JS_EVENT_T eEventType);
51 	void									RemoveEventsInLoop(CJS_FieldEvent* pStart);
52 
BeginBlock()53 	void									BeginBlock(){m_bBlocking = TRUE;}
EndBlock()54 	void									EndBlock(){m_bBlocking = FALSE;}
IsBlocking()55 	FX_BOOL									IsBlocking(){return m_bBlocking;}
56 
57 	operator								IJS_Runtime*() {return (IJS_Runtime*)m_isolate;}
GetIsolate()58 	v8::Isolate*								GetIsolate(){return m_isolate;};
SetIsolate(v8::Isolate * isolate)59 	void									SetIsolate(v8::Isolate* isolate){m_isolate = isolate;}
60 
61 	v8::Local<v8::Context>							NewJSContext();
62 protected:
63 	CFX_ArrayTemplate<CJS_Context*>		m_ContextArray;
64 	CPDFDoc_Environment*							m_pApp;
65 	CPDFSDK_Document*						m_pDocument;
66 	FX_BOOL									m_bBlocking;
67 	FX_BOOL									m_bRegistered;
68 	CJS_FieldEvent*							m_pFieldEventPath;
69 
70 	v8::Isolate* m_isolate;
71 	nonstd::unique_ptr<CJS_ArrayBufferAllocator> m_pArrayBufferAllocator;
72 	v8::Global<v8::Context> m_context;
73 };
74 
75 #endif  // FPDFSDK_INCLUDE_JAVASCRIPT_JS_RUNTIME_H_
76