1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ServiceWorkerContainerProxy.h"
9 #include "mozilla/dom/ServiceWorkerContainerParent.h"
10 #include "mozilla/dom/ServiceWorkerManager.h"
11 #include "mozilla/ipc/BackgroundParent.h"
12 #include "mozilla/SchedulerGroup.h"
13 #include "mozilla/ScopeExit.h"
15 namespace mozilla::dom
{
17 using mozilla::ipc::AssertIsOnBackgroundThread
;
19 ServiceWorkerContainerProxy::~ServiceWorkerContainerProxy() {
21 MOZ_DIAGNOSTIC_ASSERT(!mActor
);
24 ServiceWorkerContainerProxy::ServiceWorkerContainerProxy(
25 ServiceWorkerContainerParent
* aActor
)
27 AssertIsOnBackgroundThread();
28 MOZ_DIAGNOSTIC_ASSERT(mActor
);
30 // The container does not directly listen for updates, so we don't need
31 // to immediately initialize. The controllerchange event comes via the
32 // ClientSource associated with the ServiceWorkerContainer's bound global.
35 void ServiceWorkerContainerProxy::RevokeActor(
36 ServiceWorkerContainerParent
* aActor
) {
37 AssertIsOnBackgroundThread();
38 MOZ_DIAGNOSTIC_ASSERT(mActor
);
39 MOZ_DIAGNOSTIC_ASSERT(mActor
== aActor
);
43 RefPtr
<ServiceWorkerRegistrationPromise
> ServiceWorkerContainerProxy::Register(
44 const ClientInfo
& aClientInfo
, const nsACString
& aScopeURL
,
45 const nsACString
& aScriptURL
, ServiceWorkerUpdateViaCache aUpdateViaCache
) {
46 AssertIsOnBackgroundThread();
48 RefPtr
<ServiceWorkerRegistrationPromise::Private
> promise
=
49 new ServiceWorkerRegistrationPromise::Private(__func__
);
51 nsCOMPtr
<nsIRunnable
> r
= NS_NewRunnableFunction(
53 [aClientInfo
, aScopeURL
= nsCString(aScopeURL
),
54 aScriptURL
= nsCString(aScriptURL
), aUpdateViaCache
, promise
]() mutable {
55 auto scopeExit
= MakeScopeExit(
56 [&] { promise
->Reject(NS_ERROR_DOM_INVALID_STATE_ERR
, __func__
); });
58 RefPtr
<ServiceWorkerManager
> swm
= ServiceWorkerManager::GetInstance();
59 NS_ENSURE_TRUE_VOID(swm
);
61 swm
->Register(aClientInfo
, aScopeURL
, aScriptURL
, aUpdateViaCache
)
62 ->ChainTo(promise
.forget(), __func__
);
68 SchedulerGroup::Dispatch(TaskCategory::Other
, r
.forget()));
73 RefPtr
<ServiceWorkerRegistrationPromise
>
74 ServiceWorkerContainerProxy::GetRegistration(const ClientInfo
& aClientInfo
,
75 const nsACString
& aURL
) {
76 AssertIsOnBackgroundThread();
78 RefPtr
<ServiceWorkerRegistrationPromise::Private
> promise
=
79 new ServiceWorkerRegistrationPromise::Private(__func__
);
81 nsCOMPtr
<nsIRunnable
> r
= NS_NewRunnableFunction(
82 __func__
, [aClientInfo
, aURL
= nsCString(aURL
), promise
]() mutable {
83 auto scopeExit
= MakeScopeExit(
84 [&] { promise
->Reject(NS_ERROR_DOM_INVALID_STATE_ERR
, __func__
); });
86 RefPtr
<ServiceWorkerManager
> swm
= ServiceWorkerManager::GetInstance();
87 NS_ENSURE_TRUE_VOID(swm
);
89 swm
->GetRegistration(aClientInfo
, aURL
)
90 ->ChainTo(promise
.forget(), __func__
);
96 SchedulerGroup::Dispatch(TaskCategory::Other
, r
.forget()));
101 RefPtr
<ServiceWorkerRegistrationListPromise
>
102 ServiceWorkerContainerProxy::GetRegistrations(const ClientInfo
& aClientInfo
) {
103 AssertIsOnBackgroundThread();
105 RefPtr
<ServiceWorkerRegistrationListPromise::Private
> promise
=
106 new ServiceWorkerRegistrationListPromise::Private(__func__
);
108 nsCOMPtr
<nsIRunnable
> r
=
109 NS_NewRunnableFunction(__func__
, [aClientInfo
, promise
]() mutable {
110 auto scopeExit
= MakeScopeExit(
111 [&] { promise
->Reject(NS_ERROR_DOM_INVALID_STATE_ERR
, __func__
); });
113 RefPtr
<ServiceWorkerManager
> swm
= ServiceWorkerManager::GetInstance();
114 NS_ENSURE_TRUE_VOID(swm
);
116 swm
->GetRegistrations(aClientInfo
)->ChainTo(promise
.forget(), __func__
);
122 SchedulerGroup::Dispatch(TaskCategory::Other
, r
.forget()));
127 RefPtr
<ServiceWorkerRegistrationPromise
> ServiceWorkerContainerProxy::GetReady(
128 const ClientInfo
& aClientInfo
) {
129 AssertIsOnBackgroundThread();
131 RefPtr
<ServiceWorkerRegistrationPromise::Private
> promise
=
132 new ServiceWorkerRegistrationPromise::Private(__func__
);
134 nsCOMPtr
<nsIRunnable
> r
=
135 NS_NewRunnableFunction(__func__
, [aClientInfo
, promise
]() mutable {
136 auto scopeExit
= MakeScopeExit(
137 [&] { promise
->Reject(NS_ERROR_DOM_INVALID_STATE_ERR
, __func__
); });
139 RefPtr
<ServiceWorkerManager
> swm
= ServiceWorkerManager::GetInstance();
140 NS_ENSURE_TRUE_VOID(swm
);
142 swm
->WhenReady(aClientInfo
)->ChainTo(promise
.forget(), __func__
);
148 SchedulerGroup::Dispatch(TaskCategory::Other
, r
.forget()));
153 } // namespace mozilla::dom