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"
16 class ArrayBufferBuilder
;
19 class XMLHttpRequestStringBuffer
;
20 class XMLHttpRequestStringSnapshot
;
21 class XMLHttpRequestStringWriterHelper
;
22 class XMLHttpRequestStringSnapshotReaderHelper
;
24 // We want to avoid the dup of strings when XHR in workers has access to
25 // responseText for events dispatched during the loading state. For this reason
26 // we use this class, able to create snapshots of the current size of itself
27 // without making extra copies.
28 class XMLHttpRequestString final
{
29 friend class XMLHttpRequestStringWriterHelper
;
32 XMLHttpRequestString();
33 ~XMLHttpRequestString();
37 uint32_t Length() const;
39 void Append(const nsAString
& aString
);
41 // This method should be called only when the string is really needed because
42 // it can cause the duplication of the strings in case the loading of the XHR
43 // is not completed yet.
44 [[nodiscard
]] bool GetAsString(nsAString
& aString
) const;
46 size_t SizeOfThis(MallocSizeOf aMallocSizeOf
) const;
48 const char16_t
* Data() const;
52 void CreateSnapshot(XMLHttpRequestStringSnapshot
& aSnapshot
);
55 XMLHttpRequestString(const XMLHttpRequestString
&) = delete;
56 XMLHttpRequestString
& operator=(const XMLHttpRequestString
&) = delete;
57 XMLHttpRequestString
& operator=(const XMLHttpRequestString
&&) = delete;
59 RefPtr
<XMLHttpRequestStringBuffer
> mBuffer
;
62 // This class locks the buffer and allows the callee to write data into it.
63 class MOZ_STACK_CLASS XMLHttpRequestStringWriterHelper final
{
65 explicit XMLHttpRequestStringWriterHelper(XMLHttpRequestString
& aString
);
66 ~XMLHttpRequestStringWriterHelper();
69 * The existing length of the string. Do not call during BulkWrite().
71 uint32_t Length() const;
73 mozilla::Result
<mozilla::BulkWriteHandle
<char16_t
>, nsresult
> BulkWrite(
77 XMLHttpRequestStringWriterHelper(const XMLHttpRequestStringWriterHelper
&) =
79 XMLHttpRequestStringWriterHelper
& operator=(
80 const XMLHttpRequestStringWriterHelper
&) = delete;
81 XMLHttpRequestStringWriterHelper
& operator=(
82 const XMLHttpRequestStringWriterHelper
&&) = delete;
84 RefPtr
<XMLHttpRequestStringBuffer
> mBuffer
;
88 // This class is the internal XMLHttpRequestStringBuffer of the
89 // XMLHttpRequestString plus the current length. GetAsString will return the
90 // string with that particular length also if the XMLHttpRequestStringBuffer is
91 // grown in the meantime.
92 class XMLHttpRequestStringSnapshot final
{
93 friend class XMLHttpRequestStringBuffer
;
94 friend class XMLHttpRequestStringSnapshotReaderHelper
;
97 XMLHttpRequestStringSnapshot();
98 ~XMLHttpRequestStringSnapshot();
100 XMLHttpRequestStringSnapshot
& operator=(const XMLHttpRequestStringSnapshot
&) =
103 void Reset() { ResetInternal(false /* isVoid */); }
105 void SetVoid() { ResetInternal(true /* isVoid */); }
107 bool IsVoid() const { return mVoid
; }
109 bool IsEmpty() const { return !mLength
; }
111 [[nodiscard
]] bool GetAsString(DOMString
& aString
) const;
114 XMLHttpRequestStringSnapshot(const XMLHttpRequestStringSnapshot
&) = delete;
115 XMLHttpRequestStringSnapshot
& operator=(
116 const XMLHttpRequestStringSnapshot
&&) = delete;
118 void Set(XMLHttpRequestStringBuffer
* aBuffer
, uint32_t aLength
);
120 void ResetInternal(bool aIsVoid
);
122 RefPtr
<XMLHttpRequestStringBuffer
> mBuffer
;
127 // This class locks the buffer and allows the callee to read data from it.
128 class MOZ_STACK_CLASS XMLHttpRequestStringSnapshotReaderHelper final
{
130 explicit XMLHttpRequestStringSnapshotReaderHelper(
131 XMLHttpRequestStringSnapshot
& aSnapshot
);
132 ~XMLHttpRequestStringSnapshotReaderHelper();
134 const char16_t
* Buffer() const;
136 uint32_t Length() const;
139 XMLHttpRequestStringSnapshotReaderHelper(
140 const XMLHttpRequestStringSnapshotReaderHelper
&) = delete;
141 XMLHttpRequestStringSnapshotReaderHelper
& operator=(
142 const XMLHttpRequestStringSnapshotReaderHelper
&) = delete;
143 XMLHttpRequestStringSnapshotReaderHelper
& operator=(
144 const XMLHttpRequestStringSnapshotReaderHelper
&&) = delete;
146 RefPtr
<XMLHttpRequestStringBuffer
> mBuffer
;
151 } // namespace mozilla
153 #endif // mozilla_dom_XMLHttpRequestString_h