txURIUtils.cpp (3233B)
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 "txURIUtils.h" 7 8 #include "mozilla/LoadInfo.h" 9 #include "mozilla/dom/Document.h" 10 #include "mozilla/dom/PolicyContainer.h" 11 #include "mozilla/dom/nsCSPContext.h" 12 #include "nsIPrincipal.h" 13 #include "nsNetUtil.h" 14 15 using mozilla::dom::Document; 16 using mozilla::net::LoadInfo; 17 18 /** 19 * URIUtils 20 * A set of utilities for handling URIs 21 **/ 22 23 /** 24 * Resolves the given href argument, using the given documentBase 25 * if necessary. 26 * The new resolved href will be appended to the given dest String 27 **/ 28 void URIUtils::resolveHref(const nsAString& href, const nsAString& base, 29 nsAString& dest) { 30 if (base.IsEmpty()) { 31 dest.Append(href); 32 return; 33 } 34 if (href.IsEmpty()) { 35 dest.Append(base); 36 return; 37 } 38 nsCOMPtr<nsIURI> pURL; 39 nsAutoString resultHref; 40 nsresult result = NS_NewURI(getter_AddRefs(pURL), base); 41 if (NS_SUCCEEDED(result)) { 42 NS_MakeAbsoluteURI(resultHref, href, pURL); 43 dest.Append(resultHref); 44 } 45 } //-- resolveHref 46 47 // static 48 void URIUtils::ResetWithSource(Document* aNewDoc, nsINode* aSourceNode) { 49 nsCOMPtr<Document> sourceDoc = aSourceNode->OwnerDoc(); 50 nsIPrincipal* sourcePrincipal = sourceDoc->NodePrincipal(); 51 nsIPrincipal* sourcePartitionedPrincipal = sourceDoc->PartitionedPrincipal(); 52 53 // Copy the channel and loadgroup from the source document. 54 nsCOMPtr<nsILoadGroup> loadGroup = sourceDoc->GetDocumentLoadGroup(); 55 nsCOMPtr<nsIChannel> channel = sourceDoc->GetChannel(); 56 if (!channel) { 57 // Need to synthesize one 58 nsresult rv = NS_NewChannel( 59 getter_AddRefs(channel), sourceDoc->GetDocumentURI(), sourceDoc, 60 nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL, nsIContentPolicy::TYPE_OTHER, 61 nullptr, // aPerformanceStorage 62 loadGroup, 63 nullptr, // aCallbacks 64 nsIChannel::LOAD_BYPASS_SERVICE_WORKER); 65 66 if (NS_FAILED(rv)) { 67 return; 68 } 69 } 70 71 aNewDoc->Reset(channel, loadGroup); 72 aNewDoc->SetPrincipals(sourcePrincipal, sourcePartitionedPrincipal); 73 aNewDoc->SetBaseURI(sourceDoc->GetDocBaseURI()); 74 aNewDoc->SetSandboxFlags(sourceDoc->GetSandboxFlags()); 75 aNewDoc->SetReferrerInfo(sourceDoc->GetReferrerInfo()); 76 aNewDoc->SetEmbedderPolicy(sourceDoc->GetEmbedderPolicy()); 77 78 // Inherit the policyContainer if there is one 79 nsCOMPtr<nsIPolicyContainer> policyContainer = 80 sourceDoc->GetPolicyContainer(); 81 if (policyContainer) { 82 RefPtr<PolicyContainer> policyContainerToInherit = new PolicyContainer(); 83 policyContainerToInherit->InitFromOther( 84 PolicyContainer::Cast(policyContainer.get())); 85 aNewDoc->SetPolicyContainer(policyContainerToInherit); 86 } 87 // Copy charset 88 aNewDoc->SetDocumentCharacterSetSource( 89 sourceDoc->GetDocumentCharacterSetSource()); 90 aNewDoc->SetDocumentCharacterSet(sourceDoc->GetDocumentCharacterSet()); 91 92 // Inherit the CookieJarSettings from the source document. 93 aNewDoc->SetCookieJarSettings(sourceDoc->CookieJarSettings()); 94 }