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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 #include "services/shell/public/interfaces/service.mojom.h" | 42 #include "services/shell/public/interfaces/service.mojom.h" |
43 #include "services/shell/public/interfaces/service_factory.mojom.h" | 43 #include "services/shell/public/interfaces/service_factory.mojom.h" |
44 #include "services/shell/runner/common/client_util.h" | 44 #include "services/shell/runner/common/client_util.h" |
45 #include "services/shell/runner/host/in_process_native_runner.h" | 45 #include "services/shell/runner/host/in_process_native_runner.h" |
46 #include "services/user/public/cpp/constants.h" | 46 #include "services/user/public/cpp/constants.h" |
47 | 47 |
48 namespace content { | 48 namespace content { |
49 | 49 |
50 namespace { | 50 namespace { |
51 | 51 |
52 base::LazyInstance<std::unique_ptr<shell::Connector>>::Leaky | 52 using ConnectorPtr = base::ThreadLocalPointer<shell::Connector>; |
53 g_io_thread_connector = LAZY_INSTANCE_INITIALIZER; | |
54 | 53 |
55 void DestroyConnectorOnIOThread() { g_io_thread_connector.Get().reset(); } | 54 base::LazyInstance<ConnectorPtr>::Leaky io_connector_tls_ptr = |
| 55 LAZY_INSTANCE_INITIALIZER; |
| 56 |
| 57 void SetConnectorOnIOThread(std::unique_ptr<shell::Connector> connector) { |
| 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 59 io_connector_tls_ptr.Pointer()->Set(connector.release()); |
| 60 } |
| 61 |
| 62 void DestroyConnectorOnIOThread() { |
| 63 delete MojoShellContext::GetConnectorForIOThread(); |
| 64 io_connector_tls_ptr.Pointer()->Set(nullptr); |
| 65 } |
56 | 66 |
57 void StartUtilityProcessOnIOThread( | 67 void StartUtilityProcessOnIOThread( |
58 mojo::InterfaceRequest<mojom::ProcessControl> request, | 68 mojo::InterfaceRequest<mojom::ProcessControl> request, |
59 const base::string16& process_name, | 69 const base::string16& process_name, |
60 bool use_sandbox) { | 70 bool use_sandbox) { |
61 UtilityProcessHost* process_host = | 71 UtilityProcessHost* process_host = |
62 UtilityProcessHost::Create(nullptr, nullptr); | 72 UtilityProcessHost::Create(nullptr, nullptr); |
63 process_host->SetName(process_name); | 73 process_host->SetName(process_name); |
64 if (!use_sandbox) | 74 if (!use_sandbox) |
65 process_host->DisableSandbox(); | 75 process_host->DisableSandbox(); |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 if (shell::ShellIsRemote()) { | 266 if (shell::ShellIsRemote()) { |
257 mojo::edk::SetParentPipeHandleFromCommandLine(); | 267 mojo::edk::SetParentPipeHandleFromCommandLine(); |
258 request = shell::GetServiceRequestFromCommandLine(); | 268 request = shell::GetServiceRequestFromCommandLine(); |
259 } else { | 269 } else { |
260 service_manager_.reset( | 270 service_manager_.reset( |
261 new shell::ServiceManager(std::move(native_runner_factory), | 271 new shell::ServiceManager(std::move(native_runner_factory), |
262 catalog_->TakeService())); | 272 catalog_->TakeService())); |
263 request = service_manager_->StartEmbedderService( | 273 request = service_manager_->StartEmbedderService( |
264 kBrowserMojoApplicationName); | 274 kBrowserMojoApplicationName); |
265 } | 275 } |
266 MojoShellConnection::SetForProcess(MojoShellConnection::Create( | 276 MojoShellConnection::SetForProcess( |
267 std::move(request), | 277 MojoShellConnection::Create(std::move(request))); |
268 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); | 278 |
| 279 std::unique_ptr<shell::Connector> io_connector = |
| 280 MojoShellConnection::GetForProcess()->GetConnector()->Clone(); |
| 281 BrowserThread::PostTask( |
| 282 BrowserThread::IO, FROM_HERE, |
| 283 base::Bind(&SetConnectorOnIOThread, base::Passed(&io_connector))); |
269 | 284 |
270 ContentBrowserClient::StaticMojoApplicationMap apps; | 285 ContentBrowserClient::StaticMojoApplicationMap apps; |
271 GetContentClient()->browser()->RegisterInProcessMojoApplications(&apps); | 286 GetContentClient()->browser()->RegisterInProcessMojoApplications(&apps); |
272 for (const auto& entry : apps) { | 287 for (const auto& entry : apps) { |
273 MojoShellConnection::GetForProcess()->AddEmbeddedService(entry.first, | 288 MojoShellConnection::GetForProcess()->AddEmbeddedService(entry.first, |
274 entry.second); | 289 entry.second); |
275 } | 290 } |
276 | 291 |
277 // This is safe to assign directly from any thread, because MojoShellContext | |
278 // must be constructed before anyone can call GetConnectorForIOThread(). | |
279 g_io_thread_connector.Get() = | |
280 MojoShellConnection::GetForProcess()->GetConnector()->Clone(); | |
281 | |
282 MojoShellConnection::GetForProcess()->Start(); | |
283 | |
284 ContentBrowserClient::OutOfProcessMojoApplicationMap sandboxed_apps; | 292 ContentBrowserClient::OutOfProcessMojoApplicationMap sandboxed_apps; |
285 GetContentClient() | 293 GetContentClient() |
286 ->browser() | 294 ->browser() |
287 ->RegisterOutOfProcessMojoApplications(&sandboxed_apps); | 295 ->RegisterOutOfProcessMojoApplications(&sandboxed_apps); |
288 for (const auto& app : sandboxed_apps) { | 296 for (const auto& app : sandboxed_apps) { |
289 MojoShellConnection::GetForProcess()->AddServiceRequestHandler( | 297 MojoShellConnection::GetForProcess()->AddServiceRequestHandler( |
290 app.first, | 298 app.first, |
291 base::Bind(&LaunchAppInUtilityProcess, app.first, app.second, | 299 base::Bind(&LaunchAppInUtilityProcess, app.first, app.second, |
292 true /* use_sandbox */)); | 300 true /* use_sandbox */)); |
293 } | 301 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 shell::mojom::InterfaceProviderPtr exposed_services, | 334 shell::mojom::InterfaceProviderPtr exposed_services, |
327 const shell::mojom::Connector::ConnectCallback& callback) { | 335 const shell::mojom::Connector::ConnectCallback& callback) { |
328 proxy_.Get()->ConnectToApplication(user_id, name, requestor_name, | 336 proxy_.Get()->ConnectToApplication(user_id, name, requestor_name, |
329 std::move(request), | 337 std::move(request), |
330 std::move(exposed_services), callback); | 338 std::move(exposed_services), callback); |
331 } | 339 } |
332 | 340 |
333 // static | 341 // static |
334 shell::Connector* MojoShellContext::GetConnectorForIOThread() { | 342 shell::Connector* MojoShellContext::GetConnectorForIOThread() { |
335 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
336 return g_io_thread_connector.Get().get(); | 344 return io_connector_tls_ptr.Pointer()->Get(); |
337 } | 345 } |
338 | 346 |
339 void MojoShellContext::ConnectToApplicationOnOwnThread( | 347 void MojoShellContext::ConnectToApplicationOnOwnThread( |
340 const std::string& user_id, | 348 const std::string& user_id, |
341 const std::string& name, | 349 const std::string& name, |
342 const std::string& requestor_name, | 350 const std::string& requestor_name, |
343 shell::mojom::InterfaceProviderRequest request, | 351 shell::mojom::InterfaceProviderRequest request, |
344 shell::mojom::InterfaceProviderPtr exposed_services, | 352 shell::mojom::InterfaceProviderPtr exposed_services, |
345 const shell::mojom::Connector::ConnectCallback& callback) { | 353 const shell::mojom::Connector::ConnectCallback& callback) { |
346 std::unique_ptr<shell::ConnectParams> params(new shell::ConnectParams); | 354 std::unique_ptr<shell::ConnectParams> params(new shell::ConnectParams); |
347 shell::Identity source_id(requestor_name, user_id); | 355 shell::Identity source_id(requestor_name, user_id); |
348 params->set_source(source_id); | 356 params->set_source(source_id); |
349 params->set_target(shell::Identity(name, user_id)); | 357 params->set_target(shell::Identity(name, user_id)); |
350 params->set_remote_interfaces(std::move(request)); | 358 params->set_remote_interfaces(std::move(request)); |
351 params->set_local_interfaces(std::move(exposed_services)); | 359 params->set_local_interfaces(std::move(exposed_services)); |
352 params->set_connect_callback(callback); | 360 params->set_connect_callback(callback); |
353 service_manager_->Connect(std::move(params)); | 361 service_manager_->Connect(std::move(params)); |
354 } | 362 } |
355 | 363 |
356 } // namespace content | 364 } // namespace content |
OLD | NEW |