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