| 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/ipc_mojo_bootstrap.h" | 5 #include "ipc/ipc_mojo_bootstrap.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| 11 #include <utility> | 11 #include <utility> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/atomicops.h" |
| 14 #include "base/callback.h" | 15 #include "base/callback.h" |
| 15 #include "base/logging.h" | 16 #include "base/logging.h" |
| 16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
| 18 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 19 #include "base/stl_util.h" | 20 #include "base/stl_util.h" |
| 20 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
| 21 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
| 22 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 23 #include "ipc/ipc_message_utils.h" | 24 #include "ipc/ipc_message_utils.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 control_message_proxy_(&control_message_proxy_thunk_) { | 59 control_message_proxy_(&control_message_proxy_thunk_) { |
| 59 thread_checker_.DetachFromThread(); | 60 thread_checker_.DetachFromThread(); |
| 60 control_message_handler_.SetDescription( | 61 control_message_handler_.SetDescription( |
| 61 "IPC::mojom::Bootstrap [master] PipeControlMessageHandler"); | 62 "IPC::mojom::Bootstrap [master] PipeControlMessageHandler"); |
| 62 } | 63 } |
| 63 | 64 |
| 64 void Bind(mojo::ScopedMessagePipeHandle handle) { | 65 void Bind(mojo::ScopedMessagePipeHandle handle) { |
| 65 DCHECK(thread_checker_.CalledOnValidThread()); | 66 DCHECK(thread_checker_.CalledOnValidThread()); |
| 66 DCHECK(task_runner_->BelongsToCurrentThread()); | 67 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 67 thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 68 thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 69 base::subtle::Release_Store(&is_thread_task_runner_set_, 1); |
| 70 |
| 68 connector_.reset(new mojo::Connector( | 71 connector_.reset(new mojo::Connector( |
| 69 std::move(handle), mojo::Connector::SINGLE_THREADED_SEND, | 72 std::move(handle), mojo::Connector::SINGLE_THREADED_SEND, |
| 70 task_runner_)); | 73 task_runner_)); |
| 71 connector_->set_incoming_receiver(&header_validator_); | 74 connector_->set_incoming_receiver(&header_validator_); |
| 72 connector_->set_connection_error_handler( | 75 connector_->set_connection_error_handler( |
| 73 base::Bind(&ChannelAssociatedGroupController::OnPipeError, | 76 base::Bind(&ChannelAssociatedGroupController::OnPipeError, |
| 74 base::Unretained(this))); | 77 base::Unretained(this))); |
| 75 | 78 |
| 76 std::vector<std::unique_ptr<mojo::Message>> outgoing_messages; | 79 std::vector<std::unique_ptr<mojo::Message>> outgoing_messages; |
| 77 std::swap(outgoing_messages, outgoing_messages_); | 80 std::swap(outgoing_messages, outgoing_messages_); |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 } | 355 } |
| 353 | 356 |
| 354 DCHECK(endpoints_.empty()); | 357 DCHECK(endpoints_.empty()); |
| 355 } | 358 } |
| 356 | 359 |
| 357 bool IsRunningOnIPCThread() { | 360 bool IsRunningOnIPCThread() { |
| 358 // |task_runner_| is always non-null but may incorrectly report that | 361 // |task_runner_| is always non-null but may incorrectly report that |
| 359 // BelongsToCurrentThread() == false during shutdown. By the time shutdown | 362 // BelongsToCurrentThread() == false during shutdown. By the time shutdown |
| 360 // occurs, |thread_task_runner_| will be non-null and is guaranteed to run | 363 // occurs, |thread_task_runner_| will be non-null and is guaranteed to run |
| 361 // tasks on the same thread as |task_runner_|. | 364 // tasks on the same thread as |task_runner_|. |
| 362 return task_runner_->BelongsToCurrentThread() || | 365 base::subtle::Atomic32 has_thread_task_runner = |
| 363 (thread_task_runner_ && thread_task_runner_->BelongsToCurrentThread()); | 366 base::subtle::Acquire_Load(&is_thread_task_runner_set_); |
| 367 if (has_thread_task_runner) |
| 368 return thread_task_runner_->BelongsToCurrentThread(); |
| 369 return task_runner_->BelongsToCurrentThread(); |
| 364 } | 370 } |
| 365 | 371 |
| 366 bool SendMessage(mojo::Message* message) { | 372 bool SendMessage(mojo::Message* message) { |
| 367 if (IsRunningOnIPCThread()) { | 373 if (IsRunningOnIPCThread()) { |
| 368 DCHECK(thread_checker_.CalledOnValidThread()); | 374 DCHECK(thread_checker_.CalledOnValidThread()); |
| 369 if (!connector_) { | 375 if (!connector_) { |
| 370 // Pipe may not be bound yet, so we queue the message. | 376 // Pipe may not be bound yet, so we queue the message. |
| 371 std::unique_ptr<mojo::Message> queued_message(new mojo::Message); | 377 std::unique_ptr<mojo::Message> queued_message(new mojo::Message); |
| 372 message->MoveTo(queued_message.get()); | 378 message->MoveTo(queued_message.get()); |
| 373 outgoing_messages_.emplace_back(std::move(queued_message)); | 379 outgoing_messages_.emplace_back(std::move(queued_message)); |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 604 base::ThreadChecker thread_checker_; | 610 base::ThreadChecker thread_checker_; |
| 605 | 611 |
| 606 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 612 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 607 | 613 |
| 608 // A TaskRunner that runs tasks on the same thread as |task_runner_| but which | 614 // A TaskRunner that runs tasks on the same thread as |task_runner_| but which |
| 609 // is used exclusively to do thread safety checking. This is an unfortunate | 615 // is used exclusively to do thread safety checking. This is an unfortunate |
| 610 // consequence of bad interaction between some TaskRunner implementations and | 616 // consequence of bad interaction between some TaskRunner implementations and |
| 611 // MessageLoop destruction which may cause the user-provided |task_runner_| to | 617 // MessageLoop destruction which may cause the user-provided |task_runner_| to |
| 612 // incorrectly report that BelongsToCurrentThread() == false during shutdown. | 618 // incorrectly report that BelongsToCurrentThread() == false during shutdown. |
| 613 scoped_refptr<base::SingleThreadTaskRunner> thread_task_runner_; | 619 scoped_refptr<base::SingleThreadTaskRunner> thread_task_runner_; |
| 620 base::subtle::Atomic32 is_thread_task_runner_set_ = 0; |
| 614 | 621 |
| 615 scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_; | 622 scoped_refptr<base::SingleThreadTaskRunner> proxy_task_runner_; |
| 616 const bool set_interface_id_namespace_bit_; | 623 const bool set_interface_id_namespace_bit_; |
| 617 std::unique_ptr<mojo::Connector> connector_; | 624 std::unique_ptr<mojo::Connector> connector_; |
| 618 mojo::MessageHeaderValidator header_validator_; | 625 mojo::MessageHeaderValidator header_validator_; |
| 619 mojo::PipeControlMessageHandler control_message_handler_; | 626 mojo::PipeControlMessageHandler control_message_handler_; |
| 620 ControlMessageProxyThunk control_message_proxy_thunk_; | 627 ControlMessageProxyThunk control_message_proxy_thunk_; |
| 621 mojo::PipeControlMessageProxy control_message_proxy_; | 628 mojo::PipeControlMessageProxy control_message_proxy_; |
| 622 | 629 |
| 623 // Outgoing messages that were sent before this controller was bound to a | 630 // Outgoing messages that were sent before this controller was bound to a |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 686 Channel::Mode mode, | 693 Channel::Mode mode, |
| 687 Delegate* delegate, | 694 Delegate* delegate, |
| 688 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { | 695 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
| 689 return base::MakeUnique<MojoBootstrapImpl>( | 696 return base::MakeUnique<MojoBootstrapImpl>( |
| 690 std::move(handle), delegate, | 697 std::move(handle), delegate, |
| 691 new ChannelAssociatedGroupController(mode == Channel::MODE_SERVER, | 698 new ChannelAssociatedGroupController(mode == Channel::MODE_SERVER, |
| 692 ipc_task_runner)); | 699 ipc_task_runner)); |
| 693 } | 700 } |
| 694 | 701 |
| 695 } // namespace IPC | 702 } // namespace IPC |
| OLD | NEW |