gecko-dev/dom/serviceworkers/RemoteServiceWorkerRegistrationImpl.cpp
Perry Jiang 96edc2a092 Bug 1557244 - Disallow resurrection of unregistered ServiceWorkerRegistrations r=asuth
- Remove `ServiceWorkerRegistration`s' pending uninstall flag.

- Rename `ServiceWorkerRegistrationListener`'s `RegistrationRemoved` method to
  `RegistrationCleared`; registrations are no longer necessarily consider
  invalid when they are removed from the "scope to registration map", but rather
  when they're both removed from the map and not controlling clients (at which
  point `Clear` is called).

- Maintain the invariant that no `ServiceWorkerRegistrationInfo` in
  `ServiceWorkerManager::RegistrationDataPerPrincipal::mInfos` (i.e. the "scope
  to registration map") is in the unregistered state. Assertions check this in
  `ServiceWorkerManager::AddScopeAndRegistration` and
  `ServiceWorkerRegistrationInfo::SetUnregistered`.

- Fix some incorrect web platform tests.

Differential Revision: https://phabricator.services.mozilla.com/D38684

--HG--
extra : moz-landing-system : lando
2019-07-26 18:41:48 +00:00

166 lines
4.7 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "RemoteServiceWorkerRegistrationImpl.h"
#include "ServiceWorkerRegistrationChild.h"
namespace mozilla {
namespace dom {
using mozilla::ipc::IPCResult;
using mozilla::ipc::ResponseRejectReason;
RemoteServiceWorkerRegistrationImpl::~RemoteServiceWorkerRegistrationImpl() {
MOZ_DIAGNOSTIC_ASSERT(!mOuter);
Shutdown();
}
void RemoteServiceWorkerRegistrationImpl::Shutdown() {
if (mShutdown) {
return;
}
mShutdown = true;
if (mActor) {
mActor->RevokeOwner(this);
mActor->MaybeStartTeardown();
mActor = nullptr;
}
}
void RemoteServiceWorkerRegistrationImpl::SetServiceWorkerRegistration(
ServiceWorkerRegistration* aReg) {
NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerRegistrationImpl);
MOZ_DIAGNOSTIC_ASSERT(!mOuter);
MOZ_DIAGNOSTIC_ASSERT(aReg);
mOuter = aReg;
}
void RemoteServiceWorkerRegistrationImpl::ClearServiceWorkerRegistration(
ServiceWorkerRegistration* aReg) {
NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerRegistrationImpl);
MOZ_DIAGNOSTIC_ASSERT(mOuter);
MOZ_DIAGNOSTIC_ASSERT(aReg == mOuter);
mOuter = nullptr;
}
void RemoteServiceWorkerRegistrationImpl::Update(
ServiceWorkerRegistrationCallback&& aSuccessCB,
ServiceWorkerFailureCallback&& aFailureCB) {
if (!mActor) {
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
return;
}
mActor->SendUpdate(
[successCB = std::move(aSuccessCB), aFailureCB](
const IPCServiceWorkerRegistrationDescriptorOrCopyableErrorResult&
aResult) {
if (aResult.type() ==
IPCServiceWorkerRegistrationDescriptorOrCopyableErrorResult::
TCopyableErrorResult) {
// application layer error
auto& rv = aResult.get_CopyableErrorResult();
MOZ_DIAGNOSTIC_ASSERT(rv.Failed());
aFailureCB(CopyableErrorResult(rv));
return;
}
// success
auto& ipcDesc = aResult.get_IPCServiceWorkerRegistrationDescriptor();
successCB(ServiceWorkerRegistrationDescriptor(ipcDesc));
},
[aFailureCB](ResponseRejectReason&& aReason) {
// IPC layer error
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
});
}
void RemoteServiceWorkerRegistrationImpl::Unregister(
ServiceWorkerBoolCallback&& aSuccessCB,
ServiceWorkerFailureCallback&& aFailureCB) {
if (!mActor) {
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
return;
}
mActor->SendUnregister(
[successCB = std::move(aSuccessCB),
aFailureCB](Tuple<bool, CopyableErrorResult>&& aResult) {
if (Get<1>(aResult).Failed()) {
// application layer error
aFailureCB(Get<1>(aResult));
return;
}
// success
successCB(Get<0>(aResult));
},
[aFailureCB](ResponseRejectReason&& aReason) {
// IPC layer error
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
});
}
RemoteServiceWorkerRegistrationImpl::RemoteServiceWorkerRegistrationImpl(
const ServiceWorkerRegistrationDescriptor& aDescriptor)
: mActor(nullptr), mOuter(nullptr), mShutdown(false) {
PBackgroundChild* parentActor =
BackgroundChild::GetOrCreateForCurrentThread();
if (NS_WARN_IF(!parentActor)) {
Shutdown();
return;
}
ServiceWorkerRegistrationChild* actor =
ServiceWorkerRegistrationChild::Create();
if (NS_WARN_IF(!actor)) {
Shutdown();
return;
}
PServiceWorkerRegistrationChild* sentActor =
parentActor->SendPServiceWorkerRegistrationConstructor(
actor, aDescriptor.ToIPC());
if (NS_WARN_IF(!sentActor)) {
Shutdown();
return;
}
MOZ_DIAGNOSTIC_ASSERT(sentActor == actor);
mActor = actor;
mActor->SetOwner(this);
}
void RemoteServiceWorkerRegistrationImpl::RevokeActor(
ServiceWorkerRegistrationChild* aActor) {
MOZ_DIAGNOSTIC_ASSERT(mActor);
MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
mActor->RevokeOwner(this);
mActor = nullptr;
mShutdown = true;
if (mOuter) {
mOuter->RegistrationCleared();
}
}
void RemoteServiceWorkerRegistrationImpl::UpdateState(
const ServiceWorkerRegistrationDescriptor& aDescriptor) {
if (mOuter) {
mOuter->UpdateState(aDescriptor);
}
}
void RemoteServiceWorkerRegistrationImpl::FireUpdateFound() {
if (mOuter) {
mOuter->MaybeDispatchUpdateFoundRunnable();
}
}
} // namespace dom
} // namespace mozilla