Roll ANGLE e754fb8..6ffeb74
[chromium-blink-merge.git] / content / browser / mojo / mojo_shell_context.cc
blob7541a0e3e917cae6441ec338d57d8fbc97df35d7
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/mojo/mojo_shell_context.h"
7 #include "base/lazy_instance.h"
8 #include "base/macros.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "content/common/process_control.mojom.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/content_browser_client.h"
15 #include "content/public/browser/utility_process_host.h"
16 #include "content/public/browser/utility_process_host_client.h"
17 #include "content/public/common/content_client.h"
18 #include "content/public/common/service_registry.h"
19 #include "mojo/application/public/cpp/application_delegate.h"
20 #include "mojo/common/url_type_converters.h"
21 #include "mojo/services/network/public/interfaces/url_loader.mojom.h"
22 #include "mojo/shell/application_loader.h"
23 #include "mojo/shell/static_application_loader.h"
24 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
25 #include "third_party/mojo/src/mojo/public/cpp/bindings/string.h"
27 namespace content {
29 namespace {
31 // An extra set of apps to register on initialization, if set by a test.
32 const MojoShellContext::StaticApplicationMap* g_applications_for_test;
34 void StartProcessOnIOThread(mojo::InterfaceRequest<ProcessControl> request) {
35 UtilityProcessHost* process_host =
36 UtilityProcessHost::Create(nullptr, nullptr);
37 // TODO(rockot): Make it possible for the embedder to associate app URLs with
38 // app names so we can have more meaningful process names here.
39 process_host->SetName(base::UTF8ToUTF16("Mojo Application"));
40 process_host->StartMojoMode();
41 ServiceRegistry* services = process_host->GetServiceRegistry();
42 services->ConnectToRemoteService(request.Pass());
45 void OnApplicationLoaded(const GURL& url, bool success) {
46 if (!success)
47 LOG(ERROR) << "Failed to launch Mojo application for " << url.spec();
50 // The default loader to use for all applications. This launches a utility
51 // process and forwards the Load request the ProcessControl service there.
52 class UtilityProcessLoader : public mojo::shell::ApplicationLoader {
53 public:
54 UtilityProcessLoader() {}
55 ~UtilityProcessLoader() override {}
57 private:
58 // mojo::shell::ApplicationLoader:
59 void Load(
60 const GURL& url,
61 mojo::InterfaceRequest<mojo::Application> application_request) override {
62 ProcessControlPtr process_control;
63 auto process_request = mojo::GetProxy(&process_control);
64 BrowserThread::PostTask(
65 BrowserThread::IO, FROM_HERE,
66 base::Bind(&StartProcessOnIOThread, base::Passed(&process_request)));
67 process_control->LoadApplication(url.spec(), application_request.Pass(),
68 base::Bind(&OnApplicationLoaded, url));
71 DISALLOW_COPY_AND_ASSIGN(UtilityProcessLoader);
74 } // namespace
76 // Thread-safe proxy providing access to the shell context from any thread.
77 class MojoShellContext::Proxy {
78 public:
79 Proxy(MojoShellContext* shell_context)
80 : shell_context_(shell_context),
81 task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
83 ~Proxy() {}
85 void ConnectToApplication(
86 const GURL& url,
87 const GURL& requestor_url,
88 mojo::InterfaceRequest<mojo::ServiceProvider> request) {
89 if (task_runner_ == base::ThreadTaskRunnerHandle::Get()) {
90 if (shell_context_)
91 shell_context_->ConnectToApplicationOnOwnThread(url, requestor_url,
92 request.Pass());
93 } else {
94 // |shell_context_| outlives the main MessageLoop, so it's safe for it to
95 // be unretained here.
96 task_runner_->PostTask(
97 FROM_HERE,
98 base::Bind(&MojoShellContext::ConnectToApplicationOnOwnThread,
99 base::Unretained(shell_context_), url, requestor_url,
100 base::Passed(&request)));
104 private:
105 MojoShellContext* shell_context_;
106 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
108 DISALLOW_COPY_AND_ASSIGN(Proxy);
111 // static
112 base::LazyInstance<scoped_ptr<MojoShellContext::Proxy>>
113 MojoShellContext::proxy_ = LAZY_INSTANCE_INITIALIZER;
115 void MojoShellContext::SetApplicationsForTest(
116 const StaticApplicationMap* apps) {
117 g_applications_for_test = apps;
120 MojoShellContext::MojoShellContext()
121 : application_manager_(new mojo::shell::ApplicationManager(this)) {
122 proxy_.Get().reset(new Proxy(this));
123 application_manager_->set_default_loader(
124 scoped_ptr<mojo::shell::ApplicationLoader>(new UtilityProcessLoader));
126 StaticApplicationMap apps;
127 GetContentClient()->browser()->RegisterMojoApplications(&apps);
128 if (g_applications_for_test) {
129 // Add testing apps to the map, potentially overwriting whatever the
130 // browser client registered.
131 for (const auto& entry : *g_applications_for_test)
132 apps[entry.first] = entry.second;
134 for (const auto& entry : apps) {
135 application_manager_->SetLoaderForURL(
136 scoped_ptr<mojo::shell::ApplicationLoader>(
137 new mojo::shell::StaticApplicationLoader(entry.second)),
138 entry.first);
142 MojoShellContext::~MojoShellContext() {
145 // static
146 void MojoShellContext::ConnectToApplication(
147 const GURL& url,
148 const GURL& requestor_url,
149 mojo::InterfaceRequest<mojo::ServiceProvider> request) {
150 proxy_.Get()->ConnectToApplication(url, requestor_url, request.Pass());
153 void MojoShellContext::ConnectToApplicationOnOwnThread(
154 const GURL& url,
155 const GURL& requestor_url,
156 mojo::InterfaceRequest<mojo::ServiceProvider> request) {
157 mojo::URLRequestPtr url_request = mojo::URLRequest::New();
158 url_request->url = mojo::String::From(url);
159 application_manager_->ConnectToApplication(
160 url_request.Pass(), requestor_url, request.Pass(),
161 mojo::ServiceProviderPtr(), base::Bind(&base::DoNothing));
164 GURL MojoShellContext::ResolveMappings(const GURL& url) {
165 return url;
168 GURL MojoShellContext::ResolveMojoURL(const GURL& url) {
169 return url;
172 bool MojoShellContext::CreateFetcher(
173 const GURL& url,
174 const mojo::shell::Fetcher::FetchCallback& loader_callback) {
175 return false;
178 } // namespace content