Don't add an aura tooltip to bubble close buttons on Windows.
[chromium-blink-merge.git] / gin / v8_initializer.cc
blob67b5f88de2f846f336053731dcbfc0fe44291b11
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/v8_initializer.h"
7 #include "base/basictypes.h"
8 #include "base/files/file.h"
9 #include "base/files/file_path.h"
10 #include "base/files/memory_mapped_file.h"
11 #include "base/logging.h"
12 #include "base/rand_util.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "crypto/sha2.h"
16 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
17 #if defined(OS_MACOSX)
18 #include "base/mac/foundation_util.h"
19 #endif // OS_MACOSX
20 #include "base/path_service.h"
21 #endif // V8_USE_EXTERNAL_STARTUP_DATA
23 namespace gin {
25 namespace {
27 base::MemoryMappedFile* g_mapped_natives = nullptr;
28 base::MemoryMappedFile* g_mapped_snapshot = nullptr;
30 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
31 #if !defined(OS_MACOSX)
32 const int kV8SnapshotBasePathKey =
33 #if defined(OS_ANDROID)
34 base::DIR_ANDROID_APP_DATA;
35 #elif defined(OS_POSIX)
36 base::DIR_EXE;
37 #elif defined(OS_WIN)
38 base::DIR_MODULE;
39 #endif // OS_ANDROID
40 #endif // !OS_MACOSX
42 const char kNativesFileName[] = "natives_blob.bin";
43 const char kSnapshotFileName[] = "snapshot_blob.bin";
45 void GetV8FilePaths(base::FilePath* natives_path_out,
46 base::FilePath* snapshot_path_out) {
47 #if !defined(OS_MACOSX)
48 base::FilePath data_path;
49 PathService::Get(kV8SnapshotBasePathKey, &data_path);
50 DCHECK(!data_path.empty());
52 *natives_path_out = data_path.AppendASCII(kNativesFileName);
53 *snapshot_path_out = data_path.AppendASCII(kSnapshotFileName);
54 #else // !defined(OS_MACOSX)
55 base::ScopedCFTypeRef<CFStringRef> natives_file_name(
56 base::SysUTF8ToCFStringRef(kNativesFileName));
57 *natives_path_out =
58 base::mac::PathForFrameworkBundleResource(natives_file_name);
59 base::ScopedCFTypeRef<CFStringRef> snapshot_file_name(
60 base::SysUTF8ToCFStringRef(kSnapshotFileName));
61 *snapshot_path_out =
62 base::mac::PathForFrameworkBundleResource(snapshot_file_name);
63 DCHECK(!natives_path_out->empty());
64 DCHECK(!snapshot_path_out->empty());
65 #endif // !defined(OS_MACOSX)
68 bool MapV8Files(base::File natives_file,
69 base::File snapshot_file,
70 base::MemoryMappedFile::Region natives_region =
71 base::MemoryMappedFile::Region::kWholeFile,
72 base::MemoryMappedFile::Region snapshot_region =
73 base::MemoryMappedFile::Region::kWholeFile) {
74 g_mapped_natives = new base::MemoryMappedFile;
75 if (!g_mapped_natives->IsValid()) {
76 if (!g_mapped_natives->Initialize(natives_file.Pass(), natives_region)) {
77 delete g_mapped_natives;
78 g_mapped_natives = NULL;
79 LOG(FATAL) << "Couldn't mmap v8 natives data file";
80 return false;
84 g_mapped_snapshot = new base::MemoryMappedFile;
85 if (!g_mapped_snapshot->IsValid()) {
86 if (!g_mapped_snapshot->Initialize(snapshot_file.Pass(), snapshot_region)) {
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
108 #endif // V8_USE_EXTERNAL_STARTUP_DATA
110 bool GenerateEntropy(unsigned char* buffer, size_t amount) {
111 base::RandBytes(buffer, amount);
112 return true;
115 } // namespace
117 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
118 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
119 // Defined in gen/gin/v8_snapshot_fingerprint.cc
120 extern const unsigned char g_natives_fingerprint[];
121 extern const unsigned char g_snapshot_fingerprint[];
122 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
124 // static
125 bool V8Initializer::LoadV8Snapshot() {
126 if (g_mapped_natives && g_mapped_snapshot)
127 return true;
129 base::FilePath natives_data_path;
130 base::FilePath snapshot_data_path;
131 GetV8FilePaths(&natives_data_path, &snapshot_data_path);
133 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
134 if (!MapV8Files(base::File(natives_data_path, flags),
135 base::File(snapshot_data_path, flags)))
136 return false;
138 #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
139 return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) &&
140 VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint);
141 #else
142 return true;
143 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
146 // static
147 bool V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile natives_pf,
148 int64 natives_offset,
149 int64 natives_size,
150 base::PlatformFile snapshot_pf,
151 int64 snapshot_offset,
152 int64 snapshot_size) {
153 if (g_mapped_natives && g_mapped_snapshot)
154 return true;
156 base::MemoryMappedFile::Region natives_region =
157 base::MemoryMappedFile::Region::kWholeFile;
158 if (natives_size != 0 || natives_offset != 0) {
159 natives_region =
160 base::MemoryMappedFile::Region(natives_offset, natives_size);
163 base::MemoryMappedFile::Region snapshot_region =
164 base::MemoryMappedFile::Region::kWholeFile;
165 if (natives_size != 0 || natives_offset != 0) {
166 snapshot_region =
167 base::MemoryMappedFile::Region(snapshot_offset, snapshot_size);
170 return MapV8Files(base::File(natives_pf), base::File(snapshot_pf),
171 natives_region, snapshot_region);
174 // static
175 bool V8Initializer::OpenV8FilesForChildProcesses(
176 base::PlatformFile* natives_fd_out,
177 base::PlatformFile* snapshot_fd_out) {
178 base::FilePath natives_data_path;
179 base::FilePath snapshot_data_path;
180 GetV8FilePaths(&natives_data_path, &snapshot_data_path);
182 int file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
183 base::File natives_data_file(natives_data_path, file_flags);
184 base::File snapshot_data_file(snapshot_data_path, file_flags);
186 if (!natives_data_file.IsValid() || !snapshot_data_file.IsValid())
187 return false;
189 *natives_fd_out = natives_data_file.TakePlatformFile();
190 *snapshot_fd_out = snapshot_data_file.TakePlatformFile();
191 return true;
194 #endif // V8_USE_EXTERNAL_STARTUP_DATA
196 // static
197 void V8Initializer::Initialize(gin::IsolateHolder::ScriptMode mode,
198 v8::ArrayBuffer::Allocator* allocator) {
199 CHECK(allocator);
201 static bool v8_is_initialized = false;
202 if (v8_is_initialized)
203 return;
205 v8::V8::InitializePlatform(V8Platform::Get());
206 v8::V8::SetArrayBufferAllocator(allocator);
208 if (gin::IsolateHolder::kStrictMode == mode) {
209 static const char use_strict[] = "--use_strict";
210 v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1);
213 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
214 v8::StartupData natives;
215 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
216 natives.raw_size = static_cast<int>(g_mapped_natives->length());
217 v8::V8::SetNativesDataBlob(&natives);
219 v8::StartupData snapshot;
220 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
221 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
222 v8::V8::SetSnapshotDataBlob(&snapshot);
223 #endif // V8_USE_EXTERNAL_STARTUP_DATA
225 v8::V8::SetEntropySource(&GenerateEntropy);
226 v8::V8::Initialize();
228 v8_is_initialized = true;
231 // static
232 void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out,
233 int* natives_size_out,
234 const char** snapshot_data_out,
235 int* snapshot_size_out) {
236 if (!g_mapped_natives || !g_mapped_snapshot) {
237 *natives_data_out = *snapshot_data_out = NULL;
238 *natives_size_out = *snapshot_size_out = 0;
239 return;
241 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
242 *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data());
243 *natives_size_out = static_cast<int>(g_mapped_natives->length());
244 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
247 } // namespace gin