Chromium Code Reviews| 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/client_channel.mojom.h" | |
| 11 #include "ipc/mojo/ipc_channel_mojo_readers.h" | 12 #include "ipc/mojo/ipc_channel_mojo_readers.h" |
| 12 #include "ipc/mojo/ipc_mojo_bootstrap.h" | 13 #include "ipc/mojo/ipc_mojo_bootstrap.h" |
| 13 #include "mojo/edk/embedder/embedder.h" | 14 #include "mojo/edk/embedder/embedder.h" |
| 15 #include "mojo/public/cpp/bindings/error_handler.h" | |
| 14 | 16 |
| 15 #if defined(OS_POSIX) && !defined(OS_NACL) | 17 #if defined(OS_POSIX) && !defined(OS_NACL) |
| 16 #include "ipc/file_descriptor_set_posix.h" | 18 #include "ipc/file_descriptor_set_posix.h" |
| 17 #endif | 19 #endif |
| 18 | 20 |
| 19 namespace IPC { | 21 namespace IPC { |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 class MojoChannelFactory : public ChannelFactory { | 25 class MojoChannelFactory : public ChannelFactory { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 34 scoped_ptr<Channel> BuildChannel(Listener* listener) override { | 36 scoped_ptr<Channel> BuildChannel(Listener* listener) override { |
| 35 return ChannelMojo::Create(delegate_, channel_handle_, mode_, listener); | 37 return ChannelMojo::Create(delegate_, channel_handle_, mode_, listener); |
| 36 } | 38 } |
| 37 | 39 |
| 38 private: | 40 private: |
| 39 ChannelMojo::Delegate* delegate_; | 41 ChannelMojo::Delegate* delegate_; |
| 40 ChannelHandle channel_handle_; | 42 ChannelHandle channel_handle_; |
| 41 Channel::Mode mode_; | 43 Channel::Mode mode_; |
| 42 }; | 44 }; |
| 43 | 45 |
| 46 //------------------------------------------------------------------------------ | |
| 47 | |
| 48 class ClientChannelMojo | |
| 49 : public ChannelMojo, | |
| 50 public NON_EXPORTED_BASE(mojo::InterfaceImpl<ClientChannel>) { | |
| 51 public: | |
| 52 ClientChannelMojo(ChannelMojo::Delegate* delegate, | |
| 53 const ChannelHandle& handle, | |
| 54 Listener* listener); | |
| 55 | |
| 56 // MojoBootstrap::Delegate implementation | |
| 57 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; | |
| 58 // InterfaceImpl implementation | |
| 59 void OnConnectionError() override; | |
| 60 // ClientChannel implementation | |
| 61 void InitClientChannel(mojo::ScopedMessagePipeHandle pipe, | |
| 62 int32_t peer_pid) override; | |
| 63 }; | |
| 64 | |
| 65 ClientChannelMojo::ClientChannelMojo(ChannelMojo::Delegate* delegate, | |
| 66 const ChannelHandle& handle, | |
| 67 Listener* listener) | |
| 68 : ChannelMojo(delegate, handle, Channel::MODE_CLIENT, listener) { | |
| 69 } | |
| 70 | |
| 71 void ClientChannelMojo::OnPipeAvailable( | |
| 72 mojo::embedder::ScopedPlatformHandle handle) { | |
| 73 mojo::WeakBindToPipe(this, CreateMessagingPipe(handle.Pass())); | |
| 74 } | |
| 75 | |
| 76 void ClientChannelMojo::OnConnectionError() { | |
| 77 listener()->OnChannelError(); | |
| 78 } | |
| 79 | |
| 80 void ClientChannelMojo::InitClientChannel(mojo::ScopedMessagePipeHandle pipe, | |
| 81 int32_t peer_pid) { | |
| 82 InitMessageReader(pipe.Pass(), static_cast<base::ProcessId>(peer_pid)); | |
| 83 client()->ClientChannelWasInitialized(GetSelfPID()); | |
| 84 } | |
| 85 | |
| 86 //------------------------------------------------------------------------------ | |
| 87 | |
| 88 class ServerChannelMojo : public ChannelMojo, | |
| 89 public ClientChannelClient, | |
| 90 public mojo::ErrorHandler { | |
| 91 public: | |
| 92 ServerChannelMojo(ChannelMojo::Delegate* delegate, | |
| 93 const ChannelHandle& handle, | |
| 94 Listener* listener); | |
| 95 virtual ~ServerChannelMojo(); | |
| 96 | |
| 97 // MojoBootstrap::Delegate implementation | |
| 98 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; | |
| 99 // ClientChannelClient implementation | |
| 100 void ClientChannelWasInitialized(int32_t peer_pid) override; | |
| 101 // ErrorHandler implementation | |
| 102 void OnConnectionError() override; | |
| 103 // Channel override | |
| 104 void Close() override; | |
| 105 | |
| 106 private: | |
| 107 mojo::InterfacePtr<ClientChannel> client_channel_; | |
| 108 mojo::ScopedMessagePipeHandle message_pipe_; | |
| 109 }; | |
| 110 | |
| 111 ServerChannelMojo::ServerChannelMojo(ChannelMojo::Delegate* delegate, | |
| 112 const ChannelHandle& handle, | |
| 113 Listener* listener) | |
| 114 : ChannelMojo(delegate, handle, Channel::MODE_SERVER, listener) { | |
| 115 } | |
| 116 | |
| 117 ServerChannelMojo::~ServerChannelMojo() { | |
| 118 Close(); | |
| 119 } | |
| 120 | |
| 121 void ServerChannelMojo::OnPipeAvailable( | |
| 122 mojo::embedder::ScopedPlatformHandle handle) { | |
| 123 mojo::ScopedMessagePipeHandle peer; | |
| 124 MojoResult create_result = | |
| 125 mojo::CreateMessagePipe(NULL, &message_pipe_, &peer); | |
|
viettrungluu
2014/10/24 01:23:46
nullptr
Hajime Morrita
2014/10/24 17:35:11
Done.
| |
| 126 if (MOJO_RESULT_OK != create_result) { | |
|
viettrungluu
2014/10/24 01:23:46
Side note: In Chromium, we usually prefer to write
Hajime Morrita
2014/10/24 17:35:11
My bad habit :-(
Fixed.
| |
| 127 DLOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result; | |
| 128 listener()->OnChannelError(); | |
| 129 return; | |
| 130 } | |
| 131 | |
| 132 client_channel_.Bind(CreateMessagingPipe(handle.Pass())); | |
| 133 client_channel_.set_client(this); | |
| 134 client_channel_.set_error_handler(this); | |
| 135 client_channel_->InitClientChannel(peer.Pass(), | |
| 136 static_cast<int32_t>(GetSelfPID())); | |
| 137 } | |
| 138 | |
| 139 void ServerChannelMojo::ClientChannelWasInitialized(int32_t peer_pid) { | |
| 140 InitMessageReader(message_pipe_.Pass(), peer_pid); | |
| 141 } | |
| 142 | |
| 143 void ServerChannelMojo::OnConnectionError() { | |
| 144 listener()->OnChannelError(); | |
| 145 } | |
| 146 | |
| 147 void ServerChannelMojo::Close() { | |
| 148 client_channel_.reset(); | |
| 149 message_pipe_.reset(); | |
| 150 ChannelMojo::Close(); | |
| 151 } | |
| 152 | |
| 44 } // namespace | 153 } // namespace |
| 45 | 154 |
| 46 //------------------------------------------------------------------------------ | 155 //------------------------------------------------------------------------------ |
| 47 | 156 |
| 48 void ChannelMojo::ChannelInfoDeleter::operator()( | 157 void ChannelMojo::ChannelInfoDeleter::operator()( |
| 49 mojo::embedder::ChannelInfo* ptr) const { | 158 mojo::embedder::ChannelInfo* ptr) const { |
| 50 mojo::embedder::DestroyChannelOnIOThread(ptr); | 159 mojo::embedder::DestroyChannelOnIOThread(ptr); |
| 51 } | 160 } |
| 52 | 161 |
| 53 //------------------------------------------------------------------------------ | 162 //------------------------------------------------------------------------------ |
| 54 | 163 |
| 55 // static | 164 // static |
| 56 bool ChannelMojo::ShouldBeUsed() { | 165 bool ChannelMojo::ShouldBeUsed() { |
| 57 // TODO(morrita): Turn this on for a set of platforms. | 166 // TODO(morrita): Turn this on for a set of platforms. |
| 58 return false; | 167 return false; |
| 59 } | 168 } |
| 60 | 169 |
| 61 // static | 170 // static |
| 62 scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojo::Delegate* delegate, | 171 scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojo::Delegate* delegate, |
| 63 const ChannelHandle& channel_handle, | 172 const ChannelHandle& channel_handle, |
| 64 Mode mode, | 173 Mode mode, |
| 65 Listener* listener) { | 174 Listener* listener) { |
| 66 return make_scoped_ptr( | 175 switch (mode) { |
| 67 new ChannelMojo(delegate, channel_handle, mode, listener)); | 176 case Channel::MODE_CLIENT: |
| 177 return make_scoped_ptr( | |
| 178 new ClientChannelMojo(delegate, channel_handle, listener)); | |
| 179 case Channel::MODE_SERVER: | |
| 180 return make_scoped_ptr( | |
| 181 new ServerChannelMojo(delegate, channel_handle, listener)); | |
| 182 default: | |
| 183 NOTREACHED(); | |
| 184 return nullptr; | |
| 185 } | |
| 68 } | 186 } |
| 69 | 187 |
| 70 // static | 188 // static |
| 71 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( | 189 scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( |
| 72 ChannelMojo::Delegate* delegate, | 190 ChannelMojo::Delegate* delegate, |
| 73 const ChannelHandle& channel_handle) { | 191 const ChannelHandle& channel_handle) { |
| 74 return make_scoped_ptr( | 192 return make_scoped_ptr( |
| 75 new MojoChannelFactory(delegate, channel_handle, Channel::MODE_SERVER)); | 193 new MojoChannelFactory(delegate, channel_handle, Channel::MODE_SERVER)); |
| 76 } | 194 } |
| 77 | 195 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 108 | 226 |
| 109 ChannelMojo::~ChannelMojo() { | 227 ChannelMojo::~ChannelMojo() { |
| 110 Close(); | 228 Close(); |
| 111 } | 229 } |
| 112 | 230 |
| 113 void ChannelMojo::InitDelegate(ChannelMojo::Delegate* delegate) { | 231 void ChannelMojo::InitDelegate(ChannelMojo::Delegate* delegate) { |
| 114 delegate_ = delegate->ToWeakPtr(); | 232 delegate_ = delegate->ToWeakPtr(); |
| 115 delegate_->OnChannelCreated(weak_factory_.GetWeakPtr()); | 233 delegate_->OnChannelCreated(weak_factory_.GetWeakPtr()); |
| 116 } | 234 } |
| 117 | 235 |
| 118 void ChannelMojo::InitControlReader( | 236 mojo::ScopedMessagePipeHandle ChannelMojo::CreateMessagingPipe( |
| 119 mojo::embedder::ScopedPlatformHandle handle) { | 237 mojo::embedder::ScopedPlatformHandle handle) { |
| 120 DCHECK(base::MessageLoopForIO::IsCurrent()); | 238 DCHECK(!channel_info_.get()); |
| 121 mojo::embedder::ChannelInfo* channel_info; | 239 mojo::embedder::ChannelInfo* channel_info; |
| 122 mojo::ScopedMessagePipeHandle control_pipe = | 240 mojo::ScopedMessagePipeHandle pipe = |
| 123 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); | 241 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); |
| 124 channel_info_.reset(channel_info); | 242 channel_info_.reset(channel_info); |
| 125 | 243 return pipe.Pass(); |
| 126 switch (mode_) { | |
| 127 case MODE_SERVER: | |
| 128 control_reader_.reset( | |
| 129 new internal::ServerControlReader(control_pipe.Pass(), this)); | |
| 130 break; | |
| 131 case MODE_CLIENT: | |
| 132 control_reader_.reset( | |
| 133 new internal::ClientControlReader(control_pipe.Pass(), this)); | |
| 134 break; | |
| 135 default: | |
| 136 NOTREACHED(); | |
| 137 break; | |
| 138 } | |
| 139 } | 244 } |
| 140 | 245 |
| 141 bool ChannelMojo::Connect() { | 246 bool ChannelMojo::Connect() { |
| 142 DCHECK(!message_reader_); | 247 DCHECK(!message_reader_); |
| 143 DCHECK(!control_reader_); | |
| 144 return bootstrap_->Connect(); | 248 return bootstrap_->Connect(); |
| 145 } | 249 } |
| 146 | 250 |
| 147 void ChannelMojo::Close() { | 251 void ChannelMojo::Close() { |
| 148 control_reader_.reset(); | |
| 149 message_reader_.reset(); | 252 message_reader_.reset(); |
| 150 channel_info_.reset(); | 253 channel_info_.reset(); |
| 151 } | 254 } |
| 152 | 255 |
| 153 void ChannelMojo::OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) { | |
| 154 InitControlReader(handle.Pass()); | |
| 155 control_reader_->Connect(); | |
| 156 } | |
| 157 | |
| 158 void ChannelMojo::OnBootstrapError() { | 256 void ChannelMojo::OnBootstrapError() { |
| 159 listener_->OnChannelError(); | 257 listener_->OnChannelError(); |
| 160 } | 258 } |
| 161 | 259 |
| 162 void ChannelMojo::OnConnected(mojo::ScopedMessagePipeHandle pipe) { | 260 void ChannelMojo::InitMessageReader(mojo::ScopedMessagePipeHandle pipe, |
| 261 int32_t peer_pid) { | |
| 163 message_reader_ = | 262 message_reader_ = |
| 164 make_scoped_ptr(new internal::MessageReader(pipe.Pass(), this)); | 263 make_scoped_ptr(new internal::MessageReader(pipe.Pass(), this)); |
| 165 | 264 |
| 166 for (size_t i = 0; i < pending_messages_.size(); ++i) { | 265 for (size_t i = 0; i < pending_messages_.size(); ++i) { |
| 167 bool sent = message_reader_->Send(make_scoped_ptr(pending_messages_[i])); | 266 bool sent = message_reader_->Send(make_scoped_ptr(pending_messages_[i])); |
| 168 pending_messages_[i] = NULL; | 267 pending_messages_[i] = NULL; |
| 169 if (!sent) { | 268 if (!sent) { |
| 170 pending_messages_.clear(); | 269 pending_messages_.clear(); |
| 171 listener_->OnChannelError(); | 270 listener_->OnChannelError(); |
| 172 return; | 271 return; |
| 173 } | 272 } |
| 174 } | 273 } |
| 175 | 274 |
| 176 pending_messages_.clear(); | 275 pending_messages_.clear(); |
| 177 | 276 |
| 277 set_peer_pid(peer_pid); | |
| 178 listener_->OnChannelConnected(GetPeerPID()); | 278 listener_->OnChannelConnected(GetPeerPID()); |
| 179 } | 279 } |
| 180 | 280 |
| 181 void ChannelMojo::OnPipeClosed(internal::MessagePipeReader* reader) { | 281 void ChannelMojo::OnPipeClosed(internal::MessagePipeReader* reader) { |
| 182 Close(); | 282 Close(); |
| 183 } | 283 } |
| 184 | 284 |
| 185 void ChannelMojo::OnPipeError(internal::MessagePipeReader* reader) { | 285 void ChannelMojo::OnPipeError(internal::MessagePipeReader* reader) { |
| 186 listener_->OnChannelError(); | 286 listener_->OnChannelError(); |
| 187 } | 287 } |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 281 | 381 |
| 282 fdset->CommitAll(); | 382 fdset->CommitAll(); |
| 283 } | 383 } |
| 284 | 384 |
| 285 return MOJO_RESULT_OK; | 385 return MOJO_RESULT_OK; |
| 286 } | 386 } |
| 287 | 387 |
| 288 #endif // defined(OS_POSIX) && !defined(OS_NACL) | 388 #endif // defined(OS_POSIX) && !defined(OS_NACL) |
| 289 | 389 |
| 290 } // namespace IPC | 390 } // namespace IPC |
| OLD | NEW |