|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 sts=2 cin et: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include <stdlib.h> |
|
8 #include "TestCommon.h" |
|
9 #include "nsNetUtil.h" |
|
10 #include "nsIIncrementalDownload.h" |
|
11 #include "nsIRequestObserver.h" |
|
12 #include "nsIProgressEventSink.h" |
|
13 #include "nsThreadUtils.h" |
|
14 #include "nsAutoPtr.h" |
|
15 #include "prprf.h" |
|
16 #include "prenv.h" |
|
17 #include "mozilla/Attributes.h" |
|
18 |
|
19 //----------------------------------------------------------------------------- |
|
20 |
|
21 class FetchObserver MOZ_FINAL : public nsIRequestObserver |
|
22 , public nsIProgressEventSink |
|
23 { |
|
24 public: |
|
25 NS_DECL_ISUPPORTS |
|
26 NS_DECL_NSIREQUESTOBSERVER |
|
27 NS_DECL_NSIPROGRESSEVENTSINK |
|
28 }; |
|
29 |
|
30 NS_IMPL_ISUPPORTS(FetchObserver, nsIRequestObserver, |
|
31 nsIProgressEventSink) |
|
32 |
|
33 NS_IMETHODIMP |
|
34 FetchObserver::OnStartRequest(nsIRequest *request, nsISupports *context) |
|
35 { |
|
36 printf("FetchObserver::OnStartRequest\n"); |
|
37 return NS_OK; |
|
38 } |
|
39 |
|
40 NS_IMETHODIMP |
|
41 FetchObserver::OnProgress(nsIRequest *request, nsISupports *context, |
|
42 uint64_t progress, uint64_t progressMax) |
|
43 { |
|
44 printf("FetchObserver::OnProgress [%lu/%lu]\n", |
|
45 (unsigned long)progress, (unsigned long)progressMax); |
|
46 return NS_OK; |
|
47 } |
|
48 |
|
49 NS_IMETHODIMP |
|
50 FetchObserver::OnStatus(nsIRequest *request, nsISupports *context, |
|
51 nsresult status, const char16_t *statusText) |
|
52 { |
|
53 return NS_OK; |
|
54 } |
|
55 |
|
56 NS_IMETHODIMP |
|
57 FetchObserver::OnStopRequest(nsIRequest *request, nsISupports *context, |
|
58 nsresult status) |
|
59 { |
|
60 printf("FetchObserver::OnStopRequest [status=%x]\n", |
|
61 static_cast<uint32_t>(status)); |
|
62 |
|
63 QuitPumpingEvents(); |
|
64 return NS_OK; |
|
65 } |
|
66 |
|
67 //----------------------------------------------------------------------------- |
|
68 |
|
69 static nsresult |
|
70 DoIncrementalFetch(const char *uriSpec, const char *resultPath, int32_t chunkSize, |
|
71 int32_t interval) |
|
72 { |
|
73 nsCOMPtr<nsIFile> resultFile; |
|
74 nsresult rv = NS_NewNativeLocalFile(nsDependentCString(resultPath), |
|
75 false, getter_AddRefs(resultFile)); |
|
76 if (NS_FAILED(rv)) |
|
77 return rv; |
|
78 |
|
79 nsCOMPtr<nsIURI> uri; |
|
80 rv = NS_NewURI(getter_AddRefs(uri), uriSpec); |
|
81 if (NS_FAILED(rv)) |
|
82 return rv; |
|
83 |
|
84 nsCOMPtr<nsIRequestObserver> observer = new FetchObserver(); |
|
85 if (!observer) |
|
86 return NS_ERROR_OUT_OF_MEMORY; |
|
87 |
|
88 nsCOMPtr<nsIIncrementalDownload> download = |
|
89 do_CreateInstance(NS_INCREMENTALDOWNLOAD_CONTRACTID, &rv); |
|
90 if (NS_FAILED(rv)) |
|
91 return rv; |
|
92 |
|
93 rv = download->Init(uri, resultFile, chunkSize, interval); |
|
94 if (NS_FAILED(rv)) |
|
95 return rv; |
|
96 |
|
97 rv = download->Start(observer, nullptr); |
|
98 if (NS_FAILED(rv)) |
|
99 return rv; |
|
100 |
|
101 PumpEvents(); |
|
102 return NS_OK; |
|
103 } |
|
104 |
|
105 int |
|
106 main(int argc, char **argv) |
|
107 { |
|
108 if (PR_GetEnv("MOZ_BREAK_ON_MAIN")) |
|
109 NS_BREAK(); |
|
110 |
|
111 if (argc < 5) { |
|
112 fprintf(stderr, "USAGE: TestIncrementalDownload <url> <file> <chunksize> <interval-in-seconds>\n"); |
|
113 return -1; |
|
114 } |
|
115 |
|
116 nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); |
|
117 if (NS_FAILED(rv)) |
|
118 return -1; |
|
119 |
|
120 int32_t chunkSize = atoi(argv[3]); |
|
121 int32_t interval = atoi(argv[4]); |
|
122 |
|
123 rv = DoIncrementalFetch(argv[1], argv[2], chunkSize, interval); |
|
124 if (NS_FAILED(rv)) |
|
125 fprintf(stderr, "ERROR: DoIncrementalFetch failed [%x]\n", |
|
126 static_cast<uint32_t>(rv)); |
|
127 |
|
128 NS_ShutdownXPCOM(nullptr); |
|
129 return 0; |
|
130 } |