Bug 1526591 - Remove devtools.inspector.shapesHighlighter.enabled pref. r=rcaliman
[gecko.git] / netwerk / base / MemoryDownloader.cpp
blobad55feb277e32926cb82de9b5a12c489619eaa8e
1 /* -*- Mode: C++; 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/. */
6 #include "MemoryDownloader.h"
8 #include "mozilla/Assertions.h"
9 #include "nsIInputStream.h"
11 namespace mozilla {
12 namespace net {
14 NS_IMPL_ISUPPORTS(MemoryDownloader, nsIStreamListener, nsIRequestObserver)
16 MemoryDownloader::MemoryDownloader(IObserver* aObserver)
17 : mObserver(aObserver), mStatus(NS_ERROR_NOT_INITIALIZED) {}
19 NS_IMETHODIMP
20 MemoryDownloader::OnStartRequest(nsIRequest* aRequest, nsISupports* aCtxt) {
21 MOZ_ASSERT(!mData);
22 mData.reset(new FallibleTArray<uint8_t>());
23 mStatus = NS_OK;
24 return NS_OK;
27 NS_IMETHODIMP
28 MemoryDownloader::OnStopRequest(nsIRequest* aRequest, nsISupports* aCtxt,
29 nsresult aStatus) {
30 MOZ_ASSERT_IF(NS_FAILED(mStatus), NS_FAILED(aStatus));
31 MOZ_ASSERT(!mData == NS_FAILED(mStatus));
32 Data data;
33 data.swap(mData);
34 RefPtr<IObserver> observer;
35 observer.swap(mObserver);
36 observer->OnDownloadComplete(this, aRequest, aCtxt, aStatus, std::move(data));
37 return NS_OK;
40 nsresult MemoryDownloader::ConsumeData(nsIInputStream* aIn, void* aClosure,
41 const char* aFromRawSegment,
42 uint32_t aToOffset, uint32_t aCount,
43 uint32_t* aWriteCount) {
44 MemoryDownloader* self = static_cast<MemoryDownloader*>(aClosure);
45 if (!self->mData->AppendElements(aFromRawSegment, aCount, fallible)) {
46 // The error returned by ConsumeData isn't propagated to the
47 // return of ReadSegments, so it has to be passed as state.
48 self->mStatus = NS_ERROR_OUT_OF_MEMORY;
49 return NS_ERROR_OUT_OF_MEMORY;
51 *aWriteCount = aCount;
52 return NS_OK;
55 NS_IMETHODIMP
56 MemoryDownloader::OnDataAvailable(nsIRequest* aRequest, nsISupports* aCtxt,
57 nsIInputStream* aInStr,
58 uint64_t aSourceOffset, uint32_t aCount) {
59 uint32_t n;
60 MOZ_ASSERT(mData);
61 nsresult rv = aInStr->ReadSegments(ConsumeData, this, aCount, &n);
62 if (NS_SUCCEEDED(mStatus) && NS_FAILED(rv)) {
63 mStatus = rv;
65 if (NS_WARN_IF(NS_FAILED(mStatus))) {
66 mData.reset(nullptr);
67 return mStatus;
69 return NS_OK;
72 } // namespace net
73 } // namespace mozilla