ServiceWorker: Consolidate version manipulation functions in SWProviderContext
[chromium-blink-merge.git] / gin / isolate_holder.cc
blobc666e29950a0a652a4085880fb716b86a07e4f75
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/strings/sys_string_conversions.h"
15 #include "base/sys_info.h"
16 #include "crypto/sha2.h"
17 #include "gin/array_buffer.h"
18 #include "gin/debug_impl.h"
19 #include "gin/function_template.h"
20 #include "gin/per_isolate_data.h"
21 #include "gin/public/v8_platform.h"
22 #include "gin/run_microtasks_observer.h"
24 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
25 #if defined(OS_MACOSX)
26 #include "base/mac/foundation_util.h"
27 #endif // OS_MACOSX
28 #include "base/path_service.h"
29 #endif // V8_USE_EXTERNAL_STARTUP_DATA
31 namespace gin {
33 namespace {
35 v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL;
37 bool GenerateEntropy(unsigned char* buffer, size_t amount) {
38 base::RandBytes(buffer, amount);
39 return true;
42 base::MemoryMappedFile* g_mapped_natives = NULL;
43 base::MemoryMappedFile* g_mapped_snapshot = NULL;
45 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
46 bool MapV8Files(base::FilePath* natives_path,
47 base::FilePath* snapshot_path,
48 int natives_fd = -1,
49 int snapshot_fd = -1,
50 base::MemoryMappedFile::Region natives_region =
51 base::MemoryMappedFile::Region::kWholeFile,
52 base::MemoryMappedFile::Region snapshot_region =
53 base::MemoryMappedFile::Region::kWholeFile) {
54 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
56 g_mapped_natives = new base::MemoryMappedFile;
57 if (!g_mapped_natives->IsValid()) {
58 #if !defined(OS_WIN)
59 if (natives_fd == -1
60 ? !g_mapped_natives->Initialize(base::File(*natives_path, flags),
61 natives_region)
62 : !g_mapped_natives->Initialize(base::File(natives_fd),
63 natives_region)) {
64 #else
65 if (!g_mapped_natives->Initialize(base::File(*natives_path, flags),
66 natives_region)) {
67 #endif // !OS_WIN
68 delete g_mapped_natives;
69 g_mapped_natives = NULL;
70 LOG(FATAL) << "Couldn't mmap v8 natives data file";
71 return false;
75 g_mapped_snapshot = new base::MemoryMappedFile;
76 if (!g_mapped_snapshot->IsValid()) {
77 #if !defined(OS_WIN)
78 if (snapshot_fd == -1
79 ? !g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags),
80 snapshot_region)
81 : !g_mapped_snapshot->Initialize(base::File(snapshot_fd),
82 snapshot_region)) {
83 #else
84 if (!g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags),
85 snapshot_region)) {
86 #endif // !OS_WIN
87 delete g_mapped_snapshot;
88 g_mapped_snapshot = NULL;
89 LOG(ERROR) << "Couldn't mmap v8 snapshot data file";
90 return false;
94 return true;
97 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
98 bool VerifyV8SnapshotFile(base::MemoryMappedFile* snapshot_file,
99 const unsigned char* fingerprint) {
100 unsigned char output[crypto::kSHA256Length];
101 crypto::SHA256HashString(
102 base::StringPiece(reinterpret_cast<const char*>(snapshot_file->data()),
103 snapshot_file->length()),
104 output, sizeof(output));
105 return !memcmp(fingerprint, output, sizeof(output));
107 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
109 #if !defined(OS_MACOSX)
110 const int v8_snapshot_dir =
111 #if defined(OS_ANDROID)
112 base::DIR_ANDROID_APP_DATA;
113 #elif defined(OS_POSIX)
114 base::DIR_EXE;
115 #elif defined(OS_WIN)
116 base::DIR_MODULE;
117 #endif // OS_ANDROID
118 #endif // !OS_MACOSX
120 #endif // V8_USE_EXTERNAL_STARTUP_DATA
122 } // namespace
124 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
126 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
127 // Defined in gen/gin/v8_snapshot_fingerprint.cc
128 extern const unsigned char g_natives_fingerprint[];
129 extern const unsigned char g_snapshot_fingerprint[];
130 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
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(v8_snapshot_dir, &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();
258 // static
259 void IsolateHolder::Initialize(ScriptMode mode,
260 v8::ArrayBuffer::Allocator* allocator) {
261 CHECK(allocator);
262 static bool v8_is_initialized = false;
263 if (v8_is_initialized)
264 return;
265 v8::V8::InitializePlatform(V8Platform::Get());
266 v8::V8::SetArrayBufferAllocator(allocator);
267 g_array_buffer_allocator = allocator;
268 if (mode == gin::IsolateHolder::kStrictMode) {
269 static const char v8_flags[] = "--use_strict";
270 v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1);
272 v8::V8::SetEntropySource(&GenerateEntropy);
273 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
274 v8::StartupData natives;
275 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
276 natives.raw_size = static_cast<int>(g_mapped_natives->length());
277 v8::V8::SetNativesDataBlob(&natives);
279 v8::StartupData snapshot;
280 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
281 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
282 v8::V8::SetSnapshotDataBlob(&snapshot);
283 #endif // V8_USE_EXTERNAL_STARTUP_DATA
284 v8::V8::Initialize();
285 v8_is_initialized = true;
288 void IsolateHolder::AddRunMicrotasksObserver() {
289 DCHECK(!task_observer_.get());
290 task_observer_.reset(new RunMicrotasksObserver(isolate_));;
291 base::MessageLoop::current()->AddTaskObserver(task_observer_.get());
294 void IsolateHolder::RemoveRunMicrotasksObserver() {
295 DCHECK(task_observer_.get());
296 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get());
297 task_observer_.reset();
300 } // namespace gin