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 namespace mozilla::dom
{
18 class ArrayBufferBuilder
;
21 class XMLHttpRequestStringBuffer
;
22 class XMLHttpRequestStringSnapshot
;
23 class XMLHttpRequestStringWriterHelper
;
25 // We want to avoid the dup of strings when XHR in workers has access to
26 // responseText for events dispatched during the loading state. For this reason
27 // we use this class, able to create snapshots of the current size of itself
28 // without making extra copies.
29 class XMLHttpRequestString final
{
30 friend class XMLHttpRequestStringWriterHelper
;
33 XMLHttpRequestString();
34 ~XMLHttpRequestString();
38 uint32_t Length() const;
40 void Append(const nsAString
& aString
);
42 // This method should be called only when the string is really needed because
43 // it can cause the duplication of the strings in case the loading of the XHR
44 // is not completed yet.
45 [[nodiscard
]] bool GetAsString(nsAString
& aString
) const;
47 size_t SizeOfThis(MallocSizeOf aMallocSizeOf
) const;
49 const char16_t
* Data() const;
53 void CreateSnapshot(XMLHttpRequestStringSnapshot
& aSnapshot
);
56 XMLHttpRequestString(const XMLHttpRequestString
&) = delete;
57 XMLHttpRequestString
& operator=(const XMLHttpRequestString
&) = delete;
58 XMLHttpRequestString
& operator=(const XMLHttpRequestString
&&) = delete;
60 RefPtr
<XMLHttpRequestStringBuffer
> mBuffer
;
63 // This class locks the buffer and allows the callee to write data into it.
64 class MOZ_STACK_CLASS XMLHttpRequestStringWriterHelper final
{
66 explicit XMLHttpRequestStringWriterHelper(XMLHttpRequestString
& aString
);
67 ~XMLHttpRequestStringWriterHelper();
70 * The existing length of the string. Do not call during BulkWrite().
72 uint32_t Length() const;
74 mozilla::Result
<mozilla::BulkWriteHandle
<char16_t
>, nsresult
> BulkWrite(
78 XMLHttpRequestStringWriterHelper(const XMLHttpRequestStringWriterHelper
&) =
80 XMLHttpRequestStringWriterHelper
& operator=(
81 const XMLHttpRequestStringWriterHelper
&) = delete;
82 XMLHttpRequestStringWriterHelper
& operator=(
83 const XMLHttpRequestStringWriterHelper
&&) = delete;
85 RefPtr
<XMLHttpRequestStringBuffer
> mBuffer
;
89 // This class is the internal XMLHttpRequestStringBuffer of the
90 // XMLHttpRequestString plus the current length. GetAsString will return the
91 // string with that particular length also if the XMLHttpRequestStringBuffer is
92 // grown in the meantime.
93 class XMLHttpRequestStringSnapshot final
{
94 friend class XMLHttpRequestStringBuffer
;
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;
113 JSString
* GetAsJSStringCopy(JSContext
* aCx
) const;
116 XMLHttpRequestStringSnapshot(const XMLHttpRequestStringSnapshot
&) = delete;
117 XMLHttpRequestStringSnapshot
& operator=(
118 const XMLHttpRequestStringSnapshot
&&) = delete;
120 void Set(XMLHttpRequestStringBuffer
* aBuffer
, uint32_t aLength
);
122 void ResetInternal(bool aIsVoid
);
124 RefPtr
<XMLHttpRequestStringBuffer
> mBuffer
;
129 } // namespace mozilla::dom
131 #endif // mozilla_dom_XMLHttpRequestString_h