Chromium Code Reviews| Index: gin/v8_initializer.cc |
| diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc |
| index 0568fe77db9300785c3830a0461028d93e89b47c..66894cabf6bdca04d946ef84c343e40e5daeabce 100644 |
| --- a/gin/v8_initializer.cc |
| +++ b/gin/v8_initializer.cc |
| @@ -17,6 +17,9 @@ |
| #include "crypto/sha2.h" |
| #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| +#if defined(OS_ANDROID) |
| +#include "base/android/apk_assets.h" |
| +#endif |
| #if defined(OS_MACOSX) |
| #include "base/mac/foundation_util.h" |
| #endif // OS_MACOSX |
| @@ -42,14 +45,20 @@ const int kV8SnapshotBasePathKey = |
| #endif // OS_ANDROID |
| #endif // !OS_MACOSX |
| +#if defined(OS_ANDROID) |
| +const char kNativesFileName[] = "assets/natives_blob.bin"; |
| +const char kSnapshotFileName[] = "assets/snapshot_blob.bin"; |
| +#else |
| const char kNativesFileName[] = "natives_blob.bin"; |
| const char kSnapshotFileName[] = "snapshot_blob.bin"; |
| +#endif // defined(OS_ANDROID) |
| // Constants for snapshot loading retries taken from: |
| // https://support.microsoft.com/en-us/kb/316609. |
| const int kMaxOpenAttempts = 5; |
| const int kOpenRetryDelayMillis = 250; |
| +#if !defined(OS_ANDROID) |
| void GetV8FilePath(const char* file_name, base::FilePath* path_out) { |
| #if !defined(OS_MACOSX) |
| base::FilePath data_path; |
| @@ -64,6 +73,7 @@ void GetV8FilePath(const char* file_name, base::FilePath* path_out) { |
| #endif // !defined(OS_MACOSX) |
| DCHECK(!path_out->empty()); |
| } |
| +#endif // !defined(OS_ANDROID) |
| static bool MapV8File(base::File file, |
| base::MemoryMappedFile::Region region, |
| @@ -80,9 +90,11 @@ static bool MapV8File(base::File file, |
| return true; |
| } |
| -static bool OpenV8File(const base::FilePath& path, |
| - int flags, |
| - base::File& file) { |
| +base::File OpenV8File(const char* file_name, |
| + base::MemoryMappedFile::Region* out_region) { |
|
rmcilroy
2015/06/09 11:52:55
nit - usual convention is to suffix with "_out" ra
rmcilroy
2015/06/09 11:52:55
align arguments (or run "git cl format" :))
|
| +#if defined(OS_ANDROID) |
| + return base::File(base::android::OpenApkAsset(file_name, out_region)); |
| +#else |
| // Re-try logic here is motivated by http://crbug.com/479537 |
| // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609). |
| @@ -95,10 +107,15 @@ static bool OpenV8File(const base::FilePath& path, |
| MAX_VALUE |
| }; |
| + int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
|
rmcilroy
2015/06/09 11:52:54
nit - move flags down below with OpenV8FileResult
|
| + base::FilePath path; |
| + GetV8FilePath(file_name, &path); |
| OpenV8FileResult result = OpenV8FileResult::FAILED_IN_USE; |
| + base::File file; |
| for (int attempt = 0; attempt < kMaxOpenAttempts; attempt++) { |
| file.Initialize(path, flags); |
| if (file.IsValid()) { |
| + *out_region = base::MemoryMappedFile::Region::kWholeFile; |
| if (attempt == 0) { |
| result = OpenV8FileResult::OPENED; |
| break; |
| @@ -118,9 +135,8 @@ static bool OpenV8File(const base::FilePath& path, |
| UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result", |
| result, |
| OpenV8FileResult::MAX_VALUE); |
| - |
| - return result == OpenV8FileResult::OPENED |
| - || result == OpenV8FileResult::OPENED_RETRY; |
| + return file.Pass(); |
| +#endif // defined(OS_ANDROID) |
| } |
| #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| @@ -169,16 +185,12 @@ static LoadV8FileResult OpenMapVerify( |
| const unsigned char* fingerprint, |
| #endif |
| base::MemoryMappedFile** mmapped_file_out) { |
| - base::FilePath path; |
| - GetV8FilePath(file_name, &path); |
| + base::MemoryMappedFile::Region region; |
| - base::File file; |
| - int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| - |
| - if (!OpenV8File(path, flags, file)) |
| + base::File file(OpenV8File(file_name, ®ion)); |
| + if (!file.IsValid()) |
| return V8_LOAD_FAILED_OPEN; |
| - if (!MapV8File(file.Pass(), base::MemoryMappedFile::Region::kWholeFile, |
| - mmapped_file_out)) |
| + if (!MapV8File(file.Pass(), region, mmapped_file_out)) |
| return V8_LOAD_FAILED_MAP; |
| #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| if (!VerifyV8StartupFile(mmapped_file_out, fingerprint)) |
| @@ -197,6 +209,8 @@ void V8Initializer::LoadV8Snapshot() { |
| g_snapshot_fingerprint, |
| #endif |
| &g_mapped_snapshot); |
| + // V8 can't start up without the source of the natives, but it can |
| + // start up (slower) without the snapshot. |
|
rmcilroy
2015/06/09 11:52:54
nit - probably clearer as "Failure to load the V8
|
| UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
| V8_LOAD_MAX_VALUE); |
| } |
| @@ -270,34 +284,25 @@ void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf, |
| #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
| } |
| -// static |
| -bool V8Initializer::OpenV8FilesForChildProcesses( |
| - base::PlatformFile* natives_fd_out, |
| - base::PlatformFile* snapshot_fd_out) { |
| - base::FilePath natives_data_path; |
| - base::FilePath snapshot_data_path; |
| - GetV8FilePath(kNativesFileName, &natives_data_path); |
| - GetV8FilePath(kSnapshotFileName, &snapshot_data_path); |
| - |
| - base::File natives_data_file; |
| - base::File snapshot_data_file; |
| - int file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| - |
| - bool natives_success = |
| - OpenV8File(natives_data_path, file_flags, natives_data_file); |
| - if (natives_success) { |
| - *natives_fd_out = natives_data_file.TakePlatformFile(); |
| - } |
| - bool snapshot_success = |
| - OpenV8File(snapshot_data_path, file_flags, snapshot_data_file); |
| - if (snapshot_success) { |
| - *snapshot_fd_out = snapshot_data_file.TakePlatformFile(); |
| - } |
| - // We can start up without the snapshot file, but not without the natives. |
| - return natives_success; |
| +#if defined(OS_POSIX) |
| +V8Initializer::V8Files::V8Files() { |
|
rmcilroy
2015/06/09 11:52:54
initialize the fds to "-1"
|
| } |
| -#endif // V8_USE_EXTERNAL_STARTUP_DATA |
| +V8Initializer::V8Files::~V8Files() { |
| +} |
| + |
| +// static |
| +void V8Initializer::OpenV8FilesForChildProcesses( |
| + V8Initializer::V8Files* files_out) { |
| + base::File natives_file = |
| + OpenV8File(kNativesFileName, &files_out->natives_region); |
| + files_out->natives_fd.reset(natives_file.TakePlatformFile()); |
| + base::File snapshot_file = |
| + OpenV8File(kSnapshotFileName, &files_out->snapshot_region); |
| + files_out->snapshot_fd.reset(snapshot_file.TakePlatformFile()); |
| +} |
| +#endif // defined(OS_POSIX) |
| +#endif // defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| // static |
| void V8Initializer::Initialize(gin::IsolateHolder::ScriptMode mode) { |