nsIncrementalStreamLoader.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* -*- Mode: C++; tab-width: 2; 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 "nsIncrementalStreamLoader.h"
  6. #include "nsIInputStream.h"
  7. #include "nsIChannel.h"
  8. #include "nsError.h"
  9. #include "GeckoProfiler.h"
  10. #include <limits>
  11. nsIncrementalStreamLoader::nsIncrementalStreamLoader() = default;
  12. nsIncrementalStreamLoader::~nsIncrementalStreamLoader()
  13. {
  14. }
  15. NS_IMETHODIMP
  16. nsIncrementalStreamLoader::Init(nsIIncrementalStreamLoaderObserver* observer)
  17. {
  18. NS_ENSURE_ARG_POINTER(observer);
  19. mObserver = observer;
  20. return NS_OK;
  21. }
  22. nsresult
  23. nsIncrementalStreamLoader::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
  24. {
  25. if (aOuter) return NS_ERROR_NO_AGGREGATION;
  26. nsIncrementalStreamLoader* it = new nsIncrementalStreamLoader();
  27. if (it == nullptr)
  28. return NS_ERROR_OUT_OF_MEMORY;
  29. NS_ADDREF(it);
  30. nsresult rv = it->QueryInterface(aIID, aResult);
  31. NS_RELEASE(it);
  32. return rv;
  33. }
  34. NS_IMPL_ISUPPORTS(nsIncrementalStreamLoader, nsIIncrementalStreamLoader,
  35. nsIRequestObserver, nsIStreamListener,
  36. nsIThreadRetargetableStreamListener)
  37. NS_IMETHODIMP
  38. nsIncrementalStreamLoader::GetNumBytesRead(uint32_t* aNumBytes)
  39. {
  40. *aNumBytes = mBytesRead;
  41. return NS_OK;
  42. }
  43. /* readonly attribute nsIRequest request; */
  44. NS_IMETHODIMP
  45. nsIncrementalStreamLoader::GetRequest(nsIRequest **aRequest)
  46. {
  47. NS_IF_ADDREF(*aRequest = mRequest);
  48. return NS_OK;
  49. }
  50. NS_IMETHODIMP
  51. nsIncrementalStreamLoader::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
  52. {
  53. nsCOMPtr<nsIChannel> chan( do_QueryInterface(request) );
  54. if (chan) {
  55. int64_t contentLength = -1;
  56. chan->GetContentLength(&contentLength);
  57. if (contentLength >= 0) {
  58. if (uint64_t(contentLength) > std::numeric_limits<size_t>::max()) {
  59. // Too big to fit into size_t, so let's bail.
  60. return NS_ERROR_OUT_OF_MEMORY;
  61. }
  62. // preallocate buffer
  63. if (!mData.initCapacity(contentLength)) {
  64. return NS_ERROR_OUT_OF_MEMORY;
  65. }
  66. }
  67. }
  68. mContext = ctxt;
  69. return NS_OK;
  70. }
  71. NS_IMETHODIMP
  72. nsIncrementalStreamLoader::OnStopRequest(nsIRequest* request, nsISupports *ctxt,
  73. nsresult aStatus)
  74. {
  75. PROFILER_LABEL("nsIncrementalStreamLoader", "OnStopRequest",
  76. js::ProfileEntry::Category::NETWORK);
  77. if (mObserver) {
  78. // provide nsIIncrementalStreamLoader::request during call to OnStreamComplete
  79. mRequest = request;
  80. size_t length = mData.length();
  81. uint8_t* elems = mData.extractOrCopyRawBuffer();
  82. nsresult rv = mObserver->OnStreamComplete(this, mContext, aStatus,
  83. length, elems);
  84. if (rv != NS_SUCCESS_ADOPTED_DATA) {
  85. // The observer didn't take ownership of the extracted data buffer, so
  86. // put it back into mData.
  87. mData.replaceRawBuffer(elems, length);
  88. }
  89. // done.. cleanup
  90. ReleaseData();
  91. mRequest = nullptr;
  92. mObserver = nullptr;
  93. mContext = nullptr;
  94. }
  95. return NS_OK;
  96. }
  97. nsresult
  98. nsIncrementalStreamLoader::WriteSegmentFun(nsIInputStream *inStr,
  99. void *closure,
  100. const char *fromSegment,
  101. uint32_t toOffset,
  102. uint32_t count,
  103. uint32_t *writeCount)
  104. {
  105. nsIncrementalStreamLoader *self = (nsIncrementalStreamLoader *) closure;
  106. const uint8_t *data = reinterpret_cast<const uint8_t *>(fromSegment);
  107. uint32_t consumedCount = 0;
  108. nsresult rv;
  109. if (self->mData.empty()) {
  110. // Shortcut when observer wants to keep the listener's buffer empty.
  111. rv = self->mObserver->OnIncrementalData(self, self->mContext,
  112. count, data, &consumedCount);
  113. if (rv != NS_OK) {
  114. return rv;
  115. }
  116. if (consumedCount > count) {
  117. return NS_ERROR_INVALID_ARG;
  118. }
  119. if (consumedCount < count) {
  120. if (!self->mData.append(fromSegment + consumedCount,
  121. count - consumedCount)) {
  122. self->mData.clearAndFree();
  123. return NS_ERROR_OUT_OF_MEMORY;
  124. }
  125. }
  126. } else {
  127. // We have some non-consumed data from previous OnIncrementalData call,
  128. // appending new data and reporting combined data.
  129. if (!self->mData.append(fromSegment, count)) {
  130. self->mData.clearAndFree();
  131. return NS_ERROR_OUT_OF_MEMORY;
  132. }
  133. size_t length = self->mData.length();
  134. uint32_t reportCount = length > UINT32_MAX ? UINT32_MAX : (uint32_t)length;
  135. uint8_t* elems = self->mData.extractOrCopyRawBuffer();
  136. rv = self->mObserver->OnIncrementalData(self, self->mContext,
  137. reportCount, elems, &consumedCount);
  138. // We still own elems, freeing its memory when exiting scope.
  139. if (rv != NS_OK) {
  140. free(elems);
  141. return rv;
  142. }
  143. if (consumedCount > reportCount) {
  144. free(elems);
  145. return NS_ERROR_INVALID_ARG;
  146. }
  147. if (consumedCount == length) {
  148. free(elems); // good case -- fully consumed data
  149. } else {
  150. // Adopting elems back (at least its portion).
  151. self->mData.replaceRawBuffer(elems, length);
  152. if (consumedCount > 0) {
  153. self->mData.erase(self->mData.begin() + consumedCount);
  154. }
  155. }
  156. }
  157. *writeCount = count;
  158. return NS_OK;
  159. }
  160. NS_IMETHODIMP
  161. nsIncrementalStreamLoader::OnDataAvailable(nsIRequest* request, nsISupports *ctxt,
  162. nsIInputStream *inStr,
  163. uint64_t sourceOffset, uint32_t count)
  164. {
  165. if (mObserver) {
  166. // provide nsIIncrementalStreamLoader::request during call to OnStreamComplete
  167. mRequest = request;
  168. }
  169. uint32_t countRead;
  170. nsresult rv = inStr->ReadSegments(WriteSegmentFun, this, count, &countRead);
  171. mRequest = nullptr;
  172. NS_ENSURE_SUCCESS(rv, rv);
  173. mBytesRead += countRead;
  174. return rv;
  175. }
  176. void
  177. nsIncrementalStreamLoader::ReleaseData()
  178. {
  179. mData.clearAndFree();
  180. }
  181. NS_IMETHODIMP
  182. nsIncrementalStreamLoader::CheckListenerChain()
  183. {
  184. return NS_OK;
  185. }