Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "gin/v8_initializer.h" | 5 #include "gin/v8_initializer.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/files/file.h" | 8 #include "base/files/file.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/memory_mapped_file.h" | 10 #include "base/files/memory_mapped_file.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 14 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "crypto/sha2.h" | 17 #include "crypto/sha2.h" |
| 18 | 18 |
| 19 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | 19 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 20 #if defined(OS_ANDROID) | |
| 21 #include "base/android/apk_assets.h" | |
| 22 #endif | |
| 20 #if defined(OS_MACOSX) | 23 #if defined(OS_MACOSX) |
| 21 #include "base/mac/foundation_util.h" | 24 #include "base/mac/foundation_util.h" |
| 22 #endif // OS_MACOSX | 25 #endif // OS_MACOSX |
| 23 #include "base/path_service.h" | 26 #include "base/path_service.h" |
| 24 #endif // V8_USE_EXTERNAL_STARTUP_DATA | 27 #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 25 | 28 |
| 26 namespace gin { | 29 namespace gin { |
| 27 | 30 |
| 28 namespace { | 31 namespace { |
| 29 base::MemoryMappedFile* g_mapped_natives = nullptr; | 32 base::MemoryMappedFile* g_mapped_natives = nullptr; |
| 30 base::MemoryMappedFile* g_mapped_snapshot = nullptr; | 33 base::MemoryMappedFile* g_mapped_snapshot = nullptr; |
| 31 | 34 |
| 32 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) | 35 #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 33 base::File* g_natives_file = nullptr; | 36 base::File* g_natives_file = nullptr; |
| 34 base::File* g_snapshot_file = nullptr; | 37 base::File* g_snapshot_file = nullptr; |
| 35 base::MemoryMappedFile::Region g_natives_region; | 38 base::MemoryMappedFile::Region g_natives_region; |
| 36 base::MemoryMappedFile::Region g_snapshot_region; | 39 base::MemoryMappedFile::Region g_snapshot_region; |
| 37 | 40 |
| 38 #if !defined(OS_MACOSX) | 41 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 39 const int kV8SnapshotBasePathKey = | 42 const int kV8SnapshotBasePathKey = |
| 40 #if defined(OS_ANDROID) | 43 #if defined(OS_POSIX) |
| 41 base::DIR_ANDROID_APP_DATA; | |
| 42 #elif defined(OS_POSIX) | |
| 43 base::DIR_EXE; | 44 base::DIR_EXE; |
| 44 #elif defined(OS_WIN) | 45 #elif defined(OS_WIN) |
| 45 base::DIR_MODULE; | 46 base::DIR_MODULE; |
| 46 #endif // OS_ANDROID | 47 #endif // defined(OS_POSIX) |
| 47 #endif // !OS_MACOSX | 48 #endif // !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 48 | 49 |
| 50 #if defined(OS_ANDROID) | |
| 51 const char kNativesFileName[] = "assets/natives_blob.bin"; | |
| 52 const char kSnapshotFileName[] = "assets/snapshot_blob.bin"; | |
|
rmcilroy
2015/06/15 10:21:02
this is effective the file path (within the APK) n
agrieve
2015/06/15 14:22:04
Done.
| |
| 53 #else | |
| 49 const char kNativesFileName[] = "natives_blob.bin"; | 54 const char kNativesFileName[] = "natives_blob.bin"; |
| 50 const char kSnapshotFileName[] = "snapshot_blob.bin"; | 55 const char kSnapshotFileName[] = "snapshot_blob.bin"; |
| 56 #endif // defined(OS_ANDROID) | |
| 51 | 57 |
| 52 // Constants for snapshot loading retries taken from: | 58 #if !defined(OS_ANDROID) |
| 53 // https://support.microsoft.com/en-us/kb/316609. | |
| 54 const int kMaxOpenAttempts = 5; | |
| 55 const int kOpenRetryDelayMillis = 250; | |
| 56 | |
| 57 void GetV8FilePath(const char* file_name, base::FilePath* path_out) { | 59 void GetV8FilePath(const char* file_name, base::FilePath* path_out) { |
| 58 #if !defined(OS_MACOSX) | 60 #if !defined(OS_MACOSX) |
| 59 base::FilePath data_path; | 61 base::FilePath data_path; |
| 60 PathService::Get(kV8SnapshotBasePathKey, &data_path); | 62 PathService::Get(kV8SnapshotBasePathKey, &data_path); |
| 61 DCHECK(!data_path.empty()); | 63 DCHECK(!data_path.empty()); |
| 62 | 64 |
| 63 *path_out = data_path.AppendASCII(file_name); | 65 *path_out = data_path.AppendASCII(file_name); |
| 64 #else // !defined(OS_MACOSX) | 66 #else // !defined(OS_MACOSX) |
| 65 base::ScopedCFTypeRef<CFStringRef> natives_file_name( | 67 base::ScopedCFTypeRef<CFStringRef> natives_file_name( |
| 66 base::SysUTF8ToCFStringRef(file_name)); | 68 base::SysUTF8ToCFStringRef(file_name)); |
| 67 *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name); | 69 *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name); |
| 68 #endif // !defined(OS_MACOSX) | 70 #endif // !defined(OS_MACOSX) |
| 69 DCHECK(!path_out->empty()); | 71 DCHECK(!path_out->empty()); |
| 70 } | 72 } |
| 73 #endif // !defined(OS_ANDROID) | |
| 71 | 74 |
| 72 static bool MapV8File(base::File file, | 75 static bool MapV8File(base::File file, |
| 73 base::MemoryMappedFile::Region region, | 76 base::MemoryMappedFile::Region region, |
| 74 base::MemoryMappedFile** mmapped_file_out) { | 77 base::MemoryMappedFile** mmapped_file_out) { |
| 75 DCHECK(*mmapped_file_out == NULL); | 78 DCHECK(*mmapped_file_out == NULL); |
| 76 base::MemoryMappedFile* mmapped_file = *mmapped_file_out = | 79 base::MemoryMappedFile* mmapped_file = *mmapped_file_out = |
| 77 new base::MemoryMappedFile; | 80 new base::MemoryMappedFile; |
| 78 if (!mmapped_file->Initialize(file.Pass(), region)) { | 81 if (!mmapped_file->Initialize(file.Pass(), region)) { |
| 79 delete mmapped_file; | 82 delete mmapped_file; |
| 80 *mmapped_file_out = NULL; | 83 *mmapped_file_out = NULL; |
| 81 return false; | 84 return false; |
| 82 } | 85 } |
| 83 | 86 |
| 84 return true; | 87 return true; |
| 85 } | 88 } |
| 86 | 89 |
| 87 base::File OpenV8File(const char* file_name, | 90 base::File OpenV8File(const char* file_name, |
| 88 base::MemoryMappedFile::Region* region_out) { | 91 base::MemoryMappedFile::Region* region_out) { |
| 89 // Re-try logic here is motivated by http://crbug.com/479537 | |
| 90 // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609). | |
| 91 | |
| 92 // These match tools/metrics/histograms.xml | 92 // These match tools/metrics/histograms.xml |
| 93 enum OpenV8FileResult { | 93 enum OpenV8FileResult { |
| 94 OPENED = 0, | 94 OPENED = 0, |
| 95 OPENED_RETRY, | 95 OPENED_RETRY, |
| 96 FAILED_IN_USE, | 96 FAILED_IN_USE, |
| 97 FAILED_OTHER, | 97 FAILED_OTHER, |
| 98 MAX_VALUE | 98 MAX_VALUE |
| 99 }; | 99 }; |
| 100 #if defined(OS_ANDROID) | |
| 101 base::File file(base::android::OpenApkAsset(file_name, region_out)); | |
| 102 OpenV8FileResult result = file.IsValid() ? OpenV8FileResult::OPENED | |
| 103 : OpenV8FileResult::FAILED_OTHER; | |
| 104 #else | |
| 105 // Re-try logic here is motivated by http://crbug.com/479537 | |
| 106 // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609). | |
| 107 const int kMaxOpenAttempts = 5; | |
| 108 const int kOpenRetryDelayMillis = 250; | |
| 100 | 109 |
| 101 base::FilePath path; | 110 base::FilePath path; |
| 102 GetV8FilePath(file_name, &path); | 111 GetV8FilePath(file_name, &path); |
| 103 | 112 |
| 104 OpenV8FileResult result = OpenV8FileResult::FAILED_IN_USE; | 113 OpenV8FileResult result = OpenV8FileResult::FAILED_IN_USE; |
| 105 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; | 114 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 106 base::File file; | 115 base::File file; |
| 107 for (int attempt = 0; attempt < kMaxOpenAttempts; attempt++) { | 116 for (int attempt = 0; attempt < kMaxOpenAttempts; attempt++) { |
| 108 file.Initialize(path, flags); | 117 file.Initialize(path, flags); |
| 109 if (file.IsValid()) { | 118 if (file.IsValid()) { |
| 110 *region_out = base::MemoryMappedFile::Region::kWholeFile; | 119 *region_out = base::MemoryMappedFile::Region::kWholeFile; |
| 111 if (attempt == 0) { | 120 if (attempt == 0) { |
| 112 result = OpenV8FileResult::OPENED; | 121 result = OpenV8FileResult::OPENED; |
| 113 break; | 122 break; |
| 114 } else { | 123 } else { |
| 115 result = OpenV8FileResult::OPENED_RETRY; | 124 result = OpenV8FileResult::OPENED_RETRY; |
| 116 break; | 125 break; |
| 117 } | 126 } |
| 118 } else if (file.error_details() != base::File::FILE_ERROR_IN_USE) { | 127 } else if (file.error_details() != base::File::FILE_ERROR_IN_USE) { |
| 119 result = OpenV8FileResult::FAILED_OTHER; | 128 result = OpenV8FileResult::FAILED_OTHER; |
| 120 break; | 129 break; |
| 121 } else if (kMaxOpenAttempts - 1 != attempt) { | 130 } else if (kMaxOpenAttempts - 1 != attempt) { |
| 122 base::PlatformThread::Sleep( | 131 base::PlatformThread::Sleep( |
| 123 base::TimeDelta::FromMilliseconds(kOpenRetryDelayMillis)); | 132 base::TimeDelta::FromMilliseconds(kOpenRetryDelayMillis)); |
| 124 } | 133 } |
| 125 } | 134 } |
| 135 #endif // defined(OS_ANDROID) | |
| 126 | 136 |
| 127 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result", | 137 UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result", |
| 128 result, | 138 result, |
| 129 OpenV8FileResult::MAX_VALUE); | 139 OpenV8FileResult::MAX_VALUE); |
| 130 return file.Pass(); | 140 return file.Pass(); |
| 131 } | 141 } |
| 132 | 142 |
| 133 void OpenNativesFileIfNecessary() { | 143 void OpenNativesFileIfNecessary() { |
| 134 if (g_natives_file == nullptr) { | 144 if (g_natives_file == nullptr) { |
| 135 g_natives_file = | 145 g_natives_file = |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 *snapshot_data_out = | 366 *snapshot_data_out = |
| 357 reinterpret_cast<const char*>(g_mapped_snapshot->data()); | 367 reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| 358 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); | 368 *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); |
| 359 } else { | 369 } else { |
| 360 *snapshot_data_out = NULL; | 370 *snapshot_data_out = NULL; |
| 361 *snapshot_size_out = 0; | 371 *snapshot_size_out = 0; |
| 362 } | 372 } |
| 363 } | 373 } |
| 364 | 374 |
| 365 } // namespace gin | 375 } // namespace gin |
| OLD | NEW |