| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium 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 #include "gin/public/isolate_holder.h" | 5 #include "gin/public/isolate_holder.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "base/files/memory_mapped_file.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 12 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 13 #include "base/sys_info.h" | 14 #include "base/sys_info.h" |
| 14 #include "gin/array_buffer.h" | 15 #include "gin/array_buffer.h" |
| 15 #include "gin/debug_impl.h" | 16 #include "gin/debug_impl.h" |
| 16 #include "gin/function_template.h" | 17 #include "gin/function_template.h" |
| 17 #include "gin/per_isolate_data.h" | 18 #include "gin/per_isolate_data.h" |
| 18 #include "gin/public/v8_platform.h" | 19 #include "gin/public/v8_platform.h" |
| 19 #include "gin/run_microtasks_observer.h" | 20 #include "gin/run_microtasks_observer.h" |
| 20 | 21 |
| 21 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 22 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 22 #include "base/files/memory_mapped_file.h" | |
| 23 #include "base/path_service.h" | 23 #include "base/path_service.h" |
| 24 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 24 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 25 | 25 |
| 26 namespace gin { | 26 namespace gin { |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; | 30 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; |
| 31 | 31 |
| 32 bool GenerateEntropy(unsigned char* buffer, size_t amount) { | 32 bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
| 33 base::RandBytes(buffer, amount); | 33 base::RandBytes(buffer, amount); |
| 34 return true; | 34 return true; |
| 35 } | 35 } |
| 36 | 36 |
| 37 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | |
| 38 base::MemoryMappedFile* g_mapped_natives = NULL; | 37 base::MemoryMappedFile* g_mapped_natives = NULL; |
| 39 base::MemoryMappedFile* g_mapped_snapshot = NULL; | 38 base::MemoryMappedFile* g_mapped_snapshot = NULL; |
| 40 | 39 |
| 40 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 41 bool MapV8Files(base::FilePath* natives_path, base::FilePath* snapshot_path, | 41 bool MapV8Files(base::FilePath* natives_path, base::FilePath* snapshot_path, |
| 42 int natives_fd = -1, int snapshot_fd = -1) { | 42 int natives_fd = -1, int snapshot_fd = -1) { |
| 43 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; | 43 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 44 | 44 |
| 45 g_mapped_natives = new base::MemoryMappedFile; | 45 g_mapped_natives = new base::MemoryMappedFile; |
| 46 if (!g_mapped_natives->IsValid()) { | 46 if (!g_mapped_natives->IsValid()) { |
| 47 if (natives_fd == -1 | 47 if (natives_fd == -1 |
| 48 ? !g_mapped_natives->Initialize(base::File(*natives_path, flags)) | 48 ? !g_mapped_natives->Initialize(base::File(*natives_path, flags)) |
| 49 : !g_mapped_natives->Initialize(base::File(natives_fd))) { | 49 : !g_mapped_natives->Initialize(base::File(natives_fd))) { |
| 50 delete g_mapped_natives; | 50 delete g_mapped_natives; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 73 } // namespace | 73 } // namespace |
| 74 | 74 |
| 75 | 75 |
| 76 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 76 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 77 // static | 77 // static |
| 78 bool IsolateHolder::LoadV8Snapshot() { | 78 bool IsolateHolder::LoadV8Snapshot() { |
| 79 if (g_mapped_natives && g_mapped_snapshot) | 79 if (g_mapped_natives && g_mapped_snapshot) |
| 80 return true; | 80 return true; |
| 81 | 81 |
| 82 base::FilePath data_path; | 82 base::FilePath data_path; |
| 83 PathService::Get(base::DIR_ANDROID_APP_DATA, &data_path); | 83 PathService::Get( |
| 84 #if defined(OS_ANDROID) |
| 85 base::DIR_ANDROID_APP_DATA, |
| 86 #elif defined(OS_POSIX) |
| 87 base::DIR_EXE, |
| 88 #endif |
| 89 &data_path); |
| 84 DCHECK(!data_path.empty()); | 90 DCHECK(!data_path.empty()); |
| 85 | 91 |
| 86 base::FilePath natives_path = data_path.AppendASCII("natives_blob.bin"); | 92 base::FilePath natives_path = data_path.AppendASCII("natives_blob.bin"); |
| 87 base::FilePath snapshot_path = data_path.AppendASCII("snapshot_blob.bin"); | 93 base::FilePath snapshot_path = data_path.AppendASCII("snapshot_blob.bin"); |
| 88 | 94 |
| 89 return MapV8Files(&natives_path, &snapshot_path); | 95 return MapV8Files(&natives_path, &snapshot_path); |
| 90 } | 96 } |
| 91 | 97 |
| 92 //static | 98 //static |
| 93 bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) { | 99 bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) { |
| 94 if (g_mapped_natives && g_mapped_snapshot) | 100 if (g_mapped_natives && g_mapped_snapshot) |
| 95 return true; | 101 return true; |
| 96 | 102 |
| 97 return MapV8Files(NULL, NULL, natives_fd, snapshot_fd); | 103 return MapV8Files(NULL, NULL, natives_fd, snapshot_fd); |
| 98 } | 104 } |
| 99 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 105 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 100 | 106 |
| 107 //static |
| 108 void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out, |
| 109 int* natives_size_out, |
| 110 const char** snapshot_data_out, |
| 111 int* snapshot_size_out) { |
| 112 if (!g_mapped_natives || !g_mapped_snapshot) { |
| 113 *natives_data_out = *snapshot_data_out = NULL; |
| 114 *natives_size_out = *snapshot_size_out = 0; |
| 115 return; |
| 116 } |
| 117 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data()); |
| 118 *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| 119 *natives_size_out = static_cast<int>(g_mapped_natives->length()); |
| 120 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); |
| 121 } |
| 122 |
| 101 IsolateHolder::IsolateHolder() { | 123 IsolateHolder::IsolateHolder() { |
| 102 CHECK(g_array_buffer_allocator) | 124 CHECK(g_array_buffer_allocator) |
| 103 << "You need to invoke gin::IsolateHolder::Initialize first"; | 125 << "You need to invoke gin::IsolateHolder::Initialize first"; |
| 104 v8::Isolate::CreateParams params; | 126 v8::Isolate::CreateParams params; |
| 105 params.entry_hook = DebugImpl::GetFunctionEntryHook(); | 127 params.entry_hook = DebugImpl::GetFunctionEntryHook(); |
| 106 params.code_event_handler = DebugImpl::GetJitCodeEventHandler(); | 128 params.code_event_handler = DebugImpl::GetJitCodeEventHandler(); |
| 107 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), | 129 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), |
| 108 base::SysInfo::AmountOfVirtualMemory(), | 130 base::SysInfo::AmountOfVirtualMemory(), |
| 109 base::SysInfo::NumberOfProcessors()); | 131 base::SysInfo::NumberOfProcessors()); |
| 110 isolate_ = v8::Isolate::New(params); | 132 isolate_ = v8::Isolate::New(params); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 v8::V8::SetArrayBufferAllocator(allocator); | 173 v8::V8::SetArrayBufferAllocator(allocator); |
| 152 g_array_buffer_allocator = allocator; | 174 g_array_buffer_allocator = allocator; |
| 153 if (mode == gin::IsolateHolder::kStrictMode) { | 175 if (mode == gin::IsolateHolder::kStrictMode) { |
| 154 static const char v8_flags[] = "--use_strict"; | 176 static const char v8_flags[] = "--use_strict"; |
| 155 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); | 177 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); |
| 156 } | 178 } |
| 157 v8::V8::SetEntropySource(&GenerateEntropy); | 179 v8::V8::SetEntropySource(&GenerateEntropy); |
| 158 #ifdef V8_USE_EXTERNAL_STARTUP_DATA | 180 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 159 v8::StartupData natives; | 181 v8::StartupData natives; |
| 160 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); | 182 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); |
| 161 natives.raw_size = g_mapped_natives->length(); | 183 natives.raw_size = static_cast<int>(g_mapped_natives->length()); |
| 162 natives.compressed_size = g_mapped_natives->length(); | 184 natives.compressed_size = static_cast<int>(g_mapped_natives->length()); |
| 163 v8::V8::SetNativesDataBlob(&natives); | 185 v8::V8::SetNativesDataBlob(&natives); |
| 164 | 186 |
| 165 v8::StartupData snapshot; | 187 v8::StartupData snapshot; |
| 166 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); | 188 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| 167 snapshot.raw_size = g_mapped_snapshot->length(); | 189 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); |
| 168 snapshot.compressed_size = g_mapped_snapshot->length(); | 190 snapshot.compressed_size = static_cast<int>(g_mapped_snapshot->length()); |
| 169 v8::V8::SetSnapshotDataBlob(&snapshot); | 191 v8::V8::SetSnapshotDataBlob(&snapshot); |
| 170 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 192 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 171 v8::V8::Initialize(); | 193 v8::V8::Initialize(); |
| 172 v8_is_initialized = true; | 194 v8_is_initialized = true; |
| 173 } | 195 } |
| 174 | 196 |
| 175 void IsolateHolder::AddRunMicrotasksObserver() { | 197 void IsolateHolder::AddRunMicrotasksObserver() { |
| 176 DCHECK(!task_observer_.get()); | 198 DCHECK(!task_observer_.get()); |
| 177 task_observer_.reset(new RunMicrotasksObserver(isolate_));; | 199 task_observer_.reset(new RunMicrotasksObserver(isolate_));; |
| 178 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); | 200 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); |
| 179 } | 201 } |
| 180 | 202 |
| 181 void IsolateHolder::RemoveRunMicrotasksObserver() { | 203 void IsolateHolder::RemoveRunMicrotasksObserver() { |
| 182 DCHECK(task_observer_.get()); | 204 DCHECK(task_observer_.get()); |
| 183 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); | 205 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); |
| 184 task_observer_.reset(); | 206 task_observer_.reset(); |
| 185 } | 207 } |
| 186 | 208 |
| 187 } // namespace gin | 209 } // namespace gin |
| OLD | NEW |