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 #ifndef mozilla_dom_ReadableStreamBYOBReader_h
8 #define mozilla_dom_ReadableStreamBYOBReader_h
9 
10 #include "js/TypeDecls.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/ErrorResult.h"
13 #include "mozilla/dom/BindingDeclarations.h"
14 #include "mozilla/dom/ReadableStreamGenericReader.h"
15 #include "mozilla/dom/ReadIntoRequest.h"
16 #include "mozilla/dom/TypedArray.h"
17 #include "nsCycleCollectionParticipant.h"
18 #include "nsWrapperCache.h"
19 #include "mozilla/LinkedList.h"
20 
21 namespace mozilla::dom {
22 
23 class Promise;
24 class ReadableStream;
25 
26 }  // namespace mozilla::dom
27 
28 namespace mozilla::dom {
29 
30 class ReadableStreamBYOBReader final : public ReadableStreamGenericReader,
31                                        public nsWrapperCache {
32  public:
33   NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(ReadableStreamBYOBReader,ReadableStreamGenericReader)34   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(
35       ReadableStreamBYOBReader, ReadableStreamGenericReader)
36 
37  public:
38   explicit ReadableStreamBYOBReader(nsISupports* aGlobal)
39       : ReadableStreamGenericReader(do_QueryInterface(aGlobal)) {}
40 
IsDefault()41   bool IsDefault() override { return false; };
IsBYOB()42   bool IsBYOB() override { return true; }
AsDefault()43   ReadableStreamDefaultReader* AsDefault() override {
44     MOZ_CRASH("Should have verified IsDefault first");
45     return nullptr;
46   }
AsBYOB()47   ReadableStreamBYOBReader* AsBYOB() override { return this; }
48 
49   JSObject* WrapObject(JSContext* aCx,
50                        JS::Handle<JSObject*> aGivenProto) override;
51 
52   static already_AddRefed<ReadableStreamBYOBReader> Constructor(
53       const GlobalObject& global, ReadableStream& stream, ErrorResult& rv);
54 
55   MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> Read(
56       const ArrayBufferView& aArray, ErrorResult& rv);
57 
58   void ReleaseLock(ErrorResult& rv);
59 
ReadIntoRequests()60   LinkedList<RefPtr<ReadIntoRequest>>& ReadIntoRequests() {
61     return mReadIntoRequests;
62   }
63 
64  private:
65   ~ReadableStreamBYOBReader() override = default;
66 
67   LinkedList<RefPtr<ReadIntoRequest>> mReadIntoRequests = {};
68 };
69 
70 already_AddRefed<ReadableStreamBYOBReader> AcquireReadableStreamBYOBReader(
71     ReadableStream* aStream, ErrorResult& aRv);
72 
73 MOZ_CAN_RUN_SCRIPT void ReadableStreamBYOBReaderRead(
74     JSContext* aCx, ReadableStreamBYOBReader* aReader, JS::HandleObject aView,
75     ReadIntoRequest* aReadIntoRequest, ErrorResult& aRv);
76 
77 void ReadableStreamBYOBReaderErrorReadIntoRequests(
78     JSContext* aCx, ReadableStreamBYOBReader* aReader,
79     JS::Handle<JS::Value> aError, ErrorResult& aRv);
80 
81 void ReadableStreamBYOBReaderRelease(JSContext* aCx,
82                                      ReadableStreamBYOBReader* aReader,
83                                      ErrorResult& aRv);
84 
85 }  // namespace mozilla::dom
86 
87 #endif  // mozilla_dom_ReadableStreamBYOBReader_h
88