Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: content/browser/mojo/mojo_shell_context.cc

Issue 2111353002: Move content's shell connections to the IO thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
42 #include "services/shell/public/interfaces/shell_client.mojom.h" 42 #include "services/shell/public/interfaces/shell_client.mojom.h"
43 #include "services/shell/public/interfaces/shell_client_factory.mojom.h" 43 #include "services/shell/public/interfaces/shell_client_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
264 254
265 shell::mojom::ShellClientRequest request; 255 shell::mojom::ShellClientRequest request;
266 if (shell::ShellIsRemote()) { 256 if (shell::ShellIsRemote()) {
267 mojo::edk::SetParentPipeHandleFromCommandLine(); 257 mojo::edk::SetParentPipeHandleFromCommandLine();
268 request = shell::GetShellClientRequestFromCommandLine(); 258 request = shell::GetShellClientRequestFromCommandLine();
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_->TakeShellClient())); 261 catalog_->TakeShellClient()));
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),
266 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
276 267
277 std::unique_ptr<shell::Connector> io_connector = 268 // This is safe to assign directly from any thread, because MojoShellContext
269 // must be constructed before anyone can call GetConnectorForIOThread().
270 g_io_thread_connector.Get() =
278 MojoShellConnection::GetForProcess()->GetConnector()->Clone(); 271 MojoShellConnection::GetForProcess()->GetConnector()->Clone();
279 BrowserThread::PostTask(
280 BrowserThread::IO, FROM_HERE,
281 base::Bind(&SetConnectorOnIOThread, base::Passed(&io_connector)));
282 272
283 ContentBrowserClient::StaticMojoApplicationMap apps; 273 ContentBrowserClient::StaticMojoApplicationMap apps;
284 GetContentClient()->browser()->RegisterInProcessMojoApplications(&apps); 274 GetContentClient()->browser()->RegisterInProcessMojoApplications(&apps);
285 for (const auto& entry : apps) { 275 for (const auto& entry : apps) {
286 MojoShellConnection::GetForProcess()->AddEmbeddedService(entry.first, 276 MojoShellConnection::GetForProcess()->AddEmbeddedService(entry.first,
287 entry.second); 277 entry.second);
288 } 278 }
289 279
290 ContentBrowserClient::OutOfProcessMojoApplicationMap sandboxed_apps; 280 ContentBrowserClient::OutOfProcessMojoApplicationMap sandboxed_apps;
291 GetContentClient() 281 GetContentClient()
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 shell::mojom::InterfaceProviderPtr exposed_services, 322 shell::mojom::InterfaceProviderPtr exposed_services,
333 const shell::mojom::Connector::ConnectCallback& callback) { 323 const shell::mojom::Connector::ConnectCallback& callback) {
334 proxy_.Get()->ConnectToApplication(user_id, name, requestor_name, 324 proxy_.Get()->ConnectToApplication(user_id, name, requestor_name,
335 std::move(request), 325 std::move(request),
336 std::move(exposed_services), callback); 326 std::move(exposed_services), callback);
337 } 327 }
338 328
339 // static 329 // static
340 shell::Connector* MojoShellContext::GetConnectorForIOThread() { 330 shell::Connector* MojoShellContext::GetConnectorForIOThread() {
341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
342 return io_connector_tls_ptr.Pointer()->Get(); 332 DCHECK(g_io_thread_connector.Get());
333 return g_io_thread_connector.Get().get();
343 } 334 }
344 335
345 void MojoShellContext::ConnectToApplicationOnOwnThread( 336 void MojoShellContext::ConnectToApplicationOnOwnThread(
346 const std::string& user_id, 337 const std::string& user_id,
347 const std::string& name, 338 const std::string& name,
348 const std::string& requestor_name, 339 const std::string& requestor_name,
349 shell::mojom::InterfaceProviderRequest request, 340 shell::mojom::InterfaceProviderRequest request,
350 shell::mojom::InterfaceProviderPtr exposed_services, 341 shell::mojom::InterfaceProviderPtr exposed_services,
351 const shell::mojom::Connector::ConnectCallback& callback) { 342 const shell::mojom::Connector::ConnectCallback& callback) {
352 std::unique_ptr<shell::ConnectParams> params(new shell::ConnectParams); 343 std::unique_ptr<shell::ConnectParams> params(new shell::ConnectParams);
353 shell::Identity source_id(requestor_name, user_id); 344 shell::Identity source_id(requestor_name, user_id);
354 params->set_source(source_id); 345 params->set_source(source_id);
355 params->set_target(shell::Identity(name, user_id)); 346 params->set_target(shell::Identity(name, user_id));
356 params->set_remote_interfaces(std::move(request)); 347 params->set_remote_interfaces(std::move(request));
357 params->set_local_interfaces(std::move(exposed_services)); 348 params->set_local_interfaces(std::move(exposed_services));
358 params->set_connect_callback(callback); 349 params->set_connect_callback(callback);
359 shell_->Connect(std::move(params)); 350 shell_->Connect(std::move(params));
360 } 351 }
361 352
362 } // namespace content 353 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698