Bug 716661 - Convert observers in nsDownloadManager to weak observers. r=mak
authorPanagiotis Koutsourakis <panagiotis.koutsourakis@gmail.com>
Fri, 10 Feb 2012 16:06:37 +0100
changeset 89501 67017dd3b7f6650e6bcd5b6f545e0309e8de7049
parent 89500 38a007426ee26b2c51eec4af6f77bbb3a8c5fe9e
child 89502 87a5ed480992788832387ba91a77c69fbe9fe349
push id783
push userlsblakk@mozilla.com
push dateTue, 24 Apr 2012 17:33:42 +0000
treeherdermozilla-beta@11faed19f136 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs716661
milestone13.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 716661 - Convert observers in nsDownloadManager to weak observers. r=mak Make nsDownloadManager inherit from nsSupportsWeakReference, expose the new interface, and change the last argument of the addObserver calls to true to signify that we are adding a weak observer.
toolkit/components/downloads/nsDownloadManager.cpp
toolkit/components/downloads/nsDownloadManager.h
--- a/toolkit/components/downloads/nsDownloadManager.cpp
+++ b/toolkit/components/downloads/nsDownloadManager.cpp
@@ -106,21 +106,22 @@ static const PRInt64 gUpdateInterval = 4
 #define DM_DB_NAME             NS_LITERAL_STRING("downloads.sqlite")
 #define DM_DB_CORRUPT_FILENAME NS_LITERAL_STRING("downloads.sqlite.corrupt")
 
 #define NS_SYSTEMINFO_CONTRACTID "@mozilla.org/system-info;1"
 
 ////////////////////////////////////////////////////////////////////////////////
 //// nsDownloadManager
 
-NS_IMPL_ISUPPORTS3(
+NS_IMPL_ISUPPORTS4(
   nsDownloadManager
 , nsIDownloadManager
 , nsINavHistoryObserver
 , nsIObserver
+, nsISupportsWeakReference
 )
 
 nsDownloadManager *nsDownloadManager::gDownloadManagerService = nsnull;
 
 nsDownloadManager *
 nsDownloadManager::GetSingleton()
 {
   if (gDownloadManagerService) {
@@ -881,33 +882,29 @@ nsDownloadManager::Init()
   nsCOMPtr<nsINavHistoryService> history =
     do_GetService(NS_NAVHISTORYSERVICE_CONTRACTID);
 
   // The following AddObserver calls must be the last lines in this function,
   // because otherwise, this function may fail (and thus, this object would be not
   // completely initialized), but the observerservice would still keep a reference
   // to us and notify us about shutdown, which may cause crashes.
   // failure to add an observer is not critical
-  //
-  // These observers will be cleaned up automatically at app shutdown.  We do
-  // not bother explicitly breaking the observers because we are a singleton
-  // that lives for the duration of the app.
-  (void)mObserverService->AddObserver(this, "quit-application", false);
-  (void)mObserverService->AddObserver(this, "quit-application-requested", false);
-  (void)mObserverService->AddObserver(this, "offline-requested", false);
-  (void)mObserverService->AddObserver(this, "sleep_notification", false);
-  (void)mObserverService->AddObserver(this, "wake_notification", false);
-  (void)mObserverService->AddObserver(this, "profile-before-change", false);
-  (void)mObserverService->AddObserver(this, NS_IOSERVICE_GOING_OFFLINE_TOPIC, false);
-  (void)mObserverService->AddObserver(this, NS_IOSERVICE_OFFLINE_STATUS_TOPIC, false);
-  (void)mObserverService->AddObserver(this, NS_PRIVATE_BROWSING_REQUEST_TOPIC, false);
-  (void)mObserverService->AddObserver(this, NS_PRIVATE_BROWSING_SWITCH_TOPIC, false);
+  (void)mObserverService->AddObserver(this, "quit-application", true);
+  (void)mObserverService->AddObserver(this, "quit-application-requested", true);
+  (void)mObserverService->AddObserver(this, "offline-requested", true);
+  (void)mObserverService->AddObserver(this, "sleep_notification", true);
+  (void)mObserverService->AddObserver(this, "wake_notification", true);
+  (void)mObserverService->AddObserver(this, "profile-before-change", true);
+  (void)mObserverService->AddObserver(this, NS_IOSERVICE_GOING_OFFLINE_TOPIC, true);
+  (void)mObserverService->AddObserver(this, NS_IOSERVICE_OFFLINE_STATUS_TOPIC, true);
+  (void)mObserverService->AddObserver(this, NS_PRIVATE_BROWSING_REQUEST_TOPIC, true);
+  (void)mObserverService->AddObserver(this, NS_PRIVATE_BROWSING_SWITCH_TOPIC, true);
 
   if (history)
-    (void)history->AddObserver(this, false);
+    (void)history->AddObserver(this, true);
 
   return NS_OK;
 }
 
 PRInt32
 nsDownloadManager::GetRetentionBehavior()
 {
   // We use 0 as the default, which is "remove when done"
--- a/toolkit/components/downloads/nsDownloadManager.h
+++ b/toolkit/components/downloads/nsDownloadManager.h
@@ -53,16 +53,17 @@
 #include "nsIDownloadProgressListener.h"
 #include "nsILocalFile.h"
 #include "nsIMIMEInfo.h"
 #include "nsINavHistoryService.h"
 #include "nsIObserver.h"
 #include "nsIObserverService.h"
 #include "nsIStringBundle.h"
 #include "nsISupportsPrimitives.h"
+#include "nsWeakReference.h"
 #include "nsITimer.h"
 #include "nsString.h"
 
 #include "mozStorageHelper.h"
 #include "nsAutoPtr.h"
 #include "nsCOMArray.h"
 
 typedef PRInt16 DownloadState;
@@ -71,17 +72,18 @@ typedef PRInt16 DownloadType;
 class nsDownload;
 
 #ifdef DOWNLOAD_SCANNER
 #include "nsDownloadScanner.h"
 #endif
 
 class nsDownloadManager : public nsIDownloadManager,
                           public nsINavHistoryObserver,
-                          public nsIObserver
+                          public nsIObserver,
+                          public nsSupportsWeakReference
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIDOWNLOADMANAGER
   NS_DECL_NSINAVHISTORYOBSERVER
   NS_DECL_NSIOBSERVER
 
   nsresult Init();