| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "mojo/edk/system/node_controller.h" | 5 #include "mojo/edk/system/node_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
| 16 #include "base/process/process_handle.h" | 16 #include "base/process/process_handle.h" |
| 17 #include "base/rand_util.h" | 17 #include "base/rand_util.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "base/timer/elapsed_timer.h" | 19 #include "base/timer/elapsed_timer.h" |
| 20 #include "mojo/edk/embedder/embedder_internal.h" | 20 #include "mojo/edk/embedder/embedder_internal.h" |
| 21 #include "mojo/edk/embedder/platform_channel_pair.h" | 21 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 22 #include "mojo/edk/system/broker.h" | 22 #include "mojo/edk/system/broker.h" |
| 23 #include "mojo/edk/system/broker_host.h" | 23 #include "mojo/edk/system/broker_host.h" |
| 24 #include "mojo/edk/system/core.h" | 24 #include "mojo/edk/system/core.h" |
| 25 #include "mojo/edk/system/ports_message.h" | 25 #include "mojo/edk/system/ports_message.h" |
| 26 #include "mojo/edk/system/request_context.h" |
| 26 | 27 |
| 27 #if defined(OS_MACOSX) && !defined(OS_IOS) | 28 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 28 #include "mojo/edk/system/mach_port_relay.h" | 29 #include "mojo/edk/system/mach_port_relay.h" |
| 29 #endif | 30 #endif |
| 30 | 31 |
| 31 #if !defined(OS_NACL) | 32 #if !defined(OS_NACL) |
| 32 #include "crypto/random.h" | 33 #include "crypto/random.h" |
| 33 #endif | 34 #endif |
| 34 | 35 |
| 35 namespace mojo { | 36 namespace mojo { |
| (...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 messages_lock_.Release(); | 640 messages_lock_.Release(); |
| 640 | 641 |
| 641 while (!messages.empty()) { | 642 while (!messages.empty()) { |
| 642 node_->AcceptMessage(std::move(messages.front())); | 643 node_->AcceptMessage(std::move(messages.front())); |
| 643 messages.pop(); | 644 messages.pop(); |
| 644 } | 645 } |
| 645 } | 646 } |
| 646 AttemptShutdownIfRequested(); | 647 AttemptShutdownIfRequested(); |
| 647 } | 648 } |
| 648 | 649 |
| 650 void NodeController::ProcessIncomingMessages() { |
| 651 RequestContext request_context(RequestContext::Source::SYSTEM); |
| 652 AcceptIncomingMessages(); |
| 653 } |
| 654 |
| 649 void NodeController::DropAllPeers() { | 655 void NodeController::DropAllPeers() { |
| 650 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 656 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 651 | 657 |
| 652 std::vector<scoped_refptr<NodeChannel>> all_peers; | 658 std::vector<scoped_refptr<NodeChannel>> all_peers; |
| 653 { | 659 { |
| 654 base::AutoLock lock(parent_lock_); | 660 base::AutoLock lock(parent_lock_); |
| 655 if (bootstrap_parent_channel_) { | 661 if (bootstrap_parent_channel_) { |
| 656 // |bootstrap_parent_channel_| isn't null'd here becuase we rely on its | 662 // |bootstrap_parent_channel_| isn't null'd here becuase we rely on its |
| 657 // existence to determine whether or not this is the root node. Once | 663 // existence to determine whether or not this is the root node. Once |
| 658 // bootstrap_parent_channel_->ShutDown() has been called, | 664 // bootstrap_parent_channel_->ShutDown() has been called, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 686 } | 692 } |
| 687 | 693 |
| 688 void NodeController::AllocMessage(size_t num_header_bytes, | 694 void NodeController::AllocMessage(size_t num_header_bytes, |
| 689 ports::ScopedMessage* message) { | 695 ports::ScopedMessage* message) { |
| 690 message->reset(new PortsMessage(num_header_bytes, 0, 0, nullptr)); | 696 message->reset(new PortsMessage(num_header_bytes, 0, 0, nullptr)); |
| 691 } | 697 } |
| 692 | 698 |
| 693 void NodeController::ForwardMessage(const ports::NodeName& node, | 699 void NodeController::ForwardMessage(const ports::NodeName& node, |
| 694 ports::ScopedMessage message) { | 700 ports::ScopedMessage message) { |
| 695 DCHECK(message); | 701 DCHECK(message); |
| 702 bool schedule_pump_task = false; |
| 696 if (node == name_) { | 703 if (node == name_) { |
| 697 // NOTE: We need to avoid re-entering the Node instance within | 704 // NOTE: We need to avoid re-entering the Node instance within |
| 698 // ForwardMessage. Because ForwardMessage is only ever called | 705 // ForwardMessage. Because ForwardMessage is only ever called |
| 699 // (synchronously) in response to Node's ClosePort, SendMessage, or | 706 // (synchronously) in response to Node's ClosePort, SendMessage, or |
| 700 // AcceptMessage, we flush the queue after calling any of those methods. | 707 // AcceptMessage, we flush the queue after calling any of those methods. |
| 701 base::AutoLock lock(messages_lock_); | 708 base::AutoLock lock(messages_lock_); |
| 709 schedule_pump_task = incoming_messages_.empty(); |
| 702 incoming_messages_.emplace(std::move(message)); | 710 incoming_messages_.emplace(std::move(message)); |
| 703 incoming_messages_flag_.Set(true); | 711 incoming_messages_flag_.Set(true); |
| 704 } else { | 712 } else { |
| 705 SendPeerMessage(node, std::move(message)); | 713 SendPeerMessage(node, std::move(message)); |
| 706 } | 714 } |
| 715 |
| 716 // |io_task_runner_| may be null in tests or processes that don't require |
| 717 // multi-process Mojo. |
| 718 if (schedule_pump_task && io_task_runner_) { |
| 719 // Normally, the queue is processed after the action that added the local |
| 720 // message is done (i.e. SendMessage, ClosePort, etc). However, it's also |
| 721 // possible for a local message to be added as a result of a remote message, |
| 722 // and OnChannelMessage() doesn't process this queue (although |
| 723 // OnPortsMessage() does). There may also be other code paths, now or added |
| 724 // in the future, which cause local messages to be added but don't process |
| 725 // this message queue. |
| 726 // |
| 727 // Instead of adding a call to AcceptIncomingMessages() on every possible |
| 728 // code path, post a task to the IO thread to process the queue. If the |
| 729 // current call stack processes the queue, this may end up doing nothing. |
| 730 io_task_runner_->PostTask( |
| 731 FROM_HERE, |
| 732 base::Bind(&NodeController::ProcessIncomingMessages, |
| 733 base::Unretained(this))); |
| 734 } |
| 707 } | 735 } |
| 708 | 736 |
| 709 void NodeController::BroadcastMessage(ports::ScopedMessage message) { | 737 void NodeController::BroadcastMessage(ports::ScopedMessage message) { |
| 710 CHECK_EQ(message->num_ports(), 0u); | 738 CHECK_EQ(message->num_ports(), 0u); |
| 711 Channel::MessagePtr channel_message = | 739 Channel::MessagePtr channel_message = |
| 712 static_cast<PortsMessage*>(message.get())->TakeChannelMessage(); | 740 static_cast<PortsMessage*>(message.get())->TakeChannelMessage(); |
| 713 CHECK(!channel_message->has_handles()); | 741 CHECK(!channel_message->has_handles()); |
| 714 | 742 |
| 715 scoped_refptr<NodeChannel> broker = GetBrokerChannel(); | 743 scoped_refptr<NodeChannel> broker = GetBrokerChannel(); |
| 716 if (broker) | 744 if (broker) |
| (...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1203 shutdown_callback_flag_.Set(false); | 1231 shutdown_callback_flag_.Set(false); |
| 1204 } | 1232 } |
| 1205 | 1233 |
| 1206 DCHECK(!callback.is_null()); | 1234 DCHECK(!callback.is_null()); |
| 1207 | 1235 |
| 1208 callback.Run(); | 1236 callback.Run(); |
| 1209 } | 1237 } |
| 1210 | 1238 |
| 1211 } // namespace edk | 1239 } // namespace edk |
| 1212 } // namespace mojo | 1240 } // namespace mojo |
| OLD | NEW |