blob: 4298bff3a07a0c38b9af153d982716b682623d1d [file] [log] [blame]
// 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/single_thread_task_runner.h"
#include "base/task/thread_pool/thread_pool.h"
#include "base/threading/thread_task_runner_handle.h"
#include "gin/v8_initializer.h"
#include "mojo/core/embedder/embedder.h"
#include "services/service_manager/public/cpp/binder_registry.h"
#include "third_party/blink/public/web/blink.h"
#include "third_party/blink/public/web/web_v8_context_snapshot.h"
#include "v8/include/v8.h"
namespace {
constexpr char kPredictableFlag[] = "--predictable";
class SnapshotPlatform final : public blink::Platform {
bool IsTakingV8ContextSnapshot() override { return true; }
} // 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);
// Set up environment to make Blink and V8 workable.
base::MessageLoop message_loop;
// Set predictable flag in V8 to generate identical snapshot file.
v8::V8::SetFlagsFromString(kPredictableFlag, sizeof(kPredictableFlag) - 1);
// Take a snapshot.
SnapshotPlatform platform;
service_manager::BinderRegistry empty_registry;
blink::CreateMainThreadAndInitialize(&platform, &empty_registry);
v8::StartupData blob = blink::WebV8ContextSnapshot::TakeSnapshot();
// Save the snapshot as a file. Filename is given in a command line option.
base::FilePath file_path =
int written = base::WriteFile(file_path,, blob.raw_size);
int error_code = 0;
if (written != blob.raw_size) {
fprintf(stderr, "Error: WriteFile of %d snapshot bytes returned %d.\n",
blob.raw_size, written);
error_code = 1;
// v8::SnapshotCreator used in WebV8ContextSnapshot makes it complex how to
// manage lifetime of v8::Isolate, gin::IsolateHolder, and
// blink::V8PerIsolateData. Now we complete all works at this point, and can
// exit without releasing all those instances correctly.