Bug 1422277 - Add an observer notification for when the captive portal detection is "done" r=bagder draft
authorValentin Gosu <valentin.gosu@gmail.com>
Fri, 01 Dec 2017 13:20:29 +0100
changeset 706128 74ecc449bee263d27af83fa101e6ef59166fecab
parent 706025 a21f4e2ce5186e2dc9ee411b07e9348866b4ef30
child 742596 61d3806dc5e01fd797b52b629ce99f86007a1f9e
push id91729
push uservalentin.gosu@gmail.com
push dateFri, 01 Dec 2017 12:27:52 +0000
reviewersbagder
bugs1422277
milestone59.0a1
Bug 1422277 - Add an observer notification for when the captive portal detection is "done" r=bagder MozReview-Commit-ID: D9Qr0CuUJiQ
netwerk/base/CaptivePortalService.cpp
netwerk/base/CaptivePortalService.h
netwerk/base/nsICaptivePortalService.idl
--- a/netwerk/base/CaptivePortalService.cpp
+++ b/netwerk/base/CaptivePortalService.cpp
@@ -330,16 +330,27 @@ CaptivePortalService::Observe(nsISupport
   if (observerService) {
     nsCOMPtr<nsICaptivePortalService> cps(this);
     observerService->NotifyObservers(cps, NS_IPC_CAPTIVE_PORTAL_SET_STATE, nullptr);
   }
 
   return NS_OK;
 }
 
+void
+CaptivePortalService::NotifyConnectivityAvailable(bool aCaptive)
+{
+  nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
+  if (observerService) {
+    nsCOMPtr<nsICaptivePortalService> cps(this);
+    observerService->NotifyObservers(cps, NS_CAPTIVE_PORTAL_CONNECTIVITY,
+                                     aCaptive ? u"captive" : u"clear");
+  }
+}
+
 //-----------------------------------------------------------------------------
 // CaptivePortalService::nsICaptivePortalCallback
 //-----------------------------------------------------------------------------
 NS_IMETHODIMP
 CaptivePortalService::Prepare()
 {
   LOG(("CaptivePortalService::Prepare\n"));
   MOZ_ASSERT(XRE_GetProcessType() == GeckoProcessType_Default);
@@ -359,18 +370,20 @@ CaptivePortalService::Complete(bool succ
 
   // Note: this callback gets called when:
   // 1. the request is completed, and content is valid (success == true)
   // 2. when the request is aborted or times out (success == false)
 
   if (success) {
     if (mEverBeenCaptive) {
       mState = UNLOCKED_PORTAL;
+      NotifyConnectivityAvailable(true);
     } else {
       mState = NOT_CAPTIVE;
+      NotifyConnectivityAvailable(false);
     }
   }
 
   mRequestInProgress = false;
   return NS_OK;
 }
 
 } // namespace net
--- a/netwerk/base/CaptivePortalService.h
+++ b/netwerk/base/CaptivePortalService.h
@@ -40,16 +40,17 @@ public:
 
   // This method is only called in the content process, in order to mirror
   // the captive portal state in the parent process.
   void SetStateInChild(int32_t aState);
 private:
   virtual ~CaptivePortalService();
   nsresult PerformCheck();
   nsresult RearmTimer();
+  void NotifyConnectivityAvailable(bool aCaptive);
 
   nsCOMPtr<nsICaptivePortalDetector>    mCaptivePortalDetector;
   int32_t                               mState;
 
   nsCOMPtr<nsITimer>                    mTimer;
   bool                                  mStarted;
   bool                                  mInitialized;
   bool                                  mRequestInProgress;
--- a/netwerk/base/nsICaptivePortalService.idl
+++ b/netwerk/base/nsICaptivePortalService.idl
@@ -51,9 +51,18 @@ interface nsICaptivePortalService : nsIS
 /**
  * This observer notification will be emitted when the captive portal state
  * changes. After receiving it, the ContentParent will send an IPC message
  * to the ContentChild, which will set the state in the captive portal service
  * in the child.
  */
 #define NS_IPC_CAPTIVE_PORTAL_SET_STATE "ipc:network:captive-portal-set-state"
 
+/**
+ * This notification will be emitted when the captive portal service has
+ * determined that we can connect to the internet.
+ * The service will pass either "captive" if there is an unlocked captive portal
+ * present, or "clear" if no captive portal was detected.
+ * Note: this notification only gets sent in the parent process.
+ */
+#define NS_CAPTIVE_PORTAL_CONNECTIVITY "network:captive-portal-connectivity"
+
 %}