| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | |
| 10 #include <memory> | 9 #include <memory> |
| 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "ipc/ipc_listener.h" | 19 #include "ipc/ipc_listener.h" |
| 20 #include "ipc/ipc_logging.h" | 20 #include "ipc/ipc_logging.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 : ChannelMojo(io_runner, handle, Channel::MODE_CLIENT, listener), | 69 : ChannelMojo(io_runner, handle, Channel::MODE_CLIENT, listener), |
| 70 binding_(this), | 70 binding_(this), |
| 71 weak_factory_(this) { | 71 weak_factory_(this) { |
| 72 } | 72 } |
| 73 ~ClientChannelMojo() override {} | 73 ~ClientChannelMojo() override {} |
| 74 | 74 |
| 75 // MojoBootstrap::Delegate implementation | 75 // MojoBootstrap::Delegate implementation |
| 76 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle, | 76 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle, |
| 77 int32_t peer_pid) override { | 77 int32_t peer_pid) override { |
| 78 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { | 78 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { |
| 79 InitMessageReader(mojo::embedder::CreateChannel( | 79 InitMessageReader( |
| 80 handle.Pass(), base::Callback<void(mojo::embedder::ChannelInfo*)>(), | 80 mojo::embedder::CreateChannel( |
| 81 scoped_refptr<base::TaskRunner>()), peer_pid); | 81 std::move(handle), |
| 82 base::Callback<void(mojo::embedder::ChannelInfo*)>(), |
| 83 scoped_refptr<base::TaskRunner>()), |
| 84 peer_pid); |
| 82 return; | 85 return; |
| 83 } | 86 } |
| 84 CreateMessagingPipe(handle.Pass(), base::Bind(&ClientChannelMojo::BindPipe, | 87 CreateMessagingPipe( |
| 85 weak_factory_.GetWeakPtr())); | 88 std::move(handle), |
| 89 base::Bind(&ClientChannelMojo::BindPipe, weak_factory_.GetWeakPtr())); |
| 86 } | 90 } |
| 87 | 91 |
| 88 // ClientChannel implementation | 92 // ClientChannel implementation |
| 89 void Init( | 93 void Init( |
| 90 mojo::ScopedMessagePipeHandle pipe, | 94 mojo::ScopedMessagePipeHandle pipe, |
| 91 int32_t peer_pid, | 95 int32_t peer_pid, |
| 92 const mojo::Callback<void(int32_t)>& callback) override { | 96 const mojo::Callback<void(int32_t)>& callback) override { |
| 93 InitMessageReader(pipe.Pass(), static_cast<base::ProcessId>(peer_pid)); | 97 InitMessageReader(std::move(pipe), static_cast<base::ProcessId>(peer_pid)); |
| 94 callback.Run(GetSelfPID()); | 98 callback.Run(GetSelfPID()); |
| 95 } | 99 } |
| 96 | 100 |
| 97 private: | 101 private: |
| 98 void BindPipe(mojo::ScopedMessagePipeHandle handle) { | 102 void BindPipe(mojo::ScopedMessagePipeHandle handle) { |
| 99 binding_.Bind(handle.Pass()); | 103 binding_.Bind(std::move(handle)); |
| 100 } | 104 } |
| 101 void OnConnectionError() { | 105 void OnConnectionError() { |
| 102 listener()->OnChannelError(); | 106 listener()->OnChannelError(); |
| 103 } | 107 } |
| 104 | 108 |
| 105 mojo::Binding<ClientChannel> binding_; | 109 mojo::Binding<ClientChannel> binding_; |
| 106 base::WeakPtrFactory<ClientChannelMojo> weak_factory_; | 110 base::WeakPtrFactory<ClientChannelMojo> weak_factory_; |
| 107 | 111 |
| 108 DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo); | 112 DISALLOW_COPY_AND_ASSIGN(ClientChannelMojo); |
| 109 }; | 113 }; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 120 } | 124 } |
| 121 ~ServerChannelMojo() override { | 125 ~ServerChannelMojo() override { |
| 122 Close(); | 126 Close(); |
| 123 } | 127 } |
| 124 | 128 |
| 125 // MojoBootstrap::Delegate implementation | 129 // MojoBootstrap::Delegate implementation |
| 126 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle, | 130 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle, |
| 127 int32_t peer_pid) override { | 131 int32_t peer_pid) override { |
| 128 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { | 132 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { |
| 129 message_pipe_ = mojo::embedder::CreateChannel( | 133 message_pipe_ = mojo::embedder::CreateChannel( |
| 130 handle.Pass(), base::Callback<void(mojo::embedder::ChannelInfo*)>(), | 134 std::move(handle), |
| 135 base::Callback<void(mojo::embedder::ChannelInfo*)>(), |
| 131 scoped_refptr<base::TaskRunner>()); | 136 scoped_refptr<base::TaskRunner>()); |
| 132 if (!message_pipe_.is_valid()) { | 137 if (!message_pipe_.is_valid()) { |
| 133 LOG(WARNING) << "mojo::CreateMessagePipe failed: "; | 138 LOG(WARNING) << "mojo::CreateMessagePipe failed: "; |
| 134 listener()->OnChannelError(); | 139 listener()->OnChannelError(); |
| 135 return; | 140 return; |
| 136 } | 141 } |
| 137 InitMessageReader(message_pipe_.Pass(), peer_pid); | 142 InitMessageReader(std::move(message_pipe_), peer_pid); |
| 138 return; | 143 return; |
| 139 } | 144 } |
| 140 | 145 |
| 141 mojo::ScopedMessagePipeHandle peer; | 146 mojo::ScopedMessagePipeHandle peer; |
| 142 MojoResult create_result = | 147 MojoResult create_result = |
| 143 mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer); | 148 mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer); |
| 144 if (create_result != MOJO_RESULT_OK) { | 149 if (create_result != MOJO_RESULT_OK) { |
| 145 LOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result; | 150 LOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result; |
| 146 listener()->OnChannelError(); | 151 listener()->OnChannelError(); |
| 147 return; | 152 return; |
| 148 } | 153 } |
| 149 CreateMessagingPipe( | 154 CreateMessagingPipe( |
| 150 handle.Pass(), | 155 std::move(handle), |
| 151 base::Bind(&ServerChannelMojo::InitClientChannel, | 156 base::Bind(&ServerChannelMojo::InitClientChannel, |
| 152 weak_factory_.GetWeakPtr(), base::Passed(&peer))); | 157 weak_factory_.GetWeakPtr(), base::Passed(&peer))); |
| 153 } | 158 } |
| 154 // Channel override | 159 // Channel override |
| 155 void Close() override { | 160 void Close() override { |
| 156 client_channel_.reset(); | 161 client_channel_.reset(); |
| 157 message_pipe_.reset(); | 162 message_pipe_.reset(); |
| 158 ChannelMojo::Close(); | 163 ChannelMojo::Close(); |
| 159 } | 164 } |
| 160 | 165 |
| 161 private: | 166 private: |
| 162 void InitClientChannel(mojo::ScopedMessagePipeHandle peer_handle, | 167 void InitClientChannel(mojo::ScopedMessagePipeHandle peer_handle, |
| 163 mojo::ScopedMessagePipeHandle handle) { | 168 mojo::ScopedMessagePipeHandle handle) { |
| 164 client_channel_.Bind( | 169 client_channel_.Bind( |
| 165 mojo::InterfacePtrInfo<ClientChannel>(handle.Pass(), 0u)); | 170 mojo::InterfacePtrInfo<ClientChannel>(std::move(handle), 0u)); |
| 166 client_channel_.set_connection_error_handler(base::Bind( | 171 client_channel_.set_connection_error_handler(base::Bind( |
| 167 &ServerChannelMojo::OnConnectionError, base::Unretained(this))); | 172 &ServerChannelMojo::OnConnectionError, base::Unretained(this))); |
| 168 client_channel_->Init( | 173 client_channel_->Init( |
| 169 peer_handle.Pass(), static_cast<int32_t>(GetSelfPID()), | 174 std::move(peer_handle), static_cast<int32_t>(GetSelfPID()), |
| 170 base::Bind(&ServerChannelMojo::ClientChannelWasInitialized, | 175 base::Bind(&ServerChannelMojo::ClientChannelWasInitialized, |
| 171 base::Unretained(this))); | 176 base::Unretained(this))); |
| 172 } | 177 } |
| 173 | 178 |
| 174 void OnConnectionError() { | 179 void OnConnectionError() { |
| 175 listener()->OnChannelError(); | 180 listener()->OnChannelError(); |
| 176 } | 181 } |
| 177 | 182 |
| 178 // ClientChannelClient implementation | 183 // ClientChannelClient implementation |
| 179 void ClientChannelWasInitialized(int32_t peer_pid) { | 184 void ClientChannelWasInitialized(int32_t peer_pid) { |
| 180 InitMessageReader(message_pipe_.Pass(), peer_pid); | 185 InitMessageReader(std::move(message_pipe_), peer_pid); |
| 181 } | 186 } |
| 182 | 187 |
| 183 mojo::InterfacePtr<ClientChannel> client_channel_; | 188 mojo::InterfacePtr<ClientChannel> client_channel_; |
| 184 mojo::ScopedMessagePipeHandle message_pipe_; | 189 mojo::ScopedMessagePipeHandle message_pipe_; |
| 185 base::WeakPtrFactory<ServerChannelMojo> weak_factory_; | 190 base::WeakPtrFactory<ServerChannelMojo> weak_factory_; |
| 186 | 191 |
| 187 DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo); | 192 DISALLOW_COPY_AND_ASSIGN(ServerChannelMojo); |
| 188 }; | 193 }; |
| 189 | 194 |
| 190 #if defined(OS_POSIX) && !defined(OS_NACL) | 195 #if defined(OS_POSIX) && !defined(OS_NACL) |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 new ScopedIPCSupport(base::MessageLoop::current()->task_runner())); | 298 new ScopedIPCSupport(base::MessageLoop::current()->task_runner())); |
| 294 } | 299 } |
| 295 | 300 |
| 296 void ChannelMojo::CreateMessagingPipe( | 301 void ChannelMojo::CreateMessagingPipe( |
| 297 mojo::embedder::ScopedPlatformHandle handle, | 302 mojo::embedder::ScopedPlatformHandle handle, |
| 298 const CreateMessagingPipeCallback& callback) { | 303 const CreateMessagingPipeCallback& callback) { |
| 299 auto return_callback = base::Bind(&ChannelMojo::OnMessagingPipeCreated, | 304 auto return_callback = base::Bind(&ChannelMojo::OnMessagingPipeCreated, |
| 300 weak_factory_.GetWeakPtr(), callback); | 305 weak_factory_.GetWeakPtr(), callback); |
| 301 if (!g_use_channel_on_io_thread_only || | 306 if (!g_use_channel_on_io_thread_only || |
| 302 base::ThreadTaskRunnerHandle::Get() == io_runner_) { | 307 base::ThreadTaskRunnerHandle::Get() == io_runner_) { |
| 303 CreateMessagingPipeOnIOThread( | 308 CreateMessagingPipeOnIOThread(std::move(handle), |
| 304 handle.Pass(), base::ThreadTaskRunnerHandle::Get(), return_callback); | 309 base::ThreadTaskRunnerHandle::Get(), |
| 310 return_callback); |
| 305 } else { | 311 } else { |
| 306 io_runner_->PostTask( | 312 io_runner_->PostTask( |
| 307 FROM_HERE, | 313 FROM_HERE, |
| 308 base::Bind(&ChannelMojo::CreateMessagingPipeOnIOThread, | 314 base::Bind(&ChannelMojo::CreateMessagingPipeOnIOThread, |
| 309 base::Passed(&handle), base::ThreadTaskRunnerHandle::Get(), | 315 base::Passed(&handle), base::ThreadTaskRunnerHandle::Get(), |
| 310 return_callback)); | 316 return_callback)); |
| 311 } | 317 } |
| 312 } | 318 } |
| 313 | 319 |
| 314 // static | 320 // static |
| 315 void ChannelMojo::CreateMessagingPipeOnIOThread( | 321 void ChannelMojo::CreateMessagingPipeOnIOThread( |
| 316 mojo::embedder::ScopedPlatformHandle handle, | 322 mojo::embedder::ScopedPlatformHandle handle, |
| 317 scoped_refptr<base::TaskRunner> callback_runner, | 323 scoped_refptr<base::TaskRunner> callback_runner, |
| 318 const CreateMessagingPipeOnIOThreadCallback& callback) { | 324 const CreateMessagingPipeOnIOThreadCallback& callback) { |
| 319 mojo::embedder::ChannelInfo* channel_info; | 325 mojo::embedder::ChannelInfo* channel_info; |
| 320 mojo::ScopedMessagePipeHandle pipe = | 326 mojo::ScopedMessagePipeHandle pipe = |
| 321 mojo::embedder::CreateChannelOnIOThread(handle.Pass(), &channel_info); | 327 mojo::embedder::CreateChannelOnIOThread(std::move(handle), &channel_info); |
| 322 if (base::ThreadTaskRunnerHandle::Get() == callback_runner) { | 328 if (base::ThreadTaskRunnerHandle::Get() == callback_runner) { |
| 323 callback.Run(pipe.Pass(), channel_info); | 329 callback.Run(std::move(pipe), channel_info); |
| 324 } else { | 330 } else { |
| 325 callback_runner->PostTask( | 331 callback_runner->PostTask( |
| 326 FROM_HERE, base::Bind(callback, base::Passed(&pipe), channel_info)); | 332 FROM_HERE, base::Bind(callback, base::Passed(&pipe), channel_info)); |
| 327 } | 333 } |
| 328 } | 334 } |
| 329 | 335 |
| 330 void ChannelMojo::OnMessagingPipeCreated( | 336 void ChannelMojo::OnMessagingPipeCreated( |
| 331 const CreateMessagingPipeCallback& callback, | 337 const CreateMessagingPipeCallback& callback, |
| 332 mojo::ScopedMessagePipeHandle handle, | 338 mojo::ScopedMessagePipeHandle handle, |
| 333 mojo::embedder::ChannelInfo* channel_info) { | 339 mojo::embedder::ChannelInfo* channel_info) { |
| 334 DCHECK(!channel_info_.get()); | 340 DCHECK(!channel_info_.get()); |
| 335 channel_info_ = scoped_ptr<mojo::embedder::ChannelInfo, ChannelInfoDeleter>( | 341 channel_info_ = scoped_ptr<mojo::embedder::ChannelInfo, ChannelInfoDeleter>( |
| 336 channel_info, ChannelInfoDeleter(io_runner_)); | 342 channel_info, ChannelInfoDeleter(io_runner_)); |
| 337 callback.Run(handle.Pass()); | 343 callback.Run(std::move(handle)); |
| 338 } | 344 } |
| 339 | 345 |
| 340 bool ChannelMojo::Connect() { | 346 bool ChannelMojo::Connect() { |
| 341 DCHECK(!message_reader_); | 347 DCHECK(!message_reader_); |
| 342 return bootstrap_->Connect(); | 348 return bootstrap_->Connect(); |
| 343 } | 349 } |
| 344 | 350 |
| 345 void ChannelMojo::Close() { | 351 void ChannelMojo::Close() { |
| 346 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> to_be_deleted; | 352 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> to_be_deleted; |
| 347 | 353 |
| 348 { | 354 { |
| 349 // |message_reader_| has to be cleared inside the lock, | 355 // |message_reader_| has to be cleared inside the lock, |
| 350 // but the instance has to be deleted outside. | 356 // but the instance has to be deleted outside. |
| 351 base::AutoLock l(lock_); | 357 base::AutoLock l(lock_); |
| 352 to_be_deleted = message_reader_.Pass(); | 358 to_be_deleted = std::move(message_reader_); |
| 353 // We might Close() before we Connect(). | 359 // We might Close() before we Connect(). |
| 354 waiting_connect_ = false; | 360 waiting_connect_ = false; |
| 355 } | 361 } |
| 356 | 362 |
| 357 channel_info_.reset(); | 363 channel_info_.reset(); |
| 358 ipc_support_.reset(); | 364 ipc_support_.reset(); |
| 359 to_be_deleted.reset(); | 365 to_be_deleted.reset(); |
| 360 } | 366 } |
| 361 | 367 |
| 362 void ChannelMojo::OnBootstrapError() { | 368 void ChannelMojo::OnBootstrapError() { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 374 ptr->CloseWithErrorIfPending(); | 380 ptr->CloseWithErrorIfPending(); |
| 375 delete ptr; | 381 delete ptr; |
| 376 } | 382 } |
| 377 }; | 383 }; |
| 378 | 384 |
| 379 } // namespace | 385 } // namespace |
| 380 | 386 |
| 381 void ChannelMojo::InitMessageReader(mojo::ScopedMessagePipeHandle pipe, | 387 void ChannelMojo::InitMessageReader(mojo::ScopedMessagePipeHandle pipe, |
| 382 int32_t peer_pid) { | 388 int32_t peer_pid) { |
| 383 scoped_ptr<internal::MessagePipeReader, ClosingDeleter> reader( | 389 scoped_ptr<internal::MessagePipeReader, ClosingDeleter> reader( |
| 384 new internal::MessagePipeReader(pipe.Pass(), this)); | 390 new internal::MessagePipeReader(std::move(pipe), this)); |
| 385 | 391 |
| 386 { | 392 { |
| 387 base::AutoLock l(lock_); | 393 base::AutoLock l(lock_); |
| 388 for (size_t i = 0; i < pending_messages_.size(); ++i) { | 394 for (size_t i = 0; i < pending_messages_.size(); ++i) { |
| 389 bool sent = reader->Send(make_scoped_ptr(pending_messages_[i])); | 395 bool sent = reader->Send(make_scoped_ptr(pending_messages_[i])); |
| 390 pending_messages_[i] = nullptr; | 396 pending_messages_[i] = nullptr; |
| 391 if (!sent) { | 397 if (!sent) { |
| 392 // OnChannelError() is notified through ClosingDeleter. | 398 // OnChannelError() is notified through ClosingDeleter. |
| 393 pending_messages_.clear(); | 399 pending_messages_.clear(); |
| 394 LOG(ERROR) << "Failed to flush pending messages"; | 400 LOG(ERROR) << "Failed to flush pending messages"; |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 if (!ok) { | 544 if (!ok) { |
| 539 LOG(ERROR) << "Failed to add new Mojo handle."; | 545 LOG(ERROR) << "Failed to add new Mojo handle."; |
| 540 return MOJO_RESULT_UNKNOWN; | 546 return MOJO_RESULT_UNKNOWN; |
| 541 } | 547 } |
| 542 } | 548 } |
| 543 | 549 |
| 544 return MOJO_RESULT_OK; | 550 return MOJO_RESULT_OK; |
| 545 } | 551 } |
| 546 | 552 |
| 547 } // namespace IPC | 553 } // namespace IPC |
| OLD | NEW |