Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / streams / ReadableStreamBYOBRequest.cpp
blob655b91483082e811d8c608596951e0b444348358
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/ReadableStreamBYOBRequest.h"
9 #include "mozilla/dom/ByteStreamHelpers.h"
10 #include "js/ArrayBuffer.h"
11 #include "js/TypeDecls.h"
12 #include "mozilla/dom/ReadableByteStreamController.h"
13 #include "mozilla/dom/ReadableStream.h"
14 #include "mozilla/dom/ReadableStreamBYOBRequestBinding.h"
15 #include "js/experimental/TypedData.h"
16 #include "mozilla/dom/ReadableStreamController.h"
17 #include "nsCOMPtr.h"
18 #include "nsIGlobalObject.h"
19 #include "nsWrapperCache.h"
21 namespace mozilla::dom {
23 using namespace streams_abstract;
25 ReadableStreamBYOBRequest::ReadableStreamBYOBRequest(nsIGlobalObject* aGlobal)
26 : mGlobal(aGlobal) {
27 mozilla::HoldJSObjects(this);
30 ReadableStreamBYOBRequest::~ReadableStreamBYOBRequest() {
31 mozilla::DropJSObjects(this);
34 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS(ReadableStreamBYOBRequest,
35 (mGlobal, mController),
36 (mView))
38 NS_IMPL_CYCLE_COLLECTING_ADDREF(ReadableStreamBYOBRequest)
39 NS_IMPL_CYCLE_COLLECTING_RELEASE(ReadableStreamBYOBRequest)
41 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ReadableStreamBYOBRequest)
42 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
43 NS_INTERFACE_MAP_ENTRY(nsISupports)
44 NS_INTERFACE_MAP_END
46 JSObject* ReadableStreamBYOBRequest::WrapObject(
47 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
48 return ReadableStreamBYOBRequest_Binding::Wrap(aCx, this, aGivenProto);
51 // https://streams.spec.whatwg.org/#rs-byob-request-view
52 void ReadableStreamBYOBRequest::GetView(
53 JSContext* cx, JS::MutableHandle<JSObject*> aRetVal) const {
54 // Step 1.
55 aRetVal.set(mView);
58 // https://streams.spec.whatwg.org/#rs-byob-request-respond
59 void ReadableStreamBYOBRequest::Respond(JSContext* aCx, uint64_t bytesWritten,
60 ErrorResult& aRv) {
61 // Step 1.
62 if (!mController) {
63 aRv.ThrowTypeError("Undefined Controller");
64 return;
67 // Step 2.
68 bool isSharedMemory;
69 JS::Rooted<JSObject*> view(aCx, mView);
70 JS::Rooted<JSObject*> arrayBuffer(
71 aCx, JS_GetArrayBufferViewBuffer(aCx, view, &isSharedMemory));
72 if (!arrayBuffer) {
73 aRv.StealExceptionFromJSContext(aCx);
74 return;
77 if (JS::IsDetachedArrayBufferObject(arrayBuffer)) {
78 aRv.ThrowTypeError("View of Detached buffer");
79 return;
82 // Step 3.
83 MOZ_ASSERT(JS_GetArrayBufferViewByteLength(view) > 0);
85 // Step 4.
86 MOZ_ASSERT(JS::GetArrayBufferByteLength(arrayBuffer) > 0);
88 // Step 5.
89 RefPtr<ReadableByteStreamController> controller(mController);
90 ReadableByteStreamControllerRespond(aCx, controller, bytesWritten, aRv);
93 // https://streams.spec.whatwg.org/#rs-byob-request-respond-with-new-view
94 void ReadableStreamBYOBRequest::RespondWithNewView(JSContext* aCx,
95 const ArrayBufferView& view,
96 ErrorResult& aRv) {
97 // Step 1.
98 if (!mController) {
99 aRv.ThrowTypeError("Undefined Controller");
100 return;
103 // Step 2.
104 bool isSharedMemory;
105 JS::Rooted<JSObject*> rootedViewObj(aCx, view.Obj());
106 JS::Rooted<JSObject*> viewedArrayBuffer(
107 aCx, JS_GetArrayBufferViewBuffer(aCx, rootedViewObj, &isSharedMemory));
108 if (!viewedArrayBuffer) {
109 aRv.StealExceptionFromJSContext(aCx);
110 return;
113 if (JS::IsDetachedArrayBufferObject(viewedArrayBuffer)) {
114 aRv.ThrowTypeError("View of Detached Array Buffer");
115 return;
118 // Step 3.
119 RefPtr<ReadableByteStreamController> controller(mController);
120 ReadableByteStreamControllerRespondWithNewView(aCx, controller, rootedViewObj,
121 aRv);
124 void ReadableStreamBYOBRequest::SetController(
125 ReadableByteStreamController* aController) {
126 mController = aController;
129 } // namespace mozilla::dom