blob: deed15188d074685ff0808c90450aa61fa0e05a5 [file] [log] [blame]
oth05c26fde2015-04-05 14:30:571// 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.
4
5#include "gin/v8_initializer.h"
6
avi90e658dd2015-12-21 07:16:197#include <stddef.h>
8#include <stdint.h>
9
mostynbc862da82016-04-03 15:54:3310#include <memory>
11
oth29c7ed92015-06-19 14:40:0012#include "base/debug/alias.h"
rmcilroyfe515ad2016-04-08 17:59:1013#include "base/feature_list.h"
oth05c26fde2015-04-05 14:30:5714#include "base/files/file.h"
15#include "base/files/file_path.h"
16#include "base/files/memory_mapped_file.h"
tobiasjsb20016272016-02-10 11:54:1217#include "base/lazy_instance.h"
oth05c26fde2015-04-05 14:30:5718#include "base/logging.h"
oth575f7fb52015-05-08 17:35:0019#include "base/metrics/histogram.h"
oth05c26fde2015-04-05 14:30:5720#include "base/rand_util.h"
21#include "base/strings/sys_string_conversions.h"
rmcilroy542f61c2016-06-06 16:08:1922#include "base/sys_info.h"
oth575f7fb52015-05-08 17:35:0023#include "base/threading/platform_thread.h"
24#include "base/time/time.h"
oth05c26fde2015-04-05 14:30:5725#include "crypto/sha2.h"
rmcilroyfe515ad2016-04-08 17:59:1026#include "gin/public/gin_features.h"
oth05c26fde2015-04-05 14:30:5727
28#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
agrieve6f3002d2015-06-19 16:49:0629#if defined(OS_ANDROID)
30#include "base/android/apk_assets.h"
31#endif
oth05c26fde2015-04-05 14:30:5732#if defined(OS_MACOSX)
33#include "base/mac/foundation_util.h"
34#endif // OS_MACOSX
35#include "base/path_service.h"
36#endif // V8_USE_EXTERNAL_STARTUP_DATA
37
38namespace gin {
39
40namespace {
41
agrievefd2d44ab2015-06-19 04:33:0342// None of these globals are ever freed nor closed.
oth05c26fde2015-04-05 14:30:5743base::MemoryMappedFile* g_mapped_natives = nullptr;
44base::MemoryMappedFile* g_mapped_snapshot = nullptr;
45
46#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:0347
agrievefd2d44ab2015-06-19 04:33:0348// File handles intentionally never closed. Not using File here because its
49// Windows implementation guards against two instances owning the same
50// PlatformFile (which we allow since we know it is never freed).
tobiasjsb20016272016-02-10 11:54:1251typedef std::map<const char*,
52 std::pair<base::PlatformFile, base::MemoryMappedFile::Region>>
53 OpenedFileMap;
54static base::LazyInstance<OpenedFileMap>::Leaky g_opened_files =
55 LAZY_INSTANCE_INITIALIZER;
56
57OpenedFileMap::mapped_type& GetOpenedFile(const char* file) {
58 OpenedFileMap& opened_files(g_opened_files.Get());
59 if (opened_files.find(file) == opened_files.end()) {
rockot5d4213ff2016-05-25 19:07:1060 opened_files[file] = std::make_pair(base::kInvalidPlatformFile,
61 base::MemoryMappedFile::Region());
tobiasjsb20016272016-02-10 11:54:1262 }
63 return opened_files[file];
64}
agrievefd2d44ab2015-06-19 04:33:0365
michaelbai016306732015-11-03 19:48:0066#if defined(OS_ANDROID)
tobiasjsb20016272016-02-10 11:54:1267const char kNativesFileName64[] = "natives_blob_64.bin";
68const char kSnapshotFileName64[] = "snapshot_blob_64.bin";
69const char kNativesFileName32[] = "natives_blob_32.bin";
70const char kSnapshotFileName32[] = "snapshot_blob_32.bin";
71
72#if defined(__LP64__)
73#define kNativesFileName kNativesFileName64
74#define kSnapshotFileName kSnapshotFileName64
michaelbai016306732015-11-03 19:48:0075#else
tobiasjsb20016272016-02-10 11:54:1276#define kNativesFileName kNativesFileName32
77#define kSnapshotFileName kSnapshotFileName32
78#endif
michaelbai016306732015-11-03 19:48:0079
80#else // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5881const char kNativesFileName[] = "natives_blob.bin";
82const char kSnapshotFileName[] = "snapshot_blob.bin";
michaelbai016306732015-11-03 19:48:0083#endif // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5884
erikcorryc94eff12015-06-08 11:29:1685void GetV8FilePath(const char* file_name, base::FilePath* path_out) {
rmcilroy54fab5e2015-04-06 21:14:5886#if !defined(OS_MACOSX)
87 base::FilePath data_path;
agrieve6f3002d2015-06-19 16:49:0688#if defined(OS_ANDROID)
89 // This is the path within the .apk.
90 data_path = base::FilePath(FILE_PATH_LITERAL("assets"));
91#elif defined(OS_POSIX)
92 PathService::Get(base::DIR_EXE, &data_path);
93#elif defined(OS_WIN)
94 PathService::Get(base::DIR_MODULE, &data_path);
95#endif
rmcilroy54fab5e2015-04-06 21:14:5896 DCHECK(!data_path.empty());
97
erikcorryc94eff12015-06-08 11:29:1698 *path_out = data_path.AppendASCII(file_name);
rmcilroy54fab5e2015-04-06 21:14:5899#else // !defined(OS_MACOSX)
100 base::ScopedCFTypeRef<CFStringRef> natives_file_name(
erikcorryc94eff12015-06-08 11:29:16101 base::SysUTF8ToCFStringRef(file_name));
102 *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name);
rmcilroy54fab5e2015-04-06 21:14:58103#endif // !defined(OS_MACOSX)
erikcorryc94eff12015-06-08 11:29:16104 DCHECK(!path_out->empty());
rmcilroy54fab5e2015-04-06 21:14:58105}
106
agrievefd2d44ab2015-06-19 04:33:03107static bool MapV8File(base::PlatformFile platform_file,
erikcorryc94eff12015-06-08 11:29:16108 base::MemoryMappedFile::Region region,
109 base::MemoryMappedFile** mmapped_file_out) {
110 DCHECK(*mmapped_file_out == NULL);
mostynbc862da82016-04-03 15:54:33111 std::unique_ptr<base::MemoryMappedFile> mmapped_file(
112 new base::MemoryMappedFile());
agrievefd2d44ab2015-06-19 04:33:03113 if (mmapped_file->Initialize(base::File(platform_file), region)) {
114 *mmapped_file_out = mmapped_file.release();
115 return true;
oth05c26fde2015-04-05 14:30:57116 }
agrievefd2d44ab2015-06-19 04:33:03117 return false;
oth05c26fde2015-04-05 14:30:57118}
119
agrievefd2d44ab2015-06-19 04:33:03120base::PlatformFile OpenV8File(const char* file_name,
121 base::MemoryMappedFile::Region* region_out) {
oth575f7fb52015-05-08 17:35:00122 // Re-try logic here is motivated by http://crbug.com/479537
123 // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609).
124
125 // These match tools/metrics/histograms.xml
126 enum OpenV8FileResult {
127 OPENED = 0,
128 OPENED_RETRY,
129 FAILED_IN_USE,
130 FAILED_OTHER,
131 MAX_VALUE
132 };
agrievefd2d44ab2015-06-19 04:33:03133 base::FilePath path;
134 GetV8FilePath(file_name, &path);
135
agrieve6f3002d2015-06-19 16:49:06136#if defined(OS_ANDROID)
137 base::File file(base::android::OpenApkAsset(path.value(), region_out));
138 OpenV8FileResult result = file.IsValid() ? OpenV8FileResult::OPENED
139 : OpenV8FileResult::FAILED_OTHER;
140#else
141 // Re-try logic here is motivated by http://crbug.com/479537
142 // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609).
143 const int kMaxOpenAttempts = 5;
144 const int kOpenRetryDelayMillis = 250;
145
oth575f7fb52015-05-08 17:35:00146 OpenV8FileResult result = OpenV8FileResult::FAILED_IN_USE;
agrievefd2d44ab2015-06-19 04:33:03147 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
148 base::File file;
oth575f7fb52015-05-08 17:35:00149 for (int attempt = 0; attempt < kMaxOpenAttempts; attempt++) {
150 file.Initialize(path, flags);
151 if (file.IsValid()) {
agrievefd2d44ab2015-06-19 04:33:03152 *region_out = base::MemoryMappedFile::Region::kWholeFile;
oth575f7fb52015-05-08 17:35:00153 if (attempt == 0) {
154 result = OpenV8FileResult::OPENED;
155 break;
156 } else {
157 result = OpenV8FileResult::OPENED_RETRY;
158 break;
159 }
160 } else if (file.error_details() != base::File::FILE_ERROR_IN_USE) {
161 result = OpenV8FileResult::FAILED_OTHER;
oth29c7ed92015-06-19 14:40:00162#ifdef OS_WIN
163 // TODO(oth): temporary diagnostics for http://crbug.com/479537
164 std::string narrow(kNativesFileName);
165 base::FilePath::StringType nativesBlob(narrow.begin(), narrow.end());
166 if (path.BaseName().value() == nativesBlob) {
167 base::File::Error file_error = file.error_details();
168 base::debug::Alias(&file_error);
169 LOG(FATAL) << "Failed to open V8 file '" << path.value()
170 << "' (reason: " << file.error_details() << ")";
171 }
172#endif // OS_WIN
oth575f7fb52015-05-08 17:35:00173 break;
174 } else if (kMaxOpenAttempts - 1 != attempt) {
175 base::PlatformThread::Sleep(
176 base::TimeDelta::FromMilliseconds(kOpenRetryDelayMillis));
177 }
178 }
agrieve6f3002d2015-06-19 16:49:06179#endif // defined(OS_ANDROID)
oth575f7fb52015-05-08 17:35:00180
181 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result",
182 result,
183 OpenV8FileResult::MAX_VALUE);
agrievefd2d44ab2015-06-19 04:33:03184 return file.TakePlatformFile();
185}
oth575f7fb52015-05-08 17:35:00186
tobiasjsb20016272016-02-10 11:54:12187static const OpenedFileMap::mapped_type OpenFileIfNecessary(
188 const char* file_name) {
189 OpenedFileMap::mapped_type& opened = GetOpenedFile(file_name);
rockot5d4213ff2016-05-25 19:07:10190 if (opened.first == base::kInvalidPlatformFile) {
tobiasjsb20016272016-02-10 11:54:12191 opened.first = OpenV8File(file_name, &opened.second);
agrievefd2d44ab2015-06-19 04:33:03192 }
tobiasjsb20016272016-02-10 11:54:12193 return opened;
oth575f7fb52015-05-08 17:35:00194}
195
oth05c26fde2015-04-05 14:30:57196#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
erikcorryc94eff12015-06-08 11:29:16197bool VerifyV8StartupFile(base::MemoryMappedFile** file,
198 const unsigned char* fingerprint) {
oth05c26fde2015-04-05 14:30:57199 unsigned char output[crypto::kSHA256Length];
200 crypto::SHA256HashString(
erikcorryc94eff12015-06-08 11:29:16201 base::StringPiece(reinterpret_cast<const char*>((*file)->data()),
202 (*file)->length()),
oth05c26fde2015-04-05 14:30:57203 output, sizeof(output));
erikcorryc94eff12015-06-08 11:29:16204 if (!memcmp(fingerprint, output, sizeof(output))) {
205 return true;
206 }
oth29c7ed92015-06-19 14:40:00207
208 // TODO(oth): Remove this temporary diagnostics for http://crbug.com/501799
209 uint64_t input[sizeof(output)];
210 memcpy(input, fingerprint, sizeof(input));
211
212 base::debug::Alias(output);
213 base::debug::Alias(input);
214
215 const uint64_t* o64 = reinterpret_cast<const uint64_t*>(output);
216 const uint64_t* f64 = reinterpret_cast<const uint64_t*>(fingerprint);
217 LOG(FATAL) << "Natives length " << (*file)->length()
218 << " H(computed) " << o64[0] << o64[1] << o64[2] << o64[3]
219 << " H(expected) " << f64[0] << f64[1] << f64[2] << f64[3];
220
erikcorryc94eff12015-06-08 11:29:16221 delete *file;
222 *file = NULL;
223 return false;
oth05c26fde2015-04-05 14:30:57224}
225#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
226#endif // V8_USE_EXTERNAL_STARTUP_DATA
227
rmcilroy54fab5e2015-04-06 21:14:58228bool GenerateEntropy(unsigned char* buffer, size_t amount) {
229 base::RandBytes(buffer, amount);
230 return true;
231}
232
rmcilroy542f61c2016-06-06 16:08:19233bool ShouldUseIgnition() {
234 if (base::FeatureList::IsEnabled(features::kV8Ignition)) return true;
235#if defined(OS_ANDROID)
236 if (base::FeatureList::IsEnabled(features::kV8IgnitionLowEnd) &&
237 base::SysInfo::IsLowEndDevice()) {
238 return true;
239 }
240#endif
241 return false;
242}
243
244
oth05c26fde2015-04-05 14:30:57245} // namespace
246
247#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
oth05c26fde2015-04-05 14:30:57248#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
249// Defined in gen/gin/v8_snapshot_fingerprint.cc
250extern const unsigned char g_natives_fingerprint[];
251extern const unsigned char g_snapshot_fingerprint[];
252#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
253
erikcorryc94eff12015-06-08 11:29:16254enum LoadV8FileResult {
255 V8_LOAD_SUCCESS = 0,
256 V8_LOAD_FAILED_OPEN,
257 V8_LOAD_FAILED_MAP,
258 V8_LOAD_FAILED_VERIFY,
259 V8_LOAD_MAX_VALUE
260};
oth575f7fb52015-05-08 17:35:00261
tobiasjsb20016272016-02-10 11:54:12262static LoadV8FileResult MapVerify(const OpenedFileMap::mapped_type& file_region,
erikcorryc94eff12015-06-08 11:29:16263#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:03264 const unsigned char* fingerprint,
erikcorryc94eff12015-06-08 11:29:16265#endif
agrievefd2d44ab2015-06-19 04:33:03266 base::MemoryMappedFile** mmapped_file_out) {
rockot5d4213ff2016-05-25 19:07:10267 if (file_region.first == base::kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16268 return V8_LOAD_FAILED_OPEN;
tobiasjsb20016272016-02-10 11:54:12269 if (!MapV8File(file_region.first, file_region.second, mmapped_file_out))
erikcorryc94eff12015-06-08 11:29:16270 return V8_LOAD_FAILED_MAP;
oth05c26fde2015-04-05 14:30:57271#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
erikcorryc94eff12015-06-08 11:29:16272 if (!VerifyV8StartupFile(mmapped_file_out, fingerprint))
273 return V8_LOAD_FAILED_VERIFY;
oth05c26fde2015-04-05 14:30:57274#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
erikcorryc94eff12015-06-08 11:29:16275 return V8_LOAD_SUCCESS;
oth05c26fde2015-04-05 14:30:57276}
277
278// static
erikcorryc94eff12015-06-08 11:29:16279void V8Initializer::LoadV8Snapshot() {
280 if (g_mapped_snapshot)
281 return;
282
tobiasjsb20016272016-02-10 11:54:12283 OpenFileIfNecessary(kSnapshotFileName);
284 LoadV8FileResult result = MapVerify(GetOpenedFile(kSnapshotFileName),
erikcorryc94eff12015-06-08 11:29:16285#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:03286 g_snapshot_fingerprint,
erikcorryc94eff12015-06-08 11:29:16287#endif
agrievefd2d44ab2015-06-19 04:33:03288 &g_mapped_snapshot);
289 // V8 can't start up without the source of the natives, but it can
290 // start up (slower) without the snapshot.
erikcorryc94eff12015-06-08 11:29:16291 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
292 V8_LOAD_MAX_VALUE);
293}
294
295void V8Initializer::LoadV8Natives() {
296 if (g_mapped_natives)
297 return;
298
tobiasjsb20016272016-02-10 11:54:12299 OpenFileIfNecessary(kNativesFileName);
300 LoadV8FileResult result = MapVerify(GetOpenedFile(kNativesFileName),
erikcorryc94eff12015-06-08 11:29:16301#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:03302 g_natives_fingerprint,
erikcorryc94eff12015-06-08 11:29:16303#endif
agrievefd2d44ab2015-06-19 04:33:03304 &g_mapped_natives);
erikcorryc94eff12015-06-08 11:29:16305 if (result != V8_LOAD_SUCCESS) {
306 LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is "
307 << static_cast<int>(result);
308 }
309}
310
311// static
312void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf,
avi90e658dd2015-12-21 07:16:19313 int64_t snapshot_offset,
314 int64_t snapshot_size) {
erikcorryc94eff12015-06-08 11:29:16315 if (g_mapped_snapshot)
316 return;
317
rockot5d4213ff2016-05-25 19:07:10318 if (snapshot_pf == base::kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16319 return;
320
321 base::MemoryMappedFile::Region snapshot_region =
322 base::MemoryMappedFile::Region::kWholeFile;
323 if (snapshot_size != 0 || snapshot_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03324 snapshot_region.offset = snapshot_offset;
325 snapshot_region.size = snapshot_size;
erikcorryc94eff12015-06-08 11:29:16326 }
327
328 LoadV8FileResult result = V8_LOAD_SUCCESS;
agrievefd2d44ab2015-06-19 04:33:03329 if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot))
erikcorryc94eff12015-06-08 11:29:16330 result = V8_LOAD_FAILED_MAP;
331#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
332 if (!VerifyV8StartupFile(&g_mapped_snapshot, g_snapshot_fingerprint))
333 result = V8_LOAD_FAILED_VERIFY;
334#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
mnaganovd6920a62015-08-25 17:20:31335 if (result == V8_LOAD_SUCCESS) {
tobiasjsb20016272016-02-10 11:54:12336 g_opened_files.Get()[kSnapshotFileName] =
337 std::make_pair(snapshot_pf, snapshot_region);
mnaganovd6920a62015-08-25 17:20:31338 }
erikcorryc94eff12015-06-08 11:29:16339 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
340 V8_LOAD_MAX_VALUE);
341}
342
343// static
344void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf,
avi90e658dd2015-12-21 07:16:19345 int64_t natives_offset,
346 int64_t natives_size) {
erikcorryc94eff12015-06-08 11:29:16347 if (g_mapped_natives)
348 return;
349
rockot5d4213ff2016-05-25 19:07:10350 CHECK_NE(natives_pf, base::kInvalidPlatformFile);
oth05c26fde2015-04-05 14:30:57351
352 base::MemoryMappedFile::Region natives_region =
353 base::MemoryMappedFile::Region::kWholeFile;
354 if (natives_size != 0 || natives_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03355 natives_region.offset = natives_offset;
356 natives_region.size = natives_size;
oth05c26fde2015-04-05 14:30:57357 }
358
agrievefd2d44ab2015-06-19 04:33:03359 if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) {
erikcorryc94eff12015-06-08 11:29:16360 LOG(FATAL) << "Couldn't mmap v8 natives data file";
oth05c26fde2015-04-05 14:30:57361 }
erikcorryc94eff12015-06-08 11:29:16362#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
363 if (!VerifyV8StartupFile(&g_mapped_natives, g_natives_fingerprint)) {
364 LOG(FATAL) << "Couldn't verify contents of v8 natives data file";
365 }
366#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
tobiasjsb20016272016-02-10 11:54:12367 g_opened_files.Get()[kNativesFileName] =
368 std::make_pair(natives_pf, natives_region);
oth05c26fde2015-04-05 14:30:57369}
370
rmcilroy54fab5e2015-04-06 21:14:58371// static
agrievefd2d44ab2015-06-19 04:33:03372base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses(
373 base::MemoryMappedFile::Region* region_out) {
tobiasjsb20016272016-02-10 11:54:12374 const OpenedFileMap::mapped_type& opened =
375 OpenFileIfNecessary(kNativesFileName);
376 *region_out = opened.second;
377 return opened.first;
rmcilroy54fab5e2015-04-06 21:14:58378}
379
agrievefd2d44ab2015-06-19 04:33:03380// static
381base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses(
382 base::MemoryMappedFile::Region* region_out) {
tobiasjsb20016272016-02-10 11:54:12383 const OpenedFileMap::mapped_type& opened =
384 OpenFileIfNecessary(kSnapshotFileName);
385 *region_out = opened.second;
386 return opened.first;
agrievefd2d44ab2015-06-19 04:33:03387}
tobiasjsb20016272016-02-10 11:54:12388
389#if defined(OS_ANDROID)
390// static
391base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses(
392 base::MemoryMappedFile::Region* region_out,
393 bool abi_32_bit) {
394 const char* natives_file =
395 abi_32_bit ? kNativesFileName32 : kNativesFileName64;
396 const OpenedFileMap::mapped_type& opened = OpenFileIfNecessary(natives_file);
397 *region_out = opened.second;
398 return opened.first;
399}
400
401// static
402base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses(
403 base::MemoryMappedFile::Region* region_out,
404 bool abi_32_bit) {
405 const char* snapshot_file =
406 abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64;
407 const OpenedFileMap::mapped_type& opened = OpenFileIfNecessary(snapshot_file);
408 *region_out = opened.second;
409 return opened.first;
410}
mrunal.kapadeded427a2016-04-26 00:10:44411
412// static
413base::FilePath V8Initializer::GetNativesFilePath(bool abi_32_bit) {
414 base::FilePath path;
415 GetV8FilePath(abi_32_bit ? kNativesFileName32 : kNativesFileName64, &path);
416 return path;
417}
418
419// static
420base::FilePath V8Initializer::GetSnapshotFilePath(bool abi_32_bit) {
421 base::FilePath path;
422 GetV8FilePath(abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64, &path);
423 return path;
424}
tobiasjsb20016272016-02-10 11:54:12425#endif // defined(OS_ANDROID)
agrievefd2d44ab2015-06-19 04:33:03426#endif // defined(V8_USE_EXTERNAL_STARTUP_DATA)
oth05c26fde2015-04-05 14:30:57427
428// static
yhirano93150242015-12-07 12:28:33429void V8Initializer::Initialize(IsolateHolder::ScriptMode mode,
430 IsolateHolder::V8ExtrasMode v8_extras_mode) {
oth05c26fde2015-04-05 14:30:57431 static bool v8_is_initialized = false;
432 if (v8_is_initialized)
433 return;
434
435 v8::V8::InitializePlatform(V8Platform::Get());
oth05c26fde2015-04-05 14:30:57436
yhirano93150242015-12-07 12:28:33437 if (IsolateHolder::kStrictMode == mode) {
oth05c26fde2015-04-05 14:30:57438 static const char use_strict[] = "--use_strict";
439 v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1);
440 }
yhirano93150242015-12-07 12:28:33441 if (IsolateHolder::kStableAndExperimentalV8Extras == v8_extras_mode) {
442 static const char flag[] = "--experimental_extras";
443 v8::V8::SetFlagsFromString(flag, sizeof(flag) - 1);
444 }
oth05c26fde2015-04-05 14:30:57445
rmcilroy542f61c2016-06-06 16:08:19446 if (ShouldUseIgnition()) {
rmcilroyfe515ad2016-04-08 17:59:10447 std::string flag("--ignition");
448 v8::V8::SetFlagsFromString(flag.c_str(), static_cast<int>(flag.size()));
rmcilroy19ab48d2016-05-20 12:30:02449
450 if (base::FeatureList::IsEnabled(features::kV8IgnitionEager)) {
451 std::string eager_flag("--ignition-eager");
452 v8::V8::SetFlagsFromString(
453 eager_flag.c_str(), static_cast<int>(eager_flag.size()));
454 }
455
456 if (base::FeatureList::IsEnabled(features::kV8IgnitionLazy)) {
457 std::string lazy_flag("--no-ignition-eager");
458 v8::V8::SetFlagsFromString(
459 lazy_flag.c_str(), static_cast<int>(lazy_flag.size()));
460 }
rmcilroyfe515ad2016-04-08 17:59:10461 }
462
rmcilroyc7caaca2016-05-12 17:29:40463
oth05c26fde2015-04-05 14:30:57464#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
465 v8::StartupData natives;
466 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
467 natives.raw_size = static_cast<int>(g_mapped_natives->length());
468 v8::V8::SetNativesDataBlob(&natives);
469
erikcorryc94eff12015-06-08 11:29:16470 if (g_mapped_snapshot != NULL) {
471 v8::StartupData snapshot;
472 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
473 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
474 v8::V8::SetSnapshotDataBlob(&snapshot);
475 }
oth05c26fde2015-04-05 14:30:57476#endif // V8_USE_EXTERNAL_STARTUP_DATA
477
478 v8::V8::SetEntropySource(&GenerateEntropy);
479 v8::V8::Initialize();
480
481 v8_is_initialized = true;
482}
483
484// static
485void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out,
486 int* natives_size_out,
487 const char** snapshot_data_out,
488 int* snapshot_size_out) {
erikcorryc94eff12015-06-08 11:29:16489 if (g_mapped_natives) {
490 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
491 *natives_size_out = static_cast<int>(g_mapped_natives->length());
492 } else {
493 *natives_data_out = NULL;
494 *natives_size_out = 0;
oth05c26fde2015-04-05 14:30:57495 }
erikcorryc94eff12015-06-08 11:29:16496 if (g_mapped_snapshot) {
497 *snapshot_data_out =
498 reinterpret_cast<const char*>(g_mapped_snapshot->data());
499 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
500 } else {
501 *snapshot_data_out = NULL;
502 *snapshot_size_out = 0;
503 }
oth05c26fde2015-04-05 14:30:57504}
505
oth05c26fde2015-04-05 14:30:57506} // namespace gin