Bug 1806483 - Enable TSAN cppunittests by default. r=jmaher
[gecko.git] / dom / streams / ReadableStreamBYOBRequest.cpp
blob77d2ba658b90663c852758f8c43ac2ed2f047fb6
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 ReadableStreamBYOBRequest::ReadableStreamBYOBRequest(nsIGlobalObject* aGlobal)
24 : mGlobal(aGlobal) {
25 mozilla::HoldJSObjects(this);
28 ReadableStreamBYOBRequest::~ReadableStreamBYOBRequest() {
29 mozilla::DropJSObjects(this);
32 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS(ReadableStreamBYOBRequest,
33 (mGlobal, mController),
34 (mView))
36 NS_IMPL_CYCLE_COLLECTING_ADDREF(ReadableStreamBYOBRequest)
37 NS_IMPL_CYCLE_COLLECTING_RELEASE(ReadableStreamBYOBRequest)
39 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ReadableStreamBYOBRequest)
40 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
41 NS_INTERFACE_MAP_ENTRY(nsISupports)
42 NS_INTERFACE_MAP_END
44 JSObject* ReadableStreamBYOBRequest::WrapObject(
45 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
46 return ReadableStreamBYOBRequest_Binding::Wrap(aCx, this, aGivenProto);
49 // https://streams.spec.whatwg.org/#rs-byob-request-view
50 void ReadableStreamBYOBRequest::GetView(
51 JSContext* cx, JS::MutableHandle<JSObject*> aRetVal) const {
52 // Step 1.
53 aRetVal.set(mView);
56 // https://streams.spec.whatwg.org/#rs-byob-request-respond
57 void ReadableStreamBYOBRequest::Respond(JSContext* aCx, uint64_t bytesWritten,
58 ErrorResult& aRv) {
59 // Step 1.
60 if (!mController) {
61 aRv.ThrowTypeError("Undefined Controller");
62 return;
65 // Step 2.
66 bool isSharedMemory;
67 JS::Rooted<JSObject*> view(aCx, mView);
68 JS::Rooted<JSObject*> arrayBuffer(
69 aCx, JS_GetArrayBufferViewBuffer(aCx, view, &isSharedMemory));
70 if (!arrayBuffer) {
71 aRv.StealExceptionFromJSContext(aCx);
72 return;
75 if (JS::IsDetachedArrayBufferObject(arrayBuffer)) {
76 aRv.ThrowTypeError("View of Detached buffer");
77 return;
80 // Step 3.
81 MOZ_ASSERT(JS_GetArrayBufferViewByteLength(view) > 0);
83 // Step 4.
84 MOZ_ASSERT(JS::GetArrayBufferByteLength(arrayBuffer) > 0);
86 // Step 5.
87 RefPtr<ReadableByteStreamController> controller(mController);
88 ReadableByteStreamControllerRespond(aCx, controller, bytesWritten, aRv);
91 // https://streams.spec.whatwg.org/#rs-byob-request-respond-with-new-view
92 void ReadableStreamBYOBRequest::RespondWithNewView(JSContext* aCx,
93 const ArrayBufferView& view,
94 ErrorResult& aRv) {
95 // Step 1.
96 if (!mController) {
97 aRv.ThrowTypeError("Undefined Controller");
98 return;
101 // Step 2.
102 bool isSharedMemory;
103 JS::Rooted<JSObject*> rootedViewObj(aCx, view.Obj());
104 JS::Rooted<JSObject*> viewedArrayBuffer(
105 aCx, JS_GetArrayBufferViewBuffer(aCx, rootedViewObj, &isSharedMemory));
106 if (!viewedArrayBuffer) {
107 aRv.StealExceptionFromJSContext(aCx);
108 return;
111 if (JS::IsDetachedArrayBufferObject(viewedArrayBuffer)) {
112 aRv.ThrowTypeError("View of Detatched Array Buffer");
113 return;
116 // Step 3.
117 RefPtr<ReadableByteStreamController> controller(mController);
118 ReadableByteStreamControllerRespondWithNewView(aCx, controller, rootedViewObj,
119 aRv);
122 void ReadableStreamBYOBRequest::SetController(
123 ReadableByteStreamController* aController) {
124 mController = aController;
127 } // namespace mozilla::dom