OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/mojo/mojo_shell_context.h" | 5 #include "content/browser/mojo/mojo_shell_context.h" |
6 | 6 |
7 #include <unordered_map> | 7 #include <unordered_map> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "content/public/common/content_switches.h" | 29 #include "content/public/common/content_switches.h" |
30 #include "content/public/common/service_registry.h" | 30 #include "content/public/common/service_registry.h" |
31 #include "mojo/edk/embedder/embedder.h" | 31 #include "mojo/edk/embedder/embedder.h" |
32 #include "mojo/public/cpp/bindings/interface_request.h" | 32 #include "mojo/public/cpp/bindings/interface_request.h" |
33 #include "mojo/public/cpp/bindings/string.h" | 33 #include "mojo/public/cpp/bindings/string.h" |
34 #include "services/catalog/catalog.h" | 34 #include "services/catalog/catalog.h" |
35 #include "services/catalog/manifest_provider.h" | 35 #include "services/catalog/manifest_provider.h" |
36 #include "services/catalog/store.h" | 36 #include "services/catalog/store.h" |
37 #include "services/shell/connect_params.h" | 37 #include "services/shell/connect_params.h" |
38 #include "services/shell/native_runner.h" | 38 #include "services/shell/native_runner.h" |
| 39 #include "services/shell/public/cpp/connector.h" |
39 #include "services/shell/public/cpp/identity.h" | 40 #include "services/shell/public/cpp/identity.h" |
40 #include "services/shell/public/cpp/shell_client.h" | 41 #include "services/shell/public/cpp/shell_client.h" |
41 #include "services/shell/public/interfaces/connector.mojom.h" | 42 #include "services/shell/public/interfaces/connector.mojom.h" |
42 #include "services/shell/public/interfaces/shell_client.mojom.h" | 43 #include "services/shell/public/interfaces/shell_client.mojom.h" |
43 #include "services/shell/public/interfaces/shell_client_factory.mojom.h" | 44 #include "services/shell/public/interfaces/shell_client_factory.mojom.h" |
44 #include "services/shell/runner/common/client_util.h" | 45 #include "services/shell/runner/common/client_util.h" |
45 #include "services/shell/runner/host/in_process_native_runner.h" | 46 #include "services/shell/runner/host/in_process_native_runner.h" |
46 #include "services/user/public/cpp/constants.h" | 47 #include "services/user/public/cpp/constants.h" |
47 | 48 |
48 namespace content { | 49 namespace content { |
49 | 50 |
50 namespace { | 51 namespace { |
51 | 52 |
| 53 using ConnectorPtr = base::ThreadLocalPointer<shell::Connector>; |
| 54 |
| 55 base::LazyInstance<ConnectorPtr>::Leaky io_connector_tls_ptr = |
| 56 LAZY_INSTANCE_INITIALIZER; |
| 57 |
| 58 void SetConnectorOnIOThread(std::unique_ptr<shell::Connector> connector) { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 60 io_connector_tls_ptr.Pointer()->Set(connector.release()); |
| 61 } |
| 62 |
| 63 void DestroyConnectorOnIOThread() { |
| 64 delete MojoShellContext::GetConnectorForIOThread(); |
| 65 io_connector_tls_ptr.Pointer()->Set(nullptr); |
| 66 } |
| 67 |
52 void StartUtilityProcessOnIOThread( | 68 void StartUtilityProcessOnIOThread( |
53 mojo::InterfaceRequest<mojom::ProcessControl> request, | 69 mojo::InterfaceRequest<mojom::ProcessControl> request, |
54 const base::string16& process_name, | 70 const base::string16& process_name, |
55 bool use_sandbox) { | 71 bool use_sandbox) { |
56 UtilityProcessHost* process_host = | 72 UtilityProcessHost* process_host = |
57 UtilityProcessHost::Create(nullptr, nullptr); | 73 UtilityProcessHost::Create(nullptr, nullptr); |
58 process_host->SetName(process_name); | 74 process_host->SetName(process_name); |
59 if (!use_sandbox) | 75 if (!use_sandbox) |
60 process_host->DisableSandbox(); | 76 process_host->DisableSandbox(); |
61 process_host->Start(); | 77 process_host->Start(); |
62 | 78 process_host->GetRemoteInterfaces()->GetInterface(std::move(request)); |
63 ServiceRegistry* services = process_host->GetServiceRegistry(); | |
64 services->ConnectToRemoteService(std::move(request)); | |
65 } | 79 } |
66 | 80 |
67 void OnApplicationLoaded(const std::string& name, bool success) { | 81 void OnApplicationLoaded(const std::string& name, bool success) { |
68 if (!success) | 82 if (!success) |
69 LOG(ERROR) << "Failed to launch Mojo application for " << name; | 83 LOG(ERROR) << "Failed to launch Mojo application for " << name; |
70 } | 84 } |
71 | 85 |
72 void LaunchAppInUtilityProcess(const std::string& app_name, | 86 void LaunchAppInUtilityProcess(const std::string& app_name, |
73 const base::string16& process_name, | 87 const base::string16& process_name, |
74 bool use_sandbox, | 88 bool use_sandbox, |
(...skipping 21 matching lines...) Expand all Loading... |
96 if (!process_host) { | 110 if (!process_host) { |
97 DLOG(ERROR) << "GPU process host not available."; | 111 DLOG(ERROR) << "GPU process host not available."; |
98 return; | 112 return; |
99 } | 113 } |
100 | 114 |
101 // TODO(xhwang): It's possible that |process_host| is non-null, but the actual | 115 // TODO(xhwang): It's possible that |process_host| is non-null, but the actual |
102 // process is dead. In that case, |request| will be dropped and application | 116 // process is dead. In that case, |request| will be dropped and application |
103 // load requests through mojom::ProcessControl will also fail. Make sure we | 117 // load requests through mojom::ProcessControl will also fail. Make sure we |
104 // handle | 118 // handle |
105 // these cases correctly. | 119 // these cases correctly. |
106 process_host->GetServiceRegistry()->ConnectToRemoteService( | 120 process_host->GetRemoteInterfaces()->GetInterface(std::move(request)); |
107 std::move(request)); | |
108 } | 121 } |
109 | 122 |
110 void LaunchAppInGpuProcess(const std::string& app_name, | 123 void LaunchAppInGpuProcess(const std::string& app_name, |
111 shell::mojom::ShellClientRequest request) { | 124 shell::mojom::ShellClientRequest request) { |
112 mojom::ProcessControlPtr process_control; | 125 mojom::ProcessControlPtr process_control; |
113 mojom::ProcessControlRequest process_request = | 126 mojom::ProcessControlRequest process_request = |
114 mojo::GetProxy(&process_control); | 127 mojo::GetProxy(&process_control); |
115 BrowserThread::PostTask( | 128 BrowserThread::PostTask( |
116 BrowserThread::IO, FROM_HERE, | 129 BrowserThread::IO, FROM_HERE, |
117 base::Bind(&RequestGpuProcessControl, base::Passed(&process_request))); | 130 base::Bind(&RequestGpuProcessControl, base::Passed(&process_request))); |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 if (shell::ShellIsRemote()) { | 262 if (shell::ShellIsRemote()) { |
250 mojo::edk::SetParentPipeHandleFromCommandLine(); | 263 mojo::edk::SetParentPipeHandleFromCommandLine(); |
251 request = shell::GetShellClientRequestFromCommandLine(); | 264 request = shell::GetShellClientRequestFromCommandLine(); |
252 } else { | 265 } else { |
253 shell_.reset(new shell::Shell(std::move(native_runner_factory), | 266 shell_.reset(new shell::Shell(std::move(native_runner_factory), |
254 catalog_->TakeShellClient())); | 267 catalog_->TakeShellClient())); |
255 request = shell_->InitInstanceForEmbedder(kBrowserMojoApplicationName); | 268 request = shell_->InitInstanceForEmbedder(kBrowserMojoApplicationName); |
256 } | 269 } |
257 MojoShellConnection::SetForProcess( | 270 MojoShellConnection::SetForProcess( |
258 MojoShellConnection::Create(std::move(request))); | 271 MojoShellConnection::Create(std::move(request))); |
| 272 std::unique_ptr<shell::Connector> io_connector = |
| 273 MojoShellConnection::GetForProcess()->GetConnector()->Clone(); |
| 274 BrowserThread::PostTask( |
| 275 BrowserThread::IO, FROM_HERE, |
| 276 base::Bind(&SetConnectorOnIOThread, base::Passed(&io_connector))); |
259 | 277 |
260 ContentBrowserClient::StaticMojoApplicationMap apps; | 278 ContentBrowserClient::StaticMojoApplicationMap apps; |
261 GetContentClient()->browser()->RegisterInProcessMojoApplications(&apps); | 279 GetContentClient()->browser()->RegisterInProcessMojoApplications(&apps); |
262 for (const auto& entry : apps) { | 280 for (const auto& entry : apps) { |
263 MojoShellConnection::GetForProcess()->AddEmbeddedService(entry.first, | 281 MojoShellConnection::GetForProcess()->AddEmbeddedService(entry.first, |
264 entry.second); | 282 entry.second); |
265 } | 283 } |
266 | 284 |
267 ContentBrowserClient::OutOfProcessMojoApplicationMap sandboxed_apps; | 285 ContentBrowserClient::OutOfProcessMojoApplicationMap sandboxed_apps; |
268 GetContentClient() | 286 GetContentClient() |
(...skipping 17 matching lines...) Expand all Loading... |
286 false /* use_sandbox */)); | 304 false /* use_sandbox */)); |
287 } | 305 } |
288 | 306 |
289 #if (ENABLE_MOJO_MEDIA_IN_GPU_PROCESS) | 307 #if (ENABLE_MOJO_MEDIA_IN_GPU_PROCESS) |
290 MojoShellConnection::GetForProcess()->AddShellClientRequestHandler( | 308 MojoShellConnection::GetForProcess()->AddShellClientRequestHandler( |
291 "mojo:media", base::Bind(&LaunchAppInGpuProcess, "mojo:media")); | 309 "mojo:media", base::Bind(&LaunchAppInGpuProcess, "mojo:media")); |
292 #endif | 310 #endif |
293 } | 311 } |
294 | 312 |
295 MojoShellContext::~MojoShellContext() { | 313 MojoShellContext::~MojoShellContext() { |
| 314 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 315 base::Bind(&DestroyConnectorOnIOThread)); |
296 if (MojoShellConnection::GetForProcess()) | 316 if (MojoShellConnection::GetForProcess()) |
297 MojoShellConnection::DestroyForProcess(); | 317 MojoShellConnection::DestroyForProcess(); |
298 catalog_.reset(); | 318 catalog_.reset(); |
299 } | 319 } |
300 | 320 |
301 // static | 321 // static |
302 void MojoShellContext::ConnectToApplication( | 322 void MojoShellContext::ConnectToApplication( |
303 const std::string& user_id, | 323 const std::string& user_id, |
304 const std::string& name, | 324 const std::string& name, |
305 const std::string& requestor_name, | 325 const std::string& requestor_name, |
306 shell::mojom::InterfaceProviderRequest request, | 326 shell::mojom::InterfaceProviderRequest request, |
307 shell::mojom::InterfaceProviderPtr exposed_services, | 327 shell::mojom::InterfaceProviderPtr exposed_services, |
308 const shell::mojom::Connector::ConnectCallback& callback) { | 328 const shell::mojom::Connector::ConnectCallback& callback) { |
309 proxy_.Get()->ConnectToApplication(user_id, name, requestor_name, | 329 proxy_.Get()->ConnectToApplication(user_id, name, requestor_name, |
310 std::move(request), | 330 std::move(request), |
311 std::move(exposed_services), callback); | 331 std::move(exposed_services), callback); |
312 } | 332 } |
313 | 333 |
| 334 // static |
| 335 shell::Connector* MojoShellContext::GetConnectorForIOThread() { |
| 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 337 return io_connector_tls_ptr.Pointer()->Get(); |
| 338 } |
| 339 |
314 void MojoShellContext::ConnectToApplicationOnOwnThread( | 340 void MojoShellContext::ConnectToApplicationOnOwnThread( |
315 const std::string& user_id, | 341 const std::string& user_id, |
316 const std::string& name, | 342 const std::string& name, |
317 const std::string& requestor_name, | 343 const std::string& requestor_name, |
318 shell::mojom::InterfaceProviderRequest request, | 344 shell::mojom::InterfaceProviderRequest request, |
319 shell::mojom::InterfaceProviderPtr exposed_services, | 345 shell::mojom::InterfaceProviderPtr exposed_services, |
320 const shell::mojom::Connector::ConnectCallback& callback) { | 346 const shell::mojom::Connector::ConnectCallback& callback) { |
321 std::unique_ptr<shell::ConnectParams> params(new shell::ConnectParams); | 347 std::unique_ptr<shell::ConnectParams> params(new shell::ConnectParams); |
322 shell::Identity source_id(requestor_name, user_id); | 348 shell::Identity source_id(requestor_name, user_id); |
323 params->set_source(source_id); | 349 params->set_source(source_id); |
324 params->set_target(shell::Identity(name, user_id)); | 350 params->set_target(shell::Identity(name, user_id)); |
325 params->set_remote_interfaces(std::move(request)); | 351 params->set_remote_interfaces(std::move(request)); |
326 params->set_local_interfaces(std::move(exposed_services)); | 352 params->set_local_interfaces(std::move(exposed_services)); |
327 params->set_connect_callback(callback); | 353 params->set_connect_callback(callback); |
328 shell_->Connect(std::move(params)); | 354 shell_->Connect(std::move(params)); |
329 } | 355 } |
330 | 356 |
331 } // namespace content | 357 } // namespace content |
OLD | NEW |