txXMLParser.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. #include "txXMLParser.h"
  6. #include "txURIUtils.h"
  7. #include "txXPathTreeWalker.h"
  8. #include "nsIDocument.h"
  9. #include "nsIDOMDocument.h"
  10. #include "nsSyncLoadService.h"
  11. #include "nsNetUtil.h"
  12. #include "nsIURI.h"
  13. #include "nsIPrincipal.h"
  14. nsresult
  15. txParseDocumentFromURI(const nsAString& aHref,
  16. const txXPathNode& aLoader,
  17. nsAString& aErrMsg,
  18. 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. nsIDocument* loaderDocument = txXPathNativeNode::getDocument(aLoader);
  26. nsCOMPtr<nsILoadGroup> loadGroup = loaderDocument->GetDocumentLoadGroup();
  27. // For the system principal loaderUri will be null here, which is good
  28. // since that means that chrome documents can load any uri.
  29. // Raw pointer, we want the resulting txXPathNode to hold a reference to
  30. // the document.
  31. nsIDOMDocument* theDocument = nullptr;
  32. nsAutoSyncOperation sync(loaderDocument);
  33. rv = nsSyncLoadService::LoadDocument(documentURI,
  34. nsIContentPolicy::TYPE_INTERNAL_XMLHTTPREQUEST,
  35. loaderDocument->NodePrincipal(),
  36. nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS,
  37. loadGroup, true,
  38. loaderDocument->GetReferrerPolicy(),
  39. &theDocument);
  40. if (NS_FAILED(rv)) {
  41. aErrMsg.AppendLiteral("Document load of ");
  42. aErrMsg.Append(aHref);
  43. aErrMsg.AppendLiteral(" failed.");
  44. return NS_FAILED(rv) ? rv : NS_ERROR_FAILURE;
  45. }
  46. *aResult = txXPathNativeNode::createXPathNode(theDocument);
  47. if (!*aResult) {
  48. NS_RELEASE(theDocument);
  49. return NS_ERROR_FAILURE;
  50. }
  51. return NS_OK;
  52. }