diff --git a/engine/src/flutter/shell/BUILD.gn b/engine/src/flutter/shell/BUILD.gn index a71ad12c13..4032deb4d9 100644 --- a/engine/src/flutter/shell/BUILD.gn +++ b/engine/src/flutter/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/engine/src/flutter/shell/linux/main.cc b/engine/src/flutter/shell/linux/main.cc index 56fd84f5ae..b40223d582 100644 --- a/engine/src/flutter/shell/linux/main.cc +++ b/engine/src/flutter/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/engine/src/flutter/shell/switches.cc b/engine/src/flutter/shell/switches.cc new file mode 100644 index 0000000000..0320e96ac4 --- /dev/null +++ b/engine/src/flutter/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/engine/src/flutter/shell/switches.h b/engine/src/flutter/shell/switches.h new file mode 100644 index 0000000000..05b93f78d6 --- /dev/null +++ b/engine/src/flutter/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/engine/src/flutter/shell/ui/engine.cc b/engine/src/flutter/shell/ui/engine.cc index 6e35f098aa..b75b731656 100644 --- a/engine/src/flutter/shell/ui/engine.cc +++ b/engine/src/flutter/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/engine/src/flutter/shell/ui/engine.h b/engine/src/flutter/shell/ui/engine.h index cfaf073695..688600646a 100644 --- a/engine/src/flutter/shell/ui/engine.h +++ b/engine/src/flutter/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/engine/src/flutter/tools/packager/loader.cc b/engine/src/flutter/tools/packager/loader.cc index 489359da7e..9b5e0240c9 100644 --- a/engine/src/flutter/tools/packager/loader.cc +++ b/engine/src/flutter/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/engine/src/flutter/tools/packager/main.cc b/engine/src/flutter/tools/packager/main.cc index 7b5e1f3f2b..fcfd111cae 100644 --- a/engine/src/flutter/tools/packager/main.cc +++ b/engine/src/flutter/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/engine/src/flutter/tools/packager/switches.cc b/engine/src/flutter/tools/packager/switches.cc index cf867e228d..664055f545 100644 --- a/engine/src/flutter/tools/packager/switches.cc +++ b/engine/src/flutter/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/engine/src/flutter/tools/packager/switches.h b/engine/src/flutter/tools/packager/switches.h index 976fef8890..0a5d1be7ef 100644 --- a/engine/src/flutter/tools/packager/switches.h +++ b/engine/src/flutter/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_