Bug 1471628 - Use singleton for captive portal constructor r=bagder draft
authorValentin Gosu <valentin.gosu@gmail.com>
Wed, 27 Jun 2018 19:44:17 +0200
changeset 811464 120511f472e1bc1b126f81c56b7215e5ee6ac869
parent 811388 800a118ff82eb341dc41c44eb1f0d98e70722641
push id114316
push uservalentin.gosu@gmail.com
push dateWed, 27 Jun 2018 17:44:57 +0000
reviewersbagder
bugs1471628
milestone63.0a1
Bug 1471628 - Use singleton for captive portal constructor r=bagder This is to make sure that the test is using the same Captive Portal Service that nsIOService initializes. MozReview-Commit-ID: E8iURrsBkdu
netwerk/base/CaptivePortalService.cpp
netwerk/base/CaptivePortalService.h
netwerk/build/nsNetModule.cpp
--- a/netwerk/base/CaptivePortalService.cpp
+++ b/netwerk/base/CaptivePortalService.cpp
@@ -1,13 +1,14 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/net/CaptivePortalService.h"
+#include "mozilla/ClearOnShutdown.h"
 #include "mozilla/Services.h"
 #include "mozilla/Preferences.h"
 #include "nsIObserverService.h"
 #include "nsServiceManagerUtils.h"
 #include "nsXULAppAPI.h"
 #include "xpcpublic.h"
 
 static const char16_t kInterfaceName[] = u"captive-portal-inteface";
@@ -24,16 +25,31 @@ namespace net {
 static LazyLogModule gCaptivePortalLog("CaptivePortalService");
 #undef LOG
 #define LOG(args) MOZ_LOG(gCaptivePortalLog, mozilla::LogLevel::Debug, args)
 
 NS_IMPL_ISUPPORTS(CaptivePortalService, nsICaptivePortalService, nsIObserver,
                   nsISupportsWeakReference, nsITimerCallback,
                   nsICaptivePortalCallback, nsINamed)
 
+static StaticRefPtr<CaptivePortalService> gCPService;
+
+// static
+already_AddRefed<nsICaptivePortalService>
+CaptivePortalService::GetSingleton()
+{
+  if (gCPService) {
+    return do_AddRef(gCPService);
+  }
+
+  gCPService = new CaptivePortalService();
+  ClearOnShutdown(&gCPService);
+  return do_AddRef(gCPService);
+}
+
 CaptivePortalService::CaptivePortalService()
   : mState(UNKNOWN)
   , mStarted(false)
   , mInitialized(false)
   , mRequestInProgress(false)
   , mEverBeenCaptive(false)
   , mDelay(kDefaultInterval)
   , mSlackCount(0)
--- a/netwerk/base/CaptivePortalService.h
+++ b/netwerk/base/CaptivePortalService.h
@@ -28,25 +28,27 @@ class CaptivePortalService
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSICAPTIVEPORTALSERVICE
   NS_DECL_NSIOBSERVER
   NS_DECL_NSITIMERCALLBACK
   NS_DECL_NSICAPTIVEPORTALCALLBACK
   NS_DECL_NSINAMED
 
-  CaptivePortalService();
   nsresult Initialize();
   nsresult Start();
   nsresult Stop();
 
+  static already_AddRefed<nsICaptivePortalService> GetSingleton();
+
   // 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:
+  CaptivePortalService();
   virtual ~CaptivePortalService();
   nsresult PerformCheck();
   nsresult RearmTimer();
   void NotifyConnectivityAvailable(bool aCaptive);
 
   nsCOMPtr<nsICaptivePortalDetector>    mCaptivePortalDetector;
   int32_t                               mState;
 
--- a/netwerk/build/nsNetModule.cpp
+++ b/netwerk/build/nsNetModule.cpp
@@ -143,17 +143,18 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(CacheStor
 typedef mozilla::net::LoadContextInfoFactory LoadContextInfoFactory;
 NS_GENERIC_FACTORY_CONSTRUCTOR(LoadContextInfoFactory)
 
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "mozilla/net/CaptivePortalService.h"
 namespace mozilla {
 namespace net {
-  NS_GENERIC_FACTORY_CONSTRUCTOR(CaptivePortalService)
+  NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsICaptivePortalService,
+    CaptivePortalService::GetSingleton)
 } // namespace net
 } // namespace mozilla
 
 #include "RequestContextService.h"
 typedef mozilla::net::RequestContextService RequestContextService;
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(RequestContextService, Init)
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -891,17 +892,17 @@ static const mozilla::Module::CIDEntry k
 #elif defined(XP_LINUX)
     { &kNS_NETWORK_LINK_SERVICE_CID, false, nullptr, nsNotifyAddrListenerConstructor },
 #endif
     { &kNS_SERIALIZATION_HELPER_CID, false, nullptr, nsSerializationHelperConstructor },
     { &kNS_REDIRECTCHANNELREGISTRAR_CID, false, nullptr, RedirectChannelRegistrarConstructor },
     { &kNS_CACHE_STORAGE_SERVICE_CID, false, nullptr, CacheStorageServiceConstructor },
     { &kNS_NSILOADCONTEXTINFOFACTORY_CID, false, nullptr, LoadContextInfoFactoryConstructor },
     { &kNS_NETWORKPREDICTOR_CID, false, nullptr, mozilla::net::Predictor::Create },
-    { &kNS_CAPTIVEPORTAL_CID, false, nullptr, mozilla::net::CaptivePortalServiceConstructor },
+    { &kNS_CAPTIVEPORTAL_CID, false, nullptr, mozilla::net::nsICaptivePortalServiceConstructor },
     { &kNS_REQUESTCONTEXTSERVICE_CID, false, nullptr, RequestContextServiceConstructor },
 #ifdef BUILD_NETWORK_INFO_SERVICE
     { &kNETWORKINFOSERVICE_CID, false, nullptr, nsNetworkInfoServiceConstructor },
 #endif
     { nullptr }
 };
 
 static const mozilla::Module::ContractIDEntry kNeckoContracts[] = {