Bug 1277405: Clear Places observers at XPCOM shutdown. r?mak draft
authorKris Maglione <maglione.k@gmail.com>
Wed, 01 Jun 2016 16:47:34 -0700
changeset 374883 e3828d8cc57246ef2633dc7d7ef7145437f2ebf6
parent 374456 92e0c73391e71a400e2c6674bca5ca70804ab081
child 374975 40ef1d1fcc1570cfea6f493ea768882461856553
push id20102
push usermaglione.k@gmail.com
push dateThu, 02 Jun 2016 22:52:10 +0000
reviewersmak
bugs1277405
milestone49.0a1
Bug 1277405: Clear Places observers at XPCOM shutdown. r?mak MozReview-Commit-ID: 6JYMX7TXDLZ
toolkit/components/places/nsNavBookmarks.cpp
toolkit/components/places/nsNavHistory.cpp
--- a/toolkit/components/places/nsNavBookmarks.cpp
+++ b/toolkit/components/places/nsNavBookmarks.cpp
@@ -2517,16 +2517,20 @@ nsNavBookmarks::RunInBatchMode(nsINavHis
 }
 
 
 NS_IMETHODIMP
 nsNavBookmarks::AddObserver(nsINavBookmarkObserver* aObserver,
                             bool aOwnsWeak)
 {
   NS_ENSURE_ARG(aObserver);
+
+  if (NS_WARN_IF(!mCanNotify))
+    return NS_ERROR_UNEXPECTED;
+
   return mObservers.AppendWeakElement(aObserver, aOwnsWeak);
 }
 
 
 NS_IMETHODIMP
 nsNavBookmarks::RemoveObserver(nsINavBookmarkObserver* aObserver)
 {
   return mObservers.RemoveWeakElement(aObserver);
@@ -2629,16 +2633,17 @@ nsNavBookmarks::Observe(nsISupports *aSu
     if (annosvc) {
       annosvc->RemoveObserver(this);
     }
   }
   else if (strcmp(aTopic, TOPIC_PLACES_CONNECTION_CLOSED) == 0) {
     // Don't even try to notify observers from this point on, the category
     // cache would init services that could try to use our APIs.
     mCanNotify = false;
+    mObservers.Clear();
   }
 
   return NS_OK;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 //// nsINavHistoryObserver
 
--- a/toolkit/components/places/nsNavHistory.cpp
+++ b/toolkit/components/places/nsNavHistory.cpp
@@ -2245,16 +2245,19 @@ nsNavHistory::GetQueryResults(nsNavHisto
 }
 
 NS_IMETHODIMP
 nsNavHistory::AddObserver(nsINavHistoryObserver* aObserver, bool aOwnsWeak)
 {
   NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread");
   NS_ENSURE_ARG(aObserver);
 
+  if (NS_WARN_IF(!mCanNotify))
+    return NS_ERROR_UNEXPECTED;
+
   return mObservers.AppendWeakElement(aObserver, aOwnsWeak);
 }
 
 NS_IMETHODIMP
 nsNavHistory::RemoveObserver(nsINavHistoryObserver* aObserver)
 {
   NS_ASSERTION(NS_IsMainThread(), "This can only be called on the main thread");
   NS_ENSURE_ARG(aObserver);
@@ -3082,19 +3085,20 @@ nsNavHistory::Observe(nsISupports *aSubj
       strcmp(aTopic, TOPIC_PROFILE_CHANGE) == 0 ||
       strcmp(aTopic, TOPIC_SIMULATE_PLACES_SHUTDOWN) == 0) {
     // These notifications are used by tests to simulate a Places shutdown.
     // They should just be forwarded to the Database handle.
     mDB->Observe(aSubject, aTopic, aData);
   }
 
   else if (strcmp(aTopic, TOPIC_PLACES_CONNECTION_CLOSED) == 0) {
-      // Don't even try to notify observers from this point on, the category
-      // cache would init services that could try to use our APIs.
-      mCanNotify = false;
+    // Don't even try to notify observers from this point on, the category
+    // cache would init services that could try to use our APIs.
+    mCanNotify = false;
+    mObservers.Clear();
   }
 
 #ifdef MOZ_XUL
   else if (strcmp(aTopic, TOPIC_AUTOCOMPLETE_FEEDBACK_INCOMING) == 0) {
     nsCOMPtr<nsIAutoCompleteInput> input = do_QueryInterface(aSubject);
     if (!input)
       return NS_OK;