michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 cin et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include "TestCommon.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsIIncrementalDownload.h" michael@0: #include "nsIRequestObserver.h" michael@0: #include "nsIProgressEventSink.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "prprf.h" michael@0: #include "prenv.h" michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: class FetchObserver MOZ_FINAL : public nsIRequestObserver michael@0: , public nsIProgressEventSink michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIREQUESTOBSERVER michael@0: NS_DECL_NSIPROGRESSEVENTSINK michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(FetchObserver, nsIRequestObserver, michael@0: nsIProgressEventSink) michael@0: michael@0: NS_IMETHODIMP michael@0: FetchObserver::OnStartRequest(nsIRequest *request, nsISupports *context) michael@0: { michael@0: printf("FetchObserver::OnStartRequest\n"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: FetchObserver::OnProgress(nsIRequest *request, nsISupports *context, michael@0: uint64_t progress, uint64_t progressMax) michael@0: { michael@0: printf("FetchObserver::OnProgress [%lu/%lu]\n", michael@0: (unsigned long)progress, (unsigned long)progressMax); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: FetchObserver::OnStatus(nsIRequest *request, nsISupports *context, michael@0: nsresult status, const char16_t *statusText) michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: FetchObserver::OnStopRequest(nsIRequest *request, nsISupports *context, michael@0: nsresult status) michael@0: { michael@0: printf("FetchObserver::OnStopRequest [status=%x]\n", michael@0: static_cast(status)); michael@0: michael@0: QuitPumpingEvents(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: static nsresult michael@0: DoIncrementalFetch(const char *uriSpec, const char *resultPath, int32_t chunkSize, michael@0: int32_t interval) michael@0: { michael@0: nsCOMPtr resultFile; michael@0: nsresult rv = NS_NewNativeLocalFile(nsDependentCString(resultPath), michael@0: false, getter_AddRefs(resultFile)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsCOMPtr uri; michael@0: rv = NS_NewURI(getter_AddRefs(uri), uriSpec); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsCOMPtr observer = new FetchObserver(); michael@0: if (!observer) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: nsCOMPtr download = michael@0: do_CreateInstance(NS_INCREMENTALDOWNLOAD_CONTRACTID, &rv); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: rv = download->Init(uri, resultFile, chunkSize, interval); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: rv = download->Start(observer, nullptr); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: PumpEvents(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: if (PR_GetEnv("MOZ_BREAK_ON_MAIN")) michael@0: NS_BREAK(); michael@0: michael@0: if (argc < 5) { michael@0: fprintf(stderr, "USAGE: TestIncrementalDownload \n"); michael@0: return -1; michael@0: } michael@0: michael@0: nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); michael@0: if (NS_FAILED(rv)) michael@0: return -1; michael@0: michael@0: int32_t chunkSize = atoi(argv[3]); michael@0: int32_t interval = atoi(argv[4]); michael@0: michael@0: rv = DoIncrementalFetch(argv[1], argv[2], chunkSize, interval); michael@0: if (NS_FAILED(rv)) michael@0: fprintf(stderr, "ERROR: DoIncrementalFetch failed [%x]\n", michael@0: static_cast(rv)); michael@0: michael@0: NS_ShutdownXPCOM(nullptr); michael@0: return 0; michael@0: }