blob: 117723eac99ca20729fe7d572f94ea711eca7c67 [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"
oth05c26fde2015-04-05 14:30:5713#include "base/files/file.h"
14#include "base/files/file_path.h"
15#include "base/files/memory_mapped_file.h"
tobiasjsb20016272016-02-10 11:54:1216#include "base/lazy_instance.h"
oth05c26fde2015-04-05 14:30:5717#include "base/logging.h"
oth575f7fb52015-05-08 17:35:0018#include "base/metrics/histogram.h"
oth05c26fde2015-04-05 14:30:5719#include "base/rand_util.h"
20#include "base/strings/sys_string_conversions.h"
oth575f7fb52015-05-08 17:35:0021#include "base/threading/platform_thread.h"
22#include "base/time/time.h"
oth05c26fde2015-04-05 14:30:5723#include "crypto/sha2.h"
24
25#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
agrieve6f3002d2015-06-19 16:49:0626#if defined(OS_ANDROID)
27#include "base/android/apk_assets.h"
28#endif
oth05c26fde2015-04-05 14:30:5729#if defined(OS_MACOSX)
30#include "base/mac/foundation_util.h"
31#endif // OS_MACOSX
32#include "base/path_service.h"
33#endif // V8_USE_EXTERNAL_STARTUP_DATA
34
35namespace gin {
36
37namespace {
38
agrievefd2d44ab2015-06-19 04:33:0339// None of these globals are ever freed nor closed.
oth05c26fde2015-04-05 14:30:5740base::MemoryMappedFile* g_mapped_natives = nullptr;
41base::MemoryMappedFile* g_mapped_snapshot = nullptr;
42
43#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:0344
45const base::PlatformFile kInvalidPlatformFile =
46#if defined(OS_WIN)
47 INVALID_HANDLE_VALUE;
48#else
49 -1;
50#endif
51
52// File handles intentionally never closed. Not using File here because its
53// Windows implementation guards against two instances owning the same
54// PlatformFile (which we allow since we know it is never freed).
tobiasjsb20016272016-02-10 11:54:1255typedef std::map<const char*,
56 std::pair<base::PlatformFile, base::MemoryMappedFile::Region>>
57 OpenedFileMap;
58static base::LazyInstance<OpenedFileMap>::Leaky g_opened_files =
59 LAZY_INSTANCE_INITIALIZER;
60
61OpenedFileMap::mapped_type& GetOpenedFile(const char* file) {
62 OpenedFileMap& opened_files(g_opened_files.Get());
63 if (opened_files.find(file) == opened_files.end()) {
64 opened_files[file] =
65 std::make_pair(kInvalidPlatformFile, base::MemoryMappedFile::Region());
66 }
67 return opened_files[file];
68}
agrievefd2d44ab2015-06-19 04:33:0369
michaelbai016306732015-11-03 19:48:0070#if defined(OS_ANDROID)
tobiasjsb20016272016-02-10 11:54:1271const char kNativesFileName64[] = "natives_blob_64.bin";
72const char kSnapshotFileName64[] = "snapshot_blob_64.bin";
73const char kNativesFileName32[] = "natives_blob_32.bin";
74const char kSnapshotFileName32[] = "snapshot_blob_32.bin";
75
76#if defined(__LP64__)
77#define kNativesFileName kNativesFileName64
78#define kSnapshotFileName kSnapshotFileName64
michaelbai016306732015-11-03 19:48:0079#else
tobiasjsb20016272016-02-10 11:54:1280#define kNativesFileName kNativesFileName32
81#define kSnapshotFileName kSnapshotFileName32
82#endif
michaelbai016306732015-11-03 19:48:0083
84#else // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5885const char kNativesFileName[] = "natives_blob.bin";
86const char kSnapshotFileName[] = "snapshot_blob.bin";
michaelbai016306732015-11-03 19:48:0087#endif // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5888
erikcorryc94eff12015-06-08 11:29:1689void GetV8FilePath(const char* file_name, base::FilePath* path_out) {
rmcilroy54fab5e2015-04-06 21:14:5890#if !defined(OS_MACOSX)
91 base::FilePath data_path;
agrieve6f3002d2015-06-19 16:49:0692#if defined(OS_ANDROID)
93 // This is the path within the .apk.
94 data_path = base::FilePath(FILE_PATH_LITERAL("assets"));
95#elif defined(OS_POSIX)
96 PathService::Get(base::DIR_EXE, &data_path);
97#elif defined(OS_WIN)
98 PathService::Get(base::DIR_MODULE, &data_path);
99#endif
rmcilroy54fab5e2015-04-06 21:14:58100 DCHECK(!data_path.empty());
101
erikcorryc94eff12015-06-08 11:29:16102 *path_out = data_path.AppendASCII(file_name);
rmcilroy54fab5e2015-04-06 21:14:58103#else // !defined(OS_MACOSX)
104 base::ScopedCFTypeRef<CFStringRef> natives_file_name(
erikcorryc94eff12015-06-08 11:29:16105 base::SysUTF8ToCFStringRef(file_name));
106 *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name);
rmcilroy54fab5e2015-04-06 21:14:58107#endif // !defined(OS_MACOSX)
erikcorryc94eff12015-06-08 11:29:16108 DCHECK(!path_out->empty());
rmcilroy54fab5e2015-04-06 21:14:58109}
110
agrievefd2d44ab2015-06-19 04:33:03111static bool MapV8File(base::PlatformFile platform_file,
erikcorryc94eff12015-06-08 11:29:16112 base::MemoryMappedFile::Region region,
113 base::MemoryMappedFile** mmapped_file_out) {
114 DCHECK(*mmapped_file_out == NULL);
mostynbc862da82016-04-03 15:54:33115 std::unique_ptr<base::MemoryMappedFile> mmapped_file(
116 new base::MemoryMappedFile());
agrievefd2d44ab2015-06-19 04:33:03117 if (mmapped_file->Initialize(base::File(platform_file), region)) {
118 *mmapped_file_out = mmapped_file.release();
119 return true;
oth05c26fde2015-04-05 14:30:57120 }
agrievefd2d44ab2015-06-19 04:33:03121 return false;
oth05c26fde2015-04-05 14:30:57122}
123
agrievefd2d44ab2015-06-19 04:33:03124base::PlatformFile OpenV8File(const char* file_name,
125 base::MemoryMappedFile::Region* region_out) {
oth575f7fb52015-05-08 17:35:00126 // Re-try logic here is motivated by http://crbug.com/479537
127 // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609).
128
129 // These match tools/metrics/histograms.xml
130 enum OpenV8FileResult {
131 OPENED = 0,
132 OPENED_RETRY,
133 FAILED_IN_USE,
134 FAILED_OTHER,
135 MAX_VALUE
136 };
agrievefd2d44ab2015-06-19 04:33:03137 base::FilePath path;
138 GetV8FilePath(file_name, &path);
139
agrieve6f3002d2015-06-19 16:49:06140#if defined(OS_ANDROID)
141 base::File file(base::android::OpenApkAsset(path.value(), region_out));
142 OpenV8FileResult result = file.IsValid() ? OpenV8FileResult::OPENED
143 : OpenV8FileResult::FAILED_OTHER;
144#else
145 // Re-try logic here is motivated by http://crbug.com/479537
146 // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609).
147 const int kMaxOpenAttempts = 5;
148 const int kOpenRetryDelayMillis = 250;
149
oth575f7fb52015-05-08 17:35:00150 OpenV8FileResult result = OpenV8FileResult::FAILED_IN_USE;
agrievefd2d44ab2015-06-19 04:33:03151 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
152 base::File file;
oth575f7fb52015-05-08 17:35:00153 for (int attempt = 0; attempt < kMaxOpenAttempts; attempt++) {
154 file.Initialize(path, flags);
155 if (file.IsValid()) {
agrievefd2d44ab2015-06-19 04:33:03156 *region_out = base::MemoryMappedFile::Region::kWholeFile;
oth575f7fb52015-05-08 17:35:00157 if (attempt == 0) {
158 result = OpenV8FileResult::OPENED;
159 break;
160 } else {
161 result = OpenV8FileResult::OPENED_RETRY;
162 break;
163 }
164 } else if (file.error_details() != base::File::FILE_ERROR_IN_USE) {
165 result = OpenV8FileResult::FAILED_OTHER;
oth29c7ed92015-06-19 14:40:00166#ifdef OS_WIN
167 // TODO(oth): temporary diagnostics for http://crbug.com/479537
168 std::string narrow(kNativesFileName);
169 base::FilePath::StringType nativesBlob(narrow.begin(), narrow.end());
170 if (path.BaseName().value() == nativesBlob) {
171 base::File::Error file_error = file.error_details();
172 base::debug::Alias(&file_error);
173 LOG(FATAL) << "Failed to open V8 file '" << path.value()
174 << "' (reason: " << file.error_details() << ")";
175 }
176#endif // OS_WIN
oth575f7fb52015-05-08 17:35:00177 break;
178 } else if (kMaxOpenAttempts - 1 != attempt) {
179 base::PlatformThread::Sleep(
180 base::TimeDelta::FromMilliseconds(kOpenRetryDelayMillis));
181 }
182 }
agrieve6f3002d2015-06-19 16:49:06183#endif // defined(OS_ANDROID)
oth575f7fb52015-05-08 17:35:00184
185 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result",
186 result,
187 OpenV8FileResult::MAX_VALUE);
agrievefd2d44ab2015-06-19 04:33:03188 return file.TakePlatformFile();
189}
oth575f7fb52015-05-08 17:35:00190
tobiasjsb20016272016-02-10 11:54:12191static const OpenedFileMap::mapped_type OpenFileIfNecessary(
192 const char* file_name) {
193 OpenedFileMap::mapped_type& opened = GetOpenedFile(file_name);
194 if (opened.first == kInvalidPlatformFile) {
195 opened.first = OpenV8File(file_name, &opened.second);
agrievefd2d44ab2015-06-19 04:33:03196 }
tobiasjsb20016272016-02-10 11:54:12197 return opened;
oth575f7fb52015-05-08 17:35:00198}
199
oth05c26fde2015-04-05 14:30:57200#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
erikcorryc94eff12015-06-08 11:29:16201bool VerifyV8StartupFile(base::MemoryMappedFile** file,
202 const unsigned char* fingerprint) {
oth05c26fde2015-04-05 14:30:57203 unsigned char output[crypto::kSHA256Length];
204 crypto::SHA256HashString(
erikcorryc94eff12015-06-08 11:29:16205 base::StringPiece(reinterpret_cast<const char*>((*file)->data()),
206 (*file)->length()),
oth05c26fde2015-04-05 14:30:57207 output, sizeof(output));
erikcorryc94eff12015-06-08 11:29:16208 if (!memcmp(fingerprint, output, sizeof(output))) {
209 return true;
210 }
oth29c7ed92015-06-19 14:40:00211
212 // TODO(oth): Remove this temporary diagnostics for http://crbug.com/501799
213 uint64_t input[sizeof(output)];
214 memcpy(input, fingerprint, sizeof(input));
215
216 base::debug::Alias(output);
217 base::debug::Alias(input);
218
219 const uint64_t* o64 = reinterpret_cast<const uint64_t*>(output);
220 const uint64_t* f64 = reinterpret_cast<const uint64_t*>(fingerprint);
221 LOG(FATAL) << "Natives length " << (*file)->length()
222 << " H(computed) " << o64[0] << o64[1] << o64[2] << o64[3]
223 << " H(expected) " << f64[0] << f64[1] << f64[2] << f64[3];
224
erikcorryc94eff12015-06-08 11:29:16225 delete *file;
226 *file = NULL;
227 return false;
oth05c26fde2015-04-05 14:30:57228}
229#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
230#endif // V8_USE_EXTERNAL_STARTUP_DATA
231
rmcilroy54fab5e2015-04-06 21:14:58232bool GenerateEntropy(unsigned char* buffer, size_t amount) {
233 base::RandBytes(buffer, amount);
234 return true;
235}
236
oth05c26fde2015-04-05 14:30:57237} // namespace
238
239#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
oth05c26fde2015-04-05 14:30:57240#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
241// Defined in gen/gin/v8_snapshot_fingerprint.cc
242extern const unsigned char g_natives_fingerprint[];
243extern const unsigned char g_snapshot_fingerprint[];
244#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
245
erikcorryc94eff12015-06-08 11:29:16246enum LoadV8FileResult {
247 V8_LOAD_SUCCESS = 0,
248 V8_LOAD_FAILED_OPEN,
249 V8_LOAD_FAILED_MAP,
250 V8_LOAD_FAILED_VERIFY,
251 V8_LOAD_MAX_VALUE
252};
oth575f7fb52015-05-08 17:35:00253
tobiasjsb20016272016-02-10 11:54:12254static LoadV8FileResult MapVerify(const OpenedFileMap::mapped_type& file_region,
erikcorryc94eff12015-06-08 11:29:16255#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:03256 const unsigned char* fingerprint,
erikcorryc94eff12015-06-08 11:29:16257#endif
agrievefd2d44ab2015-06-19 04:33:03258 base::MemoryMappedFile** mmapped_file_out) {
tobiasjsb20016272016-02-10 11:54:12259 if (file_region.first == kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16260 return V8_LOAD_FAILED_OPEN;
tobiasjsb20016272016-02-10 11:54:12261 if (!MapV8File(file_region.first, file_region.second, mmapped_file_out))
erikcorryc94eff12015-06-08 11:29:16262 return V8_LOAD_FAILED_MAP;
oth05c26fde2015-04-05 14:30:57263#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
erikcorryc94eff12015-06-08 11:29:16264 if (!VerifyV8StartupFile(mmapped_file_out, fingerprint))
265 return V8_LOAD_FAILED_VERIFY;
oth05c26fde2015-04-05 14:30:57266#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
erikcorryc94eff12015-06-08 11:29:16267 return V8_LOAD_SUCCESS;
oth05c26fde2015-04-05 14:30:57268}
269
270// static
erikcorryc94eff12015-06-08 11:29:16271void V8Initializer::LoadV8Snapshot() {
272 if (g_mapped_snapshot)
273 return;
274
tobiasjsb20016272016-02-10 11:54:12275 OpenFileIfNecessary(kSnapshotFileName);
276 LoadV8FileResult result = MapVerify(GetOpenedFile(kSnapshotFileName),
erikcorryc94eff12015-06-08 11:29:16277#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:03278 g_snapshot_fingerprint,
erikcorryc94eff12015-06-08 11:29:16279#endif
agrievefd2d44ab2015-06-19 04:33:03280 &g_mapped_snapshot);
281 // V8 can't start up without the source of the natives, but it can
282 // start up (slower) without the snapshot.
erikcorryc94eff12015-06-08 11:29:16283 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
284 V8_LOAD_MAX_VALUE);
285}
286
287void V8Initializer::LoadV8Natives() {
288 if (g_mapped_natives)
289 return;
290
tobiasjsb20016272016-02-10 11:54:12291 OpenFileIfNecessary(kNativesFileName);
292 LoadV8FileResult result = MapVerify(GetOpenedFile(kNativesFileName),
erikcorryc94eff12015-06-08 11:29:16293#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
agrievefd2d44ab2015-06-19 04:33:03294 g_natives_fingerprint,
erikcorryc94eff12015-06-08 11:29:16295#endif
agrievefd2d44ab2015-06-19 04:33:03296 &g_mapped_natives);
erikcorryc94eff12015-06-08 11:29:16297 if (result != V8_LOAD_SUCCESS) {
298 LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is "
299 << static_cast<int>(result);
300 }
301}
302
303// static
304void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf,
avi90e658dd2015-12-21 07:16:19305 int64_t snapshot_offset,
306 int64_t snapshot_size) {
erikcorryc94eff12015-06-08 11:29:16307 if (g_mapped_snapshot)
308 return;
309
agrievefd2d44ab2015-06-19 04:33:03310 if (snapshot_pf == kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16311 return;
312
313 base::MemoryMappedFile::Region snapshot_region =
314 base::MemoryMappedFile::Region::kWholeFile;
315 if (snapshot_size != 0 || snapshot_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03316 snapshot_region.offset = snapshot_offset;
317 snapshot_region.size = snapshot_size;
erikcorryc94eff12015-06-08 11:29:16318 }
319
320 LoadV8FileResult result = V8_LOAD_SUCCESS;
agrievefd2d44ab2015-06-19 04:33:03321 if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot))
erikcorryc94eff12015-06-08 11:29:16322 result = V8_LOAD_FAILED_MAP;
323#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
324 if (!VerifyV8StartupFile(&g_mapped_snapshot, g_snapshot_fingerprint))
325 result = V8_LOAD_FAILED_VERIFY;
326#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
mnaganovd6920a62015-08-25 17:20:31327 if (result == V8_LOAD_SUCCESS) {
tobiasjsb20016272016-02-10 11:54:12328 g_opened_files.Get()[kSnapshotFileName] =
329 std::make_pair(snapshot_pf, snapshot_region);
mnaganovd6920a62015-08-25 17:20:31330 }
erikcorryc94eff12015-06-08 11:29:16331 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
332 V8_LOAD_MAX_VALUE);
333}
334
335// static
336void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf,
avi90e658dd2015-12-21 07:16:19337 int64_t natives_offset,
338 int64_t natives_size) {
erikcorryc94eff12015-06-08 11:29:16339 if (g_mapped_natives)
340 return;
341
agrievefd2d44ab2015-06-19 04:33:03342 CHECK_NE(natives_pf, kInvalidPlatformFile);
oth05c26fde2015-04-05 14:30:57343
344 base::MemoryMappedFile::Region natives_region =
345 base::MemoryMappedFile::Region::kWholeFile;
346 if (natives_size != 0 || natives_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03347 natives_region.offset = natives_offset;
348 natives_region.size = natives_size;
oth05c26fde2015-04-05 14:30:57349 }
350
agrievefd2d44ab2015-06-19 04:33:03351 if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) {
erikcorryc94eff12015-06-08 11:29:16352 LOG(FATAL) << "Couldn't mmap v8 natives data file";
oth05c26fde2015-04-05 14:30:57353 }
erikcorryc94eff12015-06-08 11:29:16354#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
355 if (!VerifyV8StartupFile(&g_mapped_natives, g_natives_fingerprint)) {
356 LOG(FATAL) << "Couldn't verify contents of v8 natives data file";
357 }
358#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
tobiasjsb20016272016-02-10 11:54:12359 g_opened_files.Get()[kNativesFileName] =
360 std::make_pair(natives_pf, natives_region);
oth05c26fde2015-04-05 14:30:57361}
362
rmcilroy54fab5e2015-04-06 21:14:58363// static
agrievefd2d44ab2015-06-19 04:33:03364base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses(
365 base::MemoryMappedFile::Region* region_out) {
tobiasjsb20016272016-02-10 11:54:12366 const OpenedFileMap::mapped_type& opened =
367 OpenFileIfNecessary(kNativesFileName);
368 *region_out = opened.second;
369 return opened.first;
rmcilroy54fab5e2015-04-06 21:14:58370}
371
agrievefd2d44ab2015-06-19 04:33:03372// static
373base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses(
374 base::MemoryMappedFile::Region* region_out) {
tobiasjsb20016272016-02-10 11:54:12375 const OpenedFileMap::mapped_type& opened =
376 OpenFileIfNecessary(kSnapshotFileName);
377 *region_out = opened.second;
378 return opened.first;
agrievefd2d44ab2015-06-19 04:33:03379}
tobiasjsb20016272016-02-10 11:54:12380
381#if defined(OS_ANDROID)
382// static
383base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses(
384 base::MemoryMappedFile::Region* region_out,
385 bool abi_32_bit) {
386 const char* natives_file =
387 abi_32_bit ? kNativesFileName32 : kNativesFileName64;
388 const OpenedFileMap::mapped_type& opened = OpenFileIfNecessary(natives_file);
389 *region_out = opened.second;
390 return opened.first;
391}
392
393// static
394base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses(
395 base::MemoryMappedFile::Region* region_out,
396 bool abi_32_bit) {
397 const char* snapshot_file =
398 abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64;
399 const OpenedFileMap::mapped_type& opened = OpenFileIfNecessary(snapshot_file);
400 *region_out = opened.second;
401 return opened.first;
402}
403#endif // defined(OS_ANDROID)
agrievefd2d44ab2015-06-19 04:33:03404#endif // defined(V8_USE_EXTERNAL_STARTUP_DATA)
oth05c26fde2015-04-05 14:30:57405
406// static
yhirano93150242015-12-07 12:28:33407void V8Initializer::Initialize(IsolateHolder::ScriptMode mode,
408 IsolateHolder::V8ExtrasMode v8_extras_mode) {
oth05c26fde2015-04-05 14:30:57409 static bool v8_is_initialized = false;
410 if (v8_is_initialized)
411 return;
412
413 v8::V8::InitializePlatform(V8Platform::Get());
oth05c26fde2015-04-05 14:30:57414
yhirano93150242015-12-07 12:28:33415 if (IsolateHolder::kStrictMode == mode) {
oth05c26fde2015-04-05 14:30:57416 static const char use_strict[] = "--use_strict";
417 v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1);
418 }
yhirano93150242015-12-07 12:28:33419 if (IsolateHolder::kStableAndExperimentalV8Extras == v8_extras_mode) {
420 static const char flag[] = "--experimental_extras";
421 v8::V8::SetFlagsFromString(flag, sizeof(flag) - 1);
422 }
oth05c26fde2015-04-05 14:30:57423
424#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
425 v8::StartupData natives;
426 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
427 natives.raw_size = static_cast<int>(g_mapped_natives->length());
428 v8::V8::SetNativesDataBlob(&natives);
429
erikcorryc94eff12015-06-08 11:29:16430 if (g_mapped_snapshot != NULL) {
431 v8::StartupData snapshot;
432 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
433 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
434 v8::V8::SetSnapshotDataBlob(&snapshot);
435 }
oth05c26fde2015-04-05 14:30:57436#endif // V8_USE_EXTERNAL_STARTUP_DATA
437
438 v8::V8::SetEntropySource(&GenerateEntropy);
439 v8::V8::Initialize();
440
441 v8_is_initialized = true;
442}
443
444// static
445void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out,
446 int* natives_size_out,
447 const char** snapshot_data_out,
448 int* snapshot_size_out) {
erikcorryc94eff12015-06-08 11:29:16449 if (g_mapped_natives) {
450 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
451 *natives_size_out = static_cast<int>(g_mapped_natives->length());
452 } else {
453 *natives_data_out = NULL;
454 *natives_size_out = 0;
oth05c26fde2015-04-05 14:30:57455 }
erikcorryc94eff12015-06-08 11:29:16456 if (g_mapped_snapshot) {
457 *snapshot_data_out =
458 reinterpret_cast<const char*>(g_mapped_snapshot->data());
459 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
460 } else {
461 *snapshot_data_out = NULL;
462 *snapshot_size_out = 0;
463 }
oth05c26fde2015-04-05 14:30:57464}
465
tobiasjsb20016272016-02-10 11:54:12466#if defined(OS_ANDROID)
467// static
468base::FilePath V8Initializer::GetNativesFilePath(bool abi_32_bit) {
469 base::FilePath path;
470 GetV8FilePath(abi_32_bit ? kNativesFileName32 : kNativesFileName64, &path);
471 return path;
472}
473
474// static
475base::FilePath V8Initializer::GetSnapshotFilePath(bool abi_32_bit) {
476 base::FilePath path;
477 GetV8FilePath(abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64, &path);
478 return path;
479}
480#endif // defined(OS_ANDROID)
481
oth05c26fde2015-04-05 14:30:57482} // namespace gin