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
#ifndef mozilla_dom_serviceworkerprivateimpl_h__
8
#define mozilla_dom_serviceworkerprivateimpl_h__
9
10
#include <functional>
11
12
#include "nsCOMPtr.h"
13
#include "nsISupportsImpl.h"
14
#include "nsTArray.h"
15
16
#include "ServiceWorkerPrivate.h"
17
#include "mozilla/Attributes.h"
18
#include "mozilla/MozPromise.h"
19
#include "mozilla/RefPtr.h"
20
#include "mozilla/UniquePtr.h"
21
#include "mozilla/dom/RemoteWorkerController.h"
22
#include "mozilla/dom/RemoteWorkerTypes.h"
23
#include "mozilla/dom/ServiceWorkerOpArgs.h"
24
25
class nsIInterceptedChannel;
26
27
namespace mozilla {
28
29
template <typename T>
30
class Maybe;
31
32
namespace dom {
33
34
class ClientInfoAndState;
35
class LifeCycleEventCallback;
36
class RemoteWorkerControllerChild;
37
class ServiceWorkerCloneData;
38
class ServiceWorkerRegistrationInfo;
39
40
class ServiceWorkerPrivateImpl final : public ServiceWorkerPrivate::Inner,
41
public RemoteWorkerObserver {
42
public:
43
NS_INLINE_DECL_REFCOUNTING(ServiceWorkerPrivateImpl, override);
44
45
explicit ServiceWorkerPrivateImpl(RefPtr<ServiceWorkerPrivate> aOuter);
46
47
nsresult Initialize();
48
49
RefPtr<GenericPromise> SetSkipWaitingFlag();
50
51
private:
52
class RAIIActorPtrHolder;
53
54
~ServiceWorkerPrivateImpl();
55
56
/**
57
* ServiceWorkerPrivate::Inner
58
*/
59
nsresult SendMessageEvent(
60
RefPtr<ServiceWorkerCloneData>&& aData,
61
const ClientInfoAndState& aClientInfoAndState) override;
62
63
nsresult CheckScriptEvaluation(
64
RefPtr<LifeCycleEventCallback> aCallback) override;
65
66
nsresult SendLifeCycleEvent(
67
const nsAString& aEventName,
68
RefPtr<LifeCycleEventCallback> aCallback) override;
69
70
nsresult SendPushEvent(RefPtr<ServiceWorkerRegistrationInfo> aRegistration,
71
const nsAString& aMessageId,
72
const Maybe<nsTArray<uint8_t>>& aData) override;
73
74
nsresult SendPushSubscriptionChangeEvent() override;
75
76
nsresult SendNotificationEvent(const nsAString& aEventName,
77
const nsAString& aID, const nsAString& aTitle,
78
const nsAString& aDir, const nsAString& aLang,
79
const nsAString& aBody, const nsAString& aTag,
80
const nsAString& aIcon, const nsAString& aData,
81
const nsAString& aBehavior,
82
const nsAString& aScope,
83
uint32_t aDisableOpenClickDelay) override;
84
85
nsresult SendFetchEvent(RefPtr<ServiceWorkerRegistrationInfo> aRegistration,
86
nsCOMPtr<nsIInterceptedChannel> aChannel,
87
const nsAString& aClientId,
88
const nsAString& aResultingClientId,
89
bool aIsReload) override;
90
91
nsresult SpawnWorkerIfNeeded() override;
92
93
void TerminateWorker() override;
94
95
void UpdateState(ServiceWorkerState aState) override;
96
97
void NoteDeadOuter() override;
98
99
bool WorkerIsDead() const override;
100
101
/**
102
* RemoteWorkerObserver
103
*/
104
void CreationFailed() override;
105
106
void CreationSucceeded() override;
107
108
void ErrorReceived(const ErrorValue& aError) override;
109
110
void Terminated() override;
111
112
// Refreshes only the parts of mRemoteWorkerData that may change over time.
113
void RefreshRemoteWorkerData(
114
const RefPtr<ServiceWorkerRegistrationInfo>& aRegistration);
115
116
nsresult SendPushEventInternal(
117
RefPtr<ServiceWorkerRegistrationInfo>&& aRegistration,
118
ServiceWorkerPushEventOpArgs&& aArgs);
119
120
nsresult SendFetchEventInternal(
121
RefPtr<ServiceWorkerRegistrationInfo>&& aRegistration,
122
ServiceWorkerFetchEventOpArgs&& aArgs,
123
nsCOMPtr<nsIInterceptedChannel>&& aChannel);
124
125
void Shutdown();
126
127
RefPtr<GenericNonExclusivePromise> ShutdownInternal();
128
129
nsresult ExecServiceWorkerOp(
130
ServiceWorkerOpArgs&& aArgs,
131
std::function<void(ServiceWorkerOpResult&&)>&& aSuccessCallback,
132
std::function<void()>&& aFailureCallback = [] {});
133
134
class PendingFunctionalEvent {
135
public:
136
PendingFunctionalEvent(
137
ServiceWorkerPrivateImpl* aOwner,
138
RefPtr<ServiceWorkerRegistrationInfo>&& aRegistration);
139
140
virtual ~PendingFunctionalEvent();
141
142
virtual nsresult Send() = 0;
143
144
protected:
145
ServiceWorkerPrivateImpl* const MOZ_NON_OWNING_REF mOwner;
146
RefPtr<ServiceWorkerRegistrationInfo> mRegistration;
147
};
148
149
class PendingPushEvent final : public PendingFunctionalEvent {
150
public:
151
PendingPushEvent(ServiceWorkerPrivateImpl* aOwner,
152
RefPtr<ServiceWorkerRegistrationInfo>&& aRegistration,
153
ServiceWorkerPushEventOpArgs&& aArgs);
154
155
nsresult Send() override;
156
157
private:
158
ServiceWorkerPushEventOpArgs mArgs;
159
};
160
161
class PendingFetchEvent final : public PendingFunctionalEvent {
162
public:
163
PendingFetchEvent(ServiceWorkerPrivateImpl* aOwner,
164
RefPtr<ServiceWorkerRegistrationInfo>&& aRegistration,
165
ServiceWorkerFetchEventOpArgs&& aArgs,
166
nsCOMPtr<nsIInterceptedChannel>&& aChannel);
167
168
nsresult Send() override;
169
170
~PendingFetchEvent();
171
172
private:
173
ServiceWorkerFetchEventOpArgs mArgs;
174
nsCOMPtr<nsIInterceptedChannel> mChannel;
175
};
176
177
nsTArray<UniquePtr<PendingFunctionalEvent>> mPendingFunctionalEvents;
178
179
/**
180
* It's possible that there are still in-progress operations when a
181
* a termination operation is issued. In this case, it's important to keep
182
* the RemoteWorkerControllerChild actor alive until all pending operations
183
* have completed before destroying it with Send__delete__().
184
*
185
* RAIIActorPtrHolder holds a singular, owning reference to a
186
* RemoteWorkerControllerChild actor and is responsible for destroying the
187
* actor in its (i.e. the holder's) destructor. This implies that all
188
* in-progress operations must maintain a strong reference to their
189
* corresponding holders and release the reference once completed/canceled.
190
*
191
* Additionally a RAIIActorPtrHolder must be initialized with a non-null actor
192
* and cannot be moved or copied. Therefore, the identities of two held
193
* actors can be compared by simply comparing their holders' addresses.
194
*/
195
class RAIIActorPtrHolder final {
196
public:
197
NS_INLINE_DECL_REFCOUNTING(RAIIActorPtrHolder)
198
199
explicit RAIIActorPtrHolder(
200
already_AddRefed<RemoteWorkerControllerChild> aActor);
201
202
RAIIActorPtrHolder(const RAIIActorPtrHolder& aOther) = delete;
203
RAIIActorPtrHolder& operator=(const RAIIActorPtrHolder& aOther) = delete;
204
205
RAIIActorPtrHolder(RAIIActorPtrHolder&& aOther) = delete;
206
RAIIActorPtrHolder& operator=(RAIIActorPtrHolder&& aOther) = delete;
207
208
RemoteWorkerControllerChild* operator->() const
209
MOZ_NO_ADDREF_RELEASE_ON_RETURN;
210
211
RemoteWorkerControllerChild* get() const;
212
213
RefPtr<GenericPromise> OnDestructor();
214
215
private:
216
~RAIIActorPtrHolder();
217
218
MozPromiseHolder<GenericPromise> mDestructorPromiseHolder;
219
220
const RefPtr<RemoteWorkerControllerChild> mActor;
221
};
222
223
RefPtr<RAIIActorPtrHolder> mControllerChild;
224
225
RefPtr<ServiceWorkerPrivate> mOuter;
226
227
RemoteWorkerData mRemoteWorkerData;
228
};
229
230
} // namespace dom
231
} // namespace mozilla
232
233
#endif // mozilla_dom_serviceworkerprivateimpl_h__