summaryrefslogtreecommitdiffstats
path: root/netwerk/base/MemoryDownloader.cpp
blob: f8add31aacce7f7994a92b769bdd618f0f8951a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "MemoryDownloader.h"

#include "mozilla/Assertions.h"
#include "nsIInputStream.h"

namespace mozilla {
namespace net {

NS_IMPL_ISUPPORTS(MemoryDownloader,
		  nsIStreamListener,
		  nsIRequestObserver)

MemoryDownloader::MemoryDownloader(IObserver* aObserver)
: mObserver(aObserver)
{
}

MemoryDownloader::~MemoryDownloader()
{
}

NS_IMETHODIMP
MemoryDownloader::OnStartRequest(nsIRequest* aRequest, nsISupports* aCtxt)
{
  MOZ_ASSERT(!mData);
  mData.reset(new FallibleTArray<uint8_t>());
  mStatus = NS_OK;
  return NS_OK;
}

NS_IMETHODIMP
MemoryDownloader::OnStopRequest(nsIRequest* aRequest,
                                nsISupports* aCtxt,
                                nsresult aStatus)
{
  MOZ_ASSERT_IF(NS_FAILED(mStatus), NS_FAILED(aStatus));
  MOZ_ASSERT(!mData == NS_FAILED(mStatus));
  Data data;
  data.swap(mData);
  RefPtr<IObserver> observer;
  observer.swap(mObserver);
  observer->OnDownloadComplete(this, aRequest, aCtxt, aStatus,
                               mozilla::Move(data));
  return NS_OK;
}

nsresult
MemoryDownloader::ConsumeData(nsIInputStream* aIn,
                              void* aClosure,
                              const char* aFromRawSegment,
                              uint32_t aToOffset,
                              uint32_t aCount,
                              uint32_t* aWriteCount)
{
  MemoryDownloader* self = static_cast<MemoryDownloader*>(aClosure);
  if (!self->mData->AppendElements(aFromRawSegment, aCount, fallible)) {
    // The error returned by ConsumeData isn't propagated to the
    // return of ReadSegments, so it has to be passed as state.
    self->mStatus = NS_ERROR_OUT_OF_MEMORY;
    return NS_ERROR_OUT_OF_MEMORY;
  }
  *aWriteCount = aCount;
  return NS_OK;
}

NS_IMETHODIMP
MemoryDownloader::OnDataAvailable(nsIRequest* aRequest,
                                  nsISupports* aCtxt,
                                  nsIInputStream* aInStr,
                                  uint64_t aSourceOffset,
                                  uint32_t aCount)
{
  uint32_t n;
  MOZ_ASSERT(mData);
  nsresult rv = aInStr->ReadSegments(ConsumeData, this, aCount, &n);
  if (NS_SUCCEEDED(mStatus) && NS_FAILED(rv)) {
    mStatus = rv;
  }
  if (NS_WARN_IF(NS_FAILED(mStatus))) {
    mData.reset(nullptr);
    return mStatus;
  }
  return NS_OK;
}

} // namespace net
} // namespace mozilla