Commit 7978814b authored by hiroshige's avatar hiroshige Committed by Commit bot

Make internals.isPreloaded() to remain the same before/after clearPreloads()

Previously, internals.isPreloaded() depends on |ResourceFetcher::m_preloads|,
which is cleared by ResourceFetcher::clearPreloads().
This caused internals.isPreloaded() to turn false after around document's
load event.

This CL makes ResourceFetcher to keep a set of preloaded URLs (separately
from |m_preloads|) if a blink::Internals object is created, and use the set
to calculate internals.isPreloaded().

BUG=643621

Review-Url: https://codereview.chromium.org/2332333003
Cr-Commit-Position: refs/heads/master@{#418821}
parent 6e49adc6
CONSOLE MESSAGE: line 11: PASS: image1.png
CONSOLE MESSAGE: line 12: PASS: non-existant.js
CONSOLE MESSAGE: line 11: PASS: image1.png
CONSOLE MESSAGE: line 12: PASS: non-existant.js
internals.isPreloaded() should remain the same even after ResourceFetcher::clearPreloads() is called (around document's load event). crbug.com/643621
<img src=resources/image1.png>
<body>
<script>
if (window.testRunner) {
testRunner.dumpAsText();
testRunner.waitUntilDone();
}
function test()
{
if (window.internals) {
console.log((internals.isPreloaded("resources/image1.png") ? "PASS" : "FAIL") + ": image1.png");
console.log((internals.isPreloaded("resources/non-existant.js") ? "PASS" : "FAIL") + ": non-existant.js");
}
}
window.addEventListener('DOMContentLoaded', function () {
test();
setTimeout(function() {
test();
if (window.testRunner) {
testRunner.notifyDone();
}
}, 100);
}, false);
</script>
internals.isPreloaded() should remain the same even after
ResourceFetcher::clearPreloads() is called (around document's load event).
crbug.com/643621
<script src=resources/non-existant.js></script>
<script>document.write("<plaintext>");</script>
<img src=resources/image1.png>
......@@ -3,9 +3,13 @@
<script src="../resources/testharnessreport.js"></script>
<script>
var t = async_test('Makes sure that Link headers are loaded from an iframe');
var count = 0;
addEventListener("message", t.step_func(function(event) {
assert_equals(event.data, "squareloaded", "FAIL: image resource was not loaded");
t.done();
++count;
if (count == 3) {
t.done();
}
}, false));
</script>
<iframe src="resources/iframe-link-headers.php"></iframe>
......@@ -2,12 +2,24 @@
header("Link: <http://127.0.0.1:8000/resources/square.png>;rel=preload;as=image;", false);
?>
<!DOCTYPE html>
<!-- Test that
(1) the URL specified in the link header is preloaded, and
(2) isPreloaded() returns true before, in, and after document's load event.
-->
<script>
function test() {
if (window.internals) {
if (internals.isPreloaded("http://127.0.0.1:8000/resources/square.png"))
top.postMessage("squareloaded", "*");
else
top.postMessage("notloaded", "*");
}
}
function onLoad() {
test();
setTimeout(test, 100);
}
test();
</script>
<body onload="onLoad()">
</body>
......@@ -873,18 +873,27 @@ void ResourceFetcher::preloadStarted(Resource* resource)
if (!m_preloads)
m_preloads = new HeapListHashSet<Member<Resource>>;
m_preloads->add(resource);
if (m_preloadedURLsForTest)
m_preloadedURLsForTest->add(resource->url().getString());
}
bool ResourceFetcher::isPreloaded(const KURL& url) const
void ResourceFetcher::enableIsPreloadedForTest()
{
if (m_preloadedURLsForTest)
return;
m_preloadedURLsForTest = wrapUnique(new HashSet<String>);
if (m_preloads) {
for (auto resource : *m_preloads) {
if (resource->url() == url)
return true;
}
for (const auto& resource : *m_preloads)
m_preloadedURLsForTest->add(resource->url().getString());
}
}
return false;
bool ResourceFetcher::isPreloadedForTest(const KURL& url) const
{
DCHECK(m_preloadedURLsForTest);
return m_preloadedURLsForTest->contains(url.getString());
}
void ResourceFetcher::clearPreloads(ClearPreloadsPolicy policy)
......
......@@ -94,7 +94,9 @@ public:
enum ClearPreloadsPolicy { ClearAllPreloads, ClearSpeculativeMarkupPreloads };
bool isPreloaded(const KURL&) const;
void enableIsPreloadedForTest();
bool isPreloadedForTest(const KURL&) const;
int countPreloads() const { return m_preloads ? m_preloads->size() : 0; }
void clearPreloads(ClearPreloadsPolicy = ClearAllPreloads);
void preloadStarted(Resource*);
......@@ -208,6 +210,8 @@ private:
};
DeadResourceStatsRecorder m_deadStatsRecorder;
std::unique_ptr<HashSet<String>> m_preloadedURLsForTest;
// 28 bits left
bool m_autoLoadImages : 1;
bool m_imagesEnabled : 1;
......
......@@ -250,6 +250,7 @@ Internals::Internals(ScriptState* scriptState)
: ContextLifecycleObserver(scriptState->getExecutionContext())
, m_runtimeFlags(InternalRuntimeFlags::create())
{
contextDocument()->fetcher()->enableIsPreloadedForTest();
}
Document* Internals::contextDocument() const
......@@ -396,7 +397,7 @@ bool Internals::isPreloadedBy(const String& url, Document* document)
{
if (!document)
return false;
return document->fetcher()->isPreloaded(document->completeURL(url));
return document->fetcher()->isPreloadedForTest(document->completeURL(url));
}
bool Internals::isLoadingFromMemoryCache(const String& url)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment