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"
13 namespace mozilla::dom
{
15 class ArrayBufferBuilder
;
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
;
31 XMLHttpRequestString();
32 ~XMLHttpRequestString();
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;
51 void CreateSnapshot(XMLHttpRequestStringSnapshot
& aSnapshot
);
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
{
64 explicit XMLHttpRequestStringWriterHelper(XMLHttpRequestString
& aString
);
65 ~XMLHttpRequestStringWriterHelper();
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(
76 XMLHttpRequestStringWriterHelper(const XMLHttpRequestStringWriterHelper
&) =
78 XMLHttpRequestStringWriterHelper
& operator=(
79 const XMLHttpRequestStringWriterHelper
&) = delete;
80 XMLHttpRequestStringWriterHelper
& operator=(
81 const XMLHttpRequestStringWriterHelper
&&) = delete;
83 RefPtr
<XMLHttpRequestStringBuffer
> mBuffer
;
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
;
96 XMLHttpRequestStringSnapshot();
97 ~XMLHttpRequestStringSnapshot();
99 XMLHttpRequestStringSnapshot
& operator=(const XMLHttpRequestStringSnapshot
&) =
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;
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
;
126 // This class locks the buffer and allows the callee to read data from it.
127 class MOZ_STACK_CLASS XMLHttpRequestStringSnapshotReaderHelper final
{
129 explicit XMLHttpRequestStringSnapshotReaderHelper(
130 XMLHttpRequestStringSnapshot
& aSnapshot
);
131 ~XMLHttpRequestStringSnapshotReaderHelper();
133 const char16_t
* Buffer() const;
135 uint32_t Length() const;
138 XMLHttpRequestStringSnapshotReaderHelper(
139 const XMLHttpRequestStringSnapshotReaderHelper
&) = delete;
140 XMLHttpRequestStringSnapshotReaderHelper
& operator=(
141 const XMLHttpRequestStringSnapshotReaderHelper
&) = delete;
142 XMLHttpRequestStringSnapshotReaderHelper
& operator=(
143 const XMLHttpRequestStringSnapshotReaderHelper
&&) = delete;
145 RefPtr
<XMLHttpRequestStringBuffer
> mBuffer
;
149 } // namespace mozilla::dom
151 #endif // mozilla_dom_XMLHttpRequestString_h