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 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 25 |
21 namespace gin { | 26 namespace gin { |
22 | 27 |
23 namespace { | 28 namespace { |
24 | 29 |
25 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; | 30 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL; |
26 | 31 |
27 bool GenerateEntropy(unsigned char* buffer, size_t amount) { | 32 bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
28 base::RandBytes(buffer, amount); | 33 base::RandBytes(buffer, amount); |
29 return true; | 34 return true; |
30 } | 35 } |
31 | 36 |
| 37 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 38 base::MemoryMappedFile* g_mapped_natives = NULL; |
| 39 base::MemoryMappedFile* g_mapped_snapshot = NULL; |
| 40 |
| 41 bool MapV8Files(base::FilePath* natives_path, base::FilePath* snapshot_path, |
| 42 int natives_fd = -1, int snapshot_fd = -1) { |
| 43 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 44 |
| 45 g_mapped_natives = new base::MemoryMappedFile; |
| 46 if (!g_mapped_natives->IsValid()) { |
| 47 if (natives_fd == -1 |
| 48 ? !g_mapped_natives->Initialize(base::File(*natives_path, flags)) |
| 49 : !g_mapped_natives->Initialize(base::File(natives_fd))) { |
| 50 delete g_mapped_natives; |
| 51 g_mapped_natives = NULL; |
| 52 LOG(FATAL) << "Couldn't mmap v8 natives data file"; |
| 53 return false; |
| 54 } |
| 55 } |
| 56 |
| 57 g_mapped_snapshot = new base::MemoryMappedFile; |
| 58 if (!g_mapped_snapshot->IsValid()) { |
| 59 if (snapshot_fd == -1 |
| 60 ? !g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags)) |
| 61 : !g_mapped_snapshot->Initialize(base::File(snapshot_fd))) { |
| 62 delete g_mapped_snapshot; |
| 63 g_mapped_snapshot = NULL; |
| 64 LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; |
| 65 return false; |
| 66 } |
| 67 } |
| 68 |
| 69 return true; |
| 70 } |
| 71 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 72 |
32 } // namespace | 73 } // namespace |
33 | 74 |
| 75 |
| 76 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 77 // static |
| 78 bool IsolateHolder::LoadV8Snapshot() { |
| 79 if (g_mapped_natives && g_mapped_snapshot) |
| 80 return true; |
| 81 |
| 82 base::FilePath data_path; |
| 83 PathService::Get(base::DIR_ANDROID_APP_DATA, &data_path); |
| 84 DCHECK(!data_path.empty()); |
| 85 |
| 86 base::FilePath natives_path = data_path.AppendASCII("natives_blob.bin"); |
| 87 base::FilePath snapshot_path = data_path.AppendASCII("snapshot_blob.bin"); |
| 88 |
| 89 return MapV8Files(&natives_path, &snapshot_path); |
| 90 } |
| 91 |
| 92 //static |
| 93 bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) { |
| 94 if (g_mapped_natives && g_mapped_snapshot) |
| 95 return true; |
| 96 |
| 97 return MapV8Files(NULL, NULL, natives_fd, snapshot_fd); |
| 98 } |
| 99 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 100 |
34 IsolateHolder::IsolateHolder() { | 101 IsolateHolder::IsolateHolder() { |
35 CHECK(g_array_buffer_allocator) | 102 CHECK(g_array_buffer_allocator) |
36 << "You need to invoke gin::IsolateHolder::Initialize first"; | 103 << "You need to invoke gin::IsolateHolder::Initialize first"; |
37 v8::Isolate::CreateParams params; | 104 v8::Isolate::CreateParams params; |
38 params.entry_hook = DebugImpl::GetFunctionEntryHook(); | 105 params.entry_hook = DebugImpl::GetFunctionEntryHook(); |
39 params.code_event_handler = DebugImpl::GetJitCodeEventHandler(); | 106 params.code_event_handler = DebugImpl::GetJitCodeEventHandler(); |
40 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), | 107 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(), |
41 base::SysInfo::AmountOfVirtualMemory(), | 108 base::SysInfo::AmountOfVirtualMemory(), |
42 base::SysInfo::NumberOfProcessors()); | 109 base::SysInfo::NumberOfProcessors()); |
43 isolate_ = v8::Isolate::New(params); | 110 isolate_ = v8::Isolate::New(params); |
(...skipping 37 matching lines...) Loading... |
81 if (v8_is_initialized) | 148 if (v8_is_initialized) |
82 return; | 149 return; |
83 v8::V8::InitializePlatform(V8Platform::Get()); | 150 v8::V8::InitializePlatform(V8Platform::Get()); |
84 v8::V8::SetArrayBufferAllocator(allocator); | 151 v8::V8::SetArrayBufferAllocator(allocator); |
85 g_array_buffer_allocator = allocator; | 152 g_array_buffer_allocator = allocator; |
86 if (mode == gin::IsolateHolder::kStrictMode) { | 153 if (mode == gin::IsolateHolder::kStrictMode) { |
87 static const char v8_flags[] = "--use_strict"; | 154 static const char v8_flags[] = "--use_strict"; |
88 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); | 155 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); |
89 } | 156 } |
90 v8::V8::SetEntropySource(&GenerateEntropy); | 157 v8::V8::SetEntropySource(&GenerateEntropy); |
| 158 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 159 v8::StartupData natives; |
| 160 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); |
| 161 natives.raw_size = g_mapped_natives->length(); |
| 162 natives.compressed_size = g_mapped_natives->length(); |
| 163 v8::V8::SetNativesDataBlob(&natives); |
| 164 |
| 165 v8::StartupData snapshot; |
| 166 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| 167 snapshot.raw_size = g_mapped_snapshot->length(); |
| 168 snapshot.compressed_size = g_mapped_snapshot->length(); |
| 169 v8::V8::SetSnapshotDataBlob(&snapshot); |
| 170 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
91 v8::V8::Initialize(); | 171 v8::V8::Initialize(); |
92 v8_is_initialized = true; | 172 v8_is_initialized = true; |
93 } | 173 } |
94 | 174 |
95 void IsolateHolder::AddRunMicrotasksObserver() { | 175 void IsolateHolder::AddRunMicrotasksObserver() { |
96 DCHECK(!task_observer_.get()); | 176 DCHECK(!task_observer_.get()); |
97 task_observer_.reset(new RunMicrotasksObserver(isolate_));; | 177 task_observer_.reset(new RunMicrotasksObserver(isolate_));; |
98 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); | 178 base::MessageLoop::current()->AddTaskObserver(task_observer_.get()); |
99 } | 179 } |
100 | 180 |
101 void IsolateHolder::RemoveRunMicrotasksObserver() { | 181 void IsolateHolder::RemoveRunMicrotasksObserver() { |
102 DCHECK(task_observer_.get()); | 182 DCHECK(task_observer_.get()); |
103 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); | 183 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get()); |
104 task_observer_.reset(); | 184 task_observer_.reset(); |
105 } | 185 } |
106 | 186 |
107 } // namespace gin | 187 } // namespace gin |
OLD | NEW |