ReadableStreamBYOBRequest.cpp (4173B)
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/. */ 6 7 #include "mozilla/dom/ReadableStreamBYOBRequest.h" 8 9 #include "js/ArrayBuffer.h" 10 #include "js/TypeDecls.h" 11 #include "js/experimental/TypedData.h" 12 #include "mozilla/dom/ByteStreamHelpers.h" 13 #include "mozilla/dom/ReadableByteStreamController.h" 14 #include "mozilla/dom/ReadableStream.h" 15 #include "mozilla/dom/ReadableStreamBYOBRequestBinding.h" 16 #include "mozilla/dom/ReadableStreamControllerBase.h" 17 #include "nsCOMPtr.h" 18 #include "nsIGlobalObject.h" 19 #include "nsWrapperCache.h" 20 21 namespace mozilla::dom { 22 23 using namespace streams_abstract; 24 25 ReadableStreamBYOBRequest::ReadableStreamBYOBRequest(nsIGlobalObject* aGlobal) 26 : mGlobal(aGlobal) { 27 mozilla::HoldJSObjects(this); 28 } 29 30 ReadableStreamBYOBRequest::~ReadableStreamBYOBRequest() { 31 mozilla::DropJSObjects(this); 32 } 33 34 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS(ReadableStreamBYOBRequest, 35 (mGlobal, mController), 36 (mView)) 37 38 NS_IMPL_CYCLE_COLLECTING_ADDREF(ReadableStreamBYOBRequest) 39 NS_IMPL_CYCLE_COLLECTING_RELEASE(ReadableStreamBYOBRequest) 40 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 45 46 JSObject* ReadableStreamBYOBRequest::WrapObject( 47 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) { 48 return ReadableStreamBYOBRequest_Binding::Wrap(aCx, this, aGivenProto); 49 } 50 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); 56 } 57 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; 65 } 66 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; 75 } 76 77 if (JS::IsDetachedArrayBufferObject(arrayBuffer)) { 78 aRv.ThrowTypeError("View of Detached buffer"); 79 return; 80 } 81 82 // Step 3. 83 MOZ_ASSERT(JS_GetArrayBufferViewByteLength(view) > 0); 84 85 // Step 4. 86 MOZ_ASSERT(JS::GetArrayBufferByteLength(arrayBuffer) > 0); 87 88 // Step 5. 89 RefPtr<ReadableByteStreamController> controller(mController); 90 ReadableByteStreamControllerRespond(aCx, controller, bytesWritten, aRv); 91 } 92 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; 101 } 102 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; 111 } 112 113 if (JS::IsDetachedArrayBufferObject(viewedArrayBuffer)) { 114 aRv.ThrowTypeError("View of Detached Array Buffer"); 115 return; 116 } 117 118 // Step 3. 119 RefPtr<ReadableByteStreamController> controller(mController); 120 ReadableByteStreamControllerRespondWithNewView(aCx, controller, rootedViewObj, 121 aRv); 122 } 123 124 void ReadableStreamBYOBRequest::SetController( 125 ReadableByteStreamController* aController) { 126 mController = aController; 127 } 128 129 } // namespace mozilla::dom