Chromium Code Reviews| Index: content/browser/mojo_shell_context_impl.cc |
| diff --git a/content/browser/mojo_shell_context_impl.cc b/content/browser/mojo_shell_context_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..77a055a9c159c9e1ce9a0984a386bb4bc4b09161 |
| --- /dev/null |
| +++ b/content/browser/mojo_shell_context_impl.cc |
| @@ -0,0 +1,114 @@ |
| +// 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 "content/browser/mojo_shell_context_impl.h" |
| + |
| +#include "base/callback.h" |
| +#include "base/message_loop/message_loop_proxy.h" |
| +#include "base/run_loop.h" |
| +#include "base/sequenced_task_runner.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/mojo_app_connection.h" |
| +#include "content/public/browser/utility_process_host.h" |
| +#include "content/public/browser/utility_process_host_client.h" |
| +#include "content/public/common/process.mojom.h" |
| +#include "content/public/common/service_registry.h" |
| +#include "content/public/common/static_mojo_application_loader.h" |
| +#include "mojo/shell/application_loader.h" |
| +#include "net/interfaces/proxy_resolver_service.mojom.h" |
| +#include "net/proxy/mojo_proxy_resolver_factory_impl.h" |
| +#include "third_party/mojo/src/mojo/public/cpp/application/application_connection.h" |
| +#include "third_party/mojo/src/mojo/public/cpp/application/application_delegate.h" |
| +#include "third_party/mojo/src/mojo/public/cpp/application/application_impl.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +// The loader for all out-of-process applications. This launches a utility |
| +// process and forwards the Load request to a Process service there. This is the |
| +// default loader used by content's Mojo shell. |
| +class UtilityProcessLoader : public mojo::shell::ApplicationLoader { |
| + public: |
| + UtilityProcessLoader() {} |
| + ~UtilityProcessLoader() override {} |
| + |
| + private: |
| + // mojo::shell::ApplicationLoader |
| + void Load( |
| + const GURL& url, |
| + mojo::InterfaceRequest<mojo::Application> application_request) override { |
| + ProcessPtr process; |
| + auto process_request = mojo::GetProxy(&process); |
| + BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO) |
| + ->PostTask(FROM_HERE, |
| + base::Bind(&UtilityProcessLoader::StartProcessOnIOThread, |
| + base::Passed(&process_request))); |
| + process->LoadApplication( |
| + mojo::String::From(url.spec()), application_request.Pass(), |
| + base::Bind(&UtilityProcessLoader::OnApplicationLoaded)); |
| + } |
| + |
| + static void StartProcessOnIOThread( |
| + mojo::InterfaceRequest<Process> process_request) { |
| + UtilityProcessHost* process_host = |
| + UtilityProcessHost::Create(nullptr, nullptr); |
| + process_host->SetName(base::UTF8ToUTF16("Mojo Application")); |
|
xhwang
2015/05/13 04:36:20
This will appear in task manager and is visible to
|
| + if (process_host->StartMojoMode()) { |
| + ServiceRegistry* services = process_host->GetServiceRegistry(); |
| + services->ConnectToRemoteService(process_request.Pass()); |
| + } else { |
| + LOG(ERROR) << "Unable to start utility process."; |
| + } |
| + } |
| + |
| + static void OnApplicationLoaded(LaunchResult result) { |
| + if (result != LAUNCH_RESULT_OK) { |
| + LOG(ERROR) << "Failed to launch Mojo application."; |
| + } |
| + } |
| +}; |
| + |
| +} // namespace |
| + |
| +MojoShellContextImpl::MojoShellContextImpl() |
| + : application_manager_(new mojo::shell::ApplicationManager(this)) { |
| + MojoAppConnection::SetApplicationManager(application_manager_.get()); |
| + application_manager_->set_default_loader( |
| + scoped_ptr<mojo::shell::ApplicationLoader>(new UtilityProcessLoader)); |
| +} |
| + |
| +MojoShellContextImpl::~MojoShellContextImpl() { |
| + MojoAppConnection::SetApplicationManager(nullptr); |
| +} |
| + |
| +void MojoShellContextImpl::AddCustomLoader( |
| + const GURL& url, |
| + scoped_ptr<mojo::shell::ApplicationLoader> loader) { |
| + application_manager_->SetLoaderForURL(loader.Pass(), url); |
| +} |
| + |
| +// static |
| +scoped_ptr<MojoShellContext> MojoShellContext::Create() { |
| + return scoped_ptr<MojoShellContext>(new MojoShellContextImpl); |
| +} |
| + |
| +// static |
| +scoped_ptr<mojo::shell::ApplicationLoader> MojoShellContext::CreateStaticLoader( |
| + const std::string& name, |
| + const base::Callback<scoped_ptr<mojo::ApplicationDelegate>()>& factory) { |
| + return scoped_ptr<mojo::shell::ApplicationLoader>( |
| + new StaticMojoApplicationLoader(name, factory)); |
| +} |
| + |
| +GURL MojoShellContextImpl::ResolveMappings(const GURL& url) { |
| + return url; |
| +} |
| + |
| +GURL MojoShellContextImpl::ResolveMojoURL(const GURL& url) { |
| + return url; |
| +} |
| + |
| +} // namespace content |