netwerk/test/TestIncrementalDownload.cpp

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

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

mercurial