Bug 1842509 - Remove media.webvtt.regions.enabled pref r=alwu,webidl,smaug,peterv
[gecko.git] / dom / serviceworkers / ServiceWorkerContainerProxy.cpp
blob71a853e1ee3a687b136274b58105c5c2aa167f15
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(
68 SchedulerGroup::Dispatch(TaskCategory::Other, r.forget()));
70 return promise;
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__);
92 scopeExit.release();
93 });
95 MOZ_ALWAYS_SUCCEEDS(
96 SchedulerGroup::Dispatch(TaskCategory::Other, r.forget()));
98 return promise;
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__);
118 scopeExit.release();
121 MOZ_ALWAYS_SUCCEEDS(
122 SchedulerGroup::Dispatch(TaskCategory::Other, r.forget()));
124 return promise;
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__);
144 scopeExit.release();
147 MOZ_ALWAYS_SUCCEEDS(
148 SchedulerGroup::Dispatch(TaskCategory::Other, r.forget()));
150 return promise;
153 } // namespace mozilla::dom