2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 cin et: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "TestCommon.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIIncrementalDownload.h"
|
|
|
|
#include "nsIRequestObserver.h"
|
|
|
|
#include "nsIProgressEventSink.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "prprf.h"
|
|
|
|
#include "prenv.h"
|
2012-06-05 20:18:25 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-06-05 20:18:25 -07:00
|
|
|
class FetchObserver MOZ_FINAL : public nsIRequestObserver
|
|
|
|
, public nsIProgressEventSink
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSIPROGRESSEVENTSINK
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(FetchObserver, nsIRequestObserver,
|
|
|
|
nsIProgressEventSink)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FetchObserver::OnStartRequest(nsIRequest *request, nsISupports *context)
|
|
|
|
{
|
|
|
|
printf("FetchObserver::OnStartRequest\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FetchObserver::OnProgress(nsIRequest *request, nsISupports *context,
|
|
|
|
PRUint64 progress, PRUint64 progressMax)
|
|
|
|
{
|
|
|
|
printf("FetchObserver::OnProgress [%lu/%lu]\n",
|
2010-05-11 12:07:35 -07:00
|
|
|
(unsigned long)progress, (unsigned long)progressMax);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FetchObserver::OnStatus(nsIRequest *request, nsISupports *context,
|
|
|
|
nsresult status, const PRUnichar *statusText)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
FetchObserver::OnStopRequest(nsIRequest *request, nsISupports *context,
|
|
|
|
nsresult status)
|
|
|
|
{
|
|
|
|
printf("FetchObserver::OnStopRequest [status=%x]\n", status);
|
|
|
|
|
|
|
|
QuitPumpingEvents();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
DoIncrementalFetch(const char *uriSpec, const char *resultPath, PRInt32 chunkSize,
|
|
|
|
PRInt32 interval)
|
|
|
|
{
|
2012-06-05 19:08:30 -07:00
|
|
|
nsCOMPtr<nsIFile> resultFile;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_NewNativeLocalFile(nsDependentCString(resultPath),
|
2011-10-17 07:59:28 -07:00
|
|
|
false, getter_AddRefs(resultFile));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), uriSpec);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRequestObserver> observer = new FetchObserver();
|
|
|
|
if (!observer)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIIncrementalDownload> download =
|
|
|
|
do_CreateInstance(NS_INCREMENTALDOWNLOAD_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = download->Init(uri, resultFile, chunkSize, interval);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = download->Start(observer, nsnull);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
PumpEvents();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (PR_GetEnv("MOZ_BREAK_ON_MAIN"))
|
|
|
|
NS_BREAK();
|
|
|
|
|
|
|
|
if (argc < 5) {
|
|
|
|
fprintf(stderr, "USAGE: TestIncrementalDownload <url> <file> <chunksize> <interval-in-seconds>\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
PRInt32 chunkSize = atoi(argv[3]);
|
|
|
|
PRInt32 interval = atoi(argv[4]);
|
|
|
|
|
|
|
|
rv = DoIncrementalFetch(argv[1], argv[2], chunkSize, interval);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
fprintf(stderr, "ERROR: DoIncrementalFetch failed [%x]\n", rv);
|
|
|
|
|
|
|
|
NS_ShutdownXPCOM(nsnull);
|
|
|
|
return 0;
|
|
|
|
}
|