no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / dom / xslt / base / txURIUtils.cpp
blobbbb06221cfa233a2f703db1c5c7d370e2483cd6b
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"
7 #include "nsNetUtil.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;
16 /**
17 * URIUtils
18 * A set of utilities for handling URIs
19 **/
21 /**
22 * Resolves the given href argument, using the given documentBase
23 * if necessary.
24 * The new resolved href will be appended to the given dest String
25 **/
26 void URIUtils::resolveHref(const nsAString& href, const nsAString& base,
27 nsAString& dest) {
28 if (base.IsEmpty()) {
29 dest.Append(href);
30 return;
32 if (href.IsEmpty()) {
33 dest.Append(base);
34 return;
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);
43 } //-- resolveHref
45 // static
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();
54 if (!channel) {
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
60 loadGroup,
61 nullptr, // aCallbacks
62 nsIChannel::LOAD_BYPASS_SERVICE_WORKER);
64 if (NS_FAILED(rv)) {
65 return;
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();
78 if (csp) {
79 RefPtr<nsCSPContext> cspToInherit = new nsCSPContext();
80 cspToInherit->InitFromOther(static_cast<nsCSPContext*>(csp.get()));
81 aNewDoc->SetCsp(cspToInherit);
83 // Copy charset
84 aNewDoc->SetDocumentCharacterSetSource(
85 sourceDoc->GetDocumentCharacterSetSource());
86 aNewDoc->SetDocumentCharacterSet(sourceDoc->GetDocumentCharacterSet());