|
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 "nsIPrincipal.h" |
|
15 |
|
16 nsresult |
|
17 txParseDocumentFromURI(const nsAString& aHref, const txXPathNode& aLoader, |
|
18 nsAString& aErrMsg, txXPathNode** aResult) |
|
19 { |
|
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 nsIDocument* 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 nsIDOMDocument* theDocument = nullptr; |
|
36 nsAutoSyncOperation sync(loaderDocument); |
|
37 rv = nsSyncLoadService::LoadDocument(documentURI, |
|
38 loaderDocument->NodePrincipal(), |
|
39 loadGroup, true, &theDocument); |
|
40 |
|
41 if (NS_FAILED(rv)) { |
|
42 aErrMsg.Append(NS_LITERAL_STRING("Document load of ") + |
|
43 aHref + NS_LITERAL_STRING(" failed.")); |
|
44 return NS_FAILED(rv) ? rv : NS_ERROR_FAILURE; |
|
45 } |
|
46 |
|
47 *aResult = txXPathNativeNode::createXPathNode(theDocument); |
|
48 if (!*aResult) { |
|
49 NS_RELEASE(theDocument); |
|
50 return NS_ERROR_FAILURE; |
|
51 } |
|
52 |
|
53 return NS_OK; |
|
54 } |