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/logging.h" | 10 #include "base/logging.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
13 #include "base/sys_info.h" | 13 #include "base/sys_info.h" |
14 #include "gin/array_buffer.h" | 14 #include "gin/array_buffer.h" |
15 #include "gin/debug_impl.h" | 15 #include "gin/debug_impl.h" |
16 #include "gin/function_template.h" | 16 #include "gin/function_template.h" |
17 #include "gin/per_isolate_data.h" | 17 #include "gin/per_isolate_data.h" |
18 #include "gin/public/v8_platform.h" | 18 #include "gin/public/v8_platform.h" |
19 #include "gin/run_microtasks_observer.h" | 19 #include "gin/run_microtasks_observer.h" |
20 | 20 |
| 21 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 22 #include "base/path_service.h" |
| 23 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 24 |
| 25 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 26 |
| 27 #ifdef OS_MACOSX |
| 28 #include "base/mac/bundle_locations.h" |
| 29 #endif |
| 30 |
| 31 #include "base/path_service.h" |
| 32 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 33 |
21 namespace gin { | 34 namespace gin { |
22 | 35 |
23 namespace { | 36 namespace { |
24 | 37 |
25 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; | 38 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; |
26 | 39 |
27 bool GenerateEntropy(unsigned char* buffer, size_t amount) { | 40 bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
28 base::RandBytes(buffer, amount); | 41 base::RandBytes(buffer, amount); |
29 return true; | 42 return true; |
30 } | 43 } |
31 | 44 |
| 45 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 46 base::MemoryMappedFile* g_mapped_natives = NULL; |
| 47 base::MemoryMappedFile* g_mapped_snapshot = NULL; |
| 48 #endif |
| 49 |
32 } // namespace | 50 } // namespace |
33 | 51 |
| 52 |
| 53 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 54 //static |
| 55 bool IsolateHolder::MapV8Files( |
| 56 base::FilePath* natives_path, base::FilePath* snapshot_path, |
| 57 int natives_fd, int snapshot_fd) { |
| 58 |
| 59 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 60 |
| 61 g_mapped_natives = new base::MemoryMappedFile; |
| 62 if (!g_mapped_natives->IsValid()) { |
| 63 #ifdef OS_ANDROID |
| 64 if (natives_fd == -1 |
| 65 ? !g_mapped_natives->Initialize(base::File(*natives_path, flags)) |
| 66 : !g_mapped_natives->Initialize(base::File(natives_fd)) |
| 67 #else |
| 68 if (!g_mapped_natives->Initialize(base::File(*natives_path, flags)) |
| 69 #endif |
| 70 ) { |
| 71 delete g_mapped_natives; |
| 72 g_mapped_natives = NULL; |
| 73 LOG(ERROR) << "Couldn't mmap v8 natives data file"; |
| 74 return false; |
| 75 } |
| 76 } |
| 77 |
| 78 g_mapped_snapshot = new base::MemoryMappedFile; |
| 79 if (!g_mapped_snapshot->IsValid()) { |
| 80 #ifdef OS_ANDROID |
| 81 if (snapshot_fd == -1 |
| 82 ? !g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags)) |
| 83 : !g_mapped_snapshot->Initialize(base::File(snapshot_fd)) |
| 84 #else |
| 85 if (!g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags)) |
| 86 #endif |
| 87 ) { |
| 88 delete g_mapped_snapshot; |
| 89 g_mapped_snapshot = NULL; |
| 90 LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; |
| 91 return false; |
| 92 } |
| 93 } |
| 94 |
| 95 return true; |
| 96 } |
| 97 |
| 98 // static |
| 99 bool IsolateHolder::LoadV8Snapshot() { |
| 100 if (g_mapped_natives && g_mapped_snapshot) |
| 101 return true; |
| 102 |
| 103 #if !defined(OS_MACOSX) |
| 104 base::FilePath data_path; |
| 105 PathService::Get( |
| 106 #if defined(OS_ANDROID) |
| 107 base::DIR_ANDROID_APP_DATA, |
| 108 #elif defined(OS_POSIX) || defined(OS_WIN) |
| 109 base::DIR_EXE, |
| 110 #endif |
| 111 &data_path); |
| 112 #else |
| 113 base::FilePath data_path = base::mac::FrameworkBundlePath(); |
| 114 #endif |
| 115 DCHECK(!data_path.empty()); |
| 116 |
| 117 base::FilePath natives_path = data_path.AppendASCII("natives_blob.bin"); |
| 118 base::FilePath snapshot_path = data_path.AppendASCII("snapshot_blob.bin"); |
| 119 |
| 120 return IsolateHolder::MapV8Files(&natives_path, &snapshot_path); |
| 121 } |
| 122 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 123 |
| 124 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) && defined(OS_ANDROID) |
| 125 //static |
| 126 bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) { |
| 127 if (g_mapped_natives && g_mapped_snapshot) |
| 128 return true; |
| 129 |
| 130 return IsolateHolder::MapV8Files(NULL, NULL, natives_fd, snapshot_fd); |
| 131 } |
| 132 #endif // V8_USE_EXTERNAL_STARTUP_DATA && OS_ANDROID |
| 133 |
34 IsolateHolder::IsolateHolder() { | 134 IsolateHolder::IsolateHolder() { |
35 CHECK(g_array_buffer_allocator) | 135 CHECK(g_array_buffer_allocator) |
36 << "You need to invoke gin::IsolateHolder::Initialize first"; | 136 << "You need to invoke gin::IsolateHolder::Initialize first"; |
37 v8::Isolate::CreateParams params; | 137 v8::Isolate::CreateParams params; |
38 params.entry_hook = DebugImpl::GetFunctionEntryHook(); | 138 params.entry_hook = DebugImpl::GetFunctionEntryHook(); |
39 params.code_event_handler = DebugImpl::GetJitCodeEventHandler(); | 139 params.code_event_handler = DebugImpl::GetJitCodeEventHandler(); |
40 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), | 140 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), |
41 base::SysInfo::AmountOfVirtualMemory(), | 141 base::SysInfo::AmountOfVirtualMemory(), |
42 base::SysInfo::NumberOfProcessors()); | 142 base::SysInfo::NumberOfProcessors()); |
43 isolate_ = v8::Isolate::New(params); | 143 isolate_ = v8::Isolate::New(params); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 if (v8_is_initialized) | 181 if (v8_is_initialized) |
82 return; | 182 return; |
83 v8::V8::InitializePlatform(V8Platform::Get()); | 183 v8::V8::InitializePlatform(V8Platform::Get()); |
84 v8::V8::SetArrayBufferAllocator(allocator); | 184 v8::V8::SetArrayBufferAllocator(allocator); |
85 g_array_buffer_allocator = allocator; | 185 g_array_buffer_allocator = allocator; |
86 if (mode == gin::IsolateHolder::kStrictMode) { | 186 if (mode == gin::IsolateHolder::kStrictMode) { |
87 static const char v8_flags[] = "--use_strict"; | 187 static const char v8_flags[] = "--use_strict"; |
88 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); | 188 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); |
89 } | 189 } |
90 v8::V8::SetEntropySource(&GenerateEntropy); | 190 v8::V8::SetEntropySource(&GenerateEntropy); |
| 191 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 192 v8::StartupData natives; |
| 193 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); |
| 194 natives.raw_size = g_mapped_natives->length(); |
| 195 natives.compressed_size = g_mapped_natives->length(); |
| 196 v8::V8::SetNativesDataBlob(&natives); |
| 197 |
| 198 v8::StartupData snapshot; |
| 199 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| 200 snapshot.raw_size = g_mapped_snapshot->length(); |
| 201 snapshot.compressed_size = g_mapped_snapshot->length(); |
| 202 v8::V8::SetSnapshotDataBlob(&snapshot); |
| 203 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
91 v8::V8::Initialize(); | 204 v8::V8::Initialize(); |
92 v8_is_initialized = true; | 205 v8_is_initialized = true; |
93 } | 206 } |
94 | 207 |
95 void IsolateHolder::AddRunMicrotasksObserver() { | 208 void IsolateHolder::AddRunMicrotasksObserver() { |
96 DCHECK(!task_observer_.get()); | 209 DCHECK(!task_observer_.get()); |
97 task_observer_.reset(new RunMicrotasksObserver(isolate_));; | 210 task_observer_.reset(new RunMicrotasksObserver(isolate_));; |
98 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); | 211 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); |
99 } | 212 } |
100 | 213 |
101 void IsolateHolder::RemoveRunMicrotasksObserver() { | 214 void IsolateHolder::RemoveRunMicrotasksObserver() { |
102 DCHECK(task_observer_.get()); | 215 DCHECK(task_observer_.get()); |
103 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); | 216 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); |
104 task_observer_.reset(); | 217 task_observer_.reset(); |
105 } | 218 } |
106 | 219 |
107 } // namespace gin | 220 } // namespace gin |
OLD | NEW |