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