Cleanup ChildFrameCompositingHelper on destruction.
[chromium-blink-merge.git] / gin / isolate_holder.cc
blob21598a878ea465da1cc2985e728c0b93e42bf63e
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
110 #if !defined(OS_MACOSX)
111 const int v8_snapshot_dir =
112 #if defined(OS_ANDROID)
113 base::DIR_ANDROID_APP_DATA;
114 #elif defined(OS_POSIX)
115 base::DIR_EXE;
116 #elif defined(OS_WIN)
117 base::DIR_MODULE;
118 #endif // OS_ANDROID
119 #endif // !OS_MACOSX
121 #endif // V8_USE_EXTERNAL_STARTUP_DATA
123 } // namespace
125 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
127 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
128 // Defined in gen/gin/v8_snapshot_fingerprint.cc
129 extern const unsigned char g_natives_fingerprint[];
130 extern const unsigned char g_snapshot_fingerprint[];
131 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
133 const char IsolateHolder::kNativesFileName[] = "natives_blob.bin";
134 const char IsolateHolder::kSnapshotFileName[] = "snapshot_blob.bin";
136 // static
137 bool IsolateHolder::LoadV8Snapshot() {
138 if (g_mapped_natives && g_mapped_snapshot)
139 return true;
141 #if !defined(OS_MACOSX)
142 base::FilePath data_path;
143 PathService::Get(v8_snapshot_dir, &data_path);
144 DCHECK(!data_path.empty());
146 base::FilePath natives_path = data_path.AppendASCII(kNativesFileName);
147 base::FilePath snapshot_path = data_path.AppendASCII(kSnapshotFileName);
148 #else // !defined(OS_MACOSX)
149 base::ScopedCFTypeRef<CFStringRef> natives_file_name(
150 base::SysUTF8ToCFStringRef(kNativesFileName));
151 base::FilePath natives_path = base::mac::PathForFrameworkBundleResource(
152 natives_file_name);
153 base::ScopedCFTypeRef<CFStringRef> snapshot_file_name(
154 base::SysUTF8ToCFStringRef(kSnapshotFileName));
155 base::FilePath snapshot_path = base::mac::PathForFrameworkBundleResource(
156 snapshot_file_name);
157 DCHECK(!natives_path.empty());
158 DCHECK(!snapshot_path.empty());
159 #endif // !defined(OS_MACOSX)
161 if (!MapV8Files(&natives_path, &snapshot_path))
162 return false;
164 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
165 return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) &&
166 VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint);
167 #else
168 return true;
169 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
172 //static
173 bool IsolateHolder::LoadV8SnapshotFd(int natives_fd,
174 int64 natives_offset,
175 int64 natives_size,
176 int snapshot_fd,
177 int64 snapshot_offset,
178 int64 snapshot_size) {
179 if (g_mapped_natives && g_mapped_snapshot)
180 return true;
182 base::MemoryMappedFile::Region natives_region =
183 base::MemoryMappedFile::Region::kWholeFile;
184 if (natives_size != 0 || natives_offset != 0) {
185 natives_region =
186 base::MemoryMappedFile::Region(natives_offset, natives_size);
189 base::MemoryMappedFile::Region snapshot_region =
190 base::MemoryMappedFile::Region::kWholeFile;
191 if (natives_size != 0 || natives_offset != 0) {
192 snapshot_region =
193 base::MemoryMappedFile::Region(snapshot_offset, snapshot_size);
196 return MapV8Files(
197 NULL, NULL, natives_fd, snapshot_fd, natives_region, snapshot_region);
199 #endif // V8_USE_EXTERNAL_STARTUP_DATA
201 //static
202 void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out,
203 int* natives_size_out,
204 const char** snapshot_data_out,
205 int* snapshot_size_out) {
206 if (!g_mapped_natives || !g_mapped_snapshot) {
207 *natives_data_out = *snapshot_data_out = NULL;
208 *natives_size_out = *snapshot_size_out = 0;
209 return;
211 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
212 *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data());
213 *natives_size_out = static_cast<int>(g_mapped_natives->length());
214 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
217 IsolateHolder::IsolateHolder() {
218 CHECK(g_array_buffer_allocator)
219 << "You need to invoke gin::IsolateHolder::Initialize first";
220 v8::Isolate::CreateParams params;
221 params.entry_hook = DebugImpl::GetFunctionEntryHook();
222 params.code_event_handler = DebugImpl::GetJitCodeEventHandler();
223 params.constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(),
224 base::SysInfo::AmountOfVirtualMemory(),
225 base::SysInfo::NumberOfProcessors());
226 isolate_ = v8::Isolate::New(params);
227 isolate_data_.reset(new PerIsolateData(isolate_, g_array_buffer_allocator));
228 #if defined(OS_WIN)
230 void* code_range;
231 size_t size;
232 isolate_->GetCodeRange(&code_range, &size);
233 Debug::CodeRangeCreatedCallback callback =
234 DebugImpl::GetCodeRangeCreatedCallback();
235 if (code_range && size && callback)
236 callback(code_range, size);
238 #endif
241 IsolateHolder::~IsolateHolder() {
242 if (task_observer_.get())
243 base::MessageLoop::current()->RemoveTaskObserver(task_observer_.get());
244 #if defined(OS_WIN)
246 void* code_range;
247 size_t size;
248 isolate_->GetCodeRange(&code_range, &size);
249 Debug::CodeRangeDeletedCallback callback =
250 DebugImpl::GetCodeRangeDeletedCallback();
251 if (code_range && callback)
252 callback(code_range);
254 #endif
255 isolate_data_.reset();
256 isolate_->Dispose();
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