2001-09-25 22:43:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2005-10-17 18:47:13 +00:00
|
|
|
/* vim: set ts=2 sw=2 et tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1998-08-13 04:34:53 +00:00
|
|
|
|
2011-08-03 04:22:40 +00:00
|
|
|
#include "nsLocation.h"
|
2003-06-26 00:41:23 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2012-10-18 23:37:14 +00:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2003-06-26 00:41:23 +00:00
|
|
|
#include "nsIScriptContext.h"
|
2000-02-08 13:40:10 +00:00
|
|
|
#include "nsIDocShell.h"
|
2000-03-30 22:38:32 +00:00
|
|
|
#include "nsIDocShellLoadInfo.h"
|
2000-02-24 03:57:32 +00:00
|
|
|
#include "nsIWebNavigation.h"
|
2002-12-12 15:48:30 +00:00
|
|
|
#include "nsCDefaultURIFixup.h"
|
|
|
|
#include "nsIURIFixup.h"
|
1998-08-13 04:34:53 +00:00
|
|
|
#include "nsIURL.h"
|
2004-04-24 16:09:31 +00:00
|
|
|
#include "nsIJARURI.h"
|
1999-11-30 04:50:42 +00:00
|
|
|
#include "nsNetUtil.h"
|
1999-08-03 23:16:48 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2002-03-06 07:48:55 +00:00
|
|
|
#include "nsEscape.h"
|
2000-01-15 02:02:27 +00:00
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIDocument.h"
|
2004-10-20 02:19:57 +00:00
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsPresContext.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2011-10-03 19:11:31 +00:00
|
|
|
#include "nsDOMClassInfoID.h"
|
2003-12-23 15:41:10 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2004-05-21 00:53:40 +00:00
|
|
|
#include "nsITextToSubURI.h"
|
2010-12-15 16:55:13 +00:00
|
|
|
#include "nsJSUtils.h"
|
2012-07-19 07:43:33 +00:00
|
|
|
#include "nsContentUtils.h"
|
2012-10-26 13:32:10 +00:00
|
|
|
#include "mozilla/Likely.h"
|
2012-11-06 22:29:09 +00:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2014-01-03 22:37:17 +00:00
|
|
|
#include "ScriptSettings.h"
|
2001-11-14 23:45:23 +00:00
|
|
|
|
2004-02-09 22:48:53 +00:00
|
|
|
static nsresult
|
|
|
|
GetDocumentCharacterSetForURI(const nsAString& aHref, nsACString& aCharset)
|
2001-11-14 23:45:23 +00:00
|
|
|
{
|
2002-08-13 23:26:05 +00:00
|
|
|
aCharset.Truncate();
|
2001-11-14 23:45:23 +00:00
|
|
|
|
2012-10-19 21:26:17 +00:00
|
|
|
JSContext *cx = nsContentUtils::GetCurrentJSContext();
|
2004-03-03 18:13:00 +00:00
|
|
|
if (cx) {
|
2013-07-23 23:39:17 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window =
|
2004-03-03 18:13:00 +00:00
|
|
|
do_QueryInterface(nsJSUtils::GetDynamicScriptGlobal(cx));
|
|
|
|
NS_ENSURE_TRUE(window, NS_ERROR_FAILURE);
|
2001-11-14 23:45:23 +00:00
|
|
|
|
2013-07-23 23:39:17 +00:00
|
|
|
if (nsIDocument* doc = window->GetDoc()) {
|
2004-03-03 18:13:00 +00:00
|
|
|
aCharset = doc->GetDocumentCharacterSet();
|
|
|
|
}
|
|
|
|
}
|
2001-11-14 23:45:23 +00:00
|
|
|
|
2003-10-22 06:09:48 +00:00
|
|
|
return NS_OK;
|
2001-11-14 23:45:23 +00:00
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::nsLocation(nsIDocShell *aDocShell)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2006-06-20 18:56:03 +00:00
|
|
|
mDocShell = do_GetWeakReference(aDocShell);
|
2012-10-18 23:37:14 +00:00
|
|
|
nsCOMPtr<nsIDOMWindow> outer = do_GetInterface(aDocShell);
|
|
|
|
mOuter = do_GetWeakReference(outer);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::~nsLocation()
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-01-12 13:08:43 +00:00
|
|
|
DOMCI_DATA(Location, nsLocation)
|
1998-08-13 04:34:53 +00:00
|
|
|
|
2004-12-10 19:48:22 +00:00
|
|
|
// QueryInterface implementation for nsLocation
|
2012-11-06 22:29:09 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsLocation)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
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 17:42:36 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMLocation)
|
2012-11-06 22:29:09 +00: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 17:42:36 +00:00
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(Location)
|
|
|
|
NS_INTERFACE_MAP_END
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2012-11-06 22:29:09 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(nsLocation)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsLocation)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsLocation)
|
1998-08-13 04:34:53 +00:00
|
|
|
|
2004-02-09 22:48:53 +00:00
|
|
|
void
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::SetDocShell(nsIDocShell *aDocShell)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2006-06-20 18:56:03 +00:00
|
|
|
mDocShell = do_GetWeakReference(aDocShell);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocShell *
|
|
|
|
nsLocation::GetDocShell()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocShell> docshell(do_QueryReferent(mDocShell));
|
|
|
|
return docshell;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
nsresult
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::CheckURL(nsIURI* aURI, nsIDocShellLoadInfo** aLoadInfo)
|
1999-09-07 02:54:19 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
*aLoadInfo = nullptr;
|
2002-07-24 20:42:22 +00:00
|
|
|
|
2006-06-20 18:56:03 +00:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
2010-12-15 16:55:13 +00:00
|
|
|
NS_ENSURE_TRUE(docShell, NS_ERROR_NOT_AVAILABLE);
|
2006-06-20 18:56:03 +00:00
|
|
|
|
2005-02-24 20:19:58 +00:00
|
|
|
nsCOMPtr<nsISupports> owner;
|
2010-12-15 16:55:13 +00:00
|
|
|
nsCOMPtr<nsIURI> sourceURI;
|
2005-02-24 20:19:58 +00:00
|
|
|
|
2012-10-19 21:26:17 +00:00
|
|
|
if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) {
|
2002-07-24 20:42:22 +00: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 20:19:58 +00:00
|
|
|
// does). We won't bother with security checks in this case, but
|
|
|
|
// we need to create the loadinfo etc.
|
2002-07-24 20:42:22 +00:00
|
|
|
|
2005-02-24 20:19:58 +00:00
|
|
|
// Get security manager.
|
2012-10-19 21:26:17 +00:00
|
|
|
nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
NS_ENSURE_STATE(ssm);
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2005-02-24 20:19:58 +00:00
|
|
|
// Check to see if URI is allowed.
|
2012-10-19 21:26:17 +00:00
|
|
|
nsresult rv = ssm->CheckLoadURIFromScript(cx, aURI);
|
2010-12-15 16:55:13 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2010-12-15 16:55:13 +00:00
|
|
|
// Make the load's referrer reflect changes to the document's URI caused by
|
|
|
|
// push/replaceState, if possible. First, get the document corresponding to
|
|
|
|
// fp. If the document's original URI (i.e. its URI before
|
|
|
|
// push/replaceState) matches the principal's URI, use the document's
|
|
|
|
// current URI as the referrer. If they don't match, use the principal's
|
|
|
|
// URI.
|
|
|
|
|
2012-10-17 17:44:30 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc;
|
2012-10-10 09:01:26 +00:00
|
|
|
nsCOMPtr<nsIURI> docOriginalURI, docCurrentURI, principalURI;
|
2014-01-03 22:37:17 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> incumbent =
|
|
|
|
do_QueryInterface(mozilla::dom::GetIncumbentGlobal());
|
|
|
|
if (incumbent) {
|
|
|
|
doc = incumbent->GetDoc();
|
2012-10-17 17:44:30 +00:00
|
|
|
}
|
2012-10-10 09:01:26 +00:00
|
|
|
if (doc) {
|
|
|
|
docOriginalURI = doc->GetOriginalURI();
|
|
|
|
docCurrentURI = doc->GetDocumentURI();
|
|
|
|
rv = doc->NodePrincipal()->GetURI(getter_AddRefs(principalURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-12-15 16:55:13 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool urisEqual = false;
|
2010-12-15 16:55:13 +00:00
|
|
|
if (docOriginalURI && docCurrentURI && principalURI) {
|
|
|
|
principalURI->Equals(docOriginalURI, &urisEqual);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (urisEqual) {
|
|
|
|
sourceURI = docCurrentURI;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sourceURI = principalURI;
|
|
|
|
}
|
|
|
|
|
2012-10-19 21:26:17 +00:00
|
|
|
owner = do_QueryInterface(ssm->GetCxSubjectPrincipal(cx));
|
2005-02-24 20:19:58 +00:00
|
|
|
}
|
1999-09-07 02:54:19 +00:00
|
|
|
|
2000-08-11 04:31:08 +00:00
|
|
|
// Create load info
|
|
|
|
nsCOMPtr<nsIDocShellLoadInfo> loadInfo;
|
2006-06-20 18:56:03 +00:00
|
|
|
docShell->CreateLoadInfo(getter_AddRefs(loadInfo));
|
2000-08-11 04:31:08 +00:00
|
|
|
NS_ENSURE_TRUE(loadInfo, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
loadInfo->SetOwner(owner);
|
|
|
|
|
2010-12-15 16:55:13 +00:00
|
|
|
if (sourceURI) {
|
2001-11-17 17:26:18 +00:00
|
|
|
loadInfo->SetReferrer(sourceURI);
|
2010-12-15 16:55:13 +00:00
|
|
|
}
|
2005-02-24 20:19:58 +00:00
|
|
|
|
|
|
|
loadInfo.swap(*aLoadInfo);
|
2001-01-09 01:16:36 +00:00
|
|
|
|
1999-09-07 02:54:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsLocation::GetURI(nsIURI** aURI, bool aGetInnermostURI)
|
2002-03-06 07:48:55 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
*aURI = nullptr;
|
2002-03-23 00:48:40 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsresult rv;
|
2006-06-20 18:56:03 +00:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
|
|
|
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(docShell, &rv));
|
2002-03-23 00:48:40 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2002-03-06 07:48:55 +00:00
|
|
|
|
2002-12-12 15:48:30 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = webNav->GetCurrentURI(getter_AddRefs(uri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2003-01-05 06:34:53 +00:00
|
|
|
// It is valid for docshell to return a null URI. Don't try to fixup
|
|
|
|
// if this happens.
|
|
|
|
if (!uri) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-04-24 16:09:31 +00: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 15:48:30 +00:00
|
|
|
nsCOMPtr<nsIURIFixup> urifixup(do_GetService(NS_URIFIXUP_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return urifixup->CreateExposableURI(uri, aURI);
|
2002-03-06 07:48:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::GetWritableURI(nsIURI** aURI)
|
2002-03-06 07:48:55 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
*aURI = nullptr;
|
2002-03-23 00:48:40 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
2002-03-23 00:48:40 +00:00
|
|
|
if (NS_FAILED(rv) || !uri) {
|
|
|
|
return rv;
|
|
|
|
}
|
2002-03-06 07:48:55 +00:00
|
|
|
|
|
|
|
return uri->Clone(aURI);
|
|
|
|
}
|
1999-09-07 02:54:19 +00:00
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsLocation::SetURI(nsIURI* aURI, bool aReplace)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2006-06-20 18:56:03 +00:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
|
|
|
if (docShell) {
|
2000-03-30 22:38:32 +00:00
|
|
|
nsCOMPtr<nsIDocShellLoadInfo> loadInfo;
|
2006-06-20 18:56:03 +00:00
|
|
|
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(docShell));
|
2001-01-09 01:16:36 +00:00
|
|
|
|
|
|
|
if(NS_FAILED(CheckURL(aURI, getter_AddRefs(loadInfo))))
|
1999-09-07 02:54:19 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2005-01-20 16:57:04 +00:00
|
|
|
if (aReplace) {
|
|
|
|
loadInfo->SetLoadType(nsIDocShellLoadInfo::loadStopContentAndReplace);
|
|
|
|
} else {
|
|
|
|
loadInfo->SetLoadType(nsIDocShellLoadInfo::loadStopContent);
|
|
|
|
}
|
|
|
|
|
2006-06-20 18:56:03 +00:00
|
|
|
return docShell->LoadURI(aURI, loadInfo,
|
2011-10-17 14:59:28 +00:00
|
|
|
nsIWebNavigation::LOAD_FLAGS_NONE, true);
|
1998-11-23 01:09:27 +00:00
|
|
|
}
|
2001-01-09 01:16:36 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::GetHash(nsAString& aHash)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
aHash.SetLength(0);
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2004-05-21 00:53:40 +00:00
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri));
|
2011-05-22 01:12:46 +00:00
|
|
|
if (NS_FAILED(rv) || !uri) {
|
|
|
|
return rv;
|
|
|
|
}
|
1999-06-18 17:34:08 +00:00
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString ref;
|
2011-05-22 01:12:46 +00:00
|
|
|
nsAutoString unicodeRef;
|
2002-03-23 00:48:40 +00:00
|
|
|
|
2011-05-22 01:12:46 +00:00
|
|
|
rv = uri->GetRef(ref);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsITextToSubURI> textToSubURI(
|
|
|
|
do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv));
|
2004-05-21 00:53:40 +00:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString charset;
|
2011-05-22 01:12:46 +00:00
|
|
|
uri->GetOriginCharset(charset);
|
2004-05-21 00:53:40 +00:00
|
|
|
|
2011-05-22 01:12:46 +00:00
|
|
|
rv = textToSubURI->UnEscapeURIForUI(charset, ref, unicodeRef);
|
|
|
|
}
|
2004-05-21 00:53:40 +00:00
|
|
|
|
2011-05-22 01:12:46 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Oh, well. No intl here!
|
|
|
|
NS_UnescapeURL(ref);
|
|
|
|
CopyASCIItoUTF16(ref, unicodeRef);
|
|
|
|
rv = NS_OK;
|
2004-05-21 00:53:40 +00:00
|
|
|
}
|
2011-05-22 01:12:46 +00:00
|
|
|
}
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2011-05-22 01:12:46 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && !unicodeRef.IsEmpty()) {
|
2014-01-04 15:02:17 +00:00
|
|
|
aHash.Assign(char16_t('#'));
|
2011-05-22 01:12:46 +00:00
|
|
|
aHash.Append(unicodeRef);
|
|
|
|
}
|
2011-05-20 20:21:27 +00:00
|
|
|
|
2011-05-22 01:12:46 +00: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 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2004-05-21 00:53:40 +00:00
|
|
|
return rv;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::SetHash(const nsAString& aHash)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-04 06:42:28 +00:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2011-05-22 01:12:46 +00:00
|
|
|
if (NS_FAILED(rv) || !uri) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2011-05-22 01:12:46 +00:00
|
|
|
NS_ConvertUTF16toUTF8 hash(aHash);
|
2014-01-04 15:02:17 +00:00
|
|
|
if (hash.IsEmpty() || hash.First() != char16_t('#')) {
|
|
|
|
hash.Insert(char16_t('#'), 0);
|
2011-05-22 01:12:46 +00:00
|
|
|
}
|
|
|
|
rv = uri->SetRef(hash);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
SetURI(uri);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2007-05-04 06:42:28 +00:00
|
|
|
return rv;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::GetHost(nsAString& aHost)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2003-12-23 15:41:10 +00:00
|
|
|
aHost.Truncate();
|
2002-03-23 00:48:40 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult result;
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
result = GetURI(getter_AddRefs(uri), true);
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (uri) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString hostport;
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
result = uri->GetHostPort(hostport);
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2003-12-23 15:41:10 +00:00
|
|
|
AppendUTF8toUTF16(hostport, aHost);
|
2002-03-23 00:48:40 +00:00
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2010-08-07 07:11:43 +00:00
|
|
|
return NS_OK;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::SetHost(const nsAString& aHost)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-04 06:42:28 +00:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (uri) {
|
2007-05-04 06:42:28 +00:00
|
|
|
rv = uri->SetHostPort(NS_ConvertUTF16toUTF8(aHost));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
SetURI(uri);
|
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2007-05-04 06:42:28 +00:00
|
|
|
return rv;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::GetHostname(nsAString& aHostname)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2003-12-23 15:41:10 +00:00
|
|
|
aHostname.Truncate();
|
2002-03-23 00:48:40 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult result;
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
result = GetURI(getter_AddRefs(uri), true);
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (uri) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString host;
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
result = uri->GetHost(host);
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2003-12-23 15:41:10 +00:00
|
|
|
AppendUTF8toUTF16(host, aHostname);
|
2002-03-23 00:48:40 +00:00
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2010-08-07 07:11:43 +00:00
|
|
|
return NS_OK;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::SetHostname(const nsAString& aHostname)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-04 06:42:28 +00:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (uri) {
|
2007-05-04 06:42:28 +00:00
|
|
|
rv = uri->SetHost(NS_ConvertUTF16toUTF8(aHostname));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
SetURI(uri);
|
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2007-05-04 06:42:28 +00:00
|
|
|
return rv;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::GetHref(nsAString& aHref)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2003-12-23 15:41:10 +00:00
|
|
|
aHref.Truncate();
|
2002-03-23 00:48:40 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult result;
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
result = GetURI(getter_AddRefs(uri));
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (uri) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString uriString;
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
result = uri->GetSpec(uriString);
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2003-12-23 15:41:10 +00:00
|
|
|
AppendUTF8toUTF16(uriString, aHref);
|
2002-03-23 00:48:40 +00:00
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::SetHref(const nsAString& aHref)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
1999-08-03 23:16:48 +00:00
|
|
|
nsAutoString oldHref;
|
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 17:42:36 +00:00
|
|
|
nsresult rv = NS_OK;
|
1998-11-23 01:09:27 +00:00
|
|
|
|
2012-10-19 21:26:17 +00: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 17:42:36 +00:00
|
|
|
if (cx) {
|
2013-01-15 11:46:35 +00:00
|
|
|
rv = SetHrefWithContext(cx, aHref, false);
|
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 17:42:36 +00:00
|
|
|
} else {
|
|
|
|
rv = GetHref(oldHref);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIURI> oldUri;
|
|
|
|
|
|
|
|
rv = NS_NewURI(getter_AddRefs(oldUri), oldHref);
|
|
|
|
|
|
|
|
if (oldUri) {
|
2013-01-15 11:46:35 +00:00
|
|
|
rv = SetHrefWithBase(aHref, oldUri, false);
|
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 17:42:36 +00:00
|
|
|
}
|
1999-08-03 23:16:48 +00: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 17:42:36 +00:00
|
|
|
return rv;
|
1999-08-03 23:16:48 +00:00
|
|
|
}
|
|
|
|
|
2000-08-29 00:14:23 +00:00
|
|
|
nsresult
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::SetHrefWithContext(JSContext* cx, const nsAString& aHref,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aReplace)
|
2000-08-29 00:14:23 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> base;
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2000-08-29 00:14:23 +00:00
|
|
|
// Get the source of the caller
|
2001-11-17 17:26:18 +00:00
|
|
|
nsresult result = GetSourceBaseURL(cx, getter_AddRefs(base));
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2000-08-29 00:14:23 +00:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-05-15 20:58:31 +00:00
|
|
|
return SetHrefWithBase(aHref, base, aReplace);
|
2001-01-09 01:16:36 +00:00
|
|
|
}
|
2000-08-29 00:14:23 +00:00
|
|
|
|
1999-08-03 23:16:48 +00:00
|
|
|
nsresult
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::SetHrefWithBase(const nsAString& aHref, nsIURI* aBase,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aReplace)
|
1999-08-03 23:16:48 +00:00
|
|
|
{
|
|
|
|
nsresult result;
|
2009-03-25 01:52:24 +00:00
|
|
|
nsCOMPtr<nsIURI> newUri;
|
2003-03-05 22:23:47 +00:00
|
|
|
|
2013-01-15 11:46:35 +00:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString docCharset;
|
2002-08-13 23:26:05 +00:00
|
|
|
if (NS_SUCCEEDED(GetDocumentCharacterSetForURI(aHref, docCharset)))
|
2009-03-25 01:52:24 +00:00
|
|
|
result = NS_NewURI(getter_AddRefs(newUri), aHref, docCharset.get(), aBase);
|
2001-11-14 23:45:23 +00:00
|
|
|
else
|
2012-07-30 14:20:58 +00:00
|
|
|
result = NS_NewURI(getter_AddRefs(newUri), aHref, nullptr, aBase);
|
1999-09-01 00:54:35 +00:00
|
|
|
|
2005-01-20 16:57:04 +00:00
|
|
|
if (newUri) {
|
2013-01-15 11:46:35 +00:00
|
|
|
/* Check with the scriptContext if it is currently processing a script tag.
|
|
|
|
* If so, this must be a <script> tag with a location.href in it.
|
|
|
|
* we want to do a replace load, in such a situation.
|
|
|
|
* In other cases, for example if a event handler or a JS timer
|
|
|
|
* had a location.href in it, we want to do a normal load,
|
|
|
|
* so that the new url will be appended to Session History.
|
|
|
|
* This solution is tricky. Hopefully it isn't going to bite
|
|
|
|
* anywhere else. This is part of solution for bug # 39938, 72197
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
bool inScriptTag=false;
|
|
|
|
JSContext *cx = nsContentUtils::GetCurrentJSContext();
|
|
|
|
if (cx) {
|
|
|
|
nsIScriptContext *scriptContext =
|
|
|
|
nsJSUtils::GetDynamicScriptContext(cx);
|
|
|
|
|
|
|
|
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(do_GetInterface(docShell));
|
|
|
|
inScriptTag = (ourGlobal == scriptContext->GetGlobalObject());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} //cx
|
|
|
|
|
|
|
|
return SetURI(newUri, aReplace || inScriptTag);
|
1998-11-23 01:09:27 +00:00
|
|
|
}
|
2001-01-09 01:16:36 +00:00
|
|
|
|
1998-11-23 01:09:27 +00:00
|
|
|
return result;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 16:37:25 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::GetOrigin(nsAString& aOrigin)
|
|
|
|
{
|
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
|
|
|
aOrigin.Truncate();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = GetURI(getter_AddRefs(uri), true);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString origin;
|
|
|
|
rv = nsContentUtils::GetUTFOrigin(uri, origin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aOrigin = origin;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::GetPathname(nsAString& aPathname)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2003-12-23 15:41:10 +00:00
|
|
|
aPathname.Truncate();
|
2002-03-23 00:48:40 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
result = GetURI(getter_AddRefs(uri));
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (url) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString file;
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
result = url->GetFilePath(file);
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2003-12-23 15:41:10 +00:00
|
|
|
AppendUTF8toUTF16(file, aPathname);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::SetPathname(const nsAString& aPathname)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-04 06:42:28 +00:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (uri) {
|
2007-05-04 06:42:28 +00:00
|
|
|
rv = uri->SetPath(NS_ConvertUTF16toUTF8(aPathname));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
SetURI(uri);
|
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2007-05-04 06:42:28 +00:00
|
|
|
return rv;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::GetPort(nsAString& aPort)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
aPort.SetLength(0);
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
result = GetURI(getter_AddRefs(uri), true);
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (uri) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t port;
|
2005-07-19 21:12:18 +00:00
|
|
|
result = uri->GetPort(&port);
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2005-07-19 21:12:18 +00:00
|
|
|
if (NS_SUCCEEDED(result) && -1 != port) {
|
2002-03-06 07:48:55 +00:00
|
|
|
nsAutoString portStr;
|
|
|
|
portStr.AppendInt(port);
|
|
|
|
aPort.Append(portStr);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
2005-07-19 21:12:18 +00:00
|
|
|
|
|
|
|
// Don't propagate this exception to caller
|
|
|
|
result = NS_OK;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::SetPort(const nsAString& aPort)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-04 06:42:28 +00:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (uri) {
|
2002-03-06 07:48:55 +00:00
|
|
|
// perhaps use nsReadingIterators at some point?
|
2006-02-03 14:18:39 +00:00
|
|
|
NS_ConvertUTF16toUTF8 portStr(aPort);
|
2002-03-06 07:48:55 +00:00
|
|
|
const char *buf = portStr.get();
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t port = -1;
|
2002-03-06 07:48:55 +00:00
|
|
|
|
2013-12-05 21:05:51 +00:00
|
|
|
if (!portStr.IsEmpty() && buf) {
|
2002-03-06 07:48:55 +00:00
|
|
|
if (*buf == ':') {
|
|
|
|
port = atol(buf+1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
port = atol(buf);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
2002-03-23 00:48:40 +00:00
|
|
|
|
2007-05-04 06:42:28 +00:00
|
|
|
rv = uri->SetPort(port);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
SetURI(uri);
|
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2007-05-04 06:42:28 +00:00
|
|
|
return rv;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::GetProtocol(nsAString& aProtocol)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
aProtocol.SetLength(0);
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
result = GetURI(getter_AddRefs(uri));
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (uri) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString protocol;
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
result = uri->GetScheme(protocol);
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
2003-12-23 15:41:10 +00:00
|
|
|
CopyASCIItoUTF16(protocol, aProtocol);
|
2014-01-04 15:02:17 +00:00
|
|
|
aProtocol.Append(char16_t(':'));
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::SetProtocol(const nsAString& aProtocol)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-04 06:42:28 +00:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
if (uri) {
|
2007-05-04 06:42:28 +00:00
|
|
|
rv = uri->SetScheme(NS_ConvertUTF16toUTF8(aProtocol));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
SetURI(uri);
|
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2007-05-04 06:42:28 +00:00
|
|
|
return rv;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::GetSearch(nsAString& aSearch)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
aSearch.SetLength(0);
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
result = GetURI(getter_AddRefs(uri));
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
|
|
|
|
if (url) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString search;
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
result = url->GetQuery(search);
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
if (NS_SUCCEEDED(result) && !search.IsEmpty()) {
|
2014-01-04 15:02:17 +00:00
|
|
|
aSearch.Assign(char16_t('?'));
|
2003-12-23 15:41:10 +00:00
|
|
|
AppendUTF8toUTF16(search, aSearch);
|
2002-03-06 07:48:55 +00:00
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2000-07-15 00:14:02 +00:00
|
|
|
return NS_OK;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::SetSearch(const nsAString& aSearch)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2007-05-04 06:42:28 +00:00
|
|
|
nsresult rv = GetWritableURI(getter_AddRefs(uri));
|
2001-01-09 01:16:36 +00:00
|
|
|
|
2002-03-23 00:48:40 +00:00
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (url) {
|
2007-05-04 06:42:28 +00:00
|
|
|
rv = url->SetQuery(NS_ConvertUTF16toUTF8(aSearch));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
SetURI(uri);
|
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2007-05-04 06:42:28 +00:00
|
|
|
return rv;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsLocation::Reload(bool aForceget)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
2001-10-09 05:02:25 +00:00
|
|
|
nsresult rv;
|
2006-06-20 18:56:03 +00:00
|
|
|
nsCOMPtr<nsIDocShell> docShell(do_QueryReferent(mDocShell));
|
|
|
|
nsCOMPtr<nsIWebNavigation> webNav(do_QueryInterface(docShell));
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window(do_GetInterface(docShell));
|
2004-10-20 02:19:57 +00: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 04:22:37 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
|
2004-10-20 02:19:57 +00:00
|
|
|
|
|
|
|
nsIPresShell *shell;
|
|
|
|
nsPresContext *pcx;
|
2010-06-25 13:59:57 +00:00
|
|
|
if (doc && (shell = doc->GetShell()) && (pcx = shell->GetPresContext())) {
|
2008-02-08 19:52:46 +00:00
|
|
|
pcx->RebuildAllStyleData(NS_STYLE_HINT_REFLOW);
|
2004-10-20 02:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-05 21:54:53 +00:00
|
|
|
if (webNav) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t reloadFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
|
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 17:42:36 +00:00
|
|
|
|
2008-09-05 21:54:53 +00:00
|
|
|
if (aForceget) {
|
|
|
|
reloadFlags = nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE |
|
|
|
|
nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
|
|
|
|
}
|
|
|
|
rv = webNav->Reload(reloadFlags);
|
|
|
|
if (rv == NS_BINDING_ABORTED) {
|
|
|
|
// This happens when we attempt to reload a POST result and the user says
|
|
|
|
// no at the "do you want to reload?" prompt. Don't propagate this one
|
|
|
|
// back to callers.
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
2008-09-05 21:54:53 +00:00
|
|
|
return rv;
|
1999-08-03 23:16:48 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::Replace(const nsAString& aUrl)
|
2000-09-08 07:29:54 +00:00
|
|
|
{
|
2001-05-15 20:58:31 +00:00
|
|
|
nsresult rv = NS_OK;
|
2012-10-19 21:26:17 +00:00
|
|
|
if (JSContext *cx = nsContentUtils::GetCurrentJSContext()) {
|
|
|
|
return SetHrefWithContext(cx, aUrl, true);
|
2000-09-08 07:29:54 +00:00
|
|
|
}
|
|
|
|
|
2001-05-15 20:58:31 +00:00
|
|
|
nsAutoString oldHref;
|
|
|
|
|
|
|
|
rv = GetHref(oldHref);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> oldUri;
|
|
|
|
|
|
|
|
rv = NS_NewURI(getter_AddRefs(oldUri), oldHref);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return SetHrefWithBase(aUrl, oldUri, true);
|
2000-09-08 07:29:54 +00:00
|
|
|
}
|
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::Assign(const nsAString& aUrl)
|
1999-08-03 23:16:48 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
if (!CallerSubsumes())
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
|
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 17:42:36 +00:00
|
|
|
nsAutoString oldHref;
|
1999-08-03 23:16:48 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
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 17:42:36 +00:00
|
|
|
result = GetHref(oldHref);
|
1999-08-03 23:16:48 +00: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 17:42:36 +00:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
nsCOMPtr<nsIURI> oldUri;
|
2001-01-09 01:16:36 +00: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 17:42:36 +00:00
|
|
|
result = NS_NewURI(getter_AddRefs(oldUri), oldHref);
|
2001-01-09 01:16:36 +00: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 17:42:36 +00:00
|
|
|
if (oldUri) {
|
2011-10-17 14:59:28 +00:00
|
|
|
result = SetHrefWithBase(aUrl, oldUri, false);
|
1999-08-03 23:16:48 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-09 01:16:36 +00:00
|
|
|
|
1999-08-03 23:16:48 +00:00
|
|
|
return result;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
1999-01-06 17:22:34 +00:00
|
|
|
|
2001-01-09 01:16:36 +00:00
|
|
|
NS_IMETHODIMP
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::ToString(nsAString& aReturn)
|
1999-01-06 17:22:34 +00:00
|
|
|
{
|
2012-10-18 23:37:14 +00:00
|
|
|
// NB: GetHref checks CallerSubsumes().
|
1999-01-06 17:22:34 +00:00
|
|
|
return GetHref(aReturn);
|
|
|
|
}
|
|
|
|
|
2012-11-07 16:27:23 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsLocation::ValueOf(nsIDOMLocation** aReturn)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMLocation> loc(this);
|
|
|
|
loc.forget(aReturn);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-11-17 17:26:18 +00:00
|
|
|
nsresult
|
2004-12-10 19:48:22 +00:00
|
|
|
nsLocation::GetSourceBaseURL(JSContext* cx, nsIURI** sourceURL)
|
2001-11-17 17:26:18 +00:00
|
|
|
{
|
2003-07-03 02:45:34 +00:00
|
|
|
|
2012-10-19 21:26:17 +00:00
|
|
|
*sourceURL = nullptr;
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = nsJSUtils::GetDynamicScriptGlobal(cx);
|
|
|
|
// If this JS context doesn't have an associated DOM window, we effectively
|
|
|
|
// have no script entry point stack. This doesn't generally happen with the DOM,
|
|
|
|
// but can sometimes happen with extension code in certain IPC configurations.
|
|
|
|
// If this happens, try falling back on the current document associated with
|
|
|
|
// the docshell. If that fails, just return null and hope that the caller passed
|
|
|
|
// an absolute URI.
|
|
|
|
if (!sgo && GetDocShell()) {
|
|
|
|
sgo = do_GetInterface(GetDocShell());
|
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(sgo, NS_OK);
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(sgo);
|
|
|
|
NS_ENSURE_TRUE(window, NS_ERROR_UNEXPECTED);
|
|
|
|
nsIDocument* doc = window->GetDoc();
|
|
|
|
NS_ENSURE_TRUE(doc, NS_OK);
|
|
|
|
*sourceURL = doc->GetBaseURI().get();
|
|
|
|
return NS_OK;
|
2001-11-17 17:26:18 +00:00
|
|
|
}
|
2012-10-18 23:37:14 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
nsLocation::CallerSubsumes()
|
|
|
|
{
|
|
|
|
// Get the principal associated with the location object.
|
|
|
|
nsCOMPtr<nsIDOMWindow> outer = do_QueryReferent(mOuter);
|
2012-10-26 13:32:10 +00:00
|
|
|
if (MOZ_UNLIKELY(!outer))
|
2012-10-18 23:37:14 +00:00
|
|
|
return false;
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(outer);
|
|
|
|
bool subsumes = false;
|
|
|
|
nsresult rv = nsContentUtils::GetSubjectPrincipal()->Subsumes(sop->GetPrincipal(), &subsumes);
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
return subsumes;
|
|
|
|
}
|