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"
|
2003-06-26 04:41:23 +04:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2012-10-19 03:37:14 +04:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2003-06-26 04:41:23 +04:00
|
|
|
#include "nsIScriptContext.h"
|
2000-02-08 16:40:10 +03:00
|
|
|
#include "nsIDocShell.h"
|
2018-10-30 03:13:29 +03:00
|
|
|
#include "nsDocShellLoadState.h"
|
2000-02-24 06:57:32 +03:00
|
|
|
#include "nsIWebNavigation.h"
|
2002-12-12 18:48:30 +03:00
|
|
|
#include "nsIURIFixup.h"
|
1998-08-13 08:34:53 +04:00
|
|
|
#include "nsIURL.h"
|
2018-02-26 22:43:45 +03:00
|
|
|
#include "nsIURIMutator.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"
|
2000-01-15 05:02:27 +03:00
|
|
|
#include "nsIDOMWindow.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"
|
2004-05-21 04:53:40 +04:00
|
|
|
#include "nsITextToSubURI.h"
|
2010-12-15 19:55:13 +03:00
|
|
|
#include "nsJSUtils.h"
|
2012-07-19 11:43:33 +04:00
|
|
|
#include "nsContentUtils.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
|
|
|
|
2016-09-16 10:18:18 +03:00
|
|
|
Location::Location(nsPIDOMWindowInner* aWindow, nsIDocShell* aDocShell)
|
2014-07-12 03:31:46 +04:00
|
|
|
: mInnerWindow(aWindow) {
|
2017-08-28 12:12:47 +03:00
|
|
|
// aDocShell can be null if it gets called after nsDocShell::Destory().
|
2006-06-20 22:56:03 +04:00
|
|
|
mDocShell = do_GetWeakReference(aDocShell);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2016-09-16 10:18:18 +03:00
|
|
|
Location::~Location() {}
|
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
|
|
|
|
2018-10-30 03:13:29 +03:00
|
|
|
already_AddRefed<nsDocShellLoadState> Location::CheckURL(
|
2018-10-12 18:07:18 +03:00
|
|
|
nsIURI* aURI, nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv) {
|
2006-06-20 22:56:03 +04:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
2018-10-12 18:07:18 +03:00
|
|
|
if (NS_WARN_IF(!docShell)) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2006-06-20 22:56:03 +04:00
|
|
|
|
2016-07-28 10:20:41 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> triggeringPrincipal;
|
2010-12-15 19:55:13 +03:00
|
|
|
nsCOMPtr<nsIURI> sourceURI;
|
2017-01-05 06:29:56 +03:00
|
|
|
net::ReferrerPolicy referrerPolicy = net::RP_Unset;
|
2005-02-24 23:19:58 +03:00
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
// Get security manager.
|
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
if (NS_WARN_IF(!ssm)) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2010-12-15 19:55:13 +03:00
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
// Check to see if URI is allowed.
|
|
|
|
nsresult rv = ssm->CheckLoadURIWithPrincipal(
|
|
|
|
&aSubjectPrincipal, aURI, nsIScriptSecurityManager::STANDARD);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
nsAutoCString spec;
|
|
|
|
aURI->GetSpec(spec);
|
|
|
|
aRv.ThrowTypeError<MSG_URL_NOT_LOADABLE>(NS_ConvertUTF8toUTF16(spec));
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// The triggering principal for this load should be the principal of the
|
|
|
|
// incumbent document (which matches where the referrer information is
|
|
|
|
// coming from) when there is an incumbent document, and the subject
|
|
|
|
// principal otherwise. Note that the URI in the triggering principal
|
|
|
|
// may not match the referrer URI in various cases, notably including
|
|
|
|
// the cases when the incumbent document's document URI was modified
|
|
|
|
// after the document was loaded.
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> incumbent =
|
|
|
|
do_QueryInterface(mozilla::dom::GetIncumbentGlobal());
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = incumbent ? incumbent->GetDoc() : nullptr;
|
2018-10-12 18:07:18 +03:00
|
|
|
|
|
|
|
if (doc) {
|
|
|
|
nsCOMPtr<nsIURI> docOriginalURI, docCurrentURI, principalURI;
|
|
|
|
docOriginalURI = doc->GetOriginalURI();
|
|
|
|
docCurrentURI = doc->GetDocumentURI();
|
|
|
|
rv = doc->NodePrincipal()->GetURI(getter_AddRefs(principalURI));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-10-12 19:28:29 +03:00
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
triggeringPrincipal = doc->NodePrincipal();
|
|
|
|
referrerPolicy = doc->GetReferrerPolicy();
|
2018-10-12 19:28:29 +03:00
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
bool urisEqual = false;
|
|
|
|
if (docOriginalURI && docCurrentURI && principalURI) {
|
|
|
|
principalURI->Equals(docOriginalURI, &urisEqual);
|
|
|
|
}
|
|
|
|
if (urisEqual) {
|
|
|
|
sourceURI = docCurrentURI;
|
2018-10-16 00:30:00 +03:00
|
|
|
} else {
|
2018-10-12 18:07:18 +03:00
|
|
|
// Use principalURI as long as it is not an NullPrincipalURI. 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.
|
2019-07-30 20:51:37 +03:00
|
|
|
if (principalURI && !principalURI->SchemeIs(NS_NULLPRINCIPAL_SCHEME)) {
|
|
|
|
sourceURI = principalURI;
|
2018-10-12 18:07:18 +03:00
|
|
|
}
|
2018-10-16 00:30:00 +03:00
|
|
|
}
|
2018-10-12 18:07:18 +03:00
|
|
|
} else {
|
|
|
|
// No document; just use our subject principal as the triggering principal.
|
|
|
|
triggeringPrincipal = &aSubjectPrincipal;
|
2005-02-24 23:19:58 +03:00
|
|
|
}
|
1999-09-07 06:54:19 +04:00
|
|
|
|
2000-08-11 08:31:08 +04:00
|
|
|
// Create load info
|
2018-12-21 23:17:43 +03:00
|
|
|
RefPtr<nsDocShellLoadState> loadState = new nsDocShellLoadState(aURI);
|
2000-08-11 08:31:08 +04:00
|
|
|
|
2018-10-30 03:13:29 +03:00
|
|
|
loadState->SetTriggeringPrincipal(triggeringPrincipal);
|
2019-05-22 02:14:27 +03:00
|
|
|
if (doc) {
|
|
|
|
loadState->SetCsp(doc->GetCsp());
|
|
|
|
}
|
2019-02-21 18:00:32 +03:00
|
|
|
|
2010-12-15 19:55:13 +03:00
|
|
|
if (sourceURI) {
|
2019-02-12 22:35:32 +03:00
|
|
|
nsCOMPtr<nsIReferrerInfo> referrerInfo =
|
|
|
|
new ReferrerInfo(sourceURI, referrerPolicy);
|
|
|
|
loadState->SetReferrerInfo(referrerInfo);
|
2010-12-15 19:55:13 +03:00
|
|
|
}
|
2005-02-24 23:19:58 +03:00
|
|
|
|
2018-10-30 03:13:29 +03:00
|
|
|
return loadState.forget();
|
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
|
|
|
|
2006-06-20 22:56:03 +04:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
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?");
|
|
|
|
|
2018-12-17 04:27:58 +03:00
|
|
|
nsCOMPtr<nsIURIFixup> urifixup(components::URIFixup::Service());
|
2002-12-12 18:48:30 +03:00
|
|
|
|
|
|
|
return urifixup->CreateExposableURI(uri, aURI);
|
2002-03-06 10:48:55 +03:00
|
|
|
}
|
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
void Location::SetURI(nsIURI* aURI, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv, bool aReplace) {
|
2006-06-20 22:56:03 +04:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
|
|
|
if (docShell) {
|
2018-10-30 03:13:29 +03:00
|
|
|
RefPtr<nsDocShellLoadState> loadState =
|
2018-10-12 18:07:18 +03:00
|
|
|
CheckURL(aURI, aSubjectPrincipal, aRv);
|
|
|
|
if (aRv.Failed()) {
|
2018-10-15 21:17:48 +03:00
|
|
|
return;
|
|
|
|
}
|
1999-09-07 06:54:19 +04:00
|
|
|
|
2005-01-20 19:57:04 +03:00
|
|
|
if (aReplace) {
|
2018-10-30 03:13:29 +03:00
|
|
|
loadState->SetLoadType(LOAD_STOP_CONTENT_AND_REPLACE);
|
2005-01-20 19:57:04 +03:00
|
|
|
} else {
|
2018-10-30 03:13:29 +03:00
|
|
|
loadState->SetLoadType(LOAD_STOP_CONTENT);
|
2005-01-20 19:57:04 +03:00
|
|
|
}
|
|
|
|
|
2014-01-13 11:58:16 +04:00
|
|
|
// Get the incumbent script's browsing context to set as source.
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> sourceWindow =
|
2014-01-13 11:58:16 +04:00
|
|
|
do_QueryInterface(mozilla::dom::GetIncumbentGlobal());
|
|
|
|
if (sourceWindow) {
|
2018-10-30 03:13:29 +03:00
|
|
|
loadState->SetSourceDocShell(sourceWindow->GetDocShell());
|
2014-01-13 11:58:16 +04:00
|
|
|
}
|
|
|
|
|
2018-10-30 03:13:29 +03:00
|
|
|
loadState->SetLoadFlags(nsIWebNavigation::LOAD_FLAGS_NONE);
|
|
|
|
loadState->SetFirstParty(true);
|
|
|
|
|
|
|
|
nsresult rv = docShell->LoadURI(loadState);
|
2018-10-15 21:17:48 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
}
|
1998-11-23 04:09:27 +03:00
|
|
|
}
|
1998-08-13 08:34:53 +04: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::SetHref(const nsAString& aHref, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
2018-10-12 18:07:18 +03:00
|
|
|
DoSetHref(aHref, aSubjectPrincipal, false, aRv);
|
1999-08-04 03:16:48 +04:00
|
|
|
}
|
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
void Location::DoSetHref(const nsAString& aHref,
|
|
|
|
nsIPrincipal& aSubjectPrincipal, bool aReplace,
|
|
|
|
ErrorResult& aRv) {
|
2000-08-29 04:14:23 +04:00
|
|
|
// Get the source of the caller
|
2018-10-12 18:07:17 +03:00
|
|
|
nsCOMPtr<nsIURI> base = GetSourceBaseURL();
|
2018-10-12 18:07:18 +03:00
|
|
|
SetHrefWithBase(aHref, base, aSubjectPrincipal, aReplace, aRv);
|
2001-01-09 04:16:36 +03:00
|
|
|
}
|
2000-08-29 04:14:23 +04:00
|
|
|
|
2016-09-16 10:18:18 +03:00
|
|
|
void Location::SetHrefWithBase(const nsAString& aHref, nsIURI* aBase,
|
2018-10-12 18:07:18 +03:00
|
|
|
nsIPrincipal& aSubjectPrincipal, bool aReplace,
|
2018-10-15 21:17:48 +03:00
|
|
|
ErrorResult& aRv) {
|
1999-08-04 03:16:48 +04:00
|
|
|
nsresult result;
|
2009-03-25 04:52:24 +03:00
|
|
|
nsCOMPtr<nsIURI> newUri;
|
2003-03-06 01:23:47 +03:00
|
|
|
|
2013-01-15 15:46:35 +04:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
if (Document* doc = GetEntryDocument()) {
|
2017-06-18 14:37:50 +03:00
|
|
|
result = NS_NewURI(getter_AddRefs(newUri), aHref,
|
|
|
|
doc->GetDocumentCharacterSet(), aBase);
|
|
|
|
} else {
|
2012-07-30 18:20:58 +04:00
|
|
|
result = NS_NewURI(getter_AddRefs(newUri), aHref, nullptr, aBase);
|
2017-06-18 14:37:50 +03:00
|
|
|
}
|
1999-09-01 04:54:35 +04:00
|
|
|
|
2005-01-20 19:57:04 +03:00
|
|
|
if (newUri) {
|
2013-01-15 15:46:35 +04: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.
|
2017-05-08 21:24:13 +03:00
|
|
|
* we want to do a replace load, in such a situation.
|
2013-01-15 15:46:35 +04:00
|
|
|
* 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 23:02:08 +04:00
|
|
|
bool inScriptTag = false;
|
|
|
|
nsIScriptContext* scriptContext = nullptr;
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> win = do_QueryInterface(GetEntryGlobal());
|
2014-08-19 23:02:08 +04:00
|
|
|
if (win) {
|
2017-11-04 01:25:38 +03:00
|
|
|
scriptContext = nsGlobalWindowInner::Cast(win)->GetContextInternal();
|
2014-08-19 23:02:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 15:46:35 +04:00
|
|
|
|
2018-10-12 18:07:18 +03:00
|
|
|
SetURI(newUri, aSubjectPrincipal, aRv, aReplace || inScriptTag);
|
2018-10-15 21:17:48 +03:00
|
|
|
return;
|
2018-09-07 17:47:51 +03:00
|
|
|
}
|
2018-10-15 21:17:48 +03:00
|
|
|
|
|
|
|
aRv.Throw(result);
|
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
|
|
|
}
|
|
|
|
|
2016-09-16 10:18:18 +03:00
|
|
|
nsresult Location::Reload(bool aForceget) {
|
2006-06-20 22:56:03 +04:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
|
|
|
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(docShell));
|
2016-09-16 10:18:18 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> window =
|
|
|
|
docShell ? docShell->GetWindow() : nullptr;
|
2004-10-20 06:19:57 +04:00
|
|
|
|
2019-08-02 19:30:41 +03:00
|
|
|
if (StaticPrefs::dom_block_reload_from_resize_event_handler() && window &&
|
|
|
|
window->IsHandlingResizeEvent()) {
|
2004-10-20 06:19:57 +04:00
|
|
|
// 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.
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = window->GetExtantDoc();
|
2004-10-20 06:19:57 +04:00
|
|
|
|
2018-02-21 01:00:10 +03:00
|
|
|
nsPresContext* pcx;
|
|
|
|
if (doc && (pcx = doc->GetPresContext())) {
|
2019-03-14 14:47:50 +03:00
|
|
|
pcx->RebuildAllStyleData(NS_STYLE_HINT_REFLOW,
|
|
|
|
RestyleHint::RestyleSubtree());
|
2004-10-20 06:19:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
if (!webNav) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult 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;
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2008-09-06 01:54:53 +04:00
|
|
|
return rv;
|
1999-08-04 03:16:48 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
void Location::Replace(const nsAString& aUrl, nsIPrincipal& aSubjectPrincipal,
|
|
|
|
ErrorResult& aRv) {
|
2018-10-12 18:07:18 +03:00
|
|
|
DoSetHref(aUrl, aSubjectPrincipal, true, aRv);
|
2000-09-08 11:29:54 +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
|
|
|
}
|
|
|
|
|
2019-07-15 20:22:30 +03:00
|
|
|
nsIURI* Location::GetSourceBaseURL() {
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = GetEntryDocument();
|
2014-08-19 23:02:07 +04:00
|
|
|
// If there's no entry document, we either have no Script Entry Point or one
|
2016-09-16 10:18:18 +03:00
|
|
|
// 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
|
2012-10-20 01:26:17 +04:00
|
|
|
// an absolute URI.
|
2017-05-08 16:49:08 +03:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
|
|
|
if (!doc && docShell) {
|
2016-09-16 10:18:18 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> docShellWin =
|
2017-05-08 16:49:08 +03:00
|
|
|
do_QueryInterface(docShell->GetScriptGlobalObject());
|
2014-08-19 23:02:07 +04:00
|
|
|
if (docShellWin) {
|
|
|
|
doc = docShellWin->GetDoc();
|
|
|
|
}
|
2012-10-20 01:26:17 +04:00
|
|
|
}
|
2018-10-12 18:07:17 +03:00
|
|
|
NS_ENSURE_TRUE(doc, nullptr);
|
|
|
|
return doc->GetBaseURI();
|
2001-11-17 20:26:18 +03:00
|
|
|
}
|
2012-10-19 03:37:14 +04:00
|
|
|
|
2016-09-29 09:55:21 +03:00
|
|
|
bool Location::CallerSubsumes(nsIPrincipal* aSubjectPrincipal) {
|
|
|
|
MOZ_ASSERT(aSubjectPrincipal);
|
|
|
|
|
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.
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> outer = mInnerWindow->GetOuterWindow();
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(!outer)) return false;
|
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
|