XMLHttpRequestString.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "XMLHttpRequestString.h"
  6. #include "mozilla/Mutex.h"
  7. #include "nsISupportsImpl.h"
  8. #include "mozilla/dom/DOMString.h"
  9. namespace mozilla {
  10. namespace dom {
  11. // ---------------------------------------------------------------------------
  12. // XMLHttpRequestString
  13. XMLHttpRequestString::XMLHttpRequestString()
  14. : mBuffer(new XMLHttpRequestStringBuffer())
  15. {
  16. }
  17. XMLHttpRequestString::~XMLHttpRequestString()
  18. {
  19. }
  20. void
  21. XMLHttpRequestString::Truncate()
  22. {
  23. mBuffer = new XMLHttpRequestStringBuffer();
  24. }
  25. uint32_t
  26. XMLHttpRequestString::Length() const
  27. {
  28. return mBuffer->Length();
  29. }
  30. void
  31. XMLHttpRequestString::Append(const nsAString& aString)
  32. {
  33. mBuffer->Append(aString);
  34. }
  35. bool
  36. XMLHttpRequestString::GetAsString(nsAString& aString) const
  37. {
  38. return mBuffer->GetAsString(aString);
  39. }
  40. size_t
  41. XMLHttpRequestString::SizeOfThis(MallocSizeOf aMallocSizeOf) const
  42. {
  43. return mBuffer->SizeOfThis(aMallocSizeOf);
  44. }
  45. bool
  46. XMLHttpRequestString::IsEmpty() const
  47. {
  48. return !mBuffer->Length();
  49. }
  50. void
  51. XMLHttpRequestString::CreateSnapshot(XMLHttpRequestStringSnapshot& aSnapshot)
  52. {
  53. mBuffer->CreateSnapshot(aSnapshot);
  54. }
  55. // ---------------------------------------------------------------------------
  56. // XMLHttpRequestStringSnapshot
  57. XMLHttpRequestStringSnapshot::XMLHttpRequestStringSnapshot()
  58. : mLength(0)
  59. , mVoid(false)
  60. {
  61. }
  62. XMLHttpRequestStringSnapshot::~XMLHttpRequestStringSnapshot()
  63. {
  64. }
  65. XMLHttpRequestStringSnapshot&
  66. XMLHttpRequestStringSnapshot::operator=(const XMLHttpRequestStringSnapshot& aOther)
  67. {
  68. mBuffer = aOther.mBuffer;
  69. mLength = aOther.mLength;
  70. mVoid = aOther.mVoid;
  71. return *this;
  72. }
  73. void
  74. XMLHttpRequestStringSnapshot::ResetInternal(bool aIsVoid)
  75. {
  76. mBuffer = nullptr;
  77. mLength = 0;
  78. mVoid = aIsVoid;
  79. }
  80. void
  81. XMLHttpRequestStringSnapshot::Set(XMLHttpRequestStringBuffer* aBuffer,
  82. uint32_t aLength)
  83. {
  84. MOZ_ASSERT(aBuffer);
  85. MOZ_ASSERT(aLength <= aBuffer->UnsafeLength());
  86. mBuffer = aBuffer;
  87. mLength = aLength;
  88. mVoid = false;
  89. }
  90. bool
  91. XMLHttpRequestStringSnapshot::GetAsString(DOMString& aString) const
  92. {
  93. if (mBuffer) {
  94. MOZ_ASSERT(!mVoid);
  95. return mBuffer->GetAsString(aString, mLength);
  96. }
  97. if (mVoid) {
  98. aString.SetNull();
  99. }
  100. return true;
  101. }
  102. // ---------------------------------------------------------------------------
  103. // XMLHttpRequestStringWriterHelper
  104. XMLHttpRequestStringWriterHelper::XMLHttpRequestStringWriterHelper(XMLHttpRequestString& aString)
  105. : mBuffer(aString.mBuffer)
  106. , mLock(aString.mBuffer->mMutex)
  107. {
  108. }
  109. bool
  110. XMLHttpRequestStringWriterHelper::AddCapacity(int32_t aCapacity)
  111. {
  112. return mBuffer->UnsafeData().SetCapacity(mBuffer->UnsafeLength() + aCapacity, fallible);
  113. }
  114. char16_t*
  115. XMLHttpRequestStringWriterHelper::EndOfExistingData()
  116. {
  117. return mBuffer->UnsafeData().BeginWriting() + mBuffer->UnsafeLength();
  118. }
  119. void
  120. XMLHttpRequestStringWriterHelper::AddLength(int32_t aLength)
  121. {
  122. mBuffer->UnsafeData().SetLength(mBuffer->UnsafeLength() + aLength);
  123. }
  124. // ---------------------------------------------------------------------------
  125. // XMLHttpRequestStringReaderHelper
  126. XMLHttpRequestStringSnapshotReaderHelper::XMLHttpRequestStringSnapshotReaderHelper(XMLHttpRequestStringSnapshot& aSnapshot)
  127. : mBuffer(aSnapshot.mBuffer)
  128. , mLock(aSnapshot.mBuffer->mMutex)
  129. {
  130. }
  131. const char16_t*
  132. XMLHttpRequestStringSnapshotReaderHelper::Buffer() const
  133. {
  134. return mBuffer->UnsafeData().BeginReading();
  135. }
  136. uint32_t
  137. XMLHttpRequestStringSnapshotReaderHelper::Length() const
  138. {
  139. return mBuffer->UnsafeLength();
  140. }
  141. } // dom namespace
  142. } // mozilla namespace