2018-07-10 02:02:40 +03:00
|
|
|
/* -*- 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 "ServiceWorkerProxy.h"
|
2020-11-10 14:55:00 +03:00
|
|
|
#include "ServiceWorkerCloneData.h"
|
|
|
|
#include "ServiceWorkerManager.h"
|
|
|
|
#include "ServiceWorkerParent.h"
|
2018-07-10 02:02:40 +03:00
|
|
|
|
2020-04-07 18:16:33 +03:00
|
|
|
#include "mozilla/SchedulerGroup.h"
|
2020-12-07 17:25:59 +03:00
|
|
|
#include "mozilla/ScopeExit.h"
|
2020-11-10 14:55:00 +03:00
|
|
|
#include "mozilla/dom/ClientState.h"
|
2018-07-10 02:02:40 +03:00
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
|
|
|
#include "ServiceWorkerInfo.h"
|
|
|
|
|
2022-05-09 23:41:14 +03:00
|
|
|
namespace mozilla::dom {
|
2018-07-10 02:02:40 +03:00
|
|
|
|
|
|
|
using mozilla::ipc::AssertIsOnBackgroundThread;
|
|
|
|
|
|
|
|
ServiceWorkerProxy::~ServiceWorkerProxy() {
|
|
|
|
// Any thread
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mActor);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceWorkerProxy::MaybeShutdownOnBGThread() {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
if (!mActor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mActor->MaybeSendDelete();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceWorkerProxy::InitOnMainThread() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
auto scopeExit = MakeScopeExit([&] { MaybeShutdownOnMainThread(); });
|
|
|
|
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
NS_ENSURE_TRUE_VOID(swm);
|
|
|
|
|
|
|
|
RefPtr<ServiceWorkerRegistrationInfo> reg =
|
|
|
|
swm->GetRegistration(mDescriptor.PrincipalInfo(), mDescriptor.Scope());
|
|
|
|
NS_ENSURE_TRUE_VOID(reg);
|
|
|
|
|
|
|
|
RefPtr<ServiceWorkerInfo> info = reg->GetByDescriptor(mDescriptor);
|
|
|
|
NS_ENSURE_TRUE_VOID(info);
|
|
|
|
|
|
|
|
scopeExit.release();
|
|
|
|
|
|
|
|
mInfo = new nsMainThreadPtrHolder<ServiceWorkerInfo>(
|
|
|
|
"ServiceWorkerProxy::mInfo", info);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceWorkerProxy::MaybeShutdownOnMainThread() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r = NewRunnableMethod(
|
|
|
|
__func__, this, &ServiceWorkerProxy::MaybeShutdownOnBGThread);
|
|
|
|
|
|
|
|
MOZ_ALWAYS_SUCCEEDS(mEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceWorkerProxy::StopListeningOnMainThread() {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
mInfo = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceWorkerProxy::ServiceWorkerProxy(
|
|
|
|
const ServiceWorkerDescriptor& aDescriptor)
|
2020-10-23 14:50:00 +03:00
|
|
|
: mEventTarget(GetCurrentSerialEventTarget()), mDescriptor(aDescriptor) {}
|
2018-07-10 02:02:40 +03:00
|
|
|
|
|
|
|
void ServiceWorkerProxy::Init(ServiceWorkerParent* aActor) {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aActor);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mActor);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mEventTarget);
|
|
|
|
|
|
|
|
mActor = aActor;
|
|
|
|
|
|
|
|
// Note, this must be done from a separate Init() method and not in
|
|
|
|
// the constructor. If done from the constructor the runnable can
|
|
|
|
// execute, complete, and release its reference before the constructor
|
|
|
|
// returns.
|
|
|
|
nsCOMPtr<nsIRunnable> r = NewRunnableMethod(
|
|
|
|
"ServiceWorkerProxy::Init", this, &ServiceWorkerProxy::InitOnMainThread);
|
Bug 1624819 - Remove TaskCategory and other quantum dom remnants. r=smaug,media-playback-reviewers,credential-management-reviewers,cookie-reviewers,places-reviewers,win-reviewers,valentin,mhowell,sgalich,alwu
Sorry this is not a particularly easy patch to review. But it should be
mostly straight-forward.
I kept Document::Dispatch mostly for convenience, but could be
cleaned-up too / changed by SchedulerGroup::Dispatch. Similarly maybe
that can just be NS_DispatchToMainThread if we add an NS_IsMainThread
check there or something (to preserve shutdown semantics).
Differential Revision: https://phabricator.services.mozilla.com/D190450
2023-10-10 11:51:12 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));
|
2018-07-10 02:02:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceWorkerProxy::RevokeActor(ServiceWorkerParent* aActor) {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mActor);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
|
|
|
|
mActor = nullptr;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r = NewRunnableMethod(
|
|
|
|
__func__, this, &ServiceWorkerProxy::StopListeningOnMainThread);
|
Bug 1624819 - Remove TaskCategory and other quantum dom remnants. r=smaug,media-playback-reviewers,credential-management-reviewers,cookie-reviewers,places-reviewers,win-reviewers,valentin,mhowell,sgalich,alwu
Sorry this is not a particularly easy patch to review. But it should be
mostly straight-forward.
I kept Document::Dispatch mostly for convenience, but could be
cleaned-up too / changed by SchedulerGroup::Dispatch. Similarly maybe
that can just be NS_DispatchToMainThread if we add an NS_IsMainThread
check there or something (to preserve shutdown semantics).
Differential Revision: https://phabricator.services.mozilla.com/D190450
2023-10-10 11:51:12 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));
|
2018-07-10 02:02:40 +03:00
|
|
|
}
|
|
|
|
|
2018-07-10 02:02:40 +03:00
|
|
|
void ServiceWorkerProxy::PostMessage(RefPtr<ServiceWorkerCloneData>&& aData,
|
|
|
|
const ClientInfo& aClientInfo,
|
|
|
|
const ClientState& aClientState) {
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
RefPtr<ServiceWorkerProxy> self = this;
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
__func__,
|
|
|
|
[self, data = std::move(aData), aClientInfo, aClientState]() mutable {
|
|
|
|
if (!self->mInfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self->mInfo->PostMessage(std::move(data), aClientInfo, aClientState);
|
|
|
|
});
|
Bug 1624819 - Remove TaskCategory and other quantum dom remnants. r=smaug,media-playback-reviewers,credential-management-reviewers,cookie-reviewers,places-reviewers,win-reviewers,valentin,mhowell,sgalich,alwu
Sorry this is not a particularly easy patch to review. But it should be
mostly straight-forward.
I kept Document::Dispatch mostly for convenience, but could be
cleaned-up too / changed by SchedulerGroup::Dispatch. Similarly maybe
that can just be NS_DispatchToMainThread if we add an NS_IsMainThread
check there or something (to preserve shutdown semantics).
Differential Revision: https://phabricator.services.mozilla.com/D190450
2023-10-10 11:51:12 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));
|
2018-07-10 02:02:40 +03:00
|
|
|
}
|
|
|
|
|
2022-05-09 23:41:14 +03:00
|
|
|
} // namespace mozilla::dom
|