Bug 1728326 - Add GetRegistrationForAddonPrincipal to nsIServiceWorkerManager. r=asuth

This patch introduces a new getRegistrationForAddonPrincipal to nsIServiceWorkerManager.
This method is similar to the existing getRegistrationForPrincipal but:
- similarly to registerForAddonPrincipal it only gets the extension principal as its only
  input parameter.
- unlike getRegistrationForPrincipal, it doesn't throw if the registration has not been found
  (e.g. because it was already unregistered when the method was called).

Differential Revision: https://phabricator.services.mozilla.com/D124702
This commit is contained in:
Luca Greco 2021-11-05 21:27:04 +00:00
Родитель ef287a67cc
Коммит 873a724d1c
2 изменённых файлов: 43 добавлений и 0 удалений

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

@ -216,6 +216,14 @@ interface nsIServiceWorkerManager : nsISupports
[implicit_jscontext]
Promise registerForAddonPrincipal(in nsIPrincipal aPrincipal);
/**
* Get an extension background service worker registration for a
* given extension principal, return an nsIServiceWorkerRegistrationInfo
* if one exists (or null if no registration has been found).
*/
void getRegistrationForAddonPrincipal(in nsIPrincipal aPrincipal,
[optional, retval] out nsIServiceWorkerRegistrationInfo regInfo);
/**
* Unregister an existing ServiceWorker registration for `aScope`.
* It keeps aCallback alive until the operation is concluded.

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

@ -2740,6 +2740,41 @@ ServiceWorkerManager::RegisterForAddonPrincipal(nsIPrincipal* aPrincipal,
return NS_OK;
}
NS_IMETHODIMP
ServiceWorkerManager::GetRegistrationForAddonPrincipal(
nsIPrincipal* aPrincipal, nsIServiceWorkerRegistrationInfo** aInfo) {
MOZ_ASSERT(aPrincipal);
MOZ_ASSERT(aPrincipal);
auto* addonPolicy = BasePrincipal::Cast(aPrincipal)->AddonPolicy();
if (!addonPolicy) {
return NS_ERROR_FAILURE;
}
nsCString scope;
auto result = addonPolicy->GetURL(u""_ns);
if (result.isOk()) {
scope.Assign(NS_ConvertUTF16toUTF8(result.unwrap()));
} else {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIURI> scopeURI;
nsresult rv = NS_NewURI(getter_AddRefs(scopeURI), scope);
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
RefPtr<ServiceWorkerRegistrationInfo> info =
GetServiceWorkerRegistrationInfo(aPrincipal, scopeURI);
if (!info) {
aInfo = nullptr;
return NS_OK;
}
info.forget(aInfo);
return NS_OK;
}
NS_IMETHODIMP
ServiceWorkerManager::GetRegistrationByPrincipal(
nsIPrincipal* aPrincipal, const nsAString& aScope,