2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-05-03 03:25:48 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_responsiveimageselector_h__
|
|
|
|
#define mozilla_dom_responsiveimageselector_h__
|
|
|
|
|
2017-11-13 08:44:12 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
#include "mozilla/ServoBindingTypes.h"
|
2014-05-03 03:25:48 +04:00
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsString.h"
|
2014-10-14 00:36:01 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2014-05-03 03:25:48 +04:00
|
|
|
|
2014-05-01 03:19:54 +04:00
|
|
|
class nsMediaQuery;
|
|
|
|
class nsCSSValue;
|
|
|
|
|
2014-05-03 03:25:48 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class ResponsiveImageCandidate;
|
|
|
|
|
2014-10-14 00:36:01 +04:00
|
|
|
class ResponsiveImageSelector {
|
2014-05-01 03:19:54 +04:00
|
|
|
friend class ResponsiveImageCandidate;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-05-03 03:25:48 +04:00
|
|
|
public:
|
2014-10-14 00:36:01 +04:00
|
|
|
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(ResponsiveImageSelector)
|
|
|
|
NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(ResponsiveImageSelector)
|
|
|
|
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit ResponsiveImageSelector(nsIContent* aContent);
|
2019-01-02 16:05:23 +03:00
|
|
|
explicit ResponsiveImageSelector(dom::Document* aDocument);
|
2014-05-03 03:25:48 +04:00
|
|
|
|
2014-10-01 02:32:44 +04:00
|
|
|
// 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.
|
|
|
|
|
2014-05-03 03:25:48 +04:00
|
|
|
// Given a srcset string, parse and replace current candidates (does not
|
|
|
|
// replace default source)
|
2017-10-03 07:30:34 +03:00
|
|
|
bool SetCandidatesFromSourceSet(const nsAString& aSrcSet,
|
|
|
|
nsIPrincipal* aTriggeringPrincipal = nullptr);
|
2014-05-03 03:25:48 +04:00
|
|
|
|
2014-05-01 03:19:54 +04:00
|
|
|
// Fill the source sizes from a valid sizes descriptor. Returns false if
|
|
|
|
// descriptor is invalid.
|
|
|
|
bool SetSizesFromDescriptor(const nsAString& aSizesDescriptor);
|
|
|
|
|
2014-05-03 03:25:48 +04:00
|
|
|
// Set the default source, treated as the least-precedence 1.0 density source.
|
2017-10-03 07:30:34 +03:00
|
|
|
void SetDefaultSource(const nsAString& aURLString,
|
|
|
|
nsIPrincipal* aPrincipal = nullptr);
|
2014-05-03 03:25:48 +04:00
|
|
|
|
2014-05-03 01:32:20 +04:00
|
|
|
uint32_t NumCandidates(bool aIncludeDefault = true);
|
|
|
|
|
2014-12-11 02:53:00 +03:00
|
|
|
// If this was created for a specific content. May be null if we were only
|
|
|
|
// created for a document.
|
|
|
|
nsIContent* Content();
|
|
|
|
|
|
|
|
// The document we were created for, or the owner document of the content if
|
|
|
|
// we were created for a specific nsIContent.
|
2019-01-02 16:05:23 +03:00
|
|
|
dom::Document* Document();
|
2014-05-03 01:32:20 +04:00
|
|
|
|
2014-10-01 02:32:44 +04:00
|
|
|
// Get the url and density for the selected best candidate. These
|
|
|
|
// implicitly cause an image to be selected if necessary.
|
2014-05-03 03:25:48 +04:00
|
|
|
already_AddRefed<nsIURI> GetSelectedImageURL();
|
2014-12-11 02:53:00 +03:00
|
|
|
// Returns false if there is no selected image
|
|
|
|
bool GetSelectedImageURLSpec(nsAString& aResult);
|
2014-05-03 03:25:48 +04:00
|
|
|
double GetSelectedImageDensity();
|
2017-10-03 07:30:34 +03:00
|
|
|
nsIPrincipal* GetSelectedImageTriggeringPrincipal();
|
2014-05-03 03:25:48 +04:00
|
|
|
|
2014-10-01 02:32:44 +04:00
|
|
|
// 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);
|
|
|
|
|
2014-06-25 06:09:15 +04:00
|
|
|
protected:
|
|
|
|
virtual ~ResponsiveImageSelector();
|
|
|
|
|
2014-05-03 03:25:48 +04:00
|
|
|
private:
|
|
|
|
// Append a candidate unless its selector is duplicated by a higher priority
|
|
|
|
// candidate
|
|
|
|
void AppendCandidateIfUnique(const ResponsiveImageCandidate& aCandidate);
|
|
|
|
|
2016-03-30 07:39:05 +03:00
|
|
|
// Append a default candidate with this URL if necessary. Does not check if
|
|
|
|
// the array already contains one, use SetDefaultSource instead.
|
|
|
|
void MaybeAppendDefaultCandidate();
|
2014-05-03 03:25:48 +04:00
|
|
|
|
2014-12-11 02:53:00 +03:00
|
|
|
// Get index of selected candidate, triggering selection if necessary.
|
|
|
|
int GetSelectedCandidateIndex();
|
|
|
|
|
|
|
|
// Forget currently selected candidate. (See "NOTE ABOUT CURRENT SELECTION"
|
|
|
|
// above.)
|
|
|
|
void ClearSelectedCandidate();
|
2014-05-03 03:25:48 +04:00
|
|
|
|
2014-05-01 03:19:54 +04:00
|
|
|
// 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.
|
2016-03-23 13:46:36 +03:00
|
|
|
bool ComputeFinalWidthForCurrentViewport(double* aWidth);
|
2014-05-01 03:19:54 +04:00
|
|
|
|
2014-12-11 02:53:00 +03:00
|
|
|
nsCOMPtr<nsINode> mOwnerNode;
|
2016-03-30 07:39:05 +03:00
|
|
|
// The cached URL for default candidate.
|
|
|
|
nsString mDefaultSourceURL;
|
2017-10-03 07:30:34 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> mDefaultSourceTriggeringPrincipal;
|
2014-05-03 03:25:48 +04:00
|
|
|
// 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;
|
2014-12-11 02:53:00 +03:00
|
|
|
int mSelectedCandidateIndex;
|
2014-12-11 02:53:00 +03:00
|
|
|
// The cached resolved URL for mSelectedCandidateIndex, such that we only
|
|
|
|
// resolve the absolute URL at selection time
|
|
|
|
nsCOMPtr<nsIURI> mSelectedCandidateURL;
|
2014-05-01 03:19:54 +04:00
|
|
|
|
2017-11-13 08:44:12 +03:00
|
|
|
// Servo bits.
|
|
|
|
UniquePtr<RawServoSourceSizeList> mServoSourceSizeList;
|
2014-05-03 03:25:48 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class ResponsiveImageCandidate {
|
|
|
|
public:
|
|
|
|
ResponsiveImageCandidate();
|
2017-10-03 07:30:34 +03:00
|
|
|
ResponsiveImageCandidate(const nsAString& aURLString, double aDensity,
|
|
|
|
nsIPrincipal* aTriggeringPrincipal = nullptr);
|
2014-05-03 03:25:48 +04:00
|
|
|
|
2014-12-11 02:53:00 +03:00
|
|
|
void SetURLSpec(const nsAString& aURLString);
|
2017-10-03 07:30:34 +03:00
|
|
|
void SetTriggeringPrincipal(nsIPrincipal* aPrincipal);
|
2014-05-03 03:25:48 +04:00
|
|
|
// Set this as a default-candidate. This behaves the same as density 1.0, but
|
|
|
|
// has a differing type such that it can be replaced by subsequent
|
|
|
|
// SetDefaultSource calls.
|
|
|
|
void SetParameterDefault();
|
|
|
|
|
|
|
|
// Set this candidate as a by-density candidate with specified density.
|
|
|
|
void SetParameterAsDensity(double aDensity);
|
2014-04-15 03:49:50 +04:00
|
|
|
void SetParameterAsComputedWidth(int32_t aWidth);
|
2014-05-03 03:25:48 +04:00
|
|
|
|
2014-12-11 02:50:00 +03:00
|
|
|
void SetParameterInvalid();
|
|
|
|
|
|
|
|
// Consume descriptors from a string defined by aIter and aIterEnd, adjusts
|
|
|
|
// aIter to the end of data consumed.
|
|
|
|
// Returns false if descriptors string is invalid, but still parses to the end
|
|
|
|
// of descriptors microsyntax.
|
|
|
|
bool ConsumeDescriptors(nsAString::const_iterator& aIter,
|
|
|
|
const nsAString::const_iterator& aIterEnd);
|
2014-05-03 03:25:48 +04:00
|
|
|
|
|
|
|
// Check if our parameter (which does not include the url) is identical
|
|
|
|
bool HasSameParameter(const ResponsiveImageCandidate& aOther) const;
|
|
|
|
|
2014-12-11 02:53:00 +03:00
|
|
|
const nsAString& URLString() const;
|
2017-10-03 07:30:34 +03:00
|
|
|
nsIPrincipal* TriggeringPrincipal() const;
|
2014-05-01 03:19:54 +04:00
|
|
|
|
|
|
|
// Compute and return the density relative to a selector.
|
|
|
|
double Density(ResponsiveImageSelector* aSelector) const;
|
|
|
|
// If the width is already known. Useful when iterating over candidates to
|
|
|
|
// avoid having each call re-compute the width.
|
2016-03-23 13:46:36 +03:00
|
|
|
double Density(double aMatchingWidth) const;
|
2014-05-01 03:19:54 +04:00
|
|
|
|
|
|
|
// If this selector is computed from the selector's matching width.
|
|
|
|
bool IsComputedFromWidth() const;
|
2014-05-03 03:25:48 +04:00
|
|
|
|
|
|
|
enum eCandidateType {
|
|
|
|
eCandidateType_Invalid,
|
|
|
|
eCandidateType_Density,
|
|
|
|
// Treated as 1.0 density, but a separate type so we can update the
|
|
|
|
// responsive candidates and default separately
|
|
|
|
eCandidateType_Default,
|
2014-04-15 03:49:50 +04:00
|
|
|
eCandidateType_ComputedFromWidth
|
2014-05-03 03:25:48 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
eCandidateType Type() const { return mType; }
|
|
|
|
|
|
|
|
private:
|
2014-12-11 02:53:00 +03:00
|
|
|
nsString mURLString;
|
2017-10-03 07:30:34 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> mTriggeringPrincipal;
|
2014-05-03 03:25:48 +04:00
|
|
|
eCandidateType mType;
|
|
|
|
union {
|
|
|
|
double mDensity;
|
2014-04-15 03:49:50 +04:00
|
|
|
int32_t mWidth;
|
2014-05-03 03:25:48 +04:00
|
|
|
} mValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_responsiveimageselector_h__
|