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