gecko-dev/dom/base/nsLocation.cpp

992 lines
24 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
2012-05-21 11:12:37 +00:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
1998-08-13 04:34:53 +00:00
#include "nsLocation.h"
#include "nsIScriptSecurityManager.h"
#include "nsIScriptObjectPrincipal.h"
#include "nsIScriptContext.h"
#include "nsIDocShell.h"
#include "nsIDocShellLoadInfo.h"
#include "nsIWebNavigation.h"
#include "nsCDefaultURIFixup.h"
#include "nsIURIFixup.h"
1998-08-13 04:34:53 +00:00
#include "nsIURL.h"
#include "nsIJARURI.h"
#include "nsNetUtil.h"
#include "nsCOMPtr.h"
#include "nsEscape.h"
#include "nsIDOMWindow.h"
#include "nsIDocument.h"
#include "nsIPresShell.h"
#include "nsPresContext.h"
#include "nsError.h"
#include "nsDOMClassInfoID.h"
#include "nsReadableUtils.h"
#include "nsITextToSubURI.h"
#include "nsJSUtils.h"
#include "nsContentUtils.h"
#include "nsGlobalWindow.h"
#include "mozilla/Likely.h"
#include "nsCycleCollectionParticipant.h"
#include "nsNullPrincipal.h"
#include "ScriptSettings.h"
#include "mozilla/dom/LocationBinding.h"
using namespace mozilla;
using namespace mozilla::dom;
static nsresult
GetDocumentCharacterSetForURI(const nsAString& aHref, nsACString& aCharset)
{
aCharset.Truncate();
if (nsIDocument* doc = GetEntryDocument()) {
aCharset = doc->GetDocumentCharacterSet();
}
return NS_OK;
}
1998-08-13 04:34:53 +00:00
nsLocation::nsLocation(nsPIDOMWindow* aWindow, nsIDocShell *aDocShell)
: mInnerWindow(aWindow)
1998-08-13 04:34:53 +00:00
{
MOZ_ASSERT(aDocShell);
MOZ_ASSERT(mInnerWindow->IsInnerWindow());
mDocShell = do_GetWeakReference(aDocShell);
1998-08-13 04:34:53 +00:00
}
nsLocation::~nsLocation()
1998-08-13 04:34:53 +00:00
{
}
// QueryInterface implementation for nsLocation
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsLocation)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsIDOMLocation)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMLocation)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_CLASS(nsLocation)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsLocation)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mInnerWindow);
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsLocation)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInnerWindow)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(nsLocation)
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsLocation)
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsLocation)
1998-08-13 04:34:53 +00:00
void
nsLocation::SetDocShell(nsIDocShell *aDocShell)
1998-08-13 04:34:53 +00:00
{
mDocShell = do_GetWeakReference(aDocShell);
}
nsIDocShell *
nsLocation::GetDocShell()
{
nsCOMPtr<nsIDocShell> docshell(do_QueryReferent(mDocShell));
return docshell;
1998-08-13 04:34:53 +00:00
}
nsresult
nsLocation::CheckURL(nsIURI* aURI, nsIDocShellLoadInfo** aLoadInfo)
{
*aLoadInfo = nullptr;
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
NS_ENSURE_TRUE(docShell, NS_ERROR_NOT_AVAILABLE);
nsCOMPtr<nsISupports> owner;
nsCOMPtr<nsIURI> sourceURI;
net::ReferrerPolicy referrerPolicy = net::RP_Default;
if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) {
// No cx means that there's no JS running, or at least no JS that
// was run through code that properly pushed a context onto the
// context stack (as all code that runs JS off of web pages
// does). We won't bother with security checks in this case, but
// we need to create the loadinfo etc.
// Get security manager.
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
NS_ENSURE_STATE(ssm);
// Check to see if URI is allowed.
nsresult rv = ssm->CheckLoadURIFromScript(cx, aURI);
NS_ENSURE_SUCCESS(rv, rv);
// Make the load's referrer reflect changes to the document's URI caused by
// push/replaceState, if possible. First, get the document corresponding to
// fp. If the document's original URI (i.e. its URI before
// push/replaceState) matches the principal's URI, use the document's
// current URI as the referrer. If they don't match, use the principal's
// URI.
nsCOMPtr<nsIDocument> doc;
nsCOMPtr<nsIURI> docOriginalURI, docCurrentURI, principalURI;
nsCOMPtr<nsPIDOMWindow> incumbent =
do_QueryInterface(mozilla::dom::GetIncumbentGlobal());
if (incumbent) {
doc = incumbent->GetDoc();
}
if (doc) {
docOriginalURI = doc->GetOriginalURI();
docCurrentURI = doc->GetDocumentURI();
rv = doc->NodePrincipal()->GetURI(getter_AddRefs(principalURI));
NS_ENSURE_SUCCESS(rv, rv);
referrerPolicy = doc->GetReferrerPolicy();
}
bool urisEqual = false;
if (docOriginalURI && docCurrentURI && principalURI) {
principalURI->Equals(docOriginalURI, &urisEqual);
}
if (urisEqual) {
sourceURI = docCurrentURI;
}
else {
// Use principalURI as long as it is not an nsNullPrincipalURI.
// We could add a method such as GetReferrerURI to principals to make this
// cleaner, but given that we need to start using Source Browsing Context
// for referrer (see Bug 960639) this may be wasted effort at this stage.
if (principalURI) {
bool isNullPrincipalScheme;
rv = principalURI->SchemeIs(NS_NULLPRINCIPAL_SCHEME,
&isNullPrincipalScheme);
if (NS_SUCCEEDED(rv) && !isNullPrincipalScheme) {
sourceURI = principalURI;
}
}
}
owner = nsContentUtils::SubjectPrincipal();
}
2000-08-11 04:31:08 +00:00
// Create load info
nsCOMPtr<nsIDocShellLoadInfo> loadInfo;
docShell->CreateLoadInfo(getter_AddRefs(loadInfo));
2000-08-11 04:31:08 +00:00
NS_ENSURE_TRUE(loadInfo, NS_ERROR_FAILURE);
loadInfo->SetOwner(owner);
if (sourceURI) {
loadInfo->SetReferrer(sourceURI);
loadInfo->SetReferrerPolicy(referrerPolicy);
}
loadInfo.swap(*aLoadInfo);
return NS_OK;
}
nsresult
nsLocation::GetURI(nsIURI** aURI, bool aGetInnermostURI)
{
*aURI = nullptr;
nsresult rv;
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(docShell, &rv));
if (NS_FAILED(rv)) {
return rv;
}
nsCOMPtr<nsIURI> uri;
rv = webNav->GetCurrentURI(getter_AddRefs(uri));
NS_ENSURE_SUCCESS(rv, rv);
2003-01-05 06:34:53 +00:00
// It is valid for docshell to return a null URI. Don't try to fixup
// if this happens.
if (!uri) {
return NS_OK;
}
if (aGetInnermostURI) {
nsCOMPtr<nsIJARURI> jarURI(do_QueryInterface(uri));
while (jarURI) {
jarURI->GetJARFile(getter_AddRefs(uri));
jarURI = do_QueryInterface(uri);
}
}
NS_ASSERTION(uri, "nsJARURI screwed up?");
nsCOMPtr<nsIURIFixup> urifixup(do_GetService(NS_URIFIXUP_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
return urifixup->CreateExposableURI(uri, aURI);
}
nsresult
nsLocation::GetWritableURI(nsIURI** aURI)
{
*aURI = nullptr;
nsCOMPtr<nsIURI> uri;
nsresult rv = GetURI(getter_AddRefs(uri));
if (NS_FAILED(rv) || !uri) {
return rv;
}
return uri->Clone(aURI);
}
nsresult
nsLocation::SetURI(nsIURI* aURI, bool aReplace)
1998-08-13 04:34:53 +00:00
{
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
if (docShell) {
nsCOMPtr<nsIDocShellLoadInfo> loadInfo;
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(docShell));
if(NS_FAILED(CheckURL(aURI, getter_AddRefs(loadInfo))))
return NS_ERROR_FAILURE;
if (aReplace) {
loadInfo->SetLoadType(nsIDocShellLoadInfo::loadStopContentAndReplace);
} else {
loadInfo->SetLoadType(nsIDocShellLoadInfo::loadStopContent);
}
// Get the incumbent script's browsing context to set as source.
nsCOMPtr<nsPIDOMWindow> sourceWindow =
do_QueryInterface(mozilla::dom::GetIncumbentGlobal());
if (sourceWindow) {
loadInfo->SetSourceDocShell(sourceWindow->GetDocShell());
}
return docShell->LoadURI(aURI, loadInfo,
nsIWebNavigation::LOAD_FLAGS_NONE, true);
}
return NS_OK;
1998-08-13 04:34:53 +00:00
}
NS_IMETHODIMP
nsLocation::GetHash(nsAString& aHash)
1998-08-13 04:34:53 +00:00
{
aHash.SetLength(0);
nsCOMPtr<nsIURI> uri;
nsresult rv = GetURI(getter_AddRefs(uri));
if (NS_FAILED(rv) || !uri) {
return rv;
}
nsAutoCString ref;
nsAutoString unicodeRef;
rv = uri->GetRef(ref);
if (nsContentUtils::GettersDecodeURLHash()) {
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsITextToSubURI> textToSubURI(
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv));
if (NS_SUCCEEDED(rv)) {
nsAutoCString charset;
uri->GetOriginCharset(charset);
rv = textToSubURI->UnEscapeURIForUI(charset, ref, unicodeRef);
}
if (NS_FAILED(rv)) {
// Oh, well. No intl here!
NS_UnescapeURL(ref);
CopyASCIItoUTF16(ref, unicodeRef);
rv = NS_OK;
}
}
if (NS_SUCCEEDED(rv) && !unicodeRef.IsEmpty()) {
aHash.Assign(char16_t('#'));
aHash.Append(unicodeRef);
}
} else { // URL Hash should simply return the value of the Ref segment
if (NS_SUCCEEDED(rv) && !ref.IsEmpty()) {
aHash.Assign(char16_t('#'));
AppendUTF8toUTF16(ref, aHash);
}
}
if (aHash == mCachedHash) {
// Work around ShareThis stupidly polling location.hash every
// 5ms all the time by handing out the same exact string buffer
// we handed out last time.
aHash = mCachedHash;
} else {
mCachedHash = aHash;
1998-08-13 04:34:53 +00:00
}
return rv;
1998-08-13 04:34:53 +00:00
}
NS_IMETHODIMP
nsLocation::SetHash(const nsAString& aHash)
1998-08-13 04:34:53 +00:00
{
nsCOMPtr<nsIURI> uri;
nsresult rv = GetWritableURI(getter_AddRefs(uri));
if (NS_FAILED(rv) || !uri) {
return rv;
}
NS_ConvertUTF16toUTF8 hash(aHash);
if (hash.IsEmpty() || hash.First() != char16_t('#')) {
hash.Insert(char16_t('#'), 0);
}
rv = uri->SetRef(hash);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
1998-08-13 04:34:53 +00:00
}
return SetURI(uri);
1998-08-13 04:34:53 +00:00
}
NS_IMETHODIMP
nsLocation::GetHost(nsAString& aHost)
1998-08-13 04:34:53 +00:00
{
aHost.Truncate();
nsCOMPtr<nsIURI> uri;
nsresult result;
result = GetURI(getter_AddRefs(uri), true);
if (uri) {
nsAutoCString hostport;
result = uri->GetHostPort(hostport);
if (NS_SUCCEEDED(result)) {
AppendUTF8toUTF16(hostport, aHost);
}
1998-08-13 04:34:53 +00:00
}
return NS_OK;
1998-08-13 04:34:53 +00:00
}
NS_IMETHODIMP
nsLocation::SetHost(const nsAString& aHost)
1998-08-13 04:34:53 +00:00
{
nsCOMPtr<nsIURI> uri;
nsresult rv = GetWritableURI(getter_AddRefs(uri));
if (NS_WARN_IF(NS_FAILED(rv) || !uri)) {
return rv;
}
rv = uri->SetHostPort(NS_ConvertUTF16toUTF8(aHost));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
1998-08-13 04:34:53 +00:00
}
return SetURI(uri);
1998-08-13 04:34:53 +00:00
}
NS_IMETHODIMP
nsLocation::GetHostname(nsAString& aHostname)
1998-08-13 04:34:53 +00:00
{
aHostname.Truncate();
nsCOMPtr<nsIURI> uri;
GetURI(getter_AddRefs(uri), true);
if (uri) {
nsContentUtils::GetHostOrIPv6WithBrackets(uri, aHostname);
1998-08-13 04:34:53 +00:00
}
return NS_OK;
1998-08-13 04:34:53 +00:00
}
NS_IMETHODIMP
nsLocation::SetHostname(const nsAString& aHostname)
1998-08-13 04:34:53 +00:00
{
nsCOMPtr<nsIURI> uri;
nsresult rv = GetWritableURI(getter_AddRefs(uri));
if (NS_WARN_IF(NS_FAILED(rv) || !uri)) {
return rv;
}
rv = uri->SetHost(NS_ConvertUTF16toUTF8(aHostname));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
1998-08-13 04:34:53 +00:00
}
return SetURI(uri);
1998-08-13 04:34:53 +00:00
}
NS_IMETHODIMP
nsLocation::GetHref(nsAString& aHref)
1998-08-13 04:34:53 +00:00
{
aHref.Truncate();
nsCOMPtr<nsIURI> uri;
nsresult result;
result = GetURI(getter_AddRefs(uri));
if (uri) {
nsAutoCString uriString;
result = uri->GetSpec(uriString);
if (NS_SUCCEEDED(result)) {
AppendUTF8toUTF16(uriString, aHref);
}
1998-08-13 04:34:53 +00:00
}
return result;
}
NS_IMETHODIMP
nsLocation::SetHref(const nsAString& aHref)
1998-08-13 04:34:53 +00:00
{
nsAutoString oldHref;
nsresult rv = NS_OK;
JSContext *cx = nsContentUtils::GetCurrentJSContext();
if (cx) {
rv = SetHrefWithContext(cx, aHref, false);
} else {
rv = GetHref(oldHref);
if (NS_SUCCEEDED(rv)) {
nsCOMPtr<nsIURI> oldUri;
rv = NS_NewURI(getter_AddRefs(oldUri), oldHref);
if (oldUri) {
rv = SetHrefWithBase(aHref, oldUri, false);
}
}
}
return rv;
}
nsresult
nsLocation::SetHrefWithContext(JSContext* cx, const nsAString& aHref,
bool aReplace)
{
nsCOMPtr<nsIURI> base;
// Get the source of the caller
nsresult result = GetSourceBaseURL(cx, getter_AddRefs(base));
if (NS_FAILED(result)) {
return result;
}
return SetHrefWithBase(aHref, base, aReplace);
}
nsresult
nsLocation::SetHrefWithBase(const nsAString& aHref, nsIURI* aBase,
bool aReplace)
{
nsresult result;
nsCOMPtr<nsIURI> newUri;
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
nsAutoCString docCharset;
if (NS_SUCCEEDED(GetDocumentCharacterSetForURI(aHref, docCharset)))
result = NS_NewURI(getter_AddRefs(newUri), aHref, docCharset.get(), aBase);
else
result = NS_NewURI(getter_AddRefs(newUri), aHref, nullptr, aBase);
if (newUri) {
/* Check with the scriptContext if it is currently processing a script tag.
* If so, this must be a <script> tag with a location.href in it.
* we want to do a replace load, in such a situation.
* In other cases, for example if a event handler or a JS timer
* had a location.href in it, we want to do a normal load,
* so that the new url will be appended to Session History.
* This solution is tricky. Hopefully it isn't going to bite
* anywhere else. This is part of solution for bug # 39938, 72197
*
*/
bool inScriptTag = false;
nsIScriptContext* scriptContext = nullptr;
nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(GetEntryGlobal());
if (win) {
scriptContext = static_cast<nsGlobalWindow*>(win.get())->GetContextInternal();
}
if (scriptContext) {
if (scriptContext->GetProcessingScriptTag()) {
// Now check to make sure that the script is running in our window,
// since we only want to replace if the location is set by a
// <script> tag in the same window. See bug 178729.
nsCOMPtr<nsIScriptGlobalObject> ourGlobal =
docShell ? docShell->GetScriptGlobalObject() : nullptr;
inScriptTag = (ourGlobal == scriptContext->GetGlobalObject());
}
}
return SetURI(newUri, aReplace || inScriptTag);
}
return result;
1998-08-13 04:34:53 +00:00
}
NS_IMETHODIMP
nsLocation::GetOrigin(nsAString& aOrigin)
{
aOrigin.Truncate();
nsCOMPtr<nsIURI> uri;
nsresult rv = GetURI(getter_AddRefs(uri), true);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(uri, NS_OK);
nsAutoString origin;
rv = nsContentUtils::GetUTFOrigin(uri, origin);
NS_ENSURE_SUCCESS(rv, rv);
aOrigin = origin;
return NS_OK;
}
NS_IMETHODIMP
nsLocation::GetPathname(nsAString& aPathname)
1998-08-13 04:34:53 +00:00
{
aPathname.Truncate();
nsCOMPtr<nsIURI> uri;
1998-08-13 04:34:53 +00:00
nsresult result = NS_OK;
result = GetURI(getter_AddRefs(uri));
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
if (url) {
nsAutoCString file;
result = url->GetFilePath(file);
if (NS_SUCCEEDED(result)) {
AppendUTF8toUTF16(file, aPathname);
1998-08-13 04:34:53 +00:00
}
}
return result;
}
NS_IMETHODIMP
nsLocation::SetPathname(const nsAString& aPathname)
1998-08-13 04:34:53 +00:00
{
nsCOMPtr<nsIURI> uri;
nsresult rv = GetWritableURI(getter_AddRefs(uri));
if (NS_WARN_IF(NS_FAILED(rv) || !uri)) {
return rv;
}
rv = uri->SetPath(NS_ConvertUTF16toUTF8(aPathname));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
1998-08-13 04:34:53 +00:00
}
return SetURI(uri);
1998-08-13 04:34:53 +00:00
}
NS_IMETHODIMP
nsLocation::GetPort(nsAString& aPort)
1998-08-13 04:34:53 +00:00
{
aPort.SetLength(0);
nsCOMPtr<nsIURI> uri;
1998-08-13 04:34:53 +00:00
nsresult result = NS_OK;
result = GetURI(getter_AddRefs(uri), true);
if (uri) {
int32_t port;
result = uri->GetPort(&port);
if (NS_SUCCEEDED(result) && -1 != port) {
nsAutoString portStr;
portStr.AppendInt(port);
aPort.Append(portStr);
1998-08-13 04:34:53 +00:00
}
// Don't propagate this exception to caller
result = NS_OK;
1998-08-13 04:34:53 +00:00
}
return result;
}
NS_IMETHODIMP
nsLocation::SetPort(const nsAString& aPort)
1998-08-13 04:34:53 +00:00
{
nsCOMPtr<nsIURI> uri;
nsresult rv = GetWritableURI(getter_AddRefs(uri));
if (NS_WARN_IF(NS_FAILED(rv) || !uri)) {
return rv;
}
// perhaps use nsReadingIterators at some point?
NS_ConvertUTF16toUTF8 portStr(aPort);
const char *buf = portStr.get();
int32_t port = -1;
if (!portStr.IsEmpty() && buf) {
if (*buf == ':') {
port = atol(buf+1);
}
else {
port = atol(buf);
}
1998-08-13 04:34:53 +00:00
}
rv = uri->SetPort(port);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return SetURI(uri);
1998-08-13 04:34:53 +00:00
}
NS_IMETHODIMP
nsLocation::GetProtocol(nsAString& aProtocol)
1998-08-13 04:34:53 +00:00
{
aProtocol.SetLength(0);
nsCOMPtr<nsIURI> uri;
1998-08-13 04:34:53 +00:00
nsresult result = NS_OK;
result = GetURI(getter_AddRefs(uri));
if (uri) {
nsAutoCString protocol;
result = uri->GetScheme(protocol);
if (NS_SUCCEEDED(result)) {
CopyASCIItoUTF16(protocol, aProtocol);
aProtocol.Append(char16_t(':'));
1998-08-13 04:34:53 +00:00
}
}
return result;
}
NS_IMETHODIMP
nsLocation::SetProtocol(const nsAString& aProtocol)
1998-08-13 04:34:53 +00:00
{
nsCOMPtr<nsIURI> uri;
nsresult rv = GetWritableURI(getter_AddRefs(uri));
if (NS_WARN_IF(NS_FAILED(rv) || !uri)) {
return rv;
}
rv = uri->SetScheme(NS_ConvertUTF16toUTF8(aProtocol));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
1998-08-13 04:34:53 +00:00
}
return SetURI(uri);
1998-08-13 04:34:53 +00:00
}
void
nsLocation::GetUsername(nsAString& aUsername, ErrorResult& aError)
{
THROW_AND_RETURN_IF_CALLER_DOESNT_SUBSUME();
aUsername.Truncate();
nsCOMPtr<nsIURI> uri;
nsresult result = GetURI(getter_AddRefs(uri));
if (uri) {
nsAutoCString username;
result = uri->GetUsername(username);
if (NS_SUCCEEDED(result)) {
CopyUTF8toUTF16(username, aUsername);
}
}
}
void
nsLocation::SetUsername(const nsAString& aUsername, ErrorResult& aError)
{
THROW_AND_RETURN_IF_CALLER_DOESNT_SUBSUME();
nsCOMPtr<nsIURI> uri;
nsresult rv = GetWritableURI(getter_AddRefs(uri));
if (NS_WARN_IF(NS_FAILED(rv))) {
aError.Throw(rv);
return;
}
if (!uri) {
return;
}
rv = uri->SetUsername(NS_ConvertUTF16toUTF8(aUsername));
if (NS_WARN_IF(NS_FAILED(rv))) {
aError.Throw(rv);
return;
}
rv = SetURI(uri);
}
void
nsLocation::GetPassword(nsAString& aPassword, ErrorResult& aError)
{
THROW_AND_RETURN_IF_CALLER_DOESNT_SUBSUME();
aPassword.Truncate();
nsCOMPtr<nsIURI> uri;
nsresult result = GetURI(getter_AddRefs(uri));
if (uri) {
nsAutoCString password;
result = uri->GetPassword(password);
if (NS_SUCCEEDED(result)) {
CopyUTF8toUTF16(password, aPassword);
}
}
}
void
nsLocation::SetPassword(const nsAString& aPassword, ErrorResult& aError)
{
THROW_AND_RETURN_IF_CALLER_DOESNT_SUBSUME();
nsCOMPtr<nsIURI> uri;
nsresult rv = GetWritableURI(getter_AddRefs(uri));
if (NS_WARN_IF(NS_FAILED(rv))) {
aError.Throw(rv);
return;
}
if (!uri) {
return;
}
rv = uri->SetPassword(NS_ConvertUTF16toUTF8(aPassword));
if (NS_WARN_IF(NS_FAILED(rv))) {
aError.Throw(rv);
return;
}
rv = SetURI(uri);
}
NS_IMETHODIMP
nsLocation::GetSearch(nsAString& aSearch)
1998-08-13 04:34:53 +00:00
{
aSearch.SetLength(0);
nsCOMPtr<nsIURI> uri;
1998-08-13 04:34:53 +00:00
nsresult result = NS_OK;
result = GetURI(getter_AddRefs(uri));
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
if (url) {
nsAutoCString search;
result = url->GetQuery(search);
if (NS_SUCCEEDED(result) && !search.IsEmpty()) {
aSearch.Assign(char16_t('?'));
AppendUTF8toUTF16(search, aSearch);
}
1998-08-13 04:34:53 +00:00
}
return NS_OK;
1998-08-13 04:34:53 +00:00
}
NS_IMETHODIMP
nsLocation::SetSearch(const nsAString& aSearch)
{
nsresult rv = SetSearchInternal(aSearch);
if (NS_FAILED(rv)) {
return rv;
}
return NS_OK;
}
nsresult
nsLocation::SetSearchInternal(const nsAString& aSearch)
1998-08-13 04:34:53 +00:00
{
nsCOMPtr<nsIURI> uri;
nsresult rv = GetWritableURI(getter_AddRefs(uri));
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
if (NS_WARN_IF(NS_FAILED(rv) || !url)) {
return rv;
1998-08-13 04:34:53 +00:00
}
rv = url->SetQuery(NS_ConvertUTF16toUTF8(aSearch));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return SetURI(uri);
1998-08-13 04:34:53 +00:00
}
NS_IMETHODIMP
nsLocation::Reload(bool aForceget)
1998-08-13 04:34:53 +00:00
{
nsresult rv;
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(docShell));
nsCOMPtr<nsPIDOMWindow> window = docShell ? docShell->GetWindow() : nullptr;
if (window && window->IsHandlingResizeEvent()) {
// location.reload() was called on a window that is handling a
// resize event. Sites do this since Netscape 4.x needed it, but
// we don't, and it's a horrible experience for nothing. In stead
// of reloading the page, just clear style data and reflow the
// page since some sites may use this trick to work around gecko
// reflow bugs, and this should have the same effect.
nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
nsIPresShell *shell;
nsPresContext *pcx;
if (doc && (shell = doc->GetShell()) && (pcx = shell->GetPresContext())) {
pcx->RebuildAllStyleData(NS_STYLE_HINT_REFLOW, eRestyle_Subtree);
}
return NS_OK;
}
if (webNav) {
uint32_t reloadFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
if (aForceget) {
reloadFlags = nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE |
nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
}
rv = webNav->Reload(reloadFlags);
if (rv == NS_BINDING_ABORTED) {
// This happens when we attempt to reload a POST result and the user says
// no at the "do you want to reload?" prompt. Don't propagate this one
// back to callers.
rv = NS_OK;
}
} else {
rv = NS_ERROR_FAILURE;
1998-08-13 04:34:53 +00:00
}
return rv;
}
NS_IMETHODIMP
nsLocation::Replace(const nsAString& aUrl)
{
nsresult rv = NS_OK;
if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) {
return SetHrefWithContext(cx, aUrl, true);
}
nsAutoString oldHref;
rv = GetHref(oldHref);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURI> oldUri;
rv = NS_NewURI(getter_AddRefs(oldUri), oldHref);
NS_ENSURE_SUCCESS(rv, rv);
return SetHrefWithBase(aUrl, oldUri, true);
}
NS_IMETHODIMP
nsLocation::Assign(const nsAString& aUrl)
{
nsAutoString oldHref;
nsresult result = NS_OK;
result = GetHref(oldHref);
if (NS_SUCCEEDED(result)) {
nsCOMPtr<nsIURI> oldUri;
result = NS_NewURI(getter_AddRefs(oldUri), oldHref);
if (oldUri) {
result = SetHrefWithBase(aUrl, oldUri, false);
}
}
return result;
1998-08-13 04:34:53 +00:00
}
NS_IMETHODIMP
nsLocation::ToString(nsAString& aReturn)
{
return GetHref(aReturn);
}
NS_IMETHODIMP
nsLocation::ValueOf(nsIDOMLocation** aReturn)
{
nsCOMPtr<nsIDOMLocation> loc(this);
loc.forget(aReturn);
return NS_OK;
}
nsresult
nsLocation::GetSourceBaseURL(JSContext* cx, nsIURI** sourceURL)
{
*sourceURL = nullptr;
nsIDocument* doc = GetEntryDocument();
// If there's no entry document, we either have no Script Entry Point or one
// that isn't a DOM Window. This doesn't generally happen with the DOM,
// but can sometimes happen with extension code in certain IPC configurations.
// If this happens, try falling back on the current document associated with
// the docshell. If that fails, just return null and hope that the caller passed
// an absolute URI.
if (!doc && GetDocShell()) {
nsCOMPtr<nsPIDOMWindow> docShellWin = do_QueryInterface(GetDocShell()->GetScriptGlobalObject());
if (docShellWin) {
doc = docShellWin->GetDoc();
}
}
NS_ENSURE_TRUE(doc, NS_OK);
*sourceURL = doc->GetBaseURI().take();
return NS_OK;
}
bool
nsLocation::CallerSubsumes()
{
// Get the principal associated with the location object. Note that this is
// the principal of the page which will actually be navigated, not the
// principal of the Location object itself. This is why we need this check
// even though we only allow limited cross-origin access to Location objects
// in general.
nsCOMPtr<nsIDOMWindow> outer = mInnerWindow->GetOuterWindow();
if (MOZ_UNLIKELY(!outer))
return false;
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(outer);
bool subsumes = false;
nsresult rv = nsContentUtils::SubjectPrincipal()->SubsumesConsideringDomain(sop->GetPrincipal(), &subsumes);
NS_ENSURE_SUCCESS(rv, false);
return subsumes;
}
JSObject*
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv The only manual changes here are to BindingUtils.h, BindingUtils.cpp, Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp, dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp, Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp, Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The rest of this diff was generated by running the following commands: find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 14:13:33 +00:00
nsLocation::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv The only manual changes here are to BindingUtils.h, BindingUtils.cpp, Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp, dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp, Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp, Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The rest of this diff was generated by running the following commands: find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 14:13:33 +00:00
return LocationBinding::Wrap(aCx, this, aGivenProto);
}