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