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: #include "txXMLParser.h" michael@0: #include "txURIUtils.h" michael@0: #include "txXPathTreeWalker.h" michael@0: michael@0: #include "nsIDocument.h" michael@0: #include "nsIDOMDocument.h" michael@0: #include "nsSyncLoadService.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsIPrincipal.h" michael@0: michael@0: nsresult michael@0: txParseDocumentFromURI(const nsAString& aHref, const txXPathNode& aLoader, michael@0: nsAString& aErrMsg, txXPathNode** aResult) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aResult); michael@0: *aResult = nullptr; michael@0: nsCOMPtr documentURI; michael@0: nsresult rv = NS_NewURI(getter_AddRefs(documentURI), aHref); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsIDocument* loaderDocument = txXPathNativeNode::getDocument(aLoader); michael@0: michael@0: nsCOMPtr loadGroup = loaderDocument->GetDocumentLoadGroup(); michael@0: michael@0: // For the system principal loaderUri will be null here, which is good michael@0: // since that means that chrome documents can load any uri. michael@0: michael@0: // Raw pointer, we want the resulting txXPathNode to hold a reference to michael@0: // the document. michael@0: nsIDOMDocument* theDocument = nullptr; michael@0: nsAutoSyncOperation sync(loaderDocument); michael@0: rv = nsSyncLoadService::LoadDocument(documentURI, michael@0: loaderDocument->NodePrincipal(), michael@0: loadGroup, true, &theDocument); michael@0: michael@0: if (NS_FAILED(rv)) { michael@0: aErrMsg.Append(NS_LITERAL_STRING("Document load of ") + michael@0: aHref + NS_LITERAL_STRING(" failed.")); michael@0: return NS_FAILED(rv) ? rv : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: *aResult = txXPathNativeNode::createXPathNode(theDocument); michael@0: if (!*aResult) { michael@0: NS_RELEASE(theDocument); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: return NS_OK; michael@0: }