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/ipc_channel_mojo.h" | 5 #include "ipc/ipc_channel_mojo.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { | 267 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
268 return base::MakeUnique<MojoChannelFactory>( | 268 return base::MakeUnique<MojoChannelFactory>( |
269 std::move(handle), Channel::MODE_CLIENT, ipc_task_runner); | 269 std::move(handle), Channel::MODE_CLIENT, ipc_task_runner); |
270 } | 270 } |
271 | 271 |
272 ChannelMojo::ChannelMojo( | 272 ChannelMojo::ChannelMojo( |
273 mojo::ScopedMessagePipeHandle handle, | 273 mojo::ScopedMessagePipeHandle handle, |
274 Mode mode, | 274 Mode mode, |
275 Listener* listener, | 275 Listener* listener, |
276 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) | 276 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) |
277 : pipe_(handle.get()), listener_(listener), weak_factory_(this) { | 277 : task_runner_(ipc_task_runner), |
278 // Create MojoBootstrap after all members are set as it touches | 278 pipe_(handle.get()), |
279 // ChannelMojo from a different thread. | 279 listener_(listener), |
280 bootstrap_ = | 280 weak_factory_(this) { |
281 MojoBootstrap::Create(std::move(handle), mode, this, ipc_task_runner); | 281 bootstrap_ = MojoBootstrap::Create(std::move(handle), mode, ipc_task_runner); |
| 282 } |
| 283 |
| 284 void ChannelMojo::ForwardMessageFromThreadSafePtr(mojo::Message message) { |
| 285 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 286 if (!message_reader_) |
| 287 return; |
| 288 message_reader_->sender().internal_state()->ForwardMessage( |
| 289 std::move(message)); |
| 290 } |
| 291 |
| 292 void ChannelMojo::ForwardMessageWithResponderFromThreadSafePtr( |
| 293 mojo::Message message, |
| 294 std::unique_ptr<mojo::MessageReceiver> responder) { |
| 295 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 296 if (!message_reader_) |
| 297 return; |
| 298 message_reader_->sender().internal_state()->ForwardMessageWithResponder( |
| 299 std::move(message), std::move(responder)); |
282 } | 300 } |
283 | 301 |
284 ChannelMojo::~ChannelMojo() { | 302 ChannelMojo::~ChannelMojo() { |
| 303 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
285 Close(); | 304 Close(); |
286 } | 305 } |
287 | 306 |
288 bool ChannelMojo::Connect() { | 307 bool ChannelMojo::Connect() { |
| 308 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 309 |
289 WillConnect(); | 310 WillConnect(); |
290 | 311 |
291 DCHECK(!task_runner_); | 312 mojom::ChannelAssociatedPtr sender; |
292 task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 313 mojom::ChannelAssociatedRequest receiver; |
| 314 bootstrap_->Connect(&sender, &receiver); |
| 315 |
293 DCHECK(!message_reader_); | 316 DCHECK(!message_reader_); |
294 | 317 sender->SetPeerPid(GetSelfPID()); |
295 bootstrap_->Connect(); | 318 message_reader_.reset(new internal::MessagePipeReader( |
| 319 pipe_, std::move(sender), std::move(receiver), this)); |
296 return true; | 320 return true; |
297 } | 321 } |
298 | 322 |
299 void ChannelMojo::Pause() { | 323 void ChannelMojo::Pause() { |
300 bootstrap_->Pause(); | 324 bootstrap_->Pause(); |
301 } | 325 } |
302 | 326 |
303 void ChannelMojo::Unpause(bool flush) { | 327 void ChannelMojo::Unpause(bool flush) { |
304 bootstrap_->Unpause(); | 328 bootstrap_->Unpause(); |
305 if (flush) | 329 if (flush) |
306 Flush(); | 330 Flush(); |
307 } | 331 } |
308 | 332 |
309 void ChannelMojo::Flush() { | 333 void ChannelMojo::Flush() { |
310 bootstrap_->Flush(); | 334 bootstrap_->Flush(); |
311 } | 335 } |
312 | 336 |
313 void ChannelMojo::Close() { | 337 void ChannelMojo::Close() { |
314 // NOTE: The MessagePipeReader's destructor may re-enter this function. Use | 338 // NOTE: The MessagePipeReader's destructor may re-enter this function. Use |
315 // caution when changing this method. | 339 // caution when changing this method. |
316 std::unique_ptr<internal::MessagePipeReader> reader = | 340 std::unique_ptr<internal::MessagePipeReader> reader = |
317 std::move(message_reader_); | 341 std::move(message_reader_); |
318 reader.reset(); | 342 reader.reset(); |
319 | 343 |
320 base::AutoLock lock(associated_interface_lock_); | 344 base::AutoLock lock(associated_interface_lock_); |
321 associated_interfaces_.clear(); | 345 associated_interfaces_.clear(); |
322 } | 346 } |
323 | 347 |
324 // MojoBootstrap::Delegate implementation | |
325 void ChannelMojo::OnPipesAvailable(mojom::ChannelAssociatedPtr sender, | |
326 mojom::ChannelAssociatedRequest receiver) { | |
327 sender->SetPeerPid(GetSelfPID()); | |
328 message_reader_.reset(new internal::MessagePipeReader( | |
329 pipe_, std::move(sender), std::move(receiver), this)); | |
330 } | |
331 | |
332 void ChannelMojo::OnPipeError() { | 348 void ChannelMojo::OnPipeError() { |
333 DCHECK(task_runner_); | 349 DCHECK(task_runner_); |
334 if (task_runner_->RunsTasksOnCurrentThread()) { | 350 if (task_runner_->RunsTasksOnCurrentThread()) { |
335 listener_->OnChannelError(); | 351 listener_->OnChannelError(); |
336 } else { | 352 } else { |
337 task_runner_->PostTask( | 353 task_runner_->PostTask( |
338 FROM_HERE, | 354 FROM_HERE, |
339 base::Bind(&ChannelMojo::OnPipeError, weak_factory_.GetWeakPtr())); | 355 base::Bind(&ChannelMojo::OnPipeError, weak_factory_.GetWeakPtr())); |
340 } | 356 } |
341 } | 357 } |
(...skipping 28 matching lines...) Expand all Loading... |
370 // called. | 386 // called. |
371 // | 387 // |
372 // With Mojo, there's no OnFileCanReadWithoutBlocking, but we expect the | 388 // With Mojo, there's no OnFileCanReadWithoutBlocking, but we expect the |
373 // pipe's connection error handler will be invoked in its place. | 389 // pipe's connection error handler will be invoked in its place. |
374 return message_reader_->Send(std::move(scoped_message)); | 390 return message_reader_->Send(std::move(scoped_message)); |
375 } | 391 } |
376 | 392 |
377 Channel::AssociatedInterfaceSupport* | 393 Channel::AssociatedInterfaceSupport* |
378 ChannelMojo::GetAssociatedInterfaceSupport() { return this; } | 394 ChannelMojo::GetAssociatedInterfaceSupport() { return this; } |
379 | 395 |
| 396 std::unique_ptr<mojo::ThreadSafeForwarder<mojom::Channel>> |
| 397 ChannelMojo::CreateThreadSafeChannel() { |
| 398 return base::MakeUnique<mojo::ThreadSafeForwarder<mojom::Channel>>( |
| 399 task_runner_, base::Bind(&ChannelMojo::ForwardMessageFromThreadSafePtr, |
| 400 weak_factory_.GetWeakPtr()), |
| 401 base::Bind(&ChannelMojo::ForwardMessageWithResponderFromThreadSafePtr, |
| 402 weak_factory_.GetWeakPtr())); |
| 403 } |
| 404 |
380 void ChannelMojo::OnPeerPidReceived(int32_t peer_pid) { | 405 void ChannelMojo::OnPeerPidReceived(int32_t peer_pid) { |
381 listener_->OnChannelConnected(peer_pid); | 406 listener_->OnChannelConnected(peer_pid); |
382 } | 407 } |
383 | 408 |
384 void ChannelMojo::OnMessageReceived(const Message& message) { | 409 void ChannelMojo::OnMessageReceived(const Message& message) { |
385 TRACE_EVENT2("ipc,toplevel", "ChannelMojo::OnMessageReceived", | 410 TRACE_EVENT2("ipc,toplevel", "ChannelMojo::OnMessageReceived", |
386 "class", IPC_MESSAGE_ID_CLASS(message.type()), | 411 "class", IPC_MESSAGE_ID_CLASS(message.type()), |
387 "line", IPC_MESSAGE_ID_LINE(message.type())); | 412 "line", IPC_MESSAGE_ID_LINE(message.type())); |
388 listener_->OnMessageReceived(message); | 413 listener_->OnMessageReceived(message); |
389 if (message.dispatch_error()) | 414 if (message.dispatch_error()) |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
456 } | 481 } |
457 | 482 |
458 void ChannelMojo::GetGenericRemoteAssociatedInterface( | 483 void ChannelMojo::GetGenericRemoteAssociatedInterface( |
459 const std::string& name, | 484 const std::string& name, |
460 mojo::ScopedInterfaceEndpointHandle handle) { | 485 mojo::ScopedInterfaceEndpointHandle handle) { |
461 if (message_reader_) | 486 if (message_reader_) |
462 message_reader_->GetRemoteInterface(name, std::move(handle)); | 487 message_reader_->GetRemoteInterface(name, std::move(handle)); |
463 } | 488 } |
464 | 489 |
465 } // namespace IPC | 490 } // namespace IPC |
OLD | NEW |