From 918425999d019b5d0b6881a2b2013e56df1726a2 Mon Sep 17 00:00:00 2001 From: Dave Townsend Date: Mon, 12 Nov 2018 16:41:35 +0000 Subject: [PATCH] Bug 1503696: Cache cookies in the same way we did before Firefox 63. r=mak Previous to bug 1453751 favicons were loaded from the network by a tag with validate="never". This caused us to always use any cached version if possible. Bug 1453751 used a normal load type causing us to revalidate with the server for each request. This switches the loader to using the cache if possible. Differential Revision: https://phabricator.services.mozilla.com/D11402 --HG-- extra : moz-landing-system : lando --- .../base/content/test/favicons/browser.ini | 4 ++ .../test/favicons/browser_favicon_cache.js | 37 +++++++++++++++++++ .../content/test/favicons/cookie_favicon.html | 11 ++++++ .../content/test/favicons/cookie_favicon.sjs | 15 ++++++++ browser/modules/FaviconLoader.jsm | 4 +- 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 browser/base/content/test/favicons/browser_favicon_cache.js create mode 100644 browser/base/content/test/favicons/cookie_favicon.html create mode 100644 browser/base/content/test/favicons/cookie_favicon.sjs diff --git a/browser/base/content/test/favicons/browser.ini b/browser/base/content/test/favicons/browser.ini index abcf8605f6f5..fac72af3a5e9 100644 --- a/browser/base/content/test/favicons/browser.ini +++ b/browser/base/content/test/favicons/browser.ini @@ -62,3 +62,7 @@ support-files = file_with_slow_favicon.html blank.html file_favicon.png +[browser_favicon_cache.js] +support-files = + cookie_favicon.sjs + cookie_favicon.html diff --git a/browser/base/content/test/favicons/browser_favicon_cache.js b/browser/base/content/test/favicons/browser_favicon_cache.js new file mode 100644 index 000000000000..90fae1584e17 --- /dev/null +++ b/browser/base/content/test/favicons/browser_favicon_cache.js @@ -0,0 +1,37 @@ +add_task(async () => { + const testPath = "http://example.com/browser/browser/base/content/test/favicons/cookie_favicon.html"; + const testUrl = Services.io.newURI(testPath); + + let tab = BrowserTestUtils.addTab(gBrowser, testPath); + gBrowser.selectedTab = tab; + let browser = tab.linkedBrowser; + + let faviconPromise = waitForLinkAvailable(browser); + await BrowserTestUtils.browserLoaded(browser); + await faviconPromise; + let cookies = Services.cookies.getCookiesFromHost("example.com", browser.contentPrincipal.originAttributes); + let seenCookie = false; + for (let cookie of cookies) { + if (cookie.name == "faviconCookie") { + seenCookie = true; + is(cookie.value, 1, "Should have seen the right initial cookie."); + } + } + ok(seenCookie, "Should have seen the cookie."); + + faviconPromise = waitForLinkAvailable(browser); + BrowserTestUtils.loadURI(browser, testPath); + await BrowserTestUtils.browserLoaded(browser); + await faviconPromise; + cookies = Services.cookies.getCookiesFromHost("example.com", browser.contentPrincipal.originAttributes); + seenCookie = false; + for (let cookie of cookies) { + if (cookie.name == "faviconCookie") { + seenCookie = true; + is(cookie.value, 1, "Should have seen the cached cookie."); + } + } + ok(seenCookie, "Should have seen the cookie."); + + BrowserTestUtils.removeTab(tab); +}); diff --git a/browser/base/content/test/favicons/cookie_favicon.html b/browser/base/content/test/favicons/cookie_favicon.html new file mode 100644 index 000000000000..618ac1850bd6 --- /dev/null +++ b/browser/base/content/test/favicons/cookie_favicon.html @@ -0,0 +1,11 @@ + + + + + Favicon Test for caching + + + + Favicon!! + + diff --git a/browser/base/content/test/favicons/cookie_favicon.sjs b/browser/base/content/test/favicons/cookie_favicon.sjs new file mode 100644 index 000000000000..0621058d25a5 --- /dev/null +++ b/browser/base/content/test/favicons/cookie_favicon.sjs @@ -0,0 +1,15 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +function handleRequest(request, response) { + let state = getState("cache_cookie"); + if (!state) { + state = 0; + } + + response.setStatusLine(request.httpVersion, 302, "Moved Temporarily"); + response.setHeader("Set-Cookie", `faviconCookie=${++state}`); + response.setHeader("Location", "http://example.com/browser/browser/base/content/test/favicons/moz.png"); + setState("cache_cookie", `${state}`); +} diff --git a/browser/modules/FaviconLoader.jsm b/browser/modules/FaviconLoader.jsm index c85da7f6b43a..ee385c84a6f4 100644 --- a/browser/modules/FaviconLoader.jsm +++ b/browser/modules/FaviconLoader.jsm @@ -78,7 +78,9 @@ class FaviconLoad { Ci.nsILoadInfo.SEC_DISALLOW_SCRIPT), Ci.nsIContentPolicy.TYPE_INTERNAL_IMAGE_FAVICON); - this.channel.loadFlags |= Ci.nsIRequest.LOAD_BACKGROUND; + this.channel.loadFlags |= Ci.nsIRequest.LOAD_BACKGROUND | + Ci.nsIRequest.VALIDATE_NEVER | + Ci.nsIRequest.LOAD_FROM_CACHE; // Sometimes node is a document and sometimes it is an element. This is // the easiest single way to get to the load group in both those cases. this.channel.loadGroup = iconInfo.node.ownerGlobal.document.documentLoadGroup;