Move login windows to/from unified desktop. This is necessary because in Unified...
[chromium-blink-merge.git] / gin / v8_initializer.cc
blob40a7d1f26a37f76eda63898d0a9f346722bc5eb8
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 // TODO(oth) Remove these temporary CHECKs once http://crbug.com/479537 is
140 // fixed. These are just here to identify whether canary failures are
141 // due to verification or file/vm failures.
142 bool natives_ok =
143 VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint);
144 CHECK(natives_ok);
145 bool snapshot_ok =
146 VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint);
147 CHECK(snapshot_ok);
148 return natives_ok && snapshot_ok;
149 #else
150 return true;
151 #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
154 // static
155 bool V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile natives_pf,
156 int64 natives_offset,
157 int64 natives_size,
158 base::PlatformFile snapshot_pf,
159 int64 snapshot_offset,
160 int64 snapshot_size) {
161 if (g_mapped_natives && g_mapped_snapshot)
162 return true;
164 base::MemoryMappedFile::Region natives_region =
165 base::MemoryMappedFile::Region::kWholeFile;
166 if (natives_size != 0 || natives_offset != 0) {
167 natives_region =
168 base::MemoryMappedFile::Region(natives_offset, natives_size);
171 base::MemoryMappedFile::Region snapshot_region =
172 base::MemoryMappedFile::Region::kWholeFile;
173 if (natives_size != 0 || natives_offset != 0) {
174 snapshot_region =
175 base::MemoryMappedFile::Region(snapshot_offset, snapshot_size);
178 return MapV8Files(base::File(natives_pf), base::File(snapshot_pf),
179 natives_region, snapshot_region);
182 // static
183 bool V8Initializer::OpenV8FilesForChildProcesses(
184 base::PlatformFile* natives_fd_out,
185 base::PlatformFile* snapshot_fd_out) {
186 base::FilePath natives_data_path;
187 base::FilePath snapshot_data_path;
188 GetV8FilePaths(&natives_data_path, &snapshot_data_path);
190 int file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
191 base::File natives_data_file(natives_data_path, file_flags);
192 base::File snapshot_data_file(snapshot_data_path, file_flags);
194 if (!natives_data_file.IsValid() || !snapshot_data_file.IsValid())
195 return false;
197 *natives_fd_out = natives_data_file.TakePlatformFile();
198 *snapshot_fd_out = snapshot_data_file.TakePlatformFile();
199 return true;
202 #endif // V8_USE_EXTERNAL_STARTUP_DATA
204 // static
205 void V8Initializer::Initialize(gin::IsolateHolder::ScriptMode mode) {
206 static bool v8_is_initialized = false;
207 if (v8_is_initialized)
208 return;
210 v8::V8::InitializePlatform(V8Platform::Get());
212 if (gin::IsolateHolder::kStrictMode == mode) {
213 static const char use_strict[] = "--use_strict";
214 v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1);
217 #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
218 v8::StartupData natives;
219 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
220 natives.raw_size = static_cast<int>(g_mapped_natives->length());
221 v8::V8::SetNativesDataBlob(&natives);
223 v8::StartupData snapshot;
224 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
225 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
226 v8::V8::SetSnapshotDataBlob(&snapshot);
227 #endif // V8_USE_EXTERNAL_STARTUP_DATA
229 v8::V8::SetEntropySource(&GenerateEntropy);
230 v8::V8::Initialize();
232 v8_is_initialized = true;
235 // static
236 void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out,
237 int* natives_size_out,
238 const char** snapshot_data_out,
239 int* snapshot_size_out) {
240 if (!g_mapped_natives || !g_mapped_snapshot) {
241 *natives_data_out = *snapshot_data_out = NULL;
242 *natives_size_out = *snapshot_size_out = 0;
243 return;
245 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
246 *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data());
247 *natives_size_out = static_cast<int>(g_mapped_natives->length());
248 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
251 } // namespace gin