Bug 1037643 - Part 1 - Add ResponsiveImageSelector::SelectImage r=jst
authorJohn Schoenick <jschoenick@mozilla.com>
Tue, 30 Sep 2014 15:32:44 -0700
changeset 212305 987133583ef31ad063b90256ccea948cb78326d0
parent 212304 6345f1767ed7c7a28d204bebccffdef133ba4072
child 212306 4c739aa459fbc6b7e92bab833a07a7cdee154f68
push id27704
push userkwierso@gmail.com
push dateSat, 25 Oct 2014 01:25:30 +0000
treeherdermozilla-central@e37231060eb4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjst
bugs1037643
milestone36.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 1037643 - Part 1 - Add ResponsiveImageSelector::SelectImage r=jst
content/base/src/ResponsiveImageSelector.cpp
content/base/src/ResponsiveImageSelector.h
--- a/content/base/src/ResponsiveImageSelector.cpp
+++ b/content/base/src/ResponsiveImageSelector.cpp
@@ -258,16 +258,29 @@ ResponsiveImageSelector::GetSelectedImag
   int bestIndex = GetBestCandidateIndex();
   if (bestIndex < 0) {
     return 1.0;
   }
 
   return mCandidates[bestIndex].Density(this);
 }
 
+bool
+ResponsiveImageSelector::SelectImage(bool aReselect)
+{
+  if (!aReselect && mBestCandidateIndex != -1) {
+    // Already have selection
+    return false;
+  }
+
+  int oldBest = mBestCandidateIndex;
+  mBestCandidateIndex = -1;
+  return GetBestCandidateIndex() != oldBest;
+}
+
 int
 ResponsiveImageSelector::GetBestCandidateIndex()
 {
   if (mBestCandidateIndex != -1) {
     return mBestCandidateIndex;
   }
 
   int numCandidates = mCandidates.Length();
--- a/content/base/src/ResponsiveImageSelector.h
+++ b/content/base/src/ResponsiveImageSelector.h
@@ -20,36 +20,57 @@ class ResponsiveImageCandidate;
 
 class ResponsiveImageSelector : public nsISupports
 {
   friend class ResponsiveImageCandidate;
 public:
   NS_DECL_ISUPPORTS
   explicit ResponsiveImageSelector(nsIContent* aContent);
 
+  // NOTE ABOUT CURRENT SELECTION
+  //
+  // The best candidate is selected lazily when GetSelectedImage*() is
+  // called, or when SelectImage() is called explicitly. This result
+  // is then cached until either invalidated by further Set*() calls,
+  // or explicitly by replaced by SelectImage(aReselect = true).
+  //
+  // Because the selected image depends on external variants like
+  // viewport size and device pixel ratio, the time at which image
+  // selection occurs can affect the result.
+
+
   // Given a srcset string, parse and replace current candidates (does not
   // replace default source)
   bool SetCandidatesFromSourceSet(const nsAString & aSrcSet);
 
   // Fill the source sizes from a valid sizes descriptor. Returns false if
   // descriptor is invalid.
   bool SetSizesFromDescriptor(const nsAString & aSizesDescriptor);
 
   // Set the default source, treated as the least-precedence 1.0 density source.
   nsresult SetDefaultSource(const nsAString & aSpec);
   void SetDefaultSource(nsIURI *aURL);
 
   uint32_t NumCandidates(bool aIncludeDefault = true);
 
   nsIContent *Content() { return mContent; }
 
-  // Get the URL for the selected best candidate
+  // Get the url and density for the selected best candidate. These
+  // implicitly cause an image to be selected if necessary.
   already_AddRefed<nsIURI> GetSelectedImageURL();
   double GetSelectedImageDensity();
 
+  // Runs image selection now if necessary. If an image has already
+  // been choosen, takes no action unless aReselect is true.
+  //
+  // aReselect - Always re-run selection, replacing the previously
+  //             choosen image.
+  // return - true if the selected image result changed.
+  bool SelectImage(bool aReselect = false);
+
 protected:
   virtual ~ResponsiveImageSelector();
 
 private:
   // Append a candidate unless its selector is duplicated by a higher priority
   // candidate
   void AppendCandidateIfUnique(const ResponsiveImageCandidate &aCandidate);