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.
This commit is contained in:
Adam Barth 2015-06-25 17:10:35 -07:00
parent 28643e2cf4
commit 570cfb6299
14 changed files with 131 additions and 21 deletions

View File

@ -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()));
}

View File

@ -36,10 +36,9 @@ class DartController {
typedef base::Callback<void(RefPtr<AbstractModule>, RefPtr<DartValue>)>
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,

View File

@ -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) {

View File

@ -13,5 +13,5 @@ interface ViewportObserver {
RunFromNetwork(string url);
RunFromFile(string main, string package_root);
RunFromSnapshot(handle<data_pipe_consumer> snapshot);
RunFromSnapshot(string url, handle<data_pipe_consumer> snapshot);
};

View File

@ -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",

View File

@ -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 <iostream>
#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 << "]"
<< " <sky-app>" << 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<base::TaskRunner> 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();

17
shell/switches.cc Normal file
View File

@ -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

20
shell/switches.h Normal file
View File

@ -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_

View File

@ -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) {

View File

@ -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;

View File

@ -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;
}

View File

@ -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
<< " <sky-app>" << 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;
}

View File

@ -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

View File

@ -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_