blob: ca0e3efd3b47b9000299f514e4e59e88ffa8134d [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"
rmcilroy462e47e2016-06-24 20:45:0813#include "base/debug/crash_logging.h"
rmcilroyfe515ad2016-04-08 17:59:1014#include "base/feature_list.h"
oth05c26fde2015-04-05 14:30:5715#include "base/files/file.h"
16#include "base/files/file_path.h"
17#include "base/files/memory_mapped_file.h"
tobiasjsb20016272016-02-10 11:54:1218#include "base/lazy_instance.h"
oth05c26fde2015-04-05 14:30:5719#include "base/logging.h"
asvitkine30330812016-08-30 04:01:0820#include "base/metrics/histogram_macros.h"
Hitoshi Yoshidaf2f50de2017-08-22 13:23:5521#include "base/path_service.h"
oth05c26fde2015-04-05 14:30:5722#include "base/rand_util.h"
23#include "base/strings/sys_string_conversions.h"
rmcilroy542f61c2016-06-06 16:08:1924#include "base/sys_info.h"
oth575f7fb52015-05-08 17:35:0025#include "base/threading/platform_thread.h"
26#include "base/time/time.h"
Hitoshi Yoshidaf2f50de2017-08-22 13:23:5527#include "build/build_config.h"
oth05c26fde2015-04-05 14:30:5728
29#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
agrieve6f3002d2015-06-19 16:49:0630#if defined(OS_ANDROID)
31#include "base/android/apk_assets.h"
Hitoshi Yoshidaf2f50de2017-08-22 13:23:5532#elif defined(OS_MACOSX)
Max Morinf0d13c92017-08-17 10:04:5933#include "base/mac/foundation_util.h"
Hitoshi Yoshidaf2f50de2017-08-22 13:23:5534#endif
oth05c26fde2015-04-05 14:30:5735#endif // V8_USE_EXTERNAL_STARTUP_DATA
36
37namespace gin {
38
39namespace {
40
agrievefd2d44ab2015-06-19 04:33:0341// None of these globals are ever freed nor closed.
oth05c26fde2015-04-05 14:30:5742base::MemoryMappedFile* g_mapped_natives = nullptr;
43base::MemoryMappedFile* g_mapped_snapshot = nullptr;
Hitoshi Yoshidaf2f50de2017-08-22 13:23:5544base::MemoryMappedFile* g_mapped_v8_context_snapshot = nullptr;
oth05c26fde2015-04-05 14:30:5745
Hitoshi Yoshidaf2f50de2017-08-22 13:23:5546const char kV8ContextSnapshotFileName[] = "v8_context_snapshot.bin";
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).
Hitoshi Yoshidaf2f50de2017-08-22 13:23:5551using OpenedFileMap =
52 std::map<const char*,
53 std::pair<base::PlatformFile, base::MemoryMappedFile::Region>>;
54base::LazyInstance<OpenedFileMap>::Leaky g_opened_files =
tobiasjsb20016272016-02-10 11:54:1255 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
Hitoshi Yoshidaf2f50de2017-08-22 13:23:5566#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
67
michaelbai020375882016-06-21 16:08:1568const char kNativesFileName[] = "natives_blob.bin";
69
michaelbai016306732015-11-03 19:48:0070#if defined(OS_ANDROID)
tobiasjsb20016272016-02-10 11:54:1271const char kSnapshotFileName64[] = "snapshot_blob_64.bin";
tobiasjsb20016272016-02-10 11:54:1272const char kSnapshotFileName32[] = "snapshot_blob_32.bin";
73
74#if defined(__LP64__)
tobiasjsb20016272016-02-10 11:54:1275#define kSnapshotFileName kSnapshotFileName64
michaelbai016306732015-11-03 19:48:0076#else
tobiasjsb20016272016-02-10 11:54:1277#define kSnapshotFileName kSnapshotFileName32
78#endif
michaelbai016306732015-11-03 19:48:0079
80#else // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5881const char kSnapshotFileName[] = "snapshot_blob.bin";
michaelbai016306732015-11-03 19:48:0082#endif // defined(OS_ANDROID)
rmcilroy54fab5e2015-04-06 21:14:5883
Hitoshi Yoshidaf2f50de2017-08-22 13:23:5584#endif // defined(V8_USE_EXTERNAL_STATUP_DATA)
85
erikcorryc94eff12015-06-08 11:29:1686void GetV8FilePath(const char* file_name, base::FilePath* path_out) {
rmcilroy54fab5e2015-04-06 21:14:5887#if !defined(OS_MACOSX)
88 base::FilePath data_path;
agrieve6f3002d2015-06-19 16:49:0689#if defined(OS_ANDROID)
90 // This is the path within the .apk.
91 data_path = base::FilePath(FILE_PATH_LITERAL("assets"));
92#elif defined(OS_POSIX)
93 PathService::Get(base::DIR_EXE, &data_path);
94#elif defined(OS_WIN)
95 PathService::Get(base::DIR_MODULE, &data_path);
96#endif
rmcilroy54fab5e2015-04-06 21:14:5897 DCHECK(!data_path.empty());
98
erikcorryc94eff12015-06-08 11:29:1699 *path_out = data_path.AppendASCII(file_name);
rmcilroy54fab5e2015-04-06 21:14:58100#else // !defined(OS_MACOSX)
101 base::ScopedCFTypeRef<CFStringRef> natives_file_name(
erikcorryc94eff12015-06-08 11:29:16102 base::SysUTF8ToCFStringRef(file_name));
103 *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name);
rmcilroy54fab5e2015-04-06 21:14:58104#endif // !defined(OS_MACOSX)
105}
106
Hitoshi Yoshidaf2f50de2017-08-22 13:23:55107bool MapV8File(base::PlatformFile platform_file,
108 base::MemoryMappedFile::Region region,
109 base::MemoryMappedFile** mmapped_file_out) {
erikcorryc94eff12015-06-08 11:29:16110 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
Hitoshi Yoshidaf2f50de2017-08-22 13:23:55187const OpenedFileMap::mapped_type OpenFileIfNecessary(const char* file_name) {
tobiasjsb20016272016-02-10 11:54:12188 OpenedFileMap::mapped_type& opened = GetOpenedFile(file_name);
rockot5d4213ff2016-05-25 19:07:10189 if (opened.first == base::kInvalidPlatformFile) {
tobiasjsb20016272016-02-10 11:54:12190 opened.first = OpenV8File(file_name, &opened.second);
agrievefd2d44ab2015-06-19 04:33:03191 }
tobiasjsb20016272016-02-10 11:54:12192 return opened;
oth575f7fb52015-05-08 17:35:00193}
194
rmcilroy54fab5e2015-04-06 21:14:58195bool GenerateEntropy(unsigned char* buffer, size_t amount) {
196 base::RandBytes(buffer, amount);
197 return true;
198}
199
erikcorryc94eff12015-06-08 11:29:16200enum LoadV8FileResult {
201 V8_LOAD_SUCCESS = 0,
202 V8_LOAD_FAILED_OPEN,
203 V8_LOAD_FAILED_MAP,
jcivellidbe3dec2017-02-07 16:58:23204 V8_LOAD_FAILED_VERIFY, // Deprecated.
erikcorryc94eff12015-06-08 11:29:16205 V8_LOAD_MAX_VALUE
206};
oth575f7fb52015-05-08 17:35:00207
Hitoshi Yoshidaf2f50de2017-08-22 13:23:55208LoadV8FileResult MapOpenedFile(const OpenedFileMap::mapped_type& file_region,
209 base::MemoryMappedFile** mmapped_file_out) {
rockot5d4213ff2016-05-25 19:07:10210 if (file_region.first == base::kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16211 return V8_LOAD_FAILED_OPEN;
tobiasjsb20016272016-02-10 11:54:12212 if (!MapV8File(file_region.first, file_region.second, mmapped_file_out))
erikcorryc94eff12015-06-08 11:29:16213 return V8_LOAD_FAILED_MAP;
erikcorryc94eff12015-06-08 11:29:16214 return V8_LOAD_SUCCESS;
oth05c26fde2015-04-05 14:30:57215}
216
jcivellidbe3dec2017-02-07 16:58:23217} // namespace
218
Hitoshi Yoshidaf2f50de2017-08-22 13:23:55219#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
220
oth05c26fde2015-04-05 14:30:57221// static
erikcorryc94eff12015-06-08 11:29:16222void V8Initializer::LoadV8Snapshot() {
223 if (g_mapped_snapshot)
224 return;
225
tobiasjsb20016272016-02-10 11:54:12226 OpenFileIfNecessary(kSnapshotFileName);
jcivellidbe3dec2017-02-07 16:58:23227 LoadV8FileResult result = MapOpenedFile(GetOpenedFile(kSnapshotFileName),
228 &g_mapped_snapshot);
agrievefd2d44ab2015-06-19 04:33:03229 // V8 can't start up without the source of the natives, but it can
230 // start up (slower) without the snapshot.
erikcorryc94eff12015-06-08 11:29:16231 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
232 V8_LOAD_MAX_VALUE);
233}
234
235void V8Initializer::LoadV8Natives() {
236 if (g_mapped_natives)
237 return;
238
tobiasjsb20016272016-02-10 11:54:12239 OpenFileIfNecessary(kNativesFileName);
jcivellidbe3dec2017-02-07 16:58:23240 LoadV8FileResult result = MapOpenedFile(GetOpenedFile(kNativesFileName),
241 &g_mapped_natives);
erikcorryc94eff12015-06-08 11:29:16242 if (result != V8_LOAD_SUCCESS) {
243 LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is "
244 << static_cast<int>(result);
245 }
246}
247
248// static
249void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf,
avi90e658dd2015-12-21 07:16:19250 int64_t snapshot_offset,
251 int64_t snapshot_size) {
erikcorryc94eff12015-06-08 11:29:16252 if (g_mapped_snapshot)
253 return;
254
rockot5d4213ff2016-05-25 19:07:10255 if (snapshot_pf == base::kInvalidPlatformFile)
erikcorryc94eff12015-06-08 11:29:16256 return;
257
258 base::MemoryMappedFile::Region snapshot_region =
259 base::MemoryMappedFile::Region::kWholeFile;
260 if (snapshot_size != 0 || snapshot_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03261 snapshot_region.offset = snapshot_offset;
262 snapshot_region.size = snapshot_size;
erikcorryc94eff12015-06-08 11:29:16263 }
264
265 LoadV8FileResult result = V8_LOAD_SUCCESS;
agrievefd2d44ab2015-06-19 04:33:03266 if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot))
erikcorryc94eff12015-06-08 11:29:16267 result = V8_LOAD_FAILED_MAP;
mnaganovd6920a62015-08-25 17:20:31268 if (result == V8_LOAD_SUCCESS) {
tobiasjsb20016272016-02-10 11:54:12269 g_opened_files.Get()[kSnapshotFileName] =
270 std::make_pair(snapshot_pf, snapshot_region);
mnaganovd6920a62015-08-25 17:20:31271 }
erikcorryc94eff12015-06-08 11:29:16272 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result,
273 V8_LOAD_MAX_VALUE);
274}
275
276// static
277void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf,
avi90e658dd2015-12-21 07:16:19278 int64_t natives_offset,
279 int64_t natives_size) {
erikcorryc94eff12015-06-08 11:29:16280 if (g_mapped_natives)
281 return;
282
rockot5d4213ff2016-05-25 19:07:10283 CHECK_NE(natives_pf, base::kInvalidPlatformFile);
oth05c26fde2015-04-05 14:30:57284
285 base::MemoryMappedFile::Region natives_region =
286 base::MemoryMappedFile::Region::kWholeFile;
287 if (natives_size != 0 || natives_offset != 0) {
agrievefd2d44ab2015-06-19 04:33:03288 natives_region.offset = natives_offset;
289 natives_region.size = natives_size;
oth05c26fde2015-04-05 14:30:57290 }
291
agrievefd2d44ab2015-06-19 04:33:03292 if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) {
erikcorryc94eff12015-06-08 11:29:16293 LOG(FATAL) << "Couldn't mmap v8 natives data file";
oth05c26fde2015-04-05 14:30:57294 }
tobiasjsb20016272016-02-10 11:54:12295 g_opened_files.Get()[kNativesFileName] =
296 std::make_pair(natives_pf, natives_region);
oth05c26fde2015-04-05 14:30:57297}
298
rmcilroy54fab5e2015-04-06 21:14:58299// static
agrievefd2d44ab2015-06-19 04:33:03300base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses(
301 base::MemoryMappedFile::Region* region_out) {
tobiasjsb20016272016-02-10 11:54:12302 const OpenedFileMap::mapped_type& opened =
303 OpenFileIfNecessary(kNativesFileName);
304 *region_out = opened.second;
305 return opened.first;
rmcilroy54fab5e2015-04-06 21:14:58306}
307
agrievefd2d44ab2015-06-19 04:33:03308// static
309base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses(
310 base::MemoryMappedFile::Region* region_out) {
tobiasjsb20016272016-02-10 11:54:12311 const OpenedFileMap::mapped_type& opened =
312 OpenFileIfNecessary(kSnapshotFileName);
313 *region_out = opened.second;
314 return opened.first;
agrievefd2d44ab2015-06-19 04:33:03315}
tobiasjsb20016272016-02-10 11:54:12316
317#if defined(OS_ANDROID)
318// static
tobiasjsb20016272016-02-10 11:54:12319base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses(
320 base::MemoryMappedFile::Region* region_out,
321 bool abi_32_bit) {
322 const char* snapshot_file =
323 abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64;
324 const OpenedFileMap::mapped_type& opened = OpenFileIfNecessary(snapshot_file);
325 *region_out = opened.second;
326 return opened.first;
327}
mrunal.kapadeded427a2016-04-26 00:10:44328
329// static
michaelbai020375882016-06-21 16:08:15330base::FilePath V8Initializer::GetNativesFilePath() {
mrunal.kapadeded427a2016-04-26 00:10:44331 base::FilePath path;
michaelbai020375882016-06-21 16:08:15332 GetV8FilePath(kNativesFileName, &path);
mrunal.kapadeded427a2016-04-26 00:10:44333 return path;
334}
335
336// static
337base::FilePath V8Initializer::GetSnapshotFilePath(bool abi_32_bit) {
338 base::FilePath path;
339 GetV8FilePath(abi_32_bit ? kSnapshotFileName32 : kSnapshotFileName64, &path);
340 return path;
341}
tobiasjsb20016272016-02-10 11:54:12342#endif // defined(OS_ANDROID)
agrievefd2d44ab2015-06-19 04:33:03343#endif // defined(V8_USE_EXTERNAL_STARTUP_DATA)
oth05c26fde2015-04-05 14:30:57344
345// static
yhirano93150242015-12-07 12:28:33346void V8Initializer::Initialize(IsolateHolder::ScriptMode mode,
347 IsolateHolder::V8ExtrasMode v8_extras_mode) {
oth05c26fde2015-04-05 14:30:57348 static bool v8_is_initialized = false;
349 if (v8_is_initialized)
350 return;
351
352 v8::V8::InitializePlatform(V8Platform::Get());
oth05c26fde2015-04-05 14:30:57353
yhirano93150242015-12-07 12:28:33354 if (IsolateHolder::kStrictMode == mode) {
oth05c26fde2015-04-05 14:30:57355 static const char use_strict[] = "--use_strict";
356 v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1);
357 }
yhirano93150242015-12-07 12:28:33358 if (IsolateHolder::kStableAndExperimentalV8Extras == v8_extras_mode) {
359 static const char flag[] = "--experimental_extras";
360 v8::V8::SetFlagsFromString(flag, sizeof(flag) - 1);
361 }
oth05c26fde2015-04-05 14:30:57362
363#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
364 v8::StartupData natives;
365 natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
366 natives.raw_size = static_cast<int>(g_mapped_natives->length());
367 v8::V8::SetNativesDataBlob(&natives);
368
Hitoshi Yoshidaf2f50de2017-08-22 13:23:55369 if (g_mapped_snapshot) {
erikcorryc94eff12015-06-08 11:29:16370 v8::StartupData snapshot;
371 snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
372 snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
373 v8::V8::SetSnapshotDataBlob(&snapshot);
374 }
oth05c26fde2015-04-05 14:30:57375#endif // V8_USE_EXTERNAL_STARTUP_DATA
376
377 v8::V8::SetEntropySource(&GenerateEntropy);
378 v8::V8::Initialize();
379
380 v8_is_initialized = true;
381}
382
383// static
384void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out,
385 int* natives_size_out,
386 const char** snapshot_data_out,
387 int* snapshot_size_out) {
erikcorryc94eff12015-06-08 11:29:16388 if (g_mapped_natives) {
389 *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
390 *natives_size_out = static_cast<int>(g_mapped_natives->length());
391 } else {
392 *natives_data_out = NULL;
393 *natives_size_out = 0;
oth05c26fde2015-04-05 14:30:57394 }
erikcorryc94eff12015-06-08 11:29:16395 if (g_mapped_snapshot) {
396 *snapshot_data_out =
397 reinterpret_cast<const char*>(g_mapped_snapshot->data());
398 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
399 } else {
400 *snapshot_data_out = NULL;
401 *snapshot_size_out = 0;
402 }
oth05c26fde2015-04-05 14:30:57403}
404
Hitoshi Yoshidaf2f50de2017-08-22 13:23:55405// static
406void V8Initializer::LoadV8ContextSnapshot() {
407 if (g_mapped_v8_context_snapshot)
408 return;
409
410 OpenFileIfNecessary(kV8ContextSnapshotFileName);
411 MapOpenedFile(GetOpenedFile(kV8ContextSnapshotFileName),
412 &g_mapped_v8_context_snapshot);
413
414 // TODO(peria): Check if the snapshot file is loaded successfully.
415}
416
417// static
418void V8Initializer::LoadV8ContextSnapshotFromFD(base::PlatformFile snapshot_pf,
419 int64_t snapshot_offset,
420 int64_t snapshot_size) {
421 if (g_mapped_v8_context_snapshot)
422 return;
423 CHECK_NE(base::kInvalidPlatformFile, snapshot_pf);
424
425 base::MemoryMappedFile::Region snapshot_region =
426 base::MemoryMappedFile::Region::kWholeFile;
427 if (snapshot_size != 0 || snapshot_offset != 0) {
428 snapshot_region.offset = snapshot_offset;
429 snapshot_region.size = snapshot_size;
430 }
431
432 if (MapV8File(snapshot_pf, snapshot_region, &g_mapped_v8_context_snapshot)) {
433 g_opened_files.Get()[kV8ContextSnapshotFileName] =
434 std::make_pair(snapshot_pf, snapshot_region);
435 }
436}
437
438// static
439void V8Initializer::GetV8ContextSnapshotData(const char** snapshot_data_out,
440 int* snapshot_size_out) {
441 if (g_mapped_v8_context_snapshot) {
442 *snapshot_data_out =
443 reinterpret_cast<const char*>(g_mapped_v8_context_snapshot->data());
444 *snapshot_size_out =
445 static_cast<int>(g_mapped_v8_context_snapshot->length());
446 } else {
447 *snapshot_data_out = nullptr;
448 *snapshot_size_out = 0;
449 }
450}
451
oth05c26fde2015-04-05 14:30:57452} // namespace gin