Chromium Code Reviews| Index: gin/isolate_holder.cc |
| diff --git a/gin/isolate_holder.cc b/gin/isolate_holder.cc |
| index 740e13640a0ab40ebbc35df043906e96f8904b1e..34f0026f05dafe67b8d7a5fcf14e1be08f6836f2 100644 |
| --- a/gin/isolate_holder.cc |
| +++ b/gin/isolate_holder.cc |
| @@ -18,6 +18,19 @@ |
| #include "gin/public/v8_platform.h" |
| #include "gin/run_microtasks_observer.h" |
| +#ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| +#include "base/path_service.h" |
| +#endif // V8_USE_EXTERNAL_STARTUP_DATA |
| + |
| +#ifdef V8_USE_EXTERNAL_STARTUP_DATA |
|
rmcilroy
2014/10/07 16:16:11
nit - remove the double #ifdef V8_USE_EXTERNAL_STA
baixo
2014/10/08 11:28:55
Done.
|
| + |
| +#ifdef OS_MACOSX |
| +#include "base/mac/bundle_locations.h" |
| +#endif |
| + |
| +#include "base/path_service.h" |
| +#endif // V8_USE_EXTERNAL_STARTUP_DATA |
| + |
| namespace gin { |
| namespace { |
| @@ -29,8 +42,95 @@ bool GenerateEntropy(unsigned char* buffer, size_t amount) { |
| return true; |
| } |
| +#ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| +base::MemoryMappedFile* g_mapped_natives = NULL; |
| +base::MemoryMappedFile* g_mapped_snapshot = NULL; |
| +#endif |
| + |
| } // namespace |
| + |
| +#ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| +//static |
| +bool IsolateHolder::MapV8Files( |
| + base::FilePath* natives_path, base::FilePath* snapshot_path, |
| + int natives_fd, int snapshot_fd) { |
| + |
| + int flags = base::File::FLAG_OPEN | base::File::FLAG_READ; |
| + |
| + g_mapped_natives = new base::MemoryMappedFile; |
| + if (!g_mapped_natives->IsValid()) { |
| +#ifdef OS_ANDROID |
| + if (natives_fd == -1 |
| + ? !g_mapped_natives->Initialize(base::File(*natives_path, flags)) |
| + : !g_mapped_natives->Initialize(base::File(natives_fd)) |
| +#else |
| + if (!g_mapped_natives->Initialize(base::File(*natives_path, flags)) |
| +#endif |
| + ) { |
| + delete g_mapped_natives; |
| + g_mapped_natives = NULL; |
| + LOG(ERROR) << "Couldn't mmap v8 natives data file"; |
| + return false; |
| + } |
| + } |
| + |
| + g_mapped_snapshot = new base::MemoryMappedFile; |
| + if (!g_mapped_snapshot->IsValid()) { |
| +#ifdef OS_ANDROID |
| + if (snapshot_fd == -1 |
| + ? !g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags)) |
| + : !g_mapped_snapshot->Initialize(base::File(snapshot_fd)) |
| +#else |
| + if (!g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags)) |
| +#endif |
| + ) { |
| + delete g_mapped_snapshot; |
| + g_mapped_snapshot = NULL; |
| + LOG(ERROR) << "Couldn't mmap v8 snapshot data file"; |
| + return false; |
| + } |
| + } |
| + |
| + return true; |
| +} |
| + |
| +// static |
| +bool IsolateHolder::LoadV8Snapshot() { |
| + if (g_mapped_natives && g_mapped_snapshot) |
| + return true; |
| + |
| +#if !defined(OS_MACOSX) |
| + base::FilePath data_path; |
| + PathService::Get( |
| +#if defined(OS_ANDROID) |
| + base::DIR_ANDROID_APP_DATA, |
| +#elif defined(OS_POSIX) || defined(OS_WIN) |
| + base::DIR_EXE, |
| +#endif |
| + &data_path); |
| +#else |
| + base::FilePath data_path = base::mac::FrameworkBundlePath(); |
| +#endif |
| + DCHECK(!data_path.empty()); |
| + |
| + base::FilePath natives_path = data_path.AppendASCII("natives_blob.bin"); |
| + base::FilePath snapshot_path = data_path.AppendASCII("snapshot_blob.bin"); |
| + |
| + return IsolateHolder::MapV8Files(&natives_path, &snapshot_path); |
| +} |
| +#endif // V8_USE_EXTERNAL_STARTUP_DATA |
| + |
| +#if defined(V8_USE_EXTERNAL_STARTUP_DATA) && defined(OS_ANDROID) |
| +//static |
| +bool IsolateHolder::LoadV8SnapshotFD(int natives_fd, int snapshot_fd) { |
| + if (g_mapped_natives && g_mapped_snapshot) |
| + return true; |
| + |
| + return IsolateHolder::MapV8Files(NULL, NULL, natives_fd, snapshot_fd); |
| +} |
| +#endif // V8_USE_EXTERNAL_STARTUP_DATA && OS_ANDROID |
| + |
| IsolateHolder::IsolateHolder() { |
| CHECK(g_array_buffer_allocator) |
| << "You need to invoke gin::IsolateHolder::Initialize first"; |
| @@ -88,6 +188,19 @@ void IsolateHolder::Initialize(ScriptMode mode, |
| v8::V8::SetFlagsFromString(v8_flags, sizeof(v8_flags) - 1); |
| } |
| v8::V8::SetEntropySource(&GenerateEntropy); |
| +#ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| + v8::StartupData natives; |
| + natives.data = reinterpret_cast<const char*>(g_mapped_natives->data()); |
| + natives.raw_size = g_mapped_natives->length(); |
| + natives.compressed_size = g_mapped_natives->length(); |
| + v8::V8::SetNativesDataBlob(&natives); |
| + |
| + v8::StartupData snapshot; |
| + snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data()); |
| + snapshot.raw_size = g_mapped_snapshot->length(); |
| + snapshot.compressed_size = g_mapped_snapshot->length(); |
| + v8::V8::SetSnapshotDataBlob(&snapshot); |
| +#endif // V8_USE_EXTERNAL_STARTUP_DATA |
| v8::V8::Initialize(); |
| v8_is_initialized = true; |
| } |