Source code

Revision control

Other Tools

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/. */
6
7
#include "ServiceWorkerContainerImpl.h"
8
9
#include "ServiceWorkerRegistration.h"
10
11
namespace mozilla {
12
namespace dom {
13
14
ServiceWorkerContainerImpl::~ServiceWorkerContainerImpl() {
15
MOZ_DIAGNOSTIC_ASSERT(!mOuter);
16
}
17
18
ServiceWorkerContainerImpl::ServiceWorkerContainerImpl() : mOuter(nullptr) {}
19
20
void ServiceWorkerContainerImpl::AddContainer(ServiceWorkerContainer* aOuter) {
21
MOZ_DIAGNOSTIC_ASSERT(aOuter);
22
MOZ_DIAGNOSTIC_ASSERT(!mOuter);
23
mOuter = aOuter;
24
}
25
26
void ServiceWorkerContainerImpl::RemoveContainer(
27
ServiceWorkerContainer* aOuter) {
28
MOZ_DIAGNOSTIC_ASSERT(aOuter);
29
MOZ_DIAGNOSTIC_ASSERT(mOuter == aOuter);
30
mOuter = nullptr;
31
}
32
33
void ServiceWorkerContainerImpl::Register(
34
const ClientInfo& aClientInfo, const nsACString& aScopeURL,
35
const nsACString& aScriptURL, ServiceWorkerUpdateViaCache aUpdateViaCache,
36
ServiceWorkerRegistrationCallback&& aSuccessCB,
37
ServiceWorkerFailureCallback&& aFailureCB) const {
38
MOZ_DIAGNOSTIC_ASSERT(mOuter);
39
40
nsIGlobalObject* global = mOuter->GetParentObject();
41
if (NS_WARN_IF(!global)) {
42
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
43
return;
44
}
45
46
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
47
if (NS_WARN_IF(!swm)) {
48
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
49
return;
50
}
51
52
auto holder =
53
MakeRefPtr<DOMMozPromiseRequestHolder<ServiceWorkerRegistrationPromise>>(
54
global);
55
56
swm->Register(aClientInfo, aScopeURL, aScriptURL, aUpdateViaCache)
57
->Then(
58
global->EventTargetFor(TaskCategory::Other), __func__,
59
[successCB = std::move(aSuccessCB),
60
holder](const ServiceWorkerRegistrationDescriptor& aDescriptor) {
61
holder->Complete();
62
successCB(aDescriptor);
63
},
64
[failureCB = std::move(aFailureCB),
65
holder](const CopyableErrorResult& aResult) {
66
holder->Complete();
67
failureCB(CopyableErrorResult(aResult));
68
})
69
->Track(*holder);
70
}
71
72
void ServiceWorkerContainerImpl::GetRegistration(
73
const ClientInfo& aClientInfo, const nsACString& aURL,
74
ServiceWorkerRegistrationCallback&& aSuccessCB,
75
ServiceWorkerFailureCallback&& aFailureCB) const {
76
MOZ_DIAGNOSTIC_ASSERT(mOuter);
77
78
nsIGlobalObject* global = mOuter->GetParentObject();
79
if (NS_WARN_IF(!global)) {
80
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
81
return;
82
}
83
84
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
85
if (NS_WARN_IF(!swm)) {
86
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
87
return;
88
}
89
90
auto holder =
91
MakeRefPtr<DOMMozPromiseRequestHolder<ServiceWorkerRegistrationPromise>>(
92
global);
93
94
swm->GetRegistration(aClientInfo, aURL)
95
->Then(
96
global->EventTargetFor(TaskCategory::Other), __func__,
97
[successCB = std::move(aSuccessCB),
98
holder](const ServiceWorkerRegistrationDescriptor& aDescriptor) {
99
holder->Complete();
100
successCB(aDescriptor);
101
},
102
[failureCB = std::move(aFailureCB),
103
holder](const CopyableErrorResult& aResult) {
104
holder->Complete();
105
failureCB(CopyableErrorResult(aResult));
106
})
107
->Track(*holder);
108
}
109
110
void ServiceWorkerContainerImpl::GetRegistrations(
111
const ClientInfo& aClientInfo,
112
ServiceWorkerRegistrationListCallback&& aSuccessCB,
113
ServiceWorkerFailureCallback&& aFailureCB) const {
114
MOZ_DIAGNOSTIC_ASSERT(mOuter);
115
116
nsIGlobalObject* global = mOuter->GetParentObject();
117
if (NS_WARN_IF(!global)) {
118
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
119
return;
120
}
121
122
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
123
if (NS_WARN_IF(!swm)) {
124
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
125
return;
126
}
127
128
auto holder = MakeRefPtr<
129
DOMMozPromiseRequestHolder<ServiceWorkerRegistrationListPromise>>(global);
130
131
swm->GetRegistrations(aClientInfo)
132
->Then(
133
global->EventTargetFor(TaskCategory::Other), __func__,
134
[successCB = std::move(aSuccessCB),
135
holder](const nsTArray<ServiceWorkerRegistrationDescriptor>& aList) {
136
holder->Complete();
137
successCB(aList);
138
},
139
[failureCB = std::move(aFailureCB),
140
holder](const CopyableErrorResult& aResult) {
141
holder->Complete();
142
failureCB(CopyableErrorResult(aResult));
143
})
144
->Track(*holder);
145
}
146
147
void ServiceWorkerContainerImpl::GetReady(
148
const ClientInfo& aClientInfo,
149
ServiceWorkerRegistrationCallback&& aSuccessCB,
150
ServiceWorkerFailureCallback&& aFailureCB) const {
151
MOZ_DIAGNOSTIC_ASSERT(mOuter);
152
153
nsIGlobalObject* global = mOuter->GetParentObject();
154
if (NS_WARN_IF(!global)) {
155
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
156
return;
157
}
158
159
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
160
if (NS_WARN_IF(!swm)) {
161
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
162
return;
163
}
164
165
auto holder =
166
MakeRefPtr<DOMMozPromiseRequestHolder<ServiceWorkerRegistrationPromise>>(
167
global);
168
169
swm->WhenReady(aClientInfo)
170
->Then(
171
global->EventTargetFor(TaskCategory::Other), __func__,
172
[successCB = std::move(aSuccessCB),
173
holder](const ServiceWorkerRegistrationDescriptor& aDescriptor) {
174
holder->Complete();
175
successCB(aDescriptor);
176
},
177
[failureCB = std::move(aFailureCB),
178
holder](const CopyableErrorResult& aResult) {
179
holder->Complete();
180
failureCB(CopyableErrorResult(aResult));
181
})
182
->Track(*holder);
183
}
184
185
} // namespace dom
186
} // namespace mozilla