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