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

Unified Diff: tools/v8_context_snapshot/v8_context_snapshot_generator.cc

Issue 2841443005: [Bindings] Create and use V8 context snapshots (Closed)
Patch Set: Rebase Created 3 years, 5 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
« no previous file with comments | « tools/v8_context_snapshot/run.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/v8_context_snapshot/v8_context_snapshot_generator.cc
diff --git a/tools/v8_context_snapshot/v8_context_snapshot_generator.cc b/tools/v8_context_snapshot/v8_context_snapshot_generator.cc
new file mode 100644
index 0000000000000000000000000000000000000000..449a54633f709dd9a01adfd78066615b8dbd9f44
--- /dev/null
+++ b/tools/v8_context_snapshot/v8_context_snapshot_generator.cc
@@ -0,0 +1,69 @@
+// Copyright 2017 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 "base/at_exit.h"
+#include "base/command_line.h"
+#include "base/files/file_util.h"
+#include "base/message_loop/message_loop.h"
+#include "base/task_scheduler/task_scheduler.h"
+#include "gin/v8_initializer.h"
+#include "mojo/edk/embedder/embedder.h"
+#include "third_party/WebKit/public/platform/WebThread.h"
+#include "third_party/WebKit/public/web/WebKit.h"
+#include "third_party/WebKit/public/web/WebV8ContextSnapshot.h"
+#include "v8/include/v8.h"
+
+namespace {
+
+class SnapshotThread : public blink::WebThread {
+ public:
+ bool IsCurrentThread() const override { return true; }
+ blink::WebScheduler* Scheduler() const override { return nullptr; }
+ blink::WebTaskRunner* GetWebTaskRunner() override { return nullptr; }
+};
+
+class SnapshotPlatform final : public blink::Platform {
+ public:
+ bool IsTakingV8ContextSnapshot() override { return true; }
+ blink::WebThread* CurrentThread() override {
+ static SnapshotThread dummy_thread;
+ return &dummy_thread;
+ }
+};
+
+} // namespace
+
+// This program takes a snapshot of V8 contexts and writes it out as a file.
+// The snapshot file is consumed by Blink.
+//
+// Usage:
+// % v8_context_snapshot_generator --output_file=<filename>
+int main(int argc, char** argv) {
+ base::AtExitManager at_exit;
+ base::CommandLine::Init(argc, argv);
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA
+ gin::V8Initializer::LoadV8Snapshot();
+ gin::V8Initializer::LoadV8Natives();
+#endif
+
+ // Set up environment to make Blink and V8 workable.
+ base::MessageLoop message_loop;
+ base::TaskScheduler::CreateAndStartWithDefaultParams("TakeSnapshot");
+ mojo::edk::Init();
+
+ // Take a snapshot.
+ SnapshotPlatform platform;
+ blink::Initialize(&platform);
+ v8::StartupData blob = blink::WebV8ContextSnapshot::TakeSnapshot();
+
+ // Save the snapshot as a file. Filename is given in a command line option.
+ base::FilePath file_path =
+ base::CommandLine::ForCurrentProcess()->GetSwitchValuePath("output_file");
+ CHECK(!file_path.empty());
+ CHECK_LT(0, base::WriteFile(file_path, blob.data, blob.raw_size));
+
+ delete[] blob.data;
+
+ return 0;
+}
« no previous file with comments | « tools/v8_context_snapshot/run.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698