gecko/netwerk/test/TestIncrementalDownload.cpp
Patrick McManus fdaefb68d6 bug 1116867 - make nsIProgressEventSink and nsITransportEventSink safely scriptable r=mayhemer r=bz
These scriptable interfaces use uint_64 arguments with sentinel values
of UINT64_MAX. However, UINT64_MAX exceeds MAX_SAFE_INTEGER and cannot
be gatewayed to/from javascript - so they cannot be used
correctly. Change them to use signed 64 bit numbers and -1 as the
sentinnel. C++ implementations ought to be enough to audit as the
special value could never be used correctly in JS anyhow - also
audited OnProgressChange() uses for downstream use of this data.
---
 dom/base/nsXMLHttpRequest.cpp                      | 19 +++++++----
 dom/base/nsXMLHttpRequest.h                        | 10 +++---
 dom/plugins/base/nsPluginStreamListenerPeer.cpp    |  4 +--
 .../webbrowserpersist/nsWebBrowserPersist.cpp      | 14 ++++----
 image/src/imgLoader.cpp                            |  4 +--
 modules/libjar/nsJARChannel.cpp                    |  3 +-
 netwerk/base/public/nsIProgressEventSink.idl       |  8 ++---
 netwerk/base/public/nsITransport.idl               |  8 ++---
 netwerk/base/public/nsNetUtil.h                    | 24 ++++++++++++++
 netwerk/base/src/Dashboard.cpp                     |  2 +-
 netwerk/base/src/nsBaseChannel.cpp                 | 12 +++----
 netwerk/base/src/nsIncrementalDownload.cpp         |  4 +--
 netwerk/base/src/nsSocketTransport2.cpp            |  5 +--
 netwerk/base/src/nsStreamTransportService.cpp      | 38 +++++++++++++---------
 netwerk/base/src/nsTransportUtils.cpp              | 12 +++----
 netwerk/protocol/file/nsFileChannel.cpp            |  8 +++--
 netwerk/protocol/ftp/nsFtpConnectionThread.cpp     |  4 +--
 netwerk/protocol/http/Http2Push.cpp                |  2 +-
 netwerk/protocol/http/Http2Session.cpp             |  2 +-
 netwerk/protocol/http/HttpChannelChild.cpp         | 31 +++++++++---------
 netwerk/protocol/http/HttpChannelChild.h           |  6 ++--
 netwerk/protocol/http/HttpChannelParent.cpp        |  4 +--
 netwerk/protocol/http/HttpChannelParent.h          |  4 +--
 netwerk/protocol/http/NullHttpTransaction.cpp      |  2 +-
 netwerk/protocol/http/PHttpChannel.ipdl            |  2 +-
 netwerk/protocol/http/SpdyPush31.cpp               |  2 +-
 netwerk/protocol/http/SpdySession31.cpp            |  2 +-
 netwerk/protocol/http/TunnelUtils.cpp              |  2 +-
 netwerk/protocol/http/nsAHttpTransaction.h         |  4 +--
 netwerk/protocol/http/nsHttpChannel.cpp            | 30 +++++++++++------
 netwerk/protocol/http/nsHttpConnection.cpp         |  4 +--
 netwerk/protocol/http/nsHttpConnectionMgr.cpp      |  4 +--
 netwerk/protocol/http/nsHttpPipeline.cpp           |  4 +--
 netwerk/protocol/http/nsHttpPipeline.h             |  6 ++--
 netwerk/protocol/http/nsHttpResponseHead.cpp       |  2 +-
 netwerk/protocol/http/nsHttpResponseHead.h         |  2 +-
 netwerk/protocol/http/nsHttpTransaction.cpp        | 32 +++++++++---------
 netwerk/protocol/http/nsHttpTransaction.h          |  2 +-
 netwerk/protocol/wyciwyg/WyciwygChannelChild.cpp   |  2 +-
 netwerk/protocol/wyciwyg/nsWyciwygChannel.cpp      |  3 +-
 netwerk/test/TestIncrementalDownload.cpp           |  7 ++--
 uriloader/base/nsDocLoader.cpp                     | 14 ++++----
 42 files changed, 203 insertions(+), 151 deletions(-)
2015-01-08 14:48:52 -05:00

133 lines
3.5 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 cin et: */
/* 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/. */
#include <inttypes.h>
#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"
#include "mozilla/Attributes.h"
//-----------------------------------------------------------------------------
class FetchObserver MOZ_FINAL : public nsIRequestObserver
, public nsIProgressEventSink
{
~FetchObserver() {}
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIREQUESTOBSERVER
NS_DECL_NSIPROGRESSEVENTSINK
};
NS_IMPL_ISUPPORTS(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,
int64_t progress, int64_t progressMax)
{
printf("FetchObserver::OnProgress [%" PRId64 "/%" PRId64 "]\n",
progress, progressMax);
return NS_OK;
}
NS_IMETHODIMP
FetchObserver::OnStatus(nsIRequest *request, nsISupports *context,
nsresult status, const char16_t *statusText)
{
return NS_OK;
}
NS_IMETHODIMP
FetchObserver::OnStopRequest(nsIRequest *request, nsISupports *context,
nsresult status)
{
printf("FetchObserver::OnStopRequest [status=%x]\n",
static_cast<uint32_t>(status));
QuitPumpingEvents();
return NS_OK;
}
//-----------------------------------------------------------------------------
static nsresult
DoIncrementalFetch(const char *uriSpec, const char *resultPath, int32_t chunkSize,
int32_t interval)
{
nsCOMPtr<nsIFile> resultFile;
nsresult rv = NS_NewNativeLocalFile(nsDependentCString(resultPath),
false, getter_AddRefs(resultFile));
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, nullptr);
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(nullptr, nullptr, nullptr);
if (NS_FAILED(rv))
return -1;
int32_t chunkSize = atoi(argv[3]);
int32_t interval = atoi(argv[4]);
rv = DoIncrementalFetch(argv[1], argv[2], chunkSize, interval);
if (NS_FAILED(rv))
fprintf(stderr, "ERROR: DoIncrementalFetch failed [%x]\n",
static_cast<uint32_t>(rv));
NS_ShutdownXPCOM(nullptr);
return 0;
}