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_serviceworkerevents_h__
8
#define mozilla_dom_serviceworkerevents_h__
9
10
#include "mozilla/dom/Event.h"
11
#include "mozilla/dom/ExtendableEventBinding.h"
12
#include "mozilla/dom/ExtendableMessageEventBinding.h"
13
#include "mozilla/dom/FetchEventBinding.h"
14
#include "mozilla/dom/File.h"
15
#include "mozilla/dom/Promise.h"
16
#include "mozilla/dom/Response.h"
17
#include "mozilla/dom/WorkerCommon.h"
18
19
#include "nsProxyRelease.h"
20
#include "nsContentUtils.h"
21
22
class nsIInterceptedChannel;
23
24
namespace mozilla {
25
namespace dom {
26
27
class Blob;
28
class Client;
29
class FetchEventOp;
30
class MessagePort;
31
struct PushEventInit;
32
class Request;
33
class ResponseOrPromise;
34
class ServiceWorker;
35
class ServiceWorkerRegistrationInfo;
36
37
// Defined in ServiceWorker.cpp
38
bool ServiceWorkerVisible(JSContext* aCx, JSObject* aObj);
39
40
class CancelChannelRunnable final : public Runnable {
41
nsMainThreadPtrHandle<nsIInterceptedChannel> mChannel;
42
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> mRegistration;
43
const nsresult mStatus;
44
45
public:
46
CancelChannelRunnable(
47
nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel,
48
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
49
nsresult aStatus);
50
51
NS_IMETHOD Run() override;
52
};
53
54
enum ExtendableEventResult { Rejected = 0, Resolved };
55
56
class ExtendableEventCallback {
57
public:
58
virtual void FinishedWithResult(ExtendableEventResult aResult) = 0;
59
60
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
61
};
62
63
class ExtendableEvent : public Event {
64
public:
65
class ExtensionsHandler {
66
public:
67
virtual bool WaitOnPromise(Promise& aPromise) = 0;
68
69
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
70
};
71
72
private:
73
RefPtr<ExtensionsHandler> mExtensionsHandler;
74
75
protected:
76
bool WaitOnPromise(Promise& aPromise);
77
78
explicit ExtendableEvent(mozilla::dom::EventTarget* aOwner);
79
~ExtendableEvent() = default;
80
81
public:
82
NS_DECL_ISUPPORTS_INHERITED
83
84
void SetKeepAliveHandler(ExtensionsHandler* aExtensionsHandler);
85
86
virtual JSObject* WrapObjectInternal(
87
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override {
88
return mozilla::dom::ExtendableEvent_Binding::Wrap(aCx, this, aGivenProto);
89
}
90
91
static already_AddRefed<ExtendableEvent> Constructor(
92
mozilla::dom::EventTarget* aOwner, const nsAString& aType,
93
const EventInit& aOptions) {
94
RefPtr<ExtendableEvent> e = new ExtendableEvent(aOwner);
95
bool trusted = e->Init(aOwner);
96
e->InitEvent(aType, aOptions.mBubbles, aOptions.mCancelable);
97
e->SetTrusted(trusted);
98
e->SetComposed(aOptions.mComposed);
99
return e.forget();
100
}
101
102
static already_AddRefed<ExtendableEvent> Constructor(
103
const GlobalObject& aGlobal, const nsAString& aType,
104
const EventInit& aOptions) {
105
nsCOMPtr<EventTarget> target = do_QueryInterface(aGlobal.GetAsSupports());
106
return Constructor(target, aType, aOptions);
107
}
108
109
void WaitUntil(JSContext* aCx, Promise& aPromise, ErrorResult& aRv);
110
111
virtual ExtendableEvent* AsExtendableEvent() override { return this; }
112
};
113
114
class FetchEvent final : public ExtendableEvent {
115
RefPtr<FetchEventOp> mRespondWithHandler;
116
nsMainThreadPtrHandle<nsIInterceptedChannel> mChannel;
117
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo> mRegistration;
118
RefPtr<Request> mRequest;
119
nsCString mScriptSpec;
120
nsCString mPreventDefaultScriptSpec;
121
nsString mClientId;
122
nsString mResultingClientId;
123
uint32_t mPreventDefaultLineNumber;
124
uint32_t mPreventDefaultColumnNumber;
125
bool mIsReload;
126
bool mWaitToRespond;
127
128
protected:
129
explicit FetchEvent(EventTarget* aOwner);
130
~FetchEvent();
131
132
public:
133
NS_DECL_ISUPPORTS_INHERITED
134
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FetchEvent, ExtendableEvent)
135
136
virtual JSObject* WrapObjectInternal(
137
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override {
138
return FetchEvent_Binding::Wrap(aCx, this, aGivenProto);
139
}
140
141
void PostInit(
142
nsMainThreadPtrHandle<nsIInterceptedChannel>& aChannel,
143
nsMainThreadPtrHandle<ServiceWorkerRegistrationInfo>& aRegistration,
144
const nsACString& aScriptSpec);
145
146
void PostInit(const nsACString& aScriptSpec,
147
RefPtr<FetchEventOp> aRespondWithHandler);
148
149
static already_AddRefed<FetchEvent> Constructor(
150
const GlobalObject& aGlobal, const nsAString& aType,
151
const FetchEventInit& aOptions);
152
153
bool WaitToRespond() const { return mWaitToRespond; }
154
155
Request* Request_() const {
156
MOZ_ASSERT(mRequest);
157
return mRequest;
158
}
159
160
void GetClientId(nsAString& aClientId) const { aClientId = mClientId; }
161
162
void GetResultingClientId(nsAString& aResultingClientId) const {
163
aResultingClientId = mResultingClientId;
164
}
165
166
bool IsReload() const { return mIsReload; }
167
168
void RespondWith(JSContext* aCx, Promise& aArg, ErrorResult& aRv);
169
170
// Pull in the Event version of PreventDefault so we don't get
171
// shadowing warnings.
172
using Event::PreventDefault;
173
void PreventDefault(JSContext* aCx, CallerType aCallerType) override;
174
175
void ReportCanceled();
176
};
177
178
class PushMessageData final : public nsISupports, public nsWrapperCache {
179
public:
180
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
181
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(PushMessageData)
182
183
virtual JSObject* WrapObject(JSContext* aCx,
184
JS::Handle<JSObject*> aGivenProto) override;
185
186
nsIGlobalObject* GetParentObject() const { return mOwner; }
187
188
void Json(JSContext* cx, JS::MutableHandle<JS::Value> aRetval,
189
ErrorResult& aRv);
190
void Text(nsAString& aData);
191
void ArrayBuffer(JSContext* cx, JS::MutableHandle<JSObject*> aRetval,
192
ErrorResult& aRv);
193
already_AddRefed<mozilla::dom::Blob> Blob(ErrorResult& aRv);
194
195
PushMessageData(nsIGlobalObject* aOwner, nsTArray<uint8_t>&& aBytes);
196
197
private:
198
nsCOMPtr<nsIGlobalObject> mOwner;
199
nsTArray<uint8_t> mBytes;
200
nsString mDecodedText;
201
~PushMessageData();
202
203
nsresult EnsureDecodedText();
204
uint8_t* GetContentsCopy();
205
};
206
207
class PushEvent final : public ExtendableEvent {
208
RefPtr<PushMessageData> mData;
209
210
protected:
211
explicit PushEvent(mozilla::dom::EventTarget* aOwner);
212
~PushEvent() = default;
213
214
public:
215
NS_DECL_ISUPPORTS_INHERITED
216
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PushEvent, ExtendableEvent)
217
218
virtual JSObject* WrapObjectInternal(
219
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
220
221
static already_AddRefed<PushEvent> Constructor(
222
mozilla::dom::EventTarget* aOwner, const nsAString& aType,
223
const PushEventInit& aOptions, ErrorResult& aRv);
224
225
static already_AddRefed<PushEvent> Constructor(const GlobalObject& aGlobal,
226
const nsAString& aType,
227
const PushEventInit& aOptions,
228
ErrorResult& aRv) {
229
nsCOMPtr<EventTarget> owner = do_QueryInterface(aGlobal.GetAsSupports());
230
return Constructor(owner, aType, aOptions, aRv);
231
}
232
233
PushMessageData* GetData() const { return mData; }
234
};
235
236
class ExtendableMessageEvent final : public ExtendableEvent {
237
JS::Heap<JS::Value> mData;
238
nsString mOrigin;
239
nsString mLastEventId;
240
RefPtr<Client> mClient;
241
RefPtr<ServiceWorker> mServiceWorker;
242
RefPtr<MessagePort> mMessagePort;
243
nsTArray<RefPtr<MessagePort>> mPorts;
244
245
protected:
246
explicit ExtendableMessageEvent(EventTarget* aOwner);
247
~ExtendableMessageEvent();
248
249
public:
250
NS_DECL_ISUPPORTS_INHERITED
251
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(ExtendableMessageEvent,
252
ExtendableEvent)
253
254
virtual JSObject* WrapObjectInternal(
255
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override {
256
return mozilla::dom::ExtendableMessageEvent_Binding::Wrap(aCx, this,
257
aGivenProto);
258
}
259
260
static already_AddRefed<ExtendableMessageEvent> Constructor(
261
mozilla::dom::EventTarget* aOwner, const nsAString& aType,
262
const ExtendableMessageEventInit& aOptions);
263
264
static already_AddRefed<ExtendableMessageEvent> Constructor(
265
const GlobalObject& aGlobal, const nsAString& aType,
266
const ExtendableMessageEventInit& aOptions);
267
268
void GetData(JSContext* aCx, JS::MutableHandle<JS::Value> aData,
269
ErrorResult& aRv);
270
271
void GetSource(
272
Nullable<OwningClientOrServiceWorkerOrMessagePort>& aValue) const;
273
274
void GetOrigin(nsAString& aOrigin) const { aOrigin = mOrigin; }
275
276
void GetLastEventId(nsAString& aLastEventId) const {
277
aLastEventId = mLastEventId;
278
}
279
280
void GetPorts(nsTArray<RefPtr<MessagePort>>& aPorts);
281
};
282
283
} // namespace dom
284
} // namespace mozilla
285
286
#endif /* mozilla_dom_serviceworkerevents_h__ */