no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / serviceworkers / ServiceWorkerContainerProxy.cpp
blob888731acef9fcb22fdb04e3a22489e2ca90e580d
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() {
20 // Any thread
21 MOZ_DIAGNOSTIC_ASSERT(!mActor);
24 ServiceWorkerContainerProxy::ServiceWorkerContainerProxy(
25 ServiceWorkerContainerParent* aActor)
26 : mActor(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);
40 mActor = nullptr;
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(
52 __func__,
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__);
64 scopeExit.release();
65 });
67 MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));
69 return promise;
72 RefPtr<ServiceWorkerRegistrationPromise>
73 ServiceWorkerContainerProxy::GetRegistration(const ClientInfo& aClientInfo,
74 const nsACString& aURL) {
75 AssertIsOnBackgroundThread();
77 RefPtr<ServiceWorkerRegistrationPromise::Private> promise =
78 new ServiceWorkerRegistrationPromise::Private(__func__);
80 nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
81 __func__, [aClientInfo, aURL = nsCString(aURL), promise]() mutable {
82 auto scopeExit = MakeScopeExit(
83 [&] { promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); });
85 RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
86 NS_ENSURE_TRUE_VOID(swm);
88 swm->GetRegistration(aClientInfo, aURL)
89 ->ChainTo(promise.forget(), __func__);
91 scopeExit.release();
92 });
94 MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));
96 return promise;
99 RefPtr<ServiceWorkerRegistrationListPromise>
100 ServiceWorkerContainerProxy::GetRegistrations(const ClientInfo& aClientInfo) {
101 AssertIsOnBackgroundThread();
103 RefPtr<ServiceWorkerRegistrationListPromise::Private> promise =
104 new ServiceWorkerRegistrationListPromise::Private(__func__);
106 nsCOMPtr<nsIRunnable> r =
107 NS_NewRunnableFunction(__func__, [aClientInfo, promise]() mutable {
108 auto scopeExit = MakeScopeExit(
109 [&] { promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); });
111 RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
112 NS_ENSURE_TRUE_VOID(swm);
114 swm->GetRegistrations(aClientInfo)->ChainTo(promise.forget(), __func__);
116 scopeExit.release();
119 MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));
121 return promise;
124 RefPtr<ServiceWorkerRegistrationPromise> ServiceWorkerContainerProxy::GetReady(
125 const ClientInfo& aClientInfo) {
126 AssertIsOnBackgroundThread();
128 RefPtr<ServiceWorkerRegistrationPromise::Private> promise =
129 new ServiceWorkerRegistrationPromise::Private(__func__);
131 nsCOMPtr<nsIRunnable> r =
132 NS_NewRunnableFunction(__func__, [aClientInfo, promise]() mutable {
133 auto scopeExit = MakeScopeExit(
134 [&] { promise->Reject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__); });
136 RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
137 NS_ENSURE_TRUE_VOID(swm);
139 swm->WhenReady(aClientInfo)->ChainTo(promise.forget(), __func__);
141 scopeExit.release();
144 MOZ_ALWAYS_SUCCEEDS(SchedulerGroup::Dispatch(r.forget()));
146 return promise;
149 } // namespace mozilla::dom