Revert of Call to CrosSettings::Set() is replaced by OwnerSettingsService::Set()...
[chromium-blink-merge.git] / gin / isolate_holder.cc
blob2b6d64b35c042b2e183e6cc0c963c2f2ab96d392
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
3 // found in the LICENSE file.
5 #include "gin/public/isolate_holder.h"
7 #include <stdlib.h>
8 #include <string.h>
10 #include "base/files/memory_mapped_file.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/rand_util.h"
14 #include "base/sys_info.h"
15 #include "gin/array_buffer.h"
16 #include "gin/debug_impl.h"
17 #include "gin/function_template.h"
18 #include "gin/per_isolate_data.h"
19 #include "gin/public/v8_platform.h"
20 #include "gin/run_microtasks_observer.h"
22 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
23 #ifdef OS_MACOSX
24 #include "base/mac/foundation_util.h"
25 #endif // OS_MACOSX
26 #include "base/path_service.h"
27 #endif // V8_USE_EXTERNAL_STARTUP_DATA
29 namespace gin {
31 namespace {
33 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL;
35 bool GenerateEntropy(unsigned char* buffer, size_t amount) {
36 base::RandBytes(buffer, amount);
37 return true;
40 base::MemoryMappedFile* g_mapped_natives = NULL;
41 base::MemoryMappedFile* g_mapped_snapshot = NULL;
43 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
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;
48 g_mapped_natives = new base::MemoryMappedFile;
49 if (!g_mapped_natives->IsValid()) {
50 if (natives_fd == -1
51 ? !g_mapped_natives->Initialize(base::File(*natives_path, flags))
52 : !g_mapped_natives->Initialize(base::File(natives_fd))) {
53 delete g_mapped_natives;
54 g_mapped_natives = NULL;
55 LOG(FATAL) << "Couldn't mmap v8 natives data file";
56 return false;
60 g_mapped_snapshot = new base::MemoryMappedFile;
61 if (!g_mapped_snapshot->IsValid()) {
62 if (snapshot_fd == -1
63 ? !g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags))
64 : !g_mapped_snapshot->Initialize(base::File(snapshot_fd))) {
65 delete g_mapped_snapshot;
66 g_mapped_snapshot = NULL;
67 LOG(ERROR) << "Couldn't mmap v8 snapshot data file";
68 return false;
72 return true;
75 #if !defined(OS_MACOSX)
76 const int v8_snapshot_dir =
77 #if defined(OS_ANDROID)
78 base::DIR_ANDROID_APP_DATA;
79 #elif defined(OS_POSIX)
80 base::DIR_EXE;
81 #endif // defined(OS_ANDROID)
82 #endif // !defined(OS_MACOSX)
84 #endif // V8_USE_EXTERNAL_STARTUP_DATA
86 } // namespace
89 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
90 // static
91 bool IsolateHolder::LoadV8Snapshot() {
92 if (g_mapped_natives && g_mapped_snapshot)
93 return true;
95 #if !defined(OS_MACOSX)
96 base::FilePath data_path;
97 PathService::Get(v8_snapshot_dir, &data_path);
98 DCHECK(!data_path.empty());
100 base::FilePath natives_path = data_path.AppendASCII("natives_blob.bin");
101 base::FilePath snapshot_path = data_path.AppendASCII("snapshot_blob.bin");
102 #else // !defined(OS_MACOSX)
103 base::FilePath natives_path = base::mac::PathForFrameworkBundleResource(
104 CFSTR("natives_blob.bin"));
105 base::FilePath snapshot_path = base::mac::PathForFrameworkBundleResource(
106 CFSTR("snapshot_blob.bin"));
107 DCHECK(!natives_path.empty());
108 DCHECK(!snapshot_path.empty());
109 #endif // !defined(OS_MACOSX)
111 return MapV8Files(&natives_path, &snapshot_path);
114 //static
115 bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) {
116 if (g_mapped_natives && g_mapped_snapshot)
117 return true;
119 return MapV8Files(NULL, NULL, natives_fd, snapshot_fd);
121 #endif // V8_USE_EXTERNAL_STARTUP_DATA
123 //static
124 void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out,
125 int* natives_size_out,
126 const char** snapshot_data_out,
127 int* snapshot_size_out) {
128 if (!g_mapped_natives || !g_mapped_snapshot) {
129 *natives_data_out = *snapshot_data_out = NULL;
130 *natives_size_out = *snapshot_size_out = 0;
131 return;
133 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
134 *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data());
135 *natives_size_out = static_cast<int>(g_mapped_natives->length());
136 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
139 IsolateHolder::IsolateHolder() {
140 CHECK(g_array_buffer_allocator)
141 << "You need to invoke gin::IsolateHolder::Initialize first";
142 v8::Isolate::CreateParams params;
143 params.entry_hook = DebugImpl::GetFunctionEntryHook();
144 params.code_event_handler = DebugImpl::GetJitCodeEventHandler();
145 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(),
146 base::SysInfo::AmountOfVirtualMemory(),
147 base::SysInfo::NumberOfProcessors());
148 isolate_ = v8::Isolate::New(params);
149 isolate_data_.reset(new PerIsolateData(isolate_, g_array_buffer_allocator));
150 #if defined(OS_WIN)
152 void* code_range;
153 size_t size;
154 isolate_->GetCodeRange(&code_range, &size);
155 Debug::CodeRangeCreatedCallback callback =
156 DebugImpl::GetCodeRangeCreatedCallback();
157 if (code_range && size && callback)
158 callback(code_range, size);
160 #endif
163 IsolateHolder::~IsolateHolder() {
164 if (task_observer_.get())
165 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get());
166 #if defined(OS_WIN)
168 void* code_range;
169 size_t size;
170 isolate_->GetCodeRange(&code_range, &size);
171 Debug::CodeRangeDeletedCallback callback =
172 DebugImpl::GetCodeRangeDeletedCallback();
173 if (code_range && callback)
174 callback(code_range);
176 #endif
177 isolate_data_.reset();
178 isolate_->Dispose();
181 // static
182 void IsolateHolder::Initialize(ScriptMode mode,
183 v8::ArrayBuffer::Allocator* allocator) {
184 CHECK(allocator);
185 static bool v8_is_initialized = false;
186 if (v8_is_initialized)
187 return;
188 v8::V8::InitializePlatform(V8Platform::Get());
189 v8::V8::SetArrayBufferAllocator(allocator);
190 g_array_buffer_allocator = allocator;
191 if (mode == gin::IsolateHolder::kStrictMode) {
192 static const char v8_flags[] = "--use_strict";
193 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1);
195 v8::V8::SetEntropySource(&GenerateEntropy);
196 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
197 v8::StartupData natives;
198 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
199 natives.raw_size = static_cast<int>(g_mapped_natives->length());
200 v8::V8::SetNativesDataBlob(&natives);
202 v8::StartupData snapshot;
203 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
204 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
205 v8::V8::SetSnapshotDataBlob(&snapshot);
206 #endif // V8_USE_EXTERNAL_STARTUP_DATA
207 v8::V8::Initialize();
208 v8_is_initialized = true;
211 void IsolateHolder::AddRunMicrotasksObserver() {
212 DCHECK(!task_observer_.get());
213 task_observer_.reset(new RunMicrotasksObserver(isolate_));;
214 base::MessageLoop::current()->AddTaskObserver(task_observer_.get());
217 void IsolateHolder::RemoveRunMicrotasksObserver() {
218 DCHECK(task_observer_.get());
219 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get());
220 task_observer_.reset();
223 } // namespace gin