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/. */
7 #ifndef mozilla_dom_serviceworkerop_h__
8 #define mozilla_dom_serviceworkerop_h__
12 #include "mozilla/dom/ServiceWorkerOpPromise.h"
13 #include "nsISupportsImpl.h"
15 #include "ServiceWorkerEvents.h"
16 #include "ServiceWorkerOpPromise.h"
17 #include "mozilla/Attributes.h"
18 #include "mozilla/Maybe.h"
19 #include "mozilla/RefPtr.h"
20 #include "mozilla/TimeStamp.h"
21 #include "mozilla/dom/PromiseNativeHandler.h"
22 #include "mozilla/dom/RemoteWorkerChild.h"
23 #include "mozilla/dom/ServiceWorkerOpArgs.h"
24 #include "mozilla/dom/WorkerRunnable.h"
26 namespace mozilla::dom
{
28 class FetchEventOpProxyChild
;
30 class ServiceWorkerOp
: public RemoteWorkerChild::Op
{
32 // `aCallback` will be called when the operation completes or is canceled.
33 static already_AddRefed
<ServiceWorkerOp
> Create(
34 ServiceWorkerOpArgs
&& aArgs
,
35 std::function
<void(const ServiceWorkerOpResult
&)>&& aCallback
);
38 ServiceWorkerOpArgs
&& aArgs
,
39 std::function
<void(const ServiceWorkerOpResult
&)>&& aCallback
);
41 ServiceWorkerOp(const ServiceWorkerOp
&) = delete;
43 ServiceWorkerOp
& operator=(const ServiceWorkerOp
&) = delete;
45 ServiceWorkerOp(ServiceWorkerOp
&&) = default;
47 ServiceWorkerOp
& operator=(ServiceWorkerOp
&&) = default;
49 // Returns `true` if the operation has started and `false` otherwise.
50 bool MaybeStart(RemoteWorkerChild
* aOwner
,
51 RemoteWorkerChild::State
& aState
) final
;
53 void StartOnMainThread(RefPtr
<RemoteWorkerChild
>& aOwner
) final
;
62 bool IsTerminationOp() const;
64 // Override to provide a runnable that's not a `ServiceWorkerOpRunnable.`
65 virtual RefPtr
<WorkerRunnable
> GetRunnable(WorkerPrivate
* aWorkerPrivate
);
67 // Overridden by ServiceWorkerOp subclasses, it should return true when
68 // the ServiceWorkerOp was executed successfully (and false if it did fail).
69 // Content throwing an exception during event dispatch is still considered
71 virtual bool Exec(JSContext
* aCx
, WorkerPrivate
* aWorkerPrivate
) = 0;
73 // Override to reject any additional MozPromises that subclasses may contain.
74 virtual void RejectAll(nsresult aStatus
);
76 ServiceWorkerOpArgs mArgs
;
78 // Subclasses must settle this promise when appropriate.
79 MozPromiseHolder
<ServiceWorkerOpPromise
> mPromiseHolder
;
82 class ServiceWorkerOpRunnable
;
84 bool mStarted
= false;
87 class ExtendableEventOp
: public ServiceWorkerOp
,
88 public ExtendableEventCallback
{
89 using ServiceWorkerOp::ServiceWorkerOp
;
92 ~ExtendableEventOp() = default;
94 void FinishedWithResult(ExtendableEventResult aResult
) override
;
97 class FetchEventOp final
: public ExtendableEventOp
,
98 public PromiseNativeHandler
{
99 using ExtendableEventOp::ExtendableEventOp
;
102 NS_DECL_THREADSAFE_ISUPPORTS
105 * This must be called once and only once before the first call to
106 * `MaybeStart()`; `aActor` will be used for `AsyncLog()` and
107 * `ReportCanceled().`
109 void SetActor(RefPtr
<FetchEventOpProxyChild
> aActor
);
111 void RevokeActor(FetchEventOpProxyChild
* aActor
);
113 // This must be called at most once before the first call to `MaybeStart().`
114 RefPtr
<FetchEventRespondWithPromise
> GetRespondWithPromise();
116 // This must be called when `FetchEvent::RespondWith()` is called.
117 void RespondWithCalledAt(const nsCString
& aRespondWithScriptSpec
,
118 uint32_t aRespondWithLineNumber
,
119 uint32_t aRespondWithColumnNumber
);
121 void ReportCanceled(const nsCString
& aPreventDefaultScriptSpec
,
122 uint32_t aPreventDefaultLineNumber
,
123 uint32_t aPreventDefaultColumnNumber
);
130 bool Exec(JSContext
* aCx
, WorkerPrivate
* aWorkerPrivate
) override
;
132 void RejectAll(nsresult aStatus
) override
;
134 void FinishedWithResult(ExtendableEventResult aResult
) override
;
137 * `{Resolved,Reject}Callback()` are use to handle the
138 * `FetchEvent::RespondWith()` promise.
140 void ResolvedCallback(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
,
141 ErrorResult
& aRv
) override
;
143 void RejectedCallback(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
,
144 ErrorResult
& aRv
) override
;
146 void MaybeFinished();
148 // Requires mRespondWithClosure to be non-empty.
149 void AsyncLog(const nsCString
& aMessageName
, nsTArray
<nsString
> aParams
);
151 void AsyncLog(const nsCString
& aScriptSpec
, uint32_t aLineNumber
,
152 uint32_t aColumnNumber
, const nsCString
& aMessageName
,
153 nsTArray
<nsString
> aParams
);
155 void GetRequestURL(nsAString
& aOutRequestURL
);
157 // A failure code means that the dispatch failed.
158 nsresult
DispatchFetchEvent(JSContext
* aCx
, WorkerPrivate
* aWorkerPrivate
);
160 // Worker Launcher thread only. Used for `AsyncLog().`
161 RefPtr
<FetchEventOpProxyChild
> mActor
;
164 * Created on the Worker Launcher thread and settled on the worker thread.
165 * If this isn't settled before `mPromiseHolder` (which it should be),
166 * `FetchEventOpChild` will cancel the intercepted network request.
168 MozPromiseHolder
<FetchEventRespondWithPromise
> mRespondWithPromiseHolder
;
170 // Worker thread only.
171 Maybe
<ExtendableEventResult
> mResult
;
172 bool mPostDispatchChecksDone
= false;
174 // Worker thread only; set when `FetchEvent::RespondWith()` is called.
175 Maybe
<FetchEventRespondWithClosure
> mRespondWithClosure
;
177 // Must be set to `nullptr` on the worker thread because `Promise`'s
178 // destructor must be called on the worker thread.
179 RefPtr
<Promise
> mHandled
;
181 // Must be set to `nullptr` on the worker thread because `Promise`'s
182 // destructor must be called on the worker thread.
183 RefPtr
<Promise
> mPreloadResponse
;
185 // Holds the callback that resolves mPreloadResponse.
186 MozPromiseRequestHolder
<FetchEventPreloadResponseAvailablePromise
>
187 mPreloadResponseAvailablePromiseRequestHolder
;
188 MozPromiseRequestHolder
<FetchEventPreloadResponseTimingPromise
>
189 mPreloadResponseTimingPromiseRequestHolder
;
190 MozPromiseRequestHolder
<FetchEventPreloadResponseEndPromise
>
191 mPreloadResponseEndPromiseRequestHolder
;
193 TimeStamp mFetchHandlerStart
;
194 TimeStamp mFetchHandlerFinish
;
197 } // namespace mozilla::dom
199 #endif // mozilla_dom_serviceworkerop_h__