Changeset 216143 in webkit


Ignore:
Timestamp:
May 3, 2017 2:55:25 PM (7 years ago)
Author:
yoav@yoav.ws
Message:

Link preload HTMLPreloadScanner support
https://bugs.webkit.org/show_bug.cgi?id=170747

Reviewed by Youenn Fablet.

Source/WebCore:

Test: http/tests/preload/preloadscanner_download_resources.html

  • html/parser/HTMLPreloadScanner.cpp:

(WebCore::TokenPreloadScanner::StartTagScanner::StartTagScanner): Initialize link preload flag.
(WebCore::TokenPreloadScanner::StartTagScanner::createPreloadRequest): Create a request only if the type is known (so ignore
preloads with unknown type).
(WebCore::TokenPreloadScanner::StartTagScanner::processAttribute): Add handling for link preload and the as attribute.
(WebCore::TokenPreloadScanner::StartTagScanner::relAttributeIsStyleSheet): Get LinkRelAttribute as input.
(WebCore::TokenPreloadScanner::StartTagScanner::resourceType): Return an std::optional, in case the preload type is unknown.
(WebCore::TokenPreloadScanner::StartTagScanner::shouldPreload): Return true for the link preload case.

  • loader/cache/CachedResourceLoader.cpp:

(WebCore::CachedResourceLoader::preload): Return the resource rather than a nullptr if it's already in m_preloads.

LayoutTests:

  • http/tests/preload/preloadscanner_download_resources-expected.txt: Added.
  • http/tests/preload/preloadscanner_download_resources.html: Added.
Location:
trunk
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r216142 r216143  
     12017-05-03  Yoav Weiss  <yoav@yoav.ws>
     2
     3        Link preload HTMLPreloadScanner support
     4        https://bugs.webkit.org/show_bug.cgi?id=170747
     5
     6        Reviewed by Youenn Fablet.
     7
     8        * http/tests/preload/preloadscanner_download_resources-expected.txt: Added.
     9        * http/tests/preload/preloadscanner_download_resources.html: Added.
     10
    1112017-05-03  Ryan Haddad  <ryanhaddad@apple.com>
    212
  • trunk/Source/WebCore/ChangeLog

    r216139 r216143  
     12017-05-03  Yoav Weiss  <yoav@yoav.ws>
     2
     3        Link preload HTMLPreloadScanner support
     4        https://bugs.webkit.org/show_bug.cgi?id=170747
     5
     6        Reviewed by Youenn Fablet.
     7
     8        Test: http/tests/preload/preloadscanner_download_resources.html
     9
     10        * html/parser/HTMLPreloadScanner.cpp:
     11        (WebCore::TokenPreloadScanner::StartTagScanner::StartTagScanner): Initialize link preload flag.
     12        (WebCore::TokenPreloadScanner::StartTagScanner::createPreloadRequest): Create a request only if the type is known (so ignore
     13        preloads with unknown type).
     14        (WebCore::TokenPreloadScanner::StartTagScanner::processAttribute): Add handling for link preload and the `as` attribute.
     15        (WebCore::TokenPreloadScanner::StartTagScanner::relAttributeIsStyleSheet): Get LinkRelAttribute as input.
     16        (WebCore::TokenPreloadScanner::StartTagScanner::resourceType): Return an std::optional, in case the preload type is unknown.
     17        (WebCore::TokenPreloadScanner::StartTagScanner::shouldPreload): Return true for the link preload case.
     18        * loader/cache/CachedResourceLoader.cpp:
     19        (WebCore::CachedResourceLoader::preload): Return the resource rather than a nullptr if it's already in m_preloads.
     20
    1212017-05-03  Michael Catanzaro  <mcatanzaro@igalia.com>
    222
  • trunk/Source/WebCore/html/parser/HTMLPreloadScanner.cpp

    r210828 r216143  
    3434#include "HTMLTokenizer.h"
    3535#include "InputTypeNames.h"
     36#include "LinkLoader.h"
    3637#include "LinkRelAttribute.h"
    3738#include "MediaList.h"
     
    101102        : m_tagId(tagId)
    102103        , m_linkIsStyleSheet(false)
     104        , m_linkIsPreload(false)
    103105        , m_metaIsViewport(false)
    104106        , m_inputIsImage(false)
     
    145147            return nullptr;
    146148
    147         auto request = std::make_unique<PreloadRequest>(initiatorFor(m_tagId), m_urlToLoad, predictedBaseURL, resourceType(), m_mediaAttribute, m_moduleScript);
     149        auto type = resourceType();
     150        if (!type)
     151            return nullptr;
     152        auto request = std::make_unique<PreloadRequest>(initiatorFor(m_tagId), m_urlToLoad, predictedBaseURL, type.value(), m_mediaAttribute, m_moduleScript);
    148153        request->setCrossOriginMode(m_crossOriginMode);
    149154        request->setNonce(m_nonceAttribute);
     
    221226            if (match(attributeName, hrefAttr))
    222227                setUrlToLoad(attributeValue);
    223             else if (match(attributeName, relAttr))
    224                 m_linkIsStyleSheet = relAttributeIsStyleSheet(attributeValue);
    225             else if (match(attributeName, mediaAttr))
     228            else if (match(attributeName, relAttr)) {
     229                LinkRelAttribute parsedAttribute { attributeValue };
     230                m_linkIsStyleSheet = relAttributeIsStyleSheet(parsedAttribute);
     231                m_linkIsPreload = parsedAttribute.isLinkPreload;
     232            } else if (match(attributeName, mediaAttr))
    226233                m_mediaAttribute = attributeValue;
    227234            else if (match(attributeName, charsetAttr))
     
    231238            else if (match(attributeName, nonceAttr))
    232239                m_nonceAttribute = attributeValue;
     240            else if (match(attributeName, asAttr))
     241                m_asAttribute = attributeValue;
    233242            break;
    234243        case TagId::Input:
     
    253262    }
    254263
    255     static bool relAttributeIsStyleSheet(const String& attributeValue)
    256     {
    257         LinkRelAttribute parsedAttribute { attributeValue };
     264    static bool relAttributeIsStyleSheet(const LinkRelAttribute& parsedAttribute)
     265    {
    258266        return parsedAttribute.isStyleSheet && !parsedAttribute.isAlternate && !parsedAttribute.iconType && !parsedAttribute.isDNSPrefetch;
    259267    }
     
    276284    }
    277285
    278     CachedResource::Type resourceType() const
     286    std::optional<CachedResource::Type> resourceType() const
    279287    {
    280288        switch (m_tagId) {
     
    287295            return CachedResource::ImageResource;
    288296        case TagId::Link:
    289             ASSERT(m_linkIsStyleSheet);
    290             return CachedResource::CSSStyleSheet;
     297            if (m_linkIsStyleSheet)
     298                return CachedResource::CSSStyleSheet;
     299            if (m_linkIsPreload)
     300                return LinkLoader::resourceTypeFromAsAttribute(m_asAttribute);
     301            break;
    291302        case TagId::Meta:
    292303        case TagId::Unknown:
     
    309320            return false;
    310321
    311         if (m_tagId == TagId::Link && !m_linkIsStyleSheet)
     322        if (m_tagId == TagId::Link && !m_linkIsStyleSheet && !m_linkIsPreload)
    312323            return false;
    313324
     
    326337    String m_crossOriginMode;
    327338    bool m_linkIsStyleSheet;
     339    bool m_linkIsPreload;
    328340    String m_mediaAttribute;
    329341    String m_nonceAttribute;
    330342    String m_metaContent;
     343    String m_asAttribute;
    331344    bool m_metaIsViewport;
    332345    bool m_inputIsImage;
  • trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp

    r216051 r216143  
    12231223
    12241224    CachedResourceHandle<CachedResource> resource = requestResource(type, WTFMove(request), ForPreload::Yes);
    1225     if (!resource || (m_preloads && m_preloads->contains(resource.get())))
    1226         return nullptr;
    1227     // Fonts need special treatment since just creating the resource doesn't trigger a load.
    1228     if (type == CachedResource::FontResource)
    1229         downcast<CachedFont>(resource.get())->beginLoadIfNeeded(*this);
    1230     resource->increasePreloadCount();
    1231 
    1232     if (!m_preloads)
    1233         m_preloads = std::make_unique<ListHashSet<CachedResource*>>();
    1234     m_preloads->add(resource.get());
    1235 
     1225    if (resource && (!m_preloads || !m_preloads->contains(resource.get()))) {
     1226        // Fonts need special treatment since just creating the resource doesn't trigger a load.
     1227        if (type == CachedResource::FontResource)
     1228            downcast<CachedFont>(resource.get())->beginLoadIfNeeded(*this);
     1229        resource->increasePreloadCount();
     1230
     1231        if (!m_preloads)
     1232            m_preloads = std::make_unique<ListHashSet<CachedResource*>>();
     1233        m_preloads->add(resource.get());
     1234    }
    12361235    return resource;
    12371236}
Note: See TracChangeset for help on using the changeset viewer.