Bug 1947158 - Add url option to ChromeUtils.clearResourceCache. r=emilio

Differential Revision: https://phabricator.services.mozilla.com/D238357
This commit is contained in:
Tooru Fujisawa
2025-03-18 09:57:57 +00:00
parent 86db2478c2
commit 8fa3cc1097
12 changed files with 129 additions and 40 deletions

View File

@@ -1418,11 +1418,12 @@ nsresult imgLoader::ClearCache(
aPrincipal /* = mozilla::Nothing() */,
const mozilla::Maybe<nsCString>& aSchemelessSite /* = mozilla::Nothing() */,
const mozilla::Maybe<mozilla::OriginAttributesPattern>&
aPattern /* = mozilla::Nothing() */) {
aPattern /* = mozilla::Nothing() */,
const mozilla::Maybe<nsCString>& aURL /* = mozilla::Nothing() */) {
if (XRE_IsParentProcess()) {
for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
Unused << cp->SendClearImageCache(aPrivateLoader, aChrome, aPrincipal,
aSchemelessSite, aPattern);
aSchemelessSite, aPattern, aURL);
}
}
@@ -1434,19 +1435,36 @@ nsresult imgLoader::ClearCache(
loader = imgLoader::NormalLoader();
}
loader->RemoveEntriesInternal(aPrincipal, Nothing(), Nothing());
loader->RemoveEntriesInternal(aPrincipal, Nothing(), Nothing(), Nothing());
return NS_OK;
}
if (aSchemelessSite) {
if (!aPrivateLoader || !*aPrivateLoader) {
nsresult rv = imgLoader::NormalLoader()->RemoveEntriesInternal(
Nothing(), aSchemelessSite, aPattern);
Nothing(), aSchemelessSite, aPattern, Nothing());
NS_ENSURE_SUCCESS(rv, rv);
}
if (!aPrivateLoader || *aPrivateLoader) {
nsresult rv = imgLoader::PrivateBrowsingLoader()->RemoveEntriesInternal(
Nothing(), aSchemelessSite, aPattern);
Nothing(), aSchemelessSite, aPattern, Nothing());
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
}
if (aURL) {
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), *aURL);
NS_ENSURE_SUCCESS(rv, rv);
if (!aPrivateLoader || !*aPrivateLoader) {
nsresult rv = imgLoader::NormalLoader()->RemoveEntry(uri, nullptr);
NS_ENSURE_SUCCESS(rv, rv);
}
if (!aPrivateLoader || *aPrivateLoader) {
nsresult rv =
imgLoader::PrivateBrowsingLoader()->RemoveEntry(uri, nullptr);
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
@@ -1503,9 +1521,12 @@ imgLoader::RemoveEntriesFromSiteInAllProcesses(
nsresult imgLoader::RemoveEntriesInternal(
const Maybe<nsCOMPtr<nsIPrincipal>>& aPrincipal,
const Maybe<nsCString>& aSchemelessSite,
const Maybe<OriginAttributesPattern>& aPattern) {
const Maybe<OriginAttributesPattern>& aPattern,
const mozilla::Maybe<nsCString>& aURL) {
// Can only clear by either principal or site + pattern.
if ((!aPrincipal && !aSchemelessSite) || (aPrincipal && aSchemelessSite) ||
if ((!aPrincipal && !aSchemelessSite && !aURL) ||
(aPrincipal && aSchemelessSite) || (aPrincipal && aURL) ||
(aSchemelessSite && aURL) ||
aSchemelessSite.isSome() != aPattern.isSome()) {
return NS_ERROR_INVALID_ARG;
}
@@ -1518,6 +1539,15 @@ nsresult imgLoader::RemoveEntriesInternal(
// TODO(emilio): Deduplicate this with SharedSubresourceCache.
const bool shouldRemove = [&] {
if (aURL) {
nsAutoCString spec;
nsresult rv = key.URI()->GetSpec(spec);
if (NS_FAILED(rv)) {
return false;
}
return spec == *aURL;
}
if (aPrincipal) {
return key.LoaderPrincipal()->Equals(aPrincipal.ref());
}

View File

@@ -258,17 +258,20 @@ class imgLoader final : public imgILoader,
* If specified and true, clear chrome cache.
* If specified and false, clear content cache.
* If not specified, clear both.
* Has no effect with aPrincipal or aSchemelessSite.
* Has no effect with aPrincipal, aSchemelessSite or aURL.
* @param aPrincipal
* If specified, clear cache from the same origin and the same
* originAttributes of the passed principal.
* Exclusive with aSchemelessSite.
* Exclusive with aSchemelessSite and aURL.
* @param aSchemelessSite
* If specified, clear cache which match the the given site.
* If this is specified, aPattern should also be specified.
* Exclusive with aPrincipal.
* Exclusive with aPrincipal and aURL.
* @param aPattern
* The pattern used with aSchemelessSite.
* @param aURL
* If specified, clear cache for given URL.
* Exclusive with aPrincipal and aschemelesssite.
*/
static nsresult ClearCache(
mozilla::Maybe<bool> aPrivateLoader = mozilla::Nothing(),
@@ -277,7 +280,8 @@ class imgLoader final : public imgILoader,
mozilla::Nothing(),
const mozilla::Maybe<nsCString>& aSchemelessSite = mozilla::Nothing(),
const mozilla::Maybe<mozilla::OriginAttributesPattern>& aPattern =
mozilla::Nothing());
mozilla::Nothing(),
const mozilla::Maybe<nsCString>& aURL = mozilla::Nothing());
bool IsImageAvailable(nsIURI*, nsIPrincipal* aTriggeringPrincipal,
mozilla::CORSMode, mozilla::dom::Document*);
@@ -376,7 +380,8 @@ class imgLoader final : public imgILoader,
nsresult RemoveEntriesInternal(
const mozilla::Maybe<nsCOMPtr<nsIPrincipal>>& aPrincipal,
const mozilla::Maybe<nsCString>& aSchemelessSite,
const mozilla::Maybe<mozilla::OriginAttributesPattern>& aPattern);
const mozilla::Maybe<mozilla::OriginAttributesPattern>& aPattern,
const mozilla::Maybe<nsCString>& aURL);
// The image loader maintains a hash table of all imgCacheEntries. However,
// only some of them will be evicted from the cache: those who have no