Bug 1755226 - Only notify 'http-on-image-cache-response' when there are registered observers r=tnikkel

This is an optimization to avoid doing redundant things when there are no
registered observers.

Differential Revision: https://phabricator.services.mozilla.com/D138853
This commit is contained in:
Sean Feng 2022-02-16 17:57:39 +00:00
Родитель 3a6e295171
Коммит c87da83fd7
4 изменённых файлов: 40 добавлений и 6 удалений

Просмотреть файл

@ -1905,15 +1905,24 @@ void imgLoader::NotifyObserversForCachedImage(
if (aEntry->HasNotified()) {
return;
}
nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
bool found;
nsresult rv = obsService->HasObservers("http-on-image-cache-response", found);
MOZ_ASSERT(NS_SUCCEEDED(rv));
if (!found) {
return;
}
aEntry->SetHasNotified();
nsCOMPtr<nsIChannel> newChannel;
bool forcePrincipalCheck;
nsresult rv =
NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
nullptr, aCORSMode, aReferrerInfo, nullptr, 0,
nsIContentPolicy::TYPE_INTERNAL_IMAGE,
aTriggeringPrincipal, aLoadingDocument, mRespectPrivacy);
rv = NewImageChannel(getter_AddRefs(newChannel), &forcePrincipalCheck, aURI,
nullptr, aCORSMode, aReferrerInfo, nullptr, 0,
nsIContentPolicy::TYPE_INTERNAL_IMAGE,
aTriggeringPrincipal, aLoadingDocument, mRespectPrivacy);
if (NS_FAILED(rv)) {
return;
}
@ -1926,7 +1935,6 @@ void imgLoader::NotifyObserversForCachedImage(
if (image) {
newChannel->SetContentLength(aEntry->GetDataSize());
}
nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
obsService->NotifyObservers(newChannel, "http-on-image-cache-response",
nullptr);
}

Просмотреть файл

@ -81,6 +81,20 @@ interface nsIObserverService : nsISupports
nsresult NotifyWhenScriptSafe(nsISupports* aSubject,
const char* aTopic,
const char16_t* aData = nullptr);
/**
* hasObservers
*
* Checks to see if there are registered listeners for the given topic.
*
* Implemented in "nsObserverService.cpp".
*
* @param aTopic : The notification topic or subject.
* @param aFound : An out parameter; True if there are registered observers,
* False otherwise.
*/
virtual nsresult HasObservers(const char* aTopic,
bool& aFound);
%}
/**

Просмотреть файл

@ -320,6 +320,12 @@ nsObserverService::UnmarkGrayStrongObservers() {
return NS_OK;
}
NS_IMETHODIMP nsObserverService::HasObservers(const char* aTopic,
bool& aFound) {
aFound = mObserverTopicTable.Contains(aTopic);
return NS_OK;
}
namespace {
class NotifyWhenScriptSafeRunnable : public mozilla::Runnable {
@ -362,3 +368,8 @@ nsresult nsIObserverService::NotifyWhenScriptSafe(nsISupports* aSubject,
this, aSubject, aTopic, aData));
return NS_OK;
}
NS_IMETHODIMP
nsIObserverService::HasObservers(const char* aTopic, bool& aFound) {
return NS_ERROR_NOT_IMPLEMENTED;
}

Просмотреть файл

@ -37,6 +37,7 @@ class nsObserverService final : public nsIObserverService,
[[nodiscard]] static nsresult Create(nsISupports* aOuter, const nsIID& aIID,
void** aInstancePtr);
NS_IMETHODIMP HasObservers(const char* aTopic, bool& aFound) override;
// Unmark any strongly held observers implemented in JS so the cycle
// collector will not traverse them.
NS_IMETHOD UnmarkGrayStrongObservers();