Use is<>() / downcast<>() for StyleRuleBase subclasses
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 12 Dec 2014 20:28:17 +0000 (20:28 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 12 Dec 2014 20:28:17 +0000 (20:28 +0000)
https://bugs.webkit.org/show_bug.cgi?id=139571

Reviewed by Andreas Kling.

Use is<>() / downcast<>() for StyleRuleBase subclasses for safety and
consistency.

No new tests, no behavior change.

* css/CSSFontFaceRule.cpp:
(WebCore::CSSFontFaceRule::reattach):
* css/CSSKeyframesRule.h:
(isType):
* css/CSSPageRule.cpp:
(WebCore::CSSPageRule::reattach):
* css/CSSStyleRule.cpp:
(WebCore::CSSStyleRule::reattach):
* css/CSSStyleSheet.cpp:
(WebCore::CSSStyleSheet::insertRule):
* css/RuleSet.cpp:
(WebCore::RuleSet::addRegionRule):
(WebCore::RuleSet::addChildRules):
* css/StyleInvalidationAnalysis.cpp:
(WebCore::shouldDirtyAllStyle):
* css/StyleRule.cpp:
(WebCore::StyleRuleBase::destroy):
(WebCore::StyleRuleBase::copy):
(WebCore::StyleRuleBase::createCSSOMWrapper):
* css/StyleRule.h:
(isType):
(WebCore::toStyleRule): Deleted.
* css/StyleRuleImport.h:
(isType):
* css/StyleSheetContents.cpp:
(WebCore::StyleSheetContents::parserAppendRule):
(WebCore::StyleSheetContents::wrapperInsertRule):
(WebCore::StyleSheetContents::addSubresourceStyleURLs):
(WebCore::childRulesHaveFailedOrCanceledSubresources):
* css/WebKitCSSViewportRule.cpp:
(WebCore::WebKitCSSViewportRule::reattach):
* inspector/InspectorCSSAgent.cpp:
(WebCore::InspectorCSSAgent::buildArrayForMatchedRuleList):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@177228 268f45cc-cd09-0410-ab3c-d52691b4dbfc

14 files changed:
Source/WebCore/ChangeLog
Source/WebCore/css/CSSFontFaceRule.cpp
Source/WebCore/css/CSSKeyframesRule.h
Source/WebCore/css/CSSPageRule.cpp
Source/WebCore/css/CSSStyleRule.cpp
Source/WebCore/css/CSSStyleSheet.cpp
Source/WebCore/css/RuleSet.cpp
Source/WebCore/css/StyleInvalidationAnalysis.cpp
Source/WebCore/css/StyleRule.cpp
Source/WebCore/css/StyleRule.h
Source/WebCore/css/StyleRuleImport.h
Source/WebCore/css/StyleSheetContents.cpp
Source/WebCore/css/WebKitCSSViewportRule.cpp
Source/WebCore/inspector/InspectorCSSAgent.cpp

index eb9a2fa..fdff829 100644 (file)
@@ -1,3 +1,49 @@
+2014-12-12  Chris Dumez  <cdumez@apple.com>
+
+        Use is<>() / downcast<>() for StyleRuleBase subclasses
+        https://bugs.webkit.org/show_bug.cgi?id=139571
+
+        Reviewed by Andreas Kling.
+
+        Use is<>() / downcast<>() for StyleRuleBase subclasses for safety and
+        consistency.
+
+        No new tests, no behavior change.
+
+        * css/CSSFontFaceRule.cpp:
+        (WebCore::CSSFontFaceRule::reattach):
+        * css/CSSKeyframesRule.h:
+        (isType):
+        * css/CSSPageRule.cpp:
+        (WebCore::CSSPageRule::reattach):
+        * css/CSSStyleRule.cpp:
+        (WebCore::CSSStyleRule::reattach):
+        * css/CSSStyleSheet.cpp:
+        (WebCore::CSSStyleSheet::insertRule):
+        * css/RuleSet.cpp:
+        (WebCore::RuleSet::addRegionRule):
+        (WebCore::RuleSet::addChildRules):
+        * css/StyleInvalidationAnalysis.cpp:
+        (WebCore::shouldDirtyAllStyle):
+        * css/StyleRule.cpp:
+        (WebCore::StyleRuleBase::destroy):
+        (WebCore::StyleRuleBase::copy):
+        (WebCore::StyleRuleBase::createCSSOMWrapper):
+        * css/StyleRule.h:
+        (isType):
+        (WebCore::toStyleRule): Deleted.
+        * css/StyleRuleImport.h:
+        (isType):
+        * css/StyleSheetContents.cpp:
+        (WebCore::StyleSheetContents::parserAppendRule):
+        (WebCore::StyleSheetContents::wrapperInsertRule):
+        (WebCore::StyleSheetContents::addSubresourceStyleURLs):
+        (WebCore::childRulesHaveFailedOrCanceledSubresources):
+        * css/WebKitCSSViewportRule.cpp:
+        (WebCore::WebKitCSSViewportRule::reattach):
+        * inspector/InspectorCSSAgent.cpp:
+        (WebCore::InspectorCSSAgent::buildArrayForMatchedRuleList):
+
 2014-12-12  Jer Noble  <jer.noble@apple.com>
 
         [MSE] Add support for SourceBuffer.mode.
index f64acd9..0dda1b2 100644 (file)
@@ -62,8 +62,7 @@ String CSSFontFaceRule::cssText() const
 
 void CSSFontFaceRule::reattach(StyleRuleBase& rule)
 {
-    ASSERT_WITH_SECURITY_IMPLICATION(rule.isFontFaceRule());
-    m_fontFaceRule = static_cast<StyleRuleFontFace&>(rule);
+    m_fontFaceRule = downcast<StyleRuleFontFace>(rule);
     if (m_propertiesCSSOMWrapper)
         m_propertiesCSSOMWrapper->reattach(m_fontFaceRule->mutableProperties());
 }
index 78f9e79..6f154a9 100644 (file)
@@ -101,4 +101,8 @@ private:
 
 SPECIALIZE_TYPE_TRAITS_CSS_RULE(CSSKeyframesRule, CSSRule::KEYFRAMES_RULE)
 
+SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRuleKeyframes)
+    static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isKeyframesRule(); }
+SPECIALIZE_TYPE_TRAITS_END()
+
 #endif // CSSKeyframesRule_h
index f21430d..e7bea46 100644 (file)
@@ -96,8 +96,7 @@ String CSSPageRule::cssText() const
 
 void CSSPageRule::reattach(StyleRuleBase& rule)
 {
-    ASSERT_WITH_SECURITY_IMPLICATION(rule.isPageRule());
-    m_pageRule = static_cast<StyleRulePage&>(rule);
+    m_pageRule = downcast<StyleRulePage>(rule);
     if (m_propertiesCSSOMWrapper)
         m_propertiesCSSOMWrapper->reattach(m_pageRule.get().mutableProperties());
 }
index 95a69cf..e1c414c 100644 (file)
@@ -133,8 +133,7 @@ String CSSStyleRule::cssText() const
 
 void CSSStyleRule::reattach(StyleRuleBase& rule)
 {
-    ASSERT_WITH_SECURITY_IMPLICATION(rule.isStyleRule());
-    m_styleRule = static_cast<StyleRule&>(rule);
+    m_styleRule = downcast<StyleRule>(rule);
     if (m_propertiesCSSOMWrapper)
         m_propertiesCSSOMWrapper->reattach(m_styleRule->mutableProperties());
 }
index 4e1fe78..8e6e40f 100644 (file)
@@ -300,7 +300,7 @@ unsigned CSSStyleSheet::insertRule(const String& ruleString, unsigned index, Exc
         return 0;
     }
 
-    RuleMutationScope mutationScope(this, RuleInsertion, rule->type() == StyleRuleBase::Keyframes ? static_cast<StyleRuleKeyframes*>(rule.get()) : 0);
+    RuleMutationScope mutationScope(this, RuleInsertion, is<StyleRuleKeyframes>(*rule) ? downcast<StyleRuleKeyframes>(rule.get()) : nullptr);
 
     bool success = m_contents.get().wrapperInsertRule(rule, index);
     if (!success) {
index 1ff9c4d..59a1a26 100644 (file)
@@ -307,10 +307,9 @@ void RuleSet::addRegionRule(StyleRuleRegion* regionRule, bool hasDocumentSecurit
     const Vector<RefPtr<StyleRuleBase>>& childRules = regionRule->childRules();
     AddRuleFlags addRuleFlags = hasDocumentSecurityOrigin ? RuleHasDocumentSecurityOrigin : RuleHasNoSpecialState;
     addRuleFlags = static_cast<AddRuleFlags>(addRuleFlags | RuleIsInRegionRule);
-    for (unsigned i = 0; i < childRules.size(); ++i) {
-        StyleRuleBase* regionStylingRule = childRules[i].get();
-        if (regionStylingRule->isStyleRule())
-            regionRuleSet->addStyleRule(static_cast<StyleRule*>(regionStylingRule), addRuleFlags);
+    for (auto& childRule : childRules) {
+        if (is<StyleRule>(*childRule))
+            regionRuleSet->addStyleRule(downcast<StyleRule>(childRule.get()), addRuleFlags);
     }
     // Update the "global" rule count so that proper order is maintained
     m_ruleCount = regionRuleSet->m_ruleCount;
@@ -320,35 +319,31 @@ void RuleSet::addRegionRule(StyleRuleRegion* regionRule, bool hasDocumentSecurit
 
 void RuleSet::addChildRules(const Vector<RefPtr<StyleRuleBase>>& rules, const MediaQueryEvaluator& medium, StyleResolver* resolver, bool hasDocumentSecurityOrigin, AddRuleFlags addRuleFlags)
 {
-    for (unsigned i = 0; i < rules.size(); ++i) {
-        StyleRuleBase* rule = rules[i].get();
-
-        if (rule->isStyleRule()) {
-            StyleRule* styleRule = static_cast<StyleRule*>(rule);
-            addStyleRule(styleRule, addRuleFlags);
-        } else if (rule->isPageRule())
-            addPageRule(static_cast<StyleRulePage*>(rule));
-        else if (rule->isMediaRule()) {
-            StyleRuleMedia* mediaRule = static_cast<StyleRuleMedia*>(rule);
-            if ((!mediaRule->mediaQueries() || medium.eval(mediaRule->mediaQueries(), resolver)))
-                addChildRules(mediaRule->childRules(), medium, resolver, hasDocumentSecurityOrigin, addRuleFlags);
-        } else if (rule->isFontFaceRule() && resolver) {
+    for (auto& rule : rules) {
+        if (is<StyleRule>(*rule))
+            addStyleRule(downcast<StyleRule>(rule.get()), addRuleFlags);
+        else if (is<StyleRulePage>(*rule))
+            addPageRule(downcast<StyleRulePage>(rule.get()));
+        else if (is<StyleRuleMedia>(*rule)) {
+            auto& mediaRule = downcast<StyleRuleMedia>(*rule);
+            if ((!mediaRule.mediaQueries() || medium.eval(mediaRule.mediaQueries(), resolver)))
+                addChildRules(mediaRule.childRules(), medium, resolver, hasDocumentSecurityOrigin, addRuleFlags);
+        } else if (is<StyleRuleFontFace>(*rule) && resolver) {
             // Add this font face to our set.
-            const StyleRuleFontFace* fontFaceRule = static_cast<StyleRuleFontFace*>(rule);
-            resolver->fontSelector()->addFontFaceRule(fontFaceRule);
+            resolver->fontSelector()->addFontFaceRule(downcast<StyleRuleFontFace>(rule.get()));
             resolver->invalidateMatchedPropertiesCache();
-        } else if (rule->isKeyframesRule() && resolver)
-            resolver->addKeyframeStyle(static_cast<StyleRuleKeyframes*>(rule));
-        else if (rule->isSupportsRule() && static_cast<StyleRuleSupports*>(rule)->conditionIsSupported())
-            addChildRules(static_cast<StyleRuleSupports*>(rule)->childRules(), medium, resolver, hasDocumentSecurityOrigin, addRuleFlags);
+        } else if (is<StyleRuleKeyframes>(*rule) && resolver)
+            resolver->addKeyframeStyle(downcast<StyleRuleKeyframes>(rule.get()));
+        else if (is<StyleRuleSupports>(*rule) && downcast<StyleRuleSupports>(*rule).conditionIsSupported())
+            addChildRules(downcast<StyleRuleSupports>(*rule).childRules(), medium, resolver, hasDocumentSecurityOrigin, addRuleFlags);
 #if ENABLE(CSS_REGIONS)
-        else if (rule->isRegionRule() && resolver) {
-            addRegionRule(static_cast<StyleRuleRegion*>(rule), hasDocumentSecurityOrigin);
+        else if (is<StyleRuleRegion>(*rule) && resolver) {
+            addRegionRule(downcast<StyleRuleRegion>(rule.get()), hasDocumentSecurityOrigin);
         }
 #endif
 #if ENABLE(CSS_DEVICE_ADAPTATION)
-        else if (rule->isViewportRule() && resolver) {
-            resolver->viewportStyleResolver()->addViewportRule(static_cast<StyleRuleViewport*>(rule));
+        else if (is<StyleRuleViewport>(*rule) && resolver) {
+            resolver->viewportStyleResolver()->addViewportRule(downcast<StyleRuleViewport>(rule.get()));
         }
 #endif
     }
index 06f600e..6d51b0f 100644 (file)
@@ -39,13 +39,13 @@ namespace WebCore {
 static bool shouldDirtyAllStyle(const Vector<RefPtr<StyleRuleBase>>& rules)
 {
     for (auto& rule : rules) {
-        if (rule->isMediaRule()) {
-            if (shouldDirtyAllStyle(static_cast<StyleRuleMedia&>(*rule).childRules()))
+        if (is<StyleRuleMedia>(*rule)) {
+            if (shouldDirtyAllStyle(downcast<StyleRuleMedia>(*rule).childRules()))
                 return true;
             continue;
         }
         // FIXME: At least font faces don't need full recalc in all cases.
-        if (!rule->isStyleRule())
+        if (!is<StyleRule>(*rule))
             return true;
     }
     return false;
index 217d603..89dbc7c 100644 (file)
@@ -59,34 +59,34 @@ void StyleRuleBase::destroy()
 {
     switch (type()) {
     case Style:
-        delete static_cast<StyleRule*>(this);
+        delete downcast<StyleRule>(this);
         return;
     case Page:
-        delete static_cast<StyleRulePage*>(this);
+        delete downcast<StyleRulePage>(this);
         return;
     case FontFace:
-        delete static_cast<StyleRuleFontFace*>(this);
+        delete downcast<StyleRuleFontFace>(this);
         return;
     case Media:
-        delete static_cast<StyleRuleMedia*>(this);
+        delete downcast<StyleRuleMedia>(this);
         return;
     case Supports:
-        delete static_cast<StyleRuleSupports*>(this);
+        delete downcast<StyleRuleSupports>(this);
         return;
 #if ENABLE(CSS_REGIONS)
     case Region:
-        delete static_cast<StyleRuleRegion*>(this);
+        delete downcast<StyleRuleRegion>(this);
         return;
 #endif
     case Import:
-        delete static_cast<StyleRuleImport*>(this);
+        delete downcast<StyleRuleImport>(this);
         return;
     case Keyframes:
-        delete static_cast<StyleRuleKeyframes*>(this);
+        delete downcast<StyleRuleKeyframes>(this);
         return;
 #if ENABLE(CSS_DEVICE_ADAPTATION)
     case Viewport:
-        delete static_cast<StyleRuleViewport*>(this);
+        delete downcast<StyleRuleViewport>(this);
         return;
 #endif
     case Unknown:
@@ -105,24 +105,24 @@ PassRef<StyleRuleBase> StyleRuleBase::copy() const
 {
     switch (type()) {
     case Style:
-        return static_cast<const StyleRule*>(this)->copy();
+        return downcast<StyleRule>(*this).copy();
     case Page:
-        return static_cast<const StyleRulePage*>(this)->copy();
+        return downcast<StyleRulePage>(*this).copy();
     case FontFace:
-        return static_cast<const StyleRuleFontFace*>(this)->copy();
+        return downcast<StyleRuleFontFace>(*this).copy();
     case Media:
-        return static_cast<const StyleRuleMedia*>(this)->copy();
+        return downcast<StyleRuleMedia>(*this).copy();
     case Supports:
-        return static_cast<const StyleRuleSupports*>(this)->copy();
+        return downcast<StyleRuleSupports>(*this).copy();
 #if ENABLE(CSS_REGIONS)
     case Region:
-        return static_cast<const StyleRuleRegion*>(this)->copy();
+        return downcast<StyleRuleRegion>(*this).copy();
 #endif
     case Keyframes:
-        return static_cast<const StyleRuleKeyframes*>(this)->copy();
+        return downcast<StyleRuleKeyframes>(*this).copy();
 #if ENABLE(CSS_DEVICE_ADAPTATION)
     case Viewport:
-        return static_cast<const StyleRuleViewport*>(this)->copy();
+        return downcast<StyleRuleViewport>(*this).copy();
 #endif
     case Import:
         // FIXME: Copy import rules.
@@ -146,34 +146,34 @@ PassRefPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSStyleSheet* parentSheet
     StyleRuleBase& self = const_cast<StyleRuleBase&>(*this);
     switch (type()) {
     case Style:
-        rule = CSSStyleRule::create(static_cast<StyleRule&>(self), parentSheet);
+        rule = CSSStyleRule::create(downcast<StyleRule>(self), parentSheet);
         break;
     case Page:
-        rule = CSSPageRule::create(static_cast<StyleRulePage&>(self), parentSheet);
+        rule = CSSPageRule::create(downcast<StyleRulePage>(self), parentSheet);
         break;
     case FontFace:
-        rule = CSSFontFaceRule::create(static_cast<StyleRuleFontFace&>(self), parentSheet);
+        rule = CSSFontFaceRule::create(downcast<StyleRuleFontFace>(self), parentSheet);
         break;
     case Media:
-        rule = CSSMediaRule::create(static_cast<StyleRuleMedia&>(self), parentSheet);
+        rule = CSSMediaRule::create(downcast<StyleRuleMedia>(self), parentSheet);
         break;
     case Supports:
-        rule = CSSSupportsRule::create(static_cast<StyleRuleSupports&>(self), parentSheet);
+        rule = CSSSupportsRule::create(downcast<StyleRuleSupports>(self), parentSheet);
         break;
 #if ENABLE(CSS_REGIONS)
     case Region:
-        rule = WebKitCSSRegionRule::create(static_cast<StyleRuleRegion&>(self), parentSheet);
+        rule = WebKitCSSRegionRule::create(downcast<StyleRuleRegion>(self), parentSheet);
         break;
 #endif
     case Import:
-        rule = CSSImportRule::create(static_cast<StyleRuleImport&>(self), parentSheet);
+        rule = CSSImportRule::create(downcast<StyleRuleImport>(self), parentSheet);
         break;
     case Keyframes:
-        rule = CSSKeyframesRule::create(static_cast<StyleRuleKeyframes&>(self), parentSheet);
+        rule = CSSKeyframesRule::create(downcast<StyleRuleKeyframes>(self), parentSheet);
         break;
 #if ENABLE(CSS_DEVICE_ADAPTATION)
     case Viewport:
-        rule = WebKitCSSViewportRule::create(static_cast<StyleRuleViewport&>(self), parentSheet);
+        rule = WebKitCSSViewportRule::create(downcast<StyleRuleViewport>(self), parentSheet);
         break;
 #endif
     case Unknown:
@@ -183,7 +183,7 @@ PassRefPtr<CSSRule> StyleRuleBase::createCSSOMWrapper(CSSStyleSheet* parentSheet
     case Region:
 #endif
         ASSERT_NOT_REACHED();
-        return 0;
+        return nullptr;
     }
     if (parentRule)
         rule->setParentRule(parentRule);
index 0cae7ba..aeae9df 100644 (file)
@@ -134,12 +134,6 @@ private:
     CSSSelectorList m_selectorList;
 };
 
-inline const StyleRule* toStyleRule(const StyleRuleBase* rule)
-{
-    ASSERT_WITH_SECURITY_IMPLICATION(!rule || rule->isStyleRule());
-    return static_cast<const StyleRule*>(rule);
-}
-
 class StyleRuleFontFace : public StyleRuleBase {
 public:
     static PassRef<StyleRuleFontFace> create(PassRef<StyleProperties> properties) { return adoptRef(*new StyleRuleFontFace(WTF::move(properties))); }
@@ -274,16 +268,34 @@ private:
 
 } // namespace WebCore
 
+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::StyleRuleGroup& group) { return group.isMediaRule(); }
+    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::StyleRuleGroup& group) { return group.isSupportsRule(); }
+    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::StyleRuleGroup& group) { return group.isRegionRule(); }
+    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(); }
+SPECIALIZE_TYPE_TRAITS_END()
+#endif // ENABLE(CSS_DEVICE_ADAPTATION)
+
 #endif // StyleRule_h
index 28face7..1131f94 100644 (file)
@@ -25,6 +25,7 @@
 #include "CachedResourceHandle.h"
 #include "CachedStyleSheetClient.h"
 #include "StyleRule.h"
+#include <wtf/TypeCasts.h>
 
 namespace WebCore {
 
@@ -83,4 +84,8 @@ private:
 
 } // namespace WebCore
 
+SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::StyleRuleImport)
+    static bool isType(const WebCore::StyleRuleBase& rule) { return rule.isImportRule(); }
+SPECIALIZE_TYPE_TRAITS_END()
+
 #endif
index 94616eb..aa0d75c 100644 (file)
@@ -127,10 +127,10 @@ bool StyleSheetContents::isCacheable() const
 void StyleSheetContents::parserAppendRule(PassRefPtr<StyleRuleBase> rule)
 {
     ASSERT(!rule->isCharsetRule());
-    if (rule->isImportRule()) {
+    if (is<StyleRuleImport>(*rule)) {
         // Parser enforces that @import rules come before anything else except @charset.
         ASSERT(m_childRules.isEmpty());
-        m_importRules.append(static_cast<StyleRuleImport*>(rule.get()));
+        m_importRules.append(downcast<StyleRuleImport>(rule.get()));
         m_importRules.last()->setParentStyleSheet(this);
         m_importRules.last()->requestStyleSheet();
         return;
@@ -138,14 +138,14 @@ void StyleSheetContents::parserAppendRule(PassRefPtr<StyleRuleBase> rule)
 
 #if ENABLE(RESOLUTION_MEDIA_QUERY)
     // Add warning message to inspector if dpi/dpcm values are used for screen media.
-    if (rule->isMediaRule())
-        reportMediaQueryWarningIfNeeded(singleOwnerDocument(), static_cast<StyleRuleMedia*>(rule.get())->mediaQueries());
+    if (is<StyleRuleMedia>(*rule))
+        reportMediaQueryWarningIfNeeded(singleOwnerDocument(), downcast<StyleRuleMedia>(*rule).mediaQueries());
 #endif
 
     // NOTE: The selector list has to fit into RuleData. <http://webkit.org/b/118369>
     // If we're adding a rule with a huge number of selectors, split it up into multiple rules
-    if (rule->isStyleRule() && toStyleRule(rule.get())->selectorList().componentCount() > RuleData::maximumSelectorComponentCount) {
-        Vector<RefPtr<StyleRule>> rules = toStyleRule(rule.get())->splitIntoMultipleRulesWithMaximumSelectorComponentCount(RuleData::maximumSelectorComponentCount);
+    if (is<StyleRule>(*rule) && downcast<StyleRule>(*rule).selectorList().componentCount() > RuleData::maximumSelectorComponentCount) {
+        Vector<RefPtr<StyleRule>> rules = downcast<StyleRule>(*rule).splitIntoMultipleRulesWithMaximumSelectorComponentCount(RuleData::maximumSelectorComponentCount);
         m_childRules.appendVector(rules);
         return;
     }
@@ -221,16 +221,16 @@ bool StyleSheetContents::wrapperInsertRule(PassRefPtr<StyleRuleBase> rule, unsig
     
     if (childVectorIndex < m_importRules.size() || (childVectorIndex == m_importRules.size() && rule->isImportRule())) {
         // Inserting non-import rule before @import is not allowed.
-        if (!rule->isImportRule())
+        if (!is<StyleRuleImport>(*rule))
             return false;
-        m_importRules.insert(childVectorIndex, static_cast<StyleRuleImport*>(rule.get()));
+        m_importRules.insert(childVectorIndex, downcast<StyleRuleImport>(rule.get()));
         m_importRules[childVectorIndex]->setParentStyleSheet(this);
         m_importRules[childVectorIndex]->requestStyleSheet();
         // FIXME: Stylesheet doesn't actually change meaningfully before the imported sheets are loaded.
         return true;
     }
     // Inserting @import rule after a non-import rule is not allowed.
-    if (rule->isImportRule())
+    if (is<StyleRuleImport>(*rule))
         return false;
     childVectorIndex -= m_importRules.size();
  
@@ -410,42 +410,39 @@ void StyleSheetContents::addSubresourceStyleURLs(ListHashSet<URL>& urls)
     while (!styleSheetQueue.isEmpty()) {
         StyleSheetContents* styleSheet = styleSheetQueue.takeFirst();
         
-        for (unsigned i = 0; i < styleSheet->m_importRules.size(); ++i) {
-            StyleRuleImport* importRule = styleSheet->m_importRules[i].get();
+        for (auto& importRule : styleSheet->m_importRules) {
             if (importRule->styleSheet()) {
                 styleSheetQueue.append(importRule->styleSheet());
                 addSubresourceURL(urls, importRule->styleSheet()->baseURL());
             }
         }
-        for (unsigned i = 0; i < styleSheet->m_childRules.size(); ++i) {
-            StyleRuleBase* rule = styleSheet->m_childRules[i].get();
-            if (rule->isStyleRule())
-                static_cast<StyleRule*>(rule)->properties().addSubresourceStyleURLs(urls, this);
-            else if (rule->isFontFaceRule())
-                static_cast<StyleRuleFontFace*>(rule)->properties().addSubresourceStyleURLs(urls, this);
+        for (auto& rule : styleSheet->m_childRules) {
+            if (is<StyleRule>(*rule))
+                downcast<StyleRule>(*rule).properties().addSubresourceStyleURLs(urls, this);
+            else if (is<StyleRuleFontFace>(*rule))
+                downcast<StyleRuleFontFace>(*rule).properties().addSubresourceStyleURLs(urls, this);
         }
     }
 }
 
 static bool childRulesHaveFailedOrCanceledSubresources(const Vector<RefPtr<StyleRuleBase>>& rules)
 {
-    for (unsigned i = 0; i < rules.size(); ++i) {
-        const StyleRuleBase* rule = rules[i].get();
+    for (auto& rule : rules) {
         switch (rule->type()) {
         case StyleRuleBase::Style:
-            if (static_cast<const StyleRule*>(rule)->properties().hasFailedOrCanceledSubresources())
+            if (downcast<StyleRule>(*rule).properties().hasFailedOrCanceledSubresources())
                 return true;
             break;
         case StyleRuleBase::FontFace:
-            if (static_cast<const StyleRuleFontFace*>(rule)->properties().hasFailedOrCanceledSubresources())
+            if (downcast<StyleRuleFontFace>(*rule).properties().hasFailedOrCanceledSubresources())
                 return true;
             break;
         case StyleRuleBase::Media:
-            if (childRulesHaveFailedOrCanceledSubresources(static_cast<const StyleRuleMedia*>(rule)->childRules()))
+            if (childRulesHaveFailedOrCanceledSubresources(downcast<StyleRuleMedia>(*rule).childRules()))
                 return true;
             break;
         case StyleRuleBase::Region:
-            if (childRulesHaveFailedOrCanceledSubresources(static_cast<const StyleRuleRegion*>(rule)->childRules()))
+            if (childRulesHaveFailedOrCanceledSubresources(downcast<StyleRuleRegion>(*rule).childRules()))
                 return true;
             break;
         case StyleRuleBase::Import:
index ae4a0a5..cbd172f 100644 (file)
@@ -77,8 +77,7 @@ String WebKitCSSViewportRule::cssText() const
 
 void WebKitCSSViewportRule::reattach(StyleRuleBase& rule)
 {
-    ASSERT_WITH_SECURITY_IMPLICATION(rule.isViewportRule());
-    m_viewportRule = static_cast<StyleRuleViewport&>(rule);
+    m_viewportRule = downcast<StyleRuleViewport>(rule);
 
     if (m_propertiesCSSOMWrapper)
         m_propertiesCSSOMWrapper->reattach(m_viewportRule->mutableProperties());
index f814be4..6097623 100644 (file)
@@ -1044,17 +1044,13 @@ PassRefPtr<Inspector::Protocol::Array<Inspector::Protocol::CSS::RuleMatch>> Insp
     SelectorChecker::CheckingContext context(SelectorChecker::Mode::CollectingRules);
     SelectorChecker selectorChecker(element->document());
 
-    for (unsigned i = 0; i < matchedRules.size(); ++i) {
-        if (!matchedRules[i]->isStyleRule())
-            continue;
-
-        StyleRule* matchedStyleRule = static_cast<StyleRule*>(matchedRules[i].get());
-        RefPtr<Inspector::Protocol::CSS::CSSRule> ruleObject = buildObjectForRule(matchedStyleRule, styleResolver);
+    for (auto& matchedRule : matchedRules) {
+        RefPtr<Inspector::Protocol::CSS::CSSRule> ruleObject = buildObjectForRule(matchedRule.get(), styleResolver);
         if (!ruleObject)
             continue;
 
         RefPtr<Inspector::Protocol::Array<int>> matchingSelectors = Inspector::Protocol::Array<int>::create();
-        const CSSSelectorList& selectorList = matchedStyleRule->selectorList();
+        const CSSSelectorList& selectorList = matchedRule->selectorList();
         long index = 0;
         for (const CSSSelector* selector = selectorList.first(); selector; selector = CSSSelectorList::next(selector)) {
             unsigned ignoredSpecificity;