2015-05-03 22:32:37 +03:00
|
|
|
/* -*- 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 15:12:37 +04: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 08:34:53 +04:00
|
|
|
|
2016-09-16 10:18:18 +03:00
|
|
|
#include "Location.h"
|
2012-10-19 03:37:14 +04:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2003-06-26 04:41:23 +04:00
|
|
|
#include "nsIScriptContext.h"
|
2018-10-30 03:13:29 +03:00
|
|
|
#include "nsDocShellLoadState.h"
|
2000-02-24 06:57:32 +03:00
|
|
|
#include "nsIWebNavigation.h"
|
2020-03-13 15:44:36 +03:00
|
|
|
#include "nsIOService.h"
|
1998-08-13 08:34:53 +04:00
|
|
|
#include "nsIURL.h"
|
2004-04-24 20:09:31 +04:00
|
|
|
#include "nsIJARURI.h"
|
1999-11-30 07:50:42 +03:00
|
|
|
#include "nsNetUtil.h"
|
1999-08-04 03:16:48 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2002-03-06 10:48:55 +03:00
|
|
|
#include "nsEscape.h"
|
2004-10-20 06:19:57 +04:00
|
|
|
#include "nsPresContext.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2003-12-23 18:41:10 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2010-12-15 19:55:13 +03:00
|
|
|
#include "nsJSUtils.h"
|
2012-07-19 11:43:33 +04:00
|
|
|
#include "nsContentUtils.h"
|
2019-08-02 19:30:49 +03:00
|
|
|
#include "nsDocShell.h"
|
2014-08-19 23:02:08 +04:00
|
|
|
#include "nsGlobalWindow.h"
|
2012-10-26 17:32:10 +04:00
|
|
|
#include "mozilla/Likely.h"
|
2012-11-07 02:29:09 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2018-12-17 04:27:58 +03:00
|
|
|
#include "mozilla/Components.h"
|
2018-07-17 22:37:48 +03:00
|
|
|
#include "mozilla/NullPrincipal.h"
|
2017-02-09 02:19:02 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2019-03-29 18:11:04 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
|
|
|
#include "mozilla/dom/DocumentInlines.h"
|
2014-07-12 03:32:46 +04:00
|
|
|
#include "mozilla/dom/LocationBinding.h"
|
2017-05-08 09:24:22 +03:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2019-02-12 22:35:32 +03:00
|
|
|
#include "ReferrerInfo.h"
|
2001-11-15 02:45:23 +03:00
|
|
|
|
2016-09-16 10:18:18 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2014-07-12 03:31:46 +04:00
|
|
|
|
2019-10-01 01:06:47 +03:00
|
|
|
Location::Location(nsPIDOMWindowInner* aWindow,
|
|
|
|
BrowsingContext* aBrowsingContext)
|
2014-07-12 03:31:46 +04:00
|
|
|
: mInnerWindow(aWindow) {
|
2019-09-17 04:17:17 +03:00
|
|
|
// aBrowsingContext can be null if it gets called after nsDocShell::Destory().
|
|
|
|
if (aBrowsingContext) {
|
|
|
|
mBrowsingContextId = aBrowsingContext->Id();
|
|
|
|
}
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2020-02-21 13:41:47 +03:00
|
|
|
Location::~Location() = default;
|
1998-08-13 08:34:53 +04:00
|
|
|
|
2016-09-16 10:18:18 +03:00
|
|
|
// QueryInterface implementation for Location
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Location)
|
2012-11-07 02:29:09 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2017-05-11 08:26:08 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 21:42:36 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2017-05-11 08:26:08 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Location, mInnerWindow)
|
2014-08-08 04:45:21 +04:00
|
|
|
|
2016-09-16 10:18:18 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Location)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Location)
|
1998-08-13 08:34:53 +04:00
|
|
|
|
2019-08-24 01:26:51 +03:00
|
|
|
BrowsingContext* Location::GetBrowsingContext() {
|
2019-09-17 04:17:17 +03:00
|
|
|
RefPtr<BrowsingContext> bc = BrowsingContext::Get(mBrowsingContextId);
|
|
|
|
return bc.get();
|
2019-08-24 01:26:51 +03:00
|
|
|
}
|
2005-02-24 23:19:58 +03:00
|
|
|
|
2019-08-24 01:26:51 +03:00
|
|
|
already_AddRefed<nsIDocShell> Location::GetDocShell() {
|
2019-09-17 04:17:17 +03:00
|
|
|
if (RefPtr<BrowsingContext> bc = GetBrowsingContext()) {
|
|
|
|
return do_AddRef(bc->GetDocShell());
|
|
|
|
}
|
|
|
|
return nullptr;
|
1999-09-07 06:54:19 +04:00
|
|
|
}
|
|
|
|
|
2016-09-16 10:18:18 +03:00
|
|
|
nsresult Location::GetURI(nsIURI** aURI, bool aGetInnermostURI) {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aURI = nullptr;
|
2002-03-23 03:48:40 +03:00
|
|
|
|
2019-09-17 04:17:17 +03:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(GetDocShell());
|
2018-09-26 13:25:39 +03:00
|
|
|
if (!docShell) {
|
2017-05-08 16:49:08 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
2006-06-20 22:56:03 +04:00
|
|
|
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(docShell, &rv));
|
2002-03-23 03:48:40 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2002-03-06 10:48:55 +03:00
|
|
|
|
2002-12-12 18:48:30 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = webNav->GetCurrentURI(getter_AddRefs(uri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2003-01-05 09:34:53 +03:00
|
|
|
// It is valid for docshell to return a null URI. Don't try to fixup
|
|
|
|
// if this happens.
|
|
|
|
if (!uri) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-04-24 20:09:31 +04:00
|
|
|
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?");
|
2020-03-13 15:44:36 +03:00
|
|
|
nsCOMPtr<nsIURI> exposableURI = net::nsIOService::CreateExposableURI(uri);
|
|
|
|
exposableURI.forget(aURI);
|
|
|
|
return NS_OK;
|
2002-03-06 10:48:55 +03:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::GetHash(nsAString& aHash, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-23 03:48:40 +03:00
|
|
|
aHash.SetLength(0);
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !uri) {
|
|
|
|
return;
|
2011-05-22 05:12:46 +04:00
|
|
|
}
|
1999-06-18 21:34:08 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString ref;
|
2011-05-22 05:12:46 +04:00
|
|
|
nsAutoString unicodeRef;
|
2002-03-23 03:48:40 +03:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = uri->GetRef(ref);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2004-05-21 04:53:40 +04:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
if (!ref.IsEmpty()) {
|
2017-03-09 00:19:34 +03:00
|
|
|
aHash.Assign(char16_t('#'));
|
|
|
|
AppendUTF8toUTF16(ref, aHash);
|
2014-11-26 17:08:54 +03:00
|
|
|
}
|
|
|
|
|
2011-05-22 05:12:46 +04: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;
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::SetHash(const nsAString& aHash, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-22 05:12:46 +04:00
|
|
|
NS_ConvertUTF16toUTF8 hash(aHash);
|
2014-01-04 19:02:17 +04:00
|
|
|
if (hash.IsEmpty() || hash.First() != char16_t('#')) {
|
|
|
|
hash.Insert(char16_t('#'), 0);
|
2011-05-22 05:12:46 +04:00
|
|
|
}
|
2017-05-08 21:24:13 +03:00
|
|
|
|
2016-08-30 19:55:40 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2018-07-23 14:28:47 +03:00
|
|
|
aRv = GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !uri) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRv = NS_MutateURI(uri).SetRef(hash).Finalize(uri);
|
2017-05-08 21:24:13 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !uri) {
|
|
|
|
return;
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
SetURI(uri, aSubjectPrincipal, aRv);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::GetHost(nsAString& aHost, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-12-23 18:41:10 +03:00
|
|
|
aHost.Truncate();
|
2002-03-23 03:48:40 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult result;
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
result = GetURI(getter_AddRefs(uri), true);
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2002-03-23 03:48:40 +03:00
|
|
|
if (uri) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString hostport;
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
result = uri->GetHostPort(hostport);
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2002-03-23 03:48:40 +03:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2003-12-23 18:41:10 +03:00
|
|
|
AppendUTF8toUTF16(hostport, aHost);
|
2002-03-23 03:48:40 +03:00
|
|
|
}
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::SetHost(const nsAString& aHost, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2018-02-26 22:43:45 +03:00
|
|
|
aRv = GetURI(getter_AddRefs(uri));
|
2017-05-08 21:24:13 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !uri) {
|
|
|
|
return;
|
2014-01-14 12:54:58 +04:00
|
|
|
}
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2018-02-26 22:43:45 +03:00
|
|
|
aRv =
|
|
|
|
NS_MutateURI(uri).SetHostPort(NS_ConvertUTF16toUTF8(aHost)).Finalize(uri);
|
2017-05-08 21:24:13 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
SetURI(uri, aSubjectPrincipal, aRv);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::GetHostname(nsAString& aHostname,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-12-23 18:41:10 +03:00
|
|
|
aHostname.Truncate();
|
2002-03-23 03:48:40 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2014-10-20 14:46:20 +04:00
|
|
|
GetURI(getter_AddRefs(uri), true);
|
2002-03-23 03:48:40 +03:00
|
|
|
if (uri) {
|
2014-10-20 14:46:20 +04:00
|
|
|
nsContentUtils::GetHostOrIPv6WithBrackets(uri, aHostname);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::SetHostname(const nsAString& aHostname,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2018-02-26 22:43:47 +03:00
|
|
|
aRv = GetURI(getter_AddRefs(uri));
|
2017-05-08 21:24:13 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !uri) {
|
|
|
|
return;
|
2014-01-14 12:54:58 +04:00
|
|
|
}
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2018-02-26 22:43:47 +03:00
|
|
|
aRv =
|
|
|
|
NS_MutateURI(uri).SetHost(NS_ConvertUTF16toUTF8(aHostname)).Finalize(uri);
|
2017-05-08 21:24:13 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
SetURI(uri, aSubjectPrincipal, aRv);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:14:21 +03:00
|
|
|
nsresult Location::GetHref(nsAString& aHref) {
|
2003-12-23 18:41:10 +03:00
|
|
|
aHref.Truncate();
|
2002-03-23 03:48:40 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2017-05-08 21:24:13 +03:00
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)) || !uri) {
|
|
|
|
return rv;
|
|
|
|
}
|
2017-05-08 21:14:21 +03:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
nsAutoCString uriString;
|
|
|
|
rv = uri->GetSpec(uriString);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
AppendUTF8toUTF16(uriString, aHref);
|
|
|
|
return NS_OK;
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::GetOrigin(nsAString& aOrigin, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-09 20:37:25 +04:00
|
|
|
aOrigin.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = GetURI(getter_AddRefs(uri), true);
|
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !uri) {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-09 20:37:25 +04:00
|
|
|
|
|
|
|
nsAutoString origin;
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = nsContentUtils::GetUTFOrigin(uri, origin);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2013-01-09 20:37:25 +04:00
|
|
|
|
|
|
|
aOrigin = origin;
|
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::GetPathname(nsAString& aPathname,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-12-23 18:41:10 +03:00
|
|
|
aPathname.Truncate();
|
2002-03-23 03:48:40 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !uri) {
|
|
|
|
return;
|
2017-01-09 09:53:01 +03:00
|
|
|
}
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2016-12-28 14:40:21 +03:00
|
|
|
nsAutoCString file;
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = uri->GetFilePath(file);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
AppendUTF8toUTF16(file, aPathname);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::SetPathname(const nsAString& aPathname,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2018-02-26 22:43:45 +03:00
|
|
|
aRv = GetURI(getter_AddRefs(uri));
|
2017-05-08 21:24:13 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !uri) {
|
|
|
|
return;
|
2014-01-14 12:54:58 +04:00
|
|
|
}
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2018-02-26 22:43:45 +03:00
|
|
|
nsresult rv = NS_MutateURI(uri)
|
|
|
|
.SetFilePath(NS_ConvertUTF16toUTF8(aPathname))
|
|
|
|
.Finalize(uri);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
2018-02-26 22:43:45 +03:00
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
SetURI(uri, aSubjectPrincipal, aRv);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::GetPort(nsAString& aPort, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-23 03:48:40 +03:00
|
|
|
aPort.SetLength(0);
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = GetURI(getter_AddRefs(uri), true);
|
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !uri) {
|
|
|
|
return;
|
|
|
|
}
|
2005-07-20 01:12:18 +04:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
int32_t port;
|
|
|
|
nsresult result = uri->GetPort(&port);
|
2017-05-08 21:14:21 +03:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
// Don't propagate this exception to caller
|
|
|
|
if (NS_SUCCEEDED(result) && -1 != port) {
|
|
|
|
nsAutoString portStr;
|
|
|
|
portStr.AppendInt(port);
|
|
|
|
aPort.Append(portStr);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::SetPort(const nsAString& aPort, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2018-02-26 22:43:46 +03:00
|
|
|
aRv = GetURI(getter_AddRefs(uri));
|
2017-05-08 21:24:13 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed() || !uri)) {
|
|
|
|
return;
|
2014-01-14 12:54:58 +04:00
|
|
|
}
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2014-01-14 12:54:58 +04:00
|
|
|
// perhaps use nsReadingIterators at some point?
|
|
|
|
NS_ConvertUTF16toUTF8 portStr(aPort);
|
|
|
|
const char* buf = portStr.get();
|
|
|
|
int32_t port = -1;
|
2002-03-23 03:48:40 +03:00
|
|
|
|
2014-01-14 12:54:58 +04:00
|
|
|
if (!portStr.IsEmpty() && buf) {
|
|
|
|
if (*buf == ':') {
|
|
|
|
port = atol(buf + 1);
|
|
|
|
} else {
|
|
|
|
port = atol(buf);
|
2007-05-04 10:42:28 +04:00
|
|
|
}
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2018-02-26 22:43:46 +03:00
|
|
|
aRv = NS_MutateURI(uri).SetPort(port).Finalize(uri);
|
2017-05-08 21:24:13 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
2014-01-14 12:54:58 +04:00
|
|
|
}
|
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
SetURI(uri, aSubjectPrincipal, aRv);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::GetProtocol(nsAString& aProtocol,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-23 03:48:40 +03:00
|
|
|
aProtocol.SetLength(0);
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !uri) {
|
|
|
|
return;
|
|
|
|
}
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
nsAutoCString protocol;
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = uri->GetScheme(protocol);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
CopyASCIItoUTF16(protocol, aProtocol);
|
|
|
|
aProtocol.Append(char16_t(':'));
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::SetProtocol(const nsAString& aProtocol,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2018-02-26 22:43:47 +03:00
|
|
|
aRv = GetURI(getter_AddRefs(uri));
|
2017-05-08 21:24:13 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !uri) {
|
|
|
|
return;
|
2014-01-14 12:54:58 +04:00
|
|
|
}
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2017-02-09 02:19:02 +03:00
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
aProtocol.BeginReading(start);
|
|
|
|
aProtocol.EndReading(end);
|
|
|
|
nsAString::const_iterator iter(start);
|
|
|
|
Unused << FindCharInReadable(':', iter, end);
|
|
|
|
|
2018-02-26 22:43:47 +03:00
|
|
|
nsresult rv = NS_MutateURI(uri)
|
|
|
|
.SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)))
|
|
|
|
.Finalize(uri);
|
2014-01-14 12:54:58 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2017-02-09 02:19:02 +03:00
|
|
|
// Oh, I wish nsStandardURL returned NS_ERROR_MALFORMED_URI for _all_ the
|
|
|
|
// malformed cases, not just some of them!
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
2017-05-08 21:24:13 +03:00
|
|
|
|
2016-02-12 14:21:01 +03:00
|
|
|
nsAutoCString newSpec;
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = uri->GetSpec(newSpec);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
2016-02-12 14:21:01 +03:00
|
|
|
}
|
|
|
|
// We may want a new URI class for the new URI, so recreate it:
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), newSpec);
|
|
|
|
if (NS_FAILED(rv)) {
|
2017-02-09 02:19:02 +03:00
|
|
|
if (rv == NS_ERROR_MALFORMED_URI) {
|
|
|
|
rv = NS_ERROR_DOM_SYNTAX_ERR;
|
|
|
|
}
|
2017-05-08 21:24:13 +03:00
|
|
|
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
2016-02-12 14:21:01 +03:00
|
|
|
}
|
1998-08-13 08:34:53 +04:00
|
|
|
|
2019-07-30 20:51:37 +03:00
|
|
|
if (!uri->SchemeIs("http") && !uri->SchemeIs("https")) {
|
2017-02-09 02:19:01 +03:00
|
|
|
// No-op, per spec.
|
2017-05-08 21:24:13 +03:00
|
|
|
return;
|
2017-02-09 02:19:01 +03:00
|
|
|
}
|
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
SetURI(uri, aSubjectPrincipal, aRv);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::GetSearch(nsAString& aSearch, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-23 03:48:40 +03:00
|
|
|
aSearch.SetLength(0);
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
1998-08-13 08:34:53 +04:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
result = GetURI(getter_AddRefs(uri));
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2002-03-23 03:48:40 +03:00
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
|
|
|
|
if (url) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString search;
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2002-03-23 03:48:40 +03:00
|
|
|
result = url->GetQuery(search);
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
if (NS_SUCCEEDED(result) && !search.IsEmpty()) {
|
2014-01-04 19:02:17 +04:00
|
|
|
aSearch.Assign(char16_t('?'));
|
2003-12-23 18:41:10 +03:00
|
|
|
AppendUTF8toUTF16(search, aSearch);
|
2002-03-06 10:48:55 +03:00
|
|
|
}
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::SetSearch(const nsAString& aSearch,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
2014-08-08 04:45:21 +04:00
|
|
|
}
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2018-02-26 22:43:45 +03:00
|
|
|
aRv = GetURI(getter_AddRefs(uri));
|
2002-03-23 03:48:40 +03:00
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
2017-05-08 21:24:13 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !url) {
|
|
|
|
return;
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
if (Document* doc = GetEntryDocument()) {
|
2018-02-26 22:43:45 +03:00
|
|
|
aRv = NS_MutateURI(uri)
|
|
|
|
.SetQueryWithEncoding(NS_ConvertUTF16toUTF8(aSearch),
|
|
|
|
doc->GetDocumentCharacterSet())
|
|
|
|
.Finalize(uri);
|
2017-08-02 14:43:30 +03:00
|
|
|
} else {
|
2018-02-26 22:43:45 +03:00
|
|
|
aRv = NS_MutateURI(uri)
|
|
|
|
.SetQuery(NS_ConvertUTF16toUTF8(aSearch))
|
|
|
|
.Finalize(uri);
|
2017-08-02 14:43:30 +03:00
|
|
|
}
|
2017-05-08 21:24:13 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
2014-01-14 12:54:58 +04:00
|
|
|
}
|
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
SetURI(uri, aSubjectPrincipal, aRv);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2019-08-02 21:02:11 +03:00
|
|
|
void Location::Reload(bool aForceget, ErrorResult& aRv) {
|
2019-09-17 04:17:17 +03:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(GetDocShell());
|
2019-08-02 19:30:49 +03:00
|
|
|
if (!docShell) {
|
2019-08-02 21:02:11 +03:00
|
|
|
return aRv.Throw(NS_ERROR_FAILURE);
|
2004-10-20 06:19:57 +04:00
|
|
|
}
|
|
|
|
|
2019-08-02 19:30:49 +03:00
|
|
|
if (StaticPrefs::dom_block_reload_from_resize_event_handler()) {
|
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> window = docShell->GetWindow();
|
|
|
|
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.
|
|
|
|
RefPtr<Document> doc = window->GetExtantDoc();
|
|
|
|
|
|
|
|
nsPresContext* pcx;
|
|
|
|
if (doc && (pcx = doc->GetPresContext())) {
|
|
|
|
pcx->RebuildAllStyleData(NS_STYLE_HINT_REFLOW,
|
|
|
|
RestyleHint::RestyleSubtree());
|
|
|
|
}
|
2019-08-02 21:02:11 +03:00
|
|
|
return;
|
2019-08-02 19:30:49 +03:00
|
|
|
}
|
2017-05-08 21:24:13 +03:00
|
|
|
}
|
2017-05-08 20:43:55 +03:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
uint32_t reloadFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
|
|
|
|
|
|
|
|
if (aForceget) {
|
|
|
|
reloadFlags = nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE |
|
|
|
|
nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
|
|
|
|
}
|
|
|
|
|
2019-08-02 19:30:49 +03:00
|
|
|
nsresult rv = nsDocShell::Cast(docShell)->Reload(reloadFlags);
|
2019-08-02 22:10:09 +03:00
|
|
|
if (NS_FAILED(rv) && rv != NS_BINDING_ABORTED) {
|
2019-08-02 21:02:11 +03:00
|
|
|
// NS_BINDING_ABORTED is returned 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.
|
|
|
|
return aRv.Throw(rv);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
1999-08-04 03:16:48 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::Assign(const nsAString& aUrl, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
DoSetHref(aUrl, aSubjectPrincipal, false, aRv);
|
2012-11-07 20:27:23 +04:00
|
|
|
}
|
|
|
|
|
2016-09-29 09:55:21 +03:00
|
|
|
bool Location::CallerSubsumes(nsIPrincipal* aSubjectPrincipal) {
|
|
|
|
MOZ_ASSERT(aSubjectPrincipal);
|
|
|
|
|
2019-09-17 04:17:17 +03:00
|
|
|
RefPtr<BrowsingContext> bc(GetBrowsingContext());
|
|
|
|
if (MOZ_UNLIKELY(!bc) || MOZ_UNLIKELY(bc->IsDiscarded())) {
|
|
|
|
// Per spec, operations on a Location object with a discarded BC are no-ops,
|
|
|
|
// not security errors, so we need to return true from the access check and
|
|
|
|
// let the caller do its own discarded docShell check.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (MOZ_UNLIKELY(!bc->IsInProcess())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-12 03:31:46 +04: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.
|
2019-09-17 04:17:17 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> outer = bc->GetDOMWindow();
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(outer);
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(!outer)) return false;
|
2019-09-17 04:17:17 +03:00
|
|
|
|
2012-10-19 03:37:14 +04:00
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(outer);
|
|
|
|
bool subsumes = false;
|
2016-09-29 09:55:21 +03:00
|
|
|
nsresult rv = aSubjectPrincipal->SubsumesConsideringDomain(
|
|
|
|
sop->GetPrincipal(), &subsumes);
|
2012-10-19 03:37:14 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
return subsumes;
|
|
|
|
}
|
2014-07-12 03:32:46 +04:00
|
|
|
|
2016-09-16 10:18:18 +03:00
|
|
|
JSObject* Location::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return Location_Binding::Wrap(aCx, this, aGivenProto);
|
2014-07-12 03:32:46 +04:00
|
|
|
}
|
2016-09-16 10:18:18 +03:00
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|