Bug 1948875 - Part 1: Add static imgLoader::ClearCache and refactor IPC. r=tnikkel
In order to clear cache from ChromeUtils.clearResourceCache with variuos filtering, imgLoader needs a method that are adaptive to the current process, where in the parent process, clear all processes' cache, and in the content process, clear the cache in the process. Refactored the cache methods based on single static method and single IPC method, this is also necessary to extend the ChromeUtils.clearResourceCache in bug 1947158. Differential Revision: https://phabricator.services.mozilla.com/D239138
This commit is contained in:
@@ -1372,17 +1372,28 @@ imgLoader::Observe(nsISupports* aSubject, const char* aTopic,
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
imgLoader::ClearCache(JS::Handle<JS::Value> chrome) {
|
||||
return ClearCache(chrome.isBoolean() ? Some(chrome.toBoolean()) : Nothing());
|
||||
}
|
||||
imgLoader::ClearCache(JS::Handle<JS::Value> aChrome) {
|
||||
nsresult rv = NS_OK;
|
||||
|
||||
nsresult imgLoader::ClearCache(mozilla::Maybe<bool> chrome) {
|
||||
Maybe<bool> chrome =
|
||||
aChrome.isBoolean() ? Some(aChrome.toBoolean()) : Nothing();
|
||||
if (XRE_IsParentProcess()) {
|
||||
bool privateLoader = this == gPrivateBrowsingLoader;
|
||||
for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
|
||||
Unused << cp->SendClearImageCache(privateLoader, chrome);
|
||||
rv = ClearCache(Some(privateLoader), chrome, Nothing(), Nothing(),
|
||||
Nothing());
|
||||
|
||||
if (this == gNormalLoader || this == gPrivateBrowsingLoader) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
// NOTE: There can be other loaders created with
|
||||
// Cc["@mozilla.org/image/loader;1"].createInstance(Ci.imgILoader).
|
||||
// If ClearCache is called on them, the above static ClearCache
|
||||
// doesn't handle it, and ClearImageCache needs to be called on
|
||||
// the current instance.
|
||||
// The favicon handling and some tests can create such loaders.
|
||||
}
|
||||
|
||||
ClearOptions options;
|
||||
if (chrome) {
|
||||
if (*chrome) {
|
||||
@@ -1391,7 +1402,74 @@ nsresult imgLoader::ClearCache(mozilla::Maybe<bool> chrome) {
|
||||
options += ClearOption::ContentOnly;
|
||||
}
|
||||
}
|
||||
return ClearImageCache(options);
|
||||
nsresult rv2 = ClearImageCache(options);
|
||||
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
return rv2;
|
||||
}
|
||||
|
||||
/*static */
|
||||
nsresult imgLoader::ClearCache(
|
||||
mozilla::Maybe<bool> aPrivateLoader /* = mozilla::Nothing() */,
|
||||
mozilla::Maybe<bool> aChrome /* = mozilla::Nothing() */,
|
||||
const mozilla::Maybe<nsCOMPtr<nsIPrincipal>>&
|
||||
aPrincipal /* = mozilla::Nothing() */,
|
||||
const mozilla::Maybe<nsCString>& aSchemelessSite /* = mozilla::Nothing() */,
|
||||
const mozilla::Maybe<mozilla::OriginAttributesPattern>&
|
||||
aPattern /* = mozilla::Nothing() */) {
|
||||
if (XRE_IsParentProcess()) {
|
||||
for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
|
||||
Unused << cp->SendClearImageCache(aPrivateLoader, aChrome, aPrincipal,
|
||||
aSchemelessSite, aPattern);
|
||||
}
|
||||
}
|
||||
|
||||
if (aPrincipal) {
|
||||
imgLoader* loader;
|
||||
if ((*aPrincipal)->OriginAttributesRef().IsPrivateBrowsing()) {
|
||||
loader = imgLoader::PrivateBrowsingLoader();
|
||||
} else {
|
||||
loader = imgLoader::NormalLoader();
|
||||
}
|
||||
|
||||
loader->RemoveEntriesInternal(aPrincipal, Nothing(), Nothing());
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
if (aSchemelessSite) {
|
||||
if (!aPrivateLoader || !*aPrivateLoader) {
|
||||
nsresult rv = imgLoader::NormalLoader()->RemoveEntriesInternal(
|
||||
Nothing(), aSchemelessSite, aPattern);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
}
|
||||
if (!aPrivateLoader || *aPrivateLoader) {
|
||||
nsresult rv = imgLoader::PrivateBrowsingLoader()->RemoveEntriesInternal(
|
||||
Nothing(), aSchemelessSite, aPattern);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
ClearOptions options;
|
||||
if (aChrome) {
|
||||
if (*aChrome) {
|
||||
options += ClearOption::ChromeOnly;
|
||||
} else {
|
||||
options += ClearOption::ContentOnly;
|
||||
}
|
||||
}
|
||||
|
||||
if (!aPrivateLoader || !*aPrivateLoader) {
|
||||
nsresult rv = imgLoader::NormalLoader()->ClearImageCache(options);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
}
|
||||
if (!aPrivateLoader || *aPrivateLoader) {
|
||||
nsresult rv = imgLoader::PrivateBrowsingLoader()->ClearImageCache(options);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
@@ -1400,18 +1478,8 @@ imgLoader::RemoveEntriesFromPrincipalInAllProcesses(nsIPrincipal* aPrincipal) {
|
||||
return NS_ERROR_NOT_AVAILABLE;
|
||||
}
|
||||
|
||||
for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
|
||||
Unused << cp->SendClearImageCacheFromPrincipal(aPrincipal);
|
||||
}
|
||||
|
||||
imgLoader* loader;
|
||||
if (aPrincipal->OriginAttributesRef().IsPrivateBrowsing()) {
|
||||
loader = imgLoader::PrivateBrowsingLoader();
|
||||
} else {
|
||||
loader = imgLoader::NormalLoader();
|
||||
}
|
||||
|
||||
return loader->RemoveEntriesInternal(Some(aPrincipal), Nothing(), Nothing());
|
||||
nsCOMPtr<nsIPrincipal> principal = aPrincipal;
|
||||
return ClearCache(Nothing(), Nothing(), Some(principal));
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
@@ -1428,12 +1496,8 @@ imgLoader::RemoveEntriesFromSiteInAllProcesses(
|
||||
return NS_ERROR_INVALID_ARG;
|
||||
}
|
||||
|
||||
for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
|
||||
Unused << cp->SendClearImageCacheFromSite(aSchemelessSite, pattern);
|
||||
}
|
||||
|
||||
return RemoveEntriesInternal(Nothing(), Some(nsCString(aSchemelessSite)),
|
||||
Some(pattern));
|
||||
return ClearCache(Nothing(), Nothing(), Nothing(),
|
||||
Some(nsCString(aSchemelessSite)), Some(pattern));
|
||||
}
|
||||
|
||||
nsresult imgLoader::RemoveEntriesInternal(
|
||||
|
||||
@@ -244,7 +244,40 @@ class imgLoader final : public imgILoader,
|
||||
imgLoader();
|
||||
nsresult Init();
|
||||
|
||||
nsresult ClearCache(mozilla::Maybe<bool> chrome);
|
||||
/**
|
||||
* Clear cache that matches the specified filters.
|
||||
* If called on the parent process, clear cache from all processes.
|
||||
* If called in the content process, clear cache within the process.
|
||||
*
|
||||
* @param aPrivateLoader
|
||||
* If specified and true, clear private loader.
|
||||
* If specified and false, clear normal loader.
|
||||
* If not specified, clear both loaders.
|
||||
* Has no effect with aPrincipal.
|
||||
* @param aChrome
|
||||
* 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.
|
||||
* @param aPrincipal
|
||||
* If specified, clear cache from the same origin and the same
|
||||
* originAttributes of the passed principal.
|
||||
* Exclusive with aSchemelessSite.
|
||||
* @param aSchemelessSite
|
||||
* If specified, clear cache which match the the given site.
|
||||
* If this is specified, aPattern should also be specified.
|
||||
* Exclusive with aPrincipal.
|
||||
* @param aPattern
|
||||
* The pattern used with aSchemelessSite.
|
||||
*/
|
||||
static nsresult ClearCache(
|
||||
mozilla::Maybe<bool> aPrivateLoader = mozilla::Nothing(),
|
||||
mozilla::Maybe<bool> aChrome = mozilla::Nothing(),
|
||||
const mozilla::Maybe<nsCOMPtr<nsIPrincipal>>& aPrincipal =
|
||||
mozilla::Nothing(),
|
||||
const mozilla::Maybe<nsCString>& aSchemelessSite = mozilla::Nothing(),
|
||||
const mozilla::Maybe<mozilla::OriginAttributesPattern>& aPattern =
|
||||
mozilla::Nothing());
|
||||
|
||||
bool IsImageAvailable(nsIURI*, nsIPrincipal* aTriggeringPrincipal,
|
||||
mozilla::CORSMode, mozilla::dom::Document*);
|
||||
|
||||
Reference in New Issue
Block a user