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

Side by Side Diff: ipc/mojo/ipc_channel_mojo.cc

Issue 1554443003: Stop linking in the old Mojo EDK. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge and fix new flaky test Created 4 years, 11 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
« no previous file with comments | « ipc/mojo/ipc_channel_mojo.h ('k') | ipc/mojo/ipc_channel_mojo_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "ipc/mojo/ipc_channel_mojo.h" 5 #include "ipc/mojo/ipc_channel_mojo.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/bind_helpers.h" 13 #include "base/bind_helpers.h"
14 #include "base/command_line.h" 14 #include "base/command_line.h"
15 #include "base/lazy_instance.h" 15 #include "base/lazy_instance.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/thread_task_runner_handle.h" 17 #include "base/thread_task_runner_handle.h"
18 #include "build/build_config.h" 18 #include "build/build_config.h"
19 #include "ipc/ipc_listener.h" 19 #include "ipc/ipc_listener.h"
20 #include "ipc/ipc_logging.h" 20 #include "ipc/ipc_logging.h"
21 #include "ipc/ipc_message_attachment_set.h" 21 #include "ipc/ipc_message_attachment_set.h"
22 #include "ipc/ipc_message_macros.h" 22 #include "ipc/ipc_message_macros.h"
23 #include "ipc/mojo/client_channel.mojom.h"
24 #include "ipc/mojo/ipc_mojo_bootstrap.h" 23 #include "ipc/mojo/ipc_mojo_bootstrap.h"
25 #include "ipc/mojo/ipc_mojo_handle_attachment.h" 24 #include "ipc/mojo/ipc_mojo_handle_attachment.h"
25 #include "mojo/edk/embedder/embedder.h"
26 #include "mojo/public/cpp/bindings/binding.h" 26 #include "mojo/public/cpp/bindings/binding.h"
27 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h"
28 27
29 #if defined(OS_POSIX) && !defined(OS_NACL) 28 #if defined(OS_POSIX) && !defined(OS_NACL)
30 #include "ipc/ipc_platform_file_attachment_posix.h" 29 #include "ipc/ipc_platform_file_attachment_posix.h"
31 #endif 30 #endif
32 31
33 namespace IPC { 32 namespace IPC {
34 33
35 namespace { 34 namespace {
36 35
37 // TODO(jam): do more tests on using channel on same thread if it supports it (
38 // i.e. with use-new-edk and Windows). Also see message_pipe_dispatcher.cc
39 bool g_use_channel_on_io_thread_only = true;
40
41 class MojoChannelFactory : public ChannelFactory { 36 class MojoChannelFactory : public ChannelFactory {
42 public: 37 public:
43 MojoChannelFactory(scoped_refptr<base::TaskRunner> io_runner, 38 MojoChannelFactory(scoped_refptr<base::TaskRunner> io_runner,
44 ChannelHandle channel_handle, 39 ChannelHandle channel_handle,
45 Channel::Mode mode) 40 Channel::Mode mode)
46 : io_runner_(io_runner), channel_handle_(channel_handle), mode_(mode) {} 41 : io_runner_(io_runner), channel_handle_(channel_handle), mode_(mode) {}
47 42
48 std::string GetName() const override { 43 std::string GetName() const override {
49 return channel_handle_.name; 44 return channel_handle_.name;
50 } 45 }
51 46
52 scoped_ptr<Channel> BuildChannel(Listener* listener) override { 47 scoped_ptr<Channel> BuildChannel(Listener* listener) override {
53 return ChannelMojo::Create(io_runner_, channel_handle_, mode_, listener); 48 return ChannelMojo::Create(io_runner_, channel_handle_, mode_, listener);
54 } 49 }
55 50
56 private: 51 private:
57 scoped_refptr<base::TaskRunner> io_runner_; 52 scoped_refptr<base::TaskRunner> io_runner_;
58 ChannelHandle channel_handle_; 53 ChannelHandle channel_handle_;
59 Channel::Mode mode_; 54 Channel::Mode mode_;
60 }; 55 };
61 56
62 //------------------------------------------------------------------------------ 57 //------------------------------------------------------------------------------
63 58
64 class ClientChannelMojo : public ChannelMojo, public ClientChannel { 59 class ClientChannelMojo : public ChannelMojo {
65 public: 60 public:
66 ClientChannelMojo(scoped_refptr<base::TaskRunner> io_runner, 61 ClientChannelMojo(scoped_refptr<base::TaskRunner> io_runner,
67 const ChannelHandle& handle, 62 const ChannelHandle& handle,
68 Listener* listener) 63 Listener* listener)
69 : ChannelMojo(io_runner, handle, Channel::MODE_CLIENT, listener), 64 : ChannelMojo(io_runner, handle, Channel::MODE_CLIENT, listener) {
70 binding_(this),
71 weak_factory_(this) {
72 } 65 }
73 ~ClientChannelMojo() override {} 66 ~ClientChannelMojo() override {}
74 67
75 // MojoBootstrap::Delegate implementation 68 // MojoBootstrap::Delegate implementation
76 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle, 69 void OnPipeAvailable(mojo::edk::ScopedPlatformHandle handle,
77 int32_t peer_pid) override { 70 int32_t peer_pid) override {
78 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { 71 InitMessageReader(mojo::edk::CreateMessagePipe(std::move(handle)),
79 InitMessageReader( 72 peer_pid);
80 mojo::embedder::CreateChannel( 73 return;
81 std::move(handle),
82 base::Callback<void(mojo::embedder::ChannelInfo*)>(),
83 scoped_refptr<base::TaskRunner>()),
84 peer_pid);
85 return;
86 }
87 CreateMessagingPipe(
88 std::move(handle),
89 base::Bind(&ClientChannelMojo::BindPipe, weak_factory_.GetWeakPtr()));
90 }
91
92 // ClientChannel implementation
93 void Init(
94 mojo::ScopedMessagePipeHandle pipe,
95 int32_t peer_pid,
96 const mojo::Callback<void(int32_t)>& callback) override {
97 InitMessageReader(std::move(pipe), static_cast<base::ProcessId>(peer_pid));
98 callback.Run(GetSelfPID());
99 } 74 }
100 75
101 private: 76 private:
102 void BindPipe(mojo::ScopedMessagePipeHandle handle) {
103 binding_.Bind(std::move(handle));
104 }
105 void OnConnectionError() {
106 listener()->OnChannelError();
107 }
108
109 mojo::Binding<ClientChannel> binding_;
110 base::WeakPtrFactory<ClientChannelMojo> weak_factory_;
111
112 DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo); 77 DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo);
113 }; 78 };
114 79
115 //------------------------------------------------------------------------------ 80 //------------------------------------------------------------------------------
116 81
117 class ServerChannelMojo : public ChannelMojo { 82 class ServerChannelMojo : public ChannelMojo {
118 public: 83 public:
119 ServerChannelMojo(scoped_refptr<base::TaskRunner> io_runner, 84 ServerChannelMojo(scoped_refptr<base::TaskRunner> io_runner,
120 const ChannelHandle& handle, 85 const ChannelHandle& handle,
121 Listener* listener) 86 Listener* listener)
122 : ChannelMojo(io_runner, handle, Channel::MODE_SERVER, listener), 87 : ChannelMojo(io_runner, handle, Channel::MODE_SERVER, listener) {
123 weak_factory_(this) {
124 } 88 }
125 ~ServerChannelMojo() override { 89 ~ServerChannelMojo() override {
126 Close(); 90 Close();
127 } 91 }
128 92
129 // MojoBootstrap::Delegate implementation 93 // MojoBootstrap::Delegate implementation
130 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle, 94 void OnPipeAvailable(mojo::edk::ScopedPlatformHandle handle,
131 int32_t peer_pid) override { 95 int32_t peer_pid) override {
132 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { 96 message_pipe_ = mojo::edk::CreateMessagePipe(std::move(handle));
133 message_pipe_ = mojo::embedder::CreateChannel( 97 if (!message_pipe_.is_valid()) {
134 std::move(handle), 98 LOG(WARNING) << "mojo::CreateMessagePipe failed: ";
135 base::Callback<void(mojo::embedder::ChannelInfo*)>(), 99 listener()->OnChannelError();
136 scoped_refptr<base::TaskRunner>());
137 if (!message_pipe_.is_valid()) {
138 LOG(WARNING) << "mojo::CreateMessagePipe failed: ";
139 listener()->OnChannelError();
140 return;
141 }
142 InitMessageReader(std::move(message_pipe_), peer_pid);
143 return; 100 return;
144 } 101 }
145 102
146 mojo::ScopedMessagePipeHandle peer;
147 MojoResult create_result =
148 mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer);
149 if (create_result != MOJO_RESULT_OK) {
150 LOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result;
151 listener()->OnChannelError();
152 return;
153 }
154 CreateMessagingPipe(
155 std::move(handle),
156 base::Bind(&ServerChannelMojo::InitClientChannel,
157 weak_factory_.GetWeakPtr(), base::Passed(&peer)));
158 }
159 // Channel override
160 void Close() override {
161 client_channel_.reset();
162 message_pipe_.reset();
163 ChannelMojo::Close();
164 }
165
166 private:
167 void InitClientChannel(mojo::ScopedMessagePipeHandle peer_handle,
168 mojo::ScopedMessagePipeHandle handle) {
169 client_channel_.Bind(
170 mojo::InterfacePtrInfo<ClientChannel>(std::move(handle), 0u));
171 client_channel_.set_connection_error_handler(base::Bind(
172 &ServerChannelMojo::OnConnectionError, base::Unretained(this)));
173 client_channel_->Init(
174 std::move(peer_handle), static_cast<int32_t>(GetSelfPID()),
175 base::Bind(&ServerChannelMojo::ClientChannelWasInitialized,
176 base::Unretained(this)));
177 }
178
179 void OnConnectionError() {
180 listener()->OnChannelError();
181 }
182
183 // ClientChannelClient implementation
184 void ClientChannelWasInitialized(int32_t peer_pid) {
185 InitMessageReader(std::move(message_pipe_), peer_pid); 103 InitMessageReader(std::move(message_pipe_), peer_pid);
186 } 104 }
187 105
188 mojo::InterfacePtr<ClientChannel> client_channel_;
189 mojo::ScopedMessagePipeHandle message_pipe_; 106 mojo::ScopedMessagePipeHandle message_pipe_;
190 base::WeakPtrFactory<ServerChannelMojo> weak_factory_;
191 107
192 DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo); 108 DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo);
193 }; 109 };
194 110
195 #if defined(OS_POSIX) && !defined(OS_NACL) 111 #if defined(OS_POSIX) && !defined(OS_NACL)
196 112
197 base::ScopedFD TakeOrDupFile(internal::PlatformFileAttachment* attachment) { 113 base::ScopedFD TakeOrDupFile(internal::PlatformFileAttachment* attachment) {
198 return attachment->Owns() ? base::ScopedFD(attachment->TakePlatformFile()) 114 return attachment->Owns() ? base::ScopedFD(attachment->TakePlatformFile())
199 : base::ScopedFD(dup(attachment->file())); 115 : base::ScopedFD(dup(attachment->file()));
200 } 116 }
201 117
202 #endif 118 #endif
203 119
204 } // namespace 120 } // namespace
205 121
206 //------------------------------------------------------------------------------ 122 //------------------------------------------------------------------------------
207 123
208 ChannelMojo::ChannelInfoDeleter::ChannelInfoDeleter(
209 scoped_refptr<base::TaskRunner> io_runner)
210 : io_runner(io_runner) {
211 }
212
213 ChannelMojo::ChannelInfoDeleter::~ChannelInfoDeleter() {
214 }
215
216 void ChannelMojo::ChannelInfoDeleter::operator()(
217 mojo::embedder::ChannelInfo* ptr) const {
218 if (base::ThreadTaskRunnerHandle::Get() == io_runner) {
219 mojo::embedder::DestroyChannelOnIOThread(ptr);
220 } else {
221 io_runner->PostTask(
222 FROM_HERE, base::Bind(&mojo::embedder::DestroyChannelOnIOThread, ptr));
223 }
224 }
225
226 //------------------------------------------------------------------------------
227
228 // static 124 // static
229 bool ChannelMojo::ShouldBeUsed() { 125 bool ChannelMojo::ShouldBeUsed() {
230 // TODO(rockot): Investigate performance bottlenecks and hopefully reenable 126 // TODO(rockot): Investigate performance bottlenecks and hopefully reenable
231 // this at some point. http://crbug.com/500019 127 // this at some point. http://crbug.com/500019
232 return false; 128 return false;
233 } 129 }
234 130
235 // static 131 // static
236 scoped_ptr<ChannelMojo> ChannelMojo::Create( 132 scoped_ptr<ChannelMojo> ChannelMojo::Create(
237 scoped_refptr<base::TaskRunner> io_runner, 133 scoped_refptr<base::TaskRunner> io_runner,
(...skipping 29 matching lines...) Expand all
267 new MojoChannelFactory(io_runner, channel_handle, Channel::MODE_CLIENT)); 163 new MojoChannelFactory(io_runner, channel_handle, Channel::MODE_CLIENT));
268 } 164 }
269 165
270 ChannelMojo::ChannelMojo(scoped_refptr<base::TaskRunner> io_runner, 166 ChannelMojo::ChannelMojo(scoped_refptr<base::TaskRunner> io_runner,
271 const ChannelHandle& handle, 167 const ChannelHandle& handle,
272 Mode mode, 168 Mode mode,
273 Listener* listener) 169 Listener* listener)
274 : listener_(listener), 170 : listener_(listener),
275 peer_pid_(base::kNullProcessId), 171 peer_pid_(base::kNullProcessId),
276 io_runner_(io_runner), 172 io_runner_(io_runner),
277 channel_info_(nullptr, ChannelInfoDeleter(nullptr)),
278 waiting_connect_(true), 173 waiting_connect_(true),
279 weak_factory_(this) { 174 weak_factory_(this) {
280 // Create MojoBootstrap after all members are set as it touches 175 // Create MojoBootstrap after all members are set as it touches
281 // ChannelMojo from a different thread. 176 // ChannelMojo from a different thread.
282 bootstrap_ = MojoBootstrap::Create(handle, mode, this); 177 bootstrap_ = MojoBootstrap::Create(handle, mode, this);
283 if (!g_use_channel_on_io_thread_only || 178 if (io_runner == base::MessageLoop::current()->task_runner()) {
284 io_runner == base::MessageLoop::current()->task_runner()) {
285 InitOnIOThread(); 179 InitOnIOThread();
286 } else { 180 } else {
287 io_runner->PostTask(FROM_HERE, base::Bind(&ChannelMojo::InitOnIOThread, 181 io_runner->PostTask(FROM_HERE, base::Bind(&ChannelMojo::InitOnIOThread,
288 base::Unretained(this))); 182 base::Unretained(this)));
289 } 183 }
290 } 184 }
291 185
292 ChannelMojo::~ChannelMojo() { 186 ChannelMojo::~ChannelMojo() {
293 Close(); 187 Close();
294 } 188 }
295 189
296 void ChannelMojo::InitOnIOThread() { 190 void ChannelMojo::InitOnIOThread() {
297 ipc_support_.reset( 191 ipc_support_.reset(
298 new ScopedIPCSupport(base::MessageLoop::current()->task_runner())); 192 new ScopedIPCSupport(base::MessageLoop::current()->task_runner()));
299 } 193 }
300 194
301 void ChannelMojo::CreateMessagingPipe(
302 mojo::embedder::ScopedPlatformHandle handle,
303 const CreateMessagingPipeCallback& callback) {
304 auto return_callback = base::Bind(&ChannelMojo::OnMessagingPipeCreated,
305 weak_factory_.GetWeakPtr(), callback);
306 if (!g_use_channel_on_io_thread_only ||
307 base::ThreadTaskRunnerHandle::Get() == io_runner_) {
308 CreateMessagingPipeOnIOThread(std::move(handle),
309 base::ThreadTaskRunnerHandle::Get(),
310 return_callback);
311 } else {
312 io_runner_->PostTask(
313 FROM_HERE,
314 base::Bind(&ChannelMojo::CreateMessagingPipeOnIOThread,
315 base::Passed(&handle), base::ThreadTaskRunnerHandle::Get(),
316 return_callback));
317 }
318 }
319
320 // static
321 void ChannelMojo::CreateMessagingPipeOnIOThread(
322 mojo::embedder::ScopedPlatformHandle handle,
323 scoped_refptr<base::TaskRunner> callback_runner,
324 const CreateMessagingPipeOnIOThreadCallback& callback) {
325 mojo::embedder::ChannelInfo* channel_info;
326 mojo::ScopedMessagePipeHandle pipe =
327 mojo::embedder::CreateChannelOnIOThread(std::move(handle), &channel_info);
328 if (base::ThreadTaskRunnerHandle::Get() == callback_runner) {
329 callback.Run(std::move(pipe), channel_info);
330 } else {
331 callback_runner->PostTask(
332 FROM_HERE, base::Bind(callback, base::Passed(&pipe), channel_info));
333 }
334 }
335
336 void ChannelMojo::OnMessagingPipeCreated(
337 const CreateMessagingPipeCallback& callback,
338 mojo::ScopedMessagePipeHandle handle,
339 mojo::embedder::ChannelInfo* channel_info) {
340 DCHECK(!channel_info_.get());
341 channel_info_ = scoped_ptr<mojo::embedder::ChannelInfo, ChannelInfoDeleter>(
342 channel_info, ChannelInfoDeleter(io_runner_));
343 callback.Run(std::move(handle));
344 }
345
346 bool ChannelMojo::Connect() { 195 bool ChannelMojo::Connect() {
347 DCHECK(!message_reader_); 196 DCHECK(!message_reader_);
348 return bootstrap_->Connect(); 197 return bootstrap_->Connect();
349 } 198 }
350 199
351 void ChannelMojo::Close() { 200 void ChannelMojo::Close() {
352 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> to_be_deleted; 201 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> to_be_deleted;
353 202
354 { 203 {
355 // |message_reader_| has to be cleared inside the lock, 204 // |message_reader_| has to be cleared inside the lock,
356 // but the instance has to be deleted outside. 205 // but the instance has to be deleted outside.
357 base::AutoLock l(lock_); 206 base::AutoLock l(lock_);
358 to_be_deleted = std::move(message_reader_); 207 to_be_deleted = std::move(message_reader_);
359 // We might Close() before we Connect(). 208 // We might Close() before we Connect().
360 waiting_connect_ = false; 209 waiting_connect_ = false;
361 } 210 }
362 211
363 channel_info_.reset();
364 ipc_support_.reset(); 212 ipc_support_.reset();
365 to_be_deleted.reset(); 213 to_be_deleted.reset();
366 } 214 }
367 215
368 void ChannelMojo::OnBootstrapError() { 216 void ChannelMojo::OnBootstrapError() {
369 listener_->OnChannelError(); 217 listener_->OnChannelError();
370 } 218 }
371 219
372 namespace { 220 namespace {
373 221
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 base::ScopedFD file = 336 base::ScopedFD file =
489 TakeOrDupFile(static_cast<IPC::internal::PlatformFileAttachment*>( 337 TakeOrDupFile(static_cast<IPC::internal::PlatformFileAttachment*>(
490 attachment.get())); 338 attachment.get()));
491 if (!file.is_valid()) { 339 if (!file.is_valid()) {
492 DPLOG(WARNING) << "Failed to dup FD to transmit."; 340 DPLOG(WARNING) << "Failed to dup FD to transmit.";
493 set->CommitAllDescriptors(); 341 set->CommitAllDescriptors();
494 return MOJO_RESULT_UNKNOWN; 342 return MOJO_RESULT_UNKNOWN;
495 } 343 }
496 344
497 MojoHandle wrapped_handle; 345 MojoHandle wrapped_handle;
498 MojoResult wrap_result = mojo::embedder::CreatePlatformHandleWrapper( 346 MojoResult wrap_result = mojo::edk::CreatePlatformHandleWrapper(
499 mojo::embedder::ScopedPlatformHandle( 347 mojo::edk::ScopedPlatformHandle(
500 mojo::embedder::PlatformHandle(file.release())), 348 mojo::edk::PlatformHandle(file.release())),
501 &wrapped_handle); 349 &wrapped_handle);
502 if (MOJO_RESULT_OK != wrap_result) { 350 if (MOJO_RESULT_OK != wrap_result) {
503 LOG(WARNING) << "Pipe failed to wrap handles. Closing: " 351 LOG(WARNING) << "Pipe failed to wrap handles. Closing: "
504 << wrap_result; 352 << wrap_result;
505 set->CommitAllDescriptors(); 353 set->CommitAllDescriptors();
506 return wrap_result; 354 return wrap_result;
507 } 355 }
508 356
509 handles->push_back(wrapped_handle); 357 handles->push_back(wrapped_handle);
510 } 358 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
544 if (!ok) { 392 if (!ok) {
545 LOG(ERROR) << "Failed to add new Mojo handle."; 393 LOG(ERROR) << "Failed to add new Mojo handle.";
546 return MOJO_RESULT_UNKNOWN; 394 return MOJO_RESULT_UNKNOWN;
547 } 395 }
548 } 396 }
549 397
550 return MOJO_RESULT_OK; 398 return MOJO_RESULT_OK;
551 } 399 }
552 400
553 } // namespace IPC 401 } // namespace IPC
OLDNEW
« no previous file with comments | « ipc/mojo/ipc_channel_mojo.h ('k') | ipc/mojo/ipc_channel_mojo_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698