/src/mozilla-central/netwerk/base/MemoryDownloader.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
5 | | |
6 | | #include "MemoryDownloader.h" |
7 | | |
8 | | #include "mozilla/Assertions.h" |
9 | | #include "nsIInputStream.h" |
10 | | |
11 | | namespace mozilla { |
12 | | namespace net { |
13 | | |
14 | | NS_IMPL_ISUPPORTS(MemoryDownloader, |
15 | | nsIStreamListener, |
16 | | nsIRequestObserver) |
17 | | |
18 | | MemoryDownloader::MemoryDownloader(IObserver* aObserver) |
19 | | : mObserver(aObserver) |
20 | | , mStatus(NS_ERROR_NOT_INITIALIZED) |
21 | 0 | { |
22 | 0 | } |
23 | | |
24 | | NS_IMETHODIMP |
25 | | MemoryDownloader::OnStartRequest(nsIRequest* aRequest, nsISupports* aCtxt) |
26 | 0 | { |
27 | 0 | MOZ_ASSERT(!mData); |
28 | 0 | mData.reset(new FallibleTArray<uint8_t>()); |
29 | 0 | mStatus = NS_OK; |
30 | 0 | return NS_OK; |
31 | 0 | } |
32 | | |
33 | | NS_IMETHODIMP |
34 | | MemoryDownloader::OnStopRequest(nsIRequest* aRequest, |
35 | | nsISupports* aCtxt, |
36 | | nsresult aStatus) |
37 | 0 | { |
38 | 0 | MOZ_ASSERT_IF(NS_FAILED(mStatus), NS_FAILED(aStatus)); |
39 | 0 | MOZ_ASSERT(!mData == NS_FAILED(mStatus)); |
40 | 0 | Data data; |
41 | 0 | data.swap(mData); |
42 | 0 | RefPtr<IObserver> observer; |
43 | 0 | observer.swap(mObserver); |
44 | 0 | observer->OnDownloadComplete(this, aRequest, aCtxt, aStatus, |
45 | 0 | std::move(data)); |
46 | 0 | return NS_OK; |
47 | 0 | } |
48 | | |
49 | | nsresult |
50 | | MemoryDownloader::ConsumeData(nsIInputStream* aIn, |
51 | | void* aClosure, |
52 | | const char* aFromRawSegment, |
53 | | uint32_t aToOffset, |
54 | | uint32_t aCount, |
55 | | uint32_t* aWriteCount) |
56 | 0 | { |
57 | 0 | MemoryDownloader* self = static_cast<MemoryDownloader*>(aClosure); |
58 | 0 | if (!self->mData->AppendElements(aFromRawSegment, aCount, fallible)) { |
59 | 0 | // The error returned by ConsumeData isn't propagated to the |
60 | 0 | // return of ReadSegments, so it has to be passed as state. |
61 | 0 | self->mStatus = NS_ERROR_OUT_OF_MEMORY; |
62 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
63 | 0 | } |
64 | 0 | *aWriteCount = aCount; |
65 | 0 | return NS_OK; |
66 | 0 | } |
67 | | |
68 | | NS_IMETHODIMP |
69 | | MemoryDownloader::OnDataAvailable(nsIRequest* aRequest, |
70 | | nsISupports* aCtxt, |
71 | | nsIInputStream* aInStr, |
72 | | uint64_t aSourceOffset, |
73 | | uint32_t aCount) |
74 | 0 | { |
75 | 0 | uint32_t n; |
76 | 0 | MOZ_ASSERT(mData); |
77 | 0 | nsresult rv = aInStr->ReadSegments(ConsumeData, this, aCount, &n); |
78 | 0 | if (NS_SUCCEEDED(mStatus) && NS_FAILED(rv)) { |
79 | 0 | mStatus = rv; |
80 | 0 | } |
81 | 0 | if (NS_WARN_IF(NS_FAILED(mStatus))) { |
82 | 0 | mData.reset(nullptr); |
83 | 0 | return mStatus; |
84 | 0 | } |
85 | 0 | return NS_OK; |
86 | 0 | } |
87 | | |
88 | | } // namespace net |
89 | | } // namespace mozilla |