AllocationSequence: switch signal to callback.

Bug: webrtc:12840
Change-Id: Ic25ceb9a487b28575ab7530c54b13781ed404f7a
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/221367
Commit-Queue: Markus Handell <handellm@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34240}
diff --git a/p2p/client/basic_port_allocator.cc b/p2p/client/basic_port_allocator.cc
index b74365d..49d4958 100644
--- a/p2p/client/basic_port_allocator.cc
+++ b/p2p/client/basic_port_allocator.cc
@@ -809,9 +809,11 @@
       }
 
       AllocationSequence* sequence =
-          new AllocationSequence(this, networks[i], config, sequence_flags);
-      sequence->SignalPortAllocationComplete.connect(
-          this, &BasicPortAllocatorSession::OnPortAllocationComplete);
+          new AllocationSequence(this, networks[i], config, sequence_flags,
+                                 [this, safety_flag = network_safety_.flag()] {
+                                   if (safety_flag->alive())
+                                     OnPortAllocationComplete();
+                                 });
       sequence->Init();
       sequence->Start();
       sequences_.push_back(sequence);
@@ -1124,8 +1126,7 @@
           !host_candidates_disabled);
 }
 
-void BasicPortAllocatorSession::OnPortAllocationComplete(
-    AllocationSequence* seq) {
+void BasicPortAllocatorSession::OnPortAllocationComplete() {
   RTC_DCHECK_RUN_ON(network_thread_);
   // Send candidate allocation complete signal if all ports are done.
   MaybeSignalCandidatesAllocationDone();
@@ -1216,10 +1217,12 @@
 
 // AllocationSequence
 
-AllocationSequence::AllocationSequence(BasicPortAllocatorSession* session,
-                                       rtc::Network* network,
-                                       PortConfiguration* config,
-                                       uint32_t flags)
+AllocationSequence::AllocationSequence(
+    BasicPortAllocatorSession* session,
+    rtc::Network* network,
+    PortConfiguration* config,
+    uint32_t flags,
+    std::function<void()> port_allocation_complete_callback)
     : session_(session),
       network_(network),
       config_(config),
@@ -1227,7 +1230,9 @@
       flags_(flags),
       udp_socket_(),
       udp_port_(NULL),
-      phase_(0) {}
+      phase_(0),
+      port_allocation_complete_callback_(
+          std::move(port_allocation_complete_callback)) {}
 
 void AllocationSequence::Init() {
   if (IsFlagSet(PORTALLOCATOR_ENABLE_SHARED_SOCKET)) {
@@ -1386,7 +1391,7 @@
     // If all phases in AllocationSequence are completed, no allocation
     // steps needed further. Canceling  pending signal.
     session_->network_thread()->Clear(this, MSG_ALLOCATION_PHASE);
-    SignalPortAllocationComplete(this);
+    port_allocation_complete_callback_();
   }
 }
 
diff --git a/p2p/client/basic_port_allocator.h b/p2p/client/basic_port_allocator.h
index 2964daf..ede9395 100644
--- a/p2p/client/basic_port_allocator.h
+++ b/p2p/client/basic_port_allocator.h
@@ -237,7 +237,7 @@
   void OnProtocolEnabled(AllocationSequence* seq, ProtocolType proto);
   void OnPortDestroyed(PortInterface* port);
   void MaybeSignalCandidatesAllocationDone();
-  void OnPortAllocationComplete(AllocationSequence* seq);
+  void OnPortAllocationComplete();
   PortData* FindPort(Port* port);
   std::vector<rtc::Network*> GetNetworks();
   std::vector<rtc::Network*> GetFailedNetworks();
@@ -338,10 +338,18 @@
 
     // kInit --> kRunning --> {kCompleted|kStopped}
   };
+  // |port_allocation_complete_callback| is called when AllocationSequence is
+  // done with allocating ports. This signal is useful when port allocation
+  // fails which doesn't result in any candidates. Using this signal
+  // BasicPortAllocatorSession can send its candidate discovery conclusion
+  // signal. Without this signal, BasicPortAllocatorSession doesn't have any
+  // event to trigger signal. This can also be achieved by starting a timer in
+  // BPAS, but this is less deterministic.
   AllocationSequence(BasicPortAllocatorSession* session,
                      rtc::Network* network,
                      PortConfiguration* config,
-                     uint32_t flags);
+                     uint32_t flags,
+                     std::function<void()> port_allocation_complete_callback);
   ~AllocationSequence() override;
   void Init();
   void Clear();
@@ -367,14 +375,6 @@
   // MessageHandler
   void OnMessage(rtc::Message* msg) override;
 
-  // Signal from AllocationSequence, when it's done with allocating ports.
-  // This signal is useful, when port allocation fails which doesn't result
-  // in any candidates. Using this signal BasicPortAllocatorSession can send
-  // its candidate discovery conclusion signal. Without this signal,
-  // BasicPortAllocatorSession doesn't have any event to trigger signal. This
-  // can also be achieved by starting timer in BPAS.
-  sigslot::signal1<AllocationSequence*> SignalPortAllocationComplete;
-
  protected:
   // For testing.
   void CreateTurnPort(const RelayServerConfig& config);
@@ -410,6 +410,7 @@
   UDPPort* udp_port_;
   std::vector<Port*> relay_ports_;
   int phase_;
+  std::function<void()> port_allocation_complete_callback_;
 };
 
 }  // namespace cricket