[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ipc/mojo/ipc_channel_mojo.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/bind_helpers.h" |
| 9 | #include "base/lazy_instance.h" |
| 10 | #include "ipc/ipc_listener.h" |
morrita | 7126b7a | 2014-12-17 19:01:40 | [diff] [blame] | 11 | #include "ipc/ipc_logging.h" |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 12 | #include "ipc/ipc_message_attachment_set.h" |
morrita | 7126b7a | 2014-12-17 19:01:40 | [diff] [blame] | 13 | #include "ipc/ipc_message_macros.h" |
morrita | f8f92dcd | 2014-10-27 20:10:25 | [diff] [blame] | 14 | #include "ipc/mojo/client_channel.mojom.h" |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 15 | #include "ipc/mojo/ipc_mojo_bootstrap.h" |
blundell | 471b74f | 2015-01-23 16:27:14 | [diff] [blame] | 16 | #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" |
| 17 | #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 18 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 19 | namespace IPC { |
| 20 | |
| 21 | namespace { |
| 22 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 23 | class MojoChannelFactory : public ChannelFactory { |
| 24 | public: |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 25 | MojoChannelFactory(ChannelMojo::Delegate* delegate, |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 26 | ChannelHandle channel_handle, |
| 27 | Channel::Mode mode) |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 28 | : delegate_(delegate), channel_handle_(channel_handle), mode_(mode) {} |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 29 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 30 | std::string GetName() const override { |
dcheng | f3076af | 2014-10-21 18:02:42 | [diff] [blame] | 31 | return channel_handle_.name; |
| 32 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 33 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 34 | scoped_ptr<Channel> BuildChannel(Listener* listener) override { |
dcheng | ecc340f | 2014-10-17 00:43:54 | [diff] [blame] | 35 | return ChannelMojo::Create(delegate_, channel_handle_, mode_, listener); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | private: |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 39 | ChannelMojo::Delegate* delegate_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 40 | ChannelHandle channel_handle_; |
| 41 | Channel::Mode mode_; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 42 | }; |
| 43 | |
morrita | f8f92dcd | 2014-10-27 20:10:25 | [diff] [blame] | 44 | //------------------------------------------------------------------------------ |
| 45 | |
| 46 | class ClientChannelMojo |
| 47 | : public ChannelMojo, |
| 48 | public NON_EXPORTED_BASE(mojo::InterfaceImpl<ClientChannel>) { |
| 49 | public: |
| 50 | ClientChannelMojo(ChannelMojo::Delegate* delegate, |
| 51 | const ChannelHandle& handle, |
| 52 | Listener* listener); |
| 53 | ~ClientChannelMojo() override; |
| 54 | // MojoBootstrap::Delegate implementation |
| 55 | void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; |
| 56 | // InterfaceImpl implementation |
| 57 | void OnConnectionError() override; |
| 58 | // ClientChannel implementation |
| 59 | void Init( |
| 60 | mojo::ScopedMessagePipeHandle pipe, |
| 61 | int32_t peer_pid, |
| 62 | const mojo::Callback<void(int32_t)>& callback) override; |
| 63 | |
| 64 | DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo); |
| 65 | }; |
| 66 | |
| 67 | ClientChannelMojo::ClientChannelMojo(ChannelMojo::Delegate* delegate, |
| 68 | const ChannelHandle& handle, |
| 69 | Listener* listener) |
| 70 | : ChannelMojo(delegate, handle, Channel::MODE_CLIENT, listener) { |
| 71 | } |
| 72 | |
| 73 | ClientChannelMojo::~ClientChannelMojo() { |
| 74 | } |
| 75 | |
| 76 | void ClientChannelMojo::OnPipeAvailable( |
| 77 | mojo::embedder::ScopedPlatformHandle handle) { |
| 78 | mojo::WeakBindToPipe(this, CreateMessagingPipe(handle.Pass())); |
| 79 | } |
| 80 | |
| 81 | void ClientChannelMojo::OnConnectionError() { |
| 82 | listener()->OnChannelError(); |
| 83 | } |
| 84 | |
| 85 | void ClientChannelMojo::Init( |
| 86 | mojo::ScopedMessagePipeHandle pipe, |
| 87 | int32_t peer_pid, |
| 88 | const mojo::Callback<void(int32_t)>& callback) { |
| 89 | InitMessageReader(pipe.Pass(), static_cast<base::ProcessId>(peer_pid)); |
| 90 | callback.Run(GetSelfPID()); |
| 91 | } |
| 92 | |
| 93 | //------------------------------------------------------------------------------ |
| 94 | |
| 95 | class ServerChannelMojo : public ChannelMojo, public mojo::ErrorHandler { |
| 96 | public: |
| 97 | ServerChannelMojo(ChannelMojo::Delegate* delegate, |
| 98 | const ChannelHandle& handle, |
| 99 | Listener* listener); |
| 100 | ~ServerChannelMojo() override; |
| 101 | |
| 102 | // MojoBootstrap::Delegate implementation |
| 103 | void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle) override; |
| 104 | // ErrorHandler implementation |
| 105 | void OnConnectionError() override; |
| 106 | // Channel override |
| 107 | void Close() override; |
| 108 | |
| 109 | private: |
| 110 | // ClientChannelClient implementation |
| 111 | void ClientChannelWasInitialized(int32_t peer_pid); |
| 112 | |
| 113 | mojo::InterfacePtr<ClientChannel> client_channel_; |
| 114 | mojo::ScopedMessagePipeHandle message_pipe_; |
| 115 | |
| 116 | DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo); |
| 117 | }; |
| 118 | |
| 119 | ServerChannelMojo::ServerChannelMojo(ChannelMojo::Delegate* delegate, |
| 120 | const ChannelHandle& handle, |
| 121 | Listener* listener) |
| 122 | : ChannelMojo(delegate, handle, Channel::MODE_SERVER, listener) { |
| 123 | } |
| 124 | |
| 125 | ServerChannelMojo::~ServerChannelMojo() { |
| 126 | Close(); |
| 127 | } |
| 128 | |
| 129 | void ServerChannelMojo::OnPipeAvailable( |
| 130 | mojo::embedder::ScopedPlatformHandle handle) { |
| 131 | mojo::ScopedMessagePipeHandle peer; |
| 132 | MojoResult create_result = |
| 133 | mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer); |
| 134 | if (create_result != MOJO_RESULT_OK) { |
| 135 | DLOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result; |
| 136 | listener()->OnChannelError(); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | client_channel_.Bind(CreateMessagingPipe(handle.Pass())); |
| 141 | client_channel_.set_error_handler(this); |
| 142 | client_channel_->Init( |
| 143 | peer.Pass(), |
| 144 | static_cast<int32_t>(GetSelfPID()), |
| 145 | base::Bind(&ServerChannelMojo::ClientChannelWasInitialized, |
| 146 | base::Unretained(this))); |
| 147 | } |
| 148 | |
| 149 | void ServerChannelMojo::ClientChannelWasInitialized(int32_t peer_pid) { |
| 150 | InitMessageReader(message_pipe_.Pass(), peer_pid); |
| 151 | } |
| 152 | |
| 153 | void ServerChannelMojo::OnConnectionError() { |
| 154 | listener()->OnChannelError(); |
| 155 | } |
| 156 | |
| 157 | void ServerChannelMojo::Close() { |
| 158 | client_channel_.reset(); |
| 159 | message_pipe_.reset(); |
| 160 | ChannelMojo::Close(); |
| 161 | } |
| 162 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 163 | } // namespace |
| 164 | |
| 165 | //------------------------------------------------------------------------------ |
| 166 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 167 | void ChannelMojo::ChannelInfoDeleter::operator()( |
| 168 | mojo::embedder::ChannelInfo* ptr) const { |
jamesr | a912526 | 2014-11-19 01:35:28 | [diff] [blame] | 169 | mojo::embedder::DestroyChannel(ptr); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | //------------------------------------------------------------------------------ |
| 173 | |
| 174 | // static |
morrita | 49ca46d | 2014-10-21 01:16:35 | [diff] [blame] | 175 | bool ChannelMojo::ShouldBeUsed() { |
morrita | 599be60 | 2015-01-27 18:56:47 | [diff] [blame^] | 176 | // TODO(morrita): Remove this if it sticks. |
| 177 | return true; |
morrita | 49ca46d | 2014-10-21 01:16:35 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // static |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 181 | scoped_ptr<ChannelMojo> ChannelMojo::Create(ChannelMojo::Delegate* delegate, |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 182 | const ChannelHandle& channel_handle, |
| 183 | Mode mode, |
| 184 | Listener* listener) { |
morrita | f8f92dcd | 2014-10-27 20:10:25 | [diff] [blame] | 185 | switch (mode) { |
| 186 | case Channel::MODE_CLIENT: |
| 187 | return make_scoped_ptr( |
| 188 | new ClientChannelMojo(delegate, channel_handle, listener)); |
| 189 | case Channel::MODE_SERVER: |
| 190 | return make_scoped_ptr( |
| 191 | new ServerChannelMojo(delegate, channel_handle, listener)); |
| 192 | default: |
| 193 | NOTREACHED(); |
| 194 | return nullptr; |
| 195 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // static |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 199 | scoped_ptr<ChannelFactory> ChannelMojo::CreateServerFactory( |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 200 | ChannelMojo::Delegate* delegate, |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 201 | const ChannelHandle& channel_handle) { |
dcheng | ecc340f | 2014-10-17 00:43:54 | [diff] [blame] | 202 | return make_scoped_ptr( |
| 203 | new MojoChannelFactory(delegate, channel_handle, Channel::MODE_SERVER)); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 204 | } |
| 205 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 206 | // static |
| 207 | scoped_ptr<ChannelFactory> ChannelMojo::CreateClientFactory( |
| 208 | const ChannelHandle& channel_handle) { |
| 209 | return make_scoped_ptr( |
dcheng | ecc340f | 2014-10-17 00:43:54 | [diff] [blame] | 210 | new MojoChannelFactory(NULL, channel_handle, Channel::MODE_CLIENT)); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 211 | } |
| 212 | |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 213 | ChannelMojo::ChannelMojo(ChannelMojo::Delegate* delegate, |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 214 | const ChannelHandle& handle, |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 215 | Mode mode, |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 216 | Listener* listener) |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 217 | : mode_(mode), |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 218 | listener_(listener), |
anujk.sharma | 0184ced | 2014-08-28 06:49:02 | [diff] [blame] | 219 | peer_pid_(base::kNullProcessId), |
| 220 | weak_factory_(this) { |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 221 | // Create MojoBootstrap after all members are set as it touches |
| 222 | // ChannelMojo from a different thread. |
| 223 | bootstrap_ = MojoBootstrap::Create(handle, mode, this); |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 224 | if (delegate) { |
| 225 | if (delegate->GetIOTaskRunner() == |
| 226 | base::MessageLoop::current()->message_loop_proxy()) { |
| 227 | InitDelegate(delegate); |
| 228 | } else { |
| 229 | delegate->GetIOTaskRunner()->PostTask( |
| 230 | FROM_HERE, |
| 231 | base::Bind( |
| 232 | &ChannelMojo::InitDelegate, base::Unretained(this), delegate)); |
| 233 | } |
| 234 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | ChannelMojo::~ChannelMojo() { |
| 238 | Close(); |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 239 | } |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 240 | |
morrita | e9453ea | 2014-09-26 03:20:48 | [diff] [blame] | 241 | void ChannelMojo::InitDelegate(ChannelMojo::Delegate* delegate) { |
| 242 | delegate_ = delegate->ToWeakPtr(); |
| 243 | delegate_->OnChannelCreated(weak_factory_.GetWeakPtr()); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 244 | } |
| 245 | |
morrita | f8f92dcd | 2014-10-27 20:10:25 | [diff] [blame] | 246 | mojo::ScopedMessagePipeHandle ChannelMojo::CreateMessagingPipe( |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 247 | mojo::embedder::ScopedPlatformHandle handle) { |
morrita | f8f92dcd | 2014-10-27 20:10:25 | [diff] [blame] | 248 | DCHECK(!channel_info_.get()); |
[email protected] | efbf95d | 2014-08-12 21:44:01 | [diff] [blame] | 249 | mojo::embedder::ChannelInfo* channel_info; |
morrita | f8f92dcd | 2014-10-27 20:10:25 | [diff] [blame] | 250 | mojo::ScopedMessagePipeHandle pipe = |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 251 | mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); |
[email protected] | efbf95d | 2014-08-12 21:44:01 | [diff] [blame] | 252 | channel_info_.reset(channel_info); |
morrita | f8f92dcd | 2014-10-27 20:10:25 | [diff] [blame] | 253 | return pipe.Pass(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | bool ChannelMojo::Connect() { |
| 257 | DCHECK(!message_reader_); |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 258 | return bootstrap_->Connect(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void ChannelMojo::Close() { |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 262 | message_reader_.reset(); |
| 263 | channel_info_.reset(); |
| 264 | } |
| 265 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 266 | void ChannelMojo::OnBootstrapError() { |
| 267 | listener_->OnChannelError(); |
| 268 | } |
| 269 | |
morrita | f8f92dcd | 2014-10-27 20:10:25 | [diff] [blame] | 270 | void ChannelMojo::InitMessageReader(mojo::ScopedMessagePipeHandle pipe, |
| 271 | int32_t peer_pid) { |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 272 | message_reader_ = |
morrita | d68bedf4 | 2014-11-25 23:35:57 | [diff] [blame] | 273 | make_scoped_ptr(new internal::MessagePipeReader(pipe.Pass(), this)); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 274 | |
| 275 | for (size_t i = 0; i < pending_messages_.size(); ++i) { |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 276 | bool sent = message_reader_->Send(make_scoped_ptr(pending_messages_[i])); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 277 | pending_messages_[i] = NULL; |
morrita | 0a24cfc9 | 2014-09-16 03:20:48 | [diff] [blame] | 278 | if (!sent) { |
| 279 | pending_messages_.clear(); |
| 280 | listener_->OnChannelError(); |
| 281 | return; |
| 282 | } |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | pending_messages_.clear(); |
| 286 | |
morrita | f8f92dcd | 2014-10-27 20:10:25 | [diff] [blame] | 287 | set_peer_pid(peer_pid); |
| 288 | listener_->OnChannelConnected(static_cast<int32_t>(GetPeerPID())); |
morrita | dcc28ab | 2015-01-10 01:49:21 | [diff] [blame] | 289 | if (message_reader_) |
| 290 | message_reader_->ReadMessagesThenWait(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void ChannelMojo::OnPipeClosed(internal::MessagePipeReader* reader) { |
| 294 | Close(); |
| 295 | } |
| 296 | |
| 297 | void ChannelMojo::OnPipeError(internal::MessagePipeReader* reader) { |
| 298 | listener_->OnChannelError(); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | bool ChannelMojo::Send(Message* message) { |
| 303 | if (!message_reader_) { |
| 304 | pending_messages_.push_back(message); |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | return message_reader_->Send(make_scoped_ptr(message)); |
| 309 | } |
| 310 | |
| 311 | base::ProcessId ChannelMojo::GetPeerPID() const { |
| 312 | return peer_pid_; |
| 313 | } |
| 314 | |
| 315 | base::ProcessId ChannelMojo::GetSelfPID() const { |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 316 | return base::GetCurrentProcId(); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 317 | } |
| 318 | |
morrita | 54f6f80c | 2014-09-23 21:16:00 | [diff] [blame] | 319 | void ChannelMojo::OnClientLaunched(base::ProcessHandle handle) { |
| 320 | bootstrap_->OnClientLaunched(handle); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 321 | } |
| 322 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 323 | void ChannelMojo::OnMessageReceived(Message& message) { |
morrita | 7126b7a | 2014-12-17 19:01:40 | [diff] [blame] | 324 | TRACE_EVENT2("ipc,toplevel", "ChannelMojo::OnMessageReceived", |
| 325 | "class", IPC_MESSAGE_ID_CLASS(message.type()), |
| 326 | "line", IPC_MESSAGE_ID_LINE(message.type())); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 327 | listener_->OnMessageReceived(message); |
| 328 | if (message.dispatch_error()) |
| 329 | listener_->OnBadMessageReceived(message); |
| 330 | } |
| 331 | |
| 332 | #if defined(OS_POSIX) && !defined(OS_NACL) |
| 333 | int ChannelMojo::GetClientFileDescriptor() const { |
| 334 | return bootstrap_->GetClientFileDescriptor(); |
| 335 | } |
| 336 | |
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame] | 337 | base::ScopedFD ChannelMojo::TakeClientFileDescriptor() { |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 338 | return bootstrap_->TakeClientFileDescriptor(); |
| 339 | } |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 340 | |
| 341 | // static |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 342 | MojoResult ChannelMojo::WriteToMessageAttachmentSet( |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 343 | const std::vector<MojoHandle>& handle_buffer, |
| 344 | Message* message) { |
| 345 | for (size_t i = 0; i < handle_buffer.size(); ++i) { |
| 346 | mojo::embedder::ScopedPlatformHandle platform_handle; |
| 347 | MojoResult unwrap_result = mojo::embedder::PassWrappedPlatformHandle( |
| 348 | handle_buffer[i], &platform_handle); |
| 349 | if (unwrap_result != MOJO_RESULT_OK) { |
| 350 | DLOG(WARNING) << "Pipe failed to covert handles. Closing: " |
| 351 | << unwrap_result; |
| 352 | return unwrap_result; |
| 353 | } |
| 354 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 355 | bool ok = message->attachment_set()->AddToOwn( |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 356 | base::ScopedFD(platform_handle.release().fd)); |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 357 | DCHECK(ok); |
| 358 | } |
| 359 | |
| 360 | return MOJO_RESULT_OK; |
| 361 | } |
| 362 | |
| 363 | // static |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 364 | MojoResult ChannelMojo::ReadFromMessageAttachmentSet( |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 365 | Message* message, |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 366 | std::vector<MojoHandle>* handles) { |
| 367 | // We dup() the handles in IPC::Message to transmit. |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 368 | // IPC::MessageAttachmentSet has intricate lifecycle semantics |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 369 | // of FDs, so just to dup()-and-own them is the safest option. |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 370 | if (message->HasFileDescriptors()) { |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 371 | MessageAttachmentSet* fdset = message->attachment_set(); |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 372 | std::vector<base::PlatformFile> fds_to_send(fdset->size()); |
| 373 | fdset->PeekDescriptors(&fds_to_send[0]); |
| 374 | for (size_t i = 0; i < fds_to_send.size(); ++i) { |
| 375 | int fd_to_send = dup(fds_to_send[i]); |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 376 | if (-1 == fd_to_send) { |
| 377 | DPLOG(WARNING) << "Failed to dup FD to transmit."; |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 378 | fdset->CommitAll(); |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 379 | return MOJO_RESULT_UNKNOWN; |
| 380 | } |
| 381 | |
| 382 | MojoHandle wrapped_handle; |
| 383 | MojoResult wrap_result = CreatePlatformHandleWrapper( |
| 384 | mojo::embedder::ScopedPlatformHandle( |
| 385 | mojo::embedder::PlatformHandle(fd_to_send)), |
| 386 | &wrapped_handle); |
| 387 | if (MOJO_RESULT_OK != wrap_result) { |
| 388 | DLOG(WARNING) << "Pipe failed to wrap handles. Closing: " |
| 389 | << wrap_result; |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 390 | fdset->CommitAll(); |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 391 | return wrap_result; |
| 392 | } |
| 393 | |
| 394 | handles->push_back(wrapped_handle); |
| 395 | } |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 396 | |
| 397 | fdset->CommitAll(); |
morrita | 3b41d6c | 2014-09-11 19:06:29 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | return MOJO_RESULT_OK; |
| 401 | } |
| 402 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 403 | #endif // defined(OS_POSIX) && !defined(OS_NACL) |
| 404 | |
| 405 | } // namespace IPC |