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"
11 #include "nsIStreamListener.h"
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.
26 class MemoryDownloader final
: public nsIStreamListener
{
29 NS_DECL_NSIREQUESTOBSERVER
30 NS_DECL_NSISTREAMLISTENER
32 using Data
= mozilla::UniquePtr
<FallibleTArray
<uint8_t>>;
34 class IObserver
: public nsISupports
{
36 // Note: aData may be null if (and only if) aStatus indicates failure.
37 virtual void OnDownloadComplete(MemoryDownloader
* aDownloader
,
38 nsIRequest
* aRequest
, nsresult aStatus
,
42 explicit MemoryDownloader(IObserver
* aObserver
);
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
;
57 } // namespace mozilla
59 #endif // mozilla_net_MemoryDownloader_h__