/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* 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_StyleSheet_h #define mozilla_StyleSheet_h #include "mozilla/css/SheetParsingMode.h" #include "mozilla/dom/CSSStyleSheetBinding.h" #include "mozilla/dom/SRIMetadata.h" #include "mozilla/net/ReferrerPolicy.h" #include "mozilla/CORSMode.h" #include "mozilla/MozPromise.h" #include "mozilla/RefPtr.h" #include "mozilla/ServoBindingTypes.h" #include "mozilla/ServoUtils.h" #include "mozilla/StyleSheetInfo.h" #include "mozilla/URLExtraData.h" #include "nsICSSLoaderObserver.h" #include "nsWrapperCache.h" #include "nsCompatibility.h" #include "nsStringFwd.h" class nsINode; class nsIPrincipal; struct nsLayoutStylesheetCacheShm; struct RawServoSharedMemoryBuilder; class nsIReferrerInfo; namespace mozilla { class ServoCSSRuleList; class ServoStyleSet; typedef MozPromise StyleSheetParsePromise; namespace css { class GroupRule; class Loader; class LoaderReusableStyleSheets; class Rule; class SheetLoadData; } // namespace css namespace dom { class CSSImportRule; class CSSRuleList; class DocumentOrShadowRoot; class MediaList; class ShadowRoot; class SRIMetadata; } // namespace dom enum class StyleSheetState : uint8_t { // Whether the sheet is disabled. Sheets can be made disabled via CSSOM, or // via alternate links and such. Disabled = 1 << 0, // Whether the sheet is complete. The sheet is complete if it's finished // loading. See StyleSheet::SetComplete. Complete = 1 << 1, // Whether we've forced a unique inner. StyleSheet objects share an 'inner' // StyleSheetInfo object if they share URL, CORS mode, etc. // // See the Loader's `mCompleteSheets` and `mLoadingSheets`. ForcedUniqueInner = 1 << 2, // Whether this stylesheet has suffered any modification to the rules via // CSSOM. // // FIXME(emilio): I think as of right now we also set this flag for normal // @import rules, which looks very fishy. ModifiedRules = 1 << 3, }; MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(StyleSheetState) class StyleSheet final : public nsICSSLoaderObserver, public nsWrapperCache { StyleSheet(const StyleSheet& aCopy, StyleSheet* aParentToUse, dom::CSSImportRule* aOwnerRuleToUse, dom::DocumentOrShadowRoot* aDocOrShadowRootToUse, nsINode* aOwningNodeToUse); virtual ~StyleSheet(); using State = StyleSheetState; public: StyleSheet(css::SheetParsingMode aParsingMode, CORSMode aCORSMode, const dom::SRIMetadata& aIntegrity); NS_DECL_CYCLE_COLLECTING_ISUPPORTS NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(StyleSheet) already_AddRefed CreateEmptyChildSheet( already_AddRefed aMediaList) const; bool HasRules() const; // Parses a stylesheet. The aLoadData argument corresponds to the // SheetLoadData for this stylesheet. It may be null in some cases. RefPtr ParseSheet(css::Loader* aLoader, const nsACString& aBytes, css::SheetLoadData* aLoadData); // Common code that needs to be called after servo finishes parsing. This is // shared between the parallel and sequential paths. void FinishAsyncParse( already_AddRefed aSheetContents); // Similar to the above, but guarantees that parsing will be performed // synchronously. void ParseSheetSync( css::Loader* aLoader, const nsACString& aBytes, css::SheetLoadData* aLoadData, uint32_t aLineNumber, css::LoaderReusableStyleSheets* aReusableSheets = nullptr); nsresult ReparseSheet(const nsAString& aInput); const RawServoStyleSheetContents* RawContents() const { return Inner().mContents; } void SetContentsForImport(const RawServoStyleSheetContents* aContents) { MOZ_ASSERT(!Inner().mContents); Inner().mContents = aContents; } URLExtraData* URLData() const { return Inner().mURLData; } // nsICSSLoaderObserver interface NS_IMETHOD StyleSheetLoaded(StyleSheet* aSheet, bool aWasAlternate, nsresult aStatus) final; // Internal GetCssRules methods which do not have security check and // completeness check. ServoCSSRuleList* GetCssRulesInternal(); // Returns the stylesheet's Servo origin as a StyleOrigin value. mozilla::StyleOrigin GetOrigin() const; /** * The different changes that a stylesheet may go through. * * Used by the StyleSets in order to handle more efficiently some kinds of * changes. */ enum class ChangeType { Added, Removed, ApplicableStateChanged, RuleAdded, RuleRemoved, RuleChanged, }; void SetOwningNode(nsINode* aOwningNode) { mOwningNode = aOwningNode; } css::SheetParsingMode ParsingMode() const { return mParsingMode; } mozilla::dom::CSSStyleSheetParsingMode ParsingModeDOM(); /** * Whether the sheet is complete. */ bool IsComplete() const { return bool(mState & State::Complete); } void SetComplete(); void SetEnabled(bool aEnabled) { SetDisabled(!aEnabled); } // Whether the sheet is for an inline