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

Side by Side Diff: content/browser/mojo/mojo_child_connection.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_child_connection.h" 5 #include "content/browser/mojo/mojo_child_connection.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/macros.h"
10 #include "content/public/common/mojo_shell_connection.h" 11 #include "content/public/common/mojo_shell_connection.h"
11 #include "mojo/edk/embedder/embedder.h" 12 #include "mojo/edk/embedder/embedder.h"
12 #include "mojo/public/cpp/system/message_pipe.h" 13 #include "mojo/public/cpp/system/message_pipe.h"
13 #include "services/shell/public/cpp/connector.h" 14 #include "services/shell/public/cpp/connector.h"
14 #include "services/shell/public/cpp/identity.h" 15 #include "services/shell/public/cpp/identity.h"
15 #include "services/shell/public/interfaces/shell_client.mojom.h" 16 #include "services/shell/public/interfaces/shell_client.mojom.h"
16 17
17 namespace content { 18 namespace content {
18 19
19 MojoChildConnection::MojoChildConnection(const std::string& application_name, 20 namespace {
20 const std::string& instance_id, 21
21 const std::string& child_token, 22 void CallBinderOnTaskRunner(
22 shell::Connector* connector) 23 const shell::InterfaceRegistry::Binder& binder,
23 : shell_client_token_(mojo::edk::GenerateRandomToken()) { 24 scoped_refptr<base::SequencedTaskRunner> task_runner,
25 const mojo::String& interface_name,
26 mojo::ScopedMessagePipeHandle request_handle) {
27 task_runner->PostTask(
28 FROM_HERE,
29 base::Bind(binder, interface_name, base::Passed(&request_handle)));
30 }
31
32 } // namespace
33
34 class MojoChildConnection::IOThreadContext
35 : public base::RefCountedThreadSafe<IOThreadContext> {
36 public:
37 IOThreadContext() {}
38
39 void Initialize(const std::string& application_name,
40 const std::string& instance_id,
41 shell::Connector* connector,
42 mojo::ScopedMessagePipeHandle shell_client_pipe,
43 scoped_refptr<base::SequencedTaskRunner> io_task_runner,
44 const shell::InterfaceRegistry::Binder& default_binder) {
45 DCHECK(!io_task_runner_);
46 io_task_runner_ = io_task_runner;
47 std::unique_ptr<shell::Connector> io_thread_connector = connector->Clone();
48 io_task_runner_->PostTask(
49 FROM_HERE,
50 base::Bind(&IOThreadContext::InitializeOnIOThread, this,
51 application_name, instance_id,
52 base::Passed(&io_thread_connector),
53 base::Passed(&shell_client_pipe),
54 base::Bind(&CallBinderOnTaskRunner, default_binder,
55 base::ThreadTaskRunnerHandle::Get())));
56 }
57
58 void ShutDown() {
59 if (!io_task_runner_)
60 return;
61 bool posted = io_task_runner_->PostTask(
62 FROM_HERE,
63 base::Bind(&IOThreadContext::ShutDownOnIOThread, this));
64 DCHECK(posted);
65 }
66
67 void GetRemoteInterfaceOnIOThread(
68 const mojo::String& interface_name,
69 mojo::ScopedMessagePipeHandle request_handle) {
70 if (connection_) {
71 connection_->GetRemoteInterfaces()->GetInterface(
72 interface_name, std::move(request_handle));
73 }
74 }
75
76 void SetProcessHandle(base::ProcessHandle handle) {
77 DCHECK(io_task_runner_);
78 io_task_runner_->PostTask(
79 FROM_HERE,
80 base::Bind(&IOThreadContext::SetProcessHandleOnIOThread, this, handle));
81 }
82
83 private:
84 friend class base::RefCountedThreadSafe<IOThreadContext>;
85
86 virtual ~IOThreadContext() {}
87
88 void InitializeOnIOThread(
89 const std::string& application_name,
90 const std::string& instance_id,
91 std::unique_ptr<shell::Connector> connector,
92 mojo::ScopedMessagePipeHandle shell_client_pipe,
93 const shell::InterfaceRegistry::Binder& default_binder) {
94 shell::mojom::ShellClientPtr client;
95 client.Bind(mojo::InterfacePtrInfo<shell::mojom::ShellClient>(
96 std::move(shell_client_pipe), 0u));
97 shell::mojom::PIDReceiverRequest pid_receiver_request =
98 mojo::GetProxy(&pid_receiver_);
99
100 shell::Identity target(application_name, shell::mojom::kInheritUserID,
101 instance_id);
102 shell::Connector::ConnectParams params(target);
103 params.set_client_process_connection(std::move(client),
104 std::move(pid_receiver_request));
105
106 // In some unit testing scenarios a null connector is passed.
107 if (!connector)
108 return;
109
110 connection_ = connector->Connect(&params);
111 connection_->GetInterfaceRegistry()->set_default_binder(default_binder);
112 }
113
114 void ShutDownOnIOThread() {
115 connection_.reset();
116 pid_receiver_.reset();
117 }
118
119 void SetProcessHandleOnIOThread(base::ProcessHandle handle) {
120 DCHECK(pid_receiver_.is_bound());
121 pid_receiver_->SetPID(base::GetProcId(handle));
122 pid_receiver_.reset();
123 }
124
125 scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
126 std::unique_ptr<shell::Connection> connection_;
127 shell::mojom::PIDReceiverPtr pid_receiver_;
128
129 DISALLOW_COPY_AND_ASSIGN(IOThreadContext);
130 };
131
132 MojoChildConnection::MojoChildConnection(
133 const std::string& application_name,
134 const std::string& instance_id,
135 const std::string& child_token,
136 shell::Connector* connector,
137 scoped_refptr<base::SequencedTaskRunner> io_task_runner)
138 : context_(new IOThreadContext),
139 shell_client_token_(mojo::edk::GenerateRandomToken()),
140 interface_registry_(nullptr),
141 weak_factory_(this) {
24 mojo::ScopedMessagePipeHandle shell_client_pipe = 142 mojo::ScopedMessagePipeHandle shell_client_pipe =
25 mojo::edk::CreateParentMessagePipe(shell_client_token_, child_token); 143 mojo::edk::CreateParentMessagePipe(shell_client_token_, child_token);
26 144
27 shell::mojom::ShellClientPtr client; 145 context_ = new IOThreadContext;
28 client.Bind(mojo::InterfacePtrInfo<shell::mojom::ShellClient>( 146 context_->Initialize(
29 std::move(shell_client_pipe), 0u)); 147 application_name, instance_id, connector, std::move(shell_client_pipe),
30 shell::mojom::PIDReceiverRequest pid_receiver_request = 148 io_task_runner,
31 GetProxy(&pid_receiver_); 149 base::Bind(&MojoChildConnection::GetInterface,
150 weak_factory_.GetWeakPtr()));
151 remote_interfaces_.Forward(
152 base::Bind(&CallBinderOnTaskRunner,
153 base::Bind(&IOThreadContext::GetRemoteInterfaceOnIOThread,
154 context_), io_task_runner));
32 155
33 shell::Identity target(application_name, shell::mojom::kInheritUserID,
34 instance_id);
35 shell::Connector::ConnectParams params(target);
36 params.set_client_process_connection(std::move(client),
37 std::move(pid_receiver_request));
38
39 // In some unit testing scenarios a null connector is passed.
40 if (!connector)
41 return;
42
43 connection_ = connector->Connect(&params);
44 #if defined(OS_ANDROID) 156 #if defined(OS_ANDROID)
45 service_registry_android_ = ServiceRegistryAndroid::Create( 157 service_registry_android_ =
46 connection_->GetInterfaceRegistry(), connection_->GetRemoteInterfaces()); 158 ServiceRegistryAndroid::Create(&interface_registry_, &remote_interfaces_);
47 #endif 159 #endif
48 } 160 }
49 161
50 MojoChildConnection::~MojoChildConnection() {} 162 MojoChildConnection::~MojoChildConnection() {
163 context_->ShutDown();
164 }
51 165
52 void MojoChildConnection::SetProcessHandle(base::ProcessHandle handle) { 166 void MojoChildConnection::SetProcessHandle(base::ProcessHandle handle) {
53 DCHECK(pid_receiver_.is_bound()); 167 context_->SetProcessHandle(handle);
54 pid_receiver_->SetPID(base::GetProcId(handle)); 168 }
55 pid_receiver_.reset(); 169
170 void MojoChildConnection::GetInterface(
171 const mojo::String& interface_name,
172 mojo::ScopedMessagePipeHandle request_handle) {
173 static_cast<shell::mojom::InterfaceProvider*>(&interface_registry_)
174 ->GetInterface(interface_name, std::move(request_handle));
56 } 175 }
57 176
58 } // namespace content 177 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698