| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "xfa/fxjse/runtime.h" | 7 #include "xfa/fxjse/runtime.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "fpdfsdk/jsapi/include/fxjs_v8.h" | 11 #include "fpdfsdk/jsapi/include/fxjs_v8.h" |
| 12 #include "xfa/fxjse/scope_inline.h" | 12 #include "xfa/fxjse/scope_inline.h" |
| 13 | 13 |
| 14 // Duplicates fpdfsdk's cjs_runtime.h, but keeps XFA from depending on it. | 14 // Duplicates fpdfsdk's cjs_runtime.h, but keeps XFA from depending on it. |
| 15 // TODO(tsepez): make a single version of this. | 15 // TODO(tsepez): make a single version of this. |
| 16 class FXJSE_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | 16 class FXJSE_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { |
| 17 void* Allocate(size_t length) override { return calloc(1, length); } | 17 void* Allocate(size_t length) override { return calloc(1, length); } |
| 18 void* AllocateUninitialized(size_t length) override { return malloc(length); } | 18 void* AllocateUninitialized(size_t length) override { return malloc(length); } |
| 19 void Free(void* data, size_t length) override { free(data); } | 19 void Free(void* data, size_t length) override { free(data); } |
| 20 }; | 20 }; |
| 21 | 21 |
| 22 static void FXJSE_KillV8() { | 22 static void FXJSE_KillV8() { |
| 23 v8::V8::Dispose(); | 23 v8::V8::Dispose(); |
| 24 } | 24 } |
| 25 | 25 |
| 26 void FXJSE_Initialize() { | 26 void FXJSE_Initialize() { |
| 27 if (!CFXJSE_RuntimeData::g_RuntimeList) { | 27 if (!CFXJSE_IsolateTracker::g_pInstance) |
| 28 CFXJSE_RuntimeData::g_RuntimeList = new CFXJSE_RuntimeList; | 28 CFXJSE_IsolateTracker::g_pInstance = new CFXJSE_IsolateTracker; |
| 29 } | 29 |
| 30 static FX_BOOL bV8Initialized = FALSE; | 30 static FX_BOOL bV8Initialized = FALSE; |
| 31 if (bV8Initialized) { | 31 if (bV8Initialized) |
| 32 return; | 32 return; |
| 33 } | 33 |
| 34 bV8Initialized = TRUE; | 34 bV8Initialized = TRUE; |
| 35 atexit(FXJSE_KillV8); | 35 atexit(FXJSE_KillV8); |
| 36 } | 36 } |
| 37 | 37 |
| 38 static void FXJSE_Runtime_DisposeCallback(v8::Isolate* pIsolate) { | 38 static void FXJSE_Runtime_DisposeCallback(v8::Isolate* pIsolate, bool bOwned) { |
| 39 if (FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate)) { | 39 if (FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate)) { |
| 40 delete pData->m_pFXJSERuntimeData; | 40 delete pData->m_pFXJSERuntimeData; |
| 41 pData->m_pFXJSERuntimeData = nullptr; | 41 pData->m_pFXJSERuntimeData = nullptr; |
| 42 } | 42 } |
| 43 pIsolate->Dispose(); | 43 if (bOwned) |
| 44 pIsolate->Dispose(); |
| 44 } | 45 } |
| 45 | 46 |
| 46 void FXJSE_Finalize() { | 47 void FXJSE_Finalize() { |
| 47 if (CFXJSE_RuntimeData::g_RuntimeList) { | 48 if (!CFXJSE_IsolateTracker::g_pInstance) |
| 48 CFXJSE_RuntimeData::g_RuntimeList->RemoveAllRuntimes( | 49 return; |
| 49 FXJSE_Runtime_DisposeCallback); | 50 |
| 50 delete CFXJSE_RuntimeData::g_RuntimeList; | 51 CFXJSE_IsolateTracker::g_pInstance->RemoveAll(FXJSE_Runtime_DisposeCallback); |
| 51 CFXJSE_RuntimeData::g_RuntimeList = NULL; | 52 delete CFXJSE_IsolateTracker::g_pInstance; |
| 52 } | 53 CFXJSE_IsolateTracker::g_pInstance = nullptr; |
| 53 } | 54 } |
| 54 | 55 |
| 55 v8::Isolate* FXJSE_Runtime_Create() { | 56 v8::Isolate* FXJSE_Runtime_Create_Own() { |
| 56 v8::Isolate::CreateParams params; | 57 v8::Isolate::CreateParams params; |
| 57 params.array_buffer_allocator = new FXJSE_ArrayBufferAllocator(); | 58 params.array_buffer_allocator = new FXJSE_ArrayBufferAllocator(); |
| 58 v8::Isolate* pIsolate = v8::Isolate::New(params); | 59 v8::Isolate* pIsolate = v8::Isolate::New(params); |
| 59 ASSERT(pIsolate && CFXJSE_RuntimeData::g_RuntimeList); | 60 ASSERT(pIsolate && CFXJSE_IsolateTracker::g_pInstance); |
| 60 CFXJSE_RuntimeData::g_RuntimeList->AppendRuntime(pIsolate); | 61 CFXJSE_IsolateTracker::g_pInstance->Append(pIsolate); |
| 61 return pIsolate; | 62 return pIsolate; |
| 62 } | 63 } |
| 63 | 64 |
| 64 void FXJSE_Runtime_Release(v8::Isolate* pIsolate, bool bOwnedRuntime) { | 65 void FXJSE_Runtime_Release(v8::Isolate* pIsolate) { |
| 65 if (!pIsolate) | 66 if (!pIsolate) |
| 66 return; | 67 return; |
| 67 if (bOwnedRuntime) { | 68 CFXJSE_IsolateTracker::g_pInstance->Remove(pIsolate, |
| 68 ASSERT(CFXJSE_RuntimeData::g_RuntimeList); | 69 FXJSE_Runtime_DisposeCallback); |
| 69 CFXJSE_RuntimeData::g_RuntimeList->RemoveRuntime( | |
| 70 pIsolate, FXJSE_Runtime_DisposeCallback); | |
| 71 } else { | |
| 72 if (FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate)) { | |
| 73 delete pData->m_pFXJSERuntimeData; | |
| 74 pData->m_pFXJSERuntimeData = nullptr; | |
| 75 } | |
| 76 } | |
| 77 } | 70 } |
| 78 | 71 |
| 79 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Create(v8::Isolate* pIsolate) { | 72 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Create(v8::Isolate* pIsolate) { |
| 80 CFXJSE_RuntimeData* pRuntimeData = new CFXJSE_RuntimeData(pIsolate); | 73 CFXJSE_RuntimeData* pRuntimeData = new CFXJSE_RuntimeData(pIsolate); |
| 81 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); | 74 CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate); |
| 82 v8::Local<v8::FunctionTemplate> hFuncTemplate = | 75 v8::Local<v8::FunctionTemplate> hFuncTemplate = |
| 83 v8::FunctionTemplate::New(pIsolate); | 76 v8::FunctionTemplate::New(pIsolate); |
| 84 v8::Local<v8::Context> hContext = | 77 v8::Local<v8::Context> hContext = |
| 85 v8::Context::New(pIsolate, 0, hFuncTemplate->InstanceTemplate()); | 78 v8::Context::New(pIsolate, 0, hFuncTemplate->InstanceTemplate()); |
| 86 hContext->SetSecurityToken(v8::External::New(pIsolate, pIsolate)); | 79 hContext->SetSecurityToken(v8::External::New(pIsolate, pIsolate)); |
| 87 pRuntimeData->m_hRootContextGlobalTemplate.Reset(pIsolate, hFuncTemplate); | 80 pRuntimeData->m_hRootContextGlobalTemplate.Reset(pIsolate, hFuncTemplate); |
| 88 pRuntimeData->m_hRootContext.Reset(pIsolate, hContext); | 81 pRuntimeData->m_hRootContext.Reset(pIsolate, hContext); |
| 89 return pRuntimeData; | 82 return pRuntimeData; |
| 90 } | 83 } |
| 91 | 84 |
| 92 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) { | 85 CFXJSE_RuntimeData* CFXJSE_RuntimeData::Get(v8::Isolate* pIsolate) { |
| 93 FXJS_PerIsolateData::SetUp(pIsolate); | 86 FXJS_PerIsolateData::SetUp(pIsolate); |
| 94 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 87 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
| 95 if (!pData->m_pFXJSERuntimeData) | 88 if (!pData->m_pFXJSERuntimeData) |
| 96 pData->m_pFXJSERuntimeData = CFXJSE_RuntimeData::Create(pIsolate); | 89 pData->m_pFXJSERuntimeData = CFXJSE_RuntimeData::Create(pIsolate); |
| 97 return pData->m_pFXJSERuntimeData; | 90 return pData->m_pFXJSERuntimeData; |
| 98 } | 91 } |
| 99 | 92 |
| 100 CFXJSE_RuntimeList* CFXJSE_RuntimeData::g_RuntimeList = nullptr; | 93 CFXJSE_IsolateTracker* CFXJSE_IsolateTracker::g_pInstance = nullptr; |
| 101 | 94 |
| 102 void CFXJSE_RuntimeList::AppendRuntime(v8::Isolate* pIsolate) { | 95 void CFXJSE_IsolateTracker::Append(v8::Isolate* pIsolate) { |
| 103 m_RuntimeList.push_back(pIsolate); | 96 m_OwnedIsolates.push_back(pIsolate); |
| 104 } | 97 } |
| 105 | 98 |
| 106 void CFXJSE_RuntimeList::RemoveRuntime( | 99 void CFXJSE_IsolateTracker::Remove( |
| 107 v8::Isolate* pIsolate, | 100 v8::Isolate* pIsolate, |
| 108 CFXJSE_RuntimeList::RuntimeDisposeCallback lpfnDisposeCallback) { | 101 CFXJSE_IsolateTracker::DisposeCallback lpfnDisposeCallback) { |
| 109 auto it = std::find(m_RuntimeList.begin(), m_RuntimeList.end(), pIsolate); | 102 auto it = std::find(m_OwnedIsolates.begin(), m_OwnedIsolates.end(), pIsolate); |
| 110 if (it != m_RuntimeList.end()) | 103 bool bFound = it != m_OwnedIsolates.end(); |
| 111 m_RuntimeList.erase(it); | 104 if (bFound) |
| 112 if (lpfnDisposeCallback) | 105 m_OwnedIsolates.erase(it); |
| 113 lpfnDisposeCallback(pIsolate); | 106 lpfnDisposeCallback(pIsolate, bFound); |
| 114 } | 107 } |
| 115 | 108 |
| 116 void CFXJSE_RuntimeList::RemoveAllRuntimes( | 109 void CFXJSE_IsolateTracker::RemoveAll( |
| 117 CFXJSE_RuntimeList::RuntimeDisposeCallback lpfnDisposeCallback) { | 110 CFXJSE_IsolateTracker::DisposeCallback lpfnDisposeCallback) { |
| 118 if (lpfnDisposeCallback) { | 111 for (v8::Isolate* pIsolate : m_OwnedIsolates) |
| 119 for (v8::Isolate* pIsolate : m_RuntimeList) | 112 lpfnDisposeCallback(pIsolate, true); |
| 120 lpfnDisposeCallback(pIsolate); | 113 |
| 121 } | 114 m_OwnedIsolates.clear(); |
| 122 m_RuntimeList.clear(); | |
| 123 } | 115 } |
| OLD | NEW |