<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[214361] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/214361">214361</a></dd>
<dt>Author</dt> <dd>yoav@yoav.ws</dd>
<dt>Date</dt> <dd>2017-03-24 11:41:42 -0700 (Fri, 24 Mar 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add a warning for unused link preloads.
https://bugs.webkit.org/show_bug.cgi?id=165670

Reviewed by Youenn Fablet.

Source/WebCore:

This patch adds a warning message, to warn developers that are using
link preloads in cases where the downloaded resource is likely to
remain unused.

Test: 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::warnUnusedPreloads): Iterate over m_preloads and issue a warning for non-referenced preloads.
(WebCore::CachedResourceLoader::documentDidFinishLoadEvent): Trigger a timer if preloads weren't cleared at load time.
(WebCore::CachedResourceLoader::warnUnusedPreloads): Triggered by the timer, and called CachedResourceLoader::warnUnusedPreloads.
(WebCore::CachedResourceLoader::stopUnusedPreloadsTimer): Stop the timer.

LayoutTests:

This adds a test for the unused preload warning, and adapts some of the other preload tests,
to avoid warnings.

* http/tests/preload/download_resources-expected.txt: Changed.
* http/tests/preload/download_resources.html: Added references to preloaded resources.
* http/tests/preload/onerrer_event-expected.txt: Changed.
* http/tests/preload/onerror_event.html: Added references to preloaded resources.
* http/tests/preload/onload_event.html: Added references to preloaded resources.
* http/tests/preload/resources/single_download_preload.html: Deflaked.
* http/tests/preload/unused_preload_warning-expected.txt: Added.
* http/tests/preload/unused_preload_warning.html: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestshttptestspreloaddownload_resourcesexpectedtxt">trunk/LayoutTests/http/tests/preload/download_resources-expected.txt</a></li>
<li><a href="#trunkLayoutTestshttptestspreloaddownload_resourceshtml">trunk/LayoutTests/http/tests/preload/download_resources.html</a></li>
<li><a href="#trunkLayoutTestshttptestspreloadonerror_eventexpectedtxt">trunk/LayoutTests/http/tests/preload/onerror_event-expected.txt</a></li>
<li><a href="#trunkLayoutTestshttptestspreloadonerror_eventhtml">trunk/LayoutTests/http/tests/preload/onerror_event.html</a></li>
<li><a href="#trunkLayoutTestshttptestspreloadonload_eventexpectedtxt">trunk/LayoutTests/http/tests/preload/onload_event-expected.txt</a></li>
<li><a href="#trunkLayoutTestshttptestspreloadonload_eventhtml">trunk/LayoutTests/http/tests/preload/onload_event.html</a></li>
<li><a href="#trunkLayoutTestshttptestspreloadsingle_download_preloadhtml">trunk/LayoutTests/http/tests/preload/single_download_preload.html</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoredomDocumentcpp">trunk/Source/WebCore/dom/Document.cpp</a></li>
<li><a href="#trunkSourceWebCoreloaderLinkPreloadResourceClientsh">trunk/Source/WebCore/loader/LinkPreloadResourceClients.h</a></li>
<li><a href="#trunkSourceWebCoreloadercacheCachedResourcecpp">trunk/Source/WebCore/loader/cache/CachedResource.cpp</a></li>
<li><a href="#trunkSourceWebCoreloadercacheCachedResourceClienth">trunk/Source/WebCore/loader/cache/CachedResourceClient.h</a></li>
<li><a href="#trunkSourceWebCoreloadercacheCachedResourceLoadercpp">trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp</a></li>
<li><a href="#trunkSourceWebCoreloadercacheCachedResourceLoaderh">trunk/Source/WebCore/loader/cache/CachedResourceLoader.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestshttptestspreloadunused_preload_warningexpectedtxt">trunk/LayoutTests/http/tests/preload/unused_preload_warning-expected.txt</a></li>
<li><a href="#trunkLayoutTestshttptestspreloadunused_preload_warninghtml">trunk/LayoutTests/http/tests/preload/unused_preload_warning.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/LayoutTests/ChangeLog        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -1,3 +1,22 @@
</span><ins>+2017-03-24  Yoav Weiss  &lt;yoav@yoav.ws&gt;
+
+        Add a warning for unused link preloads.
+        https://bugs.webkit.org/show_bug.cgi?id=165670
+
+        Reviewed by Youenn Fablet.
+
+        This adds a test for the unused preload warning, and adapts some of the other preload tests,
+        to avoid warnings.
+
+        * http/tests/preload/download_resources-expected.txt: Changed.
+        * http/tests/preload/download_resources.html: Added references to preloaded resources.
+        * http/tests/preload/onerrer_event-expected.txt: Changed.
+        * http/tests/preload/onerror_event.html: Added references to preloaded resources.
+        * http/tests/preload/onload_event.html: Added references to preloaded resources.
+        * http/tests/preload/resources/single_download_preload.html: Deflaked.
+        * http/tests/preload/unused_preload_warning-expected.txt: Added.
+        * http/tests/preload/unused_preload_warning.html: Added.
+
</ins><span class="cx"> 2017-03-24  Brent Fulgham  &lt;bfulgham@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Handle recursive calls to ProcessingInstruction::checkStyleSheet
</span></span></pre></div>
<a id="trunkLayoutTestshttptestspreloaddownload_resourcesexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/http/tests/preload/download_resources-expected.txt (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/download_resources-expected.txt        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/LayoutTests/http/tests/preload/download_resources-expected.txt        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -1,13 +1,5 @@
</span><del>-CONSOLE MESSAGE: line 11: &lt;link rel=preload&gt; 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
</del><ins>+CONSOLE MESSAGE: line 13: &lt;link rel=preload&gt; must have a valid `as` value
+This test makes sure that link preload preloads resources
</ins><span class="cx"> 
</span><del>-TEST COMPLETE
</del><ins>+PASS Makes sure that preloaded resources are downloaded 
</ins><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestshttptestspreloaddownload_resourceshtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/http/tests/preload/download_resources.html (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/download_resources.html        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/LayoutTests/http/tests/preload/download_resources.html        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -1,27 +1,43 @@
</span><span class="cx"> &lt;!DOCTYPE html&gt;
</span><del>-&lt;html&gt;
-&lt;head&gt;
-&lt;script src=&quot;/js-test-resources/js-test.js&quot;&gt;&lt;/script&gt;
</del><ins>+&lt;script src=&quot;/js-test-resources/testharness.js&quot;&gt;&lt;/script&gt;
+&lt;script src=&quot;/js-test-resources/testharnessreport.js&quot;&gt;&lt;/script&gt;
+&lt;script&gt;
+    var t = async_test('Makes sure that preloaded resources are downloaded');
+&lt;/script&gt;
</ins><span class="cx"> &lt;link rel=preload href=&quot;../resources/dummy.js&quot; as=script&gt;
</span><span class="cx"> &lt;link rel=preload href=&quot;../resources/dummy.css&quot; as=style&gt;
</span><del>-&lt;link rel=preload href=&quot;../resources/square.png&quot; as=image&gt;
-&lt;link rel=preload href=&quot;../resources/Ahem.ttf&quot; as=font crossorigin&gt;
</del><ins>+&lt;link rel=preload href=&quot;../resources/square100.png&quot; as=image&gt;
+&lt;link rel=preload href=&quot;../resources/Ahem.woff&quot; as=font crossorigin&gt;
</ins><span class="cx"> &lt;link rel=preload href=&quot;../resources/test.mp4&quot; as=media&gt;
</span><span class="cx"> &lt;link rel=preload href=&quot;../security/resources/captions.vtt&quot; as=track&gt;
</span><span class="cx"> &lt;link rel=preload href=&quot;../resources/dummy.xml?badvalue&quot; as=foobarxmlthing&gt;
</span><span class="cx"> &lt;link rel=preload href=&quot;../resources/dummy.xml&quot;&gt;
</span><del>-&lt;script src=&quot;http://127.0.0.1:8000/resources/slow-script.pl?delay=100&quot;&gt;&lt;/script&gt;
-&lt;/head&gt;
-&lt;body&gt;
</del><ins>+&lt;script src=&quot;http://127.0.0.1:8000/resources/slow-script.pl?delay=400&quot;&gt;&lt;/script&gt;
</ins><span class="cx"> &lt;script&gt;
</span><del>-    if (window.testRunner)
-        testRunner.dumpAsText();
-    shouldBeTrue(&quot;internals.isPreloaded('../resources/dummy.js');&quot;);
-    shouldBeTrue(&quot;internals.isPreloaded('../resources/dummy.css');&quot;);
-    shouldBeTrue(&quot;internals.isPreloaded('../resources/square.png');&quot;);
-    shouldBeTrue(&quot;internals.isPreloaded('../resources/Ahem.ttf');&quot;);
-    shouldBeTrue(&quot;internals.isPreloaded('../resources/test.mp4');&quot;);
-    shouldBeTrue(&quot;internals.isPreloaded('../security/resources/captions.vtt');&quot;);
-    shouldBeFalse(&quot;internals.isPreloaded('../resources/dummy.xml?badvalue');&quot;);
-    shouldBeTrue(&quot;internals.isPreloaded('../resources/dummy.xml');&quot;);
</del><ins>+    setTimeout(t.step_func(function() {
+        assert_true(internals.isPreloaded(&quot;../resources/dummy.js&quot;));
+        assert_true(internals.isPreloaded(&quot;../resources/dummy.css&quot;));
+        assert_true(internals.isPreloaded(&quot;../resources/square100.png&quot;));
+        // FIXME: RT doesn't show downloads for the resources below. Need to investigate why.
+        assert_true(internals.isPreloaded(&quot;../resources/Ahem.woff&quot;));
+        assert_true(internals.isPreloaded(&quot;../resources/test.mp4&quot;));
+        assert_true(internals.isPreloaded(&quot;../security/resources/captions.vtt&quot;));
+        assert_false(internals.isPreloaded(&quot;../resources/dummy.xml?badvalue&quot;));
+        assert_true(internals.isPreloaded(&quot;../resources/dummy.xml&quot;));
+        document.write('&lt;script src=&quot;../resources/dummy.js&quot;&gt;&lt;/scr' + 'ipt&gt;' +
+                       '&lt;link rel=stylesheet href=&quot;../resources/dummy.css&quot;&gt;' +
+                       '&lt;img src=&quot;../resources/square100.png&quot;&gt;' +
+                       '&lt;video&gt;&lt;source src=&quot;../resources/test.mp4&quot;&gt;' +
+                       '&lt;track kind=subtitles src=&quot;../security/resources/captions.vtt&quot; srclang=en&gt;' +
+                       '&lt;/video&gt;' +
+                       '&lt;style&gt;' +
+                       '    @font-face { font-family:ahem; src: url(../resources/Ahem.woff); }' +
+                       '    span { font-family: ahem, Arial; }' +
+                       '&lt;/style&gt;' +
+                       '&lt;span&gt;This test makes sure that link preload preloads resources&lt;/span&gt;');
+        var xhr = new XMLHttpRequest();
+        xhr.open(&quot;GET&quot;, &quot;../resources/dummy.xml&quot;);
+        xhr.send();
+        t.done();
+    }));
</ins><span class="cx"> &lt;/script&gt;
</span></span></pre></div>
<a id="trunkLayoutTestshttptestspreloadonerror_eventexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/http/tests/preload/onerror_event-expected.txt (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/onerror_event-expected.txt        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/LayoutTests/http/tests/preload/onerror_event-expected.txt        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -1,12 +1,5 @@
</span><del>-CONSOLE MESSAGE: line 27: &lt;link rel=preload&gt; must have a valid `as` value
-PASS successfullyParsed is true
</del><ins>+CONSOLE MESSAGE: line 25: &lt;link rel=preload&gt; must have a valid `as` value
+This test makes sure that link preload triggers error events for non-existing resources.
</ins><span class="cx"> 
</span><del>-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
</del><ins>+PASS Makes sure that preloaded resources trigger onerror 
</ins><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestshttptestspreloadonerror_eventhtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/http/tests/preload/onerror_event.html (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/onerror_event.html        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/LayoutTests/http/tests/preload/onerror_event.html        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -1,11 +1,9 @@
</span><span class="cx"> &lt;!DOCTYPE html&gt;
</span><ins>+&lt;script src=&quot;/js-test-resources/testharness.js&quot;&gt;&lt;/script&gt;
+&lt;script src=&quot;/js-test-resources/testharnessreport.js&quot;&gt;&lt;/script&gt;
</ins><span class="cx"> &lt;script&gt;
</span><del>-    if (window.testRunner) {
-        testRunner.dumpAsText()
-        testRunner.waitUntilDone();
-    }
</del><ins>+    var t = async_test('Makes sure that preloaded resources trigger onerror');
</ins><span class="cx"> &lt;/script&gt;
</span><del>-&lt;script src=&quot;/js-test-resources/js-test.js&quot;&gt;&lt;/script&gt;
</del><span class="cx"> &lt;script&gt;
</span><span class="cx">     var scriptFailed = false;
</span><span class="cx">     var styleFailed = false;
</span><span class="lines">@@ -27,20 +25,36 @@
</span><span class="cx"> &lt;link rel=preload href=&quot;../non-existent/dummy.xml&quot; as=foobarxmlthing onerror=&quot;count();gibrishFailed = true;&quot; onload=&quot;count();&quot;&gt;
</span><span class="cx"> &lt;link rel=preload href=&quot;http://127.0.0.1:9999/non-existent/dummy.xml&quot; onerror=&quot;count();noTypeFailed = true;&quot; onload=&quot;count();&quot;&gt;
</span><span class="cx"> &lt;script&gt;
</span><ins>+    document.write('&lt;script src=&quot;../non-existent/dummy.js&quot;&gt;&lt;/scr' + 'ipt&gt;' +
+                   '&lt;link rel=stylesheet href=&quot;../non-existent/dummy.css&quot;&gt;' +
+                   '&lt;img src=&quot;../non-existent/square.png&quot;&gt;' +
+                   '&lt;video&gt;&lt;source src=&quot;test.mp4&quot;&gt;' +
+                   '&lt;track kind=subtitles src=&quot;../../non-existent/security/captions.vtt&quot; srclang=en&gt;' +
+                   '&lt;/video&gt;' +
+                   '&lt;style&gt;' +
+                   '    @font-face { font-family:ahem; src: url(../../non-existent/Ahem.ttf); }' +
+                   '    span { font-family: ahem, Arial; }' +
+                   '&lt;/style&gt;' +
+                   '&lt;span&gt;This test makes sure that link preload triggers error events for non-existing resources.&lt;/span&gt;');
+    var xhr = new XMLHttpRequest();
+    xhr.open(&quot;GET&quot;, &quot;../non-existent/dummy.xml&quot;);
+    xhr.send();
+    var xhr2 = new XMLHttpRequest();
+    xhr2.open(&quot;GET&quot;, &quot;http://127.0.0.1:9999/non-existent/dummy.xml&quot;);
+    xhr2.send();
</ins><span class="cx">     function test() {
</span><del>-        shouldBeTrue(&quot;styleFailed&quot;);
-        shouldBeTrue(&quot;scriptFailed&quot;);
-        shouldBeTrue(&quot;imageFailed&quot;);
-        shouldBeTrue(&quot;fontFailed&quot;);
-        shouldBeTrue(&quot;trackFailed&quot;);
-        shouldBeTrue(&quot;gibrishFailed&quot;);
-        shouldBeTrue(&quot;noTypeFailed&quot;);
-        if (window.testRunner)
-            testRunner.notifyDone();
</del><ins>+        assert_true(styleFailed);
+        assert_true(scriptFailed);
+        assert_true(imageFailed);
+        assert_true(fontFailed);
+        assert_true(trackFailed);
+        assert_true(gibrishFailed);
+        assert_true(noTypeFailed);
+        t.done();
</ins><span class="cx">     };
</span><del>-    setInterval(function() {
</del><ins>+    setInterval(t.step_func(function() {
</ins><span class="cx">         if (window.counter &gt;= 7)
</span><span class="cx">             test();
</span><del>-    }, 100);
</del><ins>+    }, 100));
</ins><span class="cx"> &lt;/script&gt;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestshttptestspreloadonload_eventexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/http/tests/preload/onload_event-expected.txt (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/onload_event-expected.txt        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/LayoutTests/http/tests/preload/onload_event-expected.txt        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -15,4 +15,4 @@
</span><span class="cx"> PASS xsltErrored is true
</span><span class="cx"> PASS noTypeLoaded is true
</span><span class="cx"> PASS emptyTypeLoaded is true
</span><del>-
</del><ins>+This test makes sure that link preload events are fired
</ins></span></pre></div>
<a id="trunkLayoutTestshttptestspreloadonload_eventhtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/http/tests/preload/onload_event.html (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/onload_event.html        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/LayoutTests/http/tests/preload/onload_event.html        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -29,7 +29,7 @@
</span><span class="cx"> &lt;link rel=preload href=&quot;../resources/square100.png&quot; as=image onload=&quot;count(); imageLoaded = true;&quot; onerror=&quot;count()&quot;&gt;
</span><span class="cx"> &lt;link rel=preload href=&quot;../resources/Ahem.woff&quot; as=font crossorigin onload=&quot;count(); fontLoaded = true;&quot; onerror=&quot;count()&quot;&gt;
</span><span class="cx"> &lt;link rel=preload href=&quot;../resources/test.ogv&quot; as=media onload=&quot;count(); mediaLoaded = true;&quot; onerror=&quot;count()&quot;&gt;
</span><del>-&lt;link rel=preload href=&quot;../security/resources/captions.vtt&quot; as=track onload=&quot;count(); trackLoaded = true;&quot; onerror=&quot;count()&quot;&gt;
</del><ins>+&lt;link rel=preload href=&quot;../security/resources/captions.vtt&quot; as=track onload=&quot;count(); trackLoaded = true;&quot; onerror=&quot;count();&quot;&gt;
</ins><span class="cx"> &lt;link rel=preload href=&quot;../resources/dummy.xml&quot; as=foobarxmlthing onload=&quot;count(); gibberishLoaded = true;&quot; onerror=&quot;count(); gibberishErrored = true;&quot;&gt;
</span><span class="cx"> &lt;link rel=preload href=&quot;../resources/dummy.xslt&quot; as=xslt onload=&quot;count(); xsltLoaded = true;&quot; onerror=&quot;count(); xsltErrored = true;&quot;&gt;
</span><span class="cx"> &lt;link rel=preload href=&quot;../resources/dummy.xml&quot; onload=&quot;count(); noTypeLoaded = true;&quot; onerror=&quot;count()&quot;&gt;
</span><span class="lines">@@ -51,7 +51,24 @@
</span><span class="cx">         if (window.testRunner)
</span><span class="cx">             testRunner.notifyDone();
</span><span class="cx">     }
</span><del>-     addEventListener(&quot;load&quot;, function(){
</del><ins>+    document.write('&lt;script src=&quot;../resources/dummy.js&quot;&gt;&lt;/scr' + 'ipt&gt;' +
+                   '&lt;link rel=stylesheet href=&quot;../resources/dummy.css&quot;&gt;' +
+                   '&lt;img src=&quot;../resources/square.png&quot;&gt;' +
+                   '&lt;video&gt;&lt;source src=&quot;test.mp4&quot;&gt;' +
+                   '&lt;track kind=subtitles src=&quot;../../security/resources/captions.vtt&quot; srclang=en&gt;' +
+                   '&lt;/video&gt;' +
+                   '&lt;style&gt;' +
+                   '    @font-face { font-family:ahem; src: url(../../w3c/webperf/resources/Ahem.ttf); }' +
+                   '    span { font-family: ahem, Arial; }' +
+                   '&lt;/style&gt;' +
+                   '&lt;span&gt;This test makes sure that link preload events are fired&lt;/span&gt;');
+    var xhr = new XMLHttpRequest();
+    xhr.open(&quot;GET&quot;, &quot;../resources/dummy.xml&quot;);
+    xhr.send();
+    var xhr2 = new XMLHttpRequest();
+    xhr2.open(&quot;GET&quot;, &quot;../resources/dummy.xml?badvalue&quot;);
+    xhr2.send();
+    addEventListener(&quot;load&quot;, function(){
</ins><span class="cx">         setInterval(function() {
</span><span class="cx">             if (window.counter &gt;= 10)
</span><span class="cx">                 test();
</span></span></pre></div>
<a id="trunkLayoutTestshttptestspreloadsingle_download_preloadhtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/http/tests/preload/single_download_preload.html (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/single_download_preload.html        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/LayoutTests/http/tests/preload/single_download_preload.html        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -55,6 +55,12 @@
</span><span class="cx">             // FIXME: XHR should trigger a single download, but it downloads 2 resources instead.
</span><span class="cx">             verifyDownloadNumber(&quot;http://127.0.0.1:8000/resources/dummy.xml&quot;, 2);
</span><span class="cx">             // FIXME: We should verify for video and audio as well, but they seem to (flakily?) trigger multiple partial requests.
</span><ins>+            var xhr = new XMLHttpRequest();
+            xhr.open(&quot;GET&quot;, &quot;../resources/dummy.xml&quot;);
+            xhr.send();
+            var xhr2 = new XMLHttpRequest();
+            xhr2.open(&quot;GET&quot;, &quot;../resources/dummy.xml?badvalue&quot;);
+            xhr2.send();
</ins><span class="cx">             t.done();
</span><span class="cx">             }), 100);
</span><span class="cx">     }));
</span></span></pre></div>
<a id="trunkLayoutTestshttptestspreloadunused_preload_warningexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/preload/unused_preload_warning-expected.txt (0 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/unused_preload_warning-expected.txt                                (rev 0)
+++ trunk/LayoutTests/http/tests/preload/unused_preload_warning-expected.txt        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -0,0 +1,2 @@
</span><ins>+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.
</ins></span></pre></div>
<a id="trunkLayoutTestshttptestspreloadunused_preload_warninghtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/http/tests/preload/unused_preload_warning.html (0 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/preload/unused_preload_warning.html                                (rev 0)
+++ trunk/LayoutTests/http/tests/preload/unused_preload_warning.html        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -0,0 +1,17 @@
</span><ins>+&lt;!DOCTYPE html&gt;
+&lt;script&gt;
+    if (window.testRunner) {
+        testRunner.waitUntilDone();
+        testRunner.dumpAsText();
+    }
+    window.onload = function() {
+        window.setTimeout(function() {
+            if (window.testRunner)
+                testRunner.notifyDone();
+        }, 4000);
+    };
+&lt;/script&gt;
+&lt;link rel=&quot;preload&quot; as=&quot;image&quot; href=&quot;../resources/square100.png&quot;&gt;
+&lt;link rel=&quot;preload&quot; as=&quot;image&quot; href=&quot;../resources/square100.png?name=value&quot;&gt;
+&lt;img src=&quot;../resources/square100.png&quot;&gt;
+This test verifies that unused preload resources (and only unused ones) issue a warning.
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/Source/WebCore/ChangeLog        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -1,3 +1,31 @@
</span><ins>+2017-03-24  Yoav Weiss  &lt;yoav@yoav.ws&gt;
+
+        Add a warning for unused link preloads.
+        https://bugs.webkit.org/show_bug.cgi?id=165670
+
+        Reviewed by Youenn Fablet.
+
+        This patch adds a warning message, to warn developers that are using
+        link preloads in cases where the downloaded resource is likely to
+        remain unused.
+
+        Test: 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::warnUnusedPreloads): Iterate over m_preloads and issue a warning for non-referenced preloads.
+        (WebCore::CachedResourceLoader::documentDidFinishLoadEvent): Trigger a timer if preloads weren't cleared at load time.
+        (WebCore::CachedResourceLoader::warnUnusedPreloads): Triggered by the timer, and called CachedResourceLoader::warnUnusedPreloads.
+        (WebCore::CachedResourceLoader::stopUnusedPreloadsTimer): Stop the timer.
+
</ins><span class="cx"> 2017-03-24  Brent Fulgham  &lt;bfulgham@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Handle recursive calls to ProcessingInstruction::checkStyleSheet
</span></span></pre></div>
<a id="trunkSourceWebCoredomDocumentcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/dom/Document.cpp (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/dom/Document.cpp        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/Source/WebCore/dom/Document.cpp        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -2276,6 +2276,8 @@
</span><span class="cx">     }
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+    m_cachedResourceLoader-&gt;stopUnusedPreloadsTimer();
+
</ins><span class="cx">     detachFromFrame();
</span><span class="cx"> 
</span><span class="cx">     m_hasPreparedForDestruction = true;
</span></span></pre></div>
<a id="trunkSourceWebCoreloaderLinkPreloadResourceClientsh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/LinkPreloadResourceClients.h (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/LinkPreloadResourceClients.h        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/Source/WebCore/loader/LinkPreloadResourceClients.h        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -88,6 +88,7 @@
</span><span class="cx">     void notifyFinished(CachedResource&amp; resource) override { triggerEvents(resource); }
</span><span class="cx"> 
</span><span class="cx">     void clear() override { clearResource(*this); }
</span><ins>+    bool shouldMarkAsReferenced() const override { return false; }
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     LinkPreloadScriptResourceClient(LinkLoader&amp; loader, CachedScript&amp; resource)
</span><span class="lines">@@ -114,6 +115,7 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     void clear() override { clearResource(*this); }
</span><ins>+    bool shouldMarkAsReferenced() const override { return false; }
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     LinkPreloadStyleResourceClient(LinkLoader&amp; loader, CachedCSSStyleSheet&amp; resource)
</span><span class="lines">@@ -135,6 +137,7 @@
</span><span class="cx">     void notifyFinished(CachedResource&amp; resource) override { triggerEvents(resource); }
</span><span class="cx"> 
</span><span class="cx">     void clear() override { clearResource(*this); }
</span><ins>+    bool shouldMarkAsReferenced() const override { return false; }
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     LinkPreloadImageResourceClient(LinkLoader&amp; loader, CachedImage&amp; resource)
</span><span class="lines">@@ -160,6 +163,7 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     void clear() override { clearResource(*this); }
</span><ins>+    bool shouldMarkAsReferenced() const override { return false; }
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     LinkPreloadFontResourceClient(LinkLoader&amp; loader, CachedFont&amp; resource)
</span><span class="lines">@@ -181,6 +185,7 @@
</span><span class="cx">     void notifyFinished(CachedResource&amp; resource) override { triggerEvents(resource); }
</span><span class="cx"> 
</span><span class="cx">     void clear() override { clearResource(*this); }
</span><ins>+    bool shouldMarkAsReferenced() const override { return false; }
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     LinkPreloadRawResourceClient(LinkLoader&amp; loader, CachedRawResource&amp; resource)
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedResourcecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedResource.cpp (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedResource.cpp        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/Source/WebCore/loader/cache/CachedResource.cpp        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -465,7 +465,7 @@
</span><span class="cx"> 
</span><span class="cx"> bool CachedResource::addClientToSet(CachedResourceClient&amp; client)
</span><span class="cx"> {
</span><del>-    if (m_preloadResult == PreloadNotReferenced) {
</del><ins>+    if (m_preloadResult == PreloadNotReferenced &amp;&amp; client.shouldMarkAsReferenced()) {
</ins><span class="cx">         if (isLoaded())
</span><span class="cx">             m_preloadResult = PreloadReferencedWhileComplete;
</span><span class="cx">         else if (m_requestedFromNetworkingLayer)
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedResourceClienth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedResourceClient.h (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedResourceClient.h        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/Source/WebCore/loader/cache/CachedResourceClient.h        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -45,6 +45,7 @@
</span><span class="cx"> 
</span><span class="cx">     static CachedResourceClientType expectedType() { return BaseResourceType; }
</span><span class="cx">     virtual CachedResourceClientType resourceClientType() const { return expectedType(); }
</span><ins>+    virtual bool shouldMarkAsReferenced() const { return true; }
</ins><span class="cx"> 
</span><span class="cx"> protected:
</span><span class="cx">     CachedResourceClient() { }
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedResourceLoadercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/Source/WebCore/loader/cache/CachedResourceLoader.cpp        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -85,6 +85,9 @@
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><ins>+// Timeout for link preloads to be used after window.onload
+static const int unusedPreloadTimeoutInSeconds = 3;
+
</ins><span class="cx"> static CachedResource* createResource(CachedResource::Type type, CachedResourceRequest&amp;&amp; request, SessionID sessionID)
</span><span class="cx"> {
</span><span class="cx">     switch (type) {
</span><span class="lines">@@ -129,6 +132,7 @@
</span><span class="cx">     : m_document(nullptr)
</span><span class="cx">     , m_documentLoader(documentLoader)
</span><span class="cx">     , m_requestCount(0)
</span><ins>+    , m_unusedPreloadsTimer(*this, &amp;CachedResourceLoader::warnUnusedPreloads)
</ins><span class="cx">     , m_garbageCollectDocumentResourcesTimer(*this, &amp;CachedResourceLoader::garbageCollectDocumentResources)
</span><span class="cx">     , m_autoLoadImages(true)
</span><span class="cx">     , m_imagesEnabled(true)
</span><span class="lines">@@ -147,6 +151,7 @@
</span><span class="cx"> 
</span><span class="cx">     // Make sure no requests still point to this CachedResourceLoader
</span><span class="cx">     ASSERT(m_requestCount == 0);
</span><ins>+    m_unusedPreloadsTimer.stop();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> CachedResource* CachedResourceLoader::cachedResource(const String&amp; resourceURL) const
</span><span class="lines">@@ -821,8 +826,18 @@
</span><span class="cx"> void CachedResourceLoader::documentDidFinishLoadEvent()
</span><span class="cx"> {
</span><span class="cx">     m_validatedURLs.clear();
</span><ins>+
+    // If m_preloads is not empty here, it's full of link preloads,
+    // as speculative preloads were cleared at DCL.
+    if (m_preloads &amp;&amp; m_preloads-&gt;size() &amp;&amp; !m_unusedPreloadsTimer.isActive())
+        m_unusedPreloadsTimer.startOneShot(unusedPreloadTimeoutInSeconds);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void CachedResourceLoader::stopUnusedPreloadsTimer()
+{
+    m_unusedPreloadsTimer.stop();
+}
+
</ins><span class="cx"> CachedResourceHandle&lt;CachedResource&gt; CachedResourceLoader::revalidateResource(CachedResourceRequest&amp;&amp; request, CachedResource&amp; resource)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(resource.inCache());
</span><span class="lines">@@ -1228,6 +1243,19 @@
</span><span class="cx">     return resource;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void CachedResourceLoader::warnUnusedPreloads()
+{
+    if (!m_preloads)
+        return;
+    for (const auto&amp; resource : *m_preloads) {
+        if (resource &amp;&amp; resource-&gt;isLinkPreload() &amp;&amp; resource-&gt;preloadResult() == CachedResource::PreloadNotReferenced &amp;&amp; document()) {
+            document()-&gt;addConsoleMessage(MessageSource::Other, MessageLevel::Warning,
+                &quot;The resource &quot; + resource-&gt;url().string() +
+                &quot; 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.&quot;);
+        }
+    }
+}
+
</ins><span class="cx"> bool CachedResourceLoader::isPreloaded(const String&amp; urlString) const
</span><span class="cx"> {
</span><span class="cx">     const URL&amp; url = m_document-&gt;completeURL(urlString);
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedResourceLoaderh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedResourceLoader.h (214360 => 214361)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedResourceLoader.h        2017-03-24 18:39:28 UTC (rev 214360)
+++ trunk/Source/WebCore/loader/cache/CachedResourceLoader.h        2017-03-24 18:41:42 UTC (rev 214361)
</span><span class="lines">@@ -133,6 +133,8 @@
</span><span class="cx">     void clearPreloads(ClearPreloadsMode);
</span><span class="cx">     CachedResourceHandle&lt;CachedResource&gt; preload(CachedResource::Type, CachedResourceRequest&amp;&amp;);
</span><span class="cx">     void printPreloadStats();
</span><ins>+    void warnUnusedPreloads();
+    void stopUnusedPreloadsTimer();
</ins><span class="cx"> 
</span><span class="cx">     bool updateRequestAfterRedirection(CachedResource::Type, ResourceRequest&amp;, const ResourceLoaderOptions&amp;);
</span><span class="cx"> 
</span><span class="lines">@@ -188,6 +190,7 @@
</span><span class="cx">     int m_requestCount;
</span><span class="cx">     
</span><span class="cx">     std::unique_ptr&lt;ListHashSet&lt;CachedResource*&gt;&gt; m_preloads;
</span><ins>+    Timer m_unusedPreloadsTimer;
</ins><span class="cx"> 
</span><span class="cx">     Timer m_garbageCollectDocumentResourcesTimer;
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>