michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "txURIUtils.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIPrincipal.h" michael@0: michael@0: /** michael@0: * URIUtils michael@0: * A set of utilities for handling URIs michael@0: **/ michael@0: michael@0: /** michael@0: * Resolves the given href argument, using the given documentBase michael@0: * if necessary. michael@0: * The new resolved href will be appended to the given dest String michael@0: **/ michael@0: void URIUtils::resolveHref(const nsAString& href, const nsAString& base, michael@0: nsAString& dest) { michael@0: if (base.IsEmpty()) { michael@0: dest.Append(href); michael@0: return; michael@0: } michael@0: if (href.IsEmpty()) { michael@0: dest.Append(base); michael@0: return; michael@0: } michael@0: nsCOMPtr pURL; michael@0: nsAutoString resultHref; michael@0: nsresult result = NS_NewURI(getter_AddRefs(pURL), base); michael@0: if (NS_SUCCEEDED(result)) { michael@0: NS_MakeAbsoluteURI(resultHref, href, pURL); michael@0: dest.Append(resultHref); michael@0: } michael@0: } //-- resolveHref michael@0: michael@0: // static michael@0: void michael@0: URIUtils::ResetWithSource(nsIDocument *aNewDoc, nsIDOMNode *aSourceNode) michael@0: { michael@0: nsCOMPtr node = do_QueryInterface(aSourceNode); michael@0: if (!node) { michael@0: // XXXbz passing nullptr as the first arg to Reset is illegal michael@0: aNewDoc->Reset(nullptr, nullptr); michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr sourceDoc = node->OwnerDoc(); michael@0: nsIPrincipal* sourcePrincipal = sourceDoc->NodePrincipal(); michael@0: michael@0: // Copy the channel and loadgroup from the source document. michael@0: nsCOMPtr loadGroup = sourceDoc->GetDocumentLoadGroup(); michael@0: nsCOMPtr channel = sourceDoc->GetChannel(); michael@0: if (!channel) { michael@0: // Need to synthesize one michael@0: if (NS_FAILED(NS_NewChannel(getter_AddRefs(channel), michael@0: sourceDoc->GetDocumentURI(), michael@0: nullptr, michael@0: loadGroup))) { michael@0: return; michael@0: } michael@0: channel->SetOwner(sourcePrincipal); michael@0: } michael@0: aNewDoc->Reset(channel, loadGroup); michael@0: aNewDoc->SetPrincipal(sourcePrincipal); michael@0: aNewDoc->SetBaseURI(sourceDoc->GetDocBaseURI()); michael@0: michael@0: // Copy charset michael@0: aNewDoc->SetDocumentCharacterSetSource( michael@0: sourceDoc->GetDocumentCharacterSetSource()); michael@0: aNewDoc->SetDocumentCharacterSet(sourceDoc->GetDocumentCharacterSet()); michael@0: }