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