Remove unused method (DataReductionProxySettings::PrimaryOrigin) from DataReductionPr...
[chromium-blink-merge.git] / gin / isolate_holder.cc
blob92fb3628deef9baec7710d654019d422777b19be
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/metrics/field_trial.h"
14 #include "base/rand_util.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "base/sys_info.h"
17 #include "crypto/sha2.h"
18 #include "gin/array_buffer.h"
19 #include "gin/debug_impl.h"
20 #include "gin/function_template.h"
21 #include "gin/per_isolate_data.h"
22 #include "gin/public/v8_platform.h"
23 #include "gin/run_microtasks_observer.h"
25 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
26 #if defined(OS_MACOSX)
27 #include "base/mac/foundation_util.h"
28 #endif // OS_MACOSX
29 #include "base/path_service.h"
30 #endif // V8_USE_EXTERNAL_STARTUP_DATA
32 namespace gin {
34 namespace {
36 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL;
38 bool GenerateEntropy(unsigned char* buffer, size_t amount) {
39 base::RandBytes(buffer, amount);
40 return true;
43 base::MemoryMappedFile* g_mapped_natives = NULL;
44 base::MemoryMappedFile* g_mapped_snapshot = NULL;
46 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
47 bool MapV8Files(base::FilePath* natives_path,
48 base::FilePath* snapshot_path,
49 int natives_fd = -1,
50 int snapshot_fd = -1,
51 base::MemoryMappedFile::Region natives_region =
52 base::MemoryMappedFile::Region::kWholeFile,
53 base::MemoryMappedFile::Region snapshot_region =
54 base::MemoryMappedFile::Region::kWholeFile) {
55 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
57 g_mapped_natives = new base::MemoryMappedFile;
58 if (!g_mapped_natives->IsValid()) {
59 #if !defined(OS_WIN)
60 if (natives_fd == -1
61 ? !g_mapped_natives->Initialize(base::File(*natives_path, flags),
62 natives_region)
63 : !g_mapped_natives->Initialize(base::File(natives_fd),
64 natives_region)) {
65 #else
66 if (!g_mapped_natives->Initialize(base::File(*natives_path, flags),
67 natives_region)) {
68 #endif // !OS_WIN
69 delete g_mapped_natives;
70 g_mapped_natives = NULL;
71 LOG(FATAL) << "Couldn't mmap v8 natives data file";
72 return false;
76 g_mapped_snapshot = new base::MemoryMappedFile;
77 if (!g_mapped_snapshot->IsValid()) {
78 #if !defined(OS_WIN)
79 if (snapshot_fd == -1
80 ? !g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags),
81 snapshot_region)
82 : !g_mapped_snapshot->Initialize(base::File(snapshot_fd),
83 snapshot_region)) {
84 #else
85 if (!g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags),
86 snapshot_region)) {
87 #endif // !OS_WIN
88 delete g_mapped_snapshot;
89 g_mapped_snapshot = NULL;
90 LOG(ERROR) << "Couldn't mmap v8 snapshot data file";
91 return false;
95 return true;
98 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
99 bool VerifyV8SnapshotFile(base::MemoryMappedFile* snapshot_file,
100 const unsigned char* fingerprint) {
101 unsigned char output[crypto::kSHA256Length];
102 crypto::SHA256HashString(
103 base::StringPiece(reinterpret_cast<const char*>(snapshot_file->data()),
104 snapshot_file->length()),
105 output, sizeof(output));
106 return !memcmp(fingerprint, output, sizeof(output));
108 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
109 #endif // V8_USE_EXTERNAL_STARTUP_DATA
111 } // namespace
113 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
115 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
116 // Defined in gen/gin/v8_snapshot_fingerprint.cc
117 extern const unsigned char g_natives_fingerprint[];
118 extern const unsigned char g_snapshot_fingerprint[];
119 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
121 #if !defined(OS_MACOSX)
122 const int IsolateHolder::kV8SnapshotBasePathKey =
123 #if defined(OS_ANDROID)
124 base::DIR_ANDROID_APP_DATA;
125 #elif defined(OS_POSIX)
126 base::DIR_EXE;
127 #elif defined(OS_WIN)
128 base::DIR_MODULE;
129 #endif // OS_ANDROID
130 #endif // !OS_MACOSX
132 const char IsolateHolder::kNativesFileName[] = "natives_blob.bin";
133 const char IsolateHolder::kSnapshotFileName[] = "snapshot_blob.bin";
135 // static
136 bool IsolateHolder::LoadV8Snapshot() {
137 if (g_mapped_natives && g_mapped_snapshot)
138 return true;
140 #if !defined(OS_MACOSX)
141 base::FilePath data_path;
142 PathService::Get(kV8SnapshotBasePathKey, &data_path);
143 DCHECK(!data_path.empty());
145 base::FilePath natives_path = data_path.AppendASCII(kNativesFileName);
146 base::FilePath snapshot_path = data_path.AppendASCII(kSnapshotFileName);
147 #else // !defined(OS_MACOSX)
148 base::ScopedCFTypeRef<CFStringRef> natives_file_name(
149 base::SysUTF8ToCFStringRef(kNativesFileName));
150 base::FilePath natives_path = base::mac::PathForFrameworkBundleResource(
151 natives_file_name);
152 base::ScopedCFTypeRef<CFStringRef> snapshot_file_name(
153 base::SysUTF8ToCFStringRef(kSnapshotFileName));
154 base::FilePath snapshot_path = base::mac::PathForFrameworkBundleResource(
155 snapshot_file_name);
156 DCHECK(!natives_path.empty());
157 DCHECK(!snapshot_path.empty());
158 #endif // !defined(OS_MACOSX)
160 if (!MapV8Files(&natives_path, &snapshot_path))
161 return false;
163 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
164 return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) &&
165 VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint);
166 #else
167 return true;
168 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
171 //static
172 bool IsolateHolder::LoadV8SnapshotFd(int natives_fd,
173 int64 natives_offset,
174 int64 natives_size,
175 int snapshot_fd,
176 int64 snapshot_offset,
177 int64 snapshot_size) {
178 if (g_mapped_natives && g_mapped_snapshot)
179 return true;
181 base::MemoryMappedFile::Region natives_region =
182 base::MemoryMappedFile::Region::kWholeFile;
183 if (natives_size != 0 || natives_offset != 0) {
184 natives_region =
185 base::MemoryMappedFile::Region(natives_offset, natives_size);
188 base::MemoryMappedFile::Region snapshot_region =
189 base::MemoryMappedFile::Region::kWholeFile;
190 if (natives_size != 0 || natives_offset != 0) {
191 snapshot_region =
192 base::MemoryMappedFile::Region(snapshot_offset, snapshot_size);
195 return MapV8Files(
196 NULL, NULL, natives_fd, snapshot_fd, natives_region, snapshot_region);
198 #endif // V8_USE_EXTERNAL_STARTUP_DATA
200 //static
201 void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out,
202 int* natives_size_out,
203 const char** snapshot_data_out,
204 int* snapshot_size_out) {
205 if (!g_mapped_natives || !g_mapped_snapshot) {
206 *natives_data_out = *snapshot_data_out = NULL;
207 *natives_size_out = *snapshot_size_out = 0;
208 return;
210 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
211 *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data());
212 *natives_size_out = static_cast<int>(g_mapped_natives->length());
213 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
216 IsolateHolder::IsolateHolder() {
217 CHECK(g_array_buffer_allocator)
218 << "You need to invoke gin::IsolateHolder::Initialize first";
219 v8::Isolate::CreateParams params;
220 params.entry_hook = DebugImpl::GetFunctionEntryHook();
221 params.code_event_handler = DebugImpl::GetJitCodeEventHandler();
222 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(),
223 base::SysInfo::AmountOfVirtualMemory(),
224 base::SysInfo::NumberOfProcessors());
225 isolate_ = v8::Isolate::New(params);
226 isolate_data_.reset(new PerIsolateData(isolate_, g_array_buffer_allocator));
227 #if defined(OS_WIN)
229 void* code_range;
230 size_t size;
231 isolate_->GetCodeRange(&code_range, &size);
232 Debug::CodeRangeCreatedCallback callback =
233 DebugImpl::GetCodeRangeCreatedCallback();
234 if (code_range && size && callback)
235 callback(code_range, size);
237 #endif
240 IsolateHolder::~IsolateHolder() {
241 if (task_observer_.get())
242 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get());
243 #if defined(OS_WIN)
245 void* code_range;
246 size_t size;
247 isolate_->GetCodeRange(&code_range, &size);
248 Debug::CodeRangeDeletedCallback callback =
249 DebugImpl::GetCodeRangeDeletedCallback();
250 if (code_range && callback)
251 callback(code_range);
253 #endif
254 isolate_data_.reset();
255 isolate_->Dispose();
256 isolate_ = NULL;
259 // static
260 void IsolateHolder::Initialize(ScriptMode mode,
261 v8::ArrayBuffer::Allocator* allocator) {
262 CHECK(allocator);
263 static bool v8_is_initialized = false;
264 if (v8_is_initialized)
265 return;
266 v8::V8::InitializePlatform(V8Platform::Get());
267 v8::V8::SetArrayBufferAllocator(allocator);
268 g_array_buffer_allocator = allocator;
269 if (mode == gin::IsolateHolder::kStrictMode) {
270 static const char use_strict[] = "--use_strict";
271 v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1);
273 if (base::FieldTrialList::FindFullName("V8VerifyHeap") == "Enabled") {
274 static const char verify_heap[] = "--verify_heap";
275 v8::V8::SetFlagsFromString(verify_heap, sizeof(verify_heap) - 1);
277 v8::V8::SetEntropySource(&GenerateEntropy);
278 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
279 v8::StartupData natives;
280 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
281 natives.raw_size = static_cast<int>(g_mapped_natives->length());
282 v8::V8::SetNativesDataBlob(&natives);
284 v8::StartupData snapshot;
285 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
286 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
287 v8::V8::SetSnapshotDataBlob(&snapshot);
288 #endif // V8_USE_EXTERNAL_STARTUP_DATA
289 v8::V8::Initialize();
290 v8_is_initialized = true;
293 void IsolateHolder::AddRunMicrotasksObserver() {
294 DCHECK(!task_observer_.get());
295 task_observer_.reset(new RunMicrotasksObserver(isolate_));;
296 base::MessageLoop::current()->AddTaskObserver(task_observer_.get());
299 void IsolateHolder::RemoveRunMicrotasksObserver() {
300 DCHECK(task_observer_.get());
301 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get());
302 task_observer_.reset();
305 } // namespace gin