Bug 1239955 - Let DNSService rely on IOService::Offline - v2.5, r=bagder, a=jocheng
authorKershaw Chang <kechang@mozilla.com>
Tue, 23 Feb 2016 02:21:00 +0100
changeset 292656 8a1846fcd8594d382ae8effa02a13e12da6f7a83
parent 292655 38e6a8bdf412a947418ffffc54c4821fe6103f9f
child 292657 cfc70d3cdb9a2d994cc07983d707333994643d46
push id309
push usercbook@mozilla.com
push dateTue, 01 Mar 2016 08:00:21 +0000
reviewersbagder, jocheng
bugs1239955
milestone44.0
Bug 1239955 - Let DNSService rely on IOService::Offline - v2.5, r=bagder, a=jocheng
netwerk/base/nsIOService.cpp
netwerk/dns/ChildDNSService.cpp
netwerk/dns/ChildDNSService.h
netwerk/dns/nsDNSService2.cpp
netwerk/dns/nsDNSService2.h
netwerk/dns/nsPIDNSService.idl
--- a/netwerk/base/nsIOService.cpp
+++ b/netwerk/base/nsIOService.cpp
@@ -1012,32 +1012,28 @@ nsIOService::SetOffline(bool offline)
             mOffline = true; // indicate we're trying to shutdown
 
             // don't care if notifications fail
             if (observerService)
                 observerService->NotifyObservers(subject,
                                                  NS_IOSERVICE_GOING_OFFLINE_TOPIC,
                                                  offlineString.get());
 
-            if (mDNSService)
-                mDNSService->SetOffline(true);
-
             if (mSocketTransportService)
                 mSocketTransportService->SetOffline(true);
 
             mLastOfflineStateChange = PR_IntervalNow();
             if (observerService)
                 observerService->NotifyObservers(subject,
                                                  NS_IOSERVICE_OFFLINE_STATUS_TOPIC,
                                                  offlineString.get());
         }
         else if (!offline && mOffline) {
             // go online
             if (mDNSService) {
-                mDNSService->SetOffline(false);
                 DebugOnly<nsresult> rv = mDNSService->Init();
                 NS_ASSERTION(NS_SUCCEEDED(rv), "DNS service init failed");
             }
             InitializeSocketTransportService();
             mOffline = false;    // indicate success only AFTER we've
                                     // brought up the services
 
             // trigger a PAC reload when we come back online
--- a/netwerk/dns/ChildDNSService.cpp
+++ b/netwerk/dns/ChildDNSService.cpp
@@ -1,19 +1,21 @@
 /* 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/ChildDNSService.h"
 #include "nsIDNSListener.h"
+#include "nsIIOService.h"
 #include "nsIThread.h"
 #include "nsThreadUtils.h"
 #include "nsIXPConnect.h"
 #include "nsIPrefService.h"
 #include "nsIProtocolProxyService.h"
+#include "nsNetCID.h"
 #include "mozilla/net/NeckoChild.h"
 #include "mozilla/net/DNSListenerProxy.h"
 #include "nsServiceManagerUtils.h"
 
 namespace mozilla {
 namespace net {
 
 //-----------------------------------------------------------------------------
@@ -37,17 +39,16 @@ ChildDNSService* ChildDNSService::GetSin
 
 NS_IMPL_ISUPPORTS(ChildDNSService,
                   nsIDNSService,
                   nsPIDNSService,
                   nsIObserver)
 
 ChildDNSService::ChildDNSService()
   : mFirstTime(true)
-  , mOffline(false)
   , mPendingRequestsLock("DNSPendingRequestsLock")
 {
   MOZ_ASSERT(IsNeckoChild());
 }
 
 ChildDNSService::~ChildDNSService()
 {
 
@@ -97,17 +98,17 @@ ChildDNSService::AsyncResolveExtended(co
     return NS_ERROR_DNS_LOOKUP_QUEUE_FULL;
   }
 
   // We need original flags for the pending requests hash.
   uint32_t originalFlags = flags;
 
   // Support apps being 'offline' even if parent is not: avoids DNS traffic by
   // apps that have been told they are offline.
-  if (mOffline) {
+  if (GetOffline()) {
     flags |= RESOLVE_OFFLINE;
   }
 
   // We need original listener for the pending requests hash.
   nsIDNSListener *originalListener = listener;
 
   // make sure JS callers get notification on the main thread
   nsCOMPtr<nsIEventTarget> target = target_;
@@ -292,28 +293,25 @@ ChildDNSService::GetPrefetchEnabled(bool
 
 NS_IMETHODIMP
 ChildDNSService::SetPrefetchEnabled(bool inVal)
 {
   mDisablePrefetch = !inVal;
   return NS_OK;
 }
 
-NS_IMETHODIMP
-ChildDNSService::GetOffline(bool* aResult)
+bool
+ChildDNSService::GetOffline() const
 {
-  *aResult = mOffline;
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-ChildDNSService::SetOffline(bool value)
-{
-  mOffline = value;
-  return NS_OK;
+  bool offline = false;
+  nsCOMPtr<nsIIOService> io = do_GetService(NS_IOSERVICE_CONTRACTID);
+  if (io) {
+    io->GetOffline(&offline);
+  }
+  return offline;
 }
 
 //-----------------------------------------------------------------------------
 // ChildDNSService::nsIObserver
 //-----------------------------------------------------------------------------
 
 NS_IMETHODIMP
 ChildDNSService::Observe(nsISupports *subject, const char *topic,
--- a/netwerk/dns/ChildDNSService.h
+++ b/netwerk/dns/ChildDNSService.h
@@ -30,27 +30,28 @@ public:
   NS_DECL_NSIDNSSERVICE
   NS_DECL_NSIOBSERVER
 
   ChildDNSService();
 
   static ChildDNSService* GetSingleton();
 
   void NotifyRequestDone(DNSRequestChild *aDnsRequest);
+
+  bool GetOffline() const;
 private:
   virtual ~ChildDNSService();
 
   void MOZ_ALWAYS_INLINE GetDNSRecordHashKey(const nsACString &aHost,
                                              uint32_t aFlags,
                                              const nsACString &aNetworkInterface,
                                              nsIDNSListener* aListener,
                                              nsACString &aHashKey);
 
   bool mFirstTime;
-  bool mOffline;
   bool mDisablePrefetch;
 
   // We need to remember pending dns requests to be able to cancel them.
   nsClassHashtable<nsCStringHashKey, nsTArray<RefPtr<DNSRequestChild>>> mPendingRequests;
   Mutex mPendingRequestsLock;
 };
 
 } // namespace net
--- a/netwerk/dns/nsDNSService2.cpp
+++ b/netwerk/dns/nsDNSService2.cpp
@@ -475,17 +475,16 @@ private:
     nsCString                                 mHostname;
 };
 
 //-----------------------------------------------------------------------------
 
 nsDNSService::nsDNSService()
     : mLock("nsDNSServer.mLock")
     , mFirstTime(true)
-    , mOffline(false)
 {
 }
 
 nsDNSService::~nsDNSService()
 {
 }
 
 NS_IMPL_ISUPPORTS(nsDNSService, nsIDNSService, nsPIDNSService, nsIObserver,
@@ -663,28 +662,25 @@ nsDNSService::Shutdown()
     if (observerService) {
         observerService->RemoveObserver(this, NS_NETWORK_LINK_TOPIC);
         observerService->RemoveObserver(this, "last-pb-context-exited");
     }
 
     return NS_OK;
 }
 
-NS_IMETHODIMP
-nsDNSService::GetOffline(bool *offline)
+bool
+nsDNSService::GetOffline() const
 {
-    *offline = mOffline;
-    return NS_OK;
-}
-
-NS_IMETHODIMP
-nsDNSService::SetOffline(bool offline)
-{
-    mOffline = offline;
-    return NS_OK;
+    bool offline = false;
+    nsCOMPtr<nsIIOService> io = do_GetService(NS_IOSERVICE_CONTRACTID);
+    if (io) {
+        io->GetOffline(&offline);
+    }
+    return offline;
 }
 
 NS_IMETHODIMP
 nsDNSService::GetPrefetchEnabled(bool *outVal)
 {
     MutexAutoLock lock(mLock);
     *outVal = !mDisablePrefetch;
     return NS_OK;
@@ -758,17 +754,17 @@ nsDNSService::AsyncResolveExtended(const
 
     if (!res)
         return NS_ERROR_OFFLINE;
 
     nsCString hostname;
     if (!PreprocessHostname(localDomain, aHostname, idn, hostname))
         return NS_ERROR_FAILURE;
 
-    if (mOffline &&
+    if (GetOffline() &&
         (!mOfflineLocalhost || !hostname.LowerCaseEqualsASCII("localhost"))) {
         flags |= RESOLVE_OFFLINE;
     }
 
     // make sure JS callers get notification on the main thread
     nsCOMPtr<nsIXPConnectWrappedJS> wrappedListener = do_QueryInterface(listener);
     if (wrappedListener && !target) {
         nsCOMPtr<nsIThread> mainThread;
@@ -870,17 +866,17 @@ nsDNSService::Resolve(const nsACString &
     }
 
     NS_ENSURE_TRUE(res, NS_ERROR_OFFLINE);
 
     nsCString hostname;
     if (!PreprocessHostname(localDomain, aHostname, idn, hostname))
         return NS_ERROR_FAILURE;
 
-    if (mOffline &&
+    if (GetOffline() &&
         (!mOfflineLocalhost || !hostname.LowerCaseEqualsASCII("localhost"))) {
         flags |= RESOLVE_OFFLINE;
     }
 
     //
     // sync resolve: since the host resolver only works asynchronously, we need
     // to use a mutex and a condvar to wait for the result.  however, since the
     // result may be in the resolvers cache, we might get called back recursively
--- a/netwerk/dns/nsDNSService2.h
+++ b/netwerk/dns/nsDNSService2.h
@@ -31,16 +31,18 @@ public:
     NS_DECL_NSIMEMORYREPORTER
 
     nsDNSService();
 
     static nsIDNSService* GetXPCOMSingleton();
 
     size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
 
+    bool GetOffline() const;
+
 private:
     ~nsDNSService();
 
     static nsDNSService* GetSingleton();
 
     uint16_t GetAFForLookup(const nsACString &host, uint32_t flags);
 
     RefPtr<nsHostResolver>  mResolver;
@@ -51,15 +53,14 @@ private:
 
     // mIPv4OnlyDomains is a comma-separated list of domains for which only
     // IPv4 DNS lookups are performed. This allows the user to disable IPv6 on
     // a per-domain basis and work around broken DNS servers. See bug 68796.
     nsAdoptingCString                         mIPv4OnlyDomains;
     bool                                      mDisableIPv6;
     bool                                      mDisablePrefetch;
     bool                                      mFirstTime;
-    bool                                      mOffline;
     bool                                      mNotifyResolution;
     bool                                      mOfflineLocalhost;
     nsTHashtable<nsCStringHashKey>            mLocalDomains;
 };
 
 #endif //nsDNSService2_h__
--- a/netwerk/dns/nsPIDNSService.idl
+++ b/netwerk/dns/nsPIDNSService.idl
@@ -5,17 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsIDNSService.idl"
 
 /**
  * This is a private interface used by the internals of the networking library.
  * It will never be frozen.  Do not use it in external code.
  */
-[scriptable, uuid(6b16fb1f-5709-4c94-a89f-a22be873c54d)]
+[scriptable, uuid(24e598fd-7b1a-436c-9154-14d8b38df8a5)]
 interface nsPIDNSService : nsIDNSService
 {
     /**
      * called to initialize the DNS service.
      */
     void init();
 
     /**
@@ -26,14 +26,9 @@ interface nsPIDNSService : nsIDNSService
      * this method.
      */
     void shutdown();
 
     /**
      * Whether or not DNS prefetching (aka RESOLVE_SPECULATE) is enabled
      */
     attribute boolean prefetchEnabled;
-
-    /**
-     * @return whether the DNS service is offline.
-     */
-    attribute boolean offline;
 };