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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "ipc/ipc_listener.h" | 10 #include "ipc/ipc_listener.h" |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 std::for_each(handles.begin(), handles.end(), &MojoClose); | 265 std::for_each(handles.begin(), handles.end(), &MojoClose); |
266 CloseWithError(write_result); | 266 CloseWithError(write_result); |
267 return false; | 267 return false; |
268 } | 268 } |
269 | 269 |
270 return true; | 270 return true; |
271 } | 271 } |
272 | 272 |
273 //------------------------------------------------------------------------------ | 273 //------------------------------------------------------------------------------ |
274 | 274 |
275 // MessagePipeReader implemenation for control messages. | 275 // MessagePipeReader implementation for control messages. |
276 // Actual message handling is implemented by sublcasses. | 276 // Actual message handling is implemented by sublcasses. |
277 class ChannelMojo::ControlReader : public internal::MessagePipeReader { | 277 class ChannelMojo::ControlReader : public internal::MessagePipeReader { |
278 public: | 278 public: |
279 ControlReader(mojo::ScopedMessagePipeHandle pipe, ChannelMojo* owner) | 279 ControlReader(mojo::ScopedMessagePipeHandle pipe, ChannelMojo* owner) |
280 : internal::MessagePipeReader(pipe.Pass()), | 280 : internal::MessagePipeReader(pipe.Pass()), |
281 owner_(owner) {} | 281 owner_(owner) {} |
282 | 282 |
283 virtual bool Connect() { return true; } | 283 virtual bool Connect() { return true; } |
284 virtual void OnPipeClosed() OVERRIDE; | 284 virtual void OnPipeClosed() OVERRIDE; |
285 virtual void OnPipeError(MojoResult error) OVERRIDE; | 285 virtual void OnPipeError(MojoResult error) OVERRIDE; |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 io_thread_task_runner)).PassAs<ChannelFactory>(); | 481 io_thread_task_runner)).PassAs<ChannelFactory>(); |
482 } | 482 } |
483 | 483 |
484 ChannelMojo::ChannelMojo( | 484 ChannelMojo::ChannelMojo( |
485 scoped_ptr<Channel> bootstrap, Mode mode, Listener* listener, | 485 scoped_ptr<Channel> bootstrap, Mode mode, Listener* listener, |
486 scoped_refptr<base::TaskRunner> io_thread_task_runner) | 486 scoped_refptr<base::TaskRunner> io_thread_task_runner) |
487 : weak_factory_(this), | 487 : weak_factory_(this), |
488 bootstrap_(bootstrap.Pass()), | 488 bootstrap_(bootstrap.Pass()), |
489 mode_(mode), listener_(listener), | 489 mode_(mode), listener_(listener), |
490 peer_pid_(base::kNullProcessId) { | 490 peer_pid_(base::kNullProcessId) { |
491 DCHECK(mode_ == MODE_SERVER || mode_ == MODE_CLIENT); | |
492 mojo::ScopedMessagePipeHandle control_pipe | |
493 = mojo::embedder::CreateChannel( | |
494 mojo::embedder::ScopedPlatformHandle( | |
495 ToPlatformHandle(bootstrap_->TakePipeHandle())), | |
496 io_thread_task_runner, | |
497 base::Bind(&ChannelMojo::DidCreateChannel, base::Unretained(this)), | |
498 io_thread_task_runner); | |
499 | |
500 // MessagePipeReader, that is crated in InitOnIOThread(), should live only in | |
501 // IO thread, but IPC::Channel can be instantiated outside of it. | |
502 // So we move the creation to the appropriate thread. | |
503 if (base::MessageLoopProxy::current() == io_thread_task_runner) { | 491 if (base::MessageLoopProxy::current() == io_thread_task_runner) { |
504 InitOnIOThread(control_pipe.Pass()); | 492 InitOnIOThread(); |
505 } else { | 493 } else { |
506 io_thread_task_runner->PostTask( | 494 io_thread_task_runner->PostTask(FROM_HERE, |
507 FROM_HERE, | 495 base::Bind(&ChannelMojo::InitOnIOThread, |
508 base::Bind(&ChannelMojo::InitOnIOThread, | 496 weak_factory_.GetWeakPtr())); |
509 weak_factory_.GetWeakPtr(), | |
510 base::Passed(control_pipe.Pass()))); | |
511 } | 497 } |
512 } | 498 } |
513 | 499 |
514 ChannelMojo::~ChannelMojo() { | 500 ChannelMojo::~ChannelMojo() { |
515 Close(); | 501 Close(); |
516 } | 502 } |
517 | 503 |
518 void ChannelMojo::InitOnIOThread(mojo::ScopedMessagePipeHandle control_pipe) { | 504 void ChannelMojo::InitOnIOThread() { |
519 control_reader_ = CreateControlReader(control_pipe.Pass()); | 505 mojo::embedder::ChannelInfo* channel_info; |
520 } | 506 mojo::ScopedMessagePipeHandle control_pipe = |
| 507 mojo::embedder::CreateChannelOnIOThread( |
| 508 mojo::embedder::ScopedPlatformHandle( |
| 509 ToPlatformHandle(bootstrap_->TakePipeHandle())), |
| 510 &channel_info); |
| 511 channel_info_.reset(channel_info); |
521 | 512 |
522 scoped_ptr<ChannelMojo::ControlReader> ChannelMojo::CreateControlReader( | 513 switch (mode_) { |
523 mojo::ScopedMessagePipeHandle pipe) { | 514 case MODE_SERVER: |
524 if (MODE_SERVER == mode_) { | 515 control_reader_.reset(new ServerControlReader(control_pipe.Pass(), this)); |
525 return make_scoped_ptr( | 516 break; |
526 new ServerControlReader(pipe.Pass(), this)).PassAs<ControlReader>(); | 517 case MODE_CLIENT: |
| 518 control_reader_.reset(new ClientControlReader(control_pipe.Pass(), this)); |
| 519 break; |
| 520 default: |
| 521 NOTREACHED(); |
| 522 break; |
527 } | 523 } |
528 | |
529 DCHECK(mode_ == MODE_CLIENT); | |
530 return make_scoped_ptr( | |
531 new ClientControlReader(pipe.Pass(), this)).PassAs<ControlReader>(); | |
532 } | 524 } |
533 | 525 |
534 bool ChannelMojo::Connect() { | 526 bool ChannelMojo::Connect() { |
535 DCHECK(!message_reader_); | 527 DCHECK(!message_reader_); |
536 return control_reader_->Connect(); | 528 return control_reader_->Connect(); |
537 } | 529 } |
538 | 530 |
539 void ChannelMojo::Close() { | 531 void ChannelMojo::Close() { |
540 control_reader_.reset(); | 532 control_reader_.reset(); |
541 message_reader_.reset(); | 533 message_reader_.reset(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
578 } | 570 } |
579 | 571 |
580 base::ProcessId ChannelMojo::GetSelfPID() const { | 572 base::ProcessId ChannelMojo::GetSelfPID() const { |
581 return bootstrap_->GetSelfPID(); | 573 return bootstrap_->GetSelfPID(); |
582 } | 574 } |
583 | 575 |
584 ChannelHandle ChannelMojo::TakePipeHandle() { | 576 ChannelHandle ChannelMojo::TakePipeHandle() { |
585 return bootstrap_->TakePipeHandle(); | 577 return bootstrap_->TakePipeHandle(); |
586 } | 578 } |
587 | 579 |
588 void ChannelMojo::DidCreateChannel(mojo::embedder::ChannelInfo* info) { | |
589 channel_info_.reset(info); | |
590 } | |
591 | |
592 void ChannelMojo::OnMessageReceived(Message& message) { | 580 void ChannelMojo::OnMessageReceived(Message& message) { |
593 listener_->OnMessageReceived(message); | 581 listener_->OnMessageReceived(message); |
594 if (message.dispatch_error()) | 582 if (message.dispatch_error()) |
595 listener_->OnBadMessageReceived(message); | 583 listener_->OnBadMessageReceived(message); |
596 } | 584 } |
597 | 585 |
598 #if defined(OS_POSIX) && !defined(OS_NACL) | 586 #if defined(OS_POSIX) && !defined(OS_NACL) |
599 int ChannelMojo::GetClientFileDescriptor() const { | 587 int ChannelMojo::GetClientFileDescriptor() const { |
600 return bootstrap_->GetClientFileDescriptor(); | 588 return bootstrap_->GetClientFileDescriptor(); |
601 } | 589 } |
602 | 590 |
603 int ChannelMojo::TakeClientFileDescriptor() { | 591 int ChannelMojo::TakeClientFileDescriptor() { |
604 return bootstrap_->TakeClientFileDescriptor(); | 592 return bootstrap_->TakeClientFileDescriptor(); |
605 } | 593 } |
606 #endif // defined(OS_POSIX) && !defined(OS_NACL) | 594 #endif // defined(OS_POSIX) && !defined(OS_NACL) |
607 | 595 |
608 } // namespace IPC | 596 } // namespace IPC |
OLD | NEW |