|
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 |
|
6 #include "txURIUtils.h" |
|
7 #include "nsNetUtil.h" |
|
8 #include "nsIDocument.h" |
|
9 #include "nsIPrincipal.h" |
|
10 |
|
11 /** |
|
12 * URIUtils |
|
13 * A set of utilities for handling URIs |
|
14 **/ |
|
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 |
|
40 // static |
|
41 void |
|
42 URIUtils::ResetWithSource(nsIDocument *aNewDoc, nsIDOMNode *aSourceNode) |
|
43 { |
|
44 nsCOMPtr<nsINode> node = do_QueryInterface(aSourceNode); |
|
45 if (!node) { |
|
46 // XXXbz passing nullptr as the first arg to Reset is illegal |
|
47 aNewDoc->Reset(nullptr, nullptr); |
|
48 return; |
|
49 } |
|
50 |
|
51 nsCOMPtr<nsIDocument> sourceDoc = node->OwnerDoc(); |
|
52 nsIPrincipal* sourcePrincipal = sourceDoc->NodePrincipal(); |
|
53 |
|
54 // Copy the channel and loadgroup from the source document. |
|
55 nsCOMPtr<nsILoadGroup> loadGroup = sourceDoc->GetDocumentLoadGroup(); |
|
56 nsCOMPtr<nsIChannel> channel = sourceDoc->GetChannel(); |
|
57 if (!channel) { |
|
58 // Need to synthesize one |
|
59 if (NS_FAILED(NS_NewChannel(getter_AddRefs(channel), |
|
60 sourceDoc->GetDocumentURI(), |
|
61 nullptr, |
|
62 loadGroup))) { |
|
63 return; |
|
64 } |
|
65 channel->SetOwner(sourcePrincipal); |
|
66 } |
|
67 aNewDoc->Reset(channel, loadGroup); |
|
68 aNewDoc->SetPrincipal(sourcePrincipal); |
|
69 aNewDoc->SetBaseURI(sourceDoc->GetDocBaseURI()); |
|
70 |
|
71 // Copy charset |
|
72 aNewDoc->SetDocumentCharacterSetSource( |
|
73 sourceDoc->GetDocumentCharacterSetSource()); |
|
74 aNewDoc->SetDocumentCharacterSet(sourceDoc->GetDocumentCharacterSet()); |
|
75 } |