diff options
author | wolfbeast <mcwerewolf@wolfbeast.com> | 2020-04-26 12:56:46 +0200 |
---|---|---|
committer | wolfbeast <mcwerewolf@wolfbeast.com> | 2020-04-26 12:56:46 +0200 |
commit | 98fe8b0ccbea37c2681ad8c788baa91554871f4d (patch) | |
tree | 0e269a27661fea619ab07a533e0c1fa195c3c671 /dom/xhr/XMLHttpRequestString.h | |
parent | e08a4ad1c8096fc8de64fac4f36cf0c569cb4420 (diff) | |
parent | 519030e688dd7075a7cd9f14dd6fb9e210b5a689 (diff) | |
download | UXP-98fe8b0ccbea37c2681ad8c788baa91554871f4d.tar UXP-98fe8b0ccbea37c2681ad8c788baa91554871f4d.tar.gz UXP-98fe8b0ccbea37c2681ad8c788baa91554871f4d.tar.lz UXP-98fe8b0ccbea37c2681ad8c788baa91554871f4d.tar.xz UXP-98fe8b0ccbea37c2681ad8c788baa91554871f4d.zip |
Merge branch 'redwood' into 28.9-platformRELBASE_20200426
Diffstat (limited to 'dom/xhr/XMLHttpRequestString.h')
-rw-r--r-- | dom/xhr/XMLHttpRequestString.h | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/dom/xhr/XMLHttpRequestString.h b/dom/xhr/XMLHttpRequestString.h index b6b12483f..5709c1aa2 100644 --- a/dom/xhr/XMLHttpRequestString.h +++ b/dom/xhr/XMLHttpRequestString.h @@ -7,6 +7,9 @@ #ifndef mozilla_dom_XMLHttpRequestString_h #define mozilla_dom_XMLHttpRequestString_h +#include "mozilla/Mutex.h" +#include "mozilla/RefPtr.h" +#include "mozilla/dom/DOMString.h" #include "nsString.h" namespace mozilla { @@ -155,6 +158,98 @@ private: MutexAutoLock mLock; }; +class XMLHttpRequestStringBuffer final +{ + friend class XMLHttpRequestStringWriterHelper; + friend class XMLHttpRequestStringSnapshotReaderHelper; + +public: + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(XMLHttpRequestStringBuffer) + NS_DECL_OWNINGTHREAD + + XMLHttpRequestStringBuffer() + : mMutex("XMLHttpRequestStringBuffer::mMutex") + { + } + + uint32_t + Length() + { + MutexAutoLock lock(mMutex); + return mData.Length(); + } + + uint32_t + UnsafeLength() const + { + return mData.Length(); + } + + void + Append(const nsAString& aString) + { + NS_ASSERT_OWNINGTHREAD(XMLHttpRequestStringBuffer); + + MutexAutoLock lock(mMutex); + mData.Append(aString); + } + + MOZ_MUST_USE bool + GetAsString(nsAString& aString) + { + MutexAutoLock lock(mMutex); + return aString.Assign(mData, mozilla::fallible); + } + + size_t + SizeOfThis(MallocSizeOf aMallocSizeOf) const + { + return mData.SizeOfExcludingThisIfUnshared(aMallocSizeOf); + } + + MOZ_MUST_USE bool + GetAsString(DOMString& aString, uint32_t aLength) + { + MutexAutoLock lock(mMutex); + MOZ_ASSERT(aLength <= mData.Length()); + nsStringBuffer* buf = nsStringBuffer::FromString(mData); + if (buf) { + // We have to use SetEphemeralStringBuffer, because once we release our + // mutex mData can get mutated from some other thread while the DOMString + // is still alive. + aString.SetEphemeralStringBuffer(buf, aLength); + return true; + } + + // We can get here if mData is empty. In that case it won't have an + // nsStringBuffer.... + MOZ_ASSERT(mData.IsEmpty()); + return aString.AsAString().Assign(mData.BeginReading(), aLength, + mozilla::fallible); + } + + void + CreateSnapshot(XMLHttpRequestStringSnapshot& aSnapshot) + { + MutexAutoLock lock(mMutex); + aSnapshot.Set(this, mData.Length()); + } + +private: + ~XMLHttpRequestStringBuffer() + {} + + nsString& UnsafeData() + { + return mData; + } + + Mutex mMutex; + + // The following member variable is protected by mutex. + nsString mData; +}; + } // dom namespace } // mozilla namespace |