Line data Source code
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 : {
28 : public:
29 : NS_DECL_ISUPPORTS
30 : NS_DECL_NSIREQUESTOBSERVER
31 : NS_DECL_NSISTREAMLISTENER
32 :
33 : typedef mozilla::UniquePtr<FallibleTArray<uint8_t>> Data;
34 :
35 0 : class IObserver : public nsISupports {
36 : public:
37 : // Note: aData may be null if (and only if) aStatus indicates failure.
38 : virtual void OnDownloadComplete(MemoryDownloader* aDownloader,
39 : nsIRequest* aRequest,
40 : nsISupports* aCtxt,
41 : nsresult aStatus,
42 : Data aData) = 0;
43 : };
44 :
45 : explicit MemoryDownloader(IObserver* aObserver);
46 :
47 : private:
48 : virtual ~MemoryDownloader();
49 :
50 : static nsresult ConsumeData(nsIInputStream *in,
51 : void *closure,
52 : const char *fromRawSegment,
53 : uint32_t toOffset,
54 : uint32_t count,
55 : uint32_t *writeCount);
56 :
57 : RefPtr<IObserver> mObserver;
58 : Data mData;
59 : nsresult mStatus;
60 : };
61 :
62 : } // namespace net
63 : } // namespace mozilla
64 :
65 : #endif // mozilla_net_MemoryDownloader_h__
|