txURIUtils.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "txURIUtils.h"
  6. #include "nsNetUtil.h"
  7. #include "nsIDocument.h"
  8. #include "nsIHttpChannelInternal.h"
  9. #include "nsIPrincipal.h"
  10. #include "mozilla/LoadInfo.h"
  11. using mozilla::net::LoadInfo;
  12. /**
  13. * URIUtils
  14. * A set of utilities for handling URIs
  15. **/
  16. /**
  17. * Resolves the given href argument, using the given documentBase
  18. * if necessary.
  19. * The new resolved href will be appended to the given dest String
  20. **/
  21. void URIUtils::resolveHref(const nsAString& href, const nsAString& base,
  22. nsAString& dest) {
  23. if (base.IsEmpty()) {
  24. dest.Append(href);
  25. return;
  26. }
  27. if (href.IsEmpty()) {
  28. dest.Append(base);
  29. return;
  30. }
  31. nsCOMPtr<nsIURI> pURL;
  32. nsAutoString resultHref;
  33. nsresult result = NS_NewURI(getter_AddRefs(pURL), base);
  34. if (NS_SUCCEEDED(result)) {
  35. NS_MakeAbsoluteURI(resultHref, href, pURL);
  36. dest.Append(resultHref);
  37. }
  38. } //-- resolveHref
  39. // static
  40. void
  41. URIUtils::ResetWithSource(nsIDocument *aNewDoc, nsINode *aSourceNode)
  42. {
  43. if (!aSourceNode) {
  44. return;
  45. }
  46. nsCOMPtr<nsIDocument> sourceDoc = aSourceNode->OwnerDoc();
  47. nsIPrincipal* sourcePrincipal = sourceDoc->NodePrincipal();
  48. // Copy the channel and loadgroup from the source document.
  49. nsCOMPtr<nsILoadGroup> loadGroup = sourceDoc->GetDocumentLoadGroup();
  50. nsCOMPtr<nsIChannel> channel = sourceDoc->GetChannel();
  51. if (!channel) {
  52. // Need to synthesize one
  53. nsresult rv = NS_NewChannel(getter_AddRefs(channel),
  54. sourceDoc->GetDocumentURI(),
  55. sourceDoc,
  56. nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL,
  57. nsIContentPolicy::TYPE_OTHER,
  58. loadGroup,
  59. nullptr, // aCallbacks
  60. nsIChannel::LOAD_BYPASS_SERVICE_WORKER);
  61. if (NS_FAILED(rv)) {
  62. return;
  63. }
  64. }
  65. aNewDoc->Reset(channel, loadGroup);
  66. aNewDoc->SetPrincipal(sourcePrincipal);
  67. aNewDoc->SetBaseURI(sourceDoc->GetDocBaseURI());
  68. aNewDoc->SetSandboxFlags(sourceDoc->GetSandboxFlags());
  69. // Copy charset
  70. aNewDoc->SetDocumentCharacterSetSource(
  71. sourceDoc->GetDocumentCharacterSetSource());
  72. aNewDoc->SetDocumentCharacterSet(sourceDoc->GetDocumentCharacterSet());
  73. }