1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "nsIDocument.h"
11 #include "nsIDOMDocument.h"
12 #include "nsSyncLoadService.h"
13 #include "nsNetUtil.h"
14 #include "nsIURI.h"
15 #include "nsIPrincipal.h"
16
17 nsresult
txParseDocumentFromURI(const nsAString & aHref,const txXPathNode & aLoader,nsAString & aErrMsg,txXPathNode ** aResult)18 txParseDocumentFromURI(const nsAString& aHref,
19 const txXPathNode& aLoader,
20 nsAString& aErrMsg,
21 txXPathNode** aResult)
22 {
23 NS_ENSURE_ARG_POINTER(aResult);
24 *aResult = nullptr;
25 nsCOMPtr<nsIURI> documentURI;
26 nsresult rv = NS_NewURI(getter_AddRefs(documentURI), aHref);
27 NS_ENSURE_SUCCESS(rv, rv);
28
29 nsIDocument* loaderDocument = txXPathNativeNode::getDocument(aLoader);
30
31 nsCOMPtr<nsILoadGroup> loadGroup = loaderDocument->GetDocumentLoadGroup();
32
33 // For the system principal loaderUri will be null here, which is good
34 // since that means that chrome documents can load any uri.
35
36 // Raw pointer, we want the resulting txXPathNode to hold a reference to
37 // the document.
38 nsIDOMDocument* theDocument = nullptr;
39 nsAutoSyncOperation sync(loaderDocument);
40 rv = nsSyncLoadService::LoadDocument(documentURI,
41 nsIContentPolicy::TYPE_INTERNAL_XMLHTTPREQUEST,
42 loaderDocument->NodePrincipal(),
43 nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS,
44 loadGroup, true,
45 loaderDocument->GetReferrerPolicy(),
46 &theDocument);
47
48 if (NS_FAILED(rv)) {
49 aErrMsg.AppendLiteral("Document load of ");
50 aErrMsg.Append(aHref);
51 aErrMsg.AppendLiteral(" failed.");
52 return NS_FAILED(rv) ? rv : NS_ERROR_FAILURE;
53 }
54
55 *aResult = txXPathNativeNode::createXPathNode(theDocument);
56 if (!*aResult) {
57 NS_RELEASE(theDocument);
58 return NS_ERROR_FAILURE;
59 }
60
61 return NS_OK;
62 }
63