commit e697a404bb6e0b1be96b679ed6b384f7d736aa4c
parent ae2826ed85991770191016c90b8ca8c110a928db
Author: Tooru Fujisawa <arai_a@mac.com>
Date: Wed, 19 Nov 2025 00:44:39 +0000
Bug 1998925 - Part 2: Add ChromeUtils.invalidateResourceCache. r=nbp
Differential Revision: https://phabricator.services.mozilla.com/D271876
Diffstat:
8 files changed, 58 insertions(+), 0 deletions(-)
diff --git a/dom/base/ChromeUtils.cpp b/dom/base/ChromeUtils.cpp
@@ -1694,6 +1694,11 @@ void ChromeUtils::ClearResourceCache(
}
}
+void ChromeUtils::InvalidateResourceCache(GlobalObject& aGlobal,
+ ErrorResult& aRv) {
+ SharedScriptCache::Invalidate();
+}
+
void ChromeUtils::ClearBfcacheByPrincipal(GlobalObject& aGlobal,
nsIPrincipal* aPrincipal,
ErrorResult& aRv) {
diff --git a/dom/base/ChromeUtils.h b/dom/base/ChromeUtils.h
@@ -201,6 +201,8 @@ class ChromeUtils {
const dom::ClearResourceCacheOptions& aOptions,
ErrorResult& aRv);
+ static void InvalidateResourceCache(GlobalObject& aGlobal, ErrorResult& aRv);
+
static void ClearBfcacheByPrincipal(GlobalObject& aGlobal,
nsIPrincipal* aPrincipal,
ErrorResult& aRv);
diff --git a/dom/chrome-webidl/ChromeUtils.webidl b/dom/chrome-webidl/ChromeUtils.webidl
@@ -274,6 +274,18 @@ namespace ChromeUtils {
undefined clearResourceCache(optional ClearResourceCacheOptions options = {});
/**
+ * Invalidates the resource cache which supports invalidation.
+ *
+ * In contrast to the clearResourceCache, this doesn't immediately clear
+ * the cache. It can validate the cache entry on the next request and
+ * revive if the cache is confirmed to be still valid.
+ *
+ * Currently only JavaScripts supports.
+ */
+ [Throws]
+ undefined invalidateResourceCache();
+
+ /**
* Clears the bfcache (backward-forward cache)
*/
[Throws]
diff --git a/dom/ipc/ContentChild.cpp b/dom/ipc/ContentChild.cpp
@@ -2151,6 +2151,11 @@ mozilla::ipc::IPCResult ContentChild::RecvClearScriptCache(
return IPC_OK();
}
+mozilla::ipc::IPCResult ContentChild::RecvInvalidateScriptCache() {
+ SharedScriptCache::Invalidate();
+ return IPC_OK();
+}
+
mozilla::ipc::IPCResult ContentChild::RecvClearImageCache(
const Maybe<bool>& aPrivateLoader, const Maybe<bool>& aChrome,
const Maybe<RefPtr<nsIPrincipal>>& aPrincipal,
diff --git a/dom/ipc/ContentChild.h b/dom/ipc/ContentChild.h
@@ -253,6 +253,8 @@ class ContentChild final : public PContentChild,
const Maybe<OriginAttributesPattern>& aPattern,
const Maybe<nsCString>& aURL);
+ mozilla::ipc::IPCResult RecvInvalidateScriptCache();
+
mozilla::ipc::IPCResult RecvClearImageCache(
const Maybe<bool>& aPrivateLoader, const Maybe<bool>& aChrome,
const Maybe<RefPtr<nsIPrincipal>>& aPrincipal,
diff --git a/dom/ipc/PContent.ipdl b/dom/ipc/PContent.ipdl
@@ -688,6 +688,8 @@ child:
OriginAttributesPattern? aPattern,
nsCString? aURL);
+ async InvalidateScriptCache();
+
async SetOffline(bool offline);
async SetConnectivity(bool connectivity);
async SetCaptivePortalState(int32_t aState);
diff --git a/dom/script/SharedScriptCache.cpp b/dom/script/SharedScriptCache.cpp
@@ -139,6 +139,7 @@ SharedScriptCache::CollectReports(nsIHandleReportCallback* aHandleReport,
return NS_OK;
}
+/* static */
void SharedScriptCache::Clear(const Maybe<bool>& aChrome,
const Maybe<nsCOMPtr<nsIPrincipal>>& aPrincipal,
const Maybe<nsCString>& aSchemelessSite,
@@ -160,6 +161,31 @@ void SharedScriptCache::Clear(const Maybe<bool>& aChrome,
}
/* static */
+void SharedScriptCache::Invalidate() {
+ using ContentParent = dom::ContentParent;
+
+ if (XRE_IsParentProcess()) {
+ for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
+ (void)cp->SendInvalidateScriptCache();
+ }
+ }
+
+ if (sSingleton) {
+ sSingleton->InvalidateInProcess();
+ }
+}
+
+void SharedScriptCache::InvalidateInProcess() {
+ for (auto iter = mComplete.Iter(); !iter.Done(); iter.Next()) {
+ if (!iter.Data().mResource->HasCacheEntryId()) {
+ iter.Remove();
+ } else {
+ iter.Data().mResource->SetDirty();
+ }
+ }
+}
+
+/* static */
void SharedScriptCache::PrepareForLastCC() {
if (sSingleton) {
sSingleton->mComplete.Clear();
diff --git a/dom/script/SharedScriptCache.h b/dom/script/SharedScriptCache.h
@@ -201,6 +201,8 @@ class SharedScriptCache final
void EncodeAndCompress();
void SaveToDiskCache();
+ void InvalidateInProcess();
+
// This has to be static because it's also called for loaders that don't have
// a sheet cache (loaders that are not owned by a document).
static void LoadCompleted(SharedScriptCache*, ScriptLoadData&);
@@ -211,6 +213,8 @@ class SharedScriptCache final
const Maybe<OriginAttributesPattern>& aPattern = Nothing(),
const Maybe<nsCString>& aURL = Nothing());
+ static void Invalidate();
+
static void PrepareForLastCC();
protected: