1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include "txXMLParser.h"
7 #include "txURIUtils.h"
8 #include "txXPathTreeWalker.h"
9
10 #include "mozilla/dom/Document.h"
11 #include "nsSyncLoadService.h"
12 #include "nsNetUtil.h"
13 #include "nsIURI.h"
14
15 using namespace mozilla::dom;
16
txParseDocumentFromURI(const nsAString & aHref,const txXPathNode & aLoader,nsAString & aErrMsg,txXPathNode ** aResult)17 nsresult txParseDocumentFromURI(const nsAString& aHref,
18 const txXPathNode& aLoader, nsAString& aErrMsg,
19 txXPathNode** aResult) {
20 NS_ENSURE_ARG_POINTER(aResult);
21 *aResult = nullptr;
22 nsCOMPtr<nsIURI> documentURI;
23 nsresult rv = NS_NewURI(getter_AddRefs(documentURI), aHref);
24 NS_ENSURE_SUCCESS(rv, rv);
25
26 Document* loaderDocument = txXPathNativeNode::getDocument(aLoader);
27
28 nsCOMPtr<nsILoadGroup> loadGroup = loaderDocument->GetDocumentLoadGroup();
29
30 // For the system principal loaderUri will be null here, which is good
31 // since that means that chrome documents can load any uri.
32
33 // Raw pointer, we want the resulting txXPathNode to hold a reference to
34 // the document.
35 Document* theDocument = nullptr;
36 nsAutoSyncOperation sync(loaderDocument);
37 rv = nsSyncLoadService::LoadDocument(
38 documentURI, nsIContentPolicy::TYPE_INTERNAL_XMLHTTPREQUEST,
39 loaderDocument->NodePrincipal(),
40 nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS, loadGroup,
41 loaderDocument->CookieJarSettings(), true,
42 loaderDocument->GetReferrerPolicy(), &theDocument);
43
44 if (NS_FAILED(rv)) {
45 aErrMsg.AppendLiteral("Document load of ");
46 aErrMsg.Append(aHref);
47 aErrMsg.AppendLiteral(" failed.");
48 return NS_FAILED(rv) ? rv : NS_ERROR_FAILURE;
49 }
50
51 *aResult = txXPathNativeNode::createXPathNode(theDocument);
52 if (!*aResult) {
53 NS_RELEASE(theDocument);
54 return NS_ERROR_FAILURE;
55 }
56
57 return NS_OK;
58 }
59