Add a warning for unused link preloads.
authoryoav@yoav.ws <yoav@yoav.ws@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 28 Mar 2017 11:56:52 +0000 (11:56 +0000)
committeryoav@yoav.ws <yoav@yoav.ws@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 28 Mar 2017 11:56:52 +0000 (11:56 +0000)
https://bugs.webkit.org/show_bug.cgi?id=165670

Reviewed by Youenn Fablet.

Source/WebCore:

Tests: http/tests/preload/single_download_preload_headers_charset.php
       http/tests/preload/unused_preload_warning.html

* dom/Document.cpp:
(WebCore::Document::prepareForDestruction): Stop the timer once the document is destructed.
* loader/LinkPreloadResourceClients.h: Add shouldMarkAsReferenced overides for the LinkPreloadResourceClient classes.
* loader/cache/CachedResource.cpp:
(WebCore::CachedResource::addClientToSet): Make sure LinkPreloadResourceClients don't set resource to be referenced.
* loader/cache/CachedResourceClient.h:
(WebCore::CachedResourceClient::shouldMarkAsReferenced): Make sure that ResourceClients mark preloads as referenced by default.
* loader/cache/CachedResourceLoader.cpp:
(WebCore::CachedResourceLoader::CachedResourceLoader): Initialize timer.
(WebCore::CachedResourceLoader::~CachedResourceLoader): Stop timer.
(WebCore::CachedResourceLoader::documentDidFinishLoadEvent): Trigger a timer if preloads weren't cleared at load time.
(WebCore::CachedResourceLoader::stopUnusedPreloadsTimer): Stop the timer.
(WebCore::CachedResourceLoader::warnUnusedPreloads): Iterate over m_preloads and issue a warning for non-referenced preloads.
* loader/cache/CachedResourceLoader.h:
* page/DOMWindow.cpp:
(WebCore::DOMWindow::willDetachDocumentFromFrame): Clear Resource Timing buffer when document detaches, to avoid test flakiness.

LayoutTests:

* TestExpectations: Added a "Failure Pass" for the flaky charset header test.
* http/tests/preload/download_resources-expected.txt:
* http/tests/preload/download_resources.html: Added references to preloaded resources.
* http/tests/preload/onerror_event-expected.txt:
* http/tests/preload/onerror_event.html: Added references to preloaded resources.
* http/tests/preload/onload_event-expected.txt:
* http/tests/preload/onload_event.html: Added references to preloaded resources.
* http/tests/preload/single_download_preload.html: Deflaked.
* http/tests/preload/single_download_preload_headers.php: Removed the charset to avoid double download bug.
* http/tests/preload/single_download_preload_headers_charset-expected.txt: Added.
* http/tests/preload/single_download_preload_headers_charset.php: Flaky test showing the double download bug when charset is declared.
* http/tests/preload/unused_preload_warning-expected.txt: Added.
* http/tests/preload/unused_preload_warning.html: Added.

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

22 files changed:
LayoutTests/ChangeLog
LayoutTests/TestExpectations
LayoutTests/http/tests/preload/download_resources-expected.txt
LayoutTests/http/tests/preload/download_resources.html
LayoutTests/http/tests/preload/onerror_event-expected.txt
LayoutTests/http/tests/preload/onerror_event.html
LayoutTests/http/tests/preload/onload_event-expected.txt
LayoutTests/http/tests/preload/onload_event.html
LayoutTests/http/tests/preload/single_download_preload.html
LayoutTests/http/tests/preload/single_download_preload_headers.php
LayoutTests/http/tests/preload/single_download_preload_headers_charset-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/preload/single_download_preload_headers_charset.php [new file with mode: 0644]
LayoutTests/http/tests/preload/unused_preload_warning-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/preload/unused_preload_warning.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/dom/Document.cpp
Source/WebCore/loader/LinkPreloadResourceClients.h
Source/WebCore/loader/cache/CachedResource.cpp
Source/WebCore/loader/cache/CachedResourceClient.h
Source/WebCore/loader/cache/CachedResourceLoader.cpp
Source/WebCore/loader/cache/CachedResourceLoader.h
Source/WebCore/page/DOMWindow.cpp

index 4118c93..620f486 100644 (file)
@@ -1,3 +1,24 @@
+2017-03-28  Yoav Weiss  <yoav@yoav.ws>
+
+        Add a warning for unused link preloads.
+        https://bugs.webkit.org/show_bug.cgi?id=165670
+
+        Reviewed by Youenn Fablet.
+
+        * TestExpectations: Added a "Failure Pass" for the flaky charset header test.
+        * http/tests/preload/download_resources-expected.txt:
+        * http/tests/preload/download_resources.html: Added references to preloaded resources.
+        * http/tests/preload/onerror_event-expected.txt:
+        * http/tests/preload/onerror_event.html: Added references to preloaded resources.
+        * http/tests/preload/onload_event-expected.txt:
+        * http/tests/preload/onload_event.html: Added references to preloaded resources.
+        * http/tests/preload/single_download_preload.html: Deflaked.
+        * http/tests/preload/single_download_preload_headers.php: Removed the charset to avoid double download bug.
+        * http/tests/preload/single_download_preload_headers_charset-expected.txt: Added.
+        * http/tests/preload/single_download_preload_headers_charset.php: Flaky test showing the double download bug when charset is declared.
+        * http/tests/preload/unused_preload_warning-expected.txt: Added.
+        * http/tests/preload/unused_preload_warning.html: Added.
+
 2017-03-28  Antoine Quint  <graouts@apple.com>
 
         REGRESSION: Double-clicking the captions button while the captions popover is open prevents the popover from being opened again
index 7e77547..3a1e21f 100644 (file)
@@ -1086,6 +1086,8 @@ fast/history/page-cache-with-opener.html [ Skip ]
 
 webkit.org/b/168238 imported/w3c/web-platform-tests/dom/events/EventListener-invoke-legacy.html [ Pass Failure ]
 
+webkit.org/b/170122 http/tests/preload/single_download_preload_headers_charset.php [ Pass Failure ]
+
 ########################################
 ### START OF -disabled tests
 
index e488a73..ce37c99 100644 (file)
@@ -1,13 +1,5 @@
-CONSOLE MESSAGE: line 11: <link rel=preload> must have a valid `as` value
-PASS internals.isPreloaded('../resources/dummy.js'); is true
-PASS internals.isPreloaded('../resources/dummy.css'); is true
-PASS internals.isPreloaded('../resources/square.png'); is true
-PASS internals.isPreloaded('../resources/Ahem.ttf'); is true
-PASS internals.isPreloaded('../resources/test.mp4'); is true
-PASS internals.isPreloaded('../security/resources/captions.vtt'); is true
-PASS internals.isPreloaded('../resources/dummy.xml?badvalue'); is false
-PASS internals.isPreloaded('../resources/dummy.xml'); is true
-PASS successfullyParsed is true
+CONSOLE MESSAGE: line 13: <link rel=preload> must have a valid `as` value
+This test makes sure that link preload preloads resources
 
-TEST COMPLETE
+PASS Makes sure that preloaded resources are downloaded 
 
index 3e6ea1b..42432a3 100644 (file)
@@ -1,27 +1,42 @@
 <!DOCTYPE html>
-<html>
-<head>
-<script src="/js-test-resources/js-test.js"></script>
+<script src="/js-test-resources/testharness.js"></script>
+<script src="/js-test-resources/testharnessreport.js"></script>
+<script>
+    var t = async_test('Makes sure that preloaded resources are downloaded');
+</script>
 <link rel=preload href="../resources/dummy.js" as=script>
 <link rel=preload href="../resources/dummy.css" as=style>
-<link rel=preload href="../resources/square.png" as=image>
-<link rel=preload href="../resources/Ahem.ttf" as=font crossorigin>
+<link rel=preload href="../resources/square100.png" as=image>
+<link rel=preload href="../resources/Ahem.woff" as=font crossorigin>
 <link rel=preload href="../resources/test.mp4" as=media>
 <link rel=preload href="../security/resources/captions.vtt" as=track>
 <link rel=preload href="../resources/dummy.xml?badvalue" as=foobarxmlthing>
 <link rel=preload href="../resources/dummy.xml">
-<script src="http://127.0.0.1:8000/resources/slow-script.pl?delay=100"></script>
-</head>
-<body>
+<script src="http://127.0.0.1:8000/resources/slow-script.pl?delay=400"></script>
 <script>
-    if (window.testRunner)
-        testRunner.dumpAsText();
-    shouldBeTrue("internals.isPreloaded('../resources/dummy.js');");
-    shouldBeTrue("internals.isPreloaded('../resources/dummy.css');");
-    shouldBeTrue("internals.isPreloaded('../resources/square.png');");
-    shouldBeTrue("internals.isPreloaded('../resources/Ahem.ttf');");
-    shouldBeTrue("internals.isPreloaded('../resources/test.mp4');");
-    shouldBeTrue("internals.isPreloaded('../security/resources/captions.vtt');");
-    shouldBeFalse("internals.isPreloaded('../resources/dummy.xml?badvalue');");
-    shouldBeTrue("internals.isPreloaded('../resources/dummy.xml');");
+    assert_true(internals.isPreloaded("../resources/dummy.js"));
+    assert_true(internals.isPreloaded("../resources/dummy.css"));
+    assert_true(internals.isPreloaded("../resources/square100.png"));
+    // FIXME: RT doesn't show downloads for the resources below. Need to investigate why.
+    assert_true(internals.isPreloaded("../resources/Ahem.woff"));
+    assert_true(internals.isPreloaded("../resources/test.mp4"));
+    assert_true(internals.isPreloaded("../security/resources/captions.vtt"));
+
+    assert_false(internals.isPreloaded("../resources/dummy.xml?badvalue"));
+    assert_true(internals.isPreloaded("../resources/dummy.xml"));
+    document.write('<script src="../resources/dummy.js"></scr' + 'ipt>' +
+                   '<link rel=stylesheet href="../resources/dummy.css">' +
+                   '<img src="../resources/square100.png">' +
+                   '<video><source src="../resources/test.mp4">' +
+                   '<track kind=subtitles src="../security/resources/captions.vtt" srclang=en>' +
+                   '</video>' +
+                   '<style>' +
+                   '    @font-face { font-family:ahem; src: url(../resources/Ahem.woff); }' +
+                   '    span { font-family: ahem, Arial; }' +
+                   '</style>' +
+                   '<span>This test makes sure that link preload preloads resources</span>');
+    var xhr = new XMLHttpRequest();
+    xhr.open("GET", "../resources/dummy.xml");
+    xhr.send();
+    t.done();
 </script>
index 2916af2..c280364 100644 (file)
@@ -1,12 +1,5 @@
-CONSOLE MESSAGE: line 27: <link rel=preload> must have a valid `as` value
-PASS successfullyParsed is true
+CONSOLE MESSAGE: line 25: <link rel=preload> must have a valid `as` value
+This test makes sure that link preload triggers error events for non-existing resources.
 
-TEST COMPLETE
-PASS styleFailed is true
-PASS scriptFailed is true
-PASS imageFailed is true
-PASS fontFailed is true
-PASS trackFailed is true
-PASS gibrishFailed is true
-PASS noTypeFailed is true
+PASS Makes sure that preloaded resources trigger onerror 
 
index d1ad228..476c5a1 100644 (file)
@@ -1,11 +1,9 @@
 <!DOCTYPE html>
+<script src="/js-test-resources/testharness.js"></script>
+<script src="/js-test-resources/testharnessreport.js"></script>
 <script>
-    if (window.testRunner) {
-        testRunner.dumpAsText()
-        testRunner.waitUntilDone();
-    }
+    var t = async_test('Makes sure that preloaded resources trigger onerror');
 </script>
-<script src="/js-test-resources/js-test.js"></script>
 <script>
     var scriptFailed = false;
     var styleFailed = false;
 <link rel=preload href="../non-existent/dummy.xml" as=foobarxmlthing onerror="count();gibrishFailed = true;" onload="count();">
 <link rel=preload href="http://127.0.0.1:9999/non-existent/dummy.xml" onerror="count();noTypeFailed = true;" onload="count();">
 <script>
+    document.write('<script src="../non-existent/dummy.js"></scr' + 'ipt>' +
+                   '<link rel=stylesheet href="../non-existent/dummy.css">' +
+                   '<img src="../non-existent/square.png">' +
+                   '<video><source src="test.mp4">' +
+                   '<track kind=subtitles src="../../non-existent/security/captions.vtt" srclang=en>' +
+                   '</video>' +
+                   '<style>' +
+                   '    @font-face { font-family:ahem; src: url(../../non-existent/Ahem.ttf); }' +
+                   '    span { font-family: ahem, Arial; }' +
+                   '</style>' +
+                   '<span>This test makes sure that link preload triggers error events for non-existing resources.</span>');
+    var xhr = new XMLHttpRequest();
+    xhr.open("GET", "../non-existent/dummy.xml");
+    xhr.send();
+    var xhr2 = new XMLHttpRequest();
+    xhr2.open("GET", "http://127.0.0.1:9999/non-existent/dummy.xml");
+    xhr2.send();
     function test() {
-        shouldBeTrue("styleFailed");
-        shouldBeTrue("scriptFailed");
-        shouldBeTrue("imageFailed");
-        shouldBeTrue("fontFailed");
-        shouldBeTrue("trackFailed");
-        shouldBeTrue("gibrishFailed");
-        shouldBeTrue("noTypeFailed");
-        if (window.testRunner)
-            testRunner.notifyDone();
+        assert_true(styleFailed);
+        assert_true(scriptFailed);
+        assert_true(imageFailed);
+        assert_true(fontFailed);
+        assert_true(trackFailed);
+        assert_true(gibrishFailed);
+        assert_true(noTypeFailed);
+        t.done();
     };
-    setInterval(function() {
+    setInterval(t.step_func(function() {
         if (window.counter >= 7)
             test();
-    }, 100);
+    }, 100));
 </script>
 
index 1f19053..41176bd 100644 (file)
@@ -15,4 +15,4 @@ PASS xsltLoaded is false
 PASS xsltErrored is true
 PASS noTypeLoaded is true
 PASS emptyTypeLoaded is true
-
+This test makes sure that link preload events are fired
index a6cba66..d5a3e6a 100644 (file)
@@ -29,7 +29,7 @@
 <link rel=preload href="../resources/square100.png" as=image onload="count(); imageLoaded = true;" onerror="count()">
 <link rel=preload href="../resources/Ahem.woff" as=font crossorigin onload="count(); fontLoaded = true;" onerror="count()">
 <link rel=preload href="../resources/test.ogv" as=media onload="count(); mediaLoaded = true;" onerror="count()">
-<link rel=preload href="../security/resources/captions.vtt" as=track onload="count(); trackLoaded = true;" onerror="count()">
+<link rel=preload href="../security/resources/captions.vtt" as=track onload="count(); trackLoaded = true;" onerror="count();">
 <link rel=preload href="../resources/dummy.xml" as=foobarxmlthing onload="count(); gibberishLoaded = true;" onerror="count(); gibberishErrored = true;">
 <link rel=preload href="../resources/dummy.xslt" as=xslt onload="count(); xsltLoaded = true;" onerror="count(); xsltErrored = true;">
 <link rel=preload href="../resources/dummy.xml" onload="count(); noTypeLoaded = true;" onerror="count()">
         if (window.testRunner)
             testRunner.notifyDone();
     }
-     addEventListener("load", function(){
+    document.write('<script src="../resources/dummy.js"></scr' + 'ipt>' +
+                   '<link rel=stylesheet href="../resources/dummy.css">' +
+                   '<img src="../resources/square.png">' +
+                   '<video><source src="test.mp4">' +
+                   '<track kind=subtitles src="../../security/resources/captions.vtt" srclang=en>' +
+                   '</video>' +
+                   '<style>' +
+                   '    @font-face { font-family:ahem; src: url(../../w3c/webperf/resources/Ahem.ttf); }' +
+                   '    span { font-family: ahem, Arial; }' +
+                   '</style>' +
+                   '<span>This test makes sure that link preload events are fired</span>');
+    var xhr = new XMLHttpRequest();
+    xhr.open("GET", "../resources/dummy.xml");
+    xhr.send();
+    var xhr2 = new XMLHttpRequest();
+    xhr2.open("GET", "../resources/dummy.xml?badvalue");
+    xhr2.send();
+    addEventListener("load", function(){
         setInterval(function() {
             if (window.counter >= 10)
                 test();
index f00fd03..2614b30 100644 (file)
             // FIXME: XHR should trigger a single download, but it downloads 2 resources instead.
             verifyDownloadNumber("http://127.0.0.1:8000/resources/dummy.xml", 2);
             // FIXME: We should verify for video and audio as well, but they seem to (flakily?) trigger multiple partial requests.
+            var xhr = new XMLHttpRequest();
+            xhr.open("GET", "../resources/dummy.xml");
+            xhr.send();
+            var xhr2 = new XMLHttpRequest();
+            xhr2.open("GET", "../resources/dummy.xml?badvalue");
+            xhr2.send();
             t.done();
             }), 100);
     }));
index 9352cb3..8747350 100644 (file)
@@ -11,7 +11,6 @@ header("Link: <http://127.0.0.1:8000/resources/dummy.xml?foobar>; rel=preload; a
 header("Link: <http://127.0.0.1:8000/resources/dummy.xml>; crossorigin; rel=preload", false);
 ?>
 <!DOCTYPE html>
-<meta charset="utf-8">
 <script src="/js-test-resources/testharness.js"></script>
 <script src="/js-test-resources/testharnessreport.js"></script>
 <script>
diff --git a/LayoutTests/http/tests/preload/single_download_preload_headers_charset-expected.txt b/LayoutTests/http/tests/preload/single_download_preload_headers_charset-expected.txt
new file mode 100644 (file)
index 0000000..3840395
--- /dev/null
@@ -0,0 +1,5 @@
+CONSOLE MESSAGE: <link rel=preload> must have a valid `as` value
+
+PASS Makes sure that preloaded resources are not downloaded again when used 
+
diff --git a/LayoutTests/http/tests/preload/single_download_preload_headers_charset.php b/LayoutTests/http/tests/preload/single_download_preload_headers_charset.php
new file mode 100644 (file)
index 0000000..9352cb3
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+header("Link: <http://127.0.0.1:8000/resources/dummy.js>; rel=preload; as=script", false);
+header("LiNk:<http://127.0.0.1:8000/resources/dummy.css>; rel=preload; as=style", false);
+header("Link: <http://127.0.0.1:8000/resources/square100.png>;rel=preload;as=image", false);
+header("Link: <http://127.0.0.1:8000/resources/square100.png?background>;rel=preload;as=image", false);
+header("Link: <http://127.0.0.1:8000/resources/Ahem.woff>; rel=preload; as=font; crossorigin", false);
+header("Link: <http://127.0.0.1:8000/resources/test.mp4>; rel=preload; as=media", false);
+header("Link: <http://127.0.0.1:8000/resources/test.oga>; rel=preload; as=media", false);
+header("link: <http://127.0.0.1:8000/security/resources/captions.vtt>; rel=preload; as=track", false);
+header("Link: <http://127.0.0.1:8000/resources/dummy.xml?foobar>; rel=preload; as=foobar", false);
+header("Link: <http://127.0.0.1:8000/resources/dummy.xml>; crossorigin; rel=preload", false);
+?>
+<!DOCTYPE html>
+<meta charset="utf-8">
+<script src="/js-test-resources/testharness.js"></script>
+<script src="/js-test-resources/testharnessreport.js"></script>
+<script>
+    var t = async_test('Makes sure that preloaded resources are not downloaded again when used');
+</script>
+<script src="http://127.0.0.1:8000/resources/slow-script.pl?delay=200"></script>
+<style>
+    #background {
+        width: 200px;
+        height: 200px;
+        background-image: url(http://127.0.0.1:8000/resources/square100.png?background);
+    }
+    @font-face {
+      font-family:ahem;
+      src: url(http://127.0.0.1:8000/resources/Ahem.woff);
+    }
+    span { font-family: ahem, Arial; }
+</style>
+<link rel="stylesheet" href="http://127.0.0.1:8000/resources/dummy.css">
+<script src="http://127.0.0.1:8000/resources/dummy.js"></script>
+<div id="background"></div>
+<img src="http://127.0.0.1:8000/resources/square100.png">
+<video src="http://127.0.0.1:8000/resources/test.mp4">
+    <track kind=subtitles src="http://127.0.0.1:8000/security/resources/captions.vtt" srclang=en>
+</video>
+<audio src="http://127.0.0.1:8000/resources/test.oga"></audio>
+<script>
+    var xhr = new XMLHttpRequest();
+    xhr.open("GET", "http://127.0.0.1:8000/resources/dummy.xml");
+    xhr.send();
+
+    window.addEventListener("load", t.step_func(function() {
+        function verifyDownloadNumber(url, number) {
+            assert_equals(performance.getEntriesByName(url).length, number, url);
+        }
+        setTimeout(t.step_func(function() {
+            verifyDownloadNumber("http://127.0.0.1:8000/resources/dummy.js", 1);
+            verifyDownloadNumber("http://127.0.0.1:8000/resources/dummy.css", 1);
+            verifyDownloadNumber("http://127.0.0.1:8000/resources/square100.png", 1);
+            verifyDownloadNumber("http://127.0.0.1:8000/resources/square100.png?background", 1);
+            verifyDownloadNumber("http://127.0.0.1:8000/resources/Ahem.woff", 1);
+            verifyDownloadNumber("http://127.0.0.1:8000/resources/dummy.xml?foobar", 0);
+            verifyDownloadNumber("http://127.0.0.1:8000/security/resources/captions.vtt", 1);
+            // FIXME: XHR should trigger a single download, but it downloads 2 resources instead.
+            verifyDownloadNumber("http://127.0.0.1:8000/resources/dummy.xml", 2);
+            // FIXME: We should verify for video and audio as well, but they seem to (flakily?) trigger multiple partial requests.
+            t.done();
+            }), 100);
+    }));
+</script>
diff --git a/LayoutTests/http/tests/preload/unused_preload_warning-expected.txt b/LayoutTests/http/tests/preload/unused_preload_warning-expected.txt
new file mode 100644 (file)
index 0000000..14ac7fa
--- /dev/null
@@ -0,0 +1,2 @@
+CONSOLE MESSAGE: The resource http://127.0.0.1:8000/resources/square100.png?name=value was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it wasn't preloaded for nothing.
+ This test verifies that unused preload resources (and only unused ones) issue a warning.
diff --git a/LayoutTests/http/tests/preload/unused_preload_warning.html b/LayoutTests/http/tests/preload/unused_preload_warning.html
new file mode 100644 (file)
index 0000000..203a189
--- /dev/null
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<script>
+    if (window.testRunner) {
+        testRunner.waitUntilDone();
+        testRunner.dumpAsText();
+    }
+    window.onload = function() {
+        window.setTimeout(function() {
+            if (window.testRunner)
+                testRunner.notifyDone();
+        }, 4000);
+    };
+</script>
+<link rel="preload" as="image" href="../resources/square100.png">
+<link rel="preload" as="image" href="../resources/square100.png?name=value">
+<img src="../resources/square100.png">
+This test verifies that unused preload resources (and only unused ones) issue a warning.
index e298c05..225850b 100644 (file)
@@ -1,3 +1,30 @@
+2017-03-28  Yoav Weiss  <yoav@yoav.ws>
+
+        Add a warning for unused link preloads.
+        https://bugs.webkit.org/show_bug.cgi?id=165670
+
+        Reviewed by Youenn Fablet.
+
+        Tests: http/tests/preload/single_download_preload_headers_charset.php
+               http/tests/preload/unused_preload_warning.html
+
+        * dom/Document.cpp:
+        (WebCore::Document::prepareForDestruction): Stop the timer once the document is destructed.
+        * loader/LinkPreloadResourceClients.h: Add shouldMarkAsReferenced overides for the LinkPreloadResourceClient classes.
+        * loader/cache/CachedResource.cpp:
+        (WebCore::CachedResource::addClientToSet): Make sure LinkPreloadResourceClients don't set resource to be referenced.
+        * loader/cache/CachedResourceClient.h:
+        (WebCore::CachedResourceClient::shouldMarkAsReferenced): Make sure that ResourceClients mark preloads as referenced by default.
+        * loader/cache/CachedResourceLoader.cpp:
+        (WebCore::CachedResourceLoader::CachedResourceLoader): Initialize timer.
+        (WebCore::CachedResourceLoader::~CachedResourceLoader): Stop timer.
+        (WebCore::CachedResourceLoader::documentDidFinishLoadEvent): Trigger a timer if preloads weren't cleared at load time.
+        (WebCore::CachedResourceLoader::stopUnusedPreloadsTimer): Stop the timer.
+        (WebCore::CachedResourceLoader::warnUnusedPreloads): Iterate over m_preloads and issue a warning for non-referenced preloads.
+        * loader/cache/CachedResourceLoader.h:
+        * page/DOMWindow.cpp:
+        (WebCore::DOMWindow::willDetachDocumentFromFrame): Clear Resource Timing buffer when document detaches, to avoid test flakiness.
+
 2017-03-28  Antoine Quint  <graouts@apple.com>
 
         REGRESSION: Double-clicking the captions button while the captions popover is open prevents the popover from being opened again
index f7eeab0..c036813 100644 (file)
@@ -2265,6 +2265,8 @@ void Document::prepareForDestruction()
     }
 #endif
 
+    m_cachedResourceLoader->stopUnusedPreloadsTimer();
+
     detachFromFrame();
 
     m_hasPreparedForDestruction = true;
index 2c3a7a4..cb51c2a 100644 (file)
@@ -88,6 +88,7 @@ public:
     void notifyFinished(CachedResource& resource) override { triggerEvents(resource); }
 
     void clear() override { clearResource(*this); }
+    bool shouldMarkAsReferenced() const override { return false; }
 
 private:
     LinkPreloadScriptResourceClient(LinkLoader& loader, CachedScript& resource)
@@ -114,6 +115,7 @@ public:
     }
 
     void clear() override { clearResource(*this); }
+    bool shouldMarkAsReferenced() const override { return false; }
 
 private:
     LinkPreloadStyleResourceClient(LinkLoader& loader, CachedCSSStyleSheet& resource)
@@ -135,6 +137,7 @@ public:
     void notifyFinished(CachedResource& resource) override { triggerEvents(resource); }
 
     void clear() override { clearResource(*this); }
+    bool shouldMarkAsReferenced() const override { return false; }
 
 private:
     LinkPreloadImageResourceClient(LinkLoader& loader, CachedImage& resource)
@@ -160,6 +163,7 @@ public:
     }
 
     void clear() override { clearResource(*this); }
+    bool shouldMarkAsReferenced() const override { return false; }
 
 private:
     LinkPreloadFontResourceClient(LinkLoader& loader, CachedFont& resource)
@@ -181,6 +185,7 @@ public:
     void notifyFinished(CachedResource& resource) override { triggerEvents(resource); }
 
     void clear() override { clearResource(*this); }
+    bool shouldMarkAsReferenced() const override { return false; }
 
 private:
     LinkPreloadRawResourceClient(LinkLoader& loader, CachedRawResource& resource)
index 579a8b8..5939039 100644 (file)
@@ -465,7 +465,7 @@ void CachedResource::didAddClient(CachedResourceClient& client)
 
 bool CachedResource::addClientToSet(CachedResourceClient& client)
 {
-    if (m_preloadResult == PreloadNotReferenced) {
+    if (m_preloadResult == PreloadNotReferenced && client.shouldMarkAsReferenced()) {
         if (isLoaded())
             m_preloadResult = PreloadReferencedWhileComplete;
         else if (m_requestedFromNetworkingLayer)
index 17c361d..f5b1b9b 100644 (file)
@@ -45,6 +45,7 @@ public:
 
     static CachedResourceClientType expectedType() { return BaseResourceType; }
     virtual CachedResourceClientType resourceClientType() const { return expectedType(); }
+    virtual bool shouldMarkAsReferenced() const { return true; }
 
 protected:
     CachedResourceClient() { }
index d054bb2..6430701 100644 (file)
@@ -85,6 +85,9 @@
 
 namespace WebCore {
 
+// Timeout for link preloads to be used after window.onload
+static const int unusedPreloadTimeoutInSeconds = 3;
+
 static CachedResource* createResource(CachedResource::Type type, CachedResourceRequest&& request, SessionID sessionID)
 {
     switch (type) {
@@ -129,6 +132,7 @@ CachedResourceLoader::CachedResourceLoader(DocumentLoader* documentLoader)
     : m_document(nullptr)
     , m_documentLoader(documentLoader)
     , m_requestCount(0)
+    , m_unusedPreloadsTimer(*this, &CachedResourceLoader::warnUnusedPreloads)
     , m_garbageCollectDocumentResourcesTimer(*this, &CachedResourceLoader::garbageCollectDocumentResources)
     , m_autoLoadImages(true)
     , m_imagesEnabled(true)
@@ -147,6 +151,7 @@ CachedResourceLoader::~CachedResourceLoader()
 
     // Make sure no requests still point to this CachedResourceLoader
     ASSERT(m_requestCount == 0);
+    m_unusedPreloadsTimer.stop();
 }
 
 CachedResource* CachedResourceLoader::cachedResource(const String& resourceURL) const
@@ -821,6 +826,16 @@ CachedResourceHandle<CachedResource> CachedResourceLoader::requestResource(Cache
 void CachedResourceLoader::documentDidFinishLoadEvent()
 {
     m_validatedURLs.clear();
+
+    // If m_preloads is not empty here, it's full of link preloads,
+    // as speculative preloads were cleared at DCL.
+    if (m_preloads && m_preloads->size() && !m_unusedPreloadsTimer.isActive())
+        m_unusedPreloadsTimer.startOneShot(unusedPreloadTimeoutInSeconds);
+}
+
+void CachedResourceLoader::stopUnusedPreloadsTimer()
+{
+    m_unusedPreloadsTimer.stop();
 }
 
 CachedResourceHandle<CachedResource> CachedResourceLoader::revalidateResource(CachedResourceRequest&& request, CachedResource& resource)
@@ -1228,6 +1243,19 @@ CachedResourceHandle<CachedResource> CachedResourceLoader::preload(CachedResourc
     return resource;
 }
 
+void CachedResourceLoader::warnUnusedPreloads()
+{
+    if (!m_preloads)
+        return;
+    for (const auto& resource : *m_preloads) {
+        if (resource && resource->isLinkPreload() && resource->preloadResult() == CachedResource::PreloadNotReferenced && document()) {
+            document()->addConsoleMessage(MessageSource::Other, MessageLevel::Warning,
+                "The resource " + resource->url().string() +
+                " was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it wasn't preloaded for nothing.");
+        }
+    }
+}
+
 bool CachedResourceLoader::isPreloaded(const String& urlString) const
 {
     const URL& url = m_document->completeURL(urlString);
index 3a5f3b0..df1e878 100644 (file)
@@ -133,6 +133,8 @@ public:
     void clearPreloads(ClearPreloadsMode);
     CachedResourceHandle<CachedResource> preload(CachedResource::Type, CachedResourceRequest&&);
     void printPreloadStats();
+    void warnUnusedPreloads();
+    void stopUnusedPreloadsTimer();
 
     bool updateRequestAfterRedirection(CachedResource::Type, ResourceRequest&, const ResourceLoaderOptions&);
 
@@ -188,6 +190,7 @@ private:
     int m_requestCount;
     
     std::unique_ptr<ListHashSet<CachedResource*>> m_preloads;
+    Timer m_unusedPreloadsTimer;
 
     Timer m_garbageCollectDocumentResourcesTimer;
 
index 9ee7f96..68cf766 100644 (file)
@@ -515,6 +515,9 @@ void DOMWindow::willDetachDocumentFromFrame()
     copyToVector(m_properties, properties);
     for (auto& property : properties)
         property->willDetachGlobalObjectFromFrame();
+
+    if (m_performance)
+        m_performance->clearResourceTimings();
 }
 
 #if ENABLE(GAMEPAD)