Bug 1768570 [wpt PR 34013] - Update wpt metadata, a=testonly
[gecko.git] / dom / streams / UnderlyingSourceCallbackHelpers.cpp
blobfc0dfcda601347fdfa509868069407e42287823c
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 #include "mozilla/dom/ReadableStreamDefaultController.h"
8 #include "mozilla/dom/UnderlyingSourceCallbackHelpers.h"
9 #include "mozilla/dom/UnderlyingSourceBinding.h"
11 namespace mozilla::dom {
13 // UnderlyingSourceAlgorithmsBase
14 NS_IMPL_CYCLE_COLLECTION(UnderlyingSourceAlgorithmsBase)
15 NS_IMPL_CYCLE_COLLECTING_ADDREF(UnderlyingSourceAlgorithmsBase)
16 NS_IMPL_CYCLE_COLLECTING_RELEASE(UnderlyingSourceAlgorithmsBase)
17 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(UnderlyingSourceAlgorithmsBase)
18 NS_INTERFACE_MAP_ENTRY(nsISupports)
19 NS_INTERFACE_MAP_END
20 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(UnderlyingSourceAlgorithmsBase)
21 NS_IMPL_CYCLE_COLLECTION_TRACE_END
23 NS_IMPL_CYCLE_COLLECTION_INHERITED_WITH_JS_MEMBERS(
24 UnderlyingSourceAlgorithms, UnderlyingSourceAlgorithmsBase,
25 (mGlobal, mStartCallback, mPullCallback, mCancelCallback),
26 (mUnderlyingSource))
27 NS_IMPL_ADDREF_INHERITED(UnderlyingSourceAlgorithms,
28 UnderlyingSourceAlgorithmsBase)
29 NS_IMPL_RELEASE_INHERITED(UnderlyingSourceAlgorithms,
30 UnderlyingSourceAlgorithmsBase)
31 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(UnderlyingSourceAlgorithms)
32 NS_INTERFACE_MAP_END_INHERITING(UnderlyingSourceAlgorithmsBase)
34 // https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller-from-underlying-source
35 void UnderlyingSourceAlgorithms::StartCallback(
36 JSContext* aCx, ReadableStreamController& aController,
37 JS::MutableHandle<JS::Value> aRetVal, ErrorResult& aRv) {
38 if (!mStartCallback) {
39 // Step 2: Let startAlgorithm be an algorithm that returns undefined.
40 aRetVal.setUndefined();
41 return;
44 // Step 5: If underlyingSourceDict["start"] exists, then set startAlgorithm to
45 // an algorithm which returns the result of invoking
46 // underlyingSourceDict["start"] with argument list « controller » and
47 // callback this value underlyingSource.
48 JS::RootedObject thisObj(aCx, mUnderlyingSource);
49 ReadableStreamDefaultControllerOrReadableByteStreamController controller;
50 if (aController.IsDefault()) {
51 controller.SetAsReadableStreamDefaultController() = aController.AsDefault();
52 } else {
53 controller.SetAsReadableByteStreamController() = aController.AsByte();
56 return mStartCallback->Call(thisObj, controller, aRetVal, aRv,
57 "UnderlyingSource.start",
58 CallbackFunction::eRethrowExceptions);
61 // https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller-from-underlying-source
62 already_AddRefed<Promise> UnderlyingSourceAlgorithms::PullCallback(
63 JSContext* aCx, ReadableStreamController& aController, ErrorResult& aRv) {
64 JS::RootedObject thisObj(aCx, mUnderlyingSource);
65 if (!mPullCallback) {
66 // Step 3: Let pullAlgorithm be an algorithm that returns a promise resolved
67 // with undefined.
68 return Promise::CreateResolvedWithUndefined(mGlobal, aRv);
71 // Step 6: If underlyingSourceDict["pull"] exists, then set pullAlgorithm to
72 // an algorithm which returns the result of invoking
73 // underlyingSourceDict["pull"] with argument list « controller » and callback
74 // this value underlyingSource.
75 ReadableStreamDefaultControllerOrReadableByteStreamController controller;
76 if (aController.IsDefault()) {
77 controller.SetAsReadableStreamDefaultController() = aController.AsDefault();
78 } else {
79 controller.SetAsReadableByteStreamController() = aController.AsByte();
82 RefPtr<Promise> promise =
83 mPullCallback->Call(thisObj, controller, aRv, "UnderlyingSource.pull",
84 CallbackFunction::eRethrowExceptions);
86 return promise.forget();
89 // https://streams.spec.whatwg.org/#set-up-readable-stream-default-controller-from-underlying-source
90 already_AddRefed<Promise> UnderlyingSourceAlgorithms::CancelCallback(
91 JSContext* aCx, const Optional<JS::Handle<JS::Value>>& aReason,
92 ErrorResult& aRv) {
93 if (!mCancelCallback) {
94 // Step 4: Let cancelAlgorithm be an algorithm that returns a promise
95 // resolved with undefined.
96 return Promise::CreateResolvedWithUndefined(mGlobal, aRv);
99 // Step 7: If underlyingSourceDict["cancel"] exists, then set cancelAlgorithm
100 // to an algorithm which takes an argument reason and returns the result of
101 // invoking underlyingSourceDict["cancel"] with argument list « reason » and
102 // callback this value underlyingSource.
103 JS::RootedObject thisObj(aCx, mUnderlyingSource);
104 RefPtr<Promise> promise =
105 mCancelCallback->Call(thisObj, aReason, aRv, "UnderlyingSource.cancel",
106 CallbackFunction::eRethrowExceptions);
108 return promise.forget();
111 } // namespace mozilla::dom