OLD | NEW |
| (Empty) |
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 #include "xfa/src/fxjse/runtime.h" | |
8 | |
9 #include "fpdfsdk/include/jsapi/fxjs_v8.h" | |
10 #include "xfa/src/fxjse/scope_inline.h" | |
11 | |
12 // Duplicates fpdfsdk's JS_Runtime.h, but keeps XFA from depending on it. | |
13 // TODO(tsepez): make a single version of this. | |
14 class FXJSE_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | |
15 void* Allocate(size_t length) override { return calloc(1, length); } | |
16 void* AllocateUninitialized(size_t length) override { return malloc(length); } | |
17 void Free(void* data, size_t length) override { free(data); } | |
18 }; | |
19 | |
20 static void FXJSE_KillV8() { | |
21 v8::V8::Dispose(); | |
22 } | |
23 | |
24 void FXJSE_Initialize() { | |
25 if (!CFXJSE_RuntimeData::g_RuntimeList) { | |
26 CFXJSE_RuntimeData::g_RuntimeList = new CFXJSE_RuntimeList; | |
27 } | |
28 static FX_BOOL bV8Initialized = FALSE; | |
29 if (bV8Initialized) { | |
30 return; | |
31 } | |
32 bV8Initialized = TRUE; | |
33 atexit(FXJSE_KillV8); | |
34 } | |
35 | |
36 static void FXJSE_Runtime_DisposeCallback(v8::Isolate* pIsolate) { | |
37 { | |
38 v8::Locker locker(pIsolate); | |
39 if (FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate)) { | |
40 delete pData->m_pFXJSERuntimeData; | |
41 pData->m_pFXJSERuntimeData = nullptr; | |
42 } | |
43 } | |
44 pIsolate->Dispose(); | |
45 } | |
46 | |
47 void FXJSE_Finalize() { | |
48 if (CFXJSE_RuntimeData::g_RuntimeList) { | |
49 CFXJSE_RuntimeData::g_RuntimeList->RemoveAllRuntimes( | |
50 FXJSE_Runtime_DisposeCallback); | |
51 delete CFXJSE_RuntimeData::g_RuntimeList; | |
52 CFXJSE_RuntimeData::g_RuntimeList = NULL; | |
53 } | |
54 } | |
55 | |
56 FXJSE_HRUNTIME FXJSE_Runtime_Create() { | |
57 v8::Isolate::CreateParams params; | |
58 params.array_buffer_allocator = new FXJSE_ArrayBufferAllocator(); | |
59 v8::Isolate* pIsolate = v8::Isolate::New(params); | |
60 ASSERT(pIsolate && CFXJSE_RuntimeData::g_RuntimeList); | |
61 CFXJSE_RuntimeData::g_RuntimeList->AppendRuntime(pIsolate); | |
62 return reinterpret_cast<FXJSE_HRUNTIME>(pIsolate); | |
63 } | |
64 | |
65 void FXJSE_Runtime_Release(FXJSE_HRUNTIME hRuntime, bool bOwnedRuntime) { | |
66 v8::Isolate* pIsolate = reinterpret_cast<v8::Isolate*>(hRuntime); | |
67 if (!pIsolate) | |
68 return; | |
69 if (bOwnedRuntime) { | |
70 ASSERT(CFXJSE_RuntimeData::g_RuntimeList); | |
71 CFXJSE_RuntimeData::g_RuntimeList->RemoveRuntime( | |
72 pIsolate, FXJSE_Runtime_DisposeCallback); | |
73 } else { | |
74 if (FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate)) { | |
75 delete pData->m_pFXJSERuntimeData; | |
76 pData->m_pFXJSERuntimeData = nullptr; | |
77 } | |
78 } | |
79 } | |
80 | |
81 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Create(v8::Isolate* pIsolate) { | |
82 CFXJSE_RuntimeData* pRuntimeData = new CFXJSE_RuntimeData(pIsolate); | |
83 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); | |
84 v8::Local<v8::FunctionTemplate> hFuncTemplate = | |
85 v8::FunctionTemplate::New(pIsolate); | |
86 v8::Local<v8::Context> hContext = | |
87 v8::Context::New(pIsolate, 0, hFuncTemplate->InstanceTemplate()); | |
88 hContext->SetSecurityToken(v8::External::New(pIsolate, pIsolate)); | |
89 pRuntimeData->m_hRootContextGlobalTemplate.Reset(pIsolate, hFuncTemplate); | |
90 pRuntimeData->m_hRootContext.Reset(pIsolate, hContext); | |
91 return pRuntimeData; | |
92 } | |
93 | |
94 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) { | |
95 FXJS_PerIsolateData::SetUp(pIsolate); | |
96 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | |
97 if (!pData->m_pFXJSERuntimeData) | |
98 pData->m_pFXJSERuntimeData = CFXJSE_RuntimeData::Create(pIsolate); | |
99 return pData->m_pFXJSERuntimeData; | |
100 } | |
101 | |
102 CFXJSE_RuntimeList* CFXJSE_RuntimeData::g_RuntimeList = NULL; | |
103 void CFXJSE_RuntimeList::AppendRuntime(v8::Isolate* pIsolate) { | |
104 m_RuntimeList.Add(pIsolate); | |
105 } | |
106 | |
107 void CFXJSE_RuntimeList::RemoveRuntime( | |
108 v8::Isolate* pIsolate, | |
109 CFXJSE_RuntimeList::RuntimeDisposeCallback lpfnDisposeCallback) { | |
110 int32_t iIdx = m_RuntimeList.Find(pIsolate, 0); | |
111 if (iIdx >= 0) { | |
112 m_RuntimeList.RemoveAt(iIdx, 1); | |
113 } | |
114 if (lpfnDisposeCallback) { | |
115 lpfnDisposeCallback(pIsolate); | |
116 } | |
117 } | |
118 | |
119 void CFXJSE_RuntimeList::RemoveAllRuntimes( | |
120 CFXJSE_RuntimeList::RuntimeDisposeCallback lpfnDisposeCallback) { | |
121 int32_t iSize = m_RuntimeList.GetSize(); | |
122 if (lpfnDisposeCallback) { | |
123 for (int32_t iIdx = 0; iIdx < iSize; iIdx++) { | |
124 lpfnDisposeCallback(m_RuntimeList[iIdx]); | |
125 } | |
126 } | |
127 m_RuntimeList.RemoveAll(); | |
128 } | |
OLD | NEW |