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