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 "RemoteServiceWorkerRegistrationImpl.h"
8
9
#include "ServiceWorkerRegistrationChild.h"
10
11
namespace mozilla {
12
namespace dom {
13
14
using mozilla::ipc::IPCResult;
15
using mozilla::ipc::ResponseRejectReason;
16
17
RemoteServiceWorkerRegistrationImpl::~RemoteServiceWorkerRegistrationImpl() {
18
MOZ_DIAGNOSTIC_ASSERT(!mOuter);
19
Shutdown();
20
}
21
22
void RemoteServiceWorkerRegistrationImpl::Shutdown() {
23
if (mShutdown) {
24
return;
25
}
26
mShutdown = true;
27
28
if (mActor) {
29
mActor->RevokeOwner(this);
30
mActor->MaybeStartTeardown();
31
mActor = nullptr;
32
}
33
}
34
35
void RemoteServiceWorkerRegistrationImpl::SetServiceWorkerRegistration(
36
ServiceWorkerRegistration* aReg) {
37
NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerRegistrationImpl);
38
MOZ_DIAGNOSTIC_ASSERT(!mOuter);
39
MOZ_DIAGNOSTIC_ASSERT(aReg);
40
mOuter = aReg;
41
}
42
43
void RemoteServiceWorkerRegistrationImpl::ClearServiceWorkerRegistration(
44
ServiceWorkerRegistration* aReg) {
45
NS_ASSERT_OWNINGTHREAD(RemoteServiceWorkerRegistrationImpl);
46
MOZ_DIAGNOSTIC_ASSERT(mOuter);
47
MOZ_DIAGNOSTIC_ASSERT(aReg == mOuter);
48
mOuter = nullptr;
49
}
50
51
void RemoteServiceWorkerRegistrationImpl::Update(
52
const nsCString& aNewestWorkerScriptUrl,
53
ServiceWorkerRegistrationCallback&& aSuccessCB,
54
ServiceWorkerFailureCallback&& aFailureCB) {
55
if (!mActor) {
56
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
57
return;
58
}
59
60
mActor->SendUpdate(
61
aNewestWorkerScriptUrl,
62
[successCB = std::move(aSuccessCB), aFailureCB](
63
const IPCServiceWorkerRegistrationDescriptorOrCopyableErrorResult&
64
aResult) {
65
if (aResult.type() ==
66
IPCServiceWorkerRegistrationDescriptorOrCopyableErrorResult::
67
TCopyableErrorResult) {
68
// application layer error
69
auto& rv = aResult.get_CopyableErrorResult();
70
MOZ_DIAGNOSTIC_ASSERT(rv.Failed());
71
aFailureCB(CopyableErrorResult(rv));
72
return;
73
}
74
// success
75
auto& ipcDesc = aResult.get_IPCServiceWorkerRegistrationDescriptor();
76
successCB(ServiceWorkerRegistrationDescriptor(ipcDesc));
77
},
78
[aFailureCB](ResponseRejectReason&& aReason) {
79
// IPC layer error
80
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
81
});
82
}
83
84
void RemoteServiceWorkerRegistrationImpl::Unregister(
85
ServiceWorkerBoolCallback&& aSuccessCB,
86
ServiceWorkerFailureCallback&& aFailureCB) {
87
if (!mActor) {
88
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
89
return;
90
}
91
92
mActor->SendUnregister(
93
[successCB = std::move(aSuccessCB),
94
aFailureCB](Tuple<bool, CopyableErrorResult>&& aResult) {
95
if (Get<1>(aResult).Failed()) {
96
// application layer error
97
aFailureCB(Get<1>(aResult));
98
return;
99
}
100
// success
101
successCB(Get<0>(aResult));
102
},
103
[aFailureCB](ResponseRejectReason&& aReason) {
104
// IPC layer error
105
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
106
});
107
}
108
109
RemoteServiceWorkerRegistrationImpl::RemoteServiceWorkerRegistrationImpl(
110
const ServiceWorkerRegistrationDescriptor& aDescriptor)
111
: mActor(nullptr), mOuter(nullptr), mShutdown(false) {
112
PBackgroundChild* parentActor =
113
BackgroundChild::GetOrCreateForCurrentThread();
114
if (NS_WARN_IF(!parentActor)) {
115
Shutdown();
116
return;
117
}
118
119
ServiceWorkerRegistrationChild* actor =
120
ServiceWorkerRegistrationChild::Create();
121
if (NS_WARN_IF(!actor)) {
122
Shutdown();
123
return;
124
}
125
126
PServiceWorkerRegistrationChild* sentActor =
127
parentActor->SendPServiceWorkerRegistrationConstructor(
128
actor, aDescriptor.ToIPC());
129
if (NS_WARN_IF(!sentActor)) {
130
Shutdown();
131
return;
132
}
133
MOZ_DIAGNOSTIC_ASSERT(sentActor == actor);
134
135
mActor = actor;
136
mActor->SetOwner(this);
137
}
138
139
void RemoteServiceWorkerRegistrationImpl::RevokeActor(
140
ServiceWorkerRegistrationChild* aActor) {
141
MOZ_DIAGNOSTIC_ASSERT(mActor);
142
MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
143
mActor->RevokeOwner(this);
144
mActor = nullptr;
145
146
mShutdown = true;
147
148
if (mOuter) {
149
mOuter->RegistrationCleared();
150
}
151
}
152
153
void RemoteServiceWorkerRegistrationImpl::UpdateState(
154
const ServiceWorkerRegistrationDescriptor& aDescriptor) {
155
if (mOuter) {
156
mOuter->UpdateState(aDescriptor);
157
}
158
}
159
160
void RemoteServiceWorkerRegistrationImpl::FireUpdateFound() {
161
if (mOuter) {
162
mOuter->MaybeDispatchUpdateFoundRunnable();
163
}
164
}
165
166
} // namespace dom
167
} // namespace mozilla