michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: // vim:cindent:tabstop=2:expandtab:shiftwidth=2: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* representation of a CSS style sheet */ michael@0: michael@0: #ifndef nsCSSStyleSheet_h_ michael@0: #define nsCSSStyleSheet_h_ michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: #include "mozilla/dom/Element.h" michael@0: michael@0: #include "nscore.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsIStyleSheet.h" michael@0: #include "nsIDOMCSSStyleSheet.h" michael@0: #include "nsICSSLoaderObserver.h" michael@0: #include "nsCOMArray.h" michael@0: #include "nsTArray.h" michael@0: #include "nsString.h" michael@0: #include "mozilla/CORSMode.h" michael@0: #include "nsCycleCollectionParticipant.h" michael@0: #include "nsWrapperCache.h" michael@0: michael@0: class nsXMLNameSpaceMap; michael@0: class nsCSSRuleProcessor; michael@0: class nsIPrincipal; michael@0: class nsIURI; michael@0: class nsMediaList; michael@0: class nsMediaQueryResultCacheKey; michael@0: class nsCSSStyleSheet; michael@0: class nsPresContext; michael@0: michael@0: namespace mozilla { michael@0: namespace css { michael@0: class Rule; michael@0: class GroupRule; michael@0: class ImportRule; michael@0: } michael@0: } michael@0: michael@0: // ------------------------------- michael@0: // CSS Style Sheet Inner Data Container michael@0: // michael@0: michael@0: class nsCSSStyleSheetInner { michael@0: public: michael@0: friend class nsCSSStyleSheet; michael@0: friend class nsCSSRuleProcessor; michael@0: private: michael@0: nsCSSStyleSheetInner(nsCSSStyleSheet* aPrimarySheet, michael@0: mozilla::CORSMode aCORSMode); michael@0: nsCSSStyleSheetInner(nsCSSStyleSheetInner& aCopy, michael@0: nsCSSStyleSheet* aPrimarySheet); michael@0: ~nsCSSStyleSheetInner(); michael@0: michael@0: nsCSSStyleSheetInner* CloneFor(nsCSSStyleSheet* aPrimarySheet); michael@0: void AddSheet(nsCSSStyleSheet* aSheet); michael@0: void RemoveSheet(nsCSSStyleSheet* aSheet); michael@0: michael@0: void RebuildNameSpaces(); michael@0: michael@0: // Create a new namespace map michael@0: nsresult CreateNamespaceMap(); michael@0: michael@0: size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; michael@0: michael@0: nsAutoTArray mSheets; michael@0: nsCOMPtr mSheetURI; // for error reports, etc. michael@0: nsCOMPtr mOriginalSheetURI; // for GetHref. Can be null. michael@0: nsCOMPtr mBaseURI; // for resolving relative URIs michael@0: nsCOMPtr mPrincipal; michael@0: nsCOMArray mOrderedRules; michael@0: nsAutoPtr mNameSpaceMap; michael@0: // Linked list of child sheets. This is al fundamentally broken, because michael@0: // each of the child sheets has a unique parent... We can only hope (and michael@0: // currently this is the case) that any time page JS can get ts hands on a michael@0: // child sheet that means we've already ensured unique inners throughout its michael@0: // parent chain and things are good. michael@0: nsRefPtr mFirstChild; michael@0: mozilla::CORSMode mCORSMode; michael@0: bool mComplete; michael@0: michael@0: #ifdef DEBUG michael@0: bool mPrincipalSet; michael@0: #endif michael@0: }; michael@0: michael@0: michael@0: // ------------------------------- michael@0: // CSS Style Sheet michael@0: // michael@0: michael@0: class CSSRuleListImpl; michael@0: michael@0: // CID for the nsCSSStyleSheet class michael@0: // ca926f30-2a7e-477e-8467-803fb32af20a michael@0: #define NS_CSS_STYLE_SHEET_IMPL_CID \ michael@0: { 0xca926f30, 0x2a7e, 0x477e, \ michael@0: { 0x84, 0x67, 0x80, 0x3f, 0xb3, 0x2a, 0xf2, 0x0a } } michael@0: michael@0: michael@0: class nsCSSStyleSheet MOZ_FINAL : public nsIStyleSheet, michael@0: public nsIDOMCSSStyleSheet, michael@0: public nsICSSLoaderObserver, michael@0: public nsWrapperCache michael@0: { michael@0: public: michael@0: nsCSSStyleSheet(mozilla::CORSMode aCORSMode); michael@0: michael@0: NS_DECL_CYCLE_COLLECTING_ISUPPORTS michael@0: NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsCSSStyleSheet, michael@0: nsIStyleSheet) michael@0: michael@0: NS_DECLARE_STATIC_IID_ACCESSOR(NS_CSS_STYLE_SHEET_IMPL_CID) michael@0: michael@0: // nsIStyleSheet interface michael@0: virtual nsIURI* GetSheetURI() const MOZ_OVERRIDE; michael@0: virtual nsIURI* GetBaseURI() const MOZ_OVERRIDE; michael@0: virtual void GetTitle(nsString& aTitle) const MOZ_OVERRIDE; michael@0: virtual void GetType(nsString& aType) const MOZ_OVERRIDE; michael@0: virtual bool HasRules() const MOZ_OVERRIDE; michael@0: virtual bool IsApplicable() const MOZ_OVERRIDE; michael@0: virtual void SetEnabled(bool aEnabled) MOZ_OVERRIDE; michael@0: virtual bool IsComplete() const MOZ_OVERRIDE; michael@0: virtual void SetComplete() MOZ_OVERRIDE; michael@0: virtual nsIStyleSheet* GetParentSheet() const MOZ_OVERRIDE; // may be null michael@0: virtual nsIDocument* GetOwningDocument() const MOZ_OVERRIDE; // may be null michael@0: virtual void SetOwningDocument(nsIDocument* aDocument) MOZ_OVERRIDE; michael@0: michael@0: // Find the ID of the owner inner window. michael@0: uint64_t FindOwningWindowInnerID() const; michael@0: #ifdef DEBUG michael@0: virtual void List(FILE* out = stdout, int32_t aIndent = 0) const MOZ_OVERRIDE; michael@0: #endif michael@0: michael@0: void AppendStyleSheet(nsCSSStyleSheet* aSheet); michael@0: void InsertStyleSheetAt(nsCSSStyleSheet* aSheet, int32_t aIndex); michael@0: michael@0: // XXX do these belong here or are they generic? michael@0: void PrependStyleRule(mozilla::css::Rule* aRule); michael@0: void AppendStyleRule(mozilla::css::Rule* aRule); michael@0: void ReplaceStyleRule(mozilla::css::Rule* aOld, mozilla::css::Rule* aNew); michael@0: michael@0: int32_t StyleRuleCount() const; michael@0: mozilla::css::Rule* GetStyleRuleAt(int32_t aIndex) const; michael@0: michael@0: nsresult DeleteRuleFromGroup(mozilla::css::GroupRule* aGroup, uint32_t aIndex); michael@0: nsresult InsertRuleIntoGroup(const nsAString& aRule, mozilla::css::GroupRule* aGroup, uint32_t aIndex, uint32_t* _retval); michael@0: nsresult ReplaceRuleInGroup(mozilla::css::GroupRule* aGroup, mozilla::css::Rule* aOld, mozilla::css::Rule* aNew); michael@0: michael@0: int32_t StyleSheetCount() const; michael@0: michael@0: /** michael@0: * SetURIs must be called on all sheets before parsing into them. michael@0: * SetURIs may only be called while the sheet is 1) incomplete and 2) michael@0: * has no rules in it michael@0: */ michael@0: void SetURIs(nsIURI* aSheetURI, nsIURI* aOriginalSheetURI, nsIURI* aBaseURI); michael@0: michael@0: /** michael@0: * SetPrincipal should be called on all sheets before parsing into them. michael@0: * This can only be called once with a non-null principal. Calling this with michael@0: * a null pointer is allowed and is treated as a no-op. michael@0: */ michael@0: void SetPrincipal(nsIPrincipal* aPrincipal); michael@0: michael@0: // Principal() never returns a null pointer. michael@0: nsIPrincipal* Principal() const { return mInner->mPrincipal; } michael@0: michael@0: // The document this style sheet is associated with. May be null michael@0: nsIDocument* GetDocument() const { return mDocument; } michael@0: michael@0: void SetTitle(const nsAString& aTitle) { mTitle = aTitle; } michael@0: void SetMedia(nsMediaList* aMedia); michael@0: void SetOwningNode(nsINode* aOwningNode) { mOwningNode = aOwningNode; /* Not ref counted */ } michael@0: michael@0: void SetOwnerRule(mozilla::css::ImportRule* aOwnerRule) { mOwnerRule = aOwnerRule; /* Not ref counted */ } michael@0: mozilla::css::ImportRule* GetOwnerRule() const { return mOwnerRule; } michael@0: michael@0: nsXMLNameSpaceMap* GetNameSpaceMap() const { return mInner->mNameSpaceMap; } michael@0: michael@0: already_AddRefed Clone(nsCSSStyleSheet* aCloneParent, michael@0: mozilla::css::ImportRule* aCloneOwnerRule, michael@0: nsIDocument* aCloneDocument, michael@0: nsINode* aCloneOwningNode) const; michael@0: michael@0: bool IsModified() const { return mDirty; } michael@0: michael@0: void SetModifiedByChildRule() { michael@0: NS_ASSERTION(mDirty, michael@0: "sheet must be marked dirty before handing out child rules"); michael@0: DidDirty(); michael@0: } michael@0: michael@0: nsresult AddRuleProcessor(nsCSSRuleProcessor* aProcessor); michael@0: nsresult DropRuleProcessor(nsCSSRuleProcessor* aProcessor); michael@0: michael@0: /** michael@0: * Like the DOM insertRule() method, but doesn't do any security checks michael@0: */ michael@0: nsresult InsertRuleInternal(const nsAString& aRule, michael@0: uint32_t aIndex, uint32_t* aReturn); michael@0: michael@0: /* Get the URI this sheet was originally loaded from, if any. Can michael@0: return null */ michael@0: virtual nsIURI* GetOriginalURI() const; michael@0: michael@0: // nsICSSLoaderObserver interface michael@0: NS_IMETHOD StyleSheetLoaded(nsCSSStyleSheet* aSheet, bool aWasAlternate, michael@0: nsresult aStatus) MOZ_OVERRIDE; michael@0: michael@0: enum EnsureUniqueInnerResult { michael@0: // No work was needed to ensure a unique inner. michael@0: eUniqueInner_AlreadyUnique, michael@0: // A clone was done to ensure a unique inner (which means the style michael@0: // rules in this sheet have changed). michael@0: eUniqueInner_ClonedInner michael@0: }; michael@0: EnsureUniqueInnerResult EnsureUniqueInner(); michael@0: michael@0: // Append all of this sheet's child sheets to aArray. michael@0: void AppendAllChildSheets(nsTArray& aArray); michael@0: michael@0: bool UseForPresentation(nsPresContext* aPresContext, michael@0: nsMediaQueryResultCacheKey& aKey) const; michael@0: michael@0: nsresult ParseSheet(const nsAString& aInput); michael@0: michael@0: // nsIDOMStyleSheet interface michael@0: NS_DECL_NSIDOMSTYLESHEET michael@0: michael@0: // nsIDOMCSSStyleSheet interface michael@0: NS_DECL_NSIDOMCSSSTYLESHEET michael@0: michael@0: // Function used as a callback to rebuild our inner's child sheet michael@0: // list after we clone a unique inner for ourselves. michael@0: static bool RebuildChildList(mozilla::css::Rule* aRule, void* aBuilder); michael@0: michael@0: size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE; michael@0: michael@0: // Get this style sheet's CORS mode michael@0: mozilla::CORSMode GetCORSMode() const { return mInner->mCORSMode; } michael@0: michael@0: mozilla::dom::Element* GetScopeElement() const { return mScopeElement; } michael@0: void SetScopeElement(mozilla::dom::Element* aScopeElement) michael@0: { michael@0: mScopeElement = aScopeElement; michael@0: } michael@0: michael@0: // WebIDL StyleSheet API michael@0: // Our nsIStyleSheet::GetType is a const method, so it ends up michael@0: // ambiguous with with the XPCOM version. Just disambiguate. michael@0: void GetType(nsString& aType) { michael@0: const_cast(this)->GetType(aType); michael@0: } michael@0: // Our XPCOM GetHref is fine for WebIDL michael@0: nsINode* GetOwnerNode() const { return mOwningNode; } michael@0: nsCSSStyleSheet* GetParentStyleSheet() const { return mParent; } michael@0: // Our nsIStyleSheet::GetTitle is a const method, so it ends up michael@0: // ambiguous with with the XPCOM version. Just disambiguate. michael@0: void GetTitle(nsString& aTitle) { michael@0: const_cast(this)->GetTitle(aTitle); michael@0: } michael@0: nsMediaList* Media(); michael@0: bool Disabled() const { return mDisabled; } michael@0: // The XPCOM SetDisabled is fine for WebIDL michael@0: michael@0: // WebIDL CSSStyleSheet API michael@0: // Can't be inline because we can't include ImportRule here. And can't be michael@0: // called GetOwnerRule because that would be ambiguous with the ImportRule michael@0: // version. michael@0: nsIDOMCSSRule* GetDOMOwnerRule() const; michael@0: nsIDOMCSSRuleList* GetCssRules(mozilla::ErrorResult& aRv); michael@0: uint32_t InsertRule(const nsAString& aRule, uint32_t aIndex, michael@0: mozilla::ErrorResult& aRv) { michael@0: uint32_t retval; michael@0: aRv = InsertRule(aRule, aIndex, &retval); michael@0: return retval; michael@0: } michael@0: void DeleteRule(uint32_t aIndex, mozilla::ErrorResult& aRv) { michael@0: aRv = DeleteRule(aIndex); michael@0: } michael@0: michael@0: // WebIDL miscellaneous bits michael@0: mozilla::dom::ParentObject GetParentObject() const { michael@0: if (mOwningNode) { michael@0: return mozilla::dom::ParentObject(mOwningNode); michael@0: } michael@0: michael@0: return mozilla::dom::ParentObject(static_cast(mParent), michael@0: mParent); michael@0: } michael@0: virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; michael@0: michael@0: private: michael@0: nsCSSStyleSheet(const nsCSSStyleSheet& aCopy, michael@0: nsCSSStyleSheet* aParentToUse, michael@0: mozilla::css::ImportRule* aOwnerRuleToUse, michael@0: nsIDocument* aDocumentToUse, michael@0: nsINode* aOwningNodeToUse); michael@0: michael@0: nsCSSStyleSheet(const nsCSSStyleSheet& aCopy) MOZ_DELETE; michael@0: nsCSSStyleSheet& operator=(const nsCSSStyleSheet& aCopy) MOZ_DELETE; michael@0: michael@0: protected: michael@0: virtual ~nsCSSStyleSheet(); michael@0: michael@0: void ClearRuleCascades(); michael@0: michael@0: void WillDirty(); michael@0: void DidDirty(); michael@0: michael@0: // Return success if the subject principal subsumes the principal of our michael@0: // inner, error otherwise. This will also succeed if the subject has michael@0: // UniversalXPConnect or if access is allowed by CORS. In the latter case, michael@0: // it will set the principal of the inner to the subject principal. michael@0: nsresult SubjectSubsumesInnerPrincipal(); michael@0: michael@0: // Add the namespace mapping from this @namespace rule to our namespace map michael@0: nsresult RegisterNamespaceRule(mozilla::css::Rule* aRule); michael@0: michael@0: // Drop our reference to mRuleCollection michael@0: void DropRuleCollection(); michael@0: michael@0: // Drop our reference to mMedia michael@0: void DropMedia(); michael@0: michael@0: // Unlink our inner, if needed, for cycle collection michael@0: void UnlinkInner(); michael@0: // Traverse our inner, if needed, for cycle collection michael@0: void TraverseInner(nsCycleCollectionTraversalCallback &); michael@0: michael@0: protected: michael@0: nsString mTitle; michael@0: nsRefPtr mMedia; michael@0: nsRefPtr mNext; michael@0: nsCSSStyleSheet* mParent; // weak ref michael@0: mozilla::css::ImportRule* mOwnerRule; // weak ref michael@0: michael@0: nsRefPtr mRuleCollection; michael@0: nsIDocument* mDocument; // weak ref; parents maintain this for their children michael@0: nsINode* mOwningNode; // weak ref michael@0: bool mDisabled; michael@0: bool mDirty; // has been modified michael@0: nsRefPtr mScopeElement; michael@0: michael@0: nsCSSStyleSheetInner* mInner; michael@0: michael@0: nsAutoTArray* mRuleProcessors; michael@0: michael@0: friend class nsMediaList; michael@0: friend class nsCSSRuleProcessor; michael@0: friend struct ChildSheetListBuilder; michael@0: }; michael@0: michael@0: NS_DEFINE_STATIC_IID_ACCESSOR(nsCSSStyleSheet, NS_CSS_STYLE_SHEET_IMPL_CID) michael@0: michael@0: #endif /* !defined(nsCSSStyleSheet_h_) */