Merge mozilla-central to autoland. CLOSED TREE
[gecko.git] / netwerk / base / MemoryDownloader.h
blob8b5700f5bf6e56c02b2d4b6ab0c9e255151c9067
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 #ifndef mozilla_net_MemoryDownloader_h__
7 #define mozilla_net_MemoryDownloader_h__
9 #include "mozilla/UniquePtr.h"
10 #include "nsCOMPtr.h"
11 #include "nsIStreamListener.h"
12 #include "nsTArray.h"
14 /**
15 * mozilla::net::MemoryDownloader
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.
23 namespace mozilla {
24 namespace net {
26 class MemoryDownloader final : public nsIStreamListener {
27 public:
28 NS_DECL_ISUPPORTS
29 NS_DECL_NSIREQUESTOBSERVER
30 NS_DECL_NSISTREAMLISTENER
32 using Data = mozilla::UniquePtr<FallibleTArray<uint8_t>>;
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, nsresult aStatus,
39 Data aData) = 0;
42 explicit MemoryDownloader(IObserver* aObserver);
44 private:
45 virtual ~MemoryDownloader() = default;
47 static nsresult ConsumeData(nsIInputStream* in, void* closure,
48 const char* fromRawSegment, uint32_t toOffset,
49 uint32_t count, uint32_t* writeCount);
51 RefPtr<IObserver> mObserver;
52 Data mData;
53 nsresult mStatus;
56 } // namespace net
57 } // namespace mozilla
59 #endif // mozilla_net_MemoryDownloader_h__