Bug 1336240 - Remove observer in PendingLookup and nsChannelClassifier. r=dimi, r=francois, a=lizzard
authorThomas Nguyen <tnguyen@mozilla.com>
Mon, 27 Feb 2017 13:10:15 +0800
changeset 359516 34e95ff8e264d1589dc9c317be041a3ea9616acd
parent 359515 8d1fb3b386b737d7962f02e392d238fcaea8ce5f
child 359517 b05444b3fb44626a208aa609f0f56525e4478602
push id10825
push userryanvm@gmail.com
push dateWed, 01 Mar 2017 03:03:44 +0000
treeherdermozilla-aurora@65c9d527fc8c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdimi, francois, lizzard
bugs1336240
milestone53.0a2
Bug 1336240 - Remove observer in PendingLookup and nsChannelClassifier. r=dimi, r=francois, a=lizzard
netwerk/base/nsChannelClassifier.cpp
netwerk/base/nsChannelClassifier.h
toolkit/components/downloads/ApplicationReputation.cpp
--- a/netwerk/base/nsChannelClassifier.cpp
+++ b/netwerk/base/nsChannelClassifier.cpp
@@ -408,21 +408,17 @@ nsChannelClassifier::StartInternal()
         LOG(("nsChannelClassifier[%p]: suspended channel %p",
              this, mChannel.get()));
     } else {
         LOG(("nsChannelClassifier[%p]: not expecting callback", this));
         return NS_ERROR_FAILURE;
     }
 
     // Add an observer for shutdown
-    nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
-    if (!observerService)
-      return NS_ERROR_FAILURE;
-
-    observerService->AddObserver(this, "profile-change-net-teardown", false);
+    AddShutdownObserver();
     return NS_OK;
 }
 
 bool
 nsChannelClassifier::IsHostnameWhitelisted(nsIURI *aUri,
                                            const nsACString &aWhitelisted)
 {
   nsAutoCString host;
@@ -762,34 +758,55 @@ nsChannelClassifier::OnClassifyComplete(
         mChannel->Cancel(aErrorCode);
       }
       LOG(("nsChannelClassifier[%p]: resuming channel %p from "
            "OnClassifyComplete", this, mChannel.get()));
       mChannel->Resume();
     }
 
     mChannel = nullptr;
+    RemoveShutdownObserver();
 
     return NS_OK;
 }
 
+void
+nsChannelClassifier::AddShutdownObserver()
+{
+  nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
+  if (observerService) {
+    observerService->AddObserver(this, "profile-change-net-teardown", false);
+  }
+}
+
+void
+nsChannelClassifier::RemoveShutdownObserver()
+{
+  nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
+  if (observerService) {
+    observerService->RemoveObserver(this, "profile-change-net-teardown");
+  }
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 // nsIObserver implementation
 NS_IMETHODIMP
 nsChannelClassifier::Observe(nsISupports *aSubject, const char *aTopic,
                              const char16_t *aData)
 {
   if (!strcmp(aTopic, "profile-change-net-teardown")) {
     // If we aren't getting a callback for any reason, make sure
     // we resume the channel.
 
     if (mChannel && mSuspendedChannel) {
       mSuspendedChannel = false;
       mChannel->Cancel(NS_ERROR_ABORT);
       mChannel->Resume();
     }
+
+    RemoveShutdownObserver();
   }
 
   return NS_OK;
 }
 
 } // namespace net
 } // namespace mozilla
--- a/netwerk/base/nsChannelClassifier.h
+++ b/netwerk/base/nsChannelClassifier.h
@@ -27,17 +27,18 @@ public:
     NS_DECL_NSIURICLASSIFIERCALLBACK
     NS_DECL_NSIOBSERVER
 
     // Calls nsIURIClassifier.Classify with the principal of the given channel,
     // and cancels the channel on a bad verdict.
     void Start();
     // Whether or not tracking protection should be enabled on this channel.
     nsresult ShouldEnableTrackingProtection(bool *result);
-
+    void AddShutdownObserver();
+    void RemoveShutdownObserver();
 private:
     // True if the channel is on the allow list.
     bool mIsAllowListed;
     // True if the channel has been suspended.
     bool mSuspendedChannel;
     nsCOMPtr<nsIChannel> mChannel;
     Maybe<bool> mTrackingProtectionEnabled;
 
--- a/toolkit/components/downloads/ApplicationReputation.cpp
+++ b/toolkit/components/downloads/ApplicationReputation.cpp
@@ -46,16 +46,17 @@
 #include "nsString.h"
 #include "nsTArray.h"
 #include "nsThreadUtils.h"
 #include "nsXPCOMStrings.h"
 
 #include "nsIContentPolicy.h"
 #include "nsILoadInfo.h"
 #include "nsContentUtils.h"
+#include "nsWeakReference.h"
 
 using mozilla::ArrayLength;
 using mozilla::BasePrincipal;
 using mozilla::OriginAttributes;
 using mozilla::Preferences;
 using mozilla::TimeStamp;
 using mozilla::Telemetry::Accumulate;
 using safe_browsing::ClientDownloadRequest;
@@ -87,17 +88,18 @@ mozilla::LazyLogModule ApplicationReputa
 class PendingDBLookup;
 
 // A single use class private to ApplicationReputationService encapsulating an
 // nsIApplicationReputationQuery and an nsIApplicationReputationCallback. Once
 // created by ApplicationReputationService, it is guaranteed to call mCallback.
 // This class is private to ApplicationReputationService.
 class PendingLookup final : public nsIStreamListener,
                             public nsITimerCallback,
-                            public nsIObserver
+                            public nsIObserver,
+                            public nsSupportsWeakReference
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIREQUESTOBSERVER
   NS_DECL_NSISTREAMLISTENER
   NS_DECL_NSITIMERCALLBACK
   NS_DECL_NSIOBSERVER
 
@@ -367,17 +369,18 @@ PendingDBLookup::HandleEvent(const nsACS
     Accumulate(mozilla::Telemetry::APPLICATION_REPUTATION_LOCAL, NO_LIST);
   }
   return mPendingLookup->LookupNext();
 }
 
 NS_IMPL_ISUPPORTS(PendingLookup,
                   nsIStreamListener,
                   nsIRequestObserver,
-                  nsIObserver)
+                  nsIObserver,
+                  nsISupportsWeakReference)
 
 PendingLookup::PendingLookup(nsIApplicationReputationQuery* aQuery,
                              nsIApplicationReputationCallback* aCallback) :
   mBlocklistCount(0),
   mAllowlistCount(0),
   mQuery(aQuery),
   mCallback(aCallback)
 {
@@ -1534,11 +1537,11 @@ nsresult ApplicationReputationService::Q
 
   // Add an observer for shutdown
   nsCOMPtr<nsIObserverService> observerService =
     mozilla::services::GetObserverService();
   if (!observerService) {
     return NS_ERROR_FAILURE;
   }
 
-  observerService->AddObserver(lookup, "quit-application", false);
+  observerService->AddObserver(lookup, "quit-application", true);
   return lookup->StartLookup();
 }