2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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"
|
2016-06-10 03:56:23 +03:00
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIURI.h"
|
2002-10-09 11:03:15 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsWeakReference.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.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"
|
2006-06-16 00:30:44 +04:00
|
|
|
#include "nsStreamUtils.h"
|
2019-05-02 15:33:55 +03:00
|
|
|
#include "ReferrerInfo.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2002-10-09 11:03:15 +04:00
|
|
|
|
2017-10-26 12:40:12 +03:00
|
|
|
using namespace mozilla;
|
Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 10:46:35 +03:00
|
|
|
using namespace mozilla::dom;
|
2017-10-26 12:40:12 +03:00
|
|
|
|
2019-08-21 16:24:45 +03:00
|
|
|
using mozilla::dom::ReferrerPolicy;
|
2014-11-18 16:47:20 +03:00
|
|
|
|
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:
|
2018-06-16 17:21:46 +03:00
|
|
|
nsSyncLoader()
|
|
|
|
: mLoading(false), mAsyncLoadStatus(NS_ERROR_NOT_INITIALIZED) {}
|
2002-10-09 11:03:15 +04:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
NS_DECL_ISUPPORTS
|
2002-10-09 11:03:15 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult LoadDocument(nsIChannel* aChannel, bool aChannelIsSync,
|
2018-04-13 16:01:28 +03:00
|
|
|
bool aForceToXML, ReferrerPolicy aReferrerPolicy,
|
2019-05-01 11:47:10 +03:00
|
|
|
Document** aResult);
|
2002-10-09 11:03:15 +04:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
NS_FORWARD_NSISTREAMLISTENER(mListener->)
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
2002-10-09 11:03:15 +04:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
NS_DECL_NSICHANNELEVENTSINK
|
2002-10-09 11:03:15 +04:00
|
|
|
|
2018-04-13 16:01:28 +03:00
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
2002-10-09 11:03:15 +04:00
|
|
|
|
2006-06-16 00:30:44 +04:00
|
|
|
private:
|
2014-06-25 06:09:15 +04:00
|
|
|
virtual ~nsSyncLoader();
|
|
|
|
|
2019-05-01 11:47:10 +03: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:
|
2019-05-01 11:47:10 +03: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;
|
|
|
|
};
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsForceXMLListener::nsForceXMLListener(nsIStreamListener* aListener)
|
2003-05-05 13:17:36 +04:00
|
|
|
: mListener(aListener) {}
|
|
|
|
|
2020-02-21 13:41:47 +03:00
|
|
|
nsForceXMLListener::~nsForceXMLListener() = default;
|
2003-05-05 13:17:36 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsForceXMLListener, nsIStreamListener, nsIRequestObserver)
|
2003-05-05 13:17:36 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsForceXMLListener::OnStartRequest(nsIRequest* aRequest) {
|
2003-05-05 13:17:36 +04:00
|
|
|
nsresult status;
|
|
|
|
aRequest->GetStatus(&status);
|
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
|
|
|
|
if (channel && NS_SUCCEEDED(status)) {
|
|
|
|
channel->SetContentType(NS_LITERAL_CSTRING("text/xml"));
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-02-28 02:41:04 +03:00
|
|
|
return mListener->OnStartRequest(aRequest);
|
2003-05-05 13:17:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsForceXMLListener::OnStopRequest(nsIRequest* aRequest, nsresult aStatusCode) {
|
2019-02-28 02:41:31 +03:00
|
|
|
return mListener->OnStopRequest(aRequest, aStatusCode);
|
2003-05-05 13:17:36 +04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult nsSyncLoader::LoadDocument(nsIChannel* aChannel, bool aChannelIsSync,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aForceToXML,
|
2014-11-18 16:47:20 +03:00
|
|
|
ReferrerPolicy aReferrerPolicy,
|
2019-05-01 11:47:10 +03:00
|
|
|
Document** aResult) {
|
2015-10-27 00:22:59 +03:00
|
|
|
NS_ENSURE_ARG(aChannel);
|
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;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
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) {
|
2016-12-20 06:49:32 +03:00
|
|
|
rv = http->SetRequestHeader(
|
|
|
|
NS_LITERAL_CSTRING("Accept"),
|
|
|
|
NS_LITERAL_CSTRING(
|
|
|
|
"text/xml,application/xml,application/xhtml+xml,*/*;q=0.1"),
|
|
|
|
false);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2019-02-20 15:27:25 +03:00
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
|
|
|
|
nsCOMPtr<nsIURI> loaderUri;
|
|
|
|
loadInfo->TriggeringPrincipal()->GetURI(getter_AddRefs(loaderUri));
|
|
|
|
if (loaderUri) {
|
2019-05-02 15:33:55 +03:00
|
|
|
nsCOMPtr<nsIReferrerInfo> referrerInfo =
|
|
|
|
new ReferrerInfo(loaderUri, aReferrerPolicy);
|
|
|
|
rv = http->SetReferrerInfoWithoutClone(referrerInfo);
|
2019-02-20 15:27:25 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2006-05-05 15:48:07 +04:00
|
|
|
}
|
2018-11-30 13:46: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);
|
2018-11-30 13:46:48 +03: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);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-10-09 11:03:15 +04:00
|
|
|
// Create document
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> document;
|
2006-06-16 00:30:44 +04:00
|
|
|
rv = NS_NewXMLDocument(getter_AddRefs(document));
|
2002-03-19 17:14:27 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46: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;
|
2017-07-06 15:00:35 +03:00
|
|
|
rv = document->StartDocumentLoad(kLoadAsData, mChannel, loadGroup, nullptr,
|
2003-01-15 01:36:57 +03:00
|
|
|
getter_AddRefs(listener), true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2003-05-05 13:17:36 +04:00
|
|
|
if (aForceToXML) {
|
|
|
|
nsCOMPtr<nsIStreamListener> forceListener =
|
|
|
|
new nsForceXMLListener(listener);
|
|
|
|
listener.swap(forceListener);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2002-10-09 11:03:15 +04:00
|
|
|
if (aChannelIsSync) {
|
|
|
|
rv = PushSyncStream(listener);
|
2018-11-30 13:46:48 +03:00
|
|
|
} else {
|
2002-10-09 11:03:15 +04:00
|
|
|
rv = PushAsyncStream(listener);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
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;
|
2003-05-05 13:17:36 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mChannel = nullptr;
|
2003-05-05 13:17:36 +04:00
|
|
|
|
2002-10-09 11:03:15 +04:00
|
|
|
// check that the load succeeded
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-06-18 07:05:41 +04:00
|
|
|
NS_ENSURE_TRUE(document->GetRootElement(), NS_ERROR_FAILURE);
|
2002-10-09 11:03:15 +04:00
|
|
|
|
2002-06-15 03:54:18 +04:00
|
|
|
document.forget(aResult);
|
2001-01-12 23:06:48 +03:00
|
|
|
|
2010-04-30 17:12:05 +04:00
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2002-10-09 11:03:15 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult nsSyncLoader::PushAsyncStream(nsIStreamListener* aListener) {
|
2018-05-11 20:46:15 +03:00
|
|
|
mListener = aListener;
|
|
|
|
|
|
|
|
mAsyncLoadStatus = NS_OK;
|
2002-10-09 11:03:15 +04:00
|
|
|
|
|
|
|
// Start reading from the channel
|
2019-02-12 19:08:25 +03:00
|
|
|
nsresult rv = mChannel->AsyncOpen(this);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
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;
|
2019-05-01 11:47:10 +03:00
|
|
|
nsIThread* thread = NS_GetCurrentThread();
|
2002-10-09 11:03:15 +04:00
|
|
|
while (mLoading && NS_SUCCEEDED(rv)) {
|
2017-07-06 15:00:35 +03: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;
|
2001-01-12 23:06:48 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03: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
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult nsSyncLoader::PushSyncStream(nsIStreamListener* aListener) {
|
2002-10-09 11:03:15 +04:00
|
|
|
nsCOMPtr<nsIInputStream> in;
|
2019-02-12 19:08:25 +03:00
|
|
|
nsresult rv = mChannel->Open(getter_AddRefs(in));
|
2002-10-09 11:03:15 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mLoading = true;
|
2017-10-19 12:39:30 +03:00
|
|
|
rv = nsSyncLoadService::PushSyncStreamToListener(in.forget(), aListener,
|
|
|
|
mChannel);
|
2011-10-17 18:59:28 +04:00
|
|
|
mLoading = false;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2001-01-12 23:06:48 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-06-18 07:05:41 +04:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsSyncLoader::OnStartRequest(nsIRequest* aRequest) {
|
2019-02-28 02:41:04 +03:00
|
|
|
return mListener->OnStartRequest(aRequest);
|
2001-01-12 23:06:48 +03:00
|
|
|
}
|
|
|
|
|
2011-06-18 07:05:41 +04:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsSyncLoader::OnStopRequest(nsIRequest* aRequest, nsresult aStatusCode) {
|
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
|
|
|
}
|
2019-02-28 02:41:31 +03:00
|
|
|
nsresult rv = mListener->OnStopRequest(aRequest, aStatusCode);
|
2011-06-18 07:05:41 +04:00
|
|
|
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;
|
2018-11-30 13:46: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
|
2019-05-01 11:47:10 +03:00
|
|
|
nsSyncLoader::AsyncOnChannelRedirect(nsIChannel* aOldChannel,
|
|
|
|
nsIChannel* aNewChannel, uint32_t aFlags,
|
|
|
|
nsIAsyncVerifyRedirectCallback* callback) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(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
|
2019-05-01 11:47:10 +03:00
|
|
|
nsSyncLoader::GetInterface(const nsIID& aIID, void** aResult) {
|
2002-06-15 03:54:18 +04:00
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
2002-10-09 11:03:15 +04:00
|
|
|
|
2006-06-16 00:30:44 +04:00
|
|
|
/* static */
|
2015-10-27 00:22:59 +03:00
|
|
|
nsresult nsSyncLoadService::LoadDocument(
|
2019-05-01 11:47:10 +03:00
|
|
|
nsIURI* aURI, nsContentPolicyType aContentPolicyType,
|
|
|
|
nsIPrincipal* aLoaderPrincipal, nsSecurityFlags aSecurityFlags,
|
|
|
|
nsILoadGroup* aLoadGroup, nsICookieSettings* aCookieSettings,
|
|
|
|
bool aForceToXML, ReferrerPolicy aReferrerPolicy, Document** aResult) {
|
2006-06-16 00:30:44 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2019-03-08 12:04:11 +03:00
|
|
|
nsresult rv =
|
|
|
|
NS_NewChannel(getter_AddRefs(channel), aURI, aLoaderPrincipal,
|
|
|
|
aSecurityFlags, aContentPolicyType, aCookieSettings,
|
|
|
|
nullptr, // PerformanceStorage
|
|
|
|
aLoadGroup);
|
2002-10-09 11:03:15 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2006-06-16 00:30:44 +04:00
|
|
|
if (!aForceToXML) {
|
|
|
|
channel->SetContentType(NS_LITERAL_CSTRING("text/xml"));
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-12-01 07:13:20 +03:00
|
|
|
// if the load needs to enforce CORS, then force the load to be async
|
2015-10-27 00:22:59 +03:00
|
|
|
bool isSync =
|
2015-12-01 07:13:20 +03:00
|
|
|
!(aSecurityFlags & nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS) &&
|
2019-07-30 20:51:37 +03:00
|
|
|
(aURI->SchemeIs("chrome") || aURI->SchemeIs("resource"));
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsSyncLoader> loader = new nsSyncLoader();
|
2015-10-27 00:22:59 +03:00
|
|
|
return loader->LoadDocument(channel, isSync, aForceToXML, aReferrerPolicy,
|
|
|
|
aResult);
|
2002-10-09 11:03:15 +04:00
|
|
|
}
|
|
|
|
|
2006-06-16 00:30:44 +04:00
|
|
|
/* static */
|
2017-10-19 12:39:30 +03:00
|
|
|
nsresult nsSyncLoadService::PushSyncStreamToListener(
|
2019-05-01 11:47:10 +03:00
|
|
|
already_AddRefed<nsIInputStream> aIn, nsIStreamListener* aListener,
|
|
|
|
nsIChannel* aChannel) {
|
2018-05-30 22:15:35 +03:00
|
|
|
nsCOMPtr<nsIInputStream> in = std::move(aIn);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2002-10-09 11:03:15 +04:00
|
|
|
// 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;
|
2017-10-19 12:39:30 +03:00
|
|
|
if (!NS_InputStreamIsBuffered(in)) {
|
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;
|
2006-06-16 00:30:44 +04:00
|
|
|
}
|
2013-01-15 16:22:03 +04:00
|
|
|
chunkSize = std::min(int64_t(UINT16_MAX), chunkSize);
|
2002-10-09 11:03:15 +04:00
|
|
|
|
2017-10-19 12:39:30 +03:00
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), in.forget(),
|
2013-01-15 16:22:03 +04:00
|
|
|
chunkSize);
|
2006-06-16 00:30:44 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-10-19 12:39:30 +03:00
|
|
|
in = bufferedStream;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2002-10-09 11:03:15 +04:00
|
|
|
// Load
|
2019-02-28 02:41:04 +03:00
|
|
|
rv = aListener->OnStartRequest(aChannel);
|
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-09-28 10:57:33 +04:00
|
|
|
uint64_t readCount = 0;
|
2017-10-19 12:39:30 +03:00
|
|
|
rv = in->Available(&readCount);
|
2011-05-19 05:22:59 +04:00
|
|
|
if (NS_FAILED(rv) || !readCount) {
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
// End of file, but not an error
|
|
|
|
rv = NS_OK;
|
2002-10-09 11:03:15 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-28 10:57:33 +04:00
|
|
|
if (readCount > UINT32_MAX) readCount = UINT32_MAX;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-10-19 12:39:30 +03:00
|
|
|
rv = aListener->OnDataAvailable(
|
2013-01-15 16:22:03 +04:00
|
|
|
aChannel, in, (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)) {
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
|
|
|
}
|
2011-05-19 05:22:59 +04:00
|
|
|
sourceOffset += readCount;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2011-05-19 05:22:59 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aChannel->Cancel(rv);
|
2002-10-09 11:03:15 +04:00
|
|
|
}
|
2019-02-28 02:41:31 +03:00
|
|
|
aListener->OnStopRequest(aChannel, rv);
|
2011-05-19 05:22:59 +04:00
|
|
|
|
2002-10-09 11:03:15 +04:00
|
|
|
return rv;
|
|
|
|
}
|