Bug 1470420: Make Stop() infallible. r?xidorn draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Fri, 22 Jun 2018 14:23:28 +0200
changeset 809583 2fe6eb6db15b9709a5b91d189fc465b05cf1ea12
parent 809582 93ab68c6fd874ab83ef32b4ff250bb70e5725f70
child 809584 4771711f6d6287a0830b1ef04dcdb9941b574214
push id113714
push userbmo:emilio@crisal.io
push dateFri, 22 Jun 2018 13:03:48 +0000
reviewersxidorn
bugs1470420
milestone62.0a1
Bug 1470420: Make Stop() infallible. r?xidorn MozReview-Commit-ID: KScKUyUSkjj
layout/style/Loader.cpp
layout/style/Loader.h
--- a/layout/style/Loader.cpp
+++ b/layout/style/Loader.cpp
@@ -2515,53 +2515,44 @@ StopLoadingSheets(
     data->mIsCancelled = true;
 
     aArr.AppendElement(data);
 
     iter.Remove();
   }
 }
 
-nsresult
+void
 Loader::Stop()
 {
   uint32_t pendingCount = mSheets ? mSheets->mPendingDatas.Count() : 0;
   uint32_t loadingCount = mSheets ? mSheets->mLoadingDatas.Count() : 0;
   LoadDataArray arr(pendingCount + loadingCount + mPostedEvents.Length());
 
   if (pendingCount) {
     StopLoadingSheets(mSheets->mPendingDatas, arr);
   }
   if (loadingCount) {
     StopLoadingSheets(mSheets->mLoadingDatas, arr);
   }
 
-  uint32_t i;
-  for (i = 0; i < mPostedEvents.Length(); ++i) {
-    SheetLoadData* data = mPostedEvents[i];
+  for (RefPtr<SheetLoadData>& data : mPostedEvents) {
     data->mIsCancelled = true;
-    if (arr.AppendElement(data)) {
-      // SheetComplete() calls Release(), so give this an extra ref.
-      NS_ADDREF(data);
-    }
-#ifdef DEBUG
-    else {
-      NS_NOTREACHED("We preallocated this memory... shouldn't really fail, "
-                    "except we never check that preallocation succeeds.");
-    }
-#endif
+    // SheetComplete() calls Release(), so give this an extra ref.
+    NS_ADDREF(data);
+    // Move since we're about to get rid of the array below.
+    arr.AppendElement(std::move(data));
   }
   mPostedEvents.Clear();
 
   mDatasToNotifyOn += arr.Length();
-  for (i = 0; i < arr.Length(); ++i) {
+  for (RefPtr<SheetLoadData>& data : arr) {
     --mDatasToNotifyOn;
-    SheetComplete(arr[i], NS_BINDING_ABORTED);
+    SheetComplete(data, NS_BINDING_ABORTED);
   }
-  return NS_OK;
 }
 
 bool
 Loader::HasPendingLoads()
 {
   return
     (mSheets && mSheets->mLoadingDatas.Count() != 0) ||
     (mSheets && mSheets->mPendingDatas.Count() != 0) ||
--- a/layout/style/Loader.h
+++ b/layout/style/Loader.h
@@ -375,17 +375,17 @@ public:
                      CORSMode aCORSMode = CORS_NONE,
                      ReferrerPolicy aReferrerPolicy = mozilla::net::RP_Unset,
                      const nsAString& aIntegrity = EmptyString());
 
   /**
    * Stop loading all sheets.  All nsICSSLoaderObservers involved will be
    * notified with NS_BINDING_ABORTED as the status, possibly synchronously.
    */
-  nsresult Stop(void);
+  void Stop();
 
   /**
    * nsresult Loader::StopLoadingSheet(nsIURI* aURL), which notifies the
    * nsICSSLoaderObserver with NS_BINDING_ABORTED, was removed in Bug 556446.
    * It can be found in revision 2c44a32052ad.
    */
 
   /**
@@ -433,17 +433,17 @@ public:
 
   // These interfaces are public only for the benefit of static functions
   // within nsCSSLoader.cpp.
 
   // IsAlternateSheet can change our currently selected style set if none is
   // selected and aHasAlternateRel is false.
   IsAlternate IsAlternateSheet(const nsAString& aTitle, bool aHasAlternateRel);
 
-  typedef nsTArray<RefPtr<SheetLoadData> > LoadDataArray;
+  typedef nsTArray<RefPtr<SheetLoadData>> LoadDataArray;
 
   // Measure our size.
   size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
 
   // Marks all the sheets at the given URI obsolete, and removes them from the
   // cache.
   nsresult ObsoleteSheet(nsIURI* aURI);
 
@@ -590,17 +590,17 @@ private:
                       mLoadingDatas; // weak refs
     nsDataHashtable<URIPrincipalReferrerPolicyAndCORSModeHashKey, SheetLoadData*>
                       mPendingDatas; // weak refs
   };
   nsAutoPtr<Sheets> mSheets;
 
   // The array of posted stylesheet loaded events (SheetLoadDatas) we have.
   // Note that these are rare.
-  LoadDataArray     mPostedEvents;
+  LoadDataArray mPostedEvents;
 
   // Our array of "global" observers
   nsTObserverArray<nsCOMPtr<nsICSSLoaderObserver> > mObservers;
 
   // This reference is nulled by the Document in it's destructor through
   // DropDocumentReference().
   nsIDocument* MOZ_NON_OWNING_REF mDocument;  // the document we live for