layout/style/nsCSSStyleSheet.h

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

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 // vim:cindent:tabstop=2:expandtab:shiftwidth=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 /* representation of a CSS style sheet */
michael@0 8
michael@0 9 #ifndef nsCSSStyleSheet_h_
michael@0 10 #define nsCSSStyleSheet_h_
michael@0 11
michael@0 12 #include "mozilla/Attributes.h"
michael@0 13 #include "mozilla/MemoryReporting.h"
michael@0 14 #include "mozilla/dom/Element.h"
michael@0 15
michael@0 16 #include "nscore.h"
michael@0 17 #include "nsCOMPtr.h"
michael@0 18 #include "nsAutoPtr.h"
michael@0 19 #include "nsIStyleSheet.h"
michael@0 20 #include "nsIDOMCSSStyleSheet.h"
michael@0 21 #include "nsICSSLoaderObserver.h"
michael@0 22 #include "nsCOMArray.h"
michael@0 23 #include "nsTArray.h"
michael@0 24 #include "nsString.h"
michael@0 25 #include "mozilla/CORSMode.h"
michael@0 26 #include "nsCycleCollectionParticipant.h"
michael@0 27 #include "nsWrapperCache.h"
michael@0 28
michael@0 29 class nsXMLNameSpaceMap;
michael@0 30 class nsCSSRuleProcessor;
michael@0 31 class nsIPrincipal;
michael@0 32 class nsIURI;
michael@0 33 class nsMediaList;
michael@0 34 class nsMediaQueryResultCacheKey;
michael@0 35 class nsCSSStyleSheet;
michael@0 36 class nsPresContext;
michael@0 37
michael@0 38 namespace mozilla {
michael@0 39 namespace css {
michael@0 40 class Rule;
michael@0 41 class GroupRule;
michael@0 42 class ImportRule;
michael@0 43 }
michael@0 44 }
michael@0 45
michael@0 46 // -------------------------------
michael@0 47 // CSS Style Sheet Inner Data Container
michael@0 48 //
michael@0 49
michael@0 50 class nsCSSStyleSheetInner {
michael@0 51 public:
michael@0 52 friend class nsCSSStyleSheet;
michael@0 53 friend class nsCSSRuleProcessor;
michael@0 54 private:
michael@0 55 nsCSSStyleSheetInner(nsCSSStyleSheet* aPrimarySheet,
michael@0 56 mozilla::CORSMode aCORSMode);
michael@0 57 nsCSSStyleSheetInner(nsCSSStyleSheetInner& aCopy,
michael@0 58 nsCSSStyleSheet* aPrimarySheet);
michael@0 59 ~nsCSSStyleSheetInner();
michael@0 60
michael@0 61 nsCSSStyleSheetInner* CloneFor(nsCSSStyleSheet* aPrimarySheet);
michael@0 62 void AddSheet(nsCSSStyleSheet* aSheet);
michael@0 63 void RemoveSheet(nsCSSStyleSheet* aSheet);
michael@0 64
michael@0 65 void RebuildNameSpaces();
michael@0 66
michael@0 67 // Create a new namespace map
michael@0 68 nsresult CreateNamespaceMap();
michael@0 69
michael@0 70 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
michael@0 71
michael@0 72 nsAutoTArray<nsCSSStyleSheet*, 8> mSheets;
michael@0 73 nsCOMPtr<nsIURI> mSheetURI; // for error reports, etc.
michael@0 74 nsCOMPtr<nsIURI> mOriginalSheetURI; // for GetHref. Can be null.
michael@0 75 nsCOMPtr<nsIURI> mBaseURI; // for resolving relative URIs
michael@0 76 nsCOMPtr<nsIPrincipal> mPrincipal;
michael@0 77 nsCOMArray<mozilla::css::Rule> mOrderedRules;
michael@0 78 nsAutoPtr<nsXMLNameSpaceMap> mNameSpaceMap;
michael@0 79 // Linked list of child sheets. This is al fundamentally broken, because
michael@0 80 // each of the child sheets has a unique parent... We can only hope (and
michael@0 81 // currently this is the case) that any time page JS can get ts hands on a
michael@0 82 // child sheet that means we've already ensured unique inners throughout its
michael@0 83 // parent chain and things are good.
michael@0 84 nsRefPtr<nsCSSStyleSheet> mFirstChild;
michael@0 85 mozilla::CORSMode mCORSMode;
michael@0 86 bool mComplete;
michael@0 87
michael@0 88 #ifdef DEBUG
michael@0 89 bool mPrincipalSet;
michael@0 90 #endif
michael@0 91 };
michael@0 92
michael@0 93
michael@0 94 // -------------------------------
michael@0 95 // CSS Style Sheet
michael@0 96 //
michael@0 97
michael@0 98 class CSSRuleListImpl;
michael@0 99
michael@0 100 // CID for the nsCSSStyleSheet class
michael@0 101 // ca926f30-2a7e-477e-8467-803fb32af20a
michael@0 102 #define NS_CSS_STYLE_SHEET_IMPL_CID \
michael@0 103 { 0xca926f30, 0x2a7e, 0x477e, \
michael@0 104 { 0x84, 0x67, 0x80, 0x3f, 0xb3, 0x2a, 0xf2, 0x0a } }
michael@0 105
michael@0 106
michael@0 107 class nsCSSStyleSheet MOZ_FINAL : public nsIStyleSheet,
michael@0 108 public nsIDOMCSSStyleSheet,
michael@0 109 public nsICSSLoaderObserver,
michael@0 110 public nsWrapperCache
michael@0 111 {
michael@0 112 public:
michael@0 113 nsCSSStyleSheet(mozilla::CORSMode aCORSMode);
michael@0 114
michael@0 115 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
michael@0 116 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsCSSStyleSheet,
michael@0 117 nsIStyleSheet)
michael@0 118
michael@0 119 NS_DECLARE_STATIC_IID_ACCESSOR(NS_CSS_STYLE_SHEET_IMPL_CID)
michael@0 120
michael@0 121 // nsIStyleSheet interface
michael@0 122 virtual nsIURI* GetSheetURI() const MOZ_OVERRIDE;
michael@0 123 virtual nsIURI* GetBaseURI() const MOZ_OVERRIDE;
michael@0 124 virtual void GetTitle(nsString& aTitle) const MOZ_OVERRIDE;
michael@0 125 virtual void GetType(nsString& aType) const MOZ_OVERRIDE;
michael@0 126 virtual bool HasRules() const MOZ_OVERRIDE;
michael@0 127 virtual bool IsApplicable() const MOZ_OVERRIDE;
michael@0 128 virtual void SetEnabled(bool aEnabled) MOZ_OVERRIDE;
michael@0 129 virtual bool IsComplete() const MOZ_OVERRIDE;
michael@0 130 virtual void SetComplete() MOZ_OVERRIDE;
michael@0 131 virtual nsIStyleSheet* GetParentSheet() const MOZ_OVERRIDE; // may be null
michael@0 132 virtual nsIDocument* GetOwningDocument() const MOZ_OVERRIDE; // may be null
michael@0 133 virtual void SetOwningDocument(nsIDocument* aDocument) MOZ_OVERRIDE;
michael@0 134
michael@0 135 // Find the ID of the owner inner window.
michael@0 136 uint64_t FindOwningWindowInnerID() const;
michael@0 137 #ifdef DEBUG
michael@0 138 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const MOZ_OVERRIDE;
michael@0 139 #endif
michael@0 140
michael@0 141 void AppendStyleSheet(nsCSSStyleSheet* aSheet);
michael@0 142 void InsertStyleSheetAt(nsCSSStyleSheet* aSheet, int32_t aIndex);
michael@0 143
michael@0 144 // XXX do these belong here or are they generic?
michael@0 145 void PrependStyleRule(mozilla::css::Rule* aRule);
michael@0 146 void AppendStyleRule(mozilla::css::Rule* aRule);
michael@0 147 void ReplaceStyleRule(mozilla::css::Rule* aOld, mozilla::css::Rule* aNew);
michael@0 148
michael@0 149 int32_t StyleRuleCount() const;
michael@0 150 mozilla::css::Rule* GetStyleRuleAt(int32_t aIndex) const;
michael@0 151
michael@0 152 nsresult DeleteRuleFromGroup(mozilla::css::GroupRule* aGroup, uint32_t aIndex);
michael@0 153 nsresult InsertRuleIntoGroup(const nsAString& aRule, mozilla::css::GroupRule* aGroup, uint32_t aIndex, uint32_t* _retval);
michael@0 154 nsresult ReplaceRuleInGroup(mozilla::css::GroupRule* aGroup, mozilla::css::Rule* aOld, mozilla::css::Rule* aNew);
michael@0 155
michael@0 156 int32_t StyleSheetCount() const;
michael@0 157
michael@0 158 /**
michael@0 159 * SetURIs must be called on all sheets before parsing into them.
michael@0 160 * SetURIs may only be called while the sheet is 1) incomplete and 2)
michael@0 161 * has no rules in it
michael@0 162 */
michael@0 163 void SetURIs(nsIURI* aSheetURI, nsIURI* aOriginalSheetURI, nsIURI* aBaseURI);
michael@0 164
michael@0 165 /**
michael@0 166 * SetPrincipal should be called on all sheets before parsing into them.
michael@0 167 * This can only be called once with a non-null principal. Calling this with
michael@0 168 * a null pointer is allowed and is treated as a no-op.
michael@0 169 */
michael@0 170 void SetPrincipal(nsIPrincipal* aPrincipal);
michael@0 171
michael@0 172 // Principal() never returns a null pointer.
michael@0 173 nsIPrincipal* Principal() const { return mInner->mPrincipal; }
michael@0 174
michael@0 175 // The document this style sheet is associated with. May be null
michael@0 176 nsIDocument* GetDocument() const { return mDocument; }
michael@0 177
michael@0 178 void SetTitle(const nsAString& aTitle) { mTitle = aTitle; }
michael@0 179 void SetMedia(nsMediaList* aMedia);
michael@0 180 void SetOwningNode(nsINode* aOwningNode) { mOwningNode = aOwningNode; /* Not ref counted */ }
michael@0 181
michael@0 182 void SetOwnerRule(mozilla::css::ImportRule* aOwnerRule) { mOwnerRule = aOwnerRule; /* Not ref counted */ }
michael@0 183 mozilla::css::ImportRule* GetOwnerRule() const { return mOwnerRule; }
michael@0 184
michael@0 185 nsXMLNameSpaceMap* GetNameSpaceMap() const { return mInner->mNameSpaceMap; }
michael@0 186
michael@0 187 already_AddRefed<nsCSSStyleSheet> Clone(nsCSSStyleSheet* aCloneParent,
michael@0 188 mozilla::css::ImportRule* aCloneOwnerRule,
michael@0 189 nsIDocument* aCloneDocument,
michael@0 190 nsINode* aCloneOwningNode) const;
michael@0 191
michael@0 192 bool IsModified() const { return mDirty; }
michael@0 193
michael@0 194 void SetModifiedByChildRule() {
michael@0 195 NS_ASSERTION(mDirty,
michael@0 196 "sheet must be marked dirty before handing out child rules");
michael@0 197 DidDirty();
michael@0 198 }
michael@0 199
michael@0 200 nsresult AddRuleProcessor(nsCSSRuleProcessor* aProcessor);
michael@0 201 nsresult DropRuleProcessor(nsCSSRuleProcessor* aProcessor);
michael@0 202
michael@0 203 /**
michael@0 204 * Like the DOM insertRule() method, but doesn't do any security checks
michael@0 205 */
michael@0 206 nsresult InsertRuleInternal(const nsAString& aRule,
michael@0 207 uint32_t aIndex, uint32_t* aReturn);
michael@0 208
michael@0 209 /* Get the URI this sheet was originally loaded from, if any. Can
michael@0 210 return null */
michael@0 211 virtual nsIURI* GetOriginalURI() const;
michael@0 212
michael@0 213 // nsICSSLoaderObserver interface
michael@0 214 NS_IMETHOD StyleSheetLoaded(nsCSSStyleSheet* aSheet, bool aWasAlternate,
michael@0 215 nsresult aStatus) MOZ_OVERRIDE;
michael@0 216
michael@0 217 enum EnsureUniqueInnerResult {
michael@0 218 // No work was needed to ensure a unique inner.
michael@0 219 eUniqueInner_AlreadyUnique,
michael@0 220 // A clone was done to ensure a unique inner (which means the style
michael@0 221 // rules in this sheet have changed).
michael@0 222 eUniqueInner_ClonedInner
michael@0 223 };
michael@0 224 EnsureUniqueInnerResult EnsureUniqueInner();
michael@0 225
michael@0 226 // Append all of this sheet's child sheets to aArray.
michael@0 227 void AppendAllChildSheets(nsTArray<nsCSSStyleSheet*>& aArray);
michael@0 228
michael@0 229 bool UseForPresentation(nsPresContext* aPresContext,
michael@0 230 nsMediaQueryResultCacheKey& aKey) const;
michael@0 231
michael@0 232 nsresult ParseSheet(const nsAString& aInput);
michael@0 233
michael@0 234 // nsIDOMStyleSheet interface
michael@0 235 NS_DECL_NSIDOMSTYLESHEET
michael@0 236
michael@0 237 // nsIDOMCSSStyleSheet interface
michael@0 238 NS_DECL_NSIDOMCSSSTYLESHEET
michael@0 239
michael@0 240 // Function used as a callback to rebuild our inner's child sheet
michael@0 241 // list after we clone a unique inner for ourselves.
michael@0 242 static bool RebuildChildList(mozilla::css::Rule* aRule, void* aBuilder);
michael@0 243
michael@0 244 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE;
michael@0 245
michael@0 246 // Get this style sheet's CORS mode
michael@0 247 mozilla::CORSMode GetCORSMode() const { return mInner->mCORSMode; }
michael@0 248
michael@0 249 mozilla::dom::Element* GetScopeElement() const { return mScopeElement; }
michael@0 250 void SetScopeElement(mozilla::dom::Element* aScopeElement)
michael@0 251 {
michael@0 252 mScopeElement = aScopeElement;
michael@0 253 }
michael@0 254
michael@0 255 // WebIDL StyleSheet API
michael@0 256 // Our nsIStyleSheet::GetType is a const method, so it ends up
michael@0 257 // ambiguous with with the XPCOM version. Just disambiguate.
michael@0 258 void GetType(nsString& aType) {
michael@0 259 const_cast<const nsCSSStyleSheet*>(this)->GetType(aType);
michael@0 260 }
michael@0 261 // Our XPCOM GetHref is fine for WebIDL
michael@0 262 nsINode* GetOwnerNode() const { return mOwningNode; }
michael@0 263 nsCSSStyleSheet* GetParentStyleSheet() const { return mParent; }
michael@0 264 // Our nsIStyleSheet::GetTitle is a const method, so it ends up
michael@0 265 // ambiguous with with the XPCOM version. Just disambiguate.
michael@0 266 void GetTitle(nsString& aTitle) {
michael@0 267 const_cast<const nsCSSStyleSheet*>(this)->GetTitle(aTitle);
michael@0 268 }
michael@0 269 nsMediaList* Media();
michael@0 270 bool Disabled() const { return mDisabled; }
michael@0 271 // The XPCOM SetDisabled is fine for WebIDL
michael@0 272
michael@0 273 // WebIDL CSSStyleSheet API
michael@0 274 // Can't be inline because we can't include ImportRule here. And can't be
michael@0 275 // called GetOwnerRule because that would be ambiguous with the ImportRule
michael@0 276 // version.
michael@0 277 nsIDOMCSSRule* GetDOMOwnerRule() const;
michael@0 278 nsIDOMCSSRuleList* GetCssRules(mozilla::ErrorResult& aRv);
michael@0 279 uint32_t InsertRule(const nsAString& aRule, uint32_t aIndex,
michael@0 280 mozilla::ErrorResult& aRv) {
michael@0 281 uint32_t retval;
michael@0 282 aRv = InsertRule(aRule, aIndex, &retval);
michael@0 283 return retval;
michael@0 284 }
michael@0 285 void DeleteRule(uint32_t aIndex, mozilla::ErrorResult& aRv) {
michael@0 286 aRv = DeleteRule(aIndex);
michael@0 287 }
michael@0 288
michael@0 289 // WebIDL miscellaneous bits
michael@0 290 mozilla::dom::ParentObject GetParentObject() const {
michael@0 291 if (mOwningNode) {
michael@0 292 return mozilla::dom::ParentObject(mOwningNode);
michael@0 293 }
michael@0 294
michael@0 295 return mozilla::dom::ParentObject(static_cast<nsIStyleSheet*>(mParent),
michael@0 296 mParent);
michael@0 297 }
michael@0 298 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
michael@0 299
michael@0 300 private:
michael@0 301 nsCSSStyleSheet(const nsCSSStyleSheet& aCopy,
michael@0 302 nsCSSStyleSheet* aParentToUse,
michael@0 303 mozilla::css::ImportRule* aOwnerRuleToUse,
michael@0 304 nsIDocument* aDocumentToUse,
michael@0 305 nsINode* aOwningNodeToUse);
michael@0 306
michael@0 307 nsCSSStyleSheet(const nsCSSStyleSheet& aCopy) MOZ_DELETE;
michael@0 308 nsCSSStyleSheet& operator=(const nsCSSStyleSheet& aCopy) MOZ_DELETE;
michael@0 309
michael@0 310 protected:
michael@0 311 virtual ~nsCSSStyleSheet();
michael@0 312
michael@0 313 void ClearRuleCascades();
michael@0 314
michael@0 315 void WillDirty();
michael@0 316 void DidDirty();
michael@0 317
michael@0 318 // Return success if the subject principal subsumes the principal of our
michael@0 319 // inner, error otherwise. This will also succeed if the subject has
michael@0 320 // UniversalXPConnect or if access is allowed by CORS. In the latter case,
michael@0 321 // it will set the principal of the inner to the subject principal.
michael@0 322 nsresult SubjectSubsumesInnerPrincipal();
michael@0 323
michael@0 324 // Add the namespace mapping from this @namespace rule to our namespace map
michael@0 325 nsresult RegisterNamespaceRule(mozilla::css::Rule* aRule);
michael@0 326
michael@0 327 // Drop our reference to mRuleCollection
michael@0 328 void DropRuleCollection();
michael@0 329
michael@0 330 // Drop our reference to mMedia
michael@0 331 void DropMedia();
michael@0 332
michael@0 333 // Unlink our inner, if needed, for cycle collection
michael@0 334 void UnlinkInner();
michael@0 335 // Traverse our inner, if needed, for cycle collection
michael@0 336 void TraverseInner(nsCycleCollectionTraversalCallback &);
michael@0 337
michael@0 338 protected:
michael@0 339 nsString mTitle;
michael@0 340 nsRefPtr<nsMediaList> mMedia;
michael@0 341 nsRefPtr<nsCSSStyleSheet> mNext;
michael@0 342 nsCSSStyleSheet* mParent; // weak ref
michael@0 343 mozilla::css::ImportRule* mOwnerRule; // weak ref
michael@0 344
michael@0 345 nsRefPtr<CSSRuleListImpl> mRuleCollection;
michael@0 346 nsIDocument* mDocument; // weak ref; parents maintain this for their children
michael@0 347 nsINode* mOwningNode; // weak ref
michael@0 348 bool mDisabled;
michael@0 349 bool mDirty; // has been modified
michael@0 350 nsRefPtr<mozilla::dom::Element> mScopeElement;
michael@0 351
michael@0 352 nsCSSStyleSheetInner* mInner;
michael@0 353
michael@0 354 nsAutoTArray<nsCSSRuleProcessor*, 8>* mRuleProcessors;
michael@0 355
michael@0 356 friend class nsMediaList;
michael@0 357 friend class nsCSSRuleProcessor;
michael@0 358 friend struct ChildSheetListBuilder;
michael@0 359 };
michael@0 360
michael@0 361 NS_DEFINE_STATIC_IID_ACCESSOR(nsCSSStyleSheet, NS_CSS_STYLE_SHEET_IMPL_CID)
michael@0 362
michael@0 363 #endif /* !defined(nsCSSStyleSheet_h_) */

mercurial