OLD | NEW |
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" | 23 #include "ipc/mojo/client_channel.mojom.h" |
24 #include "ipc/mojo/ipc_mojo_bootstrap.h" | 24 #include "ipc/mojo/ipc_mojo_bootstrap.h" |
25 #include "ipc/mojo/ipc_mojo_handle_attachment.h" | 25 #include "ipc/mojo/ipc_mojo_handle_attachment.h" |
| 26 #include "mojo/edk/embedder/embedder.h" |
26 #include "mojo/public/cpp/bindings/binding.h" | 27 #include "mojo/public/cpp/bindings/binding.h" |
27 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | |
28 | 28 |
29 #if defined(OS_POSIX) && !defined(OS_NACL) | 29 #if defined(OS_POSIX) && !defined(OS_NACL) |
30 #include "ipc/ipc_platform_file_attachment_posix.h" | 30 #include "ipc/ipc_platform_file_attachment_posix.h" |
31 #endif | 31 #endif |
32 | 32 |
33 namespace IPC { | 33 namespace IPC { |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 // TODO(jam): do more tests on using channel on same thread if it supports it ( | 37 // TODO(jam): do more tests on using channel on same thread if it supports it ( |
(...skipping 28 matching lines...) Expand all Loading... |
66 ClientChannelMojo(scoped_refptr<base::TaskRunner> io_runner, | 66 ClientChannelMojo(scoped_refptr<base::TaskRunner> io_runner, |
67 const ChannelHandle& handle, | 67 const ChannelHandle& handle, |
68 Listener* listener) | 68 Listener* listener) |
69 : ChannelMojo(io_runner, handle, Channel::MODE_CLIENT, listener), | 69 : ChannelMojo(io_runner, handle, Channel::MODE_CLIENT, listener), |
70 binding_(this), | 70 binding_(this), |
71 weak_factory_(this) { | 71 weak_factory_(this) { |
72 } | 72 } |
73 ~ClientChannelMojo() override {} | 73 ~ClientChannelMojo() override {} |
74 | 74 |
75 // MojoBootstrap::Delegate implementation | 75 // MojoBootstrap::Delegate implementation |
76 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle, | 76 void OnPipeAvailable(mojo::edk::ScopedPlatformHandle handle, |
77 int32_t peer_pid) override { | 77 int32_t peer_pid) override { |
78 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { | 78 BindPipe(mojo::edk::CreateMessagePipe(std::move(handle))); |
79 InitMessageReader( | |
80 mojo::embedder::CreateChannel( | |
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 } | 79 } |
91 | 80 |
92 // ClientChannel implementation | 81 // ClientChannel implementation |
93 void Init( | 82 void Init( |
94 mojo::ScopedMessagePipeHandle pipe, | 83 mojo::ScopedMessagePipeHandle pipe, |
95 int32_t peer_pid, | 84 int32_t peer_pid, |
96 const mojo::Callback<void(int32_t)>& callback) override { | 85 const mojo::Callback<void(int32_t)>& callback) override { |
97 InitMessageReader(std::move(pipe), static_cast<base::ProcessId>(peer_pid)); | 86 InitMessageReader(std::move(pipe), static_cast<base::ProcessId>(peer_pid)); |
98 callback.Run(GetSelfPID()); | 87 callback.Run(GetSelfPID()); |
99 } | 88 } |
(...skipping 20 matching lines...) Expand all Loading... |
120 const ChannelHandle& handle, | 109 const ChannelHandle& handle, |
121 Listener* listener) | 110 Listener* listener) |
122 : ChannelMojo(io_runner, handle, Channel::MODE_SERVER, listener), | 111 : ChannelMojo(io_runner, handle, Channel::MODE_SERVER, listener), |
123 weak_factory_(this) { | 112 weak_factory_(this) { |
124 } | 113 } |
125 ~ServerChannelMojo() override { | 114 ~ServerChannelMojo() override { |
126 Close(); | 115 Close(); |
127 } | 116 } |
128 | 117 |
129 // MojoBootstrap::Delegate implementation | 118 // MojoBootstrap::Delegate implementation |
130 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle, | 119 void OnPipeAvailable(mojo::edk::ScopedPlatformHandle handle, |
131 int32_t peer_pid) override { | 120 int32_t peer_pid) override { |
132 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { | |
133 message_pipe_ = mojo::embedder::CreateChannel( | |
134 std::move(handle), | |
135 base::Callback<void(mojo::embedder::ChannelInfo*)>(), | |
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; | |
144 } | |
145 | |
146 mojo::ScopedMessagePipeHandle peer; | 121 mojo::ScopedMessagePipeHandle peer; |
147 MojoResult create_result = | 122 MojoResult create_result = |
148 mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer); | 123 mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer); |
149 if (create_result != MOJO_RESULT_OK) { | 124 if (create_result != MOJO_RESULT_OK) { |
150 LOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result; | 125 LOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result; |
151 listener()->OnChannelError(); | 126 listener()->OnChannelError(); |
152 return; | 127 return; |
153 } | 128 } |
154 CreateMessagingPipe( | 129 InitClientChannel(std::move(peer), |
155 std::move(handle), | 130 mojo::edk::CreateMessagePipe(std::move(handle))); |
156 base::Bind(&ServerChannelMojo::InitClientChannel, | |
157 weak_factory_.GetWeakPtr(), base::Passed(&peer))); | |
158 } | 131 } |
159 // Channel override | 132 // Channel override |
160 void Close() override { | 133 void Close() override { |
161 client_channel_.reset(); | 134 client_channel_.reset(); |
162 message_pipe_.reset(); | 135 message_pipe_.reset(); |
163 ChannelMojo::Close(); | 136 ChannelMojo::Close(); |
164 } | 137 } |
165 | 138 |
166 private: | 139 private: |
167 void InitClientChannel(mojo::ScopedMessagePipeHandle peer_handle, | 140 void InitClientChannel(mojo::ScopedMessagePipeHandle peer_handle, |
(...skipping 30 matching lines...) Expand all Loading... |
198 return attachment->Owns() ? base::ScopedFD(attachment->TakePlatformFile()) | 171 return attachment->Owns() ? base::ScopedFD(attachment->TakePlatformFile()) |
199 : base::ScopedFD(dup(attachment->file())); | 172 : base::ScopedFD(dup(attachment->file())); |
200 } | 173 } |
201 | 174 |
202 #endif | 175 #endif |
203 | 176 |
204 } // namespace | 177 } // namespace |
205 | 178 |
206 //------------------------------------------------------------------------------ | 179 //------------------------------------------------------------------------------ |
207 | 180 |
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 | 181 // static |
229 bool ChannelMojo::ShouldBeUsed() { | 182 bool ChannelMojo::ShouldBeUsed() { |
230 // TODO(rockot): Investigate performance bottlenecks and hopefully reenable | 183 // TODO(rockot): Investigate performance bottlenecks and hopefully reenable |
231 // this at some point. http://crbug.com/500019 | 184 // this at some point. http://crbug.com/500019 |
232 return false; | 185 return false; |
233 } | 186 } |
234 | 187 |
235 // static | 188 // static |
236 scoped_ptr<ChannelMojo> ChannelMojo::Create( | 189 scoped_ptr<ChannelMojo> ChannelMojo::Create( |
237 scoped_refptr<base::TaskRunner> io_runner, | 190 scoped_refptr<base::TaskRunner> io_runner, |
(...skipping 29 matching lines...) Expand all Loading... |
267 new MojoChannelFactory(io_runner, channel_handle, Channel::MODE_CLIENT)); | 220 new MojoChannelFactory(io_runner, channel_handle, Channel::MODE_CLIENT)); |
268 } | 221 } |
269 | 222 |
270 ChannelMojo::ChannelMojo(scoped_refptr<base::TaskRunner> io_runner, | 223 ChannelMojo::ChannelMojo(scoped_refptr<base::TaskRunner> io_runner, |
271 const ChannelHandle& handle, | 224 const ChannelHandle& handle, |
272 Mode mode, | 225 Mode mode, |
273 Listener* listener) | 226 Listener* listener) |
274 : listener_(listener), | 227 : listener_(listener), |
275 peer_pid_(base::kNullProcessId), | 228 peer_pid_(base::kNullProcessId), |
276 io_runner_(io_runner), | 229 io_runner_(io_runner), |
277 channel_info_(nullptr, ChannelInfoDeleter(nullptr)), | |
278 waiting_connect_(true), | 230 waiting_connect_(true), |
279 weak_factory_(this) { | 231 weak_factory_(this) { |
280 // Create MojoBootstrap after all members are set as it touches | 232 // Create MojoBootstrap after all members are set as it touches |
281 // ChannelMojo from a different thread. | 233 // ChannelMojo from a different thread. |
282 bootstrap_ = MojoBootstrap::Create(handle, mode, this); | 234 bootstrap_ = MojoBootstrap::Create(handle, mode, this); |
283 if (!g_use_channel_on_io_thread_only || | 235 if (!g_use_channel_on_io_thread_only || |
284 io_runner == base::MessageLoop::current()->task_runner()) { | 236 io_runner == base::MessageLoop::current()->task_runner()) { |
285 InitOnIOThread(); | 237 InitOnIOThread(); |
286 } else { | 238 } else { |
287 io_runner->PostTask(FROM_HERE, base::Bind(&ChannelMojo::InitOnIOThread, | 239 io_runner->PostTask(FROM_HERE, base::Bind(&ChannelMojo::InitOnIOThread, |
288 base::Unretained(this))); | 240 base::Unretained(this))); |
289 } | 241 } |
290 } | 242 } |
291 | 243 |
292 ChannelMojo::~ChannelMojo() { | 244 ChannelMojo::~ChannelMojo() { |
293 Close(); | 245 Close(); |
294 } | 246 } |
295 | 247 |
296 void ChannelMojo::InitOnIOThread() { | 248 void ChannelMojo::InitOnIOThread() { |
297 ipc_support_.reset( | 249 ipc_support_.reset( |
298 new ScopedIPCSupport(base::MessageLoop::current()->task_runner())); | 250 new ScopedIPCSupport(base::MessageLoop::current()->task_runner())); |
299 } | 251 } |
300 | 252 |
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() { | 253 bool ChannelMojo::Connect() { |
347 DCHECK(!message_reader_); | 254 DCHECK(!message_reader_); |
348 return bootstrap_->Connect(); | 255 return bootstrap_->Connect(); |
349 } | 256 } |
350 | 257 |
351 void ChannelMojo::Close() { | 258 void ChannelMojo::Close() { |
352 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> to_be_deleted; | 259 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> to_be_deleted; |
353 | 260 |
354 { | 261 { |
355 // |message_reader_| has to be cleared inside the lock, | 262 // |message_reader_| has to be cleared inside the lock, |
356 // but the instance has to be deleted outside. | 263 // but the instance has to be deleted outside. |
357 base::AutoLock l(lock_); | 264 base::AutoLock l(lock_); |
358 to_be_deleted = std::move(message_reader_); | 265 to_be_deleted = std::move(message_reader_); |
359 // We might Close() before we Connect(). | 266 // We might Close() before we Connect(). |
360 waiting_connect_ = false; | 267 waiting_connect_ = false; |
361 } | 268 } |
362 | 269 |
363 channel_info_.reset(); | |
364 ipc_support_.reset(); | 270 ipc_support_.reset(); |
365 to_be_deleted.reset(); | 271 to_be_deleted.reset(); |
366 } | 272 } |
367 | 273 |
368 void ChannelMojo::OnBootstrapError() { | 274 void ChannelMojo::OnBootstrapError() { |
369 listener_->OnChannelError(); | 275 listener_->OnChannelError(); |
370 } | 276 } |
371 | 277 |
372 namespace { | 278 namespace { |
373 | 279 |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 base::ScopedFD file = | 394 base::ScopedFD file = |
489 TakeOrDupFile(static_cast<IPC::internal::PlatformFileAttachment*>( | 395 TakeOrDupFile(static_cast<IPC::internal::PlatformFileAttachment*>( |
490 attachment.get())); | 396 attachment.get())); |
491 if (!file.is_valid()) { | 397 if (!file.is_valid()) { |
492 DPLOG(WARNING) << "Failed to dup FD to transmit."; | 398 DPLOG(WARNING) << "Failed to dup FD to transmit."; |
493 set->CommitAllDescriptors(); | 399 set->CommitAllDescriptors(); |
494 return MOJO_RESULT_UNKNOWN; | 400 return MOJO_RESULT_UNKNOWN; |
495 } | 401 } |
496 | 402 |
497 MojoHandle wrapped_handle; | 403 MojoHandle wrapped_handle; |
498 MojoResult wrap_result = mojo::embedder::CreatePlatformHandleWrapper( | 404 MojoResult wrap_result = mojo::edk::CreatePlatformHandleWrapper( |
499 mojo::embedder::ScopedPlatformHandle( | 405 mojo::edk::ScopedPlatformHandle( |
500 mojo::embedder::PlatformHandle(file.release())), | 406 mojo::edk::PlatformHandle(file.release())), |
501 &wrapped_handle); | 407 &wrapped_handle); |
502 if (MOJO_RESULT_OK != wrap_result) { | 408 if (MOJO_RESULT_OK != wrap_result) { |
503 LOG(WARNING) << "Pipe failed to wrap handles. Closing: " | 409 LOG(WARNING) << "Pipe failed to wrap handles. Closing: " |
504 << wrap_result; | 410 << wrap_result; |
505 set->CommitAllDescriptors(); | 411 set->CommitAllDescriptors(); |
506 return wrap_result; | 412 return wrap_result; |
507 } | 413 } |
508 | 414 |
509 handles->push_back(wrapped_handle); | 415 handles->push_back(wrapped_handle); |
510 } | 416 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 if (!ok) { | 450 if (!ok) { |
545 LOG(ERROR) << "Failed to add new Mojo handle."; | 451 LOG(ERROR) << "Failed to add new Mojo handle."; |
546 return MOJO_RESULT_UNKNOWN; | 452 return MOJO_RESULT_UNKNOWN; |
547 } | 453 } |
548 } | 454 } |
549 | 455 |
550 return MOJO_RESULT_OK; | 456 return MOJO_RESULT_OK; |
551 } | 457 } |
552 | 458 |
553 } // namespace IPC | 459 } // namespace IPC |
OLD | NEW |