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 @@ ...@@ -3,9 +3,13 @@
<script src="../resources/testharnessreport.js"></script> <script src="../resources/testharnessreport.js"></script>
<script> <script>
var t = async_test('Makes sure that Link headers are loaded from an iframe'); var t = async_test('Makes sure that Link headers are loaded from an iframe');
var count = 0;
addEventListener("message", t.step_func(function(event) { addEventListener("message", t.step_func(function(event) {
assert_equals(event.data, "squareloaded", "FAIL: image resource was not loaded"); assert_equals(event.data, "squareloaded", "FAIL: image resource was not loaded");
++count;
if (count == 3) {
t.done(); t.done();
}
}, false)); }, false));
</script> </script>
<iframe src="resources/iframe-link-headers.php"></iframe> <iframe src="resources/iframe-link-headers.php"></iframe>
...@@ -2,12 +2,24 @@ ...@@ -2,12 +2,24 @@
header("Link: <http://127.0.0.1:8000/resources/square.png>;rel=preload;as=image;", false); header("Link: <http://127.0.0.1:8000/resources/square.png>;rel=preload;as=image;", false);
?> ?>
<!DOCTYPE html> <!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> <script>
function test() {
if (window.internals) { if (window.internals) {
if (internals.isPreloaded("http://127.0.0.1:8000/resources/square.png")) if (internals.isPreloaded("http://127.0.0.1:8000/resources/square.png"))
top.postMessage("squareloaded", "*"); top.postMessage("squareloaded", "*");
else else
top.postMessage("notloaded", "*"); top.postMessage("notloaded", "*");
} }
}
function onLoad() {
test();
setTimeout(test, 100);
}
test();
</script> </script>
<body onload="onLoad()">
</body>
...@@ -873,18 +873,27 @@ void ResourceFetcher::preloadStarted(Resource* resource) ...@@ -873,18 +873,27 @@ void ResourceFetcher::preloadStarted(Resource* resource)
if (!m_preloads) if (!m_preloads)
m_preloads = new HeapListHashSet<Member<Resource>>; m_preloads = new HeapListHashSet<Member<Resource>>;
m_preloads->add(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) { if (m_preloads) {
for (auto resource : *m_preloads) { for (const auto& resource : *m_preloads)
if (resource->url() == url) m_preloadedURLsForTest->add(resource->url().getString());
return true;
}
} }
}
return false; bool ResourceFetcher::isPreloadedForTest(const KURL& url) const
{
DCHECK(m_preloadedURLsForTest);
return m_preloadedURLsForTest->contains(url.getString());
} }
void ResourceFetcher::clearPreloads(ClearPreloadsPolicy policy) void ResourceFetcher::clearPreloads(ClearPreloadsPolicy policy)
......
...@@ -94,7 +94,9 @@ public: ...@@ -94,7 +94,9 @@ public:
enum ClearPreloadsPolicy { ClearAllPreloads, ClearSpeculativeMarkupPreloads }; 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; } int countPreloads() const { return m_preloads ? m_preloads->size() : 0; }
void clearPreloads(ClearPreloadsPolicy = ClearAllPreloads); void clearPreloads(ClearPreloadsPolicy = ClearAllPreloads);
void preloadStarted(Resource*); void preloadStarted(Resource*);
...@@ -208,6 +210,8 @@ private: ...@@ -208,6 +210,8 @@ private:
}; };
DeadResourceStatsRecorder m_deadStatsRecorder; DeadResourceStatsRecorder m_deadStatsRecorder;
std::unique_ptr<HashSet<String>> m_preloadedURLsForTest;
// 28 bits left // 28 bits left
bool m_autoLoadImages : 1; bool m_autoLoadImages : 1;
bool m_imagesEnabled : 1; bool m_imagesEnabled : 1;
......
...@@ -250,6 +250,7 @@ Internals::Internals(ScriptState* scriptState) ...@@ -250,6 +250,7 @@ Internals::Internals(ScriptState* scriptState)
: ContextLifecycleObserver(scriptState->getExecutionContext()) : ContextLifecycleObserver(scriptState->getExecutionContext())
, m_runtimeFlags(InternalRuntimeFlags::create()) , m_runtimeFlags(InternalRuntimeFlags::create())
{ {
contextDocument()->fetcher()->enableIsPreloadedForTest();
} }
Document* Internals::contextDocument() const Document* Internals::contextDocument() const
...@@ -396,7 +397,7 @@ bool Internals::isPreloadedBy(const String& url, Document* document) ...@@ -396,7 +397,7 @@ bool Internals::isPreloadedBy(const String& url, Document* document)
{ {
if (!document) if (!document)
return false; return false;
return document->fetcher()->isPreloaded(document->completeURL(url)); return document->fetcher()->isPreloadedForTest(document->completeURL(url));
} }
bool Internals::isLoadingFromMemoryCache(const String& 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