Bug 1589448 - Add network id changed event on MacOS r=dragana
authorKershaw Chang <kershaw@mozilla.com>
Tue, 29 Oct 2019 23:19:03 +0000
changeset 500377 36144e0a8663499cd04bed7fbea8c086729a9324
parent 500376 16931240f0f1a86dfc5ce68d0ecba21f1fde736d
child 500378 dce0e0b88e8a36ed6580ef51d3aa1cfbbea36db9
push id36763
push userrmaries@mozilla.com
push dateMon, 04 Nov 2019 21:44:06 +0000
treeherdermozilla-central@75a7a3400888 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdragana
bugs1589448
milestone72.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1589448 - Add network id changed event on MacOS r=dragana Differential Revision: https://phabricator.services.mozilla.com/D50463
netwerk/base/nsINetworkLinkService.idl
netwerk/base/nsIOService.cpp
netwerk/system/mac/nsNetworkLinkService.h
netwerk/system/mac/nsNetworkLinkService.mm
--- a/netwerk/base/nsINetworkLinkService.idl
+++ b/netwerk/base/nsINetworkLinkService.idl
@@ -76,16 +76,20 @@ interface nsINetworkLinkService : nsISup
  */
 #define NS_NETWORK_LINK_DATA_DOWN    "down"
 /**
  * isLinkUp is still true, but the network setup is modified.
  * linkStatusKnown is true.
  */
 #define NS_NETWORK_LINK_DATA_CHANGED "changed"
 /**
+ * network ID has changed.
+ */
+#define NS_NETWORK_LINK_DATA_NETWORKID_CHANGED "networkid-changed"
+/**
  * linkStatusKnown is now false.
  */
 #define NS_NETWORK_LINK_DATA_UNKNOWN "unknown"
 
 /**
  * We send notifications through nsIObserverService with topic
  * NS_NETWORK_LINK_TYPE_TOPIC whenever the network connection type
  * changes. We pass one of the valid connection type constants
--- a/netwerk/base/nsIOService.cpp
+++ b/netwerk/base/nsIOService.cpp
@@ -1607,16 +1607,18 @@ nsresult nsIOService::OnNetworkLinkEvent
   }
   if (!strcmp(data, NS_NETWORK_LINK_DATA_DOWN)) {
     isUp = false;
   } else if (!strcmp(data, NS_NETWORK_LINK_DATA_UP)) {
     isUp = true;
   } else if (!strcmp(data, NS_NETWORK_LINK_DATA_UNKNOWN)) {
     nsresult rv = mNetworkLinkService->GetIsLinkUp(&isUp);
     NS_ENSURE_SUCCESS(rv, rv);
+  } else if (!strcmp(data, NS_NETWORK_LINK_DATA_NETWORKID_CHANGED)) {
+    LOG(("nsIOService::OnNetworkLinkEvent Network id changed"));
   } else {
     NS_WARNING("Unhandled network event!");
     return NS_OK;
   }
 
   return SetConnectivityInternal(isUp);
 }
 
--- a/netwerk/system/mac/nsNetworkLinkService.h
+++ b/netwerk/system/mac/nsNetworkLinkService.h
@@ -43,17 +43,20 @@ class nsNetworkLinkService : public nsIN
   bool mStatusKnown;
 
   SCNetworkReachabilityRef mReachability;
   CFRunLoopRef mCFRunLoop;
   CFRunLoopSourceRef mRunLoopSource;
   SCDynamicStoreRef mStoreRef;
 
   void UpdateReachability();
-  void SendEvent(bool aNetworkChanged);
+  void OnIPConfigChanged();
+  void OnNetworkIdChanged();
+  void OnReachabilityChanged();
+  void SendEvent(const char* aEventID);
   static void ReachabilityChanged(SCNetworkReachabilityRef target,
                                   SCNetworkConnectionFlags flags, void* info);
   static void NetworkConfigChanged(SCDynamicStoreRef store,
                                    CFArrayRef changedKeys, void* info);
   void calculateNetworkIdWithDelay(uint32_t aDelay);
   void calculateNetworkIdInternal(void);
   void DNSConfigChanged();
   void GetDnsSuffixListInternal();
--- a/netwerk/system/mac/nsNetworkLinkService.mm
+++ b/netwerk/system/mac/nsNetworkLinkService.mm
@@ -625,18 +625,19 @@ void nsNetworkLinkService::calculateNetw
       Telemetry::Accumulate(Telemetry::NETWORK_ID2, 0);
     }
   }
 
   // Don't report network change if this is the first time we calculate the id.
   static bool initialIDCalculation = true;
   if (idChanged && !initialIDCalculation) {
     RefPtr<nsNetworkLinkService> self = this;
-    NS_DispatchToMainThread(NS_NewRunnableFunction(
-        "nsNetworkLinkService::calculateNetworkIdInternal", [self]() { self->SendEvent(true); }));
+    NS_DispatchToMainThread(
+        NS_NewRunnableFunction("nsNetworkLinkService::calculateNetworkIdInternal",
+                               [self]() { self->OnNetworkIdChanged(); }));
   }
 
   initialIDCalculation = false;
 }
 
 NS_IMETHODIMP
 nsNetworkLinkService::Observe(nsISupports* subject, const char* topic, const char16_t* data) {
   if (!strcmp(topic, "xpcom-shutdown")) {
@@ -660,17 +661,17 @@ void nsNetworkLinkService::NetworkConfig
     }
     if (CFStringHasSuffix(key, kSCEntNetDNS)) {
       dnsConfigChanged = true;
     }
   }
 
   nsNetworkLinkService* service = static_cast<nsNetworkLinkService*>(aInfo);
   if (ipConfigChanged) {
-    service->calculateNetworkIdWithDelay(kNetworkIdDelayAfterChange);
+    service->OnIPConfigChanged();
   }
 
   if (dnsConfigChanged) {
     service->DNSConfigChanged();
   }
 }
 
 void nsNetworkLinkService::DNSConfigChanged() {
@@ -841,52 +842,67 @@ void nsNetworkLinkService::UpdateReachab
 
   bool reachable = (flags & kSCNetworkFlagsReachable) != 0;
   bool needsConnection = (flags & kSCNetworkFlagsConnectionRequired) != 0;
 
   mLinkUp = (reachable && !needsConnection);
   mStatusKnown = true;
 }
 
-void nsNetworkLinkService::SendEvent(bool aNetworkChanged) {
+void nsNetworkLinkService::OnIPConfigChanged() {
   MOZ_ASSERT(NS_IsMainThread());
 
-  nsCOMPtr<nsIObserverService> observerService = do_GetService("@mozilla.org/observer-service;1");
-  if (!observerService) {
+  calculateNetworkIdWithDelay(kNetworkIdDelayAfterChange);
+  if (!StaticPrefs::network_notify_changed()) {
     return;
   }
 
-  const char* event;
-  if (aNetworkChanged) {
-    if (!StaticPrefs::network_notify_changed()) {
-      return;
-    }
-    event = NS_NETWORK_LINK_DATA_CHANGED;
+  if (!mNetworkChangeTime.IsNull()) {
+    Telemetry::AccumulateTimeDelta(Telemetry::NETWORK_TIME_BETWEEN_NETWORK_CHANGE_EVENTS,
+                                   mNetworkChangeTime);
+  }
+  mNetworkChangeTime = TimeStamp::Now();
+
+  SendEvent(NS_NETWORK_LINK_DATA_CHANGED);
+}
+
+void nsNetworkLinkService::OnNetworkIdChanged() {
+  MOZ_ASSERT(NS_IsMainThread());
+
+  SendEvent(NS_NETWORK_LINK_DATA_NETWORKID_CHANGED);
+}
+
+void nsNetworkLinkService::OnReachabilityChanged() {
+  MOZ_ASSERT(NS_IsMainThread());
 
-    if (!mNetworkChangeTime.IsNull()) {
-      Telemetry::AccumulateTimeDelta(Telemetry::NETWORK_TIME_BETWEEN_NETWORK_CHANGE_EVENTS,
-                                     mNetworkChangeTime);
-    }
-    mNetworkChangeTime = TimeStamp::Now();
-  } else if (!mStatusKnown) {
-    event = NS_NETWORK_LINK_DATA_UNKNOWN;
-  } else {
-    event = mLinkUp ? NS_NETWORK_LINK_DATA_UP : NS_NETWORK_LINK_DATA_DOWN;
+  if (!mStatusKnown) {
+    SendEvent(NS_NETWORK_LINK_DATA_UNKNOWN);
+    return;
   }
-  LOG(("SendEvent: network is '%s'\n", event));
+
+  SendEvent(mLinkUp ? NS_NETWORK_LINK_DATA_UP : NS_NETWORK_LINK_DATA_DOWN);
+}
+
+void nsNetworkLinkService::SendEvent(const char* aEventID) {
+  MOZ_ASSERT(NS_IsMainThread());
 
-  observerService->NotifyObservers(static_cast<nsINetworkLinkService*>(this), NS_NETWORK_LINK_TOPIC,
-                                   NS_ConvertASCIItoUTF16(event).get());
+  LOG(("SendEvent: network is '%s'\n", aEventID));
+
+  nsCOMPtr<nsIObserverService> observerService = do_GetService("@mozilla.org/observer-service;1");
+  if (observerService) {
+    observerService->NotifyObservers(static_cast<nsINetworkLinkService*>(this),
+                                     NS_NETWORK_LINK_TOPIC, NS_ConvertASCIItoUTF16(aEventID).get());
+  }
 }
 
 /* static */
 void nsNetworkLinkService::ReachabilityChanged(SCNetworkReachabilityRef target,
                                                SCNetworkConnectionFlags flags, void* info) {
   LOG(("nsNetworkLinkService::ReachabilityChanged"));
   nsNetworkLinkService* service = static_cast<nsNetworkLinkService*>(info);
 
   service->UpdateReachability();
-  service->SendEvent(false);
+  service->OnReachabilityChanged();
   service->calculateNetworkIdWithDelay(kNetworkIdDelayAfterChange);
   // If a new interface is up or the order of interfaces is changed, we should
   // update the DNS suffix list.
   service->DNSConfigChanged();
 }