Bug 1708422: part 14) Extend documentation of `mozInlineSpellChecker::SpellCheckerTim...
[gecko.git] / dom / xhr / XMLHttpRequestString.h
blobac85a8da775d91482968ee58c445b6f8e4485a21
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 {
14 namespace dom {
16 class ArrayBufferBuilder;
17 class BlobImpl;
18 class DOMString;
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;
31 public:
32 XMLHttpRequestString();
33 ~XMLHttpRequestString();
35 void Truncate();
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;
50 bool IsEmpty() const;
52 void CreateSnapshot(XMLHttpRequestStringSnapshot& aSnapshot);
54 private:
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 {
64 public:
65 explicit XMLHttpRequestStringWriterHelper(XMLHttpRequestString& aString);
66 ~XMLHttpRequestStringWriterHelper();
68 /**
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(
74 uint32_t aCapacity);
76 private:
77 XMLHttpRequestStringWriterHelper(const XMLHttpRequestStringWriterHelper&) =
78 delete;
79 XMLHttpRequestStringWriterHelper& operator=(
80 const XMLHttpRequestStringWriterHelper&) = delete;
81 XMLHttpRequestStringWriterHelper& operator=(
82 const XMLHttpRequestStringWriterHelper&&) = delete;
84 RefPtr<XMLHttpRequestStringBuffer> mBuffer;
85 MutexAutoLock mLock;
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;
96 public:
97 XMLHttpRequestStringSnapshot();
98 ~XMLHttpRequestStringSnapshot();
100 XMLHttpRequestStringSnapshot& operator=(const XMLHttpRequestStringSnapshot&) =
101 delete;
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 private:
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;
123 uint32_t mLength;
124 bool mVoid;
127 // This class locks the buffer and allows the callee to read data from it.
128 class MOZ_STACK_CLASS XMLHttpRequestStringSnapshotReaderHelper final {
129 public:
130 explicit XMLHttpRequestStringSnapshotReaderHelper(
131 XMLHttpRequestStringSnapshot& aSnapshot);
132 ~XMLHttpRequestStringSnapshotReaderHelper();
134 const char16_t* Buffer() const;
136 uint32_t Length() const;
138 private:
139 XMLHttpRequestStringSnapshotReaderHelper(
140 const XMLHttpRequestStringSnapshotReaderHelper&) = delete;
141 XMLHttpRequestStringSnapshotReaderHelper& operator=(
142 const XMLHttpRequestStringSnapshotReaderHelper&) = delete;
143 XMLHttpRequestStringSnapshotReaderHelper& operator=(
144 const XMLHttpRequestStringSnapshotReaderHelper&&) = delete;
146 RefPtr<XMLHttpRequestStringBuffer> mBuffer;
147 MutexAutoLock mLock;
150 } // namespace dom
151 } // namespace mozilla
153 #endif // mozilla_dom_XMLHttpRequestString_h