blob: e06c1325290a21935281eab01534e693061f1092 [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
michaelbai020375882016-06-21 16:08:1566const char kNativesFileName[] = "natives_blob.bin";
67
michaelbai016306732015-11-03 19:48:0068#if defined(OS_ANDROID)
tobiasjsb20016272016-02-10 11:54:1269const char kSnapshotFileName64[] = "snapshot_blob_64.bin";
tobiasjsb20016272016-02-10 11:54:1270const char kSnapshotFileName32[] = "snapshot_blob_32.bin";
71
72#if defined(__LP64__)
tobiasjsb20016272016-02-10 11:54:1273#define kSnapshotFileName kSnapshotFileName64
michaelbai016306732015-11-03 19:48:0074#else
tobiasjsb20016272016-02-10 11:54:1275#define kSnapshotFileName kSnapshotFileName32
76#endif
michaelbai016306732015-11-03 19:48:0077
78#else // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5879const char kSnapshotFileName[] = "snapshot_blob.bin";
michaelbai016306732015-11-03 19:48:0080#endif // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5881
erikcorryc94eff12015-06-08 11:29:1682void GetV8FilePath(const char* file_name, base::FilePath* path_out) {
rmcilroy54fab5e2015-04-06 21:14:5883#if !defined(OS_MACOSX)
84 base::FilePath data_path;
agrieve6f3002d2015-06-19 16:49:0685#if defined(OS_ANDROID)
86 // This is the path within the .apk.
87 data_path = base::FilePath(FILE_PATH_LITERAL("assets"));
88#elif defined(OS_POSIX)
89 PathService::Get(base::DIR_EXE, &data_path);
90#elif defined(OS_WIN)
91 PathService::Get(base::DIR_MODULE, &data_path);
92#endif
rmcilroy54fab5e2015-04-06 21:14:5893 DCHECK(!data_path.empty());
94
erikcorryc94eff12015-06-08 11:29:1695 *path_out = data_path.AppendASCII(file_name);
rmcilroy54fab5e2015-04-06 21:14:5896#else // !defined(OS_MACOSX)
97 base::ScopedCFTypeRef<CFStringRef> natives_file_name(
erikcorryc94eff12015-06-08 11:29:1698 base::SysUTF8ToCFStringRef(file_name));
99 *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name);
rmcilroy54fab5e2015-04-06 21:14:58100#endif // !defined(OS_MACOSX)
erikcorryc94eff12015-06-08 11:29:16101 DCHECK(!path_out->empty());
rmcilroy54fab5e2015-04-06 21:14:58102}
103
agrievefd2d44ab2015-06-19 04:33:03104static bool MapV8File(base::PlatformFile platform_file,
erikcorryc94eff12015-06-08 11:29:16105 base::MemoryMappedFile::Region region,
106 base::MemoryMappedFile** mmapped_file_out) {
107 DCHECK(*mmapped_file_out == NULL);
mostynbc862da82016-04-03 15:54:33108 std::unique_ptr<base::MemoryMappedFile> mmapped_file(
109 new base::MemoryMappedFile());
agrievefd2d44ab2015-06-19 04:33:03110 if (mmapped_file->Initialize(base::File(platform_file), region)) {
111 *mmapped_file_out = mmapped_file.release();
112 return true;
oth05c26fde2015-04-05 14:30:57113 }
agrievefd2d44ab2015-06-19 04:33:03114 return false;
oth05c26fde2015-04-05 14:30:57115}
116
agrievefd2d44ab2015-06-19 04:33:03117base::PlatformFile OpenV8File(const char* file_name,
118 base::MemoryMappedFile::Region* region_out) {
oth575f7fb52015-05-08 17:35:00119 // Re-try logic here is motivated by http://crbug.com/479537
120 // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609).
121
122 // These match tools/metrics/histograms.xml
123 enum OpenV8FileResult {
124 OPENED = 0,
125 OPENED_RETRY,
126 FAILED_IN_USE,
127 FAILED_OTHER,
128 MAX_VALUE
129 };
agrievefd2d44ab2015-06-19 04:33:03130 base::FilePath path;
131 GetV8FilePath(file_name, &path);
132
agrieve6f3002d2015-06-19 16:49:06133#if defined(OS_ANDROID)
134 base::File file(base::android::OpenApkAsset(path.value(), region_out));
135 OpenV8FileResult result = file.IsValid() ? OpenV8FileResult::OPENED
136 : OpenV8FileResult::FAILED_OTHER;
137#else
138 // Re-try logic here is motivated by http://crbug.com/479537
139 // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609).
140 const int kMaxOpenAttempts = 5;
141 const int kOpenRetryDelayMillis = 250;
142
oth575f7fb52015-05-08 17:35:00143 OpenV8FileResult result = OpenV8FileResult::FAILED_IN_USE;
agrievefd2d44ab2015-06-19 04:33:03144 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
145 base::File file;
oth575f7fb52015-05-08 17:35:00146 for (int attempt = 0; attempt < kMaxOpenAttempts; attempt++) {
147 file.Initialize(path, flags);
148 if (file.IsValid()) {
agrievefd2d44ab2015-06-19 04:33:03149 *region_out = base::MemoryMappedFile::Region::kWholeFile;
oth575f7fb52015-05-08 17:35:00150 if (attempt == 0) {
151 result = OpenV8FileResult::OPENED;
152 break;
153 } else {
154 result = OpenV8FileResult::OPENED_RETRY;
155 break;
156 }
157 } else if (file.error_details() != base::File::FILE_ERROR_IN_USE) {
158 result = OpenV8FileResult::FAILED_OTHER;
oth29c7ed92015-06-19 14:40:00159#ifdef OS_WIN
160 // TODO(oth): temporary diagnostics for http://crbug.com/479537
161 std::string narrow(kNativesFileName);
162 base::FilePath::StringType nativesBlob(narrow.begin(), narrow.end());
163 if (path.BaseName().value() == nativesBlob) {
164 base::File::Error file_error = file.error_details();
165 base::debug::Alias(&file_error);
166 LOG(FATAL) << "Failed to open V8 file '" << path.value()
167 << "' (reason: " << file.error_details() << ")";
168 }
169#endif // OS_WIN
oth575f7fb52015-05-08 17:35:00170 break;
171 } else if (kMaxOpenAttempts - 1 != attempt) {
172 base::PlatformThread::Sleep(
173 base::TimeDelta::FromMilliseconds(kOpenRetryDelayMillis));
174 }
175 }
agrieve6f3002d2015-06-19 16:49:06176#endif // defined(OS_ANDROID)
oth575f7fb52015-05-08 17:35:00177
178 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result",
179 result,
180 OpenV8FileResult::MAX_VALUE);
agrievefd2d44ab2015-06-19 04:33:03181 return file.TakePlatformFile();
182}
oth575f7fb52015-05-08 17:35:00183
tobiasjsb20016272016-02-10 11:54:12184static const OpenedFileMap::mapped_type OpenFileIfNecessary(
185 const char* file_name) {
186 OpenedFileMap::mapped_type& opened = GetOpenedFile(file_name);
rockot5d4213ff2016-05-25 19:07:10187 if (opened.first == base::kInvalidPlatformFile) {
tobiasjsb20016272016-02-10 11:54:12188 opened.first = OpenV8File(file_name, &opened.second);
agrievefd2d44ab2015-06-19 04:33:03189 }
tobiasjsb20016272016-02-10 11:54:12190 return opened;
oth575f7fb52015-05-08 17:35:00191}
192
oth05c26fde2015-04-05 14:30:57193#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
erikcorryc94eff12015-06-08 11:29:16194bool VerifyV8StartupFile(base::MemoryMappedFile** file,
195 const unsigned char* fingerprint) {
oth05c26fde2015-04-05 14:30:57196 unsigned char output[crypto::kSHA256Length];
197 crypto::SHA256HashString(
erikcorryc94eff12015-06-08 11:29:16198 base::StringPiece(reinterpret_cast<const char*>((*file)->data()),
199 (*file)->length()),
oth05c26fde2015-04-05 14:30:57200 output, sizeof(output));
erikcorryc94eff12015-06-08 11:29:16201 if (!memcmp(fingerprint, output, sizeof(output))) {
202 return true;
203 }
oth29c7ed92015-06-19 14:40:00204
205 // TODO(oth): Remove this temporary diagnostics for http://crbug.com/501799
206 uint64_t input[sizeof(output)];
207 memcpy(input, fingerprint, sizeof(input));
208
209 base::debug::Alias(output);
210 base::debug::Alias(input);
211
212 const uint64_t* o64 = reinterpret_cast<const uint64_t*>(output);
213 const uint64_t* f64 = reinterpret_cast<const uint64_t*>(fingerprint);
214 LOG(FATAL) << "Natives length " << (*file)->length()
215 << " H(computed) " << o64[0] << o64[1] << o64[2] << o64[3]
216 << " H(expected) " << f64[0] << f64[1] << f64[2] << f64[3];
217
erikcorryc94eff12015-06-08 11:29:16218 delete *file;
219 *file = NULL;
220 return false;
oth05c26fde2015-04-05 14:30:57221}
222#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
223#endif // V8_USE_EXTERNAL_STARTUP_DATA
224
rmcilroy54fab5e2015-04-06 21:14:58225bool GenerateEntropy(unsigned char* buffer, size_t amount) {
226 base::RandBytes(buffer, amount);
227 return true;
228}
229
rmcilroy542f61c2016-06-06 16:08:19230bool ShouldUseIgnition() {
231 if (base::FeatureList::IsEnabled(features::kV8Ignition)) return true;
232#if defined(OS_ANDROID)
233 if (base::FeatureList::IsEnabled(features::kV8IgnitionLowEnd) &&
234 base::SysInfo::IsLowEndDevice()) {
235 return true;
236 }
237#endif
238 return false;
239}
240
241
oth05c26fde2015-04-05 14:30:57242} // namespace
243
244#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
oth05c26fde2015-04-05 14:30:57245#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
246// Defined in gen/gin/v8_snapshot_fingerprint.cc
247extern const unsigned char g_natives_fingerprint[];
248extern const unsigned char g_snapshot_fingerprint[];
249#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
250
erikcorryc94eff12015-06-08 11:29:16251enum LoadV8FileResult {
252 V8_LOAD_SUCCESS = 0,
253 V8_LOAD_FAILED_OPEN,
254 V8_LOAD_FAILED_MAP,
255 V8_LOAD_FAILED_VERIFY,
256 V8_LOAD_MAX_VALUE
257};
oth575f7fb52015-05-08 17:35:00258
tobiasjsb20016272016-02-10 11:54:12259static LoadV8FileResult MapVerify(const OpenedFileMap::mapped_type& file_region,
erikcorryc94eff12015-06-08 11:29:16260#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:03261 const unsigned char* fingerprint,
erikcorryc94eff12015-06-08 11:29:16262#endif
agrievefd2d44ab2015-06-19 04:33:03263 base::MemoryMappedFile** mmapped_file_out) {
rockot5d4213ff2016-05-25 19:07:10264 if (file_region.first == base::kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16265 return V8_LOAD_FAILED_OPEN;
tobiasjsb20016272016-02-10 11:54:12266 if (!MapV8File(file_region.first, file_region.second, mmapped_file_out))
erikcorryc94eff12015-06-08 11:29:16267 return V8_LOAD_FAILED_MAP;
oth05c26fde2015-04-05 14:30:57268#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
erikcorryc94eff12015-06-08 11:29:16269 if (!VerifyV8StartupFile(mmapped_file_out, fingerprint))
270 return V8_LOAD_FAILED_VERIFY;
oth05c26fde2015-04-05 14:30:57271#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
erikcorryc94eff12015-06-08 11:29:16272 return V8_LOAD_SUCCESS;
oth05c26fde2015-04-05 14:30:57273}
274
275// static
erikcorryc94eff12015-06-08 11:29:16276void V8Initializer::LoadV8Snapshot() {
277 if (g_mapped_snapshot)
278 return;
279
tobiasjsb20016272016-02-10 11:54:12280 OpenFileIfNecessary(kSnapshotFileName);
281 LoadV8FileResult result = MapVerify(GetOpenedFile(kSnapshotFileName),
erikcorryc94eff12015-06-08 11:29:16282#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:03283 g_snapshot_fingerprint,
erikcorryc94eff12015-06-08 11:29:16284#endif
agrievefd2d44ab2015-06-19 04:33:03285 &g_mapped_snapshot);
286 // V8 can't start up without the source of the natives, but it can
287 // start up (slower) without the snapshot.
erikcorryc94eff12015-06-08 11:29:16288 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
289 V8_LOAD_MAX_VALUE);
290}
291
292void V8Initializer::LoadV8Natives() {
293 if (g_mapped_natives)
294 return;
295
tobiasjsb20016272016-02-10 11:54:12296 OpenFileIfNecessary(kNativesFileName);
297 LoadV8FileResult result = MapVerify(GetOpenedFile(kNativesFileName),
erikcorryc94eff12015-06-08 11:29:16298#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:03299 g_natives_fingerprint,
erikcorryc94eff12015-06-08 11:29:16300#endif
agrievefd2d44ab2015-06-19 04:33:03301 &g_mapped_natives);
erikcorryc94eff12015-06-08 11:29:16302 if (result != V8_LOAD_SUCCESS) {
303 LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is "
304 << static_cast<int>(result);
305 }
306}
307
308// static
309void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf,
avi90e658dd2015-12-21 07:16:19310 int64_t snapshot_offset,
311 int64_t snapshot_size) {
erikcorryc94eff12015-06-08 11:29:16312 if (g_mapped_snapshot)
313 return;
314
rockot5d4213ff2016-05-25 19:07:10315 if (snapshot_pf == base::kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16316 return;
317
318 base::MemoryMappedFile::Region snapshot_region =
319 base::MemoryMappedFile::Region::kWholeFile;
320 if (snapshot_size != 0 || snapshot_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03321 snapshot_region.offset = snapshot_offset;
322 snapshot_region.size = snapshot_size;
erikcorryc94eff12015-06-08 11:29:16323 }
324
325 LoadV8FileResult result = V8_LOAD_SUCCESS;
agrievefd2d44ab2015-06-19 04:33:03326 if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot))
erikcorryc94eff12015-06-08 11:29:16327 result = V8_LOAD_FAILED_MAP;
328#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
329 if (!VerifyV8StartupFile(&g_mapped_snapshot, g_snapshot_fingerprint))
330 result = V8_LOAD_FAILED_VERIFY;
331#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
mnaganovd6920a62015-08-25 17:20:31332 if (result == V8_LOAD_SUCCESS) {
tobiasjsb20016272016-02-10 11:54:12333 g_opened_files.Get()[kSnapshotFileName] =
334 std::make_pair(snapshot_pf, snapshot_region);
mnaganovd6920a62015-08-25 17:20:31335 }
erikcorryc94eff12015-06-08 11:29:16336 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
337 V8_LOAD_MAX_VALUE);
338}
339
340// static
341void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf,
avi90e658dd2015-12-21 07:16:19342 int64_t natives_offset,
343 int64_t natives_size) {
erikcorryc94eff12015-06-08 11:29:16344 if (g_mapped_natives)
345 return;
346
rockot5d4213ff2016-05-25 19:07:10347 CHECK_NE(natives_pf, base::kInvalidPlatformFile);
oth05c26fde2015-04-05 14:30:57348
349 base::MemoryMappedFile::Region natives_region =
350 base::MemoryMappedFile::Region::kWholeFile;
351 if (natives_size != 0 || natives_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03352 natives_region.offset = natives_offset;
353 natives_region.size = natives_size;
oth05c26fde2015-04-05 14:30:57354 }
355
agrievefd2d44ab2015-06-19 04:33:03356 if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) {
erikcorryc94eff12015-06-08 11:29:16357 LOG(FATAL) << "Couldn't mmap v8 natives data file";
oth05c26fde2015-04-05 14:30:57358 }
erikcorryc94eff12015-06-08 11:29:16359#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
360 if (!VerifyV8StartupFile(&g_mapped_natives, g_natives_fingerprint)) {
361 LOG(FATAL) << "Couldn't verify contents of v8 natives data file";
362 }
363#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
tobiasjsb20016272016-02-10 11:54:12364 g_opened_files.Get()[kNativesFileName] =
365 std::make_pair(natives_pf, natives_region);
oth05c26fde2015-04-05 14:30:57366}
367
rmcilroy54fab5e2015-04-06 21:14:58368// static
agrievefd2d44ab2015-06-19 04:33:03369base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses(
370 base::MemoryMappedFile::Region* region_out) {
tobiasjsb20016272016-02-10 11:54:12371 const OpenedFileMap::mapped_type& opened =
372 OpenFileIfNecessary(kNativesFileName);
373 *region_out = opened.second;
374 return opened.first;
rmcilroy54fab5e2015-04-06 21:14:58375}
376
agrievefd2d44ab2015-06-19 04:33:03377// static
378base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses(
379 base::MemoryMappedFile::Region* region_out) {
tobiasjsb20016272016-02-10 11:54:12380 const OpenedFileMap::mapped_type& opened =
381 OpenFileIfNecessary(kSnapshotFileName);
382 *region_out = opened.second;
383 return opened.first;
agrievefd2d44ab2015-06-19 04:33:03384}
tobiasjsb20016272016-02-10 11:54:12385
386#if defined(OS_ANDROID)
387// static
tobiasjsb20016272016-02-10 11:54:12388base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses(
389 base::MemoryMappedFile::Region* region_out,
390 bool abi_32_bit) {
391 const char* snapshot_file =
392 abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64;
393 const OpenedFileMap::mapped_type& opened = OpenFileIfNecessary(snapshot_file);
394 *region_out = opened.second;
395 return opened.first;
396}
mrunal.kapadeded427a2016-04-26 00:10:44397
398// static
michaelbai020375882016-06-21 16:08:15399base::FilePath V8Initializer::GetNativesFilePath() {
mrunal.kapadeded427a2016-04-26 00:10:44400 base::FilePath path;
michaelbai020375882016-06-21 16:08:15401 GetV8FilePath(kNativesFileName, &path);
mrunal.kapadeded427a2016-04-26 00:10:44402 return path;
403}
404
405// static
406base::FilePath V8Initializer::GetSnapshotFilePath(bool abi_32_bit) {
407 base::FilePath path;
408 GetV8FilePath(abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64, &path);
409 return path;
410}
tobiasjsb20016272016-02-10 11:54:12411#endif // defined(OS_ANDROID)
agrievefd2d44ab2015-06-19 04:33:03412#endif // defined(V8_USE_EXTERNAL_STARTUP_DATA)
oth05c26fde2015-04-05 14:30:57413
414// static
yhirano93150242015-12-07 12:28:33415void V8Initializer::Initialize(IsolateHolder::ScriptMode mode,
416 IsolateHolder::V8ExtrasMode v8_extras_mode) {
oth05c26fde2015-04-05 14:30:57417 static bool v8_is_initialized = false;
418 if (v8_is_initialized)
419 return;
420
421 v8::V8::InitializePlatform(V8Platform::Get());
oth05c26fde2015-04-05 14:30:57422
yhirano93150242015-12-07 12:28:33423 if (IsolateHolder::kStrictMode == mode) {
oth05c26fde2015-04-05 14:30:57424 static const char use_strict[] = "--use_strict";
425 v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1);
426 }
yhirano93150242015-12-07 12:28:33427 if (IsolateHolder::kStableAndExperimentalV8Extras == v8_extras_mode) {
428 static const char flag[] = "--experimental_extras";
429 v8::V8::SetFlagsFromString(flag, sizeof(flag) - 1);
430 }
oth05c26fde2015-04-05 14:30:57431
rmcilroy542f61c2016-06-06 16:08:19432 if (ShouldUseIgnition()) {
rmcilroyfe515ad2016-04-08 17:59:10433 std::string flag("--ignition");
434 v8::V8::SetFlagsFromString(flag.c_str(), static_cast<int>(flag.size()));
rmcilroy19ab48d2016-05-20 12:30:02435
436 if (base::FeatureList::IsEnabled(features::kV8IgnitionEager)) {
437 std::string eager_flag("--ignition-eager");
438 v8::V8::SetFlagsFromString(
439 eager_flag.c_str(), static_cast<int>(eager_flag.size()));
440 }
441
442 if (base::FeatureList::IsEnabled(features::kV8IgnitionLazy)) {
443 std::string lazy_flag("--no-ignition-eager");
444 v8::V8::SetFlagsFromString(
445 lazy_flag.c_str(), static_cast<int>(lazy_flag.size()));
446 }
rmcilroyfe515ad2016-04-08 17:59:10447 }
448
rmcilroyc7caaca2016-05-12 17:29:40449
oth05c26fde2015-04-05 14:30:57450#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
451 v8::StartupData natives;
452 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
453 natives.raw_size = static_cast<int>(g_mapped_natives->length());
454 v8::V8::SetNativesDataBlob(&natives);
455
erikcorryc94eff12015-06-08 11:29:16456 if (g_mapped_snapshot != NULL) {
457 v8::StartupData snapshot;
458 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
459 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
460 v8::V8::SetSnapshotDataBlob(&snapshot);
461 }
oth05c26fde2015-04-05 14:30:57462#endif // V8_USE_EXTERNAL_STARTUP_DATA
463
464 v8::V8::SetEntropySource(&GenerateEntropy);
465 v8::V8::Initialize();
466
467 v8_is_initialized = true;
468}
469
470// static
471void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out,
472 int* natives_size_out,
473 const char** snapshot_data_out,
474 int* snapshot_size_out) {
erikcorryc94eff12015-06-08 11:29:16475 if (g_mapped_natives) {
476 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
477 *natives_size_out = static_cast<int>(g_mapped_natives->length());
478 } else {
479 *natives_data_out = NULL;
480 *natives_size_out = 0;
oth05c26fde2015-04-05 14:30:57481 }
erikcorryc94eff12015-06-08 11:29:16482 if (g_mapped_snapshot) {
483 *snapshot_data_out =
484 reinterpret_cast<const char*>(g_mapped_snapshot->data());
485 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
486 } else {
487 *snapshot_data_out = NULL;
488 *snapshot_size_out = 0;
489 }
oth05c26fde2015-04-05 14:30:57490}
491
oth05c26fde2015-04-05 14:30:57492} // namespace gin