| 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" |
| 11 #include "ipc/mojo/ipc_channel_mojo_host.h" | |
| 12 #include "ipc/mojo/ipc_channel_mojo_readers.h" | 11 #include "ipc/mojo/ipc_channel_mojo_readers.h" |
| 13 #include "ipc/mojo/ipc_mojo_bootstrap.h" | 12 #include "ipc/mojo/ipc_mojo_bootstrap.h" |
| 14 #include "mojo/embedder/embedder.h" | 13 #include "mojo/embedder/embedder.h" |
| 15 | 14 |
| 16 #if defined(OS_POSIX) && !defined(OS_NACL) | 15 #if defined(OS_POSIX) && !defined(OS_NACL) |
| 17 #include "ipc/file_descriptor_set_posix.h" | 16 #include "ipc/file_descriptor_set_posix.h" |
| 18 #endif | 17 #endif |
| 19 | 18 |
| 20 namespace IPC { | 19 namespace IPC { |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 class MojoChannelFactory : public ChannelFactory { | 23 class MojoChannelFactory : public ChannelFactory { |
| 25 public: | 24 public: |
| 26 MojoChannelFactory(ChannelMojoHost* host, | 25 MojoChannelFactory(ChannelMojo::Delegate* delegate, |
| 27 ChannelHandle channel_handle, | 26 ChannelHandle channel_handle, |
| 28 Channel::Mode mode) | 27 Channel::Mode mode) |
| 29 : host_(host), channel_handle_(channel_handle), mode_(mode) {} | 28 : delegate_(delegate), channel_handle_(channel_handle), mode_(mode) {} |
| 30 | 29 |
| 31 virtual std::string GetName() const OVERRIDE { | 30 virtual std::string GetName() const OVERRIDE { |
| 32 return channel_handle_.name; | 31 return channel_handle_.name; |
| 33 } | 32 } |
| 34 | 33 |
| 35 virtual scoped_ptr<Channel> BuildChannel(Listener* listener) OVERRIDE { | 34 virtual scoped_ptr<Channel> BuildChannel(Listener* listener) OVERRIDE { |
| 36 return ChannelMojo::Create(host_, channel_handle_, mode_, listener) | 35 return ChannelMojo::Create(delegate_, channel_handle_, mode_, listener) |
| 37 .PassAs<Channel>(); | 36 .PassAs<Channel>(); |
| 38 } | 37 } |
| 39 | 38 |
| 40 private: | 39 private: |
| 41 ChannelMojoHost* host_; | 40 ChannelMojo::Delegate* delegate_; |
| 42 ChannelHandle channel_handle_; | 41 ChannelHandle channel_handle_; |
| 43 Channel::Mode mode_; | 42 Channel::Mode mode_; |
| 44 }; | 43 }; |
| 45 | 44 |
| 46 } // namespace | 45 } // namespace |
| 47 | 46 |
| 48 //------------------------------------------------------------------------------ | 47 //------------------------------------------------------------------------------ |
| 49 | 48 |
| 50 void ChannelMojo::ChannelInfoDeleter::operator()( | 49 void ChannelMojo::ChannelInfoDeleter::operator()( |
| 51 mojo::embedder::ChannelInfo* ptr) const { | 50 mojo::embedder::ChannelInfo* ptr) const { |
| 52 mojo::embedder::DestroyChannelOnIOThread(ptr); | 51 mojo::embedder::DestroyChannelOnIOThread(ptr); |
| 53 } | 52 } |
| 54 | 53 |
| 55 //------------------------------------------------------------------------------ | 54 //------------------------------------------------------------------------------ |
| 56 | 55 |
| 57 // static | 56 // static |
| 58 scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojoHost* host, | 57 scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojo::Delegate* delegate, |
| 59 const ChannelHandle& channel_handle, | 58 const ChannelHandle& channel_handle, |
| 60 Mode mode, | 59 Mode mode, |
| 61 Listener* listener) { | 60 Listener* listener) { |
| 62 return make_scoped_ptr(new ChannelMojo(host, channel_handle, mode, listener)); | 61 return make_scoped_ptr( |
| 62 new ChannelMojo(delegate, channel_handle, mode, listener)); |
| 63 } | 63 } |
| 64 | 64 |
| 65 // static | 65 // static |
| 66 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( | 66 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( |
| 67 ChannelMojoHost* host, | 67 ChannelMojo::Delegate* delegate, |
| 68 const ChannelHandle& channel_handle) { | 68 const ChannelHandle& channel_handle) { |
| 69 return make_scoped_ptr( | 69 return make_scoped_ptr(new MojoChannelFactory( |
| 70 new MojoChannelFactory(host, channel_handle, Channel::MODE_SERVER)) | 70 delegate, channel_handle, Channel::MODE_SERVER)) |
| 71 .PassAs<ChannelFactory>(); | 71 .PassAs<ChannelFactory>(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // static | 74 // static |
| 75 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( | 75 scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( |
| 76 const ChannelHandle& channel_handle) { | 76 const ChannelHandle& channel_handle) { |
| 77 return make_scoped_ptr( | 77 return make_scoped_ptr( |
| 78 new MojoChannelFactory(NULL, channel_handle, Channel::MODE_CLIENT)) | 78 new MojoChannelFactory(NULL, channel_handle, Channel::MODE_CLIENT)) |
| 79 .PassAs<ChannelFactory>(); | 79 .PassAs<ChannelFactory>(); |
| 80 } | 80 } |
| 81 | 81 |
| 82 ChannelMojo::ChannelMojo(ChannelMojoHost* host, | 82 ChannelMojo::ChannelMojo(ChannelMojo::Delegate* delegate, |
| 83 const ChannelHandle& handle, | 83 const ChannelHandle& handle, |
| 84 Mode mode, | 84 Mode mode, |
| 85 Listener* listener) | 85 Listener* listener) |
| 86 : host_(host), | 86 : mode_(mode), |
| 87 mode_(mode), | |
| 88 listener_(listener), | 87 listener_(listener), |
| 89 peer_pid_(base::kNullProcessId), | 88 peer_pid_(base::kNullProcessId), |
| 90 weak_factory_(this) { | 89 weak_factory_(this) { |
| 91 // Create MojoBootstrap after all members are set as it touches | 90 // Create MojoBootstrap after all members are set as it touches |
| 92 // ChannelMojo from a different thread. | 91 // ChannelMojo from a different thread. |
| 93 bootstrap_ = MojoBootstrap::Create(handle, mode, this); | 92 bootstrap_ = MojoBootstrap::Create(handle, mode, this); |
| 94 if (host_) | 93 if (delegate) { |
| 95 host_->OnChannelCreated(this); | 94 delegate_ = delegate->ToWeakPtr(); |
| 95 delegate_->OnChannelCreated(weak_factory_.GetWeakPtr()); |
| 96 } |
| 96 } | 97 } |
| 97 | 98 |
| 98 ChannelMojo::~ChannelMojo() { | 99 ChannelMojo::~ChannelMojo() { |
| 99 Close(); | 100 Close(); |
| 100 | |
| 101 if (host_) | |
| 102 host_->OnChannelDestroyed(); | |
| 103 } | 101 } |
| 104 | 102 |
| 105 void ChannelMojo::InitControlReader( | 103 void ChannelMojo::InitControlReader( |
| 106 mojo::embedder::ScopedPlatformHandle handle) { | 104 mojo::embedder::ScopedPlatformHandle handle) { |
| 107 DCHECK(base::MessageLoopForIO::IsCurrent()); | 105 DCHECK(base::MessageLoopForIO::IsCurrent()); |
| 108 mojo::embedder::ChannelInfo* channel_info; | 106 mojo::embedder::ChannelInfo* channel_info; |
| 109 mojo::ScopedMessagePipeHandle control_pipe = | 107 mojo::ScopedMessagePipeHandle control_pipe = |
| 110 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); | 108 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); |
| 111 channel_info_.reset(channel_info); | 109 channel_info_.reset(channel_info); |
| 112 | 110 |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 | 271 |
| 274 fdset->CommitAll(); | 272 fdset->CommitAll(); |
| 275 } | 273 } |
| 276 | 274 |
| 277 return MOJO_RESULT_OK; | 275 return MOJO_RESULT_OK; |
| 278 } | 276 } |
| 279 | 277 |
| 280 #endif // defined(OS_POSIX) && !defined(OS_NACL) | 278 #endif // defined(OS_POSIX) && !defined(OS_NACL) |
| 281 | 279 |
| 282 } // namespace IPC | 280 } // namespace IPC |
| OLD | NEW |