Source code

Revision control

Other Tools

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
#ifndef mozilla_net_MemoryDownloader_h__
7
#define mozilla_net_MemoryDownloader_h__
8
9
#include "mozilla/UniquePtr.h"
10
#include "nsCOMPtr.h"
11
#include "nsIStreamListener.h"
12
#include "nsTArray.h"
13
14
/**
15
* mozilla::net::MemoryDownloader
16
*
17
* This class is similar to nsIDownloader, but stores the downloaded
18
* stream in memory instead of a file. Ownership of the temporary
19
* memory is transferred to the observer when download is complete;
20
* there is no need to retain a reference to the downloader.
21
*/
22
23
namespace mozilla {
24
namespace net {
25
26
class MemoryDownloader final : public nsIStreamListener {
27
public:
28
NS_DECL_ISUPPORTS
29
NS_DECL_NSIREQUESTOBSERVER
30
NS_DECL_NSISTREAMLISTENER
31
32
typedef mozilla::UniquePtr<FallibleTArray<uint8_t>> Data;
33
34
class IObserver : public nsISupports {
35
public:
36
// Note: aData may be null if (and only if) aStatus indicates failure.
37
virtual void OnDownloadComplete(MemoryDownloader* aDownloader,
38
nsIRequest* aRequest, nsISupports* aCtxt,
39
nsresult aStatus, Data aData) = 0;
40
};
41
42
explicit MemoryDownloader(IObserver* aObserver);
43
44
private:
45
virtual ~MemoryDownloader() = default;
46
47
static nsresult ConsumeData(nsIInputStream* in, void* closure,
48
const char* fromRawSegment, uint32_t toOffset,
49
uint32_t count, uint32_t* writeCount);
50
51
RefPtr<IObserver> mObserver;
52
Data mData;
53
nsresult mStatus;
54
};
55
56
} // namespace net
57
} // namespace mozilla
58
59
#endif // mozilla_net_MemoryDownloader_h__