| Index: gin/v8_initializer.cc
|
| diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc
|
| index 56011d9ff5be2e8152d01c7f41249dad6d441cc4..ca0e3efd3b47b9000299f514e4e59e88ffa8134d 100644
|
| --- a/gin/v8_initializer.cc
|
| +++ b/gin/v8_initializer.cc
|
| @@ -18,20 +18,20 @@
|
| #include "base/lazy_instance.h"
|
| #include "base/logging.h"
|
| #include "base/metrics/histogram_macros.h"
|
| +#include "base/path_service.h"
|
| #include "base/rand_util.h"
|
| #include "base/strings/sys_string_conversions.h"
|
| #include "base/sys_info.h"
|
| #include "base/threading/platform_thread.h"
|
| #include "base/time/time.h"
|
| +#include "build/build_config.h"
|
|
|
| #if defined(V8_USE_EXTERNAL_STARTUP_DATA)
|
| #if defined(OS_ANDROID)
|
| #include "base/android/apk_assets.h"
|
| -#endif
|
| -#if defined(OS_MACOSX)
|
| +#elif defined(OS_MACOSX)
|
| #include "base/mac/foundation_util.h"
|
| -#endif // OS_MACOSX
|
| -#include "base/path_service.h"
|
| +#endif
|
| #endif // V8_USE_EXTERNAL_STARTUP_DATA
|
|
|
| namespace gin {
|
| @@ -41,16 +41,17 @@ namespace {
|
| // None of these globals are ever freed nor closed.
|
| base::MemoryMappedFile* g_mapped_natives = nullptr;
|
| base::MemoryMappedFile* g_mapped_snapshot = nullptr;
|
| +base::MemoryMappedFile* g_mapped_v8_context_snapshot = nullptr;
|
|
|
| -#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
|
| +const char kV8ContextSnapshotFileName[] = "v8_context_snapshot.bin";
|
|
|
| // File handles intentionally never closed. Not using File here because its
|
| // Windows implementation guards against two instances owning the same
|
| // PlatformFile (which we allow since we know it is never freed).
|
| -typedef std::map<const char*,
|
| - std::pair<base::PlatformFile, base::MemoryMappedFile::Region>>
|
| - OpenedFileMap;
|
| -static base::LazyInstance<OpenedFileMap>::Leaky g_opened_files =
|
| +using OpenedFileMap =
|
| + std::map<const char*,
|
| + std::pair<base::PlatformFile, base::MemoryMappedFile::Region>>;
|
| +base::LazyInstance<OpenedFileMap>::Leaky g_opened_files =
|
| LAZY_INSTANCE_INITIALIZER;
|
|
|
| OpenedFileMap::mapped_type& GetOpenedFile(const char* file) {
|
| @@ -62,6 +63,8 @@ OpenedFileMap::mapped_type& GetOpenedFile(const char* file) {
|
| return opened_files[file];
|
| }
|
|
|
| +#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
|
| +
|
| const char kNativesFileName[] = "natives_blob.bin";
|
|
|
| #if defined(OS_ANDROID)
|
| @@ -78,6 +81,8 @@ const char kSnapshotFileName32[] = "snapshot_blob_32.bin";
|
| const char kSnapshotFileName[] = "snapshot_blob.bin";
|
| #endif // defined(OS_ANDROID)
|
|
|
| +#endif // defined(V8_USE_EXTERNAL_STATUP_DATA)
|
| +
|
| void GetV8FilePath(const char* file_name, base::FilePath* path_out) {
|
| #if !defined(OS_MACOSX)
|
| base::FilePath data_path;
|
| @@ -97,12 +102,11 @@ void GetV8FilePath(const char* file_name, base::FilePath* path_out) {
|
| base::SysUTF8ToCFStringRef(file_name));
|
| *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name);
|
| #endif // !defined(OS_MACOSX)
|
| - DCHECK(!path_out->empty());
|
| }
|
|
|
| -static bool MapV8File(base::PlatformFile platform_file,
|
| - base::MemoryMappedFile::Region region,
|
| - base::MemoryMappedFile** mmapped_file_out) {
|
| +bool MapV8File(base::PlatformFile platform_file,
|
| + base::MemoryMappedFile::Region region,
|
| + base::MemoryMappedFile** mmapped_file_out) {
|
| DCHECK(*mmapped_file_out == NULL);
|
| std::unique_ptr<base::MemoryMappedFile> mmapped_file(
|
| new base::MemoryMappedFile());
|
| @@ -180,8 +184,7 @@ base::PlatformFile OpenV8File(const char* file_name,
|
| return file.TakePlatformFile();
|
| }
|
|
|
| -static const OpenedFileMap::mapped_type OpenFileIfNecessary(
|
| - const char* file_name) {
|
| +const OpenedFileMap::mapped_type OpenFileIfNecessary(const char* file_name) {
|
| OpenedFileMap::mapped_type& opened = GetOpenedFile(file_name);
|
| if (opened.first == base::kInvalidPlatformFile) {
|
| opened.first = OpenV8File(file_name, &opened.second);
|
| @@ -189,19 +192,11 @@ static const OpenedFileMap::mapped_type OpenFileIfNecessary(
|
| return opened;
|
| }
|
|
|
| -#endif // V8_USE_EXTERNAL_STARTUP_DATA
|
| -
|
| bool GenerateEntropy(unsigned char* buffer, size_t amount) {
|
| base::RandBytes(buffer, amount);
|
| return true;
|
| }
|
|
|
| -} // namespace
|
| -
|
| -#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
|
| -
|
| -namespace {
|
| -
|
| enum LoadV8FileResult {
|
| V8_LOAD_SUCCESS = 0,
|
| V8_LOAD_FAILED_OPEN,
|
| @@ -210,9 +205,8 @@ enum LoadV8FileResult {
|
| V8_LOAD_MAX_VALUE
|
| };
|
|
|
| -static LoadV8FileResult MapOpenedFile(
|
| - const OpenedFileMap::mapped_type& file_region,
|
| - base::MemoryMappedFile** mmapped_file_out) {
|
| +LoadV8FileResult MapOpenedFile(const OpenedFileMap::mapped_type& file_region,
|
| + base::MemoryMappedFile** mmapped_file_out) {
|
| if (file_region.first == base::kInvalidPlatformFile)
|
| return V8_LOAD_FAILED_OPEN;
|
| if (!MapV8File(file_region.first, file_region.second, mmapped_file_out))
|
| @@ -222,6 +216,8 @@ static LoadV8FileResult MapOpenedFile(
|
|
|
| } // namespace
|
|
|
| +#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
|
| +
|
| // static
|
| void V8Initializer::LoadV8Snapshot() {
|
| if (g_mapped_snapshot)
|
| @@ -370,7 +366,7 @@ void V8Initializer::Initialize(IsolateHolder::ScriptMode mode,
|
| natives.raw_size = static_cast<int>(g_mapped_natives->length());
|
| v8::V8::SetNativesDataBlob(&natives);
|
|
|
| - if (g_mapped_snapshot != NULL) {
|
| + if (g_mapped_snapshot) {
|
| v8::StartupData snapshot;
|
| snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
|
| snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
|
| @@ -406,4 +402,51 @@ void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out,
|
| }
|
| }
|
|
|
| +// static
|
| +void V8Initializer::LoadV8ContextSnapshot() {
|
| + if (g_mapped_v8_context_snapshot)
|
| + return;
|
| +
|
| + OpenFileIfNecessary(kV8ContextSnapshotFileName);
|
| + MapOpenedFile(GetOpenedFile(kV8ContextSnapshotFileName),
|
| + &g_mapped_v8_context_snapshot);
|
| +
|
| + // TODO(peria): Check if the snapshot file is loaded successfully.
|
| +}
|
| +
|
| +// static
|
| +void V8Initializer::LoadV8ContextSnapshotFromFD(base::PlatformFile snapshot_pf,
|
| + int64_t snapshot_offset,
|
| + int64_t snapshot_size) {
|
| + if (g_mapped_v8_context_snapshot)
|
| + return;
|
| + CHECK_NE(base::kInvalidPlatformFile, snapshot_pf);
|
| +
|
| + base::MemoryMappedFile::Region snapshot_region =
|
| + base::MemoryMappedFile::Region::kWholeFile;
|
| + if (snapshot_size != 0 || snapshot_offset != 0) {
|
| + snapshot_region.offset = snapshot_offset;
|
| + snapshot_region.size = snapshot_size;
|
| + }
|
| +
|
| + if (MapV8File(snapshot_pf, snapshot_region, &g_mapped_v8_context_snapshot)) {
|
| + g_opened_files.Get()[kV8ContextSnapshotFileName] =
|
| + std::make_pair(snapshot_pf, snapshot_region);
|
| + }
|
| +}
|
| +
|
| +// static
|
| +void V8Initializer::GetV8ContextSnapshotData(const char** snapshot_data_out,
|
| + int* snapshot_size_out) {
|
| + if (g_mapped_v8_context_snapshot) {
|
| + *snapshot_data_out =
|
| + reinterpret_cast<const char*>(g_mapped_v8_context_snapshot->data());
|
| + *snapshot_size_out =
|
| + static_cast<int>(g_mapped_v8_context_snapshot->length());
|
| + } else {
|
| + *snapshot_data_out = nullptr;
|
| + *snapshot_size_out = 0;
|
| + }
|
| +}
|
| +
|
| } // namespace gin
|
|
|