Move security origin filtering for getMatchedCSSRules out of StyleResolver
[WebKit-https.git] / Source / WebCore / css / StyleRule.h
index 95841ab..dfcf19e 100644 (file)
@@ -33,9 +33,10 @@ class CSSStyleRule;
 class CSSStyleSheet;
 class MediaQuerySet;
 class MutableStyleProperties;
-class StyleKeyframe;
+class StyleRuleKeyframe;
 class StyleProperties;
-
+class StyleRuleKeyframes;
+    
 class StyleRuleBase : public WTF::RefCountedBase {
     WTF_MAKE_FAST_ALLOCATED;
 public:
@@ -48,13 +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); }
@@ -67,7 +67,6 @@ public:
     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; }
@@ -87,39 +86,51 @@ public:
     RefPtr<CSSRule> createCSSOMWrapper(CSSRule* parentRule) const;
 
 protected:
-    StyleRuleBase(Type type)
+    StyleRuleBase(Type type, bool hasDocumentSecurityOrigin = false)
         : m_type(type)
-        { }
+        , m_hasDocumentSecurityOrigin(hasDocumentSecurityOrigin)
+    {
+    }
 
     StyleRuleBase(const StyleRuleBase& o)
         : WTF::RefCountedBase()
         , m_type(o.m_type)
-        { }
+        , m_hasDocumentSecurityOrigin(o.m_hasDocumentSecurityOrigin)
+    {
+    }
 
-    ~StyleRuleBase() { }
+    ~StyleRuleBase() = default;
+
+    bool hasDocumentSecurityOrigin() const { return m_hasDocumentSecurityOrigin; }
 
 private:
-    void destroy();
+    WEBCORE_EXPORT void destroy();
     
     RefPtr<CSSRule> createCSSOMWrapper(CSSStyleSheet* parentSheet, CSSRule* parentRule) const;
 
     unsigned m_type : 5;
+    // This is only needed to support getMatchedCSSRules.
+    unsigned m_hasDocumentSecurityOrigin : 1;
 };
 
 class StyleRule final : public StyleRuleBase {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    static Ref<StyleRule> create(Ref<StyleProperties>&& properties)
+    static Ref<StyleRule> create(Ref<StylePropertiesBase>&& properties, bool hasDocumentSecurityOrigin)
     {
-        return adoptRef(*new StyleRule(WTFMove(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 = WTFMove(selectors); }
     void parserAdoptSelectorArray(CSSSelector* selectors) { m_selectorList.adoptSelectorArray(selectors); }
@@ -131,15 +142,20 @@ public:
     static unsigned averageSizeInBytes();
 
 private:
-    StyleRule(Ref<StyleProperties>&&);
+    StyleRule(Ref<StylePropertiesBase>&&, bool hasDocumentSecurityOrigin);
     StyleRule(const StyleRule&);
 
-    static Ref<StyleRule> create(const Vector<const CSSSelector*>&, Ref<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 StyleProperties* StyleRule::propertiesWithoutDeferredParsing() const
+{
+    return m_properties->type() != DeferredPropertiesType ? &downcast<StyleProperties>(m_properties.get()) : nullptr;
+}
+
 class StyleRuleFontFace final : public StyleRuleBase {
 public:
     static Ref<StyleRuleFontFace> create(Ref<StyleProperties>&& properties) { return adoptRef(*new StyleRuleFontFace(WTFMove(properties))); }
@@ -181,21 +197,43 @@ private:
     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;
+};
     
+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;
 };
 
+inline const Vector<RefPtr<StyleRuleBase>>* StyleRuleGroup::childRulesWithoutDeferredParsing() const
+{
+    return !m_deferredRules ? &m_childRules : nullptr;
+}
+
 class StyleRuleMedia final : public StyleRuleGroup {
 public:
     static Ref<StyleRuleMedia> create(Ref<MediaQuerySet>&& media, Vector<RefPtr<StyleRuleBase>>& adoptRules)
@@ -203,12 +241,18 @@ public:
         return adoptRef(*new StyleRuleMedia(WTFMove(media), adoptRules));
     }
 
+    static Ref<StyleRuleMedia> create(Ref<MediaQuerySet>&& media, std::unique_ptr<DeferredStyleGroupRuleList>&& deferredChildRules)
+    {
+        return adoptRef(*new StyleRuleMedia(WTFMove(media), WTFMove(deferredChildRules)));
+    }
+
     MediaQuerySet* mediaQueries() const { return m_mediaQueries.get(); }
 
     Ref<StyleRuleMedia> copy() const { return adoptRef(*new StyleRuleMedia(*this)); }
 
 private:
     StyleRuleMedia(Ref<MediaQuerySet>&&, Vector<RefPtr<StyleRuleBase>>& adoptRules);
+    StyleRuleMedia(Ref<MediaQuerySet>&&, std::unique_ptr<DeferredStyleGroupRuleList>&&);
     StyleRuleMedia(const StyleRuleMedia&);
 
     RefPtr<MediaQuerySet> m_mediaQueries;
@@ -220,6 +264,11 @@ public:
     {
         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; }
@@ -227,36 +276,14 @@ public:
 
 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 final : public StyleRuleGroup {
-public:
-    static Ref<StyleRuleRegion> create(Vector<std::unique_ptr<CSSParserSelector>>* selectors, Vector<RefPtr<StyleRuleBase>>& adoptRules)
-    {
-        return adoptRef(*new StyleRuleRegion(selectors, adoptRules));
-    }
-    
-    static Ref<StyleRuleRegion> create(CSSSelectorList& selectors, Vector<RefPtr<StyleRuleBase>>& adoptRules)
-    {
-        return adoptRef(*new StyleRuleRegion(selectors, adoptRules));
-    }
-    
-    const CSSSelectorList& selectorList() const { return m_selectorList; }
-
-    Ref<StyleRuleRegion> copy() const { return adoptRef(*new StyleRuleRegion(*this)); }
-
-private:
-    StyleRuleRegion(Vector<std::unique_ptr<CSSParserSelector>>*, Vector<RefPtr<StyleRuleBase>>& adoptRules);
-    StyleRuleRegion(CSSSelectorList&, Vector<RefPtr<StyleRuleBase>>&);
-    StyleRuleRegion(const StyleRuleRegion&);
-    
-    CSSSelectorList m_selectorList;
-};
-
 #if ENABLE(CSS_DEVICE_ADAPTATION)
 class StyleRuleViewport final : public StyleRuleBase {
 public:
@@ -335,10 +362,6 @@ SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRuleSupports)
     static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isSupportsRule(); }
 SPECIALIZE_TYPE_TRAITS_END()
 
-SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRuleRegion)
-    static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isRegionRule(); }
-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(); }
@@ -349,7 +372,7 @@ 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::StyleKeyframe)
+SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRuleKeyframe)
 static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isKeyframeRule(); }
 SPECIALIZE_TYPE_TRAITS_END()