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