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