Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* |
michael@0 | 8 | * representation of media lists used when linking to style sheets or by |
michael@0 | 9 | * @media rules |
michael@0 | 10 | */ |
michael@0 | 11 | |
michael@0 | 12 | #ifndef nsIMediaList_h_ |
michael@0 | 13 | #define nsIMediaList_h_ |
michael@0 | 14 | |
michael@0 | 15 | #include "nsIDOMMediaList.h" |
michael@0 | 16 | #include "nsTArray.h" |
michael@0 | 17 | #include "nsIAtom.h" |
michael@0 | 18 | #include "nsCSSValue.h" |
michael@0 | 19 | #include "nsWrapperCache.h" |
michael@0 | 20 | #include "mozilla/Attributes.h" |
michael@0 | 21 | #include "mozilla/ErrorResult.h" |
michael@0 | 22 | |
michael@0 | 23 | class nsPresContext; |
michael@0 | 24 | class nsCSSStyleSheet; |
michael@0 | 25 | class nsAString; |
michael@0 | 26 | struct nsMediaFeature; |
michael@0 | 27 | |
michael@0 | 28 | struct nsMediaExpression { |
michael@0 | 29 | enum Range { eMin, eMax, eEqual }; |
michael@0 | 30 | |
michael@0 | 31 | const nsMediaFeature *mFeature; |
michael@0 | 32 | Range mRange; |
michael@0 | 33 | nsCSSValue mValue; |
michael@0 | 34 | |
michael@0 | 35 | // aActualValue must be obtained from mFeature->mGetter |
michael@0 | 36 | bool Matches(nsPresContext* aPresContext, |
michael@0 | 37 | const nsCSSValue& aActualValue) const; |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | /** |
michael@0 | 41 | * An nsMediaQueryResultCacheKey records what feature/value combinations |
michael@0 | 42 | * a set of media query results are valid for. This allows the caller |
michael@0 | 43 | * to quickly learn whether a prior result of media query evaluation is |
michael@0 | 44 | * still valid (e.g., due to a window size change) without rerunning all |
michael@0 | 45 | * of the evaluation and rebuilding the list of rules. |
michael@0 | 46 | * |
michael@0 | 47 | * This object may not be used after any media rules in any of the |
michael@0 | 48 | * sheets it was given to have been modified. However, this is |
michael@0 | 49 | * generally not a problem since ClearRuleCascades is called on the |
michael@0 | 50 | * sheet whenever this happens, and these objects are stored inside the |
michael@0 | 51 | * rule cascades. (FIXME: We're not actually doing this all the time.) |
michael@0 | 52 | * |
michael@0 | 53 | * The implementation could be further optimized in the future to store |
michael@0 | 54 | * ranges (combinations of less-than, less-than-or-equal, greater-than, |
michael@0 | 55 | * greater-than-or-equal, equal, not-equal, present, not-present) for |
michael@0 | 56 | * each feature rather than simply storing the list of expressions. |
michael@0 | 57 | * However, this requires combining any such ranges. |
michael@0 | 58 | */ |
michael@0 | 59 | class nsMediaQueryResultCacheKey { |
michael@0 | 60 | public: |
michael@0 | 61 | nsMediaQueryResultCacheKey(nsIAtom* aMedium) |
michael@0 | 62 | : mMedium(aMedium) |
michael@0 | 63 | {} |
michael@0 | 64 | |
michael@0 | 65 | /** |
michael@0 | 66 | * Record that aExpression was tested while building the cached set |
michael@0 | 67 | * that this cache key is for, and that aExpressionMatches was whether |
michael@0 | 68 | * it matched. |
michael@0 | 69 | */ |
michael@0 | 70 | void AddExpression(const nsMediaExpression* aExpression, |
michael@0 | 71 | bool aExpressionMatches); |
michael@0 | 72 | bool Matches(nsPresContext* aPresContext) const; |
michael@0 | 73 | private: |
michael@0 | 74 | struct ExpressionEntry { |
michael@0 | 75 | // FIXME: if we were better at maintaining invariants about clearing |
michael@0 | 76 | // rule cascades when media lists change, this could be a |const |
michael@0 | 77 | // nsMediaExpression*| instead. |
michael@0 | 78 | nsMediaExpression mExpression; |
michael@0 | 79 | bool mExpressionMatches; |
michael@0 | 80 | }; |
michael@0 | 81 | struct FeatureEntry { |
michael@0 | 82 | const nsMediaFeature *mFeature; |
michael@0 | 83 | InfallibleTArray<ExpressionEntry> mExpressions; |
michael@0 | 84 | }; |
michael@0 | 85 | nsCOMPtr<nsIAtom> mMedium; |
michael@0 | 86 | nsTArray<FeatureEntry> mFeatureCache; |
michael@0 | 87 | }; |
michael@0 | 88 | |
michael@0 | 89 | class nsMediaQuery { |
michael@0 | 90 | public: |
michael@0 | 91 | nsMediaQuery() |
michael@0 | 92 | : mNegated(false) |
michael@0 | 93 | , mHasOnly(false) |
michael@0 | 94 | , mTypeOmitted(false) |
michael@0 | 95 | , mHadUnknownExpression(false) |
michael@0 | 96 | { |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | private: |
michael@0 | 100 | // for Clone only |
michael@0 | 101 | nsMediaQuery(const nsMediaQuery& aOther) |
michael@0 | 102 | : mNegated(aOther.mNegated) |
michael@0 | 103 | , mHasOnly(aOther.mHasOnly) |
michael@0 | 104 | , mTypeOmitted(aOther.mTypeOmitted) |
michael@0 | 105 | , mHadUnknownExpression(aOther.mHadUnknownExpression) |
michael@0 | 106 | , mMediaType(aOther.mMediaType) |
michael@0 | 107 | , mExpressions(aOther.mExpressions) |
michael@0 | 108 | { |
michael@0 | 109 | MOZ_ASSERT(mExpressions.Length() == aOther.mExpressions.Length()); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | public: |
michael@0 | 113 | |
michael@0 | 114 | void SetNegated() { mNegated = true; } |
michael@0 | 115 | void SetHasOnly() { mHasOnly = true; } |
michael@0 | 116 | void SetTypeOmitted() { mTypeOmitted = true; } |
michael@0 | 117 | void SetHadUnknownExpression() { mHadUnknownExpression = true; } |
michael@0 | 118 | void SetType(nsIAtom* aMediaType) { |
michael@0 | 119 | NS_ASSERTION(aMediaType, |
michael@0 | 120 | "expected non-null"); |
michael@0 | 121 | mMediaType = aMediaType; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | // Return a new nsMediaExpression in the array for the caller to fill |
michael@0 | 125 | // in. The caller must either fill it in completely, or call |
michael@0 | 126 | // SetHadUnknownExpression on this nsMediaQuery. |
michael@0 | 127 | // Returns null on out-of-memory. |
michael@0 | 128 | nsMediaExpression* NewExpression() { return mExpressions.AppendElement(); } |
michael@0 | 129 | |
michael@0 | 130 | void AppendToString(nsAString& aString) const; |
michael@0 | 131 | |
michael@0 | 132 | nsMediaQuery* Clone() const; |
michael@0 | 133 | |
michael@0 | 134 | // Does this query apply to the presentation? |
michael@0 | 135 | // If |aKey| is non-null, add cache information to it. |
michael@0 | 136 | bool Matches(nsPresContext* aPresContext, |
michael@0 | 137 | nsMediaQueryResultCacheKey* aKey) const; |
michael@0 | 138 | |
michael@0 | 139 | private: |
michael@0 | 140 | bool mNegated; |
michael@0 | 141 | bool mHasOnly; // only needed for serialization |
michael@0 | 142 | bool mTypeOmitted; // only needed for serialization |
michael@0 | 143 | bool mHadUnknownExpression; |
michael@0 | 144 | nsCOMPtr<nsIAtom> mMediaType; |
michael@0 | 145 | nsTArray<nsMediaExpression> mExpressions; |
michael@0 | 146 | }; |
michael@0 | 147 | |
michael@0 | 148 | class nsMediaList MOZ_FINAL : public nsIDOMMediaList |
michael@0 | 149 | , public nsWrapperCache |
michael@0 | 150 | { |
michael@0 | 151 | public: |
michael@0 | 152 | typedef mozilla::ErrorResult ErrorResult; |
michael@0 | 153 | |
michael@0 | 154 | nsMediaList(); |
michael@0 | 155 | |
michael@0 | 156 | virtual JSObject* |
michael@0 | 157 | WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
michael@0 | 158 | nsISupports* GetParentObject() const |
michael@0 | 159 | { |
michael@0 | 160 | return nullptr; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
michael@0 | 164 | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(nsMediaList) |
michael@0 | 165 | |
michael@0 | 166 | NS_DECL_NSIDOMMEDIALIST |
michael@0 | 167 | |
michael@0 | 168 | void GetText(nsAString& aMediaText); |
michael@0 | 169 | void SetText(const nsAString& aMediaText); |
michael@0 | 170 | |
michael@0 | 171 | // Does this query apply to the presentation? |
michael@0 | 172 | // If |aKey| is non-null, add cache information to it. |
michael@0 | 173 | bool Matches(nsPresContext* aPresContext, |
michael@0 | 174 | nsMediaQueryResultCacheKey* aKey); |
michael@0 | 175 | |
michael@0 | 176 | nsresult SetStyleSheet(nsCSSStyleSheet* aSheet); |
michael@0 | 177 | void AppendQuery(nsAutoPtr<nsMediaQuery>& aQuery) { |
michael@0 | 178 | // Takes ownership of aQuery |
michael@0 | 179 | mArray.AppendElement(aQuery.forget()); |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | already_AddRefed<nsMediaList> Clone(); |
michael@0 | 183 | |
michael@0 | 184 | nsMediaQuery* MediumAt(int32_t aIndex) { return mArray[aIndex]; } |
michael@0 | 185 | void Clear() { mArray.Clear(); } |
michael@0 | 186 | |
michael@0 | 187 | // WebIDL |
michael@0 | 188 | // XPCOM GetMediaText and SetMediaText are fine. |
michael@0 | 189 | uint32_t Length() { return mArray.Length(); } |
michael@0 | 190 | void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aReturn); |
michael@0 | 191 | // XPCOM Item is fine. |
michael@0 | 192 | void DeleteMedium(const nsAString& aMedium, ErrorResult& aRv) |
michael@0 | 193 | { |
michael@0 | 194 | aRv = DeleteMedium(aMedium); |
michael@0 | 195 | } |
michael@0 | 196 | void AppendMedium(const nsAString& aMedium, ErrorResult& aRv) |
michael@0 | 197 | { |
michael@0 | 198 | aRv = AppendMedium(aMedium); |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | protected: |
michael@0 | 202 | ~nsMediaList(); |
michael@0 | 203 | |
michael@0 | 204 | nsresult Delete(const nsAString & aOldMedium); |
michael@0 | 205 | nsresult Append(const nsAString & aOldMedium); |
michael@0 | 206 | |
michael@0 | 207 | InfallibleTArray<nsAutoPtr<nsMediaQuery> > mArray; |
michael@0 | 208 | // not refcounted; sheet will let us know when it goes away |
michael@0 | 209 | // mStyleSheet is the sheet that needs to be dirtied when this medialist |
michael@0 | 210 | // changes |
michael@0 | 211 | nsCSSStyleSheet* mStyleSheet; |
michael@0 | 212 | }; |
michael@0 | 213 | #endif /* !defined(nsIMediaList_h_) */ |