Remove threaded parser code from the preload scanners
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 18 Jan 2014 18:06:03 +0000 (18:06 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 18 Jan 2014 18:06:03 +0000 (18:06 +0000)
https://bugs.webkit.org/show_bug.cgi?id=127222

Reviewed by Andreas Kling.

* html/parser/CSSPreloadScanner.cpp:
(WebCore::CSSPreloadScanner::CSSPreloadScanner):
Use nullptr.

(WebCore::CSSPreloadScanner::scan):
Move scanCommon here and simplify the code.

* html/parser/CSSPreloadScanner.h:
Remove scanCommon.

* html/parser/HTMLPreloadScanner.cpp:
(WebCore::TokenPreloadScanner::tagIdFor):
TagId is a strong enum now, update enum values.

(WebCore::TokenPreloadScanner::initiatorFor):
Ditto.

(WebCore::TokenPreloadScanner::StartTagScanner::processAttributes):
Ditto.

(WebCore::TokenPreloadScanner::StartTagScanner::match):
Indent this.

(WebCore::TokenPreloadScanner::StartTagScanner::processAttribute):
TagId is a strong enum now, update enum values.

(WebCore::TokenPreloadScanner::StartTagScanner::charset):
Ditto.

(WebCore::TokenPreloadScanner::StartTagScanner::resourceType):
Ditto.

(WebCore::TokenPreloadScanner::StartTagScanner::shouldPreload):
Ditto.

(WebCore::TokenPreloadScanner::scan):
Merge scan and scanCommon.

* html/parser/HTMLPreloadScanner.h:

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

Source/WebCore/ChangeLog
Source/WebCore/html/parser/CSSPreloadScanner.cpp
Source/WebCore/html/parser/CSSPreloadScanner.h
Source/WebCore/html/parser/HTMLPreloadScanner.cpp
Source/WebCore/html/parser/HTMLPreloadScanner.h

index 6ac136c..97c8e56 100644 (file)
@@ -1,3 +1,50 @@
+2014-01-18  Anders Carlsson  <andersca@apple.com>
+
+        Remove threaded parser code from the preload scanners
+        https://bugs.webkit.org/show_bug.cgi?id=127222
+
+        Reviewed by Andreas Kling.
+
+        * html/parser/CSSPreloadScanner.cpp:
+        (WebCore::CSSPreloadScanner::CSSPreloadScanner):
+        Use nullptr.
+
+        (WebCore::CSSPreloadScanner::scan):
+        Move scanCommon here and simplify the code.
+
+        * html/parser/CSSPreloadScanner.h:
+        Remove scanCommon.
+
+        * html/parser/HTMLPreloadScanner.cpp:
+        (WebCore::TokenPreloadScanner::tagIdFor):
+        TagId is a strong enum now, update enum values.
+
+        (WebCore::TokenPreloadScanner::initiatorFor):
+        Ditto.
+
+        (WebCore::TokenPreloadScanner::StartTagScanner::processAttributes):
+        Ditto.
+
+        (WebCore::TokenPreloadScanner::StartTagScanner::match):
+        Indent this.
+
+        (WebCore::TokenPreloadScanner::StartTagScanner::processAttribute):
+        TagId is a strong enum now, update enum values.
+
+        (WebCore::TokenPreloadScanner::StartTagScanner::charset):
+        Ditto.
+
+        (WebCore::TokenPreloadScanner::StartTagScanner::resourceType):
+        Ditto.
+
+        (WebCore::TokenPreloadScanner::StartTagScanner::shouldPreload):
+        Ditto.
+
+        (WebCore::TokenPreloadScanner::scan):
+        Merge scan and scanCommon.
+
+        * html/parser/HTMLPreloadScanner.h:
+
 2014-01-18  Lauro Neto  <lauro.neto@openbossa.org>
 
         Remove Nix files from WebCore
index 57d5d11..74f832a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008, 2010, 2013 Apple Inc. All Rights Reserved.
+ * Copyright (C) 2008, 2010, 2013, 2014 Apple Inc. All Rights Reserved.
  * Copyright (C) 2009 Torch Mobile, Inc. http://www.torchmobile.com/
  * Copyright (C) 2010 Google Inc. All Rights Reserved.
  *
 #include "CSSPreloadScanner.h"
 
 #include "HTMLParserIdioms.h"
+#include <wtf/TemporaryChange.h>
 
 namespace WebCore {
 
 CSSPreloadScanner::CSSPreloadScanner()
     : m_state(Initial)
-    , m_requests(0)
+    , m_requests(nullptr)
 {
 }
 
@@ -49,33 +50,18 @@ void CSSPreloadScanner::reset()
     m_ruleValue.clear();
 }
 
-template<typename Char>
-void CSSPreloadScanner::scanCommon(const Char* begin, const Char* end, PreloadRequestStream& requests)
-{
-    m_requests = &requests;
-    for (const Char* it = begin; it != end && m_state != DoneParsingImportRules; ++it)
-        tokenize(*it);
-    m_requests = 0;
-}
-
 void CSSPreloadScanner::scan(const HTMLToken::DataVector& data, PreloadRequestStream& requests)
 {
-    scanCommon(data.data(), data.data() + data.size(), requests);
-}
+    ASSERT(!m_requests);
+    TemporaryChange<PreloadRequestStream*> change(m_requests, &requests);
 
-#if ENABLE(THREADED_HTML_PARSER)
-void CSSPreloadScanner::scan(const HTMLIdentifier& identifier, PreloadRequestStream& requests)
-{
-    const StringImpl* data = identifier.asStringImpl();
-    if (data->is8Bit()) {
-        const LChar* begin = data->characters8();
-        scanCommon(begin, begin + data->length(), requests);
-        return;
+    for (UChar c : data) {
+        tokenize(c);
+
+        if (m_state == DoneParsingImportRules)
+            break;
     }
-    const UChar* begin = data->characters16();
-    scanCommon(begin, begin + data->length(), requests);
 }
-#endif
 
 inline void CSSPreloadScanner::tokenize(UChar c)
 {
index 946649f..1dca288 100644 (file)
@@ -33,8 +33,6 @@
 
 namespace WebCore {
 
-class HTMLIdentifier;
-
 class CSSPreloadScanner {
     WTF_MAKE_NONCOPYABLE(CSSPreloadScanner);
 public:
@@ -44,7 +42,6 @@ public:
     void reset();
 
     void scan(const HTMLToken::DataVector&, PreloadRequestStream&);
-    void scan(const HTMLIdentifier&, PreloadRequestStream&);
 
 private:
     enum State {
@@ -60,9 +57,6 @@ private:
         DoneParsingImportRules,
     };
 
-    template<typename Char>
-    void scanCommon(const Char* begin, const Char* end, PreloadRequestStream&);
-
     inline void tokenize(UChar);
     void emitRule();
 
index 1601fc3..28caa69 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Apple Inc. All Rights Reserved.
+ * Copyright (C) 2008, 2014 Apple Inc. All Rights Reserved.
  * Copyright (C) 2009 Torch Mobile, Inc. http://www.torchmobile.com/
  * Copyright (C) 2010 Google Inc. All Rights Reserved.
  *
@@ -43,58 +43,37 @@ TokenPreloadScanner::TagId TokenPreloadScanner::tagIdFor(const HTMLToken::DataVe
 {
     AtomicString tagName(data);
     if (tagName == imgTag)
-        return ImgTagId;
+        return TagId::Img;
     if (tagName == inputTag)
-        return InputTagId;
+        return TagId::Input;
     if (tagName == linkTag)
-        return LinkTagId;
+        return TagId::Link;
     if (tagName == scriptTag)
-        return ScriptTagId;
+        return TagId::Script;
     if (tagName == styleTag)
-        return StyleTagId;
+        return TagId::Style;
     if (tagName == baseTag)
-        return BaseTagId;
+        return TagId::Base;
     if (tagName == templateTag)
-        return TemplateTagId;
-    return UnknownTagId;
+        return TagId::Template;
+    return TagId::Unknown;
 }
 
-#if ENABLE(THREADED_HTML_PARSER)
-TokenPreloadScanner::TagId TokenPreloadScanner::tagIdFor(const HTMLIdentifier& tagName)
-{
-    if (threadSafeHTMLNamesMatch(tagName, imgTag))
-        return ImgTagId;
-    if (threadSafeHTMLNamesMatch(tagName, inputTag))
-        return InputTagId;
-    if (threadSafeHTMLNamesMatch(tagName, linkTag))
-        return LinkTagId;
-    if (threadSafeHTMLNamesMatch(tagName, scriptTag))
-        return ScriptTagId;
-    if (threadSafeHTMLNamesMatch(tagName, styleTag))
-        return StyleTagId;
-    if (threadSafeHTMLNamesMatch(tagName, baseTag))
-        return BaseTagId;
-    if (threadSafeHTMLNamesMatch(tagName, templateTag))
-        return TemplateTagId;
-    return UnknownTagId;
-}
-#endif
-
 String TokenPreloadScanner::initiatorFor(TagId tagId)
 {
     switch (tagId) {
-    case ImgTagId:
+    case TagId::Img:
         return "img";
-    case InputTagId:
+    case TagId::Input:
         return "input";
-    case LinkTagId:
+    case TagId::Link:
         return "link";
-    case ScriptTagId:
+    case TagId::Script:
         return "script";
-    case UnknownTagId:
-    case StyleTagId:
-    case BaseTagId:
-    case TemplateTagId:
+    case TagId::Unknown:
+    case TagId::Style:
+    case TagId::Base:
+    case TagId::Template:
         ASSERT_NOT_REACHED();
         return "unknown";
     }
@@ -115,7 +94,7 @@ public:
     void processAttributes(const HTMLToken::AttributeList& attributes)
     {
         ASSERT(isMainThread());
-        if (m_tagId >= UnknownTagId)
+        if (m_tagId >= TagId::Unknown)
             return;
         for (HTMLToken::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter) {
             AtomicString attributeName(iter->name);
@@ -128,18 +107,7 @@ public:
             String srcMatchingScale = bestFitSourceForImageAttributes(m_deviceScaleFactor, m_urlToLoad, m_srcSetAttribute);
             setUrlToLoad(srcMatchingScale, true);
         }
-
-    }
-
-#if ENABLE(THREADED_HTML_PARSER)
-    void processAttributes(const Vector<CompactHTMLToken::Attribute>& attributes)
-    {
-        if (m_tagId >= UnknownTagId)
-            return;
-        for (Vector<CompactHTMLToken::Attribute>::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
-            processAttribute(iter->name, iter->value);
     }
-#endif
 
     OwnPtr<PreloadRequest> createPreloadRequest(const URL& predictedBaseURL)
     {
@@ -152,18 +120,11 @@ public:
         return request.release();
     }
 
-static bool match(const AtomicString& name, const QualifiedName& qName)
-{
-    ASSERT(isMainThread());
-    return qName.localName() == name;
-}
-
-#if ENABLE(THREADED_HTML_PARSER)
-static bool match(const HTMLIdentifier& name, const QualifiedName& qName)
-{
-    return threadSafeHTMLNamesMatch(name, qName);
-}
-#endif
+    static bool match(const AtomicString& name, const QualifiedName& qName)
+    {
+        ASSERT(isMainThread());
+        return qName.localName() == name;
+    }
 
 private:
     template<typename NameType>
@@ -172,21 +133,21 @@ private:
         if (match(attributeName, charsetAttr))
             m_charset = attributeValue;
 
-        if (m_tagId == ScriptTagId || m_tagId == ImgTagId) {
+        if (m_tagId == TagId::Script || m_tagId == TagId::Img) {
             if (match(attributeName, srcAttr))
                 setUrlToLoad(attributeValue);
             else if (match(attributeName, srcsetAttr))
                 m_srcSetAttribute = attributeValue;
             else if (match(attributeName, crossoriginAttr) && !attributeValue.isNull())
                 m_crossOriginMode = stripLeadingAndTrailingHTMLSpaces(attributeValue);
-        } else if (m_tagId == LinkTagId) {
+        } else if (m_tagId == TagId::Link) {
             if (match(attributeName, hrefAttr))
                 setUrlToLoad(attributeValue);
             else if (match(attributeName, relAttr))
                 m_linkIsStyleSheet = relAttributeIsStyleSheet(attributeValue);
             else if (match(attributeName, mediaAttr))
                 m_mediaAttribute = attributeValue;
-        } else if (m_tagId == InputTagId) {
+        } else if (m_tagId == TagId::Input) {
             if (match(attributeName, srcAttr))
                 setUrlToLoad(attributeValue);
             else if (match(attributeName, typeAttr))
@@ -215,18 +176,18 @@ private:
     const String& charset() const
     {
         // FIXME: Its not clear that this if is needed, the loader probably ignores charset for image requests anyway.
-        if (m_tagId == ImgTagId)
+        if (m_tagId == TagId::Img)
             return emptyString();
         return m_charset;
     }
 
     CachedResource::Type resourceType() const
     {
-        if (m_tagId == ScriptTagId)
+        if (m_tagId == TagId::Script)
             return CachedResource::Script;
-        if (m_tagId == ImgTagId || (m_tagId == InputTagId && m_inputIsImage))
+        if (m_tagId == TagId::Img || (m_tagId == TagId::Input && m_inputIsImage))
             return CachedResource::ImageResource;
-        if (m_tagId == LinkTagId && m_linkIsStyleSheet)
+        if (m_tagId == TagId::Link && m_linkIsStyleSheet)
             return CachedResource::CSSStyleSheet;
         ASSERT_NOT_REACHED();
         return CachedResource::RawResource;
@@ -237,10 +198,10 @@ private:
         if (m_urlToLoad.isEmpty())
             return false;
 
-        if (m_tagId == LinkTagId && !m_linkIsStyleSheet)
+        if (m_tagId == TagId::Link && !m_linkIsStyleSheet)
             return false;
 
-        if (m_tagId == InputTagId && !m_inputIsImage)
+        if (m_tagId == TagId::Input && !m_inputIsImage)
             return false;
 
         return true;
@@ -302,42 +263,30 @@ void TokenPreloadScanner::rewindTo(TokenPreloadScannerCheckpoint checkpointIndex
 
 void TokenPreloadScanner::scan(const HTMLToken& token, Vector<OwnPtr<PreloadRequest>>& requests)
 {
-    scanCommon(token, requests);
-}
-
-#if ENABLE(THREADED_HTML_PARSER)
-void TokenPreloadScanner::scan(const CompactHTMLToken& token, Vector<OwnPtr<PreloadRequest>>& requests)
-{
-    scanCommon(token, requests);
-}
-#endif
-
-template<typename Token>
-void TokenPreloadScanner::scanCommon(const Token& token, Vector<OwnPtr<PreloadRequest>>& requests)
-{
     switch (token.type()) {
-    case HTMLToken::Character: {
+    case HTMLToken::Character:
         if (!m_inStyle)
             return;
         m_cssScanner.scan(token.data(), requests);
         return;
-    }
+
     case HTMLToken::EndTag: {
         TagId tagId = tagIdFor(token.data());
 #if ENABLE(TEMPLATE_ELEMENT)
-        if (tagId == TemplateTagId) {
+        if (tagId == TagId::Template) {
             if (m_templateCount)
                 --m_templateCount;
             return;
         }
 #endif
-        if (tagId == StyleTagId) {
+        if (tagId == TagId::Style) {
             if (m_inStyle)
                 m_cssScanner.reset();
             m_inStyle = false;
         }
         return;
     }
+
     case HTMLToken::StartTag: {
 #if ENABLE(TEMPLATE_ELEMENT)
         if (m_templateCount)
@@ -345,16 +294,16 @@ void TokenPreloadScanner::scanCommon(const Token& token, Vector<OwnPtr<PreloadRe
 #endif
         TagId tagId = tagIdFor(token.data());
 #if ENABLE(TEMPLATE_ELEMENT)
-        if (tagId == TemplateTagId) {
+        if (tagId == TagId::Template) {
             ++m_templateCount;
             return;
         }
 #endif
-        if (tagId == StyleTagId) {
+        if (tagId == TagId::Style) {
             m_inStyle = true;
             return;
         }
-        if (tagId == BaseTagId) {
+        if (tagId == TagId::Base) {
             // The first <base> element is the one that wins.
             if (!m_predictedBaseElementURL.isEmpty())
                 return;
@@ -364,15 +313,14 @@ void TokenPreloadScanner::scanCommon(const Token& token, Vector<OwnPtr<PreloadRe
 
         StartTagScanner scanner(tagId, m_deviceScaleFactor);
         scanner.processAttributes(token.attributes());
-        OwnPtr<PreloadRequest> request = scanner.createPreloadRequest(m_predictedBaseElementURL);
-        if (request)
+        if (OwnPtr<PreloadRequest> request = scanner.createPreloadRequest(m_predictedBaseElementURL))
             requests.append(request.release());
         return;
     }
-    default: {
+
+    default:
         return;
     }
-    }
 }
 
 template<typename Token>
index ace9447..faeec8c 100644 (file)
@@ -47,9 +47,6 @@ public:
     ~TokenPreloadScanner();
 
     void scan(const HTMLToken&, PreloadRequestStream& requests);
-#if ENABLE(THREADED_HTML_PARSER)
-    void scan(const CompactHTMLToken&, PreloadRequestStream& requests);
-#endif
 
     void setPredictedBaseElementURL(const URL& url) { m_predictedBaseElementURL = url; }
 
@@ -65,27 +62,23 @@ public:
     }
 
 private:
-    enum TagId {
+    enum class TagId {
         // These tags are scanned by the StartTagScanner.
-        ImgTagId,
-        InputTagId,
-        LinkTagId,
-        ScriptTagId,
+        Img,
+        Input,
+        Link,
+        Script,
 
         // These tags are not scanned by the StartTagScanner.
-        UnknownTagId,
-        StyleTagId,
-        BaseTagId,
-        TemplateTagId,
+        Unknown,
+        Style,
+        Base,
+        Template,
     };
 
     class StartTagScanner;
 
-    template<typename Token>
-    inline void scanCommon(const Token&, PreloadRequestStream& requests);
-
     static TagId tagIdFor(const HTMLToken::DataVector&);
-    static TagId tagIdFor(const HTMLIdentifier&);
 
     static String initiatorFor(TagId);