1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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_UnderlyingSourceCallbackHelpers_h
8 #define mozilla_dom_UnderlyingSourceCallbackHelpers_h
10 #include "mozilla/HoldDropJSObjects.h"
11 #include "mozilla/dom/Promise.h"
12 #include "mozilla/dom/UnderlyingSourceBinding.h"
13 #include "nsISupports.h"
14 #include "nsISupportsImpl.h"
16 /* Since the streams specification has native descriptions of some callbacks
17 * (i.e. described in prose, rather than provided by user code), we need to be
18 * able to pass around native callbacks. To handle this, we define polymorphic
19 * classes That cover the difference between native callback and user-provided.
21 * The Streams specification wants us to invoke these callbacks, run through
22 * WebIDL as if they were methods. So we have to preserve the underlying object
23 * to use as the This value on invocation.
25 namespace mozilla::dom
{
27 class BodyStreamHolder
;
28 class ReadableStreamController
;
30 class UnderlyingSourceAlgorithmsBase
: public nsISupports
{
32 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
33 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(UnderlyingSourceAlgorithmsBase
)
35 MOZ_CAN_RUN_SCRIPT
virtual void StartCallback(
36 JSContext
* aCx
, ReadableStreamController
& aController
,
37 JS::MutableHandle
<JS::Value
> aRetVal
, ErrorResult
& aRv
) = 0;
39 // A promise-returning algorithm that pulls data from the underlying byte
41 MOZ_CAN_RUN_SCRIPT
virtual already_AddRefed
<Promise
> PullCallback(
42 JSContext
* aCx
, ReadableStreamController
& aController
,
43 ErrorResult
& aRv
) = 0;
45 // A promise-returning algorithm, taking one argument (the cancel reason),
46 // which communicates a requested cancelation to the underlying byte source
47 MOZ_CAN_RUN_SCRIPT
virtual already_AddRefed
<Promise
> CancelCallback(
48 JSContext
* aCx
, const Optional
<JS::Handle
<JS::Value
>>& aReason
,
49 ErrorResult
& aRv
) = 0;
51 // (Not in the spec) Callback called when erroring a stream.
52 virtual void ErrorCallback() = 0;
55 virtual ~UnderlyingSourceAlgorithmsBase() = default;
58 class UnderlyingSourceAlgorithms final
: public UnderlyingSourceAlgorithmsBase
{
60 NS_DECL_ISUPPORTS_INHERITED
61 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(
62 UnderlyingSourceAlgorithms
, UnderlyingSourceAlgorithmsBase
)
64 UnderlyingSourceAlgorithms(nsIGlobalObject
* aGlobal
,
65 JS::Handle
<JSObject
*> aUnderlyingSource
,
66 UnderlyingSource
& aUnderlyingSourceDict
)
67 : mGlobal(aGlobal
), mUnderlyingSource(aUnderlyingSource
) {
68 // Step 6. (implicit Step 2.)
69 if (aUnderlyingSourceDict
.mStart
.WasPassed()) {
70 mStartCallback
= aUnderlyingSourceDict
.mStart
.Value();
73 // Step 7. (implicit Step 3.)
74 if (aUnderlyingSourceDict
.mPull
.WasPassed()) {
75 mPullCallback
= aUnderlyingSourceDict
.mPull
.Value();
78 // Step 8. (implicit Step 4.)
79 if (aUnderlyingSourceDict
.mCancel
.WasPassed()) {
80 mCancelCallback
= aUnderlyingSourceDict
.mCancel
.Value();
83 mozilla::HoldJSObjects(this);
86 MOZ_CAN_RUN_SCRIPT
void StartCallback(JSContext
* aCx
,
87 ReadableStreamController
& aController
,
88 JS::MutableHandle
<JS::Value
> aRetVal
,
89 ErrorResult
& aRv
) override
;
91 MOZ_CAN_RUN_SCRIPT already_AddRefed
<Promise
> PullCallback(
92 JSContext
* aCx
, ReadableStreamController
& aController
,
93 ErrorResult
& aRv
) override
;
95 MOZ_CAN_RUN_SCRIPT already_AddRefed
<Promise
> CancelCallback(
96 JSContext
* aCx
, const Optional
<JS::Handle
<JS::Value
>>& aReason
,
97 ErrorResult
& aRv
) override
;
99 void ErrorCallback() override
{}
102 ~UnderlyingSourceAlgorithms() override
{ mozilla::DropJSObjects(this); };
105 // Virtually const, but are cycle collected
106 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
107 JS::Heap
<JSObject
*> mUnderlyingSource
;
108 MOZ_KNOWN_LIVE RefPtr
<UnderlyingSourceStartCallback
> mStartCallback
;
109 MOZ_KNOWN_LIVE RefPtr
<UnderlyingSourcePullCallback
> mPullCallback
;
110 MOZ_KNOWN_LIVE RefPtr
<UnderlyingSourceCancelCallback
> mCancelCallback
;
113 } // namespace mozilla::dom