2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* A service that provides methods for synchronously loading a DOM in various ways.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsSyncLoadService.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIChannelEventSink.h"
|
2010-08-04 19:15:55 -07:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsWeakReference.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
2012-07-01 16:45:59 -07:00
|
|
|
#include "nsIPrincipal.h"
|
2013-08-13 23:56:21 -07:00
|
|
|
#include "nsContentUtils.h" // for kLoadAsData
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsStreamUtils.h"
|
2008-09-30 17:49:30 -07:00
|
|
|
#include "nsCrossSiteListenerProxy.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-11-18 05:47:20 -08:00
|
|
|
using mozilla::net::ReferrerPolicy;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* This class manages loading a single XML document
|
|
|
|
*/
|
|
|
|
|
2011-06-17 20:05:41 -07:00
|
|
|
class nsSyncLoader : public nsIStreamListener,
|
2007-03-22 10:30:00 -07:00
|
|
|
public nsIChannelEventSink,
|
|
|
|
public nsIInterfaceRequestor,
|
|
|
|
public nsSupportsWeakReference
|
|
|
|
{
|
|
|
|
public:
|
2011-10-17 07:59:28 -07:00
|
|
|
nsSyncLoader() : mLoading(false) {}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2008-02-26 19:45:29 -08:00
|
|
|
nsresult LoadDocument(nsIChannel* aChannel, nsIPrincipal *aLoaderPrincipal,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aChannelIsSync, bool aForceToXML,
|
2014-11-18 05:47:20 -08:00
|
|
|
ReferrerPolicy aReferrerPolicy,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMDocument** aResult);
|
|
|
|
|
2011-06-17 20:05:41 -07:00
|
|
|
NS_FORWARD_NSISTREAMLISTENER(mListener->)
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_DECL_NSICHANNELEVENTSINK
|
|
|
|
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
|
|
|
|
private:
|
2014-06-24 19:09:15 -07:00
|
|
|
virtual ~nsSyncLoader();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult PushAsyncStream(nsIStreamListener* aListener);
|
|
|
|
nsresult PushSyncStream(nsIStreamListener* aListener);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
2011-06-17 20:05:41 -07:00
|
|
|
nsCOMPtr<nsIStreamListener> mListener;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mLoading;
|
2011-06-17 20:05:41 -07:00
|
|
|
nsresult mAsyncLoadStatus;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class nsForceXMLListener : public nsIStreamListener
|
|
|
|
{
|
2014-06-24 19:09:15 -07:00
|
|
|
virtual ~nsForceXMLListener();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
public:
|
2014-09-01 17:49:25 -07:00
|
|
|
explicit nsForceXMLListener(nsIStreamListener* aListener);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_FORWARD_NSISTREAMLISTENER(mListener->)
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIStreamListener> mListener;
|
|
|
|
};
|
|
|
|
|
|
|
|
nsForceXMLListener::nsForceXMLListener(nsIStreamListener* aListener)
|
|
|
|
: mListener(aListener)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsForceXMLListener::~nsForceXMLListener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsForceXMLListener,
|
|
|
|
nsIStreamListener,
|
|
|
|
nsIRequestObserver)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsForceXMLListener::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext)
|
|
|
|
{
|
|
|
|
nsresult status;
|
|
|
|
aRequest->GetStatus(&status);
|
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
|
|
|
|
if (channel && NS_SUCCEEDED(status)) {
|
|
|
|
channel->SetContentType(NS_LITERAL_CSTRING("text/xml"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return mListener->OnStartRequest(aRequest, aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsForceXMLListener::OnStopRequest(nsIRequest *aRequest, nsISupports *aContext,
|
|
|
|
nsresult aStatusCode)
|
|
|
|
{
|
|
|
|
return mListener->OnStopRequest(aRequest, aContext, aStatusCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSyncLoader::~nsSyncLoader()
|
|
|
|
{
|
|
|
|
if (mLoading && mChannel) {
|
|
|
|
mChannel->Cancel(NS_BINDING_ABORTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsSyncLoader,
|
|
|
|
nsIStreamListener,
|
|
|
|
nsIRequestObserver,
|
|
|
|
nsIChannelEventSink,
|
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsISupportsWeakReference)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSyncLoader::LoadDocument(nsIChannel* aChannel,
|
2008-02-26 19:45:29 -08:00
|
|
|
nsIPrincipal *aLoaderPrincipal,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aChannelIsSync,
|
|
|
|
bool aForceToXML,
|
2014-11-18 05:47:20 -08:00
|
|
|
ReferrerPolicy aReferrerPolicy,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMDocument **aResult)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
2012-07-30 07:20:58 -07:00
|
|
|
*aResult = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2008-02-26 19:45:29 -08:00
|
|
|
nsCOMPtr<nsIURI> loaderUri;
|
|
|
|
if (aLoaderPrincipal) {
|
|
|
|
aLoaderPrincipal->GetURI(getter_AddRefs(loaderUri));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mChannel = aChannel;
|
|
|
|
nsCOMPtr<nsIHttpChannel> http = do_QueryInterface(mChannel);
|
|
|
|
if (http) {
|
2014-11-18 05:47:20 -08:00
|
|
|
http->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_LITERAL_CSTRING("text/xml,application/xml,application/xhtml+xml,*/*;q=0.1"),
|
2011-10-17 07:59:28 -07:00
|
|
|
false);
|
2008-02-26 19:45:29 -08:00
|
|
|
if (loaderUri) {
|
2014-11-18 05:47:20 -08:00
|
|
|
http->SetReferrerWithPolicy(loaderUri, aReferrerPolicy);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-26 19:45:29 -08:00
|
|
|
// Hook us up to listen to redirects and the like.
|
|
|
|
// Do this before setting up the cross-site proxy since
|
|
|
|
// that installs its own proxies.
|
|
|
|
mChannel->SetNotificationCallbacks(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Get the loadgroup of the channel
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
rv = aChannel->GetLoadGroup(getter_AddRefs(loadGroup));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Create document
|
|
|
|
nsCOMPtr<nsIDocument> document;
|
|
|
|
rv = NS_NewXMLDocument(getter_AddRefs(document));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Start the document load. Do this before we attach the load listener
|
|
|
|
// since we reset the document which drops all observers.
|
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
|
|
|
rv = document->StartDocumentLoad(kLoadAsData, mChannel,
|
2012-07-30 07:20:58 -07:00
|
|
|
loadGroup, nullptr,
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_AddRefs(listener),
|
2011-10-17 07:59:28 -07:00
|
|
|
true);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (aForceToXML) {
|
|
|
|
nsCOMPtr<nsIStreamListener> forceListener =
|
|
|
|
new nsForceXMLListener(listener);
|
|
|
|
listener.swap(forceListener);
|
|
|
|
}
|
|
|
|
|
2008-02-26 19:45:29 -08:00
|
|
|
if (aLoaderPrincipal) {
|
2012-09-18 19:16:23 -07:00
|
|
|
nsRefPtr<nsCORSListenerProxy> corsListener =
|
|
|
|
new nsCORSListenerProxy(listener, aLoaderPrincipal, false);
|
|
|
|
rv = corsListener->Init(mChannel);
|
2008-09-30 17:49:30 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-09-18 19:16:23 -07:00
|
|
|
listener = corsListener;
|
2008-02-26 19:45:29 -08:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aChannelIsSync) {
|
|
|
|
rv = PushSyncStream(listener);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = PushAsyncStream(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
http = do_QueryInterface(mChannel);
|
2011-06-17 20:05:41 -07:00
|
|
|
if (NS_SUCCEEDED(rv) && http) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool succeeded;
|
2011-06-17 20:05:41 -07:00
|
|
|
if (NS_FAILED(http->GetRequestSucceeded(&succeeded)) || !succeeded) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
mChannel = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// check that the load succeeded
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-04-30 06:12:05 -07:00
|
|
|
NS_ENSURE_TRUE(document->GetRootElement(), NS_ERROR_FAILURE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return CallQueryInterface(document, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSyncLoader::PushAsyncStream(nsIStreamListener* aListener)
|
|
|
|
{
|
2011-06-17 20:05:41 -07:00
|
|
|
mListener = aListener;
|
|
|
|
|
|
|
|
mAsyncLoadStatus = NS_OK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Start reading from the channel
|
2012-07-30 07:20:58 -07:00
|
|
|
nsresult rv = mChannel->AsyncOpen(this, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// process events until we're finished.
|
2011-10-17 07:59:28 -07:00
|
|
|
mLoading = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIThread *thread = NS_GetCurrentThread();
|
|
|
|
while (mLoading && NS_SUCCEEDED(rv)) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool processedEvent;
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = thread->ProcessNextEvent(true, &processedEvent);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv) && !processedEvent)
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mListener = nullptr;
|
2011-06-17 20:05:41 -07:00
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Note that if AsyncOpen failed that's ok -- the only caller of
|
|
|
|
// this method nulls out mChannel immediately after we return.
|
2011-06-17 20:05:41 -07:00
|
|
|
|
|
|
|
return mAsyncLoadStatus;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsSyncLoader::PushSyncStream(nsIStreamListener* aListener)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> in;
|
|
|
|
nsresult rv = mChannel->Open(getter_AddRefs(in));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mLoading = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = nsSyncLoadService::PushSyncStreamToListener(in, aListener, mChannel);
|
2011-10-17 07:59:28 -07:00
|
|
|
mLoading = false;
|
2011-06-17 20:05:41 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-06-17 20:05:41 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSyncLoader::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-06-17 20:05:41 -07:00
|
|
|
return mListener->OnStartRequest(aRequest, aContext);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-06-17 20:05:41 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSyncLoader::OnStopRequest(nsIRequest *aRequest, nsISupports *aContext,
|
|
|
|
nsresult aStatusCode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-06-17 20:05:41 -07:00
|
|
|
if (NS_SUCCEEDED(mAsyncLoadStatus) && NS_FAILED(aStatusCode)) {
|
|
|
|
mAsyncLoadStatus = aStatusCode;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-06-17 20:05:41 -07:00
|
|
|
nsresult rv = mListener->OnStopRequest(aRequest, aContext, aStatusCode);
|
|
|
|
if (NS_SUCCEEDED(mAsyncLoadStatus) && NS_FAILED(rv)) {
|
|
|
|
mAsyncLoadStatus = rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
mLoading = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-17 20:05:41 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-08-04 19:15:55 -07:00
|
|
|
nsSyncLoader::AsyncOnChannelRedirect(nsIChannel *aOldChannel,
|
|
|
|
nsIChannel *aNewChannel,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aFlags,
|
2010-08-04 19:15:55 -07:00
|
|
|
nsIAsyncVerifyRedirectCallback *callback)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNewChannel, "Redirecting to null channel?");
|
|
|
|
|
|
|
|
mChannel = aNewChannel;
|
|
|
|
|
2010-08-04 19:15:55 -07:00
|
|
|
callback->OnRedirectVerifyCallback(NS_OK);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSyncLoader::GetInterface(const nsIID & aIID,
|
|
|
|
void **aResult)
|
|
|
|
{
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult
|
2008-02-26 19:45:29 -08:00
|
|
|
nsSyncLoadService::LoadDocument(nsIURI *aURI, nsIPrincipal *aLoaderPrincipal,
|
2011-09-28 23:19:26 -07:00
|
|
|
nsILoadGroup *aLoadGroup, bool aForceToXML,
|
2014-11-18 05:47:20 -08:00
|
|
|
ReferrerPolicy aReferrerPolicy,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMDocument** aResult)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2014-09-21 09:36:25 -07:00
|
|
|
nsresult rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
aURI,
|
|
|
|
aLoaderPrincipal,
|
|
|
|
nsILoadInfo::SEC_NORMAL,
|
|
|
|
nsIContentPolicy::TYPE_OTHER,
|
2007-03-22 10:30:00 -07:00
|
|
|
aLoadGroup);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!aForceToXML) {
|
|
|
|
channel->SetContentType(NS_LITERAL_CSTRING("text/xml"));
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isChrome = false, isResource = false;
|
|
|
|
bool isSync = (NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome)) &&
|
2007-03-22 10:30:00 -07:00
|
|
|
isChrome) ||
|
|
|
|
(NS_SUCCEEDED(aURI->SchemeIs("resource", &isResource)) &&
|
|
|
|
isResource);
|
|
|
|
|
2011-06-17 20:05:41 -07:00
|
|
|
nsRefPtr<nsSyncLoader> loader = new nsSyncLoader();
|
|
|
|
return loader->LoadDocument(channel, aLoaderPrincipal, isSync,
|
2014-11-18 05:47:20 -08:00
|
|
|
aForceToXML, aReferrerPolicy, aResult);
|
2011-06-17 20:05:41 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult
|
|
|
|
nsSyncLoadService::PushSyncStreamToListener(nsIInputStream* aIn,
|
|
|
|
nsIStreamListener* aListener,
|
|
|
|
nsIChannel* aChannel)
|
|
|
|
{
|
|
|
|
// Set up buffering stream
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIInputStream> bufferedStream;
|
|
|
|
if (!NS_InputStreamIsBuffered(aIn)) {
|
2012-10-22 10:51:07 -07:00
|
|
|
int64_t chunkSize;
|
2008-12-17 17:27:43 -08:00
|
|
|
rv = aChannel->GetContentLength(&chunkSize);
|
2013-07-30 14:38:26 -07:00
|
|
|
if (NS_FAILED(rv) || chunkSize < 1) {
|
2008-12-17 17:27:43 -08:00
|
|
|
chunkSize = 4096;
|
|
|
|
}
|
2013-01-15 04:22:03 -08:00
|
|
|
chunkSize = std::min(int64_t(UINT16_MAX), chunkSize);
|
2008-12-17 17:27:43 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), aIn,
|
2008-12-17 17:27:43 -08:00
|
|
|
chunkSize);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aIn = bufferedStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = aListener->OnStartRequest(aChannel, nullptr);
|
2011-05-18 18:22:59 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t sourceOffset = 0;
|
2011-05-18 18:22:59 -07:00
|
|
|
while (1) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint64_t readCount = 0;
|
2011-05-18 18:22:59 -07:00
|
|
|
rv = aIn->Available(&readCount);
|
|
|
|
if (NS_FAILED(rv) || !readCount) {
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
// End of file, but not an error
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
break;
|
2007-05-07 15:11:53 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-27 23:57:33 -07:00
|
|
|
if (readCount > UINT32_MAX)
|
|
|
|
readCount = UINT32_MAX;
|
2012-08-10 19:44:11 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = aListener->OnDataAvailable(aChannel, nullptr, aIn,
|
2013-01-15 04:22:03 -08:00
|
|
|
(uint32_t)std::min(sourceOffset, (uint64_t)UINT32_MAX),
|
2012-08-22 08:56:38 -07:00
|
|
|
(uint32_t)readCount);
|
2011-05-18 18:22:59 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sourceOffset += readCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-05-18 18:22:59 -07:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aChannel->Cancel(rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
aListener->OnStopRequest(aChannel, nullptr, rv);
|
2011-05-18 18:22:59 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|