oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "gin/v8_initializer.h" |
| 6 | |
| 7 | #include "base/basictypes.h" |
| 8 | #include "base/files/file.h" |
| 9 | #include "base/files/file_path.h" |
| 10 | #include "base/files/memory_mapped_file.h" |
| 11 | #include "base/logging.h" |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 12 | #include "base/memory/scoped_ptr.h" |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 13 | #include "base/metrics/histogram.h" |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 14 | #include "base/rand_util.h" |
| 15 | #include "base/strings/sys_string_conversions.h" |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 16 | #include "base/threading/platform_thread.h" |
| 17 | #include "base/time/time.h" |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 18 | #include "crypto/sha2.h" |
| 19 | |
| 20 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 21 | #if defined(OS_MACOSX) |
| 22 | #include "base/mac/foundation_util.h" |
| 23 | #endif // OS_MACOSX |
| 24 | #include "base/path_service.h" |
| 25 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 26 | |
| 27 | namespace gin { |
| 28 | |
| 29 | namespace { |
| 30 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 31 | // None of these globals are ever freed nor closed. |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 32 | base::MemoryMappedFile* g_mapped_natives = nullptr; |
| 33 | base::MemoryMappedFile* g_mapped_snapshot = nullptr; |
| 34 | |
| 35 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 36 | |
| 37 | const base::PlatformFile kInvalidPlatformFile = |
| 38 | #if defined(OS_WIN) |
| 39 | INVALID_HANDLE_VALUE; |
| 40 | #else |
| 41 | -1; |
| 42 | #endif |
| 43 | |
| 44 | // File handles intentionally never closed. Not using File here because its |
| 45 | // Windows implementation guards against two instances owning the same |
| 46 | // PlatformFile (which we allow since we know it is never freed). |
| 47 | base::PlatformFile g_natives_pf = kInvalidPlatformFile; |
| 48 | base::PlatformFile g_snapshot_pf = kInvalidPlatformFile; |
| 49 | base::MemoryMappedFile::Region g_natives_region; |
| 50 | base::MemoryMappedFile::Region g_snapshot_region; |
| 51 | |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 52 | #if !defined(OS_MACOSX) |
| 53 | const int kV8SnapshotBasePathKey = |
| 54 | #if defined(OS_ANDROID) |
| 55 | base::DIR_ANDROID_APP_DATA; |
| 56 | #elif defined(OS_POSIX) |
| 57 | base::DIR_EXE; |
| 58 | #elif defined(OS_WIN) |
| 59 | base::DIR_MODULE; |
| 60 | #endif // OS_ANDROID |
| 61 | #endif // !OS_MACOSX |
| 62 | |
| 63 | const char kNativesFileName[] = "natives_blob.bin"; |
| 64 | const char kSnapshotFileName[] = "snapshot_blob.bin"; |
| 65 | |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 66 | // Constants for snapshot loading retries taken from: |
| 67 | // https://support.microsoft.com/en-us/kb/316609. |
| 68 | const int kMaxOpenAttempts = 5; |
| 69 | const int kOpenRetryDelayMillis = 250; |
| 70 | |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 71 | void GetV8FilePath(const char* file_name, base::FilePath* path_out) { |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 72 | #if !defined(OS_MACOSX) |
| 73 | base::FilePath data_path; |
| 74 | PathService::Get(kV8SnapshotBasePathKey, &data_path); |
| 75 | DCHECK(!data_path.empty()); |
| 76 | |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 77 | *path_out = data_path.AppendASCII(file_name); |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 78 | #else // !defined(OS_MACOSX) |
| 79 | base::ScopedCFTypeRef<CFStringRef> natives_file_name( |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 80 | base::SysUTF8ToCFStringRef(file_name)); |
| 81 | *path_out = base::mac::PathForFrameworkBundleResource(natives_file_name); |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 82 | #endif // !defined(OS_MACOSX) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 83 | DCHECK(!path_out->empty()); |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 84 | } |
| 85 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 86 | static bool MapV8File(base::PlatformFile platform_file, |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 87 | base::MemoryMappedFile::Region region, |
| 88 | base::MemoryMappedFile** mmapped_file_out) { |
| 89 | DCHECK(*mmapped_file_out == NULL); |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 90 | scoped_ptr<base::MemoryMappedFile> mmapped_file(new base::MemoryMappedFile()); |
| 91 | if (mmapped_file->Initialize(base::File(platform_file), region)) { |
| 92 | *mmapped_file_out = mmapped_file.release(); |
| 93 | return true; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 94 | } |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 95 | return false; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 96 | } |
| 97 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 98 | base::PlatformFile OpenV8File(const char* file_name, |
| 99 | base::MemoryMappedFile::Region* region_out) { |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 100 | // Re-try logic here is motivated by http://crbug.com/479537 |
| 101 | // for A/V on Windows (https://support.microsoft.com/en-us/kb/316609). |
| 102 | |
| 103 | // These match tools/metrics/histograms.xml |
| 104 | enum OpenV8FileResult { |
| 105 | OPENED = 0, |
| 106 | OPENED_RETRY, |
| 107 | FAILED_IN_USE, |
| 108 | FAILED_OTHER, |
| 109 | MAX_VALUE |
| 110 | }; |
| 111 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 112 | base::FilePath path; |
| 113 | GetV8FilePath(file_name, &path); |
| 114 | |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 115 | OpenV8FileResult result = OpenV8FileResult::FAILED_IN_USE; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 116 | int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| 117 | base::File file; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 118 | for (int attempt = 0; attempt < kMaxOpenAttempts; attempt++) { |
| 119 | file.Initialize(path, flags); |
| 120 | if (file.IsValid()) { |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 121 | *region_out = base::MemoryMappedFile::Region::kWholeFile; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 122 | if (attempt == 0) { |
| 123 | result = OpenV8FileResult::OPENED; |
| 124 | break; |
| 125 | } else { |
| 126 | result = OpenV8FileResult::OPENED_RETRY; |
| 127 | break; |
| 128 | } |
| 129 | } else if (file.error_details() != base::File::FILE_ERROR_IN_USE) { |
| 130 | result = OpenV8FileResult::FAILED_OTHER; |
| 131 | break; |
| 132 | } else if (kMaxOpenAttempts - 1 != attempt) { |
| 133 | base::PlatformThread::Sleep( |
| 134 | base::TimeDelta::FromMilliseconds(kOpenRetryDelayMillis)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | UMA_HISTOGRAM_ENUMERATION("V8.Initializer.OpenV8File.Result", |
| 139 | result, |
| 140 | OpenV8FileResult::MAX_VALUE); |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 141 | return file.TakePlatformFile(); |
| 142 | } |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 143 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 144 | void OpenNativesFileIfNecessary() { |
| 145 | if (g_natives_pf == kInvalidPlatformFile) { |
| 146 | g_natives_pf = OpenV8File(kNativesFileName, &g_natives_region); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void OpenSnapshotFileIfNecessary() { |
| 151 | if (g_snapshot_pf == kInvalidPlatformFile) { |
| 152 | g_snapshot_pf = OpenV8File(kSnapshotFileName, &g_snapshot_region); |
| 153 | } |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 154 | } |
| 155 | |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 156 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 157 | bool VerifyV8StartupFile(base::MemoryMappedFile** file, |
| 158 | const unsigned char* fingerprint) { |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 159 | unsigned char output[crypto::kSHA256Length]; |
| 160 | crypto::SHA256HashString( |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 161 | base::StringPiece(reinterpret_cast<const char*>((*file)->data()), |
| 162 | (*file)->length()), |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 163 | output, sizeof(output)); |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 164 | if (!memcmp(fingerprint, output, sizeof(output))) { |
| 165 | return true; |
| 166 | } |
| 167 | delete *file; |
| 168 | *file = NULL; |
| 169 | return false; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 170 | } |
| 171 | #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
| 172 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 173 | |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 174 | bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
| 175 | base::RandBytes(buffer, amount); |
| 176 | return true; |
| 177 | } |
| 178 | |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 179 | } // namespace |
| 180 | |
| 181 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 182 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 183 | // Defined in gen/gin/v8_snapshot_fingerprint.cc |
| 184 | extern const unsigned char g_natives_fingerprint[]; |
| 185 | extern const unsigned char g_snapshot_fingerprint[]; |
| 186 | #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
| 187 | |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 188 | enum LoadV8FileResult { |
| 189 | V8_LOAD_SUCCESS = 0, |
| 190 | V8_LOAD_FAILED_OPEN, |
| 191 | V8_LOAD_FAILED_MAP, |
| 192 | V8_LOAD_FAILED_VERIFY, |
| 193 | V8_LOAD_MAX_VALUE |
| 194 | }; |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 195 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 196 | static LoadV8FileResult MapVerify(base::PlatformFile platform_file, |
| 197 | const base::MemoryMappedFile::Region& region, |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 198 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 199 | const unsigned char* fingerprint, |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 200 | #endif |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 201 | base::MemoryMappedFile** mmapped_file_out) { |
| 202 | if (platform_file == kInvalidPlatformFile) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 203 | return V8_LOAD_FAILED_OPEN; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 204 | if (!MapV8File(platform_file, region, mmapped_file_out)) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 205 | return V8_LOAD_FAILED_MAP; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 206 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 207 | if (!VerifyV8StartupFile(mmapped_file_out, fingerprint)) |
| 208 | return V8_LOAD_FAILED_VERIFY; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 209 | #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 210 | return V8_LOAD_SUCCESS; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // static |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 214 | void V8Initializer::LoadV8Snapshot() { |
| 215 | if (g_mapped_snapshot) |
| 216 | return; |
| 217 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 218 | OpenSnapshotFileIfNecessary(); |
| 219 | LoadV8FileResult result = MapVerify(g_snapshot_pf, g_snapshot_region, |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 220 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 221 | g_snapshot_fingerprint, |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 222 | #endif |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 223 | &g_mapped_snapshot); |
| 224 | // V8 can't start up without the source of the natives, but it can |
| 225 | // start up (slower) without the snapshot. |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 226 | UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
| 227 | V8_LOAD_MAX_VALUE); |
| 228 | } |
| 229 | |
| 230 | void V8Initializer::LoadV8Natives() { |
| 231 | if (g_mapped_natives) |
| 232 | return; |
| 233 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 234 | OpenNativesFileIfNecessary(); |
| 235 | LoadV8FileResult result = MapVerify(g_natives_pf, g_natives_region, |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 236 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 237 | g_natives_fingerprint, |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 238 | #endif |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 239 | &g_mapped_natives); |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 240 | if (result != V8_LOAD_SUCCESS) { |
| 241 | LOG(FATAL) << "Couldn't mmap v8 natives data file, status code is " |
| 242 | << static_cast<int>(result); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | // static |
| 247 | void V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile snapshot_pf, |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 248 | int64 snapshot_offset, |
| 249 | int64 snapshot_size) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 250 | if (g_mapped_snapshot) |
| 251 | return; |
| 252 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 253 | if (snapshot_pf == kInvalidPlatformFile) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 254 | return; |
| 255 | |
| 256 | base::MemoryMappedFile::Region snapshot_region = |
| 257 | base::MemoryMappedFile::Region::kWholeFile; |
| 258 | if (snapshot_size != 0 || snapshot_offset != 0) { |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 259 | snapshot_region.offset = snapshot_offset; |
| 260 | snapshot_region.size = snapshot_size; |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | LoadV8FileResult result = V8_LOAD_SUCCESS; |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 264 | if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_snapshot)) |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 265 | result = V8_LOAD_FAILED_MAP; |
| 266 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 267 | if (!VerifyV8StartupFile(&g_mapped_snapshot, g_snapshot_fingerprint)) |
| 268 | result = V8_LOAD_FAILED_VERIFY; |
| 269 | #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
| 270 | UMA_HISTOGRAM_ENUMERATION("V8.Initializer.LoadV8Snapshot.Result", result, |
| 271 | V8_LOAD_MAX_VALUE); |
| 272 | } |
| 273 | |
| 274 | // static |
| 275 | void V8Initializer::LoadV8NativesFromFD(base::PlatformFile natives_pf, |
| 276 | int64 natives_offset, |
| 277 | int64 natives_size) { |
| 278 | if (g_mapped_natives) |
| 279 | return; |
| 280 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 281 | CHECK_NE(natives_pf, kInvalidPlatformFile); |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 282 | |
| 283 | base::MemoryMappedFile::Region natives_region = |
| 284 | base::MemoryMappedFile::Region::kWholeFile; |
| 285 | if (natives_size != 0 || natives_offset != 0) { |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 286 | natives_region.offset = natives_offset; |
| 287 | natives_region.size = natives_size; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 288 | } |
| 289 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 290 | if (!MapV8File(natives_pf, natives_region, &g_mapped_natives)) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 291 | LOG(FATAL) << "Couldn't mmap v8 natives data file"; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 292 | } |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 293 | #if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA) |
| 294 | if (!VerifyV8StartupFile(&g_mapped_natives, g_natives_fingerprint)) { |
| 295 | LOG(FATAL) << "Couldn't verify contents of v8 natives data file"; |
| 296 | } |
| 297 | #endif // V8_VERIFY_EXTERNAL_STARTUP_DATA |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 298 | } |
| 299 | |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 300 | // static |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 301 | base::PlatformFile V8Initializer::GetOpenNativesFileForChildProcesses( |
| 302 | base::MemoryMappedFile::Region* region_out) { |
| 303 | OpenNativesFileIfNecessary(); |
| 304 | *region_out = g_natives_region; |
| 305 | return g_natives_pf; |
rmcilroy | 54fab5e | 2015-04-06 21:14:58 | [diff] [blame] | 306 | } |
| 307 | |
agrieve | fd2d44ab | 2015-06-19 04:33:03 | [diff] [blame^] | 308 | // static |
| 309 | base::PlatformFile V8Initializer::GetOpenSnapshotFileForChildProcesses( |
| 310 | base::MemoryMappedFile::Region* region_out) { |
| 311 | OpenSnapshotFileIfNecessary(); |
| 312 | *region_out = g_snapshot_region; |
| 313 | return g_snapshot_pf; |
| 314 | } |
| 315 | #endif // defined(V8_USE_EXTERNAL_STARTUP_DATA) |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 316 | |
| 317 | // static |
jochen | a0b121b | 2015-04-30 12:56:27 | [diff] [blame] | 318 | void V8Initializer::Initialize(gin::IsolateHolder::ScriptMode mode) { |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 319 | static bool v8_is_initialized = false; |
| 320 | if (v8_is_initialized) |
| 321 | return; |
| 322 | |
| 323 | v8::V8::InitializePlatform(V8Platform::Get()); |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 324 | |
| 325 | if (gin::IsolateHolder::kStrictMode == mode) { |
| 326 | static const char use_strict[] = "--use_strict"; |
| 327 | v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1); |
| 328 | } |
| 329 | |
| 330 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 331 | v8::StartupData natives; |
| 332 | natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); |
| 333 | natives.raw_size = static_cast<int>(g_mapped_natives->length()); |
| 334 | v8::V8::SetNativesDataBlob(&natives); |
| 335 | |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 336 | if (g_mapped_snapshot != NULL) { |
| 337 | v8::StartupData snapshot; |
| 338 | snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| 339 | snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length()); |
| 340 | v8::V8::SetSnapshotDataBlob(&snapshot); |
| 341 | } |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 342 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 343 | |
| 344 | v8::V8::SetEntropySource(&GenerateEntropy); |
| 345 | v8::V8::Initialize(); |
| 346 | |
| 347 | v8_is_initialized = true; |
| 348 | } |
| 349 | |
| 350 | // static |
| 351 | void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out, |
| 352 | int* natives_size_out, |
| 353 | const char** snapshot_data_out, |
| 354 | int* snapshot_size_out) { |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 355 | if (g_mapped_natives) { |
| 356 | *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data()); |
| 357 | *natives_size_out = static_cast<int>(g_mapped_natives->length()); |
| 358 | } else { |
| 359 | *natives_data_out = NULL; |
| 360 | *natives_size_out = 0; |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 361 | } |
erikcorry | c94eff1 | 2015-06-08 11:29:16 | [diff] [blame] | 362 | if (g_mapped_snapshot) { |
| 363 | *snapshot_data_out = |
| 364 | reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| 365 | *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length()); |
| 366 | } else { |
| 367 | *snapshot_data_out = NULL; |
| 368 | *snapshot_size_out = 0; |
| 369 | } |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | } // namespace gin |