Avoid evicting link preload resources when parsing is done.
authoryoav@yoav.ws <yoav@yoav.ws@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 3 Feb 2017 21:52:50 +0000 (21:52 +0000)
committeryoav@yoav.ws <yoav@yoav.ws@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 3 Feb 2017 21:52:50 +0000 (21:52 +0000)
commitb82f27978883bb5d4da26c1bdf766adb6396d0ff
treee04514c941f08c7a2cc347b3861fec309f821a7d
parent9c1c8a5a5076a3287d7bba65559d309789d4c5ff
Avoid evicting link preload resources when parsing is done.
https://bugs.webkit.org/show_bug.cgi?id=167415

Reviewed by Ryosuke Niwa.

Source/WebCore:

Currently all preloads (speculative and link preload) are being cleared when the document has finished parsing.
When it comes to link preloads, it can result in resources being cleared before the page had a chance to use them. (e.g. resources
that are preloaded in order to be loaded through script after DOMContentLoaded)

This patch fixes that by marking link preload resources as such, so that they can be handled separately inside clearPreloads().
As this fix also exposed an issue with load cancelation with invalid hrefs (which tests were passing before due to the preloads
being cleared), said issue is also fixed by clearing previousely preloaded resources if an invalid link preload is later detected.

Test: http/tests/preload/not_evicting_preload_at_onload.html

* dom/Document.cpp:
(WebCore::Document::finishedParsing): Only clear speculative preloads when parsing is finished.
* loader/LinkLoader.cpp:
(WebCore::LinkLoader::preloadIfNeeded): Set request flag indicating link preload.
(WebCore::LinkLoader::loadLink): Clear previousely preloaded resource to cancel their load.
* loader/LinkPreloadResourceClients.h:
(WebCore::LinkPreloadResourceClient::clearResource): Call cancelLoad() when the client is cleared.
* loader/cache/CachedResource.h:
(WebCore::CachedResource::isLinkPreload):
(WebCore::CachedResource::setLinkPreload):
* loader/cache/CachedResource.cpp:
(WebCore::CachedResource::CachedResource): Initialize m_isLinkPreload with the request's value.
* loader/cache/CachedResourceLoader.cpp:
(WebCore::CachedResourceLoader::~CachedResourceLoader):
(WebCore::CachedResourceLoader::requestResource): Mirror the request link preload flag to the resource if it's fetched from cache.
(WebCore::CachedResourceLoader::clearPreloads): Add a "speculative only" mode, which doesn't clear link preloads.
* loader/cache/CachedResourceLoader.h:
* loader/cache/CachedResourceRequest.cpp:
(WebCore::CachedResourceRequest::CachedResourceRequest):
* loader/cache/CachedResourceRequest.h:
(WebCore::CachedResourceRequest::isLinkPreload):
(WebCore::CachedResourceRequest::setIsLinkPreload):

LayoutTests:

* http/tests/preload/dynamic_remove_preload_href.html: Test passed before for the wrong reasons. Cache-busting in order for it to genuinely pass.
* http/tests/preload/not_delaying_window_onload_before_discovery.html: Test passed before for the wrong reasons. Cache-busting in order for it to genuinely pass.
* http/tests/preload/not_evicting_preload_at_onload-expected.txt: Added.
* http/tests/preload/not_evicting_preload_at_onload.html: Added.
* platform/mac/TestExpectations: Skipping http/tests/preload/dynamic_removing_preload.html due to https://bugs.webkit.org/show_bug.cgi?id=167792

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@211649 268f45cc-cd09-0410-ab3c-d52691b4dbfc
15 files changed:
LayoutTests/ChangeLog
LayoutTests/TestExpectations
LayoutTests/http/tests/preload/dynamic_remove_preload_href-expected.txt
LayoutTests/http/tests/preload/dynamic_remove_preload_href.html
LayoutTests/http/tests/preload/not_delaying_window_onload_before_discovery.html
LayoutTests/http/tests/preload/not_evicting_preload_at_onload-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/preload/not_evicting_preload_at_onload.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/dom/Document.cpp
Source/WebCore/loader/LinkLoader.cpp
Source/WebCore/loader/cache/CachedResource.cpp
Source/WebCore/loader/cache/CachedResource.h
Source/WebCore/loader/cache/CachedResourceLoader.cpp
Source/WebCore/loader/cache/CachedResourceLoader.h
Source/WebCore/loader/cache/CachedResourceRequest.h