Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Unified Diff: gin/v8_initializer.cc

Issue 2841443005: [Bindings] Create and use V8 context snapshots (Closed)
Patch Set: Work for some comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: gin/v8_initializer.cc
diff --git a/gin/v8_initializer.cc b/gin/v8_initializer.cc
index 726d7c043a21a81b9ee2f434a8a6706d7c2de57e..916c0411a10ef7ccef316d8398ca18e408bb2494 100644
--- a/gin/v8_initializer.cc
+++ b/gin/v8_initializer.cc
@@ -43,6 +43,9 @@ namespace {
// None of these globals are ever freed nor closed.
base::MemoryMappedFile* g_mapped_natives = nullptr;
base::MemoryMappedFile* g_mapped_snapshot = nullptr;
+base::MemoryMappedFile* g_mapped_context = nullptr;
+
+const char kV8ContextFileName[] = "context_blob.bin";
#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
@@ -388,7 +391,7 @@ void V8Initializer::Initialize(IsolateHolder::ScriptMode mode,
natives.raw_size = static_cast<int>(g_mapped_natives->length());
v8::V8::SetNativesDataBlob(&natives);
- if (g_mapped_snapshot != NULL) {
+ if (g_mapped_snapshot) {
v8::StartupData snapshot;
snapshot.data = reinterpret_cast<const char*>(g_mapped_snapshot->data());
snapshot.raw_size = static_cast<int>(g_mapped_snapshot->length());
@@ -424,4 +427,70 @@ void V8Initializer::GetV8ExternalSnapshotData(const char** natives_data_out,
}
}
+// static
+void V8Initializer::LoadV8Context() {
+ if (g_mapped_context)
+ return;
+
+ OpenFileIfNecessary(kV8ContextFileName);
+ MapOpenedFile(GetOpenedFile(kV8ContextFileName), &g_mapped_context);
+}
+
+// static
+void V8Initializer::LoadV8ContextFromFD(base::PlatformFile snapshot_pf,
+ int64_t snapshot_offset,
+ int64_t snapshot_size) {
+ if (g_mapped_context)
+ return;
+
+ if (snapshot_pf == base::kInvalidPlatformFile)
+ return;
+
+ base::MemoryMappedFile::Region snapshot_region =
+ base::MemoryMappedFile::Region::kWholeFile;
+ if (snapshot_size != 0 || snapshot_offset != 0) {
+ snapshot_region.offset = snapshot_offset;
+ snapshot_region.size = snapshot_size;
+ }
+
+ LoadV8FileResult result = V8_LOAD_SUCCESS;
+ if (!MapV8File(snapshot_pf, snapshot_region, &g_mapped_context))
+ result = V8_LOAD_FAILED_MAP;
+ if (result == V8_LOAD_SUCCESS) {
+ g_opened_files.Get()[kV8ContextFileName] =
+ std::make_pair(snapshot_pf, snapshot_region);
+ }
+}
+
+// static
+base::PlatformFile V8Initializer::GetOpenV8ContextFileForChildProcesses(
+ base::MemoryMappedFile::Region* region_out) {
+ const OpenedFileMap::mapped_type& opened =
+ OpenFileIfNecessary(kV8ContextFileName);
+ *region_out = opened.second;
+ return opened.first;
+}
+
+// static
+void V8Initializer::GetV8ContextData(const char** snapshot_data_out,
+ int* snapshot_size_out) {
+ if (g_mapped_context) {
+ *snapshot_data_out =
+ reinterpret_cast<const char*>(g_mapped_context->data());
+ *snapshot_size_out = static_cast<int>(g_mapped_context->length());
+ } else {
+ *snapshot_data_out = nullptr;
+ *snapshot_size_out = 0;
+ }
+}
+
+#if defined(OS_ANDROID)
+// static
+base::FilePath V8Initializer::GetV8ContextFilePath() {
+ base::FilePath path;
+ GetV8FilePath(kV8ContextFileName, &path);
+ return path;
+}
+#endif // defined(OS_ANDROID)
+
} // namespace gin

Powered by Google App Engine
This is Rietveld 408576698