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
ServiceWorkerRegistrationCallback&& aSuccessCB,
53
ServiceWorkerFailureCallback&& aFailureCB) {
54
if (!mActor) {
55
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
56
return;
57
}
58
59
mActor->SendUpdate(
60
[successCB = std::move(aSuccessCB), aFailureCB](
61
const IPCServiceWorkerRegistrationDescriptorOrCopyableErrorResult&
62
aResult) {
63
if (aResult.type() ==
64
IPCServiceWorkerRegistrationDescriptorOrCopyableErrorResult::
65
TCopyableErrorResult) {
66
// application layer error
67
auto& rv = aResult.get_CopyableErrorResult();
68
MOZ_DIAGNOSTIC_ASSERT(rv.Failed());
69
aFailureCB(CopyableErrorResult(rv));
70
return;
71
}
72
// success
73
auto& ipcDesc = aResult.get_IPCServiceWorkerRegistrationDescriptor();
74
successCB(ServiceWorkerRegistrationDescriptor(ipcDesc));
75
},
76
[aFailureCB](ResponseRejectReason&& aReason) {
77
// IPC layer error
78
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
79
});
80
}
81
82
void RemoteServiceWorkerRegistrationImpl::Unregister(
83
ServiceWorkerBoolCallback&& aSuccessCB,
84
ServiceWorkerFailureCallback&& aFailureCB) {
85
if (!mActor) {
86
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
87
return;
88
}
89
90
mActor->SendUnregister(
91
[successCB = std::move(aSuccessCB),
92
aFailureCB](Tuple<bool, CopyableErrorResult>&& aResult) {
93
if (Get<1>(aResult).Failed()) {
94
// application layer error
95
aFailureCB(Get<1>(aResult));
96
return;
97
}
98
// success
99
successCB(Get<0>(aResult));
100
},
101
[aFailureCB](ResponseRejectReason&& aReason) {
102
// IPC layer error
103
aFailureCB(CopyableErrorResult(NS_ERROR_DOM_INVALID_STATE_ERR));
104
});
105
}
106
107
RemoteServiceWorkerRegistrationImpl::RemoteServiceWorkerRegistrationImpl(
108
const ServiceWorkerRegistrationDescriptor& aDescriptor)
109
: mActor(nullptr), mOuter(nullptr), mShutdown(false) {
110
PBackgroundChild* parentActor =
111
BackgroundChild::GetOrCreateForCurrentThread();
112
if (NS_WARN_IF(!parentActor)) {
113
Shutdown();
114
return;
115
}
116
117
ServiceWorkerRegistrationChild* actor =
118
ServiceWorkerRegistrationChild::Create();
119
if (NS_WARN_IF(!actor)) {
120
Shutdown();
121
return;
122
}
123
124
PServiceWorkerRegistrationChild* sentActor =
125
parentActor->SendPServiceWorkerRegistrationConstructor(
126
actor, aDescriptor.ToIPC());
127
if (NS_WARN_IF(!sentActor)) {
128
Shutdown();
129
return;
130
}
131
MOZ_DIAGNOSTIC_ASSERT(sentActor == actor);
132
133
mActor = actor;
134
mActor->SetOwner(this);
135
}
136
137
void RemoteServiceWorkerRegistrationImpl::RevokeActor(
138
ServiceWorkerRegistrationChild* aActor) {
139
MOZ_DIAGNOSTIC_ASSERT(mActor);
140
MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
141
mActor->RevokeOwner(this);
142
mActor = nullptr;
143
144
mShutdown = true;
145
146
if (mOuter) {
147
mOuter->RegistrationCleared();
148
}
149
}
150
151
void RemoteServiceWorkerRegistrationImpl::UpdateState(
152
const ServiceWorkerRegistrationDescriptor& aDescriptor) {
153
if (mOuter) {
154
mOuter->UpdateState(aDescriptor);
155
}
156
}
157
158
void RemoteServiceWorkerRegistrationImpl::FireUpdateFound() {
159
if (mOuter) {
160
mOuter->MaybeDispatchUpdateFoundRunnable();
161
}
162
}
163
164
} // namespace dom
165
} // namespace mozilla