Bug 1067345 - Part 1.2 - ResponsiveImageSelector: Rename best candidate -> selected candidate, add clear function
authorJohn Schoenick <jschoenick@mozilla.com>
Wed, 10 Dec 2014 18:53:00 -0500
changeset 229463 4cc6d580cda6eb3d8f9e908cd0dfe0deb8b2560b
parent 229462 afeff812b5dda8dcbf3628fab5a53fbd0d5be564
child 229464 7884f103f1e7b1bd87ab535c8430a5f91cfdf22c
push id11352
push userryanvm@gmail.com
push dateTue, 17 Feb 2015 19:29:37 +0000
treeherderfx-team@b6c56fab513d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1067345
milestone38.0a1
Bug 1067345 - Part 1.2 - ResponsiveImageSelector: Rename best candidate -> selected candidate, add clear function
dom/base/ResponsiveImageSelector.cpp
dom/base/ResponsiveImageSelector.h
--- a/dom/base/ResponsiveImageSelector.cpp
+++ b/dom/base/ResponsiveImageSelector.cpp
@@ -39,27 +39,29 @@ ParseInteger(const nsAString& aString, i
   return !(parseResult &
            ( nsContentUtils::eParseHTMLInteger_Error |
              nsContentUtils::eParseHTMLInteger_DidNotConsumeAllInput |
              nsContentUtils::eParseHTMLInteger_IsPercent ));
 }
 
 ResponsiveImageSelector::ResponsiveImageSelector(nsIContent *aContent)
   : mContent(aContent),
-    mBestCandidateIndex(-1)
+    mSelectedCandidateIndex(-1)
 {
 }
 
 ResponsiveImageSelector::~ResponsiveImageSelector()
 {}
 
 // http://www.whatwg.org/specs/web-apps/current-work/#processing-the-image-candidates
 bool
 ResponsiveImageSelector::SetCandidatesFromSourceSet(const nsAString & aSrcSet)
 {
+  ClearSelectedCandidate();
+
   nsIDocument* doc = mContent ? mContent->OwnerDoc() : nullptr;
   nsCOMPtr<nsIURI> docBaseURI = mContent ? mContent->GetBaseURI() : nullptr;
 
   if (!mContent || !doc || !docBaseURI) {
     MOZ_ASSERT(false,
                "Should not be parsing SourceSet without a content and document");
     return false;
   }
@@ -176,38 +178,43 @@ ResponsiveImageSelector::NumCandidates(b
   }
 
   return candidates;
 }
 
 void
 ResponsiveImageSelector::SetDefaultSource(nsIURI *aURL)
 {
+  ClearSelectedCandidate();
+
   // Check if the last element of our candidates is a default
   int32_t candidates = mCandidates.Length();
   if (candidates && (mCandidates[candidates - 1].Type() ==
                      ResponsiveImageCandidate::eCandidateType_Default)) {
     mCandidates.RemoveElementAt(candidates - 1);
-    if (mBestCandidateIndex == candidates - 1) {
-      mBestCandidateIndex = -1;
-    }
   }
 
   // Add new default if set
   if (aURL) {
     AppendDefaultCandidate(aURL);
   }
 }
 
+void
+ResponsiveImageSelector::ClearSelectedCandidate()
+{
+  mSelectedCandidateIndex = -1;
+}
+
 bool
 ResponsiveImageSelector::SetSizesFromDescriptor(const nsAString & aSizes)
 {
+  ClearSelectedCandidate();
   mSizeQueries.Clear();
   mSizeValues.Clear();
-  mBestCandidateIndex = -1;
 
   nsCSSParser cssParser;
 
   if (!cssParser.ParseSourceSizeList(aSizes, nullptr, 0,
                                      mSizeQueries, mSizeValues, true)) {
     return false;
   }
 
@@ -228,68 +235,66 @@ ResponsiveImageSelector::AppendCandidate
 
   // Discard candidates with identical parameters, they will never match
   for (int i = 0; i < numCandidates; i++) {
     if (mCandidates[i].HasSameParameter(aCandidate)) {
       return;
     }
   }
 
-  mBestCandidateIndex = -1;
   mCandidates.AppendElement(aCandidate);
 }
 
 void
 ResponsiveImageSelector::AppendDefaultCandidate(nsIURI *aURL)
 {
   NS_ENSURE_TRUE(aURL, /* void */);
 
   ResponsiveImageCandidate defaultCandidate;
   defaultCandidate.SetParameterDefault();
   defaultCandidate.SetURL(aURL);
   // We don't use MaybeAppend since we want to keep this even if it can never
   // match, as it may if the source set changes.
-  mBestCandidateIndex = -1;
   mCandidates.AppendElement(defaultCandidate);
 }
 
 already_AddRefed<nsIURI>
 ResponsiveImageSelector::GetSelectedImageURL()
 {
-  int bestIndex = GetBestCandidateIndex();
+  int bestIndex = GetSelectedCandidateIndex();
   if (bestIndex < 0) {
     return nullptr;
   }
 
   nsCOMPtr<nsIURI> bestURL = mCandidates[bestIndex].URL();
   MOZ_ASSERT(bestURL, "Shouldn't have candidates with no URL in the array");
   return bestURL.forget();
 }
 
 double
 ResponsiveImageSelector::GetSelectedImageDensity()
 {
-  int bestIndex = GetBestCandidateIndex();
+  int bestIndex = GetSelectedCandidateIndex();
   if (bestIndex < 0) {
     return 1.0;
   }
 
   return mCandidates[bestIndex].Density(this);
 }
 
 bool
 ResponsiveImageSelector::SelectImage(bool aReselect)
 {
-  if (!aReselect && mBestCandidateIndex != -1) {
+  if (!aReselect && mSelectedCandidateIndex != -1) {
     // Already have selection
     return false;
   }
 
-  int oldBest = mBestCandidateIndex;
-  mBestCandidateIndex = -1;
+  int oldBest = mSelectedCandidateIndex;
+  ClearSelectedCandidate();
 
   int numCandidates = mCandidates.Length();
   if (!numCandidates) {
     return oldBest != -1;
   }
 
   nsIDocument* doc = mContent ? mContent->OwnerDoc() : nullptr;
   nsIPresShell *shell = doc ? doc->GetShell() : nullptr;
@@ -339,27 +344,27 @@ ResponsiveImageSelector::SelectImage(boo
         (bestDensity < displayDensity && candidateDensity > bestDensity) ||
         (candidateDensity >= displayDensity && candidateDensity < bestDensity)) {
       bestIndex = i;
       bestDensity = candidateDensity;
     }
   }
 
   MOZ_ASSERT(bestIndex >= 0 && bestIndex < numCandidates);
-  mBestCandidateIndex = bestIndex;
+  mSelectedCandidateIndex = bestIndex;
 
-  return mBestCandidateIndex != oldBest;
+  return mSelectedCandidateIndex != oldBest;
 }
 
 int
-ResponsiveImageSelector::GetBestCandidateIndex()
+ResponsiveImageSelector::GetSelectedCandidateIndex()
 {
   SelectImage();
 
-  return mBestCandidateIndex;
+  return mSelectedCandidateIndex;
 }
 
 bool
 ResponsiveImageSelector::ComputeFinalWidthForCurrentViewport(int32_t *aWidth)
 {
   unsigned int numSizes = mSizeQueries.Length();
   nsIDocument* doc = mContent ? mContent->OwnerDoc() : nullptr;
   nsIPresShell *presShell = doc ? doc->GetShell() : nullptr;
--- a/dom/base/ResponsiveImageSelector.h
+++ b/dom/base/ResponsiveImageSelector.h
@@ -76,31 +76,35 @@ private:
   // Append a candidate unless its selector is duplicated by a higher priority
   // candidate
   void AppendCandidateIfUnique(const ResponsiveImageCandidate &aCandidate);
 
   // Append a default candidate with this URL. Does not check if the array
   // already contains one, use SetDefaultSource instead.
   void AppendDefaultCandidate(nsIURI *aURL);
 
-  // Get index of best candidate
-  int GetBestCandidateIndex();
+  // Get index of selected candidate, triggering selection if necessary.
+  int GetSelectedCandidateIndex();
+
+  // Forget currently selected candidate. (See "NOTE ABOUT CURRENT SELECTION"
+  // above.)
+  void ClearSelectedCandidate();
 
   // Compute a density from a Candidate width. Returns false if sizes were not
   // specified for this selector.
   //
   // aContext is the presContext to use for current viewport sizing, null will
   // use the associated content's context.
   bool ComputeFinalWidthForCurrentViewport(int32_t *aWidth);
 
   nsCOMPtr<nsIContent> mContent;
   // If this array contains an eCandidateType_Default, it should be the last
   // element, such that the Setters can preserve/replace it respectively.
   nsTArray<ResponsiveImageCandidate> mCandidates;
-  int mBestCandidateIndex;
+  int mSelectedCandidateIndex;
 
   nsTArray< nsAutoPtr<nsMediaQuery> > mSizeQueries;
   nsTArray<nsCSSValue> mSizeValues;
 };
 
 class ResponsiveImageCandidate {
 public:
   ResponsiveImageCandidate();