michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * A service that provides methods for synchronously loading a DOM in various ways. michael@0: */ michael@0: michael@0: #include "nsSyncLoadService.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIChannel.h" michael@0: #include "nsIChannelEventSink.h" michael@0: #include "nsIAsyncVerifyRedirectCallback.h" michael@0: #include "nsIInterfaceRequestor.h" michael@0: #include "nsString.h" michael@0: #include "nsWeakReference.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIDOMDocument.h" michael@0: #include "nsIPrincipal.h" michael@0: #include "nsContentUtils.h" // for kLoadAsData michael@0: #include "nsThreadUtils.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsStreamUtils.h" michael@0: #include "nsCrossSiteListenerProxy.h" michael@0: #include michael@0: michael@0: /** michael@0: * This class manages loading a single XML document michael@0: */ michael@0: michael@0: class nsSyncLoader : public nsIStreamListener, michael@0: public nsIChannelEventSink, michael@0: public nsIInterfaceRequestor, michael@0: public nsSupportsWeakReference michael@0: { michael@0: public: michael@0: nsSyncLoader() : mLoading(false) {} michael@0: virtual ~nsSyncLoader(); michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: michael@0: nsresult LoadDocument(nsIChannel* aChannel, nsIPrincipal *aLoaderPrincipal, michael@0: bool aChannelIsSync, bool aForceToXML, michael@0: nsIDOMDocument** aResult); michael@0: michael@0: NS_FORWARD_NSISTREAMLISTENER(mListener->) michael@0: NS_DECL_NSIREQUESTOBSERVER michael@0: michael@0: NS_DECL_NSICHANNELEVENTSINK michael@0: michael@0: NS_DECL_NSIINTERFACEREQUESTOR michael@0: michael@0: private: michael@0: nsresult PushAsyncStream(nsIStreamListener* aListener); michael@0: nsresult PushSyncStream(nsIStreamListener* aListener); michael@0: michael@0: nsCOMPtr mChannel; michael@0: nsCOMPtr mListener; michael@0: bool mLoading; michael@0: nsresult mAsyncLoadStatus; michael@0: }; michael@0: michael@0: class nsForceXMLListener : public nsIStreamListener michael@0: { michael@0: public: michael@0: nsForceXMLListener(nsIStreamListener* aListener); michael@0: virtual ~nsForceXMLListener(); michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: NS_FORWARD_NSISTREAMLISTENER(mListener->) michael@0: NS_DECL_NSIREQUESTOBSERVER michael@0: michael@0: private: michael@0: nsCOMPtr mListener; michael@0: }; michael@0: michael@0: nsForceXMLListener::nsForceXMLListener(nsIStreamListener* aListener) michael@0: : mListener(aListener) michael@0: { michael@0: } michael@0: michael@0: nsForceXMLListener::~nsForceXMLListener() michael@0: { michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsForceXMLListener, michael@0: nsIStreamListener, michael@0: nsIRequestObserver) michael@0: michael@0: NS_IMETHODIMP michael@0: nsForceXMLListener::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext) michael@0: { michael@0: nsresult status; michael@0: aRequest->GetStatus(&status); michael@0: nsCOMPtr channel = do_QueryInterface(aRequest); michael@0: if (channel && NS_SUCCEEDED(status)) { michael@0: channel->SetContentType(NS_LITERAL_CSTRING("text/xml")); michael@0: } michael@0: michael@0: return mListener->OnStartRequest(aRequest, aContext); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsForceXMLListener::OnStopRequest(nsIRequest *aRequest, nsISupports *aContext, michael@0: nsresult aStatusCode) michael@0: { michael@0: return mListener->OnStopRequest(aRequest, aContext, aStatusCode); michael@0: } michael@0: michael@0: nsSyncLoader::~nsSyncLoader() michael@0: { michael@0: if (mLoading && mChannel) { michael@0: mChannel->Cancel(NS_BINDING_ABORTED); michael@0: } michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsSyncLoader, michael@0: nsIStreamListener, michael@0: nsIRequestObserver, michael@0: nsIChannelEventSink, michael@0: nsIInterfaceRequestor, michael@0: nsISupportsWeakReference) michael@0: michael@0: nsresult michael@0: nsSyncLoader::LoadDocument(nsIChannel* aChannel, michael@0: nsIPrincipal *aLoaderPrincipal, michael@0: bool aChannelIsSync, michael@0: bool aForceToXML, michael@0: nsIDOMDocument **aResult) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: *aResult = nullptr; michael@0: nsresult rv = NS_OK; michael@0: michael@0: nsCOMPtr loaderUri; michael@0: if (aLoaderPrincipal) { michael@0: aLoaderPrincipal->GetURI(getter_AddRefs(loaderUri)); michael@0: } michael@0: michael@0: mChannel = aChannel; michael@0: nsCOMPtr http = do_QueryInterface(mChannel); michael@0: if (http) { michael@0: http->SetRequestHeader(NS_LITERAL_CSTRING("Accept"), michael@0: NS_LITERAL_CSTRING("text/xml,application/xml,application/xhtml+xml,*/*;q=0.1"), michael@0: false); michael@0: if (loaderUri) { michael@0: http->SetReferrer(loaderUri); michael@0: } michael@0: } michael@0: michael@0: // Hook us up to listen to redirects and the like. michael@0: // Do this before setting up the cross-site proxy since michael@0: // that installs its own proxies. michael@0: mChannel->SetNotificationCallbacks(this); michael@0: michael@0: // Get the loadgroup of the channel michael@0: nsCOMPtr loadGroup; michael@0: rv = aChannel->GetLoadGroup(getter_AddRefs(loadGroup)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Create document michael@0: nsCOMPtr document; michael@0: rv = NS_NewXMLDocument(getter_AddRefs(document)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Start the document load. Do this before we attach the load listener michael@0: // since we reset the document which drops all observers. michael@0: nsCOMPtr listener; michael@0: rv = document->StartDocumentLoad(kLoadAsData, mChannel, michael@0: loadGroup, nullptr, michael@0: getter_AddRefs(listener), michael@0: true); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (aForceToXML) { michael@0: nsCOMPtr forceListener = michael@0: new nsForceXMLListener(listener); michael@0: listener.swap(forceListener); michael@0: } michael@0: michael@0: if (aLoaderPrincipal) { michael@0: nsRefPtr corsListener = michael@0: new nsCORSListenerProxy(listener, aLoaderPrincipal, false); michael@0: rv = corsListener->Init(mChannel); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: listener = corsListener; michael@0: } michael@0: michael@0: if (aChannelIsSync) { michael@0: rv = PushSyncStream(listener); michael@0: } michael@0: else { michael@0: rv = PushAsyncStream(listener); michael@0: } michael@0: michael@0: http = do_QueryInterface(mChannel); michael@0: if (NS_SUCCEEDED(rv) && http) { michael@0: bool succeeded; michael@0: if (NS_FAILED(http->GetRequestSucceeded(&succeeded)) || !succeeded) { michael@0: rv = NS_ERROR_FAILURE; michael@0: } michael@0: } michael@0: mChannel = nullptr; michael@0: michael@0: // check that the load succeeded michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: NS_ENSURE_TRUE(document->GetRootElement(), NS_ERROR_FAILURE); michael@0: michael@0: return CallQueryInterface(document, aResult); michael@0: } michael@0: michael@0: nsresult michael@0: nsSyncLoader::PushAsyncStream(nsIStreamListener* aListener) michael@0: { michael@0: mListener = aListener; michael@0: michael@0: mAsyncLoadStatus = NS_OK; michael@0: michael@0: // Start reading from the channel michael@0: nsresult rv = mChannel->AsyncOpen(this, nullptr); michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // process events until we're finished. michael@0: mLoading = true; michael@0: nsIThread *thread = NS_GetCurrentThread(); michael@0: while (mLoading && NS_SUCCEEDED(rv)) { michael@0: bool processedEvent; michael@0: rv = thread->ProcessNextEvent(true, &processedEvent); michael@0: if (NS_SUCCEEDED(rv) && !processedEvent) michael@0: rv = NS_ERROR_UNEXPECTED; michael@0: } michael@0: } michael@0: michael@0: mListener = nullptr; michael@0: michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Note that if AsyncOpen failed that's ok -- the only caller of michael@0: // this method nulls out mChannel immediately after we return. michael@0: michael@0: return mAsyncLoadStatus; michael@0: } michael@0: michael@0: nsresult michael@0: nsSyncLoader::PushSyncStream(nsIStreamListener* aListener) michael@0: { michael@0: nsCOMPtr in; michael@0: nsresult rv = mChannel->Open(getter_AddRefs(in)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: mLoading = true; michael@0: rv = nsSyncLoadService::PushSyncStreamToListener(in, aListener, mChannel); michael@0: mLoading = false; michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSyncLoader::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext) michael@0: { michael@0: return mListener->OnStartRequest(aRequest, aContext); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSyncLoader::OnStopRequest(nsIRequest *aRequest, nsISupports *aContext, michael@0: nsresult aStatusCode) michael@0: { michael@0: if (NS_SUCCEEDED(mAsyncLoadStatus) && NS_FAILED(aStatusCode)) { michael@0: mAsyncLoadStatus = aStatusCode; michael@0: } michael@0: nsresult rv = mListener->OnStopRequest(aRequest, aContext, aStatusCode); michael@0: if (NS_SUCCEEDED(mAsyncLoadStatus) && NS_FAILED(rv)) { michael@0: mAsyncLoadStatus = rv; michael@0: } michael@0: mLoading = false; michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSyncLoader::AsyncOnChannelRedirect(nsIChannel *aOldChannel, michael@0: nsIChannel *aNewChannel, michael@0: uint32_t aFlags, michael@0: nsIAsyncVerifyRedirectCallback *callback) michael@0: { michael@0: NS_PRECONDITION(aNewChannel, "Redirecting to null channel?"); michael@0: michael@0: mChannel = aNewChannel; michael@0: michael@0: callback->OnRedirectVerifyCallback(NS_OK); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSyncLoader::GetInterface(const nsIID & aIID, michael@0: void **aResult) michael@0: { michael@0: return QueryInterface(aIID, aResult); michael@0: } michael@0: michael@0: /* static */ michael@0: nsresult michael@0: nsSyncLoadService::LoadDocument(nsIURI *aURI, nsIPrincipal *aLoaderPrincipal, michael@0: nsILoadGroup *aLoadGroup, bool aForceToXML, michael@0: nsIDOMDocument** aResult) michael@0: { michael@0: nsCOMPtr channel; michael@0: nsresult rv = NS_NewChannel(getter_AddRefs(channel), aURI, nullptr, michael@0: aLoadGroup); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: if (!aForceToXML) { michael@0: channel->SetContentType(NS_LITERAL_CSTRING("text/xml")); michael@0: } michael@0: michael@0: bool isChrome = false, isResource = false; michael@0: bool isSync = (NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome)) && michael@0: isChrome) || michael@0: (NS_SUCCEEDED(aURI->SchemeIs("resource", &isResource)) && michael@0: isResource); michael@0: michael@0: nsRefPtr loader = new nsSyncLoader(); michael@0: return loader->LoadDocument(channel, aLoaderPrincipal, isSync, michael@0: aForceToXML, aResult); michael@0: michael@0: } michael@0: michael@0: /* static */ michael@0: nsresult michael@0: nsSyncLoadService::PushSyncStreamToListener(nsIInputStream* aIn, michael@0: nsIStreamListener* aListener, michael@0: nsIChannel* aChannel) michael@0: { michael@0: // Set up buffering stream michael@0: nsresult rv; michael@0: nsCOMPtr bufferedStream; michael@0: if (!NS_InputStreamIsBuffered(aIn)) { michael@0: int64_t chunkSize; michael@0: rv = aChannel->GetContentLength(&chunkSize); michael@0: if (NS_FAILED(rv) || chunkSize < 1) { michael@0: chunkSize = 4096; michael@0: } michael@0: chunkSize = std::min(int64_t(UINT16_MAX), chunkSize); michael@0: michael@0: rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedStream), aIn, michael@0: chunkSize); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: aIn = bufferedStream; michael@0: } michael@0: michael@0: // Load michael@0: rv = aListener->OnStartRequest(aChannel, nullptr); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: uint64_t sourceOffset = 0; michael@0: while (1) { michael@0: uint64_t readCount = 0; michael@0: rv = aIn->Available(&readCount); michael@0: if (NS_FAILED(rv) || !readCount) { michael@0: if (rv == NS_BASE_STREAM_CLOSED) { michael@0: // End of file, but not an error michael@0: rv = NS_OK; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: if (readCount > UINT32_MAX) michael@0: readCount = UINT32_MAX; michael@0: michael@0: rv = aListener->OnDataAvailable(aChannel, nullptr, aIn, michael@0: (uint32_t)std::min(sourceOffset, (uint64_t)UINT32_MAX), michael@0: (uint32_t)readCount); michael@0: if (NS_FAILED(rv)) { michael@0: break; michael@0: } michael@0: sourceOffset += readCount; michael@0: } michael@0: } michael@0: if (NS_FAILED(rv)) { michael@0: aChannel->Cancel(rv); michael@0: } michael@0: aListener->OnStopRequest(aChannel, nullptr, rv); michael@0: michael@0: return rv; michael@0: }