Chromium Code Reviews| Index: gin/v8_initializer.cc |
| diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc |
| index 726d7c043a21a81b9ee2f434a8a6706d7c2de57e..f6b94d6a1b35fd6c72eb18ef54acfc6bf77c79ca 100644 |
| --- a/gin/v8_initializer.cc |
| +++ b/gin/v8_initializer.cc |
| @@ -33,8 +33,8 @@ |
| #if defined(OS_MACOSX) |
| #include "base/mac/foundation_util.h" |
| #endif // OS_MACOSX |
| -#include "base/path_service.h" |
| #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| +#include "base/path_service.h" |
|
Yuki
2017/05/30 14:35:56
Should this #include be moved up?
peria
2017/06/01 08:33:32
Done.
|
| namespace gin { |
| @@ -43,16 +43,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_context = nullptr; |
| -#if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| +const char kV8ContextFileName[] = "context_blob.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) { |
| @@ -64,6 +65,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) |
| @@ -80,6 +83,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; |
| @@ -99,12 +104,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()); |
| @@ -182,8 +186,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); |
| @@ -191,19 +194,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, |
| @@ -212,9 +207,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)) |
| @@ -224,6 +218,8 @@ static LoadV8FileResult MapOpenedFile( |
| } // namespace |
| +#if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| + |
| // static |
| void V8Initializer::LoadV8Snapshot() { |
| if (g_mapped_snapshot) |
| @@ -388,7 +384,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()); |
| @@ -424,4 +420,47 @@ void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out, |
| } |
| } |
| +// static |
| +void V8Initializer::LoadV8Context() { |
| + if (g_mapped_context) |
| + return; |
| + |
| + OpenFileIfNecessary(kV8ContextFileName); |
| + MapOpenedFile(GetOpenedFile(kV8ContextFileName), &g_mapped_context); |
| +} |
| + |
| +// static |
| +void V8Initializer::LoadV8ContextFromFD(base::PlatformFile snapshot_pf, |
| + int64_t snapshot_offset, |
| + int64_t snapshot_size) { |
| + if (g_mapped_context) |
| + 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_context)) { |
| + g_opened_files.Get()[kV8ContextFileName] = |
| + std::make_pair(snapshot_pf, snapshot_region); |
| + } |
| +} |
| + |
| +// static |
| +void V8Initializer::GetV8ContextData(const char** snapshot_data_out, |
| + int* snapshot_size_out) { |
| + if (g_mapped_context) { |
| + *snapshot_data_out = |
| + reinterpret_cast<const char*>(g_mapped_context->data()); |
| + *snapshot_size_out = static_cast<int>(g_mapped_context->length()); |
| + } else { |
| + *snapshot_data_out = nullptr; |
| + *snapshot_size_out = 0; |
| + } |
| +} |
| + |
| } // namespace gin |