| 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 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 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" |
| 21 #include "ipc/ipc_message_attachment_set.h" | 21 #include "ipc/ipc_message_attachment_set.h" |
| 22 #include "ipc/ipc_message_macros.h" | 22 #include "ipc/ipc_message_macros.h" |
| 23 #include "ipc/mojo/client_channel.mojom.h" | 23 #include "ipc/mojo/client_channel.mojom.h" |
| 24 #include "ipc/mojo/ipc_mojo_bootstrap.h" | 24 #include "ipc/mojo/ipc_mojo_bootstrap.h" |
| 25 #include "ipc/mojo/ipc_mojo_handle_attachment.h" | 25 #include "ipc/mojo/ipc_mojo_handle_attachment.h" |
| 26 #include "mojo/edk/embedder/embedder.h" |
| 26 #include "mojo/public/cpp/bindings/binding.h" | 27 #include "mojo/public/cpp/bindings/binding.h" |
| 27 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | |
| 28 | 28 |
| 29 #if defined(OS_POSIX) && !defined(OS_NACL) | 29 #if defined(OS_POSIX) && !defined(OS_NACL) |
| 30 #include "ipc/ipc_platform_file_attachment_posix.h" | 30 #include "ipc/ipc_platform_file_attachment_posix.h" |
| 31 #endif | 31 #endif |
| 32 | 32 |
| 33 namespace IPC { | 33 namespace IPC { |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 // TODO(jam): do more tests on using channel on same thread if it supports it ( | 37 // TODO(jam): do more tests on using channel on same thread if it supports it ( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 66 ClientChannelMojo(scoped_refptr<base::TaskRunner> io_runner, | 66 ClientChannelMojo(scoped_refptr<base::TaskRunner> io_runner, |
| 67 const ChannelHandle& handle, | 67 const ChannelHandle& handle, |
| 68 Listener* listener) | 68 Listener* listener) |
| 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::edk::ScopedPlatformHandle handle, |
| 77 int32_t peer_pid) override { | 77 int32_t peer_pid) override { |
| 78 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { | 78 mojo::edk::CreateMessagePipe( |
| 79 InitMessageReader( | |
| 80 mojo::embedder::CreateChannel( | |
| 81 std::move(handle), | |
| 82 base::Callback<void(mojo::embedder::ChannelInfo*)>(), | |
| 83 scoped_refptr<base::TaskRunner>()), | |
| 84 peer_pid); | |
| 85 return; | |
| 86 } | |
| 87 CreateMessagingPipe( | |
| 88 std::move(handle), | 79 std::move(handle), |
| 89 base::Bind(&ClientChannelMojo::BindPipe, weak_factory_.GetWeakPtr())); | 80 base::Bind(&ClientChannelMojo::BindPipe, weak_factory_.GetWeakPtr())); |
| 90 } | 81 } |
| 91 | 82 |
| 92 // ClientChannel implementation | 83 // ClientChannel implementation |
| 93 void Init( | 84 void Init( |
| 94 mojo::ScopedMessagePipeHandle pipe, | 85 mojo::ScopedMessagePipeHandle pipe, |
| 95 int32_t peer_pid, | 86 int32_t peer_pid, |
| 96 const mojo::Callback<void(int32_t)>& callback) override { | 87 const mojo::Callback<void(int32_t)>& callback) override { |
| 97 InitMessageReader(std::move(pipe), static_cast<base::ProcessId>(peer_pid)); | 88 InitMessageReader(std::move(pipe), static_cast<base::ProcessId>(peer_pid)); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 120 const ChannelHandle& handle, | 111 const ChannelHandle& handle, |
| 121 Listener* listener) | 112 Listener* listener) |
| 122 : ChannelMojo(io_runner, handle, Channel::MODE_SERVER, listener), | 113 : ChannelMojo(io_runner, handle, Channel::MODE_SERVER, listener), |
| 123 weak_factory_(this) { | 114 weak_factory_(this) { |
| 124 } | 115 } |
| 125 ~ServerChannelMojo() override { | 116 ~ServerChannelMojo() override { |
| 126 Close(); | 117 Close(); |
| 127 } | 118 } |
| 128 | 119 |
| 129 // MojoBootstrap::Delegate implementation | 120 // MojoBootstrap::Delegate implementation |
| 130 void OnPipeAvailable(mojo::embedder::ScopedPlatformHandle handle, | 121 void OnPipeAvailable(mojo::edk::ScopedPlatformHandle handle, |
| 131 int32_t peer_pid) override { | 122 int32_t peer_pid) override { |
| 132 if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { | |
| 133 message_pipe_ = mojo::embedder::CreateChannel( | |
| 134 std::move(handle), | |
| 135 base::Callback<void(mojo::embedder::ChannelInfo*)>(), | |
| 136 scoped_refptr<base::TaskRunner>()); | |
| 137 if (!message_pipe_.is_valid()) { | |
| 138 LOG(WARNING) << "mojo::CreateMessagePipe failed: "; | |
| 139 listener()->OnChannelError(); | |
| 140 return; | |
| 141 } | |
| 142 InitMessageReader(std::move(message_pipe_), peer_pid); | |
| 143 return; | |
| 144 } | |
| 145 | |
| 146 mojo::ScopedMessagePipeHandle peer; | 123 mojo::ScopedMessagePipeHandle peer; |
| 147 MojoResult create_result = | 124 MojoResult create_result = |
| 148 mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer); | 125 mojo::CreateMessagePipe(nullptr, &message_pipe_, &peer); |
| 149 if (create_result != MOJO_RESULT_OK) { | 126 if (create_result != MOJO_RESULT_OK) { |
| 150 LOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result; | 127 LOG(WARNING) << "mojo::CreateMessagePipe failed: " << create_result; |
| 151 listener()->OnChannelError(); | 128 listener()->OnChannelError(); |
| 152 return; | 129 return; |
| 153 } | 130 } |
| 154 CreateMessagingPipe( | 131 mojo::edk::CreateMessagePipe( |
| 155 std::move(handle), | 132 std::move(handle), |
| 156 base::Bind(&ServerChannelMojo::InitClientChannel, | 133 base::Bind(&ServerChannelMojo::InitClientChannel, |
| 157 weak_factory_.GetWeakPtr(), base::Passed(&peer))); | 134 weak_factory_.GetWeakPtr(), base::Passed(&peer))); |
| 158 } | 135 } |
| 159 // Channel override | 136 // Channel override |
| 160 void Close() override { | 137 void Close() override { |
| 161 client_channel_.reset(); | 138 client_channel_.reset(); |
| 162 message_pipe_.reset(); | 139 message_pipe_.reset(); |
| 163 ChannelMojo::Close(); | 140 ChannelMojo::Close(); |
| 164 } | 141 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 return attachment->Owns() ? base::ScopedFD(attachment->TakePlatformFile()) | 175 return attachment->Owns() ? base::ScopedFD(attachment->TakePlatformFile()) |
| 199 : base::ScopedFD(dup(attachment->file())); | 176 : base::ScopedFD(dup(attachment->file())); |
| 200 } | 177 } |
| 201 | 178 |
| 202 #endif | 179 #endif |
| 203 | 180 |
| 204 } // namespace | 181 } // namespace |
| 205 | 182 |
| 206 //------------------------------------------------------------------------------ | 183 //------------------------------------------------------------------------------ |
| 207 | 184 |
| 208 ChannelMojo::ChannelInfoDeleter::ChannelInfoDeleter( | |
| 209 scoped_refptr<base::TaskRunner> io_runner) | |
| 210 : io_runner(io_runner) { | |
| 211 } | |
| 212 | |
| 213 ChannelMojo::ChannelInfoDeleter::~ChannelInfoDeleter() { | |
| 214 } | |
| 215 | |
| 216 void ChannelMojo::ChannelInfoDeleter::operator()( | |
| 217 mojo::embedder::ChannelInfo* ptr) const { | |
| 218 if (base::ThreadTaskRunnerHandle::Get() == io_runner) { | |
| 219 mojo::embedder::DestroyChannelOnIOThread(ptr); | |
| 220 } else { | |
| 221 io_runner->PostTask( | |
| 222 FROM_HERE, base::Bind(&mojo::embedder::DestroyChannelOnIOThread, ptr)); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 //------------------------------------------------------------------------------ | |
| 227 | |
| 228 // static | 185 // static |
| 229 bool ChannelMojo::ShouldBeUsed() { | 186 bool ChannelMojo::ShouldBeUsed() { |
| 230 // TODO(rockot): Investigate performance bottlenecks and hopefully reenable | 187 // TODO(rockot): Investigate performance bottlenecks and hopefully reenable |
| 231 // this at some point. http://crbug.com/500019 | 188 // this at some point. http://crbug.com/500019 |
| 232 return false; | 189 return false; |
| 233 } | 190 } |
| 234 | 191 |
| 235 // static | 192 // static |
| 236 scoped_ptr<ChannelMojo> ChannelMojo::Create( | 193 scoped_ptr<ChannelMojo> ChannelMojo::Create( |
| 237 scoped_refptr<base::TaskRunner> io_runner, | 194 scoped_refptr<base::TaskRunner> io_runner, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 267 new MojoChannelFactory(io_runner, channel_handle, Channel::MODE_CLIENT)); | 224 new MojoChannelFactory(io_runner, channel_handle, Channel::MODE_CLIENT)); |
| 268 } | 225 } |
| 269 | 226 |
| 270 ChannelMojo::ChannelMojo(scoped_refptr<base::TaskRunner> io_runner, | 227 ChannelMojo::ChannelMojo(scoped_refptr<base::TaskRunner> io_runner, |
| 271 const ChannelHandle& handle, | 228 const ChannelHandle& handle, |
| 272 Mode mode, | 229 Mode mode, |
| 273 Listener* listener) | 230 Listener* listener) |
| 274 : listener_(listener), | 231 : listener_(listener), |
| 275 peer_pid_(base::kNullProcessId), | 232 peer_pid_(base::kNullProcessId), |
| 276 io_runner_(io_runner), | 233 io_runner_(io_runner), |
| 277 channel_info_(nullptr, ChannelInfoDeleter(nullptr)), | |
| 278 waiting_connect_(true), | 234 waiting_connect_(true), |
| 279 weak_factory_(this) { | 235 weak_factory_(this) { |
| 280 // Create MojoBootstrap after all members are set as it touches | 236 // Create MojoBootstrap after all members are set as it touches |
| 281 // ChannelMojo from a different thread. | 237 // ChannelMojo from a different thread. |
| 282 bootstrap_ = MojoBootstrap::Create(handle, mode, this); | 238 bootstrap_ = MojoBootstrap::Create(handle, mode, this); |
| 283 if (!g_use_channel_on_io_thread_only || | 239 if (!g_use_channel_on_io_thread_only || |
| 284 io_runner == base::MessageLoop::current()->task_runner()) { | 240 io_runner == base::MessageLoop::current()->task_runner()) { |
| 285 InitOnIOThread(); | 241 InitOnIOThread(); |
| 286 } else { | 242 } else { |
| 287 io_runner->PostTask(FROM_HERE, base::Bind(&ChannelMojo::InitOnIOThread, | 243 io_runner->PostTask(FROM_HERE, base::Bind(&ChannelMojo::InitOnIOThread, |
| 288 base::Unretained(this))); | 244 base::Unretained(this))); |
| 289 } | 245 } |
| 290 } | 246 } |
| 291 | 247 |
| 292 ChannelMojo::~ChannelMojo() { | 248 ChannelMojo::~ChannelMojo() { |
| 293 Close(); | 249 Close(); |
| 294 } | 250 } |
| 295 | 251 |
| 296 void ChannelMojo::InitOnIOThread() { | 252 void ChannelMojo::InitOnIOThread() { |
| 297 ipc_support_.reset( | 253 ipc_support_.reset( |
| 298 new ScopedIPCSupport(base::MessageLoop::current()->task_runner())); | 254 new ScopedIPCSupport(base::MessageLoop::current()->task_runner())); |
| 299 } | 255 } |
| 300 | 256 |
| 301 void ChannelMojo::CreateMessagingPipe( | |
| 302 mojo::embedder::ScopedPlatformHandle handle, | |
| 303 const CreateMessagingPipeCallback& callback) { | |
| 304 auto return_callback = base::Bind(&ChannelMojo::OnMessagingPipeCreated, | |
| 305 weak_factory_.GetWeakPtr(), callback); | |
| 306 if (!g_use_channel_on_io_thread_only || | |
| 307 base::ThreadTaskRunnerHandle::Get() == io_runner_) { | |
| 308 CreateMessagingPipeOnIOThread(std::move(handle), | |
| 309 base::ThreadTaskRunnerHandle::Get(), | |
| 310 return_callback); | |
| 311 } else { | |
| 312 io_runner_->PostTask( | |
| 313 FROM_HERE, | |
| 314 base::Bind(&ChannelMojo::CreateMessagingPipeOnIOThread, | |
| 315 base::Passed(&handle), base::ThreadTaskRunnerHandle::Get(), | |
| 316 return_callback)); | |
| 317 } | |
| 318 } | |
| 319 | |
| 320 // static | |
| 321 void ChannelMojo::CreateMessagingPipeOnIOThread( | |
| 322 mojo::embedder::ScopedPlatformHandle handle, | |
| 323 scoped_refptr<base::TaskRunner> callback_runner, | |
| 324 const CreateMessagingPipeOnIOThreadCallback& callback) { | |
| 325 mojo::embedder::ChannelInfo* channel_info; | |
| 326 mojo::ScopedMessagePipeHandle pipe = | |
| 327 mojo::embedder::CreateChannelOnIOThread(std::move(handle), &channel_info); | |
| 328 if (base::ThreadTaskRunnerHandle::Get() == callback_runner) { | |
| 329 callback.Run(std::move(pipe), channel_info); | |
| 330 } else { | |
| 331 callback_runner->PostTask( | |
| 332 FROM_HERE, base::Bind(callback, base::Passed(&pipe), channel_info)); | |
| 333 } | |
| 334 } | |
| 335 | |
| 336 void ChannelMojo::OnMessagingPipeCreated( | |
| 337 const CreateMessagingPipeCallback& callback, | |
| 338 mojo::ScopedMessagePipeHandle handle, | |
| 339 mojo::embedder::ChannelInfo* channel_info) { | |
| 340 DCHECK(!channel_info_.get()); | |
| 341 channel_info_ = scoped_ptr<mojo::embedder::ChannelInfo, ChannelInfoDeleter>( | |
| 342 channel_info, ChannelInfoDeleter(io_runner_)); | |
| 343 callback.Run(std::move(handle)); | |
| 344 } | |
| 345 | |
| 346 bool ChannelMojo::Connect() { | 257 bool ChannelMojo::Connect() { |
| 347 DCHECK(!message_reader_); | 258 DCHECK(!message_reader_); |
| 348 return bootstrap_->Connect(); | 259 return bootstrap_->Connect(); |
| 349 } | 260 } |
| 350 | 261 |
| 351 void ChannelMojo::Close() { | 262 void ChannelMojo::Close() { |
| 352 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> to_be_deleted; | 263 scoped_ptr<internal::MessagePipeReader, ReaderDeleter> to_be_deleted; |
| 353 | 264 |
| 354 { | 265 { |
| 355 // |message_reader_| has to be cleared inside the lock, | 266 // |message_reader_| has to be cleared inside the lock, |
| 356 // but the instance has to be deleted outside. | 267 // but the instance has to be deleted outside. |
| 357 base::AutoLock l(lock_); | 268 base::AutoLock l(lock_); |
| 358 to_be_deleted = std::move(message_reader_); | 269 to_be_deleted = std::move(message_reader_); |
| 359 // We might Close() before we Connect(). | 270 // We might Close() before we Connect(). |
| 360 waiting_connect_ = false; | 271 waiting_connect_ = false; |
| 361 } | 272 } |
| 362 | 273 |
| 363 channel_info_.reset(); | |
| 364 ipc_support_.reset(); | 274 ipc_support_.reset(); |
| 365 to_be_deleted.reset(); | 275 to_be_deleted.reset(); |
| 366 } | 276 } |
| 367 | 277 |
| 368 void ChannelMojo::OnBootstrapError() { | 278 void ChannelMojo::OnBootstrapError() { |
| 369 listener_->OnChannelError(); | 279 listener_->OnChannelError(); |
| 370 } | 280 } |
| 371 | 281 |
| 372 namespace { | 282 namespace { |
| 373 | 283 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 base::ScopedFD file = | 398 base::ScopedFD file = |
| 489 TakeOrDupFile(static_cast<IPC::internal::PlatformFileAttachment*>( | 399 TakeOrDupFile(static_cast<IPC::internal::PlatformFileAttachment*>( |
| 490 attachment.get())); | 400 attachment.get())); |
| 491 if (!file.is_valid()) { | 401 if (!file.is_valid()) { |
| 492 DPLOG(WARNING) << "Failed to dup FD to transmit."; | 402 DPLOG(WARNING) << "Failed to dup FD to transmit."; |
| 493 set->CommitAllDescriptors(); | 403 set->CommitAllDescriptors(); |
| 494 return MOJO_RESULT_UNKNOWN; | 404 return MOJO_RESULT_UNKNOWN; |
| 495 } | 405 } |
| 496 | 406 |
| 497 MojoHandle wrapped_handle; | 407 MojoHandle wrapped_handle; |
| 498 MojoResult wrap_result = mojo::embedder::CreatePlatformHandleWrapper( | 408 MojoResult wrap_result = mojo::edk::CreatePlatformHandleWrapper( |
| 499 mojo::embedder::ScopedPlatformHandle( | 409 mojo::edk::ScopedPlatformHandle( |
| 500 mojo::embedder::PlatformHandle(file.release())), | 410 mojo::edk::PlatformHandle(file.release())), |
| 501 &wrapped_handle); | 411 &wrapped_handle); |
| 502 if (MOJO_RESULT_OK != wrap_result) { | 412 if (MOJO_RESULT_OK != wrap_result) { |
| 503 LOG(WARNING) << "Pipe failed to wrap handles. Closing: " | 413 LOG(WARNING) << "Pipe failed to wrap handles. Closing: " |
| 504 << wrap_result; | 414 << wrap_result; |
| 505 set->CommitAllDescriptors(); | 415 set->CommitAllDescriptors(); |
| 506 return wrap_result; | 416 return wrap_result; |
| 507 } | 417 } |
| 508 | 418 |
| 509 handles->push_back(wrapped_handle); | 419 handles->push_back(wrapped_handle); |
| 510 } | 420 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 if (!ok) { | 454 if (!ok) { |
| 545 LOG(ERROR) << "Failed to add new Mojo handle."; | 455 LOG(ERROR) << "Failed to add new Mojo handle."; |
| 546 return MOJO_RESULT_UNKNOWN; | 456 return MOJO_RESULT_UNKNOWN; |
| 547 } | 457 } |
| 548 } | 458 } |
| 549 | 459 |
| 550 return MOJO_RESULT_OK; | 460 return MOJO_RESULT_OK; |
| 551 } | 461 } |
| 552 | 462 |
| 553 } // namespace IPC | 463 } // namespace IPC |
| OLD | NEW |