diff options
author | scottmg <scottmg@chromium.org> | 2015-06-16 12:29:40 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-06-16 19:30:20 +0000 |
commit | 345aa4682f6e2c41892ad1d16a0e0a9b2be37d26 (patch) | |
tree | 6db28fb64b345c0458ec620b1b2b217c4e609e94 | |
parent | 3bd01861d487c6187510d810de5c7ba1145be2bf (diff) | |
download | chromium_src-345aa4682f6e2c41892ad1d16a0e0a9b2be37d26.zip chromium_src-345aa4682f6e2c41892ad1d16a0e0a9b2be37d26.tar.gz chromium_src-345aa4682f6e2c41892ad1d16a0e0a9b2be37d26.tar.bz2 |
mandoline: stub of updater service
This doesn't do much yet, and is disabled unless --use-updater is
passed on the command line.
The general idea is to create an Updater service that the shell connects
to in ApplicationManager, and a new Fetcher instance that will resolve
fetches by asking the Updater to acquire the desired version.
At the moment, it's just the connection to the service, and UpdateFetcher
asks the updater app for the path, but that's always just a non-updated
local disk file.
In the future the updater is intended to use components/update_client
(possibly adapted due to dependency on net/?) and then will actually
update the components somehow, and return the local file location.
R=sky@chromium.org
BUG=479169,498020
Review URL: https://codereview.chromium.org/1147243005
Cr-Commit-Position: refs/heads/master@{#334660}
-rw-r--r-- | content/content_browser.gypi | 1 | ||||
-rw-r--r-- | mandoline/app/BUILD.gn | 4 | ||||
-rw-r--r-- | mandoline/services/updater/BUILD.gn | 23 | ||||
-rw-r--r-- | mandoline/services/updater/DEPS | 9 | ||||
-rw-r--r-- | mandoline/services/updater/updater_app.cc | 42 | ||||
-rw-r--r-- | mandoline/services/updater/updater_app.h | 45 | ||||
-rw-r--r-- | mandoline/services/updater/updater_impl.cc | 39 | ||||
-rw-r--r-- | mandoline/services/updater/updater_impl.h | 39 | ||||
-rw-r--r-- | mojo/mojo_services.gyp | 105 | ||||
-rw-r--r-- | mojo/mojo_shell.gyp | 5 | ||||
-rw-r--r-- | mojo/runner/context.cc | 16 | ||||
-rw-r--r-- | mojo/services/updater/BUILD.gn | 12 | ||||
-rw-r--r-- | mojo/services/updater/updater.mojom | 11 | ||||
-rw-r--r-- | mojo/shell/BUILD.gn | 3 | ||||
-rw-r--r-- | mojo/shell/application_manager.cc | 11 | ||||
-rw-r--r-- | mojo/shell/application_manager.h | 2 | ||||
-rw-r--r-- | mojo/shell/switches.cc | 4 | ||||
-rw-r--r-- | mojo/shell/switches.h | 1 | ||||
-rw-r--r-- | mojo/shell/update_fetcher.cc | 101 | ||||
-rw-r--r-- | mojo/shell/update_fetcher.h | 55 |
20 files changed, 479 insertions, 49 deletions
diff --git a/content/content_browser.gypi b/content/content_browser.gypi index 0f3e00a..bcf4a1b 100644 --- a/content/content_browser.gypi +++ b/content/content_browser.gypi @@ -14,6 +14,7 @@ '../mojo/mojo_base.gyp:mojo_application_base', '../mojo/mojo_base.gyp:mojo_url_type_converters', '../mojo/mojo_services.gyp:network_service_bindings_lib', + '../mojo/mojo_services.gyp:updater_bindings_lib', '../mojo/mojo_shell.gyp:mojo_shell_lib', '../net/net.gyp:net', '../net/net.gyp:net_extras', diff --git a/mandoline/app/BUILD.gn b/mandoline/app/BUILD.gn index 066842d..fd52056 100644 --- a/mandoline/app/BUILD.gn +++ b/mandoline/app/BUILD.gn @@ -14,8 +14,10 @@ group("app") { } data_deps = [ - "//mandoline/ui/browser", "//mandoline/services/core_services", + "//mandoline/services/updater", + "//mandoline/ui/browser", "//mojo/runner:mojo_runner", + "//mojo/services/updater", ] } diff --git a/mandoline/services/updater/BUILD.gn b/mandoline/services/updater/BUILD.gn new file mode 100644 index 0000000..903c156 --- /dev/null +++ b/mandoline/services/updater/BUILD.gn @@ -0,0 +1,23 @@ +# Copyright 2015 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. + +import("//third_party/mojo/src/mojo/public/mojo_application.gni") +import("//third_party/mojo/src/mojo/public/tools/bindings/mojom.gni") + +mojo_native_application("updater") { + sources = [ + "updater_app.cc", + "updater_app.h", + "updater_impl.cc", + "updater_impl.h", + ] + + deps = [ + "//base", + "//components/update_client", + "//mojo/application/public/cpp", + "//mojo/services/updater", + "//third_party/mojo/src/mojo/public/cpp/system", + ] +} diff --git a/mandoline/services/updater/DEPS b/mandoline/services/updater/DEPS new file mode 100644 index 0000000..07502be --- /dev/null +++ b/mandoline/services/updater/DEPS @@ -0,0 +1,9 @@ +include_rules = [ + "+components/update_client", + "+mojo/application", + "+mojo/common", + "+mojo/public/c/system", + "+mojo/public/cpp/bindings", + "+mojo/services/network", + "+mojo/services/updater", +] diff --git a/mandoline/services/updater/updater_app.cc b/mandoline/services/updater/updater_app.cc new file mode 100644 index 0000000..0a1675c --- /dev/null +++ b/mandoline/services/updater/updater_app.cc @@ -0,0 +1,42 @@ +// Copyright 2015 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 "mandoline/services/updater/updater_app.h" + +#include "base/logging.h" +#include "mandoline/services/updater/updater_impl.h" +#include "mojo/application/public/cpp/application_connection.h" +#include "mojo/application/public/cpp/application_impl.h" +#include "mojo/application/public/cpp/application_runner.h" +#include "mojo/public/c/system/main.h" + +namespace updater { + +UpdaterApp::UpdaterApp() : app_impl_(nullptr) { +} + +UpdaterApp::~UpdaterApp() { +} + +void UpdaterApp::Initialize(mojo::ApplicationImpl* app) { + app_impl_ = app; +} + +bool UpdaterApp::ConfigureIncomingConnection( + mojo::ApplicationConnection* connection) { + connection->AddService<Updater>(this); + return true; +} + +void UpdaterApp::Create(mojo::ApplicationConnection* connection, + mojo::InterfaceRequest<Updater> request) { + new UpdaterImpl(app_impl_, this, request.Pass()); +} + +} // namespace updater + +MojoResult MojoMain(MojoHandle shell_handle) { + mojo::ApplicationRunner runner(new updater::UpdaterApp); + return runner.Run(shell_handle); +} diff --git a/mandoline/services/updater/updater_app.h b/mandoline/services/updater/updater_app.h new file mode 100644 index 0000000..45b435c --- /dev/null +++ b/mandoline/services/updater/updater_app.h @@ -0,0 +1,45 @@ +// Copyright 2015 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 MANDOLINE_SERVICES_UPDATER_UPDATER_APP_H_ +#define MANDOLINE_SERVICES_UPDATER_UPDATER_APP_H_ + +#include "base/macros.h" +#include "mojo/application/public/cpp/application_delegate.h" +#include "mojo/application/public/cpp/interface_factory.h" + +namespace mojo { +class ApplicationConnection; +class ApplicationImpl; +} // namespace mojo + +namespace updater { + +class Updater; + +class UpdaterApp : public mojo::ApplicationDelegate, + public mojo::InterfaceFactory<Updater> { + public: + UpdaterApp(); + ~UpdaterApp() override; + + void Initialize(mojo::ApplicationImpl* app) override; + + // mojo::ApplicationDelegate: + bool ConfigureIncomingConnection( + mojo::ApplicationConnection* connection) override; + + // InterfaceFactory<Updater> implementation. + void Create(mojo::ApplicationConnection* connection, + mojo::InterfaceRequest<Updater> request) override; + + private: + mojo::ApplicationImpl* app_impl_; + + DISALLOW_COPY_AND_ASSIGN(UpdaterApp); +}; + +} // namespace updater + +#endif // MANDOLINE_SERVICES_UPDATER_UPDATER_APP_H_ diff --git a/mandoline/services/updater/updater_impl.cc b/mandoline/services/updater/updater_impl.cc new file mode 100644 index 0000000..e91b351 --- /dev/null +++ b/mandoline/services/updater/updater_impl.cc @@ -0,0 +1,39 @@ +// Copyright 2015 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 "mandoline/services/updater/updater_impl.h" + +#include "base/files/file_util.h" +#include "base/path_service.h" +#include "mandoline/services/updater/updater_app.h" +#include "mojo/application/public/cpp/application_impl.h" +#include "url/gurl.h" + +namespace updater { + +UpdaterImpl::UpdaterImpl(mojo::ApplicationImpl* app_impl, + UpdaterApp* application, + mojo::InterfaceRequest<Updater> request) + : application_(application), + app_impl_(app_impl), + binding_(this, request.Pass()) { +} + +UpdaterImpl::~UpdaterImpl() { +} + +void UpdaterImpl::GetPathForApp( + const mojo::String& url, + const Updater::GetPathForAppCallback& callback) { + // TODO(scottmg): For now, just stub to local files. This will become an + // integration with components/update_client. + base::FilePath shell_dir; + PathService::Get(base::DIR_MODULE, &shell_dir); + const GURL as_url(url); + const base::FilePath path = shell_dir.Append(base::FilePath::FromUTF8Unsafe( + as_url.path().substr(2, as_url.path().size() - 3) + ".mojo")); + callback.Run(path.AsUTF8Unsafe()); +} + +} // namespace updater diff --git a/mandoline/services/updater/updater_impl.h b/mandoline/services/updater/updater_impl.h new file mode 100644 index 0000000..b8b7dbc --- /dev/null +++ b/mandoline/services/updater/updater_impl.h @@ -0,0 +1,39 @@ +// Copyright 2015 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 MANDOLINE_SERVICES_UPDATER_UPDATER_IMPL_H_ +#define MANDOLINE_SERVICES_UPDATER_UPDATER_IMPL_H_ + +#include "base/macros.h" +#include "mojo/application/public/cpp/application_impl.h" +#include "mojo/public/cpp/bindings/strong_binding.h" +#include "mojo/services/updater/updater.mojom.h" + +namespace updater { + +class ApplicationImpl; +class UpdaterApp; + +class UpdaterImpl : public Updater { + public: + UpdaterImpl(mojo::ApplicationImpl* app_impl, + UpdaterApp* application, + mojo::InterfaceRequest<Updater> request); + ~UpdaterImpl() override; + + // Updater implementation: + void GetPathForApp(const mojo::String& url, + const Updater::GetPathForAppCallback& callback) override; + + private: + UpdaterApp* const application_; + mojo::ApplicationImpl* const app_impl_; + mojo::StrongBinding<Updater> binding_; + + DISALLOW_COPY_AND_ASSIGN(UpdaterImpl); +}; + +} // namespace updater + +#endif // MANDOLINE_SERVICES_UPDATER_UPDATER_IMPL_H_ diff --git a/mojo/mojo_services.gyp b/mojo/mojo_services.gyp index 7649421..8166ccb 100644 --- a/mojo/mojo_services.gyp +++ b/mojo/mojo_services.gyp @@ -3,46 +3,71 @@ # found in the LICENSE file. { - 'targets': [{ - 'target_name': 'network_service_bindings_mojom', - 'type': 'none', - 'variables': { - 'mojom_files': [ - 'services/network/public/interfaces/cookie_store.mojom', - 'services/network/public/interfaces/http_connection.mojom', - 'services/network/public/interfaces/http_message.mojom', - 'services/network/public/interfaces/http_server.mojom', - 'services/network/public/interfaces/net_address.mojom', - 'services/network/public/interfaces/network_error.mojom', - 'services/network/public/interfaces/network_service.mojom', - 'services/network/public/interfaces/tcp_bound_socket.mojom', - 'services/network/public/interfaces/tcp_connected_socket.mojom', - 'services/network/public/interfaces/tcp_server_socket.mojom', - 'services/network/public/interfaces/udp_socket.mojom', - 'services/network/public/interfaces/url_loader.mojom', - 'services/network/public/interfaces/url_loader_factory.mojom', - 'services/network/public/interfaces/web_socket.mojom', + 'targets': [ + { + 'target_name': 'network_service_bindings_mojom', + 'type': 'none', + 'variables': { + 'mojom_files': [ + 'services/network/public/interfaces/cookie_store.mojom', + 'services/network/public/interfaces/http_connection.mojom', + 'services/network/public/interfaces/http_message.mojom', + 'services/network/public/interfaces/http_server.mojom', + 'services/network/public/interfaces/net_address.mojom', + 'services/network/public/interfaces/network_error.mojom', + 'services/network/public/interfaces/network_service.mojom', + 'services/network/public/interfaces/tcp_bound_socket.mojom', + 'services/network/public/interfaces/tcp_connected_socket.mojom', + 'services/network/public/interfaces/tcp_server_socket.mojom', + 'services/network/public/interfaces/udp_socket.mojom', + 'services/network/public/interfaces/url_loader.mojom', + 'services/network/public/interfaces/url_loader_factory.mojom', + 'services/network/public/interfaces/web_socket.mojom', + ], + 'mojom_include_path': '<(DEPTH)/mojo/services', + }, + 'includes': [ + '../third_party/mojo/mojom_bindings_generator_explicit.gypi', ], - 'mojom_include_path': '<(DEPTH)/mojo/services', }, - 'includes': [ - '../third_party/mojo/mojom_bindings_generator_explicit.gypi', - ], - }, { - # GN version: //mojo/services/network/public/interfaces - 'target_name': 'network_service_bindings_lib', - 'type': 'static_library', - 'dependencies': [ - 'network_service_bindings_mojom', - ], - }, { - # Target used to depend only on the bindings generation action, not on any - # outputs. - 'target_name': 'network_service_bindings_generation', - 'type': 'none', - 'hard_dependency': 1, - 'dependencies': [ - 'network_service_bindings_mojom', - ], - }], + { + # GN version: //mojo/services/network/public/interfaces + 'target_name': 'network_service_bindings_lib', + 'type': 'static_library', + 'dependencies': [ + 'network_service_bindings_mojom', + ], + }, + { + # Target used to depend only on the bindings generation action, not on any + # outputs. + 'target_name': 'network_service_bindings_generation', + 'type': 'none', + 'hard_dependency': 1, + 'dependencies': [ + 'network_service_bindings_mojom', + ], + }, + { + 'target_name': 'updater_bindings_mojom', + 'type': 'none', + 'variables': { + 'mojom_files': [ + 'services/updater/updater.mojom', + ], + 'mojom_include_path': '<(DEPTH)/mojo/services', + }, + 'includes': [ + '../third_party/mojo/mojom_bindings_generator_explicit.gypi', + ], + }, + { + # GN version: //mojo/services/updater + 'target_name': 'updater_bindings_lib', + 'type': 'static_library', + 'dependencies': [ + 'updater_bindings_mojom', + ], + }, + ], } diff --git a/mojo/mojo_shell.gyp b/mojo/mojo_shell.gyp index 0569a6b..66fa2d1 100644 --- a/mojo/mojo_shell.gyp +++ b/mojo/mojo_shell.gyp @@ -28,7 +28,9 @@ 'shell/static_application_loader.cc', 'shell/static_application_loader.h', 'shell/switches.cc', - 'shell/switches.h', + 'shell/switches.cc', + 'shell/update_fetcher.cc', + 'shell/update_fetcher.h', 'util/filename_util.cc', 'util/filename_util.h', ], @@ -42,6 +44,7 @@ '<(DEPTH)/mojo/mojo_base.gyp:mojo_environment_chromium', '<(DEPTH)/mojo/mojo_base.gyp:mojo_url_type_converters', '<(DEPTH)/mojo/mojo_services.gyp:network_service_bindings_lib', + '<(DEPTH)/mojo/mojo_services.gyp:updater_bindings_lib', '<(DEPTH)/url/url.gyp:url_lib', ], }, { diff --git a/mojo/runner/context.cc b/mojo/runner/context.cc index c124529..47a9561 100644 --- a/mojo/runner/context.cc +++ b/mojo/runner/context.cc @@ -62,23 +62,25 @@ bool ConfigureURLMappings(const base::CommandLine& command_line, // Configure the resolution of unknown mojo: URLs. GURL base_url; - if (command_line.HasSwitch(switches::kOrigin)) + if (command_line.HasSwitch(switches::kOrigin)) { base_url = GURL(command_line.GetSwitchValueASCII(switches::kOrigin)); - else + } else if (!command_line.HasSwitch(switches::kUseUpdater)) { // Use the shell's file root if the base was not specified. base_url = context->ResolveShellFileURL(""); + } - if (!base_url.is_valid()) - return false; - - resolver->SetMojoBaseURL(base_url); + if (base_url.is_valid()) + resolver->SetMojoBaseURL(base_url); - // The network service must be loaded from the filesystem. + // The network service and updater must be loaded from the filesystem. // This mapping is done before the command line URL mapping are processed, so // that it can be overridden. resolver->AddURLMapping(GURL("mojo:network_service"), context->ResolveShellFileURL( "file:network_service/network_service.mojo")); + resolver->AddURLMapping( + GURL("mojo:updater"), + context->ResolveShellFileURL("file:updater/updater.mojo")); // Command line URL mapping. std::vector<URLResolver::OriginMapping> origin_mappings = diff --git a/mojo/services/updater/BUILD.gn b/mojo/services/updater/BUILD.gn new file mode 100644 index 0000000..f0cffe6 --- /dev/null +++ b/mojo/services/updater/BUILD.gn @@ -0,0 +1,12 @@ +# Copyright 2015 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. + +import("//build/module_args/mojo.gni") +import("$mojo_sdk_root/mojo/public/tools/bindings/mojom.gni") + +mojom("updater") { + sources = [ + "updater.mojom", + ] +} diff --git a/mojo/services/updater/updater.mojom b/mojo/services/updater/updater.mojom new file mode 100644 index 0000000..7b26305 --- /dev/null +++ b/mojo/services/updater/updater.mojom @@ -0,0 +1,11 @@ +// Copyright 2015 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. + +module updater; + +interface Updater { + // Returns the path for the given app url. url is expected to be something + // like mojo://foo for the foo app. + GetPathForApp(string url) => (string path); +}; diff --git a/mojo/shell/BUILD.gn b/mojo/shell/BUILD.gn index 2e599b6f..113d6c4 100644 --- a/mojo/shell/BUILD.gn +++ b/mojo/shell/BUILD.gn @@ -30,6 +30,8 @@ source_set("shell") { "static_application_loader.h", "switches.cc", "switches.h", + "update_fetcher.cc", + "update_fetcher.h", ] public_deps = [ @@ -38,6 +40,7 @@ source_set("shell") { "//mojo/common", "//third_party/mojo/src/mojo/public/cpp/bindings", "//mojo/services/network/public/interfaces", + "//mojo/services/updater", "//url", ] deps = [ diff --git a/mojo/shell/application_manager.cc b/mojo/shell/application_manager.cc index 39b133f..d953a90 100644 --- a/mojo/shell/application_manager.cc +++ b/mojo/shell/application_manager.cc @@ -20,6 +20,7 @@ #include "mojo/shell/query_util.h" #include "mojo/shell/shell_impl.h" #include "mojo/shell/switches.h" +#include "mojo/shell/update_fetcher.h" namespace mojo { namespace shell { @@ -179,6 +180,16 @@ void ApplicationManager::ConnectToApplicationInternal( return; } + if (mapped_url.SchemeIs("mojo") && + base::CommandLine::ForCurrentProcess()->HasSwitch( + switches::kUseUpdater)) { + ConnectToService(GURL("mojo:updater"), &updater_); + new UpdateFetcher( + mapped_url, updater_.get(), + base::Bind(callback, NativeApplicationCleanup::DONT_DELETE)); + return; + } + if (!url_loader_factory_) ConnectToService(GURL("mojo:network_service"), &url_loader_factory_); diff --git a/mojo/shell/application_manager.h b/mojo/shell/application_manager.h index 9581ac5..727b9e6 100644 --- a/mojo/shell/application_manager.h +++ b/mojo/shell/application_manager.h @@ -16,6 +16,7 @@ #include "mojo/public/cpp/bindings/interface_ptr_info.h" #include "mojo/public/cpp/bindings/interface_request.h" #include "mojo/services/network/public/interfaces/url_loader_factory.mojom.h" +#include "mojo/services/updater/updater.mojom.h" #include "mojo/shell/application_loader.h" #include "mojo/shell/fetcher.h" #include "mojo/shell/identity.h" @@ -243,6 +244,7 @@ class ApplicationManager { base::SequencedWorkerPool* blocking_pool_; URLLoaderFactoryPtr url_loader_factory_; + updater::UpdaterPtr updater_; MimeTypeToURLMap mime_type_to_url_; ScopedVector<NativeRunner> native_runners_; bool disable_cache_; diff --git a/mojo/shell/switches.cc b/mojo/shell/switches.cc index cd68ff3..878a127 100644 --- a/mojo/shell/switches.cc +++ b/mojo/shell/switches.cc @@ -23,4 +23,8 @@ const char kEnableMultiprocess[] = "enable-multiprocess"; // their content. const char kPredictableAppFilenames[] = "predictable-app-filenames"; +// Pull apps via component updater, rather than using default local resolution +// to find them. +const char kUseUpdater[] = "use-updater"; + } // namespace switches diff --git a/mojo/shell/switches.h b/mojo/shell/switches.h index f774b35..cd96925 100644 --- a/mojo/shell/switches.h +++ b/mojo/shell/switches.h @@ -15,6 +15,7 @@ namespace switches { extern const char kDontDeleteOnDownload[]; extern const char kEnableMultiprocess[]; extern const char kPredictableAppFilenames[]; +extern const char kUseUpdater[]; } // namespace switches diff --git a/mojo/shell/update_fetcher.cc b/mojo/shell/update_fetcher.cc new file mode 100644 index 0000000..5e1d434 --- /dev/null +++ b/mojo/shell/update_fetcher.cc @@ -0,0 +1,101 @@ +// Copyright 2015 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 "mojo/shell/update_fetcher.h" + +#include "base/bind.h" +#include "base/files/file_util.h" +#include "base/format_macros.h" +#include "base/message_loop/message_loop.h" +#include "base/strings/stringprintf.h" +#include "mojo/common/common_type_converters.h" +#include "mojo/common/data_pipe_utils.h" +#include "mojo/common/url_type_converters.h" + +namespace mojo { +namespace shell { + +namespace { + +void IgnoreResult(bool result) { +} + +} // namespace +UpdateFetcher::UpdateFetcher(const GURL& url, + updater::Updater* updater, + const FetchCallback& loader_callback) + : Fetcher(loader_callback), url_(url), weak_ptr_factory_(this) { + DVLOG(1) << "updating: " << url_; + updater->GetPathForApp( + url.spec(), + base::Bind(&UpdateFetcher::OnGetAppPath, weak_ptr_factory_.GetWeakPtr())); +} + +UpdateFetcher::~UpdateFetcher() { +} + +const GURL& UpdateFetcher::GetURL() const { + return url_; +} + +GURL UpdateFetcher::GetRedirectURL() const { + return GURL::EmptyGURL(); +} + +GURL UpdateFetcher::GetRedirectReferer() const { + return GURL::EmptyGURL(); +} +URLResponsePtr UpdateFetcher::AsURLResponse(base::TaskRunner* task_runner, + uint32_t skip) { + URLResponsePtr response(URLResponse::New()); + response->url = String::From(url_); + DataPipe data_pipe; + response->body = data_pipe.consumer_handle.Pass(); + int64 file_size; + if (base::GetFileSize(path_, &file_size)) { + response->headers = Array<HttpHeaderPtr>(1); + HttpHeaderPtr header = HttpHeader::New(); + header->name = "Content-Length"; + header->value = base::StringPrintf("%" PRId64, file_size); + response->headers[0] = header.Pass(); + } + common::CopyFromFile(path_, data_pipe.producer_handle.Pass(), skip, + task_runner, base::Bind(&IgnoreResult)); + return response.Pass(); +} + +void UpdateFetcher::AsPath( + base::TaskRunner* task_runner, + base::Callback<void(const base::FilePath&, bool)> callback) { + base::MessageLoop::current()->PostTask( + FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); +} + +std::string UpdateFetcher::MimeType() { + return ""; +} + +bool UpdateFetcher::HasMojoMagic() { + std::string magic; + ReadFileToString(path_, &magic, strlen(kMojoMagic)); + return magic == kMojoMagic; +} + +bool UpdateFetcher::PeekFirstLine(std::string* line) { + std::string start_of_file; + ReadFileToString(path_, &start_of_file, kMaxShebangLength); + size_t return_position = start_of_file.find('\n'); + if (return_position == std::string::npos) + return false; + *line = start_of_file.substr(0, return_position + 1); + return true; +} + +void UpdateFetcher::OnGetAppPath(const mojo::String& path) { + path_ = base::FilePath::FromUTF8Unsafe(path); + loader_callback_.Run(make_scoped_ptr(this)); +} + +} // namespace shell +} // namespace mojo diff --git a/mojo/shell/update_fetcher.h b/mojo/shell/update_fetcher.h new file mode 100644 index 0000000..26c82df --- /dev/null +++ b/mojo/shell/update_fetcher.h @@ -0,0 +1,55 @@ +// Copyright 2015 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 MOJO_SHELL_UPDATE_FETCHER_H_ +#define MOJO_SHELL_UPDATE_FETCHER_H_ + +#include "mojo/shell/fetcher.h" + +#include "base/files/file_path.h" +#include "base/memory/weak_ptr.h" +#include "mojo/services/updater/updater.mojom.h" +#include "url/gurl.h" + +namespace mojo { + +class URLLoaderFactory; + +namespace shell { + +class UpdateFetcher : public Fetcher { + public: + UpdateFetcher(const GURL& url, + updater::Updater* updater, + const FetchCallback& loader_callback); + + ~UpdateFetcher() override; + + private: + // Fetcher implementation: + const GURL& GetURL() const override; + GURL GetRedirectURL() const override; + GURL GetRedirectReferer() const override; + URLResponsePtr AsURLResponse(base::TaskRunner* task_runner, + uint32_t skip) override; + void AsPath( + base::TaskRunner* task_runner, + base::Callback<void(const base::FilePath&, bool)> callback) override; + std::string MimeType() override; + bool HasMojoMagic() override; + bool PeekFirstLine(std::string* line) override; + + void OnGetAppPath(const mojo::String& path); + + const GURL url_; + base::FilePath path_; + base::WeakPtrFactory<UpdateFetcher> weak_ptr_factory_; + + DISALLOW_COPY_AND_ASSIGN(UpdateFetcher); +}; + +} // namespace shell +} // namespace mojo + +#endif // MOJO_SHELL_UPDATE_FETCHER_H_ |