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"
|
2000-03-31 02:38:32 +04:00
|
|
|
#include "nsIDocShellLoadInfo.h"
|
2000-02-24 06:57:32 +03:00
|
|
|
#include "nsIWebNavigation.h"
|
2002-12-12 18:48:30 +03:00
|
|
|
#include "nsCDefaultURIFixup.h"
|
|
|
|
#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"
|
|
|
|
#include "nsIDocument.h"
|
2004-10-20 06:19:57 +04:00
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#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"
|
2017-03-22 13:38:40 +03:00
|
|
|
#include "NullPrincipal.h"
|
2017-02-09 02:19:02 +03:00
|
|
|
#include "mozilla/Unused.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"
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
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
|
|
|
|
2001-01-09 04:16:36 +03:00
|
|
|
nsresult
|
2016-09-16 10:18:18 +03:00
|
|
|
Location::CheckURL(nsIURI* aURI, nsIDocShellLoadInfo** aLoadInfo)
|
1999-09-07 06:54:19 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aLoadInfo = nullptr;
|
2002-07-25 00:42:22 +04:00
|
|
|
|
2006-06-20 22:56:03 +04:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
2010-12-15 19:55:13 +03:00
|
|
|
NS_ENSURE_TRUE(docShell, NS_ERROR_NOT_AVAILABLE);
|
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
|
|
|
|
2012-10-20 01:26:17 +04:00
|
|
|
if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) {
|
2002-07-25 00:42:22 +04: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
|
2005-02-24 23:19:58 +03:00
|
|
|
// does). We won't bother with security checks in this case, but
|
|
|
|
// we need to create the loadinfo etc.
|
2002-07-25 00:42:22 +04:00
|
|
|
|
2005-02-24 23:19:58 +03:00
|
|
|
// Get security manager.
|
2012-10-20 01:26:17 +04:00
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
NS_ENSURE_STATE(ssm);
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2005-02-24 23:19:58 +03:00
|
|
|
// Check to see if URI is allowed.
|
2012-10-20 01:26:17 +04:00
|
|
|
nsresult rv = ssm->CheckLoadURIFromScript(cx, aURI);
|
2010-12-15 19:55:13 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2010-12-15 19:55:13 +03: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.
|
2016-07-26 13:46:01 +03:00
|
|
|
//
|
|
|
|
// 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.
|
2010-12-15 19:55:13 +03:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> incumbent =
|
2014-01-04 02:37:17 +04:00
|
|
|
do_QueryInterface(mozilla::dom::GetIncumbentGlobal());
|
2016-07-26 13:46:01 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = incumbent ? incumbent->GetDoc() : nullptr;
|
|
|
|
|
2012-10-10 13:01:26 +04:00
|
|
|
if (doc) {
|
2016-07-26 13:46:01 +03:00
|
|
|
nsCOMPtr<nsIURI> docOriginalURI, docCurrentURI, principalURI;
|
2012-10-10 13:01:26 +04:00
|
|
|
docOriginalURI = doc->GetOriginalURI();
|
|
|
|
docCurrentURI = doc->GetDocumentURI();
|
|
|
|
rv = doc->NodePrincipal()->GetURI(getter_AddRefs(principalURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-12-15 19:55:13 +03:00
|
|
|
|
2016-07-28 10:20:41 +03:00
|
|
|
triggeringPrincipal = doc->NodePrincipal();
|
2016-07-26 13:46:01 +03:00
|
|
|
referrerPolicy = doc->GetReferrerPolicy();
|
2010-12-15 19:55:13 +03:00
|
|
|
|
2016-07-26 13:46:01 +03:00
|
|
|
bool urisEqual = false;
|
|
|
|
if (docOriginalURI && docCurrentURI && principalURI) {
|
|
|
|
principalURI->Equals(docOriginalURI, &urisEqual);
|
|
|
|
}
|
|
|
|
if (urisEqual) {
|
|
|
|
sourceURI = docCurrentURI;
|
|
|
|
}
|
|
|
|
else {
|
2017-03-22 13:38:40 +03:00
|
|
|
// Use principalURI as long as it is not an NullPrincipalURI. We
|
2016-09-16 10:18:18 +03:00
|
|
|
// 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.
|
2016-07-26 13:46:01 +03:00
|
|
|
if (principalURI) {
|
|
|
|
bool isNullPrincipalScheme;
|
|
|
|
rv = principalURI->SchemeIs(NS_NULLPRINCIPAL_SCHEME,
|
|
|
|
&isNullPrincipalScheme);
|
|
|
|
if (NS_SUCCEEDED(rv) && !isNullPrincipalScheme) {
|
|
|
|
sourceURI = principalURI;
|
|
|
|
}
|
2014-03-29 23:10:27 +04:00
|
|
|
}
|
|
|
|
}
|
2010-12-15 19:55:13 +03:00
|
|
|
}
|
2016-07-26 13:46:01 +03:00
|
|
|
else {
|
|
|
|
// No document; determine triggeringPrincipal by quering the
|
|
|
|
// subjectPrincipal, wich is the principal of the current JS
|
|
|
|
// compartment, or a null principal in case there is no
|
|
|
|
// compartment yet.
|
2016-07-28 10:20:41 +03:00
|
|
|
triggeringPrincipal = nsContentUtils::SubjectPrincipal();
|
2016-07-26 13:46:01 +03:00
|
|
|
}
|
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
|
|
|
|
nsCOMPtr<nsIDocShellLoadInfo> loadInfo;
|
2006-06-20 22:56:03 +04:00
|
|
|
docShell->CreateLoadInfo(getter_AddRefs(loadInfo));
|
2000-08-11 08:31:08 +04:00
|
|
|
NS_ENSURE_TRUE(loadInfo, NS_ERROR_FAILURE);
|
|
|
|
|
2016-07-28 10:20:41 +03:00
|
|
|
loadInfo->SetTriggeringPrincipal(triggeringPrincipal);
|
2000-08-11 08:31:08 +04:00
|
|
|
|
2010-12-15 19:55:13 +03:00
|
|
|
if (sourceURI) {
|
2001-11-17 20:26:18 +03:00
|
|
|
loadInfo->SetReferrer(sourceURI);
|
2014-11-18 16:47:03 +03:00
|
|
|
loadInfo->SetReferrerPolicy(referrerPolicy);
|
2010-12-15 19:55:13 +03:00
|
|
|
}
|
2005-02-24 23:19:58 +03:00
|
|
|
|
|
|
|
loadInfo.swap(*aLoadInfo);
|
2001-01-09 04:16:36 +03:00
|
|
|
|
1999-09-07 06:54:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsresult
|
2016-09-16 10:18:18 +03:00
|
|
|
Location::GetURI(nsIURI** aURI, bool aGetInnermostURI)
|
2002-03-06 10:48:55 +03:00
|
|
|
{
|
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));
|
2017-05-08 16:49:08 +03:00
|
|
|
if (!mDocShell) {
|
|
|
|
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?");
|
|
|
|
|
2002-12-12 18:48:30 +03:00
|
|
|
nsCOMPtr<nsIURIFixup> urifixup(do_GetService(NS_URIFIXUP_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return urifixup->CreateExposableURI(uri, aURI);
|
2002-03-06 10:48:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-09-16 10:18:18 +03:00
|
|
|
Location::GetWritableURI(nsIURI** aURI, const nsACString* aNewRef)
|
2002-03-06 10:48:55 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aURI = nullptr;
|
2002-03-23 03:48:40 +03:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
2002-03-23 03:48:40 +03:00
|
|
|
if (NS_FAILED(rv) || !uri) {
|
|
|
|
return rv;
|
|
|
|
}
|
2002-03-06 10:48:55 +03:00
|
|
|
|
2016-08-30 19:55:40 +03:00
|
|
|
if (!aNewRef) {
|
2018-06-14 14:05:43 +03:00
|
|
|
uri.forget(aURI);
|
|
|
|
return NS_OK;
|
2016-08-30 19:55:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return uri->CloneWithNewRef(*aNewRef, aURI);
|
2002-03-06 10:48:55 +03:00
|
|
|
}
|
1999-09-07 06:54:19 +04:00
|
|
|
|
2001-01-09 04:16:36 +03:00
|
|
|
nsresult
|
2016-09-16 10:18:18 +03:00
|
|
|
Location::SetURI(nsIURI* aURI, bool aReplace)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2006-06-20 22:56:03 +04:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
|
|
|
if (docShell) {
|
2000-03-31 02:38:32 +04:00
|
|
|
nsCOMPtr<nsIDocShellLoadInfo> loadInfo;
|
2001-01-09 04:16:36 +03:00
|
|
|
|
|
|
|
if(NS_FAILED(CheckURL(aURI, getter_AddRefs(loadInfo))))
|
1999-09-07 06:54:19 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2005-01-20 19:57:04 +03:00
|
|
|
if (aReplace) {
|
|
|
|
loadInfo->SetLoadType(nsIDocShellLoadInfo::loadStopContentAndReplace);
|
|
|
|
} else {
|
|
|
|
loadInfo->SetLoadType(nsIDocShellLoadInfo::loadStopContent);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
loadInfo->SetSourceDocShell(sourceWindow->GetDocShell());
|
|
|
|
}
|
|
|
|
|
2006-06-20 22:56:03 +04:00
|
|
|
return docShell->LoadURI(aURI, loadInfo,
|
2011-10-17 18:59:28 +04:00
|
|
|
nsIWebNavigation::LOAD_FLAGS_NONE, true);
|
1998-11-23 04:09:27 +03:00
|
|
|
}
|
2001-01-09 04:16:36 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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;
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = GetWritableURI(getter_AddRefs(uri), &hash);
|
|
|
|
if (NS_WARN_IF(aRv.Failed()) || !uri) {
|
|
|
|
return;
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = SetURI(uri);
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = SetURI(uri);
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = SetURI(uri);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
nsresult
|
2017-05-08 21:14:21 +03:00
|
|
|
Location::GetHref(nsAString& aHref)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
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,
|
|
|
|
ErrorResult& aRv)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2012-10-20 01:26:17 +04:00
|
|
|
JSContext *cx = nsContentUtils::GetCurrentJSContext();
|
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
|
|
|
if (cx) {
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = SetHrefWithContext(cx, aHref, false);
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
nsAutoString oldHref;
|
|
|
|
aRv = GetHref(oldHref);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
1999-08-04 03:16:48 +04:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
nsCOMPtr<nsIURI> oldUri;
|
|
|
|
aRv = NS_NewURI(getter_AddRefs(oldUri), oldHref);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
2017-05-08 20:43:55 +03:00
|
|
|
}
|
2017-05-08 21:14:21 +03:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = SetHrefWithBase(aHref, oldUri, false);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
1999-08-04 03:16:48 +04:00
|
|
|
}
|
|
|
|
|
2000-08-29 04:14:23 +04:00
|
|
|
nsresult
|
2016-09-16 10:18:18 +03:00
|
|
|
Location::SetHrefWithContext(JSContext* cx, const nsAString& aHref,
|
2017-05-08 21:24:13 +03:00
|
|
|
bool aReplace)
|
2000-08-29 04:14:23 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> base;
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2000-08-29 04:14:23 +04:00
|
|
|
// Get the source of the caller
|
2001-11-17 20:26:18 +03:00
|
|
|
nsresult result = GetSourceBaseURL(cx, getter_AddRefs(base));
|
2001-01-09 04:16:36 +03:00
|
|
|
|
2000-08-29 04:14:23 +04:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-05-16 00:58:31 +04:00
|
|
|
return SetHrefWithBase(aHref, base, aReplace);
|
2001-01-09 04:16:36 +03:00
|
|
|
}
|
2000-08-29 04:14:23 +04:00
|
|
|
|
1999-08-04 03:16:48 +04:00
|
|
|
nsresult
|
2016-09-16 10:18:18 +03:00
|
|
|
Location::SetHrefWithBase(const nsAString& aHref, nsIURI* aBase,
|
2017-05-08 21:24:13 +03:00
|
|
|
bool aReplace)
|
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));
|
|
|
|
|
2017-06-18 14:37:50 +03:00
|
|
|
if (nsIDocument* doc = GetEntryDocument()) {
|
|
|
|
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
|
|
|
|
|
|
|
return SetURI(newUri, aReplace || inScriptTag);
|
2017-11-04 01:25:38 +03:00
|
|
|
}
|
1998-11-23 04:09:27 +03:00
|
|
|
return 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)
|
2013-01-09 20:37:25 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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
|
|
|
|
|
|
|
aRv = SetURI(uri);
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = SetURI(uri);
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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
|
|
|
|
2017-02-09 02:19:01 +03:00
|
|
|
bool isHttp;
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = uri->SchemeIs("http", &isHttp);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
2017-02-09 02:19:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isHttps;
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = uri->SchemeIs("https", &isHttps);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
2017-02-09 02:19:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isHttp && !isHttps) {
|
|
|
|
// No-op, per spec.
|
2017-05-08 21:24:13 +03:00
|
|
|
return;
|
2017-02-09 02:19:01 +03:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = SetURI(uri);
|
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)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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)
|
2014-08-08 04:45:21 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-02 14:43:30 +03:00
|
|
|
if (nsIDocument* 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
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = SetURI(uri);
|
1998-08-13 08:34:53 +04:00
|
|
|
}
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
nsresult
|
2016-09-16 10:18:18 +03:00
|
|
|
Location::Reload(bool aForceget)
|
1998-08-13 08:34:53 +04:00
|
|
|
{
|
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
|
|
|
|
|
|
|
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-24 08:22:37 +04:00
|
|
|
nsCOMPtr<nsIDocument> 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())) {
|
2014-10-09 01:26:57 +04:00
|
|
|
pcx->RebuildAllStyleData(NS_STYLE_HINT_REFLOW, eRestyle_Subtree);
|
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)
|
2000-09-08 11:29:54 +04:00
|
|
|
{
|
2012-10-20 01:26:17 +04:00
|
|
|
if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) {
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = SetHrefWithContext(cx, aUrl, true);
|
|
|
|
return;
|
2000-09-08 11:29:54 +04:00
|
|
|
}
|
|
|
|
|
2001-05-16 00:58:31 +04:00
|
|
|
nsAutoString oldHref;
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = GetHref(oldHref);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2001-05-16 00:58:31 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> oldUri;
|
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = NS_NewURI(getter_AddRefs(oldUri), oldHref);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2001-05-16 00:58:31 +04:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = SetHrefWithBase(aUrl, oldUri, true);
|
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)
|
1999-08-04 03:16:48 +04:00
|
|
|
{
|
2017-05-08 21:24:13 +03:00
|
|
|
if (!CallerSubsumes(&aSubjectPrincipal)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-14 16:58:09 +03:00
|
|
|
if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) {
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = SetHrefWithContext(cx, aUrl, false);
|
|
|
|
return;
|
2016-04-14 16:58:09 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
nsAutoString oldHref;
|
2017-05-08 21:24:13 +03:00
|
|
|
aRv = GetHref(oldHref);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
2017-05-08 20:43:55 +03:00
|
|
|
}
|
2017-05-08 21:14:21 +03:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
nsCOMPtr<nsIURI> oldUri;
|
|
|
|
aRv = NS_NewURI(getter_AddRefs(oldUri), oldHref);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-08 21:14:21 +03:00
|
|
|
|
2017-05-08 21:24:13 +03:00
|
|
|
if (oldUri) {
|
|
|
|
aRv = SetHrefWithBase(aUrl, oldUri, false);
|
|
|
|
}
|
2012-11-07 20:27:23 +04:00
|
|
|
}
|
|
|
|
|
2001-11-17 20:26:18 +03:00
|
|
|
nsresult
|
2016-09-16 10:18:18 +03:00
|
|
|
Location::GetSourceBaseURL(JSContext* cx, nsIURI** sourceURL)
|
2001-11-17 20:26:18 +03:00
|
|
|
{
|
2012-10-20 01:26:17 +04:00
|
|
|
*sourceURL = nullptr;
|
2014-08-19 23:02:07 +04:00
|
|
|
nsIDocument* doc = GetEntryDocument();
|
|
|
|
// 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
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(doc, NS_OK);
|
2014-03-15 23:00:15 +04:00
|
|
|
*sourceURL = doc->GetBaseURI().take();
|
2012-10-20 01:26:17 +04:00
|
|
|
return NS_OK;
|
2001-11-17 20:26:18 +03:00
|
|
|
}
|
2012-10-19 03:37:14 +04:00
|
|
|
|
|
|
|
bool
|
2016-09-29 09:55:21 +03:00
|
|
|
Location::CallerSubsumes(nsIPrincipal* aSubjectPrincipal)
|
2012-10-19 03:37:14 +04:00
|
|
|
{
|
2016-09-29 09:55:21 +03:00
|
|
|
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))
|
2012-10-19 03:37:14 +04:00
|
|
|
return false;
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(outer);
|
|
|
|
bool subsumes = false;
|
2016-09-16 10:18:18 +03:00
|
|
|
nsresult rv =
|
2016-09-29 09:55:21 +03:00
|
|
|
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
|
|
|
|
|
|
|
JSObject*
|
2016-09-16 10:18:18 +03:00
|
|
|
Location::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-07-12 03:32:46 +04:00
|
|
|
{
|
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
|
|
|
|
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|