2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 sw=2 et tw=80: */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-08-02 21:22:40 -07:00
|
|
|
#include "nsLocation.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2012-10-18 16:37:14 -07:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIDocShellLoadInfo.h"
|
|
|
|
#include "nsIWebNavigation.h"
|
|
|
|
#include "nsCDefaultURIFixup.h"
|
|
|
|
#include "nsIURIFixup.h"
|
|
|
|
#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"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2011-10-03 12:11:31 -07:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsITextToSubURI.h"
|
2010-12-15 08:55:13 -08:00
|
|
|
#include "nsJSUtils.h"
|
2012-07-19 00:43:33 -07:00
|
|
|
#include "nsContentUtils.h"
|
2014-08-19 12:02:08 -07:00
|
|
|
#include "nsGlobalWindow.h"
|
2012-10-26 06:32:10 -07:00
|
|
|
#include "mozilla/Likely.h"
|
2012-11-06 14:29:09 -08:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2014-03-29 12:10:27 -07:00
|
|
|
#include "nsNullPrincipal.h"
|
2014-01-03 14:37:17 -08:00
|
|
|
#include "ScriptSettings.h"
|
2014-07-11 16:32:46 -07:00
|
|
|
#include "mozilla/dom/LocationBinding.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-07-11 16:31:46 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static nsresult
|
|
|
|
GetDocumentCharacterSetForURI(const nsAString& aHref, nsACString& aCharset)
|
|
|
|
{
|
|
|
|
aCharset.Truncate();
|
|
|
|
|
2014-08-19 12:02:06 -07:00
|
|
|
if (nsIDocument* doc = GetEntryDocument()) {
|
|
|
|
aCharset = doc->GetDocumentCharacterSet();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-11 16:31:46 -07:00
|
|
|
nsLocation::nsLocation(nsPIDOMWindow* aWindow, nsIDocShell *aDocShell)
|
|
|
|
: mInnerWindow(aWindow)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-01-09 18:03:47 -08:00
|
|
|
MOZ_ASSERT(aDocShell);
|
2014-07-11 16:31:46 -07:00
|
|
|
MOZ_ASSERT(mInnerWindow->IsInnerWindow());
|
2014-01-09 18:03:47 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mDocShell = do_GetWeakReference(aDocShell);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsLocation::~nsLocation()
|
|
|
|
{
|
2014-08-07 17:45:21 -07:00
|
|
|
RemoveURLSearchParams();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryInterface implementation for nsLocation
|
2012-11-06 14:29:09 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsLocation)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMLocation)
|
2014-08-07 17:45:21 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMLocation)
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-08-07 17:45:21 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsLocation)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsLocation)
|
|
|
|
tmp->RemoveURLSearchParams();
|
|
|
|
|
|
|
|
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(mSearchParams)
|
|
|
|
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)
|
|
|
|
|
2012-11-06 14:29:09 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsLocation)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsLocation)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
nsLocation::SetDocShell(nsIDocShell *aDocShell)
|
|
|
|
{
|
|
|
|
mDocShell = do_GetWeakReference(aDocShell);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocShell *
|
|
|
|
nsLocation::GetDocShell()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocShell> docshell(do_QueryReferent(mDocShell));
|
|
|
|
return docshell;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsLocation::CheckURL(nsIURI* aURI, nsIDocShellLoadInfo** aLoadInfo)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aLoadInfo = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
2010-12-15 08:55:13 -08:00
|
|
|
NS_ENSURE_TRUE(docShell, NS_ERROR_NOT_AVAILABLE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> owner;
|
2010-12-15 08:55:13 -08:00
|
|
|
nsCOMPtr<nsIURI> sourceURI;
|
2014-11-18 05:47:03 -08:00
|
|
|
net::ReferrerPolicy referrerPolicy = net::RP_Default;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-19 14:26:17 -07:00
|
|
|
if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// 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.
|
2012-10-19 14:26:17 -07:00
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
NS_ENSURE_STATE(ssm);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Check to see if URI is allowed.
|
2012-10-19 14:26:17 -07:00
|
|
|
nsresult rv = ssm->CheckLoadURIFromScript(cx, aURI);
|
2010-12-15 08:55:13 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-12-15 08:55:13 -08:00
|
|
|
// 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.
|
|
|
|
|
2012-10-17 10:44:30 -07:00
|
|
|
nsCOMPtr<nsIDocument> doc;
|
2012-10-10 02:01:26 -07:00
|
|
|
nsCOMPtr<nsIURI> docOriginalURI, docCurrentURI, principalURI;
|
2014-01-03 14:37:17 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> incumbent =
|
|
|
|
do_QueryInterface(mozilla::dom::GetIncumbentGlobal());
|
|
|
|
if (incumbent) {
|
|
|
|
doc = incumbent->GetDoc();
|
2012-10-17 10:44:30 -07:00
|
|
|
}
|
2012-10-10 02:01:26 -07:00
|
|
|
if (doc) {
|
|
|
|
docOriginalURI = doc->GetOriginalURI();
|
|
|
|
docCurrentURI = doc->GetDocumentURI();
|
|
|
|
rv = doc->NodePrincipal()->GetURI(getter_AddRefs(principalURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-11-18 05:47:03 -08:00
|
|
|
referrerPolicy = doc->GetReferrerPolicy();
|
2010-12-15 08:55:13 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool urisEqual = false;
|
2010-12-15 08:55:13 -08:00
|
|
|
if (docOriginalURI && docCurrentURI && principalURI) {
|
|
|
|
principalURI->Equals(docOriginalURI, &urisEqual);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (urisEqual) {
|
|
|
|
sourceURI = docCurrentURI;
|
|
|
|
}
|
|
|
|
else {
|
2014-03-29 12:10:27 -07:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2010-12-15 08:55:13 -08:00
|
|
|
}
|
|
|
|
|
2014-05-13 02:58:00 -07:00
|
|
|
owner = nsContentUtils::SubjectPrincipal();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create load info
|
|
|
|
nsCOMPtr<nsIDocShellLoadInfo> loadInfo;
|
|
|
|
docShell->CreateLoadInfo(getter_AddRefs(loadInfo));
|
|
|
|
NS_ENSURE_TRUE(loadInfo, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
loadInfo->SetOwner(owner);
|
|
|
|
|
2010-12-15 08:55:13 -08:00
|
|
|
if (sourceURI) {
|
2007-03-22 10:30:00 -07:00
|
|
|
loadInfo->SetReferrer(sourceURI);
|
2014-11-18 05:47:03 -08:00
|
|
|
loadInfo->SetReferrerPolicy(referrerPolicy);
|
2010-12-15 08:55:13 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
loadInfo.swap(*aLoadInfo);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsLocation::GetURI(nsIURI** aURI, bool aGetInnermostURI)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aURI = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aURI = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv) || !uri) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return uri->Clone(aURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsLocation::SetURI(nsIURI* aURI, bool aReplace)
|
2007-03-22 10:30:00 -07: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);
|
|
|
|
}
|
|
|
|
|
2014-01-12 23:58:16 -08:00
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return docShell->LoadURI(aURI, loadInfo,
|
2011-10-17 07:59:28 -07:00
|
|
|
nsIWebNavigation::LOAD_FLAGS_NONE, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::GetHash(nsAString& aHash)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
aHash.SetLength(0);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
2011-05-21 18:12:46 -07:00
|
|
|
if (NS_FAILED(rv) || !uri) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString ref;
|
2011-05-21 18:12:46 -07:00
|
|
|
nsAutoString unicodeRef;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-21 18:12:46 -07:00
|
|
|
rv = uri->GetRef(ref);
|
2014-11-26 06:08:54 -08:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsITextToSubURI> textToSubURI(
|
|
|
|
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-11-26 06:08:54 -08:00
|
|
|
nsAutoCString charset;
|
|
|
|
uri->GetOriginCharset(charset);
|
|
|
|
|
|
|
|
rv = textToSubURI->UnEscapeURIForUI(charset, ref, unicodeRef);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-11-26 06:08:54 -08:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Oh, well. No intl here!
|
|
|
|
NS_UnescapeURL(ref);
|
|
|
|
CopyASCIItoUTF16(ref, unicodeRef);
|
|
|
|
rv = NS_OK;
|
2014-11-11 17:14:04 -08:00
|
|
|
}
|
2011-05-21 18:12:46 -07:00
|
|
|
}
|
2011-05-20 13:21:27 -07:00
|
|
|
|
2014-11-26 06:08:54 -08:00
|
|
|
if (NS_SUCCEEDED(rv) && !unicodeRef.IsEmpty()) {
|
|
|
|
aHash.Assign(char16_t('#'));
|
|
|
|
aHash.Append(unicodeRef);
|
|
|
|
}
|
|
|
|
|
2011-05-21 18:12:46 -07:00
|
|
|
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;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::SetHash(const nsAString& aHash)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-03 23:42:28 -07:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2011-05-21 18:12:46 -07:00
|
|
|
if (NS_FAILED(rv) || !uri) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-21 18:12:46 -07:00
|
|
|
NS_ConvertUTF16toUTF8 hash(aHash);
|
2014-01-04 07:02:17 -08:00
|
|
|
if (hash.IsEmpty() || hash.First() != char16_t('#')) {
|
|
|
|
hash.Insert(char16_t('#'), 0);
|
2011-05-21 18:12:46 -07:00
|
|
|
}
|
|
|
|
rv = uri->SetRef(hash);
|
2014-01-14 00:54:58 -08:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-14 00:54:58 -08:00
|
|
|
return SetURI(uri);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::GetHost(nsAString& aHost)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
aHost.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult result;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
result = GetURI(getter_AddRefs(uri), true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (uri) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString hostport;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = uri->GetHostPort(hostport);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
AppendUTF8toUTF16(hostport, aHost);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-07 00:11:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::SetHost(const nsAString& aHost)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-03 23:42:28 -07:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2014-01-14 00:54:58 -08:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv) || !uri)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-14 00:54:58 -08:00
|
|
|
rv = uri->SetHostPort(NS_ConvertUTF16toUTF8(aHost));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-14 00:54:58 -08:00
|
|
|
return SetURI(uri);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::GetHostname(nsAString& aHostname)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
aHostname.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2014-10-20 03:46:20 -07:00
|
|
|
GetURI(getter_AddRefs(uri), true);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (uri) {
|
2014-10-20 03:46:20 -07:00
|
|
|
nsContentUtils::GetHostOrIPv6WithBrackets(uri, aHostname);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-08-07 00:11:43 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::SetHostname(const nsAString& aHostname)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-03 23:42:28 -07:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2014-01-14 00:54:58 -08:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv) || !uri)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-14 00:54:58 -08:00
|
|
|
rv = uri->SetHost(NS_ConvertUTF16toUTF8(aHostname));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-14 00:54:58 -08:00
|
|
|
return SetURI(uri);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::GetHref(nsAString& aHref)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
aHref.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult result;
|
|
|
|
|
|
|
|
result = GetURI(getter_AddRefs(uri));
|
|
|
|
|
|
|
|
if (uri) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString uriString;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = uri->GetSpec(uriString);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
AppendUTF8toUTF16(uriString, aHref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::SetHref(const nsAString& aHref)
|
|
|
|
{
|
|
|
|
nsAutoString oldHref;
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2012-10-19 14:26:17 -07:00
|
|
|
JSContext *cx = nsContentUtils::GetCurrentJSContext();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (cx) {
|
2013-01-15 03:46:35 -08:00
|
|
|
rv = SetHrefWithContext(cx, aHref, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
rv = GetHref(oldHref);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIURI> oldUri;
|
|
|
|
|
|
|
|
rv = NS_NewURI(getter_AddRefs(oldUri), oldHref);
|
|
|
|
|
|
|
|
if (oldUri) {
|
2013-01-15 03:46:35 -08:00
|
|
|
rv = SetHrefWithBase(aHref, oldUri, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsLocation::SetHrefWithContext(JSContext* cx, const nsAString& aHref,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aReplace)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
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,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aReplace)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult result;
|
2009-03-24 18:52:24 -07:00
|
|
|
nsCOMPtr<nsIURI> newUri;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-01-15 03:46:35 -08:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString docCharset;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(GetDocumentCharacterSetForURI(aHref, docCharset)))
|
2009-03-24 18:52:24 -07:00
|
|
|
result = NS_NewURI(getter_AddRefs(newUri), aHref, docCharset.get(), aBase);
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
2012-07-30 07:20:58 -07:00
|
|
|
result = NS_NewURI(getter_AddRefs(newUri), aHref, nullptr, aBase);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (newUri) {
|
2013-01-15 03:46:35 -08:00
|
|
|
/* 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
|
|
|
|
*
|
|
|
|
*/
|
2014-08-19 12:02:08 -07:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
2013-01-15 03:46:35 -08:00
|
|
|
|
|
|
|
return SetURI(newUri, aReplace || inScriptTag);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-01-09 08:37:25 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::GetOrigin(nsAString& aOrigin)
|
|
|
|
{
|
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
|
|
|
aOrigin.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri), true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-02-19 18:23:48 -08:00
|
|
|
NS_ENSURE_TRUE(uri, NS_OK);
|
2013-01-09 08:37:25 -08:00
|
|
|
|
|
|
|
nsAutoString origin;
|
|
|
|
rv = nsContentUtils::GetUTFOrigin(uri, origin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aOrigin = origin;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::GetPathname(nsAString& aPathname)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
aPathname.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetURI(getter_AddRefs(uri));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (url) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString file;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = url->GetFilePath(file);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
AppendUTF8toUTF16(file, aPathname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::SetPathname(const nsAString& aPathname)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-03 23:42:28 -07:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2014-01-14 00:54:58 -08:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv) || !uri)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-14 00:54:58 -08:00
|
|
|
rv = uri->SetPath(NS_ConvertUTF16toUTF8(aPathname));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-14 00:54:58 -08:00
|
|
|
return SetURI(uri);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::GetPort(nsAString& aPort)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
aPort.SetLength(0);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
result = GetURI(getter_AddRefs(uri), true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (uri) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t port;
|
2007-03-22 10:30:00 -07:00
|
|
|
result = uri->GetPort(&port);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result) && -1 != port) {
|
|
|
|
nsAutoString portStr;
|
|
|
|
portStr.AppendInt(port);
|
|
|
|
aPort.Append(portStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't propagate this exception to caller
|
|
|
|
result = NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::SetPort(const nsAString& aPort)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-03 23:42:28 -07:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2014-01-14 00:54:58 -08:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv) || !uri)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-14 00:54:58 -08:00
|
|
|
// perhaps use nsReadingIterators at some point?
|
|
|
|
NS_ConvertUTF16toUTF8 portStr(aPort);
|
|
|
|
const char *buf = portStr.get();
|
|
|
|
int32_t port = -1;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-14 00:54:58 -08:00
|
|
|
if (!portStr.IsEmpty() && buf) {
|
|
|
|
if (*buf == ':') {
|
|
|
|
port = atol(buf+1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
port = atol(buf);
|
2007-05-03 23:42:28 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-14 00:54:58 -08:00
|
|
|
rv = uri->SetPort(port);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SetURI(uri);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::GetProtocol(nsAString& aProtocol)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
aProtocol.SetLength(0);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetURI(getter_AddRefs(uri));
|
|
|
|
|
|
|
|
if (uri) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString protocol;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = uri->GetScheme(protocol);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
CopyASCIItoUTF16(protocol, aProtocol);
|
2014-01-04 07:02:17 -08:00
|
|
|
aProtocol.Append(char16_t(':'));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::SetProtocol(const nsAString& aProtocol)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-03 23:42:28 -07:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2014-01-14 00:54:58 -08:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv) || !uri)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-14 00:54:58 -08:00
|
|
|
rv = uri->SetScheme(NS_ConvertUTF16toUTF8(aProtocol));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-14 00:54:58 -08:00
|
|
|
return SetURI(uri);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-07-11 16:31:46 -07:00
|
|
|
void
|
|
|
|
nsLocation::GetUsername(nsAString& aUsername, ErrorResult& aError)
|
|
|
|
{
|
|
|
|
if (!CallerSubsumes()) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (!CallerSubsumes()) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (!CallerSubsumes()) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (!CallerSubsumes()) {
|
|
|
|
aError.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::GetSearch(nsAString& aSearch)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
aSearch.SetLength(0);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetURI(getter_AddRefs(uri));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
|
|
|
|
if (url) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString search;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
result = url->GetQuery(search);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result) && !search.IsEmpty()) {
|
2014-01-04 07:02:17 -08:00
|
|
|
aSearch.Assign(char16_t('?'));
|
2007-03-22 10:30:00 -07:00
|
|
|
AppendUTF8toUTF16(search, aSearch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::SetSearch(const nsAString& aSearch)
|
2014-08-07 17:45:21 -07:00
|
|
|
{
|
|
|
|
nsresult rv = SetSearchInternal(aSearch);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsLocation::SetSearchInternal(const nsAString& aSearch)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-03 23:42:28 -07:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
2014-01-14 00:54:58 -08:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv) || !url)) {
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-14 00:54:58 -08:00
|
|
|
rv = url->SetQuery(NS_ConvertUTF16toUTF8(aSearch));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SetURI(uri);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsLocation::Reload(bool aForceget)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
|
|
|
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(docShell));
|
2014-01-09 18:03:47 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = docShell ? docShell->GetWindow() : nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2013-04-23 21:22:37 -07:00
|
|
|
nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsIPresShell *shell;
|
|
|
|
nsPresContext *pcx;
|
2010-06-25 06:59:57 -07:00
|
|
|
if (doc && (shell = doc->GetShell()) && (pcx = shell->GetPresContext())) {
|
2014-10-08 14:26:57 -07:00
|
|
|
pcx->RebuildAllStyleData(NS_STYLE_HINT_REFLOW, eRestyle_Subtree);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-05 14:54:53 -07:00
|
|
|
if (webNav) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t reloadFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-09-05 14:54:53 -07:00
|
|
|
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;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-09-05 14:54:53 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::Replace(const nsAString& aUrl)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2012-10-19 14:26:17 -07:00
|
|
|
if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) {
|
|
|
|
return SetHrefWithContext(cx, aUrl, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return SetHrefWithBase(aUrl, oldUri, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::Assign(const nsAString& aUrl)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
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) {
|
2011-10-17 07:59:28 -07:00
|
|
|
result = SetHrefWithBase(aUrl, oldUri, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::ToString(nsAString& aReturn)
|
|
|
|
{
|
2012-10-18 16:37:14 -07:00
|
|
|
// NB: GetHref checks CallerSubsumes().
|
2007-03-22 10:30:00 -07:00
|
|
|
return GetHref(aReturn);
|
|
|
|
}
|
|
|
|
|
2012-11-07 08:27:23 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::ValueOf(nsIDOMLocation** aReturn)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMLocation> loc(this);
|
|
|
|
loc.forget(aReturn);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
nsLocation::GetSourceBaseURL(JSContext* cx, nsIURI** sourceURL)
|
|
|
|
{
|
2012-10-19 14:26:17 -07:00
|
|
|
*sourceURL = nullptr;
|
2014-08-19 12:02:07 -07:00
|
|
|
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,
|
2012-10-19 14:26:17 -07:00
|
|
|
// 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.
|
2014-08-19 12:02:07 -07:00
|
|
|
if (!doc && GetDocShell()) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> docShellWin = do_QueryInterface(GetDocShell()->GetScriptGlobalObject());
|
|
|
|
if (docShellWin) {
|
|
|
|
doc = docShellWin->GetDoc();
|
|
|
|
}
|
2012-10-19 14:26:17 -07:00
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(doc, NS_OK);
|
2014-03-15 12:00:15 -07:00
|
|
|
*sourceURL = doc->GetBaseURI().take();
|
2012-10-19 14:26:17 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-10-18 16:37:14 -07:00
|
|
|
|
|
|
|
bool
|
|
|
|
nsLocation::CallerSubsumes()
|
|
|
|
{
|
2014-07-11 16:31:46 -07:00
|
|
|
// 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();
|
2012-10-26 06:32:10 -07:00
|
|
|
if (MOZ_UNLIKELY(!outer))
|
2012-10-18 16:37:14 -07:00
|
|
|
return false;
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(outer);
|
|
|
|
bool subsumes = false;
|
2014-05-13 02:58:00 -07:00
|
|
|
nsresult rv = nsContentUtils::SubjectPrincipal()->SubsumesConsideringDomain(sop->GetPrincipal(), &subsumes);
|
2012-10-18 16:37:14 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
return subsumes;
|
|
|
|
}
|
2014-07-11 16:32:46 -07:00
|
|
|
|
|
|
|
JSObject*
|
|
|
|
nsLocation::WrapObject(JSContext* aCx)
|
|
|
|
{
|
|
|
|
return LocationBinding::Wrap(aCx, this);
|
|
|
|
}
|
2014-08-07 17:45:21 -07:00
|
|
|
|
|
|
|
URLSearchParams*
|
|
|
|
nsLocation::GetDocShellSearchParams()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocShell> docShell = GetDocShell();
|
|
|
|
if (!docShell) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return docShell->GetURLSearchParams();
|
|
|
|
}
|
|
|
|
|
|
|
|
URLSearchParams*
|
|
|
|
nsLocation::SearchParams()
|
|
|
|
{
|
|
|
|
if (!mSearchParams) {
|
|
|
|
// We must register this object to the URLSearchParams of the docshell in
|
|
|
|
// order to receive updates.
|
|
|
|
nsRefPtr<URLSearchParams> searchParams = GetDocShellSearchParams();
|
|
|
|
if (searchParams) {
|
|
|
|
searchParams->AddObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
mSearchParams = new URLSearchParams();
|
|
|
|
mSearchParams->AddObserver(this);
|
|
|
|
UpdateURLSearchParams();
|
|
|
|
}
|
|
|
|
|
|
|
|
return mSearchParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsLocation::SetSearchParams(URLSearchParams& aSearchParams)
|
|
|
|
{
|
|
|
|
if (mSearchParams) {
|
|
|
|
mSearchParams->RemoveObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the observer will be cleared using the cycle collector.
|
|
|
|
mSearchParams = &aSearchParams;
|
|
|
|
mSearchParams->AddObserver(this);
|
|
|
|
|
|
|
|
nsAutoString search;
|
|
|
|
mSearchParams->Serialize(search);
|
|
|
|
SetSearchInternal(search);
|
|
|
|
|
|
|
|
// We don't need to inform the docShell about this new SearchParams because
|
|
|
|
// setting the new value the docShell will refresh its value automatically.
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsLocation::URLSearchParamsUpdated(URLSearchParams* aSearchParams)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mSearchParams);
|
|
|
|
|
|
|
|
// This change comes from content.
|
|
|
|
if (aSearchParams == mSearchParams) {
|
|
|
|
nsAutoString search;
|
|
|
|
mSearchParams->Serialize(search);
|
|
|
|
SetSearchInternal(search);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This change comes from the docShell.
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
nsRefPtr<URLSearchParams> searchParams = GetDocShellSearchParams();
|
|
|
|
MOZ_ASSERT(searchParams);
|
|
|
|
MOZ_ASSERT(aSearchParams == searchParams);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsAutoString search;
|
|
|
|
aSearchParams->Serialize(search);
|
|
|
|
mSearchParams->ParseInput(NS_ConvertUTF16toUTF8(search), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsLocation::UpdateURLSearchParams()
|
|
|
|
{
|
|
|
|
if (!mSearchParams) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString search;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)) || NS_WARN_IF(!uri)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (url) {
|
|
|
|
nsresult rv = url->GetQuery(search);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to get the query from a nsIURL.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mSearchParams->ParseInput(search, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsLocation::RemoveURLSearchParams()
|
|
|
|
{
|
|
|
|
if (mSearchParams) {
|
|
|
|
mSearchParams->RemoveObserver(this);
|
|
|
|
mSearchParams = nullptr;
|
|
|
|
|
|
|
|
nsRefPtr<URLSearchParams> docShellSearchParams = GetDocShellSearchParams();
|
|
|
|
if (docShellSearchParams) {
|
|
|
|
docShellSearchParams->RemoveObserver(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|