Use a provider in rtc::Network to access the mDNS responder.

Bug: chromium:930339
Change-Id: I42c22f4417f2f12b606bb9791edc074561c78518
Reviewed-on: https://webrtc-review.googlesource.com/c/122680
Commit-Queue: Qingsi Wang <qingsi@google.com>
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Reviewed-by: Sergey Ulanov <sergeyu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#26674}
diff --git a/rtc_base/network.cc b/rtc_base/network.cc
index 0bbb260..496c769 100644
--- a/rtc_base/network.cc
+++ b/rtc_base/network.cc
@@ -283,8 +283,8 @@
     ipv4_any_address_network_.reset(
         new rtc::Network("any", "any", ipv4_any_address, 0, ADAPTER_TYPE_ANY));
     ipv4_any_address_network_->set_default_local_address_provider(this);
+    ipv4_any_address_network_->set_mdns_responder_provider(this);
     ipv4_any_address_network_->AddIP(ipv4_any_address);
-    ipv4_any_address_network_->SetMdnsResponder(GetMdnsResponder());
   }
   networks->push_back(ipv4_any_address_network_.get());
 
@@ -293,8 +293,8 @@
     ipv6_any_address_network_.reset(
         new rtc::Network("any", "any", ipv6_any_address, 0, ADAPTER_TYPE_ANY));
     ipv6_any_address_network_->set_default_local_address_provider(this);
+    ipv6_any_address_network_->set_mdns_responder_provider(this);
     ipv6_any_address_network_->AddIP(ipv6_any_address);
-    ipv6_any_address_network_->SetMdnsResponder(GetMdnsResponder());
   }
   networks->push_back(ipv6_any_address_network_.get());
 }
@@ -383,7 +383,7 @@
         delete net;
       }
     }
-    networks_map_[key]->SetMdnsResponder(GetMdnsResponder());
+    networks_map_[key]->set_mdns_responder_provider(this);
   }
   // It may still happen that the merged list is a subset of |networks_|.
   // To detect this change, we compare their sizes.
@@ -739,6 +739,7 @@
           std::unique_ptr<Network> network(new Network(
               name, description, prefix, prefix_length, adapter_type));
           network->set_default_local_address_provider(this);
+          network->set_mdns_responder_provider(this);
           network->set_scope_id(scope_id);
           network->AddIP(ip);
           bool ignored = IsIgnoredNetwork(*network);
@@ -1049,6 +1050,13 @@
   return static_cast<IPAddress>(selected_ip);
 }
 
+webrtc::MdnsResponderInterface* Network::GetMdnsResponder() const {
+  if (mdns_responder_provider_ == nullptr) {
+    return nullptr;
+  }
+  return mdns_responder_provider_->GetMdnsResponder();
+}
+
 uint16_t Network::GetCost() const {
   AdapterType type = IsVpn() ? underlying_type_for_vpn_ : type_;
   return ComputeNetworkCostByType(type);
diff --git a/rtc_base/network.h b/rtc_base/network.h
index aa806af..d202f6e 100644
--- a/rtc_base/network.h
+++ b/rtc_base/network.h
@@ -57,12 +57,24 @@
 class DefaultLocalAddressProvider {
  public:
   virtual ~DefaultLocalAddressProvider() = default;
+
   // The default local address is the local address used in multi-homed endpoint
   // when the any address (0.0.0.0 or ::) is used as the local address. It's
   // important to check the return value as a IP family may not be enabled.
   virtual bool GetDefaultLocalAddress(int family, IPAddress* ipaddr) const = 0;
 };
 
+class MdnsResponderProvider {
+ public:
+  virtual ~MdnsResponderProvider() = default;
+
+  // Returns the mDNS responder that can be used to obfuscate the local IP
+  // addresses of ICE host candidates by mDNS hostnames.
+  //
+  // The provider MUST outlive the mDNS responder.
+  virtual webrtc::MdnsResponderInterface* GetMdnsResponder() const = 0;
+};
+
 // Generic network manager interface. It provides list of local
 // networks.
 //
@@ -72,7 +84,8 @@
 //
 // This allows constructing a NetworkManager subclass on one thread and
 // passing it into an object that uses it on a different thread.
-class NetworkManager : public DefaultLocalAddressProvider {
+class NetworkManager : public DefaultLocalAddressProvider,
+                       public MdnsResponderProvider {
  public:
   typedef std::vector<Network*> NetworkList;
 
@@ -139,9 +152,8 @@
     }
   };
 
-  // Returns the mDNS responder that can be used to obfuscate the local IP
-  // addresses of ICE host candidates by mDNS hostnames.
-  virtual webrtc::MdnsResponderInterface* GetMdnsResponder() const;
+  // MdnsResponderProvider interface.
+  webrtc::MdnsResponderInterface* GetMdnsResponder() const override;
 };
 
 // Base class for NetworkManager implementations.
@@ -303,7 +315,11 @@
     default_local_address_provider_ = provider;
   }
 
-  // Returns the name of the interface this network is associated wtih.
+  void set_mdns_responder_provider(const MdnsResponderProvider* provider) {
+    mdns_responder_provider_ = provider;
+  }
+
+  // Returns the name of the interface this network is associated with.
   const std::string& name() const { return name_; }
 
   // Returns the OS-assigned name for this network. This is useful for
@@ -322,7 +338,7 @@
   // Returns the Network's current idea of the 'best' IP it has.
   // Or return an unset IP if this network has no active addresses.
   // Here is the rule on how we mark the IPv6 address as ignorable for WebRTC.
-  // 1) return all global temporary dynamic and non-deprecrated ones.
+  // 1) return all global temporary dynamic and non-deprecated ones.
   // 2) if #1 not available, return global ones.
   // 3) if #2 not available, use ULA ipv6 as last resort. (ULA stands
   // for unique local address, which is not route-able in open
@@ -354,19 +370,11 @@
   const std::vector<InterfaceAddress>& GetIPs() const { return ips_; }
   // Clear the network's list of addresses.
   void ClearIPs() { ips_.clear(); }
-  // Sets the mDNS responder that can be used to obfuscate the local IP
+  // Returns the mDNS responder that can be used to obfuscate the local IP
   // addresses of host candidates by mDNS names in ICE gathering. After a
   // name-address mapping is created by the mDNS responder, queries for the
   // created name will be resolved by the responder.
-  //
-  // The mDNS responder, if not null, should outlive this rtc::Network.
-  void SetMdnsResponder(webrtc::MdnsResponderInterface* mdns_responder) {
-    mdns_responder_ = mdns_responder;
-  }
-  // Returns the mDNS responder, which is null by default.
-  webrtc::MdnsResponderInterface* GetMdnsResponder() const {
-    return mdns_responder_;
-  }
+  webrtc::MdnsResponderInterface* GetMdnsResponder() const;
 
   // Returns the scope-id of the network's address.
   // Should only be relevant for link-local IPv6 addresses.
@@ -433,13 +441,13 @@
 
  private:
   const DefaultLocalAddressProvider* default_local_address_provider_ = nullptr;
+  const MdnsResponderProvider* mdns_responder_provider_ = nullptr;
   std::string name_;
   std::string description_;
   IPAddress prefix_;
   int prefix_length_;
   std::string key_;
   std::vector<InterfaceAddress> ips_;
-  webrtc::MdnsResponderInterface* mdns_responder_ = nullptr;
   int scope_id_;
   bool ignored_;
   AdapterType type_;