Move security origin filtering for getMatchedCSSRules out of StyleResolver
[WebKit-https.git] / Source / WebCore / css / StyleRule.h
index a24d924..dfcf19e 100644 (file)
  * Boston, MA 02110-1301, USA.
  */
 
-#ifndef StyleRule_h
-#define StyleRule_h
+#pragma once
 
 #include "CSSSelectorList.h"
-#include "MediaList.h"
 #include "StyleProperties.h"
 #include <wtf/RefPtr.h>
+#include <wtf/TypeCasts.h>
 
 namespace WebCore {
 
 class CSSRule;
 class CSSStyleRule;
 class CSSStyleSheet;
+class MediaQuerySet;
 class MutableStyleProperties;
+class StyleRuleKeyframe;
 class StyleProperties;
-
+class StyleRuleKeyframes;
+    
 class StyleRuleBase : public WTF::RefCountedBase {
     WTF_MAKE_FAST_ALLOCATED;
 public:
@@ -47,12 +49,12 @@ public:
         FontFace,
         Page,
         Keyframes,
-        Keyframe, // Not used. These are internally non-rule StyleKeyframe objects.
+        Keyframe, // Not used. These are internally non-rule StyleRuleKeyframe objects.
+        Namespace,
         Supports = 12,
 #if ENABLE(CSS_DEVICE_ADAPTATION)
         Viewport = 15,
 #endif
-        Region = 16,
     };
 
     Type type() const { return static_cast<Type>(m_type); }
@@ -60,19 +62,18 @@ public:
     bool isCharsetRule() const { return type() == Charset; }
     bool isFontFaceRule() const { return type() == FontFace; }
     bool isKeyframesRule() const { return type() == Keyframes; }
+    bool isKeyframeRule() const { return type() == Keyframe; }
+    bool isNamespaceRule() const { return type() == Namespace; }
     bool isMediaRule() const { return type() == Media; }
     bool isPageRule() const { return type() == Page; }
     bool isStyleRule() const { return type() == Style; }
-    bool isRegionRule() const { return type() == Region; }
     bool isSupportsRule() const { return type() == Supports; }
 #if ENABLE(CSS_DEVICE_ADAPTATION)
     bool isViewportRule() const { return type() == Viewport; }
 #endif
     bool isImportRule() const { return type() == Import; }
 
-    PassRef<StyleRuleBase> copy() const;
-
-    int sourceLine() const { return m_sourceLine; }
+    Ref<StyleRuleBase> copy() const;
 
     void deref()
     {
@@ -81,86 +82,101 @@ public:
     }
 
     // FIXME: There shouldn't be any need for the null parent version.
-    PassRefPtr<CSSRule> createCSSOMWrapper(CSSStyleSheet* parentSheet = 0) const;
-    PassRefPtr<CSSRule> createCSSOMWrapper(CSSRule* parentRule) const;
+    RefPtr<CSSRule> createCSSOMWrapper(CSSStyleSheet* parentSheet = nullptr) const;
+    RefPtr<CSSRule> createCSSOMWrapper(CSSRule* parentRule) const;
 
 protected:
-    StyleRuleBase(Type type, signed sourceLine = 0) : m_type(type), m_sourceLine(sourceLine) { }
-    StyleRuleBase(const StyleRuleBase& o) : WTF::RefCountedBase(), m_type(o.m_type), m_sourceLine(o.m_sourceLine) { }
+    StyleRuleBase(Type type, bool hasDocumentSecurityOrigin = false)
+        : m_type(type)
+        , m_hasDocumentSecurityOrigin(hasDocumentSecurityOrigin)
+    {
+    }
 
-    ~StyleRuleBase() { }
+    StyleRuleBase(const StyleRuleBase& o)
+        : WTF::RefCountedBase()
+        , m_type(o.m_type)
+        , m_hasDocumentSecurityOrigin(o.m_hasDocumentSecurityOrigin)
+    {
+    }
+
+    ~StyleRuleBase() = default;
+
+    bool hasDocumentSecurityOrigin() const { return m_hasDocumentSecurityOrigin; }
 
 private:
-    void destroy();
+    WEBCORE_EXPORT void destroy();
     
-    PassRefPtr<CSSRule> createCSSOMWrapper(CSSStyleSheet* parentSheet, CSSRule* parentRule) const;
+    RefPtr<CSSRule> createCSSOMWrapper(CSSStyleSheet* parentSheet, CSSRule* parentRule) const;
 
     unsigned m_type : 5;
-    signed m_sourceLine : 27;
+    // This is only needed to support getMatchedCSSRules.
+    unsigned m_hasDocumentSecurityOrigin : 1;
 };
 
-class StyleRule : public StyleRuleBase {
+class StyleRule final : public StyleRuleBase {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    static PassRef<StyleRule> create(int sourceLine, PassRef<StyleProperties> properties)
+    static Ref<StyleRule> create(Ref<StylePropertiesBase>&& properties, bool hasDocumentSecurityOrigin)
     {
-        return adoptRef(*new StyleRule(sourceLine, WTF::move(properties)));
+        return adoptRef(*new StyleRule(WTFMove(properties), hasDocumentSecurityOrigin));
     }
     
     ~StyleRule();
 
     const CSSSelectorList& selectorList() const { return m_selectorList; }
-    const StyleProperties& properties() const { return m_properties; }
-    MutableStyleProperties& mutableProperties();
     
+    const StyleProperties& properties() const;
+    MutableStyleProperties& mutableProperties();
+    const StyleProperties* propertiesWithoutDeferredParsing() const;
+
+    using StyleRuleBase::hasDocumentSecurityOrigin;
+
     void parserAdoptSelectorVector(Vector<std::unique_ptr<CSSParserSelector>>& selectors) { m_selectorList.adoptSelectorVector(selectors); }
-    void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList = WTF::move(selectors); }
+    void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList = WTFMove(selectors); }
     void parserAdoptSelectorArray(CSSSelector* selectors) { m_selectorList.adoptSelectorArray(selectors); }
 
-    PassRef<StyleRule> copy() const { return adoptRef(*new StyleRule(*this)); }
+    Ref<StyleRule> copy() const { return adoptRef(*new StyleRule(*this)); }
 
     Vector<RefPtr<StyleRule>> splitIntoMultipleRulesWithMaximumSelectorComponentCount(unsigned) const;
 
     static unsigned averageSizeInBytes();
 
 private:
-    StyleRule(int sourceLine, PassRef<StyleProperties>);
+    StyleRule(Ref<StylePropertiesBase>&&, bool hasDocumentSecurityOrigin);
     StyleRule(const StyleRule&);
 
-    static PassRef<StyleRule> create(int sourceLine, const Vector<const CSSSelector*>&, PassRef<StyleProperties>);
+    static Ref<StyleRule> createForSplitting(const Vector<const CSSSelector*>&, Ref<StyleProperties>&&, bool hasDocumentSecurityOrigin);
 
-    Ref<StyleProperties> m_properties;
+    mutable Ref<StylePropertiesBase> m_properties;
     CSSSelectorList m_selectorList;
 };
 
-inline const StyleRule* toStyleRule(const StyleRuleBase* rule)
+inline const StyleProperties* StyleRule::propertiesWithoutDeferredParsing() const
 {
-    ASSERT_WITH_SECURITY_IMPLICATION(!rule || rule->isStyleRule());
-    return static_cast<const StyleRule*>(rule);
+    return m_properties->type() != DeferredPropertiesType ? &downcast<StyleProperties>(m_properties.get()) : nullptr;
 }
 
-class StyleRuleFontFace : public StyleRuleBase {
+class StyleRuleFontFace final : public StyleRuleBase {
 public:
-    static PassRef<StyleRuleFontFace> create(PassRef<StyleProperties> properties) { return adoptRef(*new StyleRuleFontFace(WTF::move(properties))); }
+    static Ref<StyleRuleFontFace> create(Ref<StyleProperties>&& properties) { return adoptRef(*new StyleRuleFontFace(WTFMove(properties))); }
     
     ~StyleRuleFontFace();
 
     const StyleProperties& properties() const { return m_properties; }
     MutableStyleProperties& mutableProperties();
 
-    PassRef<StyleRuleFontFace> copy() const { return adoptRef(*new StyleRuleFontFace(*this)); }
-
+    Ref<StyleRuleFontFace> copy() const { return adoptRef(*new StyleRuleFontFace(*this)); }
 
 private:
-    StyleRuleFontFace(PassRef<StyleProperties>);
+    explicit StyleRuleFontFace(Ref<StyleProperties>&&);
     StyleRuleFontFace(const StyleRuleFontFace&);
 
     Ref<StyleProperties> m_properties;
 };
 
-class StyleRulePage : public StyleRuleBase {
+class StyleRulePage final : public StyleRuleBase {
 public:
-    static PassRef<StyleRulePage> create(PassRef<StyleProperties> properties) { return adoptRef(*new StyleRulePage(WTF::move(properties))); }
+    static Ref<StyleRulePage> create(Ref<StyleProperties>&& properties) { return adoptRef(*new StyleRulePage(WTFMove(properties))); }
 
     ~StyleRulePage();
 
@@ -169,126 +185,198 @@ public:
     MutableStyleProperties& mutableProperties();
 
     void parserAdoptSelectorVector(Vector<std::unique_ptr<CSSParserSelector>>& selectors) { m_selectorList.adoptSelectorVector(selectors); }
-    void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList = WTF::move(selectors); }
+    void wrapperAdoptSelectorList(CSSSelectorList& selectors) { m_selectorList = WTFMove(selectors); }
 
-    PassRef<StyleRulePage> copy() const { return adoptRef(*new StyleRulePage(*this)); }
+    Ref<StyleRulePage> copy() const { return adoptRef(*new StyleRulePage(*this)); }
 
 private:
-    StyleRulePage(PassRef<StyleProperties>);
+    explicit StyleRulePage(Ref<StyleProperties>&&);
     StyleRulePage(const StyleRulePage&);
     
     Ref<StyleProperties> m_properties;
     CSSSelectorList m_selectorList;
 };
 
-class StyleRuleGroup : public StyleRuleBase {
+class DeferredStyleGroupRuleList final {
 public:
-    const Vector<RefPtr<StyleRuleBase>>& childRules() const { return m_childRules; }
+    DeferredStyleGroupRuleList(const CSSParserTokenRange&, CSSDeferredParser&);
+    
+    void parseDeferredRules(Vector<RefPtr<StyleRuleBase>>&);
+    void parseDeferredKeyframes(StyleRuleKeyframes&);
+
+private:
+    Vector<CSSParserToken> m_tokens;
+    Ref<CSSDeferredParser> m_parser;
+};
     
-    void wrapperInsertRule(unsigned, PassRef<StyleRuleBase>);
+class StyleRuleGroup : public StyleRuleBase {
+public:
+    const Vector<RefPtr<StyleRuleBase>>& childRules() const;
+    const Vector<RefPtr<StyleRuleBase>>* childRulesWithoutDeferredParsing() const;
+
+    void wrapperInsertRule(unsigned, Ref<StyleRuleBase>&&);
     void wrapperRemoveRule(unsigned);
     
 protected:
-    StyleRuleGroup(Type, Vector<RefPtr<StyleRuleBase>>& adoptRule);
+    StyleRuleGroup(Type, Vector<RefPtr<StyleRuleBase>>&);
+    StyleRuleGroup(Type, std::unique_ptr<DeferredStyleGroupRuleList>&&);
     StyleRuleGroup(const StyleRuleGroup&);
     
 private:
-    Vector<RefPtr<StyleRuleBase>> m_childRules;
+    void parseDeferredRulesIfNeeded() const;
+
+    mutable Vector<RefPtr<StyleRuleBase>> m_childRules;
+    mutable std::unique_ptr<DeferredStyleGroupRuleList> m_deferredRules;
 };
 
-class StyleRuleMedia : public StyleRuleGroup {
+inline const Vector<RefPtr<StyleRuleBase>>* StyleRuleGroup::childRulesWithoutDeferredParsing() const
+{
+    return !m_deferredRules ? &m_childRules : nullptr;
+}
+
+class StyleRuleMedia final : public StyleRuleGroup {
 public:
-    static PassRef<StyleRuleMedia> create(PassRefPtr<MediaQuerySet> media, Vector<RefPtr<StyleRuleBase>>& adoptRules)
+    static Ref<StyleRuleMedia> create(Ref<MediaQuerySet>&& media, Vector<RefPtr<StyleRuleBase>>& adoptRules)
+    {
+        return adoptRef(*new StyleRuleMedia(WTFMove(media), adoptRules));
+    }
+
+    static Ref<StyleRuleMedia> create(Ref<MediaQuerySet>&& media, std::unique_ptr<DeferredStyleGroupRuleList>&& deferredChildRules)
     {
-        return adoptRef(*new StyleRuleMedia(media, adoptRules));
+        return adoptRef(*new StyleRuleMedia(WTFMove(media), WTFMove(deferredChildRules)));
     }
 
     MediaQuerySet* mediaQueries() const { return m_mediaQueries.get(); }
 
-    PassRef<StyleRuleMedia> copy() const { return adoptRef(*new StyleRuleMedia(*this)); }
+    Ref<StyleRuleMedia> copy() const { return adoptRef(*new StyleRuleMedia(*this)); }
 
 private:
-    StyleRuleMedia(PassRefPtr<MediaQuerySet>, Vector<RefPtr<StyleRuleBase>>& adoptRules);
+    StyleRuleMedia(Ref<MediaQuerySet>&&, Vector<RefPtr<StyleRuleBase>>& adoptRules);
+    StyleRuleMedia(Ref<MediaQuerySet>&&, std::unique_ptr<DeferredStyleGroupRuleList>&&);
     StyleRuleMedia(const StyleRuleMedia&);
 
     RefPtr<MediaQuerySet> m_mediaQueries;
 };
 
-class StyleRuleSupports : public StyleRuleGroup {
+class StyleRuleSupports final : public StyleRuleGroup {
 public:
-    static PassRef<StyleRuleSupports> create(const String& conditionText, bool conditionIsSupported, Vector<RefPtr<StyleRuleBase>>& adoptRules)
+    static Ref<StyleRuleSupports> create(const String& conditionText, bool conditionIsSupported, Vector<RefPtr<StyleRuleBase>>& adoptRules)
     {
         return adoptRef(*new StyleRuleSupports(conditionText, conditionIsSupported, adoptRules));
     }
+    
+    static Ref<StyleRuleSupports> create(const String& conditionText, bool conditionIsSupported, std::unique_ptr<DeferredStyleGroupRuleList>&& deferredChildRules)
+    {
+        return adoptRef(*new StyleRuleSupports(conditionText, conditionIsSupported, WTFMove(deferredChildRules)));
+    }
 
     String conditionText() const { return m_conditionText; }
     bool conditionIsSupported() const { return m_conditionIsSupported; }
-    PassRef<StyleRuleSupports> copy() const { return adoptRef(*new StyleRuleSupports(*this)); }
+    Ref<StyleRuleSupports> copy() const { return adoptRef(*new StyleRuleSupports(*this)); }
 
 private:
     StyleRuleSupports(const String& conditionText, bool conditionIsSupported, Vector<RefPtr<StyleRuleBase>>& adoptRules);
+    StyleRuleSupports(const String& conditionText, bool conditionIsSupported, std::unique_ptr<DeferredStyleGroupRuleList>&&);
+    
     StyleRuleSupports(const StyleRuleSupports&);
 
     String m_conditionText;
     bool m_conditionIsSupported;
 };
 
-class StyleRuleRegion : public StyleRuleGroup {
-public:
-    static PassRef<StyleRuleRegion> create(Vector<std::unique_ptr<CSSParserSelector>>* selectors, Vector<RefPtr<StyleRuleBase>>& adoptRules)
-    {
-        return adoptRef(*new StyleRuleRegion(selectors, adoptRules));
-    }
-
-    const CSSSelectorList& selectorList() const { return m_selectorList; }
-
-    PassRef<StyleRuleRegion> copy() const { return adoptRef(*new StyleRuleRegion(*this)); }
-
-private:
-    StyleRuleRegion(Vector<std::unique_ptr<CSSParserSelector>>*, Vector<RefPtr<StyleRuleBase>>& adoptRules);
-    StyleRuleRegion(const StyleRuleRegion&);
-    
-    CSSSelectorList m_selectorList;
-};
-
 #if ENABLE(CSS_DEVICE_ADAPTATION)
-class StyleRuleViewport : public StyleRuleBase {
+class StyleRuleViewport final : public StyleRuleBase {
 public:
-    static PassRef<StyleRuleViewport> create(PassRef<StyleProperties> properties) { return adoptRef(*new StyleRuleViewport(WTF::move(properties))); }
+    static Ref<StyleRuleViewport> create(Ref<StyleProperties>&& properties) { return adoptRef(*new StyleRuleViewport(WTFMove(properties))); }
 
     ~StyleRuleViewport();
 
     const StyleProperties& properties() const { return m_properties.get(); }
     MutableStyleProperties& mutableProperties();
 
-    PassRef<StyleRuleViewport> copy() const { return adoptRef(*new StyleRuleViewport(*this)); }
+    Ref<StyleRuleViewport> copy() const { return adoptRef(*new StyleRuleViewport(*this)); }
 
 private:
-    StyleRuleViewport(PassRef<StyleProperties>);
+    explicit StyleRuleViewport(Ref<StyleProperties>&&);
     StyleRuleViewport(const StyleRuleViewport&);
 
     Ref<StyleProperties> m_properties;
 };
 #endif // ENABLE(CSS_DEVICE_ADAPTATION)
 
-inline const StyleRuleMedia* toStyleRuleMedia(const StyleRuleGroup* rule)
-{
-    ASSERT_WITH_SECURITY_IMPLICATION(!rule || rule->isMediaRule());
-    return static_cast<const StyleRuleMedia*>(rule);
-}
+// This is only used by the CSS parser.
+class StyleRuleCharset final : public StyleRuleBase {
+public:
+    static Ref<StyleRuleCharset> create() { return adoptRef(*new StyleRuleCharset()); }
+    
+    ~StyleRuleCharset();
+    
+    Ref<StyleRuleCharset> copy() const { return adoptRef(*new StyleRuleCharset(*this)); }
 
-inline const StyleRuleSupports* toStyleRuleSupports(const StyleRuleGroup* rule)
-{
-    ASSERT_WITH_SECURITY_IMPLICATION(!rule || rule->isSupportsRule());
-    return static_cast<const StyleRuleSupports*>(rule);
-}
+private:
+    explicit StyleRuleCharset();
+    StyleRuleCharset(const StyleRuleCharset&);
+};
 
-inline const StyleRuleRegion* toStyleRuleRegion(const StyleRuleGroup* rule)
-{
-    ASSERT_WITH_SECURITY_IMPLICATION(!rule || rule->isRegionRule());
-    return static_cast<const StyleRuleRegion*>(rule);
-}
+class StyleRuleNamespace final : public StyleRuleBase {
+public:
+    static Ref<StyleRuleNamespace> create(AtomicString prefix, AtomicString uri)
+    {
+        return adoptRef(*new StyleRuleNamespace(prefix, uri));
+    }
+    
+    ~StyleRuleNamespace();
 
+    Ref<StyleRuleNamespace> copy() const { return adoptRef(*new StyleRuleNamespace(*this)); }
+    
+    AtomicString prefix() const { return m_prefix; }
+    AtomicString uri() const { return m_uri; }
+
+private:
+    StyleRuleNamespace(AtomicString prefix, AtomicString uri);
+    StyleRuleNamespace(const StyleRuleNamespace&);
+    
+    AtomicString m_prefix;
+    AtomicString m_uri;
+};
+    
 } // namespace WebCore
 
-#endif // StyleRule_h
+SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRule)
+    static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isStyleRule(); }
+SPECIALIZE_TYPE_TRAITS_END()
+
+SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRuleFontFace)
+    static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isFontFaceRule(); }
+SPECIALIZE_TYPE_TRAITS_END()
+
+SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRuleMedia)
+    static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isMediaRule(); }
+SPECIALIZE_TYPE_TRAITS_END()
+
+SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRulePage)
+    static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isPageRule(); }
+SPECIALIZE_TYPE_TRAITS_END()
+
+SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRuleSupports)
+    static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isSupportsRule(); }
+SPECIALIZE_TYPE_TRAITS_END()
+
+#if ENABLE(CSS_DEVICE_ADAPTATION)
+SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRuleViewport)
+    static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isViewportRule(); }
+SPECIALIZE_TYPE_TRAITS_END()
+#endif // ENABLE(CSS_DEVICE_ADAPTATION)
+
+SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRuleNamespace)
+    static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isNamespaceRule(); }
+SPECIALIZE_TYPE_TRAITS_END()
+
+SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRuleKeyframe)
+static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isKeyframeRule(); }
+SPECIALIZE_TYPE_TRAITS_END()
+
+SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRuleCharset)
+static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isCharsetRule(); }
+SPECIALIZE_TYPE_TRAITS_END()
+