OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/at_exit.h" |
| 6 #include "base/command_line.h" |
| 7 #include "base/files/file_util.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/task_scheduler/task_scheduler.h" |
| 10 #include "gin/v8_initializer.h" |
| 11 #include "mojo/edk/embedder/embedder.h" |
| 12 #include "third_party/WebKit/public/platform/WebThread.h" |
| 13 #include "third_party/WebKit/public/web/WebKit.h" |
| 14 #include "third_party/WebKit/public/web/WebV8ContextSnapshot.h" |
| 15 #include "v8/include/v8.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 class SnapshotThread : public blink::WebThread { |
| 20 public: |
| 21 bool IsCurrentThread() const override { return true; } |
| 22 blink::WebScheduler* Scheduler() const override { return nullptr; } |
| 23 blink::WebTaskRunner* GetWebTaskRunner() override { return nullptr; } |
| 24 }; |
| 25 |
| 26 class SnapshotPlatform final : public blink::Platform { |
| 27 public: |
| 28 bool IsTakingV8ContextSnapshot() override { return true; } |
| 29 blink::WebThread* CurrentThread() override { |
| 30 static SnapshotThread dummy_thread; |
| 31 return &dummy_thread; |
| 32 } |
| 33 }; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 // This program takes a snapshot of V8 contexts and writes it out as a file. |
| 38 // The snapshot file is consumed by Blink. |
| 39 // |
| 40 // Usage: |
| 41 // % v8_context_snapshot_generator --output_file=<filename> |
| 42 int main(int argc, char** argv) { |
| 43 base::AtExitManager at_exit; |
| 44 base::CommandLine::Init(argc, argv); |
| 45 #ifdef V8_USE_EXTERNAL_STARTUP_DATA |
| 46 gin::V8Initializer::LoadV8Snapshot(); |
| 47 gin::V8Initializer::LoadV8Natives(); |
| 48 #endif |
| 49 |
| 50 // Set up environment to make Blink and V8 workable. |
| 51 base::MessageLoop message_loop; |
| 52 base::TaskScheduler::CreateAndStartWithDefaultParams("TakeSnapshot"); |
| 53 mojo::edk::Init(); |
| 54 |
| 55 // Take a snapshot. |
| 56 SnapshotPlatform platform; |
| 57 blink::Initialize(&platform); |
| 58 v8::StartupData blob = blink::WebV8ContextSnapshot::TakeSnapshot(); |
| 59 |
| 60 // Save the snapshot as a file. Filename is given in a command line option. |
| 61 base::FilePath file_path = |
| 62 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath("output_file"); |
| 63 CHECK(!file_path.empty()); |
| 64 CHECK_LT(0, base::WriteFile(file_path, blob.data, blob.raw_size)); |
| 65 |
| 66 delete[] blob.data; |
| 67 |
| 68 return 0; |
| 69 } |
OLD | NEW |