summaryrefslogtreecommitdiffstats
path: root/gin
diff options
context:
space:
mode:
Diffstat (limited to 'gin')
-rw-r--r--gin/BUILD.gn2
-rw-r--r--gin/gin.gyp2
-rw-r--r--gin/isolate_holder.cc233
-rw-r--r--gin/public/isolate_holder.h26
-rw-r--r--gin/shell/gin_main.cc3
-rw-r--r--gin/shell_runner_unittest.cc3
-rw-r--r--gin/test/file_runner.cc8
-rw-r--r--gin/test/v8_test.cc8
-rw-r--r--gin/v8_initializer.cc221
-rw-r--r--gin/v8_initializer.h57
10 files changed, 316 insertions, 247 deletions
diff --git a/gin/BUILD.gn b/gin/BUILD.gn
index 5446d56..9fe5a12 100644
--- a/gin/BUILD.gn
+++ b/gin/BUILD.gn
@@ -56,6 +56,8 @@ component("gin") {
"shell_runner.h",
"try_catch.cc",
"try_catch.h",
+ "v8_initializer.cc",
+ "v8_initializer.h",
"v8_platform.cc",
"wrappable.cc",
"wrappable.h",
diff --git a/gin/gin.gyp b/gin/gin.gyp
index 096c120..46afc97 100644
--- a/gin/gin.gyp
+++ b/gin/gin.gyp
@@ -73,6 +73,8 @@
'shell_runner.h',
'try_catch.cc',
'try_catch.h',
+ 'v8_initializer.h',
+ 'v8_initializer.cc',
'v8_platform.cc',
'wrappable.cc',
'wrappable.h',
diff --git a/gin/isolate_holder.cc b/gin/isolate_holder.cc
index 317b582..3cc3238 100644
--- a/gin/isolate_holder.cc
+++ b/gin/isolate_holder.cc
@@ -7,214 +7,24 @@
#include <stdlib.h>
#include <string.h>
-#include "base/files/memory_mapped_file.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
-#include "base/rand_util.h"
-#include "base/strings/sys_string_conversions.h"
#include "base/sys_info.h"
-#include "crypto/sha2.h"
-#include "gin/array_buffer.h"
#include "gin/debug_impl.h"
#include "gin/function_template.h"
#include "gin/per_isolate_data.h"
-#include "gin/public/v8_platform.h"
#include "gin/run_microtasks_observer.h"
-
-#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
-#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 "gin/v8_initializer.h"
namespace gin {
namespace {
-
-v8::ArrayBuffer::Allocator* g_array_buffer_allocator = NULL;
-
-bool GenerateEntropy(unsigned char* buffer, size_t amount) {
- base::RandBytes(buffer, amount);
- return true;
-}
-
-base::MemoryMappedFile* g_mapped_natives = NULL;
-base::MemoryMappedFile* g_mapped_snapshot = NULL;
-
-#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
-bool MapV8Files(base::FilePath* natives_path,
- base::FilePath* snapshot_path,
- int natives_fd = -1,
- int snapshot_fd = -1,
- base::MemoryMappedFile::Region natives_region =
- base::MemoryMappedFile::Region::kWholeFile,
- base::MemoryMappedFile::Region snapshot_region =
- base::MemoryMappedFile::Region::kWholeFile) {
- int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
-
- g_mapped_natives = new base::MemoryMappedFile;
- if (!g_mapped_natives->IsValid()) {
-#if !defined(OS_WIN)
- if (natives_fd == -1
- ? !g_mapped_natives->Initialize(base::File(*natives_path, flags),
- natives_region)
- : !g_mapped_natives->Initialize(base::File(natives_fd),
- natives_region)) {
-#else
- if (!g_mapped_natives->Initialize(base::File(*natives_path, flags),
- natives_region)) {
-#endif // !OS_WIN
- delete g_mapped_natives;
- g_mapped_natives = NULL;
- LOG(FATAL) << "Couldn't mmap v8 natives data file";
- return false;
- }
- }
-
- g_mapped_snapshot = new base::MemoryMappedFile;
- if (!g_mapped_snapshot->IsValid()) {
-#if !defined(OS_WIN)
- if (snapshot_fd == -1
- ? !g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags),
- snapshot_region)
- : !g_mapped_snapshot->Initialize(base::File(snapshot_fd),
- snapshot_region)) {
-#else
- if (!g_mapped_snapshot->Initialize(base::File(*snapshot_path, flags),
- snapshot_region)) {
-#endif // !OS_WIN
- delete g_mapped_snapshot;
- g_mapped_snapshot = NULL;
- LOG(ERROR) << "Couldn't mmap v8 snapshot data file";
- return false;
- }
- }
-
- return true;
-}
-
-#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
-bool VerifyV8SnapshotFile(base::MemoryMappedFile* snapshot_file,
- const unsigned char* fingerprint) {
- unsigned char output[crypto::kSHA256Length];
- crypto::SHA256HashString(
- base::StringPiece(reinterpret_cast<const char*>(snapshot_file->data()),
- snapshot_file->length()),
- output, sizeof(output));
- return !memcmp(fingerprint, output, sizeof(output));
-}
-#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
-#endif // V8_USE_EXTERNAL_STARTUP_DATA
-
+v8::ArrayBuffer::Allocator* g_array_buffer_allocator = nullptr;
} // namespace
-#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
-
-#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
-// Defined in gen/gin/v8_snapshot_fingerprint.cc
-extern const unsigned char g_natives_fingerprint[];
-extern const unsigned char g_snapshot_fingerprint[];
-#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
-
-#if !defined(OS_MACOSX)
-const int IsolateHolder::kV8SnapshotBasePathKey =
-#if defined(OS_ANDROID)
- base::DIR_ANDROID_APP_DATA;
-#elif defined(OS_POSIX)
- base::DIR_EXE;
-#elif defined(OS_WIN)
- base::DIR_MODULE;
-#endif // OS_ANDROID
-#endif // !OS_MACOSX
-
-const char IsolateHolder::kNativesFileName[] = "natives_blob.bin";
-const char IsolateHolder::kSnapshotFileName[] = "snapshot_blob.bin";
-
-// static
-bool IsolateHolder::LoadV8Snapshot() {
- if (g_mapped_natives && g_mapped_snapshot)
- return true;
-
-#if !defined(OS_MACOSX)
- base::FilePath data_path;
- PathService::Get(kV8SnapshotBasePathKey, &data_path);
- DCHECK(!data_path.empty());
-
- base::FilePath natives_path = data_path.AppendASCII(kNativesFileName);
- base::FilePath snapshot_path = data_path.AppendASCII(kSnapshotFileName);
-#else // !defined(OS_MACOSX)
- base::ScopedCFTypeRef<CFStringRef> natives_file_name(
- base::SysUTF8ToCFStringRef(kNativesFileName));
- base::FilePath natives_path = base::mac::PathForFrameworkBundleResource(
- natives_file_name);
- base::ScopedCFTypeRef<CFStringRef> snapshot_file_name(
- base::SysUTF8ToCFStringRef(kSnapshotFileName));
- base::FilePath snapshot_path = base::mac::PathForFrameworkBundleResource(
- snapshot_file_name);
- DCHECK(!natives_path.empty());
- DCHECK(!snapshot_path.empty());
-#endif // !defined(OS_MACOSX)
-
- if (!MapV8Files(&natives_path, &snapshot_path))
- return false;
-
-#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
- return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) &&
- VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint);
-#else
- return true;
-#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
-}
-
-//static
-bool IsolateHolder::LoadV8SnapshotFd(int natives_fd,
- int64 natives_offset,
- int64 natives_size,
- int snapshot_fd,
- int64 snapshot_offset,
- int64 snapshot_size) {
- if (g_mapped_natives && g_mapped_snapshot)
- return true;
-
- base::MemoryMappedFile::Region natives_region =
- base::MemoryMappedFile::Region::kWholeFile;
- if (natives_size != 0 || natives_offset != 0) {
- natives_region =
- base::MemoryMappedFile::Region(natives_offset, natives_size);
- }
-
- base::MemoryMappedFile::Region snapshot_region =
- base::MemoryMappedFile::Region::kWholeFile;
- if (natives_size != 0 || natives_offset != 0) {
- snapshot_region =
- base::MemoryMappedFile::Region(snapshot_offset, snapshot_size);
- }
-
- return MapV8Files(
- NULL, NULL, natives_fd, snapshot_fd, natives_region, snapshot_region);
-}
-#endif // V8_USE_EXTERNAL_STARTUP_DATA
-
-//static
-void IsolateHolder::GetV8ExternalSnapshotData(const char** natives_data_out,
- int* natives_size_out,
- const char** snapshot_data_out,
- int* snapshot_size_out) {
- if (!g_mapped_natives || !g_mapped_snapshot) {
- *natives_data_out = *snapshot_data_out = NULL;
- *natives_size_out = *snapshot_size_out = 0;
- return;
- }
- *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
- *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data());
- *natives_size_out = static_cast<int>(g_mapped_natives->length());
- *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
-}
-
IsolateHolder::IsolateHolder() {
- CHECK(g_array_buffer_allocator)
- << "You need to invoke gin::IsolateHolder::Initialize first";
+ v8::ArrayBuffer::Allocator* allocator = g_array_buffer_allocator;
+ CHECK(allocator) << "You need to invoke gin::IsolateHolder::Initialize first";
v8::Isolate::CreateParams params;
params.entry_hook = DebugImpl::GetFunctionEntryHook();
params.code_event_handler = DebugImpl::GetJitCodeEventHandler();
@@ -222,7 +32,7 @@ IsolateHolder::IsolateHolder() {
base::SysInfo::AmountOfVirtualMemory(),
base::SysInfo::NumberOfProcessors());
isolate_ = v8::Isolate::New(params);
- isolate_data_.reset(new PerIsolateData(isolate_, g_array_buffer_allocator));
+ isolate_data_.reset(new PerIsolateData(isolate_, allocator));
#if defined(OS_WIN)
{
void* code_range;
@@ -259,30 +69,8 @@ IsolateHolder::~IsolateHolder() {
void IsolateHolder::Initialize(ScriptMode mode,
v8::ArrayBuffer::Allocator* allocator) {
CHECK(allocator);
- static bool v8_is_initialized = false;
- if (v8_is_initialized)
- return;
- v8::V8::InitializePlatform(V8Platform::Get());
- v8::V8::SetArrayBufferAllocator(allocator);
+ gin::V8Initializer::Initialize(mode, allocator);
g_array_buffer_allocator = allocator;
- if (mode == gin::IsolateHolder::kStrictMode) {
- static const char use_strict[] = "--use_strict";
- v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1);
- }
- v8::V8::SetEntropySource(&GenerateEntropy);
-#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
- v8::StartupData natives;
- natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
- natives.raw_size = static_cast<int>(g_mapped_natives->length());
- v8::V8::SetNativesDataBlob(&natives);
-
- v8::StartupData snapshot;
- snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
- snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
- v8::V8::SetSnapshotDataBlob(&snapshot);
-#endif // V8_USE_EXTERNAL_STARTUP_DATA
- v8::V8::Initialize();
- v8_is_initialized = true;
}
void IsolateHolder::AddRunMicrotasksObserver() {
@@ -297,4 +85,13 @@ void IsolateHolder::RemoveRunMicrotasksObserver() {
task_observer_.reset();
}
+#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
+
+// static
+bool IsolateHolder::LoadV8Snapshot() {
+ return V8Initializer::LoadV8Snapshot();
+}
+
+#endif // V8_USE_EXTERNAL_STARTUP_DATA
+
} // namespace gin
diff --git a/gin/public/isolate_holder.h b/gin/public/isolate_holder.h
index 7968b1df..d2275ed 100644
--- a/gin/public/isolate_holder.h
+++ b/gin/public/isolate_holder.h
@@ -31,9 +31,10 @@ class GIN_EXPORT IsolateHolder {
~IsolateHolder();
// Should be invoked once before creating IsolateHolder instances to
- // initialize V8 and Gin. In case V8_USE_EXTERNAL_STARTUP_DATA is defined,
- // V8's initial snapshot should be loaded (by calling LoadV8Snapshot or
- // LoadV8SnapshotFd) before calling Initialize.
+ // initialize V8 and Gin. In case V8_USE_EXTERNAL_STARTUP_DATA is
+ // defined, V8's initial snapshot should be loaded (by calling
+ // V8Initializer::LoadV8SnapshotFromFD or
+ // V8Initializer::LoadV8Snapshot) before calling this method.
static void Initialize(ScriptMode mode,
v8::ArrayBuffer::Allocator* allocator);
@@ -52,22 +53,13 @@ class GIN_EXPORT IsolateHolder {
void RemoveRunMicrotasksObserver();
#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
- static const int kV8SnapshotBasePathKey;
- static const char kNativesFileName[];
- static const char kSnapshotFileName[];
-
- static bool LoadV8SnapshotFd(int natives_fd,
- int64 natives_offset,
- int64 natives_size,
- int snapshot_fd,
- int64 snapshot_offset,
- int64 snapshot_size);
+
+ // Deprecated and to be removed: Use gin::V8Initializer::LoadV8Snapshot()
+ // Load V8 snapshot from default resources. Returns true on success or
+ // snapshot is already loaded, false otherwise.
static bool LoadV8Snapshot();
+
#endif // V8_USE_EXTERNAL_STARTUP_DATA
- static void GetV8ExternalSnapshotData(const char** natives_data_out,
- int* natives_size_out,
- const char** snapshot_data_out,
- int* snapshot_size_out);
private:
v8::Isolate* isolate_;
diff --git a/gin/shell/gin_main.cc b/gin/shell/gin_main.cc
index 532f996..d3bab72 100644
--- a/gin/shell/gin_main.cc
+++ b/gin/shell/gin_main.cc
@@ -13,6 +13,7 @@
#include "gin/modules/module_runner_delegate.h"
#include "gin/public/isolate_holder.h"
#include "gin/try_catch.h"
+#include "gin/v8_initializer.h"
namespace gin {
namespace {
@@ -60,7 +61,7 @@ int main(int argc, char** argv) {
base::CommandLine::Init(argc, argv);
base::i18n::InitializeICU();
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
- gin::IsolateHolder::LoadV8Snapshot();
+ gin::V8Initializer::LoadV8Snapshot();
#endif
gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode,
diff --git a/gin/shell_runner_unittest.cc b/gin/shell_runner_unittest.cc
index 7134de1..340b673 100644
--- a/gin/shell_runner_unittest.cc
+++ b/gin/shell_runner_unittest.cc
@@ -8,6 +8,7 @@
#include "gin/array_buffer.h"
#include "gin/converter.h"
#include "gin/public/isolate_holder.h"
+#include "gin/v8_initializer.h"
#include "testing/gtest/include/gtest/gtest.h"
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
@@ -25,7 +26,7 @@ TEST(RunnerTest, Run) {
std::string source = "this.result = 'PASS';\n";
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
- gin::IsolateHolder::LoadV8Snapshot();
+ gin::V8Initializer::LoadV8Snapshot();
#endif
gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode,
diff --git a/gin/test/file_runner.cc b/gin/test/file_runner.cc
index b639d06..723ab98 100644
--- a/gin/test/file_runner.cc
+++ b/gin/test/file_runner.cc
@@ -17,12 +17,9 @@
#include "gin/test/gc.h"
#include "gin/test/gtest.h"
#include "gin/try_catch.h"
+#include "gin/v8_initializer.h"
#include "testing/gtest/include/gtest/gtest.h"
-#ifdef V8_USE_EXTERNAL_STARTUP_DATA
-#include "gin/public/isolate_holder.h"
-#endif
-
namespace gin {
namespace {
@@ -63,11 +60,12 @@ void RunTestFromFile(const base::FilePath& path, FileRunnerDelegate* delegate,
base::MessageLoop message_loop;
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
- gin::IsolateHolder::LoadV8Snapshot();
+ gin::V8Initializer::LoadV8Snapshot();
#endif
gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode,
gin::ArrayBufferAllocator::SharedInstance());
+
gin::IsolateHolder instance;
gin::ShellRunner runner(delegate, instance.isolate());
{
diff --git a/gin/test/v8_test.cc b/gin/test/v8_test.cc
index a022511..aad291d 100644
--- a/gin/test/v8_test.cc
+++ b/gin/test/v8_test.cc
@@ -6,10 +6,7 @@
#include "gin/array_buffer.h"
#include "gin/public/isolate_holder.h"
-
-#ifdef V8_USE_EXTERNAL_STARTUP_DATA
-#include "gin/public/isolate_holder.h"
-#endif
+#include "gin/v8_initializer.h"
using v8::Context;
using v8::Local;
@@ -25,10 +22,11 @@ V8Test::~V8Test() {
void V8Test::SetUp() {
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
- gin::IsolateHolder::LoadV8Snapshot();
+ gin::V8Initializer::LoadV8Snapshot();
#endif
gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode,
gin::ArrayBufferAllocator::SharedInstance());
+
instance_.reset(new gin::IsolateHolder);
instance_->isolate()->Enter();
HandleScope handle_scope(instance_->isolate());
diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc
new file mode 100644
index 0000000..76f227d
--- /dev/null
+++ b/gin/v8_initializer.cc
@@ -0,0 +1,221 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "gin/v8_initializer.h"
+
+#include "base/basictypes.h"
+#include "base/files/file.h"
+#include "base/files/file_path.h"
+#include "base/files/memory_mapped_file.h"
+#include "base/logging.h"
+#include "base/rand_util.h"
+#include "base/strings/sys_string_conversions.h"
+#include "crypto/sha2.h"
+
+#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
+#if defined(OS_MACOSX)
+#include "base/mac/foundation_util.h"
+#endif // OS_MACOSX
+#include "base/path_service.h"
+#endif // V8_USE_EXTERNAL_STARTUP_DATA
+
+namespace gin {
+
+namespace {
+
+bool GenerateEntropy(unsigned char* buffer, size_t amount) {
+ base::RandBytes(buffer, amount);
+ return true;
+}
+
+base::MemoryMappedFile* g_mapped_natives = nullptr;
+base::MemoryMappedFile* g_mapped_snapshot = nullptr;
+
+#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
+bool MapV8Files(base::File natives_file,
+ base::File snapshot_file,
+ base::MemoryMappedFile::Region natives_region =
+ base::MemoryMappedFile::Region::kWholeFile,
+ base::MemoryMappedFile::Region snapshot_region =
+ base::MemoryMappedFile::Region::kWholeFile) {
+ g_mapped_natives = new base::MemoryMappedFile;
+ if (!g_mapped_natives->IsValid()) {
+ if (!g_mapped_natives->Initialize(natives_file.Pass(), natives_region)) {
+ delete g_mapped_natives;
+ g_mapped_natives = NULL;
+ LOG(FATAL) << "Couldn't mmap v8 natives data file";
+ return false;
+ }
+ }
+
+ g_mapped_snapshot = new base::MemoryMappedFile;
+ if (!g_mapped_snapshot->IsValid()) {
+ if (!g_mapped_snapshot->Initialize(snapshot_file.Pass(), snapshot_region)) {
+ delete g_mapped_snapshot;
+ g_mapped_snapshot = NULL;
+ LOG(ERROR) << "Couldn't mmap v8 snapshot data file";
+ return false;
+ }
+ }
+
+ return true;
+}
+
+#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
+bool VerifyV8SnapshotFile(base::MemoryMappedFile* snapshot_file,
+ const unsigned char* fingerprint) {
+ unsigned char output[crypto::kSHA256Length];
+ crypto::SHA256HashString(
+ base::StringPiece(reinterpret_cast<const char*>(snapshot_file->data()),
+ snapshot_file->length()),
+ output, sizeof(output));
+ return !memcmp(fingerprint, output, sizeof(output));
+}
+#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
+#endif // V8_USE_EXTERNAL_STARTUP_DATA
+
+} // namespace
+
+#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
+
+#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
+// Defined in gen/gin/v8_snapshot_fingerprint.cc
+extern const unsigned char g_natives_fingerprint[];
+extern const unsigned char g_snapshot_fingerprint[];
+#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
+
+#if !defined(OS_MACOSX)
+const int V8Initializer::kV8SnapshotBasePathKey =
+#if defined(OS_ANDROID)
+ base::DIR_ANDROID_APP_DATA;
+#elif defined(OS_POSIX)
+ base::DIR_EXE;
+#elif defined(OS_WIN)
+ base::DIR_MODULE;
+#endif // OS_ANDROID
+#endif // !OS_MACOSX
+
+const char V8Initializer::kNativesFileName[] = "natives_blob.bin";
+const char V8Initializer::kSnapshotFileName[] = "snapshot_blob.bin";
+
+// static
+bool V8Initializer::LoadV8Snapshot() {
+ if (g_mapped_natives && g_mapped_snapshot)
+ return true;
+
+#if !defined(OS_MACOSX)
+ base::FilePath data_path;
+ PathService::Get(kV8SnapshotBasePathKey, &data_path);
+ DCHECK(!data_path.empty());
+
+ base::FilePath natives_path = data_path.AppendASCII(kNativesFileName);
+ base::FilePath snapshot_path = data_path.AppendASCII(kSnapshotFileName);
+#else // !defined(OS_MACOSX)
+ base::ScopedCFTypeRef<CFStringRef> natives_file_name(
+ base::SysUTF8ToCFStringRef(kNativesFileName));
+ base::FilePath natives_path =
+ base::mac::PathForFrameworkBundleResource(natives_file_name);
+ base::ScopedCFTypeRef<CFStringRef> snapshot_file_name(
+ base::SysUTF8ToCFStringRef(kSnapshotFileName));
+ base::FilePath snapshot_path =
+ base::mac::PathForFrameworkBundleResource(snapshot_file_name);
+ DCHECK(!natives_path.empty());
+ DCHECK(!snapshot_path.empty());
+#endif // !defined(OS_MACOSX)
+
+ int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
+ if (!MapV8Files(base::File(natives_path, flags),
+ base::File(snapshot_path, flags)))
+ return false;
+
+#if defined(V8_VERIFY_EXTERNAL_STARTUP_DATA)
+ return VerifyV8SnapshotFile(g_mapped_natives, g_natives_fingerprint) &&
+ VerifyV8SnapshotFile(g_mapped_snapshot, g_snapshot_fingerprint);
+#else
+ return true;
+#endif // V8_VERIFY_EXTERNAL_STARTUP_DATA
+}
+
+// static
+bool V8Initializer::LoadV8SnapshotFromFD(base::PlatformFile natives_pf,
+ int64 natives_offset,
+ int64 natives_size,
+ base::PlatformFile snapshot_pf,
+ int64 snapshot_offset,
+ int64 snapshot_size) {
+ if (g_mapped_natives && g_mapped_snapshot)
+ return true;
+
+ base::MemoryMappedFile::Region natives_region =
+ base::MemoryMappedFile::Region::kWholeFile;
+ if (natives_size != 0 || natives_offset != 0) {
+ natives_region =
+ base::MemoryMappedFile::Region(natives_offset, natives_size);
+ }
+
+ base::MemoryMappedFile::Region snapshot_region =
+ base::MemoryMappedFile::Region::kWholeFile;
+ if (natives_size != 0 || natives_offset != 0) {
+ snapshot_region =
+ base::MemoryMappedFile::Region(snapshot_offset, snapshot_size);
+ }
+
+ return MapV8Files(base::File(natives_pf), base::File(snapshot_pf),
+ natives_region, snapshot_region);
+}
+
+#endif // V8_USE_EXTERNAL_STARTUP_DATA
+
+// static
+void V8Initializer::Initialize(gin::IsolateHolder::ScriptMode mode,
+ v8::ArrayBuffer::Allocator* allocator) {
+ CHECK(allocator);
+
+ static bool v8_is_initialized = false;
+ if (v8_is_initialized)
+ return;
+
+ v8::V8::InitializePlatform(V8Platform::Get());
+ v8::V8::SetArrayBufferAllocator(allocator);
+
+ if (gin::IsolateHolder::kStrictMode == mode) {
+ static const char use_strict[] = "--use_strict";
+ v8::V8::SetFlagsFromString(use_strict, sizeof(use_strict) - 1);
+ }
+
+#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
+ v8::StartupData natives;
+ natives.data = reinterpret_cast<const char*>(g_mapped_natives->data());
+ natives.raw_size = static_cast<int>(g_mapped_natives->length());
+ v8::V8::SetNativesDataBlob(&natives);
+
+ v8::StartupData snapshot;
+ snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
+ snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
+ v8::V8::SetSnapshotDataBlob(&snapshot);
+#endif // V8_USE_EXTERNAL_STARTUP_DATA
+
+ v8::V8::SetEntropySource(&GenerateEntropy);
+ v8::V8::Initialize();
+
+ v8_is_initialized = true;
+}
+
+// static
+void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out,
+ int* natives_size_out,
+ const char** snapshot_data_out,
+ int* snapshot_size_out) {
+ if (!g_mapped_natives || !g_mapped_snapshot) {
+ *natives_data_out = *snapshot_data_out = NULL;
+ *natives_size_out = *snapshot_size_out = 0;
+ return;
+ }
+ *natives_data_out = reinterpret_cast<const char*>(g_mapped_natives->data());
+ *snapshot_data_out = reinterpret_cast<const char*>(g_mapped_snapshot->data());
+ *natives_size_out = static_cast<int>(g_mapped_natives->length());
+ *snapshot_size_out = static_cast<int>(g_mapped_snapshot->length());
+}
+
+} // namespace gin
diff --git a/gin/v8_initializer.h b/gin/v8_initializer.h
new file mode 100644
index 0000000..ad3ed6e
--- /dev/null
+++ b/gin/v8_initializer.h
@@ -0,0 +1,57 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef GIN_V8_INITIALIZER_H_
+#define GIN_V8_INITIALIZER_H_
+
+#include "base/files/file.h"
+#include "gin/array_buffer.h"
+#include "gin/gin_export.h"
+#include "gin/public/isolate_holder.h"
+#include "gin/public/v8_platform.h"
+#include "v8/include/v8.h"
+
+namespace gin {
+
+class GIN_EXPORT V8Initializer {
+ public:
+ static const int kV8SnapshotBasePathKey;
+ static const char kNativesFileName[];
+ static const char kSnapshotFileName[];
+
+ // This should be called by IsolateHolder::Initialize().
+ static void Initialize(gin::IsolateHolder::ScriptMode mode,
+ v8::ArrayBuffer::Allocator* allocator);
+
+ // Get address and size information for currently loaded snapshot.
+ // If no snapshot is loaded, the return values are null for addresses
+ // and 0 for sizes.
+ static void GetV8ExternalSnapshotData(const char** natives_data_out,
+ int* natives_size_out,
+ const char** snapshot_data_out,
+ int* snapshot_size_out);
+
+#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
+
+ // Load V8 snapshot from user provided platform file descriptors.
+ // The offset and size arguments, if non-zero, specify the portions
+ // of the files to be loaded. This methods returns true on success
+ // (or if snapshot is already loaded), false otherwise.
+ static bool LoadV8SnapshotFromFD(base::PlatformFile natives_fd,
+ int64 natives_offset,
+ int64 natives_size,
+ base::PlatformFile snapshot_fd,
+ int64 snapshot_offset,
+ int64 snapshot_size);
+
+ // Load V8 snapshot from default resources. Returns true on success or
+ // snapshot is already loaded, false otherwise.
+ static bool LoadV8Snapshot();
+
+#endif // V8_USE_EXTERNAL_STARTUP_DATA
+};
+
+} // namespace gin
+
+#endif // GIN_V8_INITIALIZER_H_