From 570cfb6299e68de83465d167b549fc2676a3fff7 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 25 Jun 2015 17:10:35 -0700 Subject: [PATCH] Support snapshots in sky_shell (again) Unbreak support for snapshots now that we have our offline Dart loader. Also, wire up support for snapshots in sky_shell on Linux. TBR=eseidel@chromium.org Review URL: https://codereview.chromium.org/1212623002. --- engine/core/script/dart_controller.cc | 5 ++- engine/core/script/dart_controller.h | 3 +- engine/public/sky/sky_view.cc | 3 +- services/viewport/viewport_observer.mojom | 2 +- shell/BUILD.gn | 2 + shell/linux/main.cc | 51 ++++++++++++++++++++--- shell/switches.cc | 17 ++++++++ shell/switches.h | 20 +++++++++ shell/ui/engine.cc | 8 +++- shell/ui/engine.h | 3 +- tools/packager/loader.cc | 6 ++- tools/packager/main.cc | 22 +++++++--- tools/packager/switches.cc | 5 +++ tools/packager/switches.h | 5 +++ 14 files changed, 131 insertions(+), 21 deletions(-) create mode 100644 shell/switches.cc create mode 100644 shell/switches.h diff --git a/engine/core/script/dart_controller.cc b/engine/core/script/dart_controller.cc index f12119736a7..489d8c11da4 100644 --- a/engine/core/script/dart_controller.cc +++ b/engine/core/script/dart_controller.cc @@ -149,10 +149,11 @@ void DartController::DidLoadSnapshot() { DartInvokeAppField(library, ToDart("main"), 0, nullptr); } -void DartController::LoadSnapshot(const KURL& url, mojo::URLResponsePtr response) { +void DartController::RunFromSnapshot( + mojo::ScopedDataPipeConsumerHandle snapshot) { snapshot_loader_ = adoptPtr(new DartSnapshotLoader(dart_state())); snapshot_loader_->LoadSnapshot( - response->body.Pass(), + snapshot.Pass(), base::Bind(&DartController::DidLoadSnapshot, weak_factory_.GetWeakPtr())); } diff --git a/engine/core/script/dart_controller.h b/engine/core/script/dart_controller.h index bff9f29c7af..cee23a98c27 100644 --- a/engine/core/script/dart_controller.h +++ b/engine/core/script/dart_controller.h @@ -36,10 +36,9 @@ class DartController { typedef base::Callback, RefPtr)> LoadFinishedCallback; - void LoadSnapshot(const KURL& url, mojo::URLResponsePtr response = nullptr); - void RunFromLibrary(const String& name, DartLibraryProvider* library_provider); + void RunFromSnapshot(mojo::ScopedDataPipeConsumerHandle snapshot); void LoadScriptInModule(AbstractModule* module, const String& source, diff --git a/engine/public/sky/sky_view.cc b/engine/public/sky/sky_view.cc index b18677c65e4..a761bbaf306 100644 --- a/engine/public/sky/sky_view.cc +++ b/engine/public/sky/sky_view.cc @@ -48,7 +48,8 @@ void SkyView::RunFromLibrary(const WebString& name, void SkyView::RunFromSnapshot(const WebString& name, mojo::ScopedDataPipeConsumerHandle snapshot) { - // TODO(abarth): Implement. + CreateView(name); + dart_controller_->RunFromSnapshot(snapshot.Pass()); } void SkyView::BeginFrame(base::TimeTicks frame_time) { diff --git a/services/viewport/viewport_observer.mojom b/services/viewport/viewport_observer.mojom index ce86699be25..68dec398d39 100644 --- a/services/viewport/viewport_observer.mojom +++ b/services/viewport/viewport_observer.mojom @@ -13,5 +13,5 @@ interface ViewportObserver { RunFromNetwork(string url); RunFromFile(string main, string package_root); - RunFromSnapshot(handle snapshot); + RunFromSnapshot(string url, handle snapshot); }; diff --git a/shell/BUILD.gn b/shell/BUILD.gn index a71ad12c133..4032deb4d93 100644 --- a/shell/BUILD.gn +++ b/shell/BUILD.gn @@ -44,6 +44,8 @@ source_set("common") { "shell.h", "shell_view.cc", "shell_view.h", + "switches.cc", + "switches.h", "ui/animator.cc", "ui/animator.h", "ui/engine.cc", diff --git a/shell/linux/main.cc b/shell/linux/main.cc index 56fd84f5ae3..b40223d582d 100644 --- a/shell/linux/main.cc +++ b/shell/linux/main.cc @@ -2,23 +2,37 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include + #include "base/at_exit.h" #include "base/basictypes.h" #include "base/bind.h" #include "base/command_line.h" +#include "base/files/file_path.h" #include "base/i18n/icu_util.h" #include "base/logging.h" #include "base/message_loop/message_loop.h" +#include "base/threading/worker_pool.h" +#include "mojo/common/data_pipe_utils.h" #include "sky/shell/platform_view.h" #include "sky/shell/service_provider.h" #include "sky/shell/shell.h" #include "sky/shell/shell_view.h" +#include "sky/shell/switches.h" namespace sky { namespace shell { +namespace { -const char kMain[] = "main"; -const char kPackageRoot[] = "package-root"; +void Ignored(bool) { +} + +void Usage() { + std::cerr << "Usage: sky_shell" + << " [--" << switches::kPackageRoot << "]" + << " [--" << switches::kSnapshot << "]" + << " " << std::endl; +} void Init() { Shell::Init(make_scoped_ptr(new ServiceProviderContext( @@ -32,12 +46,31 @@ void Init() { base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); - std::string main = command_line.GetSwitchValueASCII(kMain); - std::string package_root = command_line.GetSwitchValueASCII(kPackageRoot); + std::string main = command_line.GetArgs()[0]; + if (command_line.HasSwitch(switches::kSnapshot)) { + base::FilePath snapshot = + command_line.GetSwitchValuePath(switches::kSnapshot); + mojo::DataPipe pipe; + viewport_observer->RunFromSnapshot(main, pipe.consumer_handle.Pass()); + scoped_refptr runner = + base::WorkerPool::GetTaskRunner(true); + mojo::common::CopyFromFile(snapshot, pipe.producer_handle.Pass(), 0, + runner.get(), base::Bind(&Ignored)); + return; + } - viewport_observer->RunFromFile(main, package_root); + if (command_line.HasSwitch(switches::kPackageRoot)) { + std::string package_root = + command_line.GetSwitchValueASCII(switches::kPackageRoot); + viewport_observer->RunFromFile(main, package_root); + return; + } + + std::cerr << "One of --" << switches::kPackageRoot << " or --" + << switches::kSnapshot << " is required." << std::endl; } +} // namespace } // namespace shell } // namespace sky @@ -45,6 +78,14 @@ int main(int argc, const char* argv[]) { base::AtExitManager exit_manager; base::CommandLine::Init(argc, argv); + base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); + + if (command_line.HasSwitch(sky::shell::switches::kHelp) || + command_line.GetArgs().empty()) { + sky::shell::Usage(); + return 0; + } + base::MessageLoop message_loop; base::i18n::InitializeICU(); diff --git a/shell/switches.cc b/shell/switches.cc new file mode 100644 index 00000000000..0320e96ac4a --- /dev/null +++ b/shell/switches.cc @@ -0,0 +1,17 @@ +// 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 "sky/shell/switches.h" + +namespace sky { +namespace shell { +namespace switches { + +const char kHelp[] = "help"; +const char kPackageRoot[] = "package-root"; +const char kSnapshot[] = "snapshot"; + +} // namespace switches +} // namespace shell +} // namespace sky diff --git a/shell/switches.h b/shell/switches.h new file mode 100644 index 00000000000..05b93f78d6b --- /dev/null +++ b/shell/switches.h @@ -0,0 +1,20 @@ +// 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 SKY_SHELL_SWITCHES_H_ +#define SKY_SHELL_SWITCHES_H_ + +namespace sky { +namespace shell { +namespace switches { + +extern const char kHelp[]; +extern const char kPackageRoot[]; +extern const char kSnapshot[]; + +} // namespace switches +} // namespace shell +} // namespace sky + +#endif // SKY_SHELL_SWITCHES_H_ diff --git a/shell/ui/engine.cc b/shell/ui/engine.cc index 6e35f098aae..b75b731656b 100644 --- a/shell/ui/engine.cc +++ b/shell/ui/engine.cc @@ -218,8 +218,12 @@ void Engine::RunFromFile(const mojo::String& main, RunFromLibrary(main); } -void Engine::RunFromSnapshot(mojo::ScopedDataPipeConsumerHandle snapshot) { - // TODO(abarth): Implement. +void Engine::RunFromSnapshot(const mojo::String& url, + mojo::ScopedDataPipeConsumerHandle snapshot) { + CloseWebViewIfNeeded(); + sky_view_ = blink::SkyView::Create(this); + sky_view_->RunFromSnapshot(blink::WebString::fromUTF8(url), snapshot.Pass()); + UpdateSkyViewSize(); } void Engine::LoadUsingWebView(const mojo::String& mojo_url) { diff --git a/shell/ui/engine.h b/shell/ui/engine.h index cfaf073695f..688600646aa 100644 --- a/shell/ui/engine.h +++ b/shell/ui/engine.h @@ -73,7 +73,8 @@ class Engine : public UIDelegate, void RunFromNetwork(const mojo::String& url) override; void RunFromFile(const mojo::String& main, const mojo::String& package_root) override; - void RunFromSnapshot(mojo::ScopedDataPipeConsumerHandle snapshot) override; + void RunFromSnapshot(const mojo::String& url, + mojo::ScopedDataPipeConsumerHandle snapshot) override; // WebViewClient methods: void frameDetached(blink::WebFrame*) override; diff --git a/tools/packager/loader.cc b/tools/packager/loader.cc index 489359da7ea..9b5e0240c98 100644 --- a/tools/packager/loader.cc +++ b/tools/packager/loader.cc @@ -93,8 +93,10 @@ Loader* g_loader = nullptr; Loader& GetLoader() { if (!g_loader) { base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); - CHECK(command_line.HasSwitch(kPackageRoot)) << "Need --package-root"; - g_loader = new Loader(command_line.GetSwitchValuePath(kPackageRoot)); + CHECK(command_line.HasSwitch(switches::kPackageRoot)) + << "Need --package-root"; + g_loader = + new Loader(command_line.GetSwitchValuePath(switches::kPackageRoot)); } return *g_loader; } diff --git a/tools/packager/main.cc b/tools/packager/main.cc index 7b5e1f3f2b2..fcfd111cae5 100644 --- a/tools/packager/main.cc +++ b/tools/packager/main.cc @@ -18,6 +18,12 @@ #include "sky/tools/packager/switches.h" #include "sky/tools/packager/vm.h" +void Usage() { + std::cerr << "Usage: sky_packager" + << " --" << switches::kPackageRoot << " --" << switches::kSnapshot + << " " << std::endl; +} + void WriteSnapshot(base::FilePath path) { uint8_t* buffer; intptr_t size; @@ -35,13 +41,19 @@ int main(int argc, const char* argv[]) { base::EnableTerminationOnHeapCorruption(); base::CommandLine::Init(argc, argv); + const base::CommandLine& command_line = + *base::CommandLine::ForCurrentProcess(); + + if (command_line.HasSwitch(switches::kHelp) || + command_line.GetArgs().empty()) { + Usage(); + return 0; + } + InitDartVM(); Dart_Isolate isolate = CreateDartIsolate(); CHECK(isolate); - const base::CommandLine& command_line = - *base::CommandLine::ForCurrentProcess(); - DartIsolateScope scope(isolate); DartApiScope api_scope; @@ -51,8 +63,8 @@ int main(int argc, const char* argv[]) { CHECK(!LogIfError(Dart_FinalizeLoading(true))); - CHECK(command_line.HasSwitch(kSnapshot)) << "Need --snapshot"; - WriteSnapshot(command_line.GetSwitchValuePath(kSnapshot)); + CHECK(command_line.HasSwitch(switches::kSnapshot)) << "Need --snapshot"; + WriteSnapshot(command_line.GetSwitchValuePath(switches::kSnapshot)); return 0; } diff --git a/tools/packager/switches.cc b/tools/packager/switches.cc index cf867e228d6..664055f5456 100644 --- a/tools/packager/switches.cc +++ b/tools/packager/switches.cc @@ -4,5 +4,10 @@ #include "sky/tools/packager/switches.h" +namespace switches { + +const char kHelp[] = "help"; const char kPackageRoot[] = "package-root"; const char kSnapshot[] = "snapshot"; + +} // namespace switches diff --git a/tools/packager/switches.h b/tools/packager/switches.h index 976fef88901..0a5d1be7efd 100644 --- a/tools/packager/switches.h +++ b/tools/packager/switches.h @@ -5,7 +5,12 @@ #ifndef SKY_TOOLS_PACKAGER_SWITCHES_H_ #define SKY_TOOLS_PACKAGER_SWITCHES_H_ +namespace switches { + +extern const char kHelp[]; extern const char kPackageRoot[]; extern const char kSnapshot[]; +} // namespace switches + #endif // SKY_TOOLS_PACKAGER_SWITCHES_H_