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"
18 #include "nsIGlobalObject.h"
19 #include "nsWrapperCache.h"
21 namespace mozilla::dom
{
23 using namespace streams_abstract
;
25 ReadableStreamBYOBRequest::ReadableStreamBYOBRequest(nsIGlobalObject
* 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
),
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
)
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 {
58 // https://streams.spec.whatwg.org/#rs-byob-request-respond
59 void ReadableStreamBYOBRequest::Respond(JSContext
* aCx
, uint64_t bytesWritten
,
63 aRv
.ThrowTypeError("Undefined Controller");
69 JS::Rooted
<JSObject
*> view(aCx
, mView
);
70 JS::Rooted
<JSObject
*> arrayBuffer(
71 aCx
, JS_GetArrayBufferViewBuffer(aCx
, view
, &isSharedMemory
));
73 aRv
.StealExceptionFromJSContext(aCx
);
77 if (JS::IsDetachedArrayBufferObject(arrayBuffer
)) {
78 aRv
.ThrowTypeError("View of Detached buffer");
83 MOZ_ASSERT(JS_GetArrayBufferViewByteLength(view
) > 0);
86 MOZ_ASSERT(JS::GetArrayBufferByteLength(arrayBuffer
) > 0);
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
,
99 aRv
.ThrowTypeError("Undefined Controller");
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
);
113 if (JS::IsDetachedArrayBufferObject(viewedArrayBuffer
)) {
114 aRv
.ThrowTypeError("View of Detached Array Buffer");
119 RefPtr
<ReadableByteStreamController
> controller(mController
);
120 ReadableByteStreamControllerRespondWithNewView(aCx
, controller
, rootedViewObj
,
124 void ReadableStreamBYOBRequest::SetController(
125 ReadableByteStreamController
* aController
) {
126 mController
= aController
;
129 } // namespace mozilla::dom