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 #if defined(ENABLE_MOJO_MEDIA_IN_BROWSER_PROCESS)
28 #include "media/mojo/services/mojo_media_application.h"
35 // An extra set of apps to register on initialization, if set by a test.
36 const MojoShellContext::StaticApplicationMap
* g_applications_for_test
;
38 void StartProcessOnIOThread(mojo::InterfaceRequest
<ProcessControl
> request
,
40 UtilityProcessHost
* process_host
=
41 UtilityProcessHost::Create(nullptr, nullptr);
42 // TODO(rockot): Make it possible for the embedder to associate app URLs with
43 // app names so we can have more meaningful process names here.
44 process_host
->SetName(base::UTF8ToUTF16("Mojo Application"));
46 process_host
->DisableSandbox();
47 process_host
->StartMojoMode();
49 ServiceRegistry
* services
= process_host
->GetServiceRegistry();
50 services
->ConnectToRemoteService(request
.Pass());
53 void OnApplicationLoaded(const GURL
& url
, bool success
) {
55 LOG(ERROR
) << "Failed to launch Mojo application for " << url
.spec();
58 // The default loader to use for all applications. This launches a utility
59 // process and forwards the Load request the ProcessControl service there.
60 // The utility process is sandboxed if |use_sandbox| is true and vice versa.
61 class UtilityProcessLoader
: public mojo::shell::ApplicationLoader
{
63 explicit UtilityProcessLoader(bool use_sandbox
) : use_sandbox_(use_sandbox
) {}
64 ~UtilityProcessLoader() override
{}
67 // mojo::shell::ApplicationLoader:
70 mojo::InterfaceRequest
<mojo::Application
> application_request
) override
{
71 ProcessControlPtr process_control
;
72 auto process_request
= mojo::GetProxy(&process_control
);
73 BrowserThread::PostTask(
74 BrowserThread::IO
, FROM_HERE
,
75 base::Bind(&StartProcessOnIOThread
, base::Passed(&process_request
),
77 process_control
->LoadApplication(url
.spec(), application_request
.Pass(),
78 base::Bind(&OnApplicationLoaded
, url
));
81 const bool use_sandbox_
;
83 DISALLOW_COPY_AND_ASSIGN(UtilityProcessLoader
);
88 // Thread-safe proxy providing access to the shell context from any thread.
89 class MojoShellContext::Proxy
{
91 Proxy(MojoShellContext
* shell_context
)
92 : shell_context_(shell_context
),
93 task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
97 void ConnectToApplication(
99 const GURL
& requestor_url
,
100 mojo::InterfaceRequest
<mojo::ServiceProvider
> request
,
101 mojo::ServiceProviderPtr exposed_services
,
102 mojo::CapabilityFilterPtr filter
) {
103 if (task_runner_
== base::ThreadTaskRunnerHandle::Get()) {
104 if (shell_context_
) {
105 shell_context_
->ConnectToApplicationOnOwnThread(
106 url
, requestor_url
, request
.Pass(), exposed_services
.Pass(),
110 // |shell_context_| outlives the main MessageLoop, so it's safe for it to
111 // be unretained here.
112 task_runner_
->PostTask(
114 base::Bind(&MojoShellContext::ConnectToApplicationOnOwnThread
,
115 base::Unretained(shell_context_
), url
, requestor_url
,
116 base::Passed(&request
), base::Passed(&exposed_services
),
117 base::Passed(&filter
)));
122 MojoShellContext
* shell_context_
;
123 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
125 DISALLOW_COPY_AND_ASSIGN(Proxy
);
129 base::LazyInstance
<scoped_ptr
<MojoShellContext::Proxy
>>
130 MojoShellContext::proxy_
= LAZY_INSTANCE_INITIALIZER
;
132 void MojoShellContext::SetApplicationsForTest(
133 const StaticApplicationMap
* apps
) {
134 g_applications_for_test
= apps
;
137 MojoShellContext::MojoShellContext()
138 : application_manager_(new mojo::shell::ApplicationManager(this)) {
139 proxy_
.Get().reset(new Proxy(this));
141 application_manager_
->set_default_loader(
142 scoped_ptr
<mojo::shell::ApplicationLoader
>(
143 new UtilityProcessLoader(true /* use_sandbox */)));
145 StaticApplicationMap apps
;
146 GetContentClient()->browser()->RegisterInProcessMojoApplications(&apps
);
147 if (g_applications_for_test
) {
148 // Add testing apps to the map, potentially overwriting whatever the
149 // browser client registered.
150 for (const auto& entry
: *g_applications_for_test
)
151 apps
[entry
.first
] = entry
.second
;
153 for (const auto& entry
: apps
) {
154 application_manager_
->SetLoaderForURL(
155 scoped_ptr
<mojo::shell::ApplicationLoader
>(
156 new mojo::shell::StaticApplicationLoader(entry
.second
)),
160 std::vector
<GURL
> urls
;
163 ->RegisterUnsandboxedOutOfProcessMojoApplications(&urls
);
164 for (const auto& url
: urls
) {
165 application_manager_
->SetLoaderForURL(
166 scoped_ptr
<mojo::shell::ApplicationLoader
>(
167 new UtilityProcessLoader(false /* use_sandbox */)),
171 #if (ENABLE_MOJO_MEDIA_IN_BROWSER_PROCESS)
172 application_manager_
->SetLoaderForURL(
173 scoped_ptr
<mojo::shell::ApplicationLoader
>(
174 new mojo::shell::StaticApplicationLoader(
175 base::Bind(&media::MojoMediaApplication::CreateApp
))),
176 media::MojoMediaApplication::AppUrl());
180 MojoShellContext::~MojoShellContext() {
184 void MojoShellContext::ConnectToApplication(
186 const GURL
& requestor_url
,
187 mojo::InterfaceRequest
<mojo::ServiceProvider
> request
,
188 mojo::ServiceProviderPtr exposed_services
,
189 mojo::CapabilityFilterPtr filter
) {
190 proxy_
.Get()->ConnectToApplication(url
, requestor_url
, request
.Pass(),
191 exposed_services
.Pass(), filter
.Pass());
194 void MojoShellContext::ConnectToApplicationOnOwnThread(
196 const GURL
& requestor_url
,
197 mojo::InterfaceRequest
<mojo::ServiceProvider
> request
,
198 mojo::ServiceProviderPtr exposed_services
,
199 mojo::CapabilityFilterPtr filter
) {
200 mojo::URLRequestPtr url_request
= mojo::URLRequest::New();
201 url_request
->url
= mojo::String::From(url
);
202 application_manager_
->ConnectToApplication(
203 nullptr, url_request
.Pass(), std::string(), requestor_url
, request
.Pass(),
204 exposed_services
.Pass(), filter
.Pass(), base::Bind(&base::DoNothing
));
207 GURL
MojoShellContext::ResolveMappings(const GURL
& url
) {
211 GURL
MojoShellContext::ResolveMojoURL(const GURL
& url
) {
215 bool MojoShellContext::CreateFetcher(
217 const mojo::shell::Fetcher::FetchCallback
& loader_callback
) {
221 } // namespace content