CSSStyleSheet.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. // vim:cindent:tabstop=2:expandtab:shiftwidth=2:
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /* representation of a CSS style sheet */
  7. #ifndef mozilla_CSSStyleSheet_h
  8. #define mozilla_CSSStyleSheet_h
  9. #include "mozilla/Attributes.h"
  10. #include "mozilla/IncrementalClearCOMRuleArray.h"
  11. #include "mozilla/MemoryReporting.h"
  12. #include "mozilla/StyleSheet.h"
  13. #include "mozilla/StyleSheetInfo.h"
  14. #include "mozilla/css/SheetParsingMode.h"
  15. #include "mozilla/dom/Element.h"
  16. #include "nscore.h"
  17. #include "nsCOMPtr.h"
  18. #include "nsAutoPtr.h"
  19. #include "nsICSSLoaderObserver.h"
  20. #include "nsTArrayForwardDeclare.h"
  21. #include "nsString.h"
  22. #include "mozilla/CORSMode.h"
  23. #include "nsCycleCollectionParticipant.h"
  24. #include "mozilla/net/ReferrerPolicy.h"
  25. #include "mozilla/dom/SRIMetadata.h"
  26. class CSSRuleListImpl;
  27. class nsCSSRuleProcessor;
  28. class nsIURI;
  29. class nsMediaList;
  30. class nsMediaQueryResultCacheKey;
  31. class nsStyleSet;
  32. class nsPresContext;
  33. class nsXMLNameSpaceMap;
  34. namespace mozilla {
  35. struct ChildSheetListBuilder;
  36. class CSSStyleSheet;
  37. namespace css {
  38. class Rule;
  39. class GroupRule;
  40. class ImportRule;
  41. } // namespace css
  42. namespace dom {
  43. class CSSRuleList;
  44. } // namespace dom
  45. // -------------------------------
  46. // CSS Style Sheet Inner Data Container
  47. //
  48. struct CSSStyleSheetInner : public StyleSheetInfo
  49. {
  50. CSSStyleSheetInner(CSSStyleSheet* aPrimarySheet,
  51. CORSMode aCORSMode,
  52. ReferrerPolicy aReferrerPolicy,
  53. const dom::SRIMetadata& aIntegrity);
  54. CSSStyleSheetInner(CSSStyleSheetInner& aCopy,
  55. CSSStyleSheet* aPrimarySheet);
  56. ~CSSStyleSheetInner();
  57. CSSStyleSheetInner* CloneFor(CSSStyleSheet* aPrimarySheet);
  58. void AddSheet(CSSStyleSheet* aSheet);
  59. void RemoveSheet(CSSStyleSheet* aSheet);
  60. void RebuildNameSpaces();
  61. // Create a new namespace map
  62. nsresult CreateNamespaceMap();
  63. size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
  64. AutoTArray<CSSStyleSheet*, 8> mSheets;
  65. IncrementalClearCOMRuleArray mOrderedRules;
  66. nsAutoPtr<nsXMLNameSpaceMap> mNameSpaceMap;
  67. // Linked list of child sheets. This is al fundamentally broken, because
  68. // each of the child sheets has a unique parent... We can only hope (and
  69. // currently this is the case) that any time page JS can get ts hands on a
  70. // child sheet that means we've already ensured unique inners throughout its
  71. // parent chain and things are good.
  72. RefPtr<CSSStyleSheet> mFirstChild;
  73. };
  74. // -------------------------------
  75. // CSS Style Sheet
  76. //
  77. // CID for the CSSStyleSheet class
  78. // 7985c7ac-9ddc-444d-9899-0c86ec122f54
  79. #define NS_CSS_STYLE_SHEET_IMPL_CID \
  80. { 0x7985c7ac, 0x9ddc, 0x444d, \
  81. { 0x98, 0x99, 0x0c, 0x86, 0xec, 0x12, 0x2f, 0x54 } }
  82. class CSSStyleSheet final : public StyleSheet
  83. , public nsICSSLoaderObserver
  84. {
  85. public:
  86. typedef net::ReferrerPolicy ReferrerPolicy;
  87. CSSStyleSheet(css::SheetParsingMode aParsingMode,
  88. CORSMode aCORSMode, ReferrerPolicy aReferrerPolicy);
  89. CSSStyleSheet(css::SheetParsingMode aParsingMode,
  90. CORSMode aCORSMode, ReferrerPolicy aReferrerPolicy,
  91. const dom::SRIMetadata& aIntegrity);
  92. NS_DECL_ISUPPORTS_INHERITED
  93. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(CSSStyleSheet, StyleSheet)
  94. NS_DECLARE_STATIC_IID_ACCESSOR(NS_CSS_STYLE_SHEET_IMPL_CID)
  95. bool HasRules() const;
  96. /**
  97. * Set the stylesheet to be enabled. This may or may not make it
  98. * applicable. Note that this WILL inform the sheet's document of
  99. * its new applicable state if the state changes but WILL NOT call
  100. * BeginUpdate() or EndUpdate() on the document -- calling those is
  101. * the caller's responsibility. This allows use of SetEnabled when
  102. * batched updates are desired. If you want updates handled for
  103. * you, see nsIDOMStyleSheet::SetDisabled().
  104. */
  105. void SetEnabled(bool aEnabled);
  106. // style sheet owner info
  107. CSSStyleSheet* GetParentSheet() const; // may be null
  108. void SetAssociatedDocument(nsIDocument* aDocument,
  109. DocumentAssociationMode aAssociationMode);
  110. // Find the ID of the owner inner window.
  111. uint64_t FindOwningWindowInnerID() const;
  112. #ifdef DEBUG
  113. void List(FILE* out = stdout, int32_t aIndent = 0) const;
  114. #endif
  115. void AppendStyleSheet(CSSStyleSheet* aSheet);
  116. // XXX do these belong here or are they generic?
  117. void AppendStyleRule(css::Rule* aRule);
  118. int32_t StyleRuleCount() const;
  119. css::Rule* GetStyleRuleAt(int32_t aIndex) const;
  120. nsresult DeleteRuleFromGroup(css::GroupRule* aGroup, uint32_t aIndex);
  121. nsresult InsertRuleIntoGroup(const nsAString& aRule, css::GroupRule* aGroup, uint32_t aIndex, uint32_t* _retval);
  122. void SetTitle(const nsAString& aTitle) { mTitle = aTitle; }
  123. void SetMedia(nsMediaList* aMedia);
  124. void SetOwnerRule(css::ImportRule* aOwnerRule) { mOwnerRule = aOwnerRule; /* Not ref counted */ }
  125. css::ImportRule* GetOwnerRule() const { return mOwnerRule; }
  126. // Workaround overloaded-virtual warning in GCC.
  127. using StyleSheet::GetOwnerRule;
  128. nsXMLNameSpaceMap* GetNameSpaceMap() const { return mInner->mNameSpaceMap; }
  129. already_AddRefed<CSSStyleSheet> Clone(CSSStyleSheet* aCloneParent,
  130. css::ImportRule* aCloneOwnerRule,
  131. nsIDocument* aCloneDocument,
  132. nsINode* aCloneOwningNode) const;
  133. bool IsModified() const { return mDirty; }
  134. void SetModifiedByChildRule() {
  135. NS_ASSERTION(mDirty,
  136. "sheet must be marked dirty before handing out child rules");
  137. DidDirty();
  138. }
  139. nsresult AddRuleProcessor(nsCSSRuleProcessor* aProcessor);
  140. nsresult DropRuleProcessor(nsCSSRuleProcessor* aProcessor);
  141. void AddStyleSet(nsStyleSet* aStyleSet);
  142. void DropStyleSet(nsStyleSet* aStyleSet);
  143. // nsICSSLoaderObserver interface
  144. NS_IMETHOD StyleSheetLoaded(StyleSheet* aSheet, bool aWasAlternate,
  145. nsresult aStatus) override;
  146. void EnsureUniqueInner();
  147. // Append all of this sheet's child sheets to aArray.
  148. void AppendAllChildSheets(nsTArray<CSSStyleSheet*>& aArray);
  149. bool UseForPresentation(nsPresContext* aPresContext,
  150. nsMediaQueryResultCacheKey& aKey) const;
  151. nsresult ReparseSheet(const nsAString& aInput);
  152. void SetInRuleProcessorCache() { mInRuleProcessorCache = true; }
  153. // Function used as a callback to rebuild our inner's child sheet
  154. // list after we clone a unique inner for ourselves.
  155. static bool RebuildChildList(css::Rule* aRule, void* aBuilder);
  156. size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
  157. dom::Element* GetScopeElement() const { return mScopeElement; }
  158. void SetScopeElement(dom::Element* aScopeElement)
  159. {
  160. mScopeElement = aScopeElement;
  161. }
  162. // WebIDL StyleSheet API
  163. nsMediaList* Media() final;
  164. // WebIDL CSSStyleSheet API
  165. // Can't be inline because we can't include ImportRule here. And can't be
  166. // called GetOwnerRule because that would be ambiguous with the ImportRule
  167. // version.
  168. nsIDOMCSSRule* GetDOMOwnerRule() const final;
  169. void WillDirty();
  170. void DidDirty();
  171. private:
  172. CSSStyleSheet(const CSSStyleSheet& aCopy,
  173. CSSStyleSheet* aParentToUse,
  174. css::ImportRule* aOwnerRuleToUse,
  175. nsIDocument* aDocumentToUse,
  176. nsINode* aOwningNodeToUse);
  177. CSSStyleSheet(const CSSStyleSheet& aCopy) = delete;
  178. CSSStyleSheet& operator=(const CSSStyleSheet& aCopy) = delete;
  179. protected:
  180. virtual ~CSSStyleSheet();
  181. void ClearRuleCascades();
  182. // Add the namespace mapping from this @namespace rule to our namespace map
  183. nsresult RegisterNamespaceRule(css::Rule* aRule);
  184. // Drop our reference to mRuleCollection
  185. void DropRuleCollection();
  186. // Drop our reference to mMedia
  187. void DropMedia();
  188. // Unlink our inner, if needed, for cycle collection
  189. void UnlinkInner();
  190. // Traverse our inner, if needed, for cycle collection
  191. void TraverseInner(nsCycleCollectionTraversalCallback &);
  192. protected:
  193. // Internal methods which do not have security check and completeness check.
  194. dom::CSSRuleList* GetCssRulesInternal(ErrorResult& aRv);
  195. uint32_t InsertRuleInternal(const nsAString& aRule,
  196. uint32_t aIndex, ErrorResult& aRv);
  197. void DeleteRuleInternal(uint32_t aIndex, ErrorResult& aRv);
  198. RefPtr<nsMediaList> mMedia;
  199. RefPtr<CSSStyleSheet> mNext;
  200. CSSStyleSheet* mParent; // weak ref
  201. css::ImportRule* mOwnerRule; // weak ref
  202. RefPtr<CSSRuleListImpl> mRuleCollection;
  203. bool mDirty; // has been modified
  204. bool mInRuleProcessorCache;
  205. RefPtr<dom::Element> mScopeElement;
  206. CSSStyleSheetInner* mInner;
  207. AutoTArray<nsCSSRuleProcessor*, 8>* mRuleProcessors;
  208. nsTArray<nsStyleSet*> mStyleSets;
  209. friend class ::nsMediaList;
  210. friend class ::nsCSSRuleProcessor;
  211. friend class mozilla::StyleSheet;
  212. friend struct mozilla::ChildSheetListBuilder;
  213. };
  214. NS_DEFINE_STATIC_IID_ACCESSOR(CSSStyleSheet, NS_CSS_STYLE_SHEET_IMPL_CID)
  215. } // namespace mozilla
  216. #endif /* !defined(mozilla_CSSStyleSheet_h) */