Check all BasicPortAllocatorSession methods are called on the network thread

Bug: None
Change-Id: I12e56b2b95ba9822db660f7eac66fc8088988e4f
Reviewed-on: https://webrtc-review.googlesource.com/c/103307
Reviewed-by: Qingsi Wang <qingsi@webrtc.org>
Commit-Queue: Steve Anton <steveanton@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24939}
diff --git a/p2p/client/basicportallocator.cc b/p2p/client/basicportallocator.cc
index 92c8338..c1b1de5 100644
--- a/p2p/client/basicportallocator.cc
+++ b/p2p/client/basicportallocator.cc
@@ -262,7 +262,7 @@
                            ice_pwd,
                            allocator->flags()),
       allocator_(allocator),
-      network_thread_(NULL),
+      network_thread_(rtc::Thread::Current()),
       socket_factory_(allocator->socket_factory()),
       allocation_started_(false),
       network_manager_started_(false),
@@ -274,6 +274,7 @@
 }
 
 BasicPortAllocatorSession::~BasicPortAllocatorSession() {
+  RTC_DCHECK_RUN_ON(network_thread_);
   allocator_->network_manager()->StopUpdating();
   if (network_thread_ != NULL)
     network_thread_->Clear(this);
@@ -296,10 +297,12 @@
 }
 
 BasicPortAllocator* BasicPortAllocatorSession::allocator() {
+  RTC_DCHECK_RUN_ON(network_thread_);
   return allocator_;
 }
 
 void BasicPortAllocatorSession::SetCandidateFilter(uint32_t filter) {
+  RTC_DCHECK_RUN_ON(network_thread_);
   if (filter == candidate_filter_) {
     return;
   }
@@ -323,7 +326,7 @@
 }
 
 void BasicPortAllocatorSession::StartGettingPorts() {
-  network_thread_ = rtc::Thread::Current();
+  RTC_DCHECK_RUN_ON(network_thread_);
   state_ = SessionState::GATHERING;
   if (!socket_factory_) {
     owned_socket_factory_.reset(
@@ -338,7 +341,7 @@
 }
 
 void BasicPortAllocatorSession::StopGettingPorts() {
-  RTC_DCHECK(rtc::Thread::Current() == network_thread_);
+  RTC_DCHECK_RUN_ON(network_thread_);
   ClearGettingPorts();
   // Note: this must be called after ClearGettingPorts because both may set the
   // session state and we should set the state to STOPPED.
@@ -346,7 +349,7 @@
 }
 
 void BasicPortAllocatorSession::ClearGettingPorts() {
-  RTC_DCHECK(rtc::Thread::Current() == network_thread_);
+  RTC_DCHECK_RUN_ON(network_thread_);
   network_thread_->Clear(this, MSG_ALLOCATE);
   for (uint32_t i = 0; i < sequences_.size(); ++i) {
     sequences_[i]->Stop();
@@ -356,18 +359,23 @@
 }
 
 bool BasicPortAllocatorSession::IsGettingPorts() {
+  RTC_DCHECK_RUN_ON(network_thread_);
   return state_ == SessionState::GATHERING;
 }
 
 bool BasicPortAllocatorSession::IsCleared() const {
+  RTC_DCHECK_RUN_ON(network_thread_);
   return state_ == SessionState::CLEARED;
 }
 
 bool BasicPortAllocatorSession::IsStopped() const {
+  RTC_DCHECK_RUN_ON(network_thread_);
   return state_ == SessionState::STOPPED;
 }
 
 std::vector<rtc::Network*> BasicPortAllocatorSession::GetFailedNetworks() {
+  RTC_DCHECK_RUN_ON(network_thread_);
+
   std::vector<rtc::Network*> networks = GetNetworks();
 
   // A network interface may have both IPv4 and IPv6 networks. Only if
@@ -394,6 +402,8 @@
 }
 
 void BasicPortAllocatorSession::RegatherOnFailedNetworks() {
+  RTC_DCHECK_RUN_ON(network_thread_);
+
   // Find the list of networks that have no connection.
   std::vector<rtc::Network*> failed_networks = GetFailedNetworks();
   if (failed_networks.empty()) {
@@ -419,6 +429,8 @@
 }
 
 void BasicPortAllocatorSession::RegatherOnAllNetworks() {
+  RTC_DCHECK_RUN_ON(network_thread_);
+
   std::vector<rtc::Network*> networks = GetNetworks();
   if (networks.empty()) {
     return;
@@ -437,6 +449,7 @@
     const std::vector<rtc::Network*>& networks,
     bool disable_equivalent_phases,
     IceRegatheringReason reason) {
+  RTC_DCHECK_RUN_ON(network_thread_);
   // Remove ports from being used locally and send signaling to remove
   // the candidates on the remote side.
   std::vector<PortData*> ports_to_prune = GetUnprunedPorts(networks);
@@ -454,6 +467,7 @@
 
 void BasicPortAllocatorSession::SetStunKeepaliveIntervalForReadyPorts(
     const absl::optional<int>& stun_keepalive_interval) {
+  RTC_DCHECK_RUN_ON(network_thread_);
   auto ports = ReadyPorts();
   for (PortInterface* port : ports) {
     // The port type and protocol can be used to identify different subclasses
@@ -468,6 +482,7 @@
 }
 
 std::vector<PortInterface*> BasicPortAllocatorSession::ReadyPorts() const {
+  RTC_DCHECK_RUN_ON(network_thread_);
   std::vector<PortInterface*> ret;
   for (const PortData& data : ports_) {
     if (data.ready()) {
@@ -478,6 +493,7 @@
 }
 
 std::vector<Candidate> BasicPortAllocatorSession::ReadyCandidates() const {
+  RTC_DCHECK_RUN_ON(network_thread_);
   std::vector<Candidate> candidates;
   for (const PortData& data : ports_) {
     if (!data.ready()) {
@@ -491,6 +507,7 @@
 void BasicPortAllocatorSession::GetCandidatesFromPort(
     const PortData& data,
     std::vector<Candidate>* candidates) const {
+  RTC_DCHECK_RUN_ON(network_thread_);
   RTC_CHECK(candidates != nullptr);
   for (const Candidate& candidate : data.port()->Candidates()) {
     if (!CheckCandidateFilter(candidate)) {
@@ -503,6 +520,7 @@
 
 Candidate BasicPortAllocatorSession::SanitizeCandidate(
     const Candidate& c) const {
+  RTC_DCHECK_RUN_ON(network_thread_);
   Candidate copy = c;
   // If the candidate has a generated hostname, we need to obfuscate its IP
   // address when signaling this candidate.
@@ -529,6 +547,7 @@
 }
 
 bool BasicPortAllocatorSession::CandidatesAllocationDone() const {
+  RTC_DCHECK_RUN_ON(network_thread_);
   // Done only if all required AllocationSequence objects
   // are created.
   if (!allocation_sequences_created_) {
@@ -553,23 +572,18 @@
 void BasicPortAllocatorSession::OnMessage(rtc::Message* message) {
   switch (message->message_id) {
     case MSG_CONFIG_START:
-      RTC_DCHECK(rtc::Thread::Current() == network_thread_);
       GetPortConfigurations();
       break;
     case MSG_CONFIG_READY:
-      RTC_DCHECK(rtc::Thread::Current() == network_thread_);
       OnConfigReady(static_cast<PortConfiguration*>(message->pdata));
       break;
     case MSG_ALLOCATE:
-      RTC_DCHECK(rtc::Thread::Current() == network_thread_);
       OnAllocate();
       break;
     case MSG_SEQUENCEOBJECTS_CREATED:
-      RTC_DCHECK(rtc::Thread::Current() == network_thread_);
       OnAllocationSequenceObjectsCreated();
       break;
     case MSG_CONFIG_STOP:
-      RTC_DCHECK(rtc::Thread::Current() == network_thread_);
       OnConfigStop();
       break;
     default:
@@ -578,6 +592,7 @@
 }
 
 void BasicPortAllocatorSession::UpdateIceParametersInternal() {
+  RTC_DCHECK_RUN_ON(network_thread_);
   for (PortData& port : ports_) {
     port.port()->set_content_name(content_name());
     port.port()->SetIceParameters(component(), ice_ufrag(), ice_pwd());
@@ -585,6 +600,7 @@
 }
 
 void BasicPortAllocatorSession::GetPortConfigurations() {
+  RTC_DCHECK_RUN_ON(network_thread_);
   PortConfiguration* config =
       new PortConfiguration(allocator_->stun_servers(), username(), password());
 
@@ -595,11 +611,13 @@
 }
 
 void BasicPortAllocatorSession::ConfigReady(PortConfiguration* config) {
+  RTC_DCHECK_RUN_ON(network_thread_);
   network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_READY, config);
 }
 
 // Adds a configuration to the list.
 void BasicPortAllocatorSession::OnConfigReady(PortConfiguration* config) {
+  RTC_DCHECK_RUN_ON(network_thread_);
   if (config) {
     configs_.push_back(config);
   }
@@ -608,7 +626,7 @@
 }
 
 void BasicPortAllocatorSession::OnConfigStop() {
-  RTC_DCHECK(rtc::Thread::Current() == network_thread_);
+  RTC_DCHECK_RUN_ON(network_thread_);
 
   // If any of the allocated ports have not completed the candidates allocation,
   // mark those as error. Since session doesn't need any new candidates
@@ -639,11 +657,13 @@
 }
 
 void BasicPortAllocatorSession::AllocatePorts() {
-  RTC_DCHECK(rtc::Thread::Current() == network_thread_);
+  RTC_DCHECK_RUN_ON(network_thread_);
   network_thread_->Post(RTC_FROM_HERE, this, MSG_ALLOCATE);
 }
 
 void BasicPortAllocatorSession::OnAllocate() {
+  RTC_DCHECK_RUN_ON(network_thread_);
+
   if (network_manager_started_ && !IsStopped()) {
     bool disable_equivalent_phases = true;
     DoAllocate(disable_equivalent_phases);
@@ -653,6 +673,7 @@
 }
 
 std::vector<rtc::Network*> BasicPortAllocatorSession::GetNetworks() {
+  RTC_DCHECK_RUN_ON(network_thread_);
   std::vector<rtc::Network*> networks;
   rtc::NetworkManager* network_manager = allocator_->network_manager();
   RTC_DCHECK(network_manager != nullptr);
@@ -738,6 +759,7 @@
 // For each network, see if we have a sequence that covers it already.  If not,
 // create a new sequence to create the appropriate ports.
 void BasicPortAllocatorSession::DoAllocate(bool disable_equivalent) {
+  RTC_DCHECK_RUN_ON(network_thread_);
   bool done_signal_needed = false;
   std::vector<rtc::Network*> networks = GetNetworks();
   if (networks.empty()) {
@@ -801,6 +823,7 @@
 }
 
 void BasicPortAllocatorSession::OnNetworksChanged() {
+  RTC_DCHECK_RUN_ON(network_thread_);
   std::vector<rtc::Network*> networks = GetNetworks();
   std::vector<rtc::Network*> failed_networks;
   for (AllocationSequence* sequence : sequences_) {
@@ -839,6 +862,7 @@
     rtc::Network* network,
     PortConfiguration* config,
     uint32_t* flags) {
+  RTC_DCHECK_RUN_ON(network_thread_);
   for (uint32_t i = 0; i < sequences_.size() &&
                        (*flags & DISABLE_ALL_PHASES) != DISABLE_ALL_PHASES;
        ++i) {
@@ -849,6 +873,7 @@
 void BasicPortAllocatorSession::AddAllocatedPort(Port* port,
                                                  AllocationSequence* seq,
                                                  bool prepare_address) {
+  RTC_DCHECK_RUN_ON(network_thread_);
   if (!port)
     return;
 
@@ -878,6 +903,7 @@
 }
 
 void BasicPortAllocatorSession::OnAllocationSequenceObjectsCreated() {
+  RTC_DCHECK_RUN_ON(network_thread_);
   allocation_sequences_created_ = true;
   // Send candidate allocation complete signal if we have no sequences.
   MaybeSignalCandidatesAllocationDone();
@@ -885,7 +911,7 @@
 
 void BasicPortAllocatorSession::OnCandidateReady(Port* port,
                                                  const Candidate& c) {
-  RTC_DCHECK(rtc::Thread::Current() == network_thread_);
+  RTC_DCHECK_RUN_ON(network_thread_);
   PortData* data = FindPort(port);
   RTC_DCHECK(data != NULL);
   RTC_LOG(LS_INFO) << port->ToString()
@@ -938,6 +964,7 @@
 
 Port* BasicPortAllocatorSession::GetBestTurnPortForNetwork(
     const std::string& network_name) const {
+  RTC_DCHECK_RUN_ON(network_thread_);
   Port* best_turn_port = nullptr;
   for (const PortData& data : ports_) {
     if (data.port()->Network()->name() == network_name &&
@@ -950,6 +977,7 @@
 }
 
 bool BasicPortAllocatorSession::PruneTurnPorts(Port* newly_pairable_turn_port) {
+  RTC_DCHECK_RUN_ON(network_thread_);
   // Note: We determine the same network based only on their network names. So
   // if an IPv4 address and an IPv6 address have the same network name, they
   // are considered the same network here.
@@ -983,13 +1011,14 @@
 }
 
 void BasicPortAllocatorSession::PruneAllPorts() {
+  RTC_DCHECK_RUN_ON(network_thread_);
   for (PortData& data : ports_) {
     data.Prune();
   }
 }
 
 void BasicPortAllocatorSession::OnPortComplete(Port* port) {
-  RTC_DCHECK(rtc::Thread::Current() == network_thread_);
+  RTC_DCHECK_RUN_ON(network_thread_);
   RTC_LOG(LS_INFO) << port->ToString()
                    << ": Port completed gathering candidates.";
   PortData* data = FindPort(port);
@@ -1007,7 +1036,7 @@
 }
 
 void BasicPortAllocatorSession::OnPortError(Port* port) {
-  RTC_DCHECK(rtc::Thread::Current() == network_thread_);
+  RTC_DCHECK_RUN_ON(network_thread_);
   RTC_LOG(LS_INFO) << port->ToString()
                    << ": Port encountered error while gathering candidates.";
   PortData* data = FindPort(port);
@@ -1025,6 +1054,8 @@
 }
 
 bool BasicPortAllocatorSession::CheckCandidateFilter(const Candidate& c) const {
+  RTC_DCHECK_RUN_ON(network_thread_);
+
   uint32_t filter = candidate_filter_;
 
   // When binding to any address, before sending packets out, the getsockname
@@ -1057,6 +1088,8 @@
 
 bool BasicPortAllocatorSession::CandidatePairable(const Candidate& c,
                                                   const Port* port) const {
+  RTC_DCHECK_RUN_ON(network_thread_);
+
   bool candidate_signalable = CheckCandidateFilter(c);
 
   // When device enumeration is disabled (to prevent non-default IP addresses
@@ -1077,11 +1110,13 @@
 
 void BasicPortAllocatorSession::OnPortAllocationComplete(
     AllocationSequence* seq) {
+  RTC_DCHECK_RUN_ON(network_thread_);
   // Send candidate allocation complete signal if all ports are done.
   MaybeSignalCandidatesAllocationDone();
 }
 
 void BasicPortAllocatorSession::MaybeSignalCandidatesAllocationDone() {
+  RTC_DCHECK_RUN_ON(network_thread_);
   if (CandidatesAllocationDone()) {
     if (pooled()) {
       RTC_LOG(LS_INFO) << "All candidates gathered for pooled session.";
@@ -1094,7 +1129,7 @@
 }
 
 void BasicPortAllocatorSession::OnPortDestroyed(PortInterface* port) {
-  RTC_DCHECK(rtc::Thread::Current() == network_thread_);
+  RTC_DCHECK_RUN_ON(network_thread_);
   for (std::vector<PortData>::iterator iter = ports_.begin();
        iter != ports_.end(); ++iter) {
     if (port == iter->port()) {
@@ -1109,6 +1144,7 @@
 
 BasicPortAllocatorSession::PortData* BasicPortAllocatorSession::FindPort(
     Port* port) {
+  RTC_DCHECK_RUN_ON(network_thread_);
   for (std::vector<PortData>::iterator it = ports_.begin(); it != ports_.end();
        ++it) {
     if (it->port() == port) {
@@ -1121,6 +1157,7 @@
 std::vector<BasicPortAllocatorSession::PortData*>
 BasicPortAllocatorSession::GetUnprunedPorts(
     const std::vector<rtc::Network*>& networks) {
+  RTC_DCHECK_RUN_ON(network_thread_);
   std::vector<PortData*> unpruned_ports;
   for (PortData& port : ports_) {
     if (!port.pruned() &&
@@ -1134,6 +1171,7 @@
 
 void BasicPortAllocatorSession::PrunePortsAndRemoveCandidates(
     const std::vector<PortData*>& port_data_list) {
+  RTC_DCHECK_RUN_ON(network_thread_);
   std::vector<PortInterface*> pruned_ports;
   std::vector<Candidate> removed_candidates;
   for (PortData* data : port_data_list) {