summaryrefslogtreecommitdiffstats
path: root/dom/streams/ReadableStreamBYOBRequest.cpp
blob: 316b76484f24e6312dcd73a1de98d7fadc341442 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/ReadableStreamBYOBRequest.h"

#include "mozilla/dom/ByteStreamHelpers.h"
#include "js/ArrayBuffer.h"
#include "js/TypeDecls.h"
#include "mozilla/dom/ReadableByteStreamController.h"
#include "mozilla/dom/ReadableStream.h"
#include "mozilla/dom/ReadableStreamBYOBRequestBinding.h"
#include "js/experimental/TypedData.h"
#include "mozilla/dom/ReadableStreamController.h"
#include "nsCOMPtr.h"
#include "nsIGlobalObject.h"
#include "nsWrapperCache.h"

namespace mozilla::dom {

using namespace streams_abstract;

ReadableStreamBYOBRequest::ReadableStreamBYOBRequest(nsIGlobalObject* aGlobal)
    : mGlobal(aGlobal) {
  mozilla::HoldJSObjects(this);
}

ReadableStreamBYOBRequest::~ReadableStreamBYOBRequest() {
  mozilla::DropJSObjects(this);
}

NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS(ReadableStreamBYOBRequest,
                                                      (mGlobal, mController),
                                                      (mView))

NS_IMPL_CYCLE_COLLECTING_ADDREF(ReadableStreamBYOBRequest)
NS_IMPL_CYCLE_COLLECTING_RELEASE(ReadableStreamBYOBRequest)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ReadableStreamBYOBRequest)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

JSObject* ReadableStreamBYOBRequest::WrapObject(
    JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
  return ReadableStreamBYOBRequest_Binding::Wrap(aCx, this, aGivenProto);
}

// https://streams.spec.whatwg.org/#rs-byob-request-view
void ReadableStreamBYOBRequest::GetView(
    JSContext* cx, JS::MutableHandle<JSObject*> aRetVal) const {
  // Step 1.
  aRetVal.set(mView);
}

// https://streams.spec.whatwg.org/#rs-byob-request-respond
void ReadableStreamBYOBRequest::Respond(JSContext* aCx, uint64_t bytesWritten,
                                        ErrorResult& aRv) {
  // Step 1.
  if (!mController) {
    aRv.ThrowTypeError("Undefined Controller");
    return;
  }

  // Step 2.
  bool isSharedMemory;
  JS::Rooted<JSObject*> view(aCx, mView);
  JS::Rooted<JSObject*> arrayBuffer(
      aCx, JS_GetArrayBufferViewBuffer(aCx, view, &isSharedMemory));
  if (!arrayBuffer) {
    aRv.StealExceptionFromJSContext(aCx);
    return;
  }

  if (JS::IsDetachedArrayBufferObject(arrayBuffer)) {
    aRv.ThrowTypeError("View of Detached buffer");
    return;
  }

  // Step 3.
  MOZ_ASSERT(JS_GetArrayBufferViewByteLength(view) > 0);

  // Step 4.
  MOZ_ASSERT(JS::GetArrayBufferByteLength(arrayBuffer) > 0);

  // Step 5.
  RefPtr<ReadableByteStreamController> controller(mController);
  ReadableByteStreamControllerRespond(aCx, controller, bytesWritten, aRv);
}

// https://streams.spec.whatwg.org/#rs-byob-request-respond-with-new-view
void ReadableStreamBYOBRequest::RespondWithNewView(JSContext* aCx,
                                                   const ArrayBufferView& view,
                                                   ErrorResult& aRv) {
  // Step 1.
  if (!mController) {
    aRv.ThrowTypeError("Undefined Controller");
    return;
  }

  // Step 2.
  bool isSharedMemory;
  JS::Rooted<JSObject*> rootedViewObj(aCx, view.Obj());
  JS::Rooted<JSObject*> viewedArrayBuffer(
      aCx, JS_GetArrayBufferViewBuffer(aCx, rootedViewObj, &isSharedMemory));
  if (!viewedArrayBuffer) {
    aRv.StealExceptionFromJSContext(aCx);
    return;
  }

  if (JS::IsDetachedArrayBufferObject(viewedArrayBuffer)) {
    aRv.ThrowTypeError("View of Detatched Array Buffer");
    return;
  }

  // Step 3.
  RefPtr<ReadableByteStreamController> controller(mController);
  ReadableByteStreamControllerRespondWithNewView(aCx, controller, rootedViewObj,
                                                 aRv);
}

void ReadableStreamBYOBRequest::SetController(
    ReadableByteStreamController* aController) {
  mController = aController;
}

}  // namespace mozilla::dom