/* -*- 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/CORSMode.h" #include "mozilla/MozPromise.h" #include "mozilla/RefPtr.h" #include "mozilla/ServoBindingTypes.h" #include "mozilla/ServoTypes.h" #include "mozilla/StyleSheetInfo.h" #include "nsICSSLoaderObserver.h" #include "nsIPrincipal.h" #include "nsWrapperCache.h" #include "nsStringFwd.h" class nsIGlobalObject; class nsINode; class nsIPrincipal; struct ServoCssRules; class nsIReferrerInfo; namespace mozilla { class ServoCSSRuleList; class ServoStyleSet; typedef MozPromise StyleSheetParsePromise; enum class StyleRuleChangeKind : uint32_t; 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; struct CSSStyleSheetInit; } // 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. ModifiedRules = 1 << 3, // Same flag, but devtools clears it in some specific situations. // // Used to control whether devtools shows the rule in its authored form or // not. ModifiedRulesForDevtools = 1 << 4, // Whether modifications to the sheet are currently disallowed. // This flag is set during the async Replace() function to ensure // that the sheet is not modified until the promise is resolved. ModificationDisallowed = 1 << 5, }; MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(StyleSheetState) class StyleSheet final : public nsICSSLoaderObserver, public nsWrapperCache { StyleSheet(const StyleSheet& aCopy, StyleSheet* aParentSheetToUse, dom::DocumentOrShadowRoot* aDocOrShadowRootToUse, dom::Document* aConstructorDocToUse); virtual ~StyleSheet(); using State = StyleSheetState; public: StyleSheet(css::SheetParsingMode aParsingMode, CORSMode aCORSMode, const dom::SRIMetadata& aIntegrity); static already_AddRefed Constructor(const dom::GlobalObject&, const dom::CSSStyleSheetInit&, ErrorResult&); NS_DECL_CYCLE_COLLECTING_ISUPPORTS NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(StyleSheet) already_AddRefed CreateEmptyChildSheet( already_AddRefed aMediaList) const; bool HasRules() const; // Parses a stylesheet. The load data argument corresponds to the // SheetLoadData for this stylesheet. // NOTE: ParseSheet can run synchronously or asynchronously // based on the result of `AllowParallelParse` RefPtr ParseSheet(css::Loader&, const nsACString& aBytes, css::SheetLoadData&); // Common code that needs to be called after servo finishes parsing. This is // shared between the parallel and sequential paths. void FinishAsyncParse(already_AddRefed, UniquePtr); // Similar to `ParseSheet`, but guarantees that // parsing will be performed synchronously. // NOTE: ParseSheet can still run synchronously. // This is not a strict alternative. // // The load data may be null sometimes. void ParseSheetSync( css::Loader* aLoader, const nsACString& aBytes, css::SheetLoadData* aLoadData, uint32_t aLineNumber, css::LoaderReusableStyleSheets* aReusableSheets = nullptr); void ReparseSheet(const nsACString& aInput, ErrorResult& aRv); const RawServoStyleSheetContents* RawContents() const { return Inner().mContents; } const StyleUseCounters* GetStyleUseCounters() const { return Inner().mUseCounters.get(); } URLExtraData* URLData() const { return Inner().mURLData; } // nsICSSLoaderObserver interface NS_IMETHOD StyleSheetLoaded(StyleSheet* aSheet, bool aWasDeferred, 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. StyleOrigin GetOrigin() const; void SetOwningNode(nsINode* aOwningNode) { mOwningNode = aOwningNode; } css::SheetParsingMode ParsingMode() const { return mParsingMode; } 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