Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / dom / base / BodyConsumer.h
blob459f2ce9407119cd02b697e8756d105bb92d3847
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 #ifndef mozilla_dom_BodyConsumer_h
8 #define mozilla_dom_BodyConsumer_h
10 #include "mozilla/dom/AbortSignal.h"
11 #include "mozilla/dom/MutableBlobStorage.h"
12 #include "nsIInputStreamPump.h"
13 #include "nsIObserver.h"
14 #include "nsWeakReference.h"
16 class nsIThread;
18 namespace mozilla::dom {
20 class Promise;
21 class ThreadSafeWorkerRef;
23 // In order to keep alive the object all the time, we use a ThreadSafeWorkerRef,
24 // if created on workers.
25 class BodyConsumer final : public nsIObserver,
26 public nsSupportsWeakReference,
27 public AbortFollower {
28 public:
29 NS_DECL_THREADSAFE_ISUPPORTS
30 NS_DECL_NSIOBSERVER
32 enum ConsumeType {
33 CONSUME_ARRAYBUFFER,
34 CONSUME_BLOB,
35 CONSUME_FORMDATA,
36 CONSUME_JSON,
37 CONSUME_TEXT,
40 /**
41 * Returns a promise which will be resolved when the body is completely
42 * consumed and converted to the wanted type (See ConsumeType).
44 * @param aGlobal the global to construct the Promise.
45 * @param aMainThreadEventTarget the main-thread event target. The reading
46 * needs to start on the main-thread because of nsIInputStreamPump.
47 * @param aBodyStream the stream to read.
48 * @param aSignalImpl an AbortSignal object. Optional.
49 * @param aType the consume type.
50 * @param aBodyBlobURISpec this is used only if the consume type is
51 * CONSUME_BLOB. Optional.
52 * @param aBodyLocalPath local path in case the blob is created from a local
53 * file. Used only by CONSUME_BLOB. Optional.
54 * @param aBodyMimeType the mime-type for blob. Used only by CONSUME_BLOB.
55 * Optional.
56 * @param aMixedCaseMimeType is needed to get mixed case multipart
57 * boundary value to FormDataParser.
58 * @param aBlobStorageType Blobs can be saved in temporary file. This is the
59 * type of blob storage to use. Used only by CONSUME_BLOB.
60 * @param aRv An ErrorResult.
62 static already_AddRefed<Promise> Create(
63 nsIGlobalObject* aGlobal, nsISerialEventTarget* aMainThreadEventTarget,
64 nsIInputStream* aBodyStream, AbortSignalImpl* aSignalImpl,
65 ConsumeType aType, const nsACString& aBodyBlobURISpec,
66 const nsAString& aBodyLocalPath, const nsACString& aBodyMimeType,
67 const nsACString& aMixedCaseMimeType,
68 MutableBlobStorage::MutableBlobStorageType aBlobStorageType,
69 ErrorResult& aRv);
71 void ReleaseObject();
73 void BeginConsumeBodyMainThread(ThreadSafeWorkerRef* aWorkerRef);
75 void OnBlobResult(BlobImpl* aBlobImpl,
76 ThreadSafeWorkerRef* aWorkerRef = nullptr);
78 void ContinueConsumeBody(nsresult aStatus, uint32_t aLength, uint8_t* aResult,
79 bool aShuttingDown = false);
81 void ContinueConsumeBlobBody(BlobImpl* aBlobImpl, bool aShuttingDown = false);
83 void DispatchContinueConsumeBlobBody(BlobImpl* aBlobImpl,
84 ThreadSafeWorkerRef* aWorkerRef);
86 void ShutDownMainThreadConsuming();
88 void NullifyConsumeBodyPump() {
89 mShuttingDown = true;
90 mConsumeBodyPump = nullptr;
93 // AbortFollower
94 void RunAbortAlgorithm() override;
96 private:
97 BodyConsumer(nsISerialEventTarget* aMainThreadEventTarget,
98 nsIGlobalObject* aGlobalObject, nsIInputStream* aBodyStream,
99 Promise* aPromise, ConsumeType aType,
100 const nsACString& aBodyBlobURISpec,
101 const nsAString& aBodyLocalPath, const nsACString& aBodyMimeType,
102 const nsACString& aMixedCaseMimeType,
103 MutableBlobStorage::MutableBlobStorageType aBlobStorageType);
105 ~BodyConsumer();
107 nsresult GetBodyLocalFile(nsIFile** aFile) const;
109 void AssertIsOnTargetThread() const;
111 nsCOMPtr<nsIThread> mTargetThread;
112 nsCOMPtr<nsISerialEventTarget> mMainThreadEventTarget;
114 // This is nullified when the consuming of the body starts.
115 nsCOMPtr<nsIInputStream> mBodyStream;
117 MutableBlobStorage::MutableBlobStorageType mBlobStorageType;
118 nsCString mBodyMimeType;
119 nsCString mMixedCaseMimeType;
121 nsCString mBodyBlobURISpec;
122 nsString mBodyLocalPath;
124 nsCOMPtr<nsIGlobalObject> mGlobal;
126 // Touched on the main-thread only.
127 nsCOMPtr<nsIInputStreamPump> mConsumeBodyPump;
129 // Only ever set once, always on target thread.
130 ConsumeType mConsumeType;
131 RefPtr<Promise> mConsumePromise;
133 // touched only on the target thread.
134 bool mBodyConsumed;
136 // touched only on the main-thread.
137 bool mShuttingDown;
140 } // namespace mozilla::dom
142 #endif // mozilla_dom_BodyConsumer_h