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/. */
6 #include "txURIUtils.h"
8 #include "mozilla/dom/Document.h"
9 #include "nsIPrincipal.h"
10 #include "mozilla/LoadInfo.h"
11 #include "mozilla/dom/nsCSPContext.h"
13 using mozilla::dom::Document
;
14 using mozilla::net::LoadInfo
;
18 * A set of utilities for handling URIs
22 * Resolves the given href argument, using the given documentBase
24 * The new resolved href will be appended to the given dest String
26 void URIUtils::resolveHref(const nsAString
& href
, const nsAString
& base
,
36 nsCOMPtr
<nsIURI
> pURL
;
37 nsAutoString resultHref
;
38 nsresult result
= NS_NewURI(getter_AddRefs(pURL
), base
);
39 if (NS_SUCCEEDED(result
)) {
40 NS_MakeAbsoluteURI(resultHref
, href
, pURL
);
41 dest
.Append(resultHref
);
46 void URIUtils::ResetWithSource(Document
* aNewDoc
, nsINode
* aSourceNode
) {
47 nsCOMPtr
<Document
> sourceDoc
= aSourceNode
->OwnerDoc();
48 nsIPrincipal
* sourcePrincipal
= sourceDoc
->NodePrincipal();
49 nsIPrincipal
* sourcePartitionedPrincipal
= sourceDoc
->PartitionedPrincipal();
51 // Copy the channel and loadgroup from the source document.
52 nsCOMPtr
<nsILoadGroup
> loadGroup
= sourceDoc
->GetDocumentLoadGroup();
53 nsCOMPtr
<nsIChannel
> channel
= sourceDoc
->GetChannel();
55 // Need to synthesize one
56 nsresult rv
= NS_NewChannel(
57 getter_AddRefs(channel
), sourceDoc
->GetDocumentURI(), sourceDoc
,
58 nsILoadInfo::SEC_FORCE_INHERIT_PRINCIPAL
, nsIContentPolicy::TYPE_OTHER
,
59 nullptr, // aPerformanceStorage
61 nullptr, // aCallbacks
62 nsIChannel::LOAD_BYPASS_SERVICE_WORKER
);
69 aNewDoc
->Reset(channel
, loadGroup
);
70 aNewDoc
->SetPrincipals(sourcePrincipal
, sourcePartitionedPrincipal
);
71 aNewDoc
->SetBaseURI(sourceDoc
->GetDocBaseURI());
72 aNewDoc
->SetSandboxFlags(sourceDoc
->GetSandboxFlags());
73 aNewDoc
->SetReferrerInfo(sourceDoc
->GetReferrerInfo());
74 aNewDoc
->SetEmbedderPolicy(sourceDoc
->GetEmbedderPolicy());
76 // Inherit the csp if there is one
77 nsCOMPtr
<nsIContentSecurityPolicy
> csp
= sourceDoc
->GetCsp();
79 RefPtr
<nsCSPContext
> cspToInherit
= new nsCSPContext();
80 cspToInherit
->InitFromOther(static_cast<nsCSPContext
*>(csp
.get()));
81 aNewDoc
->SetCsp(cspToInherit
);
84 aNewDoc
->SetDocumentCharacterSetSource(
85 sourceDoc
->GetDocumentCharacterSetSource());
86 aNewDoc
->SetDocumentCharacterSet(sourceDoc
->GetDocumentCharacterSet());