Bug 1861467 - [wpt-sync] Update web-platform-tests to eedf737ce39c512d0ca3471f988972e...
[gecko.git] / dom / xhr / XMLHttpRequestString.h
blob76e072d78b307c51c66aa2fdcccc93bcdd373351
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_XMLHttpRequestString_h
8 #define mozilla_dom_XMLHttpRequestString_h
10 #include "mozilla/Mutex.h"
11 #include "nsString.h"
13 namespace mozilla::dom {
15 class ArrayBufferBuilder;
16 class BlobImpl;
17 class DOMString;
18 class XMLHttpRequestStringBuffer;
19 class XMLHttpRequestStringSnapshot;
20 class XMLHttpRequestStringWriterHelper;
21 class XMLHttpRequestStringSnapshotReaderHelper;
23 // We want to avoid the dup of strings when XHR in workers has access to
24 // responseText for events dispatched during the loading state. For this reason
25 // we use this class, able to create snapshots of the current size of itself
26 // without making extra copies.
27 class XMLHttpRequestString final {
28 friend class XMLHttpRequestStringWriterHelper;
30 public:
31 XMLHttpRequestString();
32 ~XMLHttpRequestString();
34 void Truncate();
36 uint32_t Length() const;
38 void Append(const nsAString& aString);
40 // This method should be called only when the string is really needed because
41 // it can cause the duplication of the strings in case the loading of the XHR
42 // is not completed yet.
43 [[nodiscard]] bool GetAsString(nsAString& aString) const;
45 size_t SizeOfThis(MallocSizeOf aMallocSizeOf) const;
47 const char16_t* Data() const;
49 bool IsEmpty() const;
51 void CreateSnapshot(XMLHttpRequestStringSnapshot& aSnapshot);
53 private:
54 XMLHttpRequestString(const XMLHttpRequestString&) = delete;
55 XMLHttpRequestString& operator=(const XMLHttpRequestString&) = delete;
56 XMLHttpRequestString& operator=(const XMLHttpRequestString&&) = delete;
58 RefPtr<XMLHttpRequestStringBuffer> mBuffer;
61 // This class locks the buffer and allows the callee to write data into it.
62 class MOZ_STACK_CLASS XMLHttpRequestStringWriterHelper final {
63 public:
64 explicit XMLHttpRequestStringWriterHelper(XMLHttpRequestString& aString);
65 ~XMLHttpRequestStringWriterHelper();
67 /**
68 * The existing length of the string. Do not call during BulkWrite().
70 uint32_t Length() const;
72 mozilla::Result<mozilla::BulkWriteHandle<char16_t>, nsresult> BulkWrite(
73 uint32_t aCapacity);
75 private:
76 XMLHttpRequestStringWriterHelper(const XMLHttpRequestStringWriterHelper&) =
77 delete;
78 XMLHttpRequestStringWriterHelper& operator=(
79 const XMLHttpRequestStringWriterHelper&) = delete;
80 XMLHttpRequestStringWriterHelper& operator=(
81 const XMLHttpRequestStringWriterHelper&&) = delete;
83 RefPtr<XMLHttpRequestStringBuffer> mBuffer;
84 MutexAutoLock mLock;
87 // This class is the internal XMLHttpRequestStringBuffer of the
88 // XMLHttpRequestString plus the current length. GetAsString will return the
89 // string with that particular length also if the XMLHttpRequestStringBuffer is
90 // grown in the meantime.
91 class XMLHttpRequestStringSnapshot final {
92 friend class XMLHttpRequestStringBuffer;
93 friend class XMLHttpRequestStringSnapshotReaderHelper;
95 public:
96 XMLHttpRequestStringSnapshot();
97 ~XMLHttpRequestStringSnapshot();
99 XMLHttpRequestStringSnapshot& operator=(const XMLHttpRequestStringSnapshot&) =
100 delete;
102 void Reset() { ResetInternal(false /* isVoid */); }
104 void SetVoid() { ResetInternal(true /* isVoid */); }
106 bool IsVoid() const { return mVoid; }
108 bool IsEmpty() const { return !mLength; }
110 [[nodiscard]] bool GetAsString(DOMString& aString) const;
112 private:
113 XMLHttpRequestStringSnapshot(const XMLHttpRequestStringSnapshot&) = delete;
114 XMLHttpRequestStringSnapshot& operator=(
115 const XMLHttpRequestStringSnapshot&&) = delete;
117 void Set(XMLHttpRequestStringBuffer* aBuffer, uint32_t aLength);
119 void ResetInternal(bool aIsVoid);
121 RefPtr<XMLHttpRequestStringBuffer> mBuffer;
122 uint32_t mLength;
123 bool mVoid;
126 // This class locks the buffer and allows the callee to read data from it.
127 class MOZ_STACK_CLASS XMLHttpRequestStringSnapshotReaderHelper final {
128 public:
129 explicit XMLHttpRequestStringSnapshotReaderHelper(
130 XMLHttpRequestStringSnapshot& aSnapshot);
131 ~XMLHttpRequestStringSnapshotReaderHelper();
133 const char16_t* Buffer() const;
135 uint32_t Length() const;
137 private:
138 XMLHttpRequestStringSnapshotReaderHelper(
139 const XMLHttpRequestStringSnapshotReaderHelper&) = delete;
140 XMLHttpRequestStringSnapshotReaderHelper& operator=(
141 const XMLHttpRequestStringSnapshotReaderHelper&) = delete;
142 XMLHttpRequestStringSnapshotReaderHelper& operator=(
143 const XMLHttpRequestStringSnapshotReaderHelper&&) = delete;
145 RefPtr<XMLHttpRequestStringBuffer> mBuffer;
146 MutexAutoLock mLock;
149 } // namespace mozilla::dom
151 #endif // mozilla_dom_XMLHttpRequestString_h