1998-08-13 04:34:53 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1998-08-13 04:34:53 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1998-08-13 04:34:53 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1998-08-13 04:34:53 +00:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 03:40:37 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1998-08-13 04:34:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsIWebShell.h"
|
|
|
|
#include "nsIURL.h"
|
1999-06-18 17:34:08 +00:00
|
|
|
#include "nsIIOService.h"
|
1999-06-23 03:29:44 +00:00
|
|
|
#include "nsIURL.h"
|
1999-06-18 17:34:08 +00:00
|
|
|
#include "nsIServiceManager.h"
|
1999-11-30 04:50:42 +00:00
|
|
|
#include "nsNetUtil.h"
|
1999-06-18 17:34:08 +00:00
|
|
|
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
1998-08-13 04:34:53 +00:00
|
|
|
#include "plstr.h"
|
|
|
|
#include "prmem.h"
|
1999-08-03 23:16:48 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsJSUtils.h"
|
1999-08-20 09:51:02 +00:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
1999-09-07 02:54:19 +00:00
|
|
|
#include "nsIJSContextStack.h"
|
1998-08-13 04:34:53 +00:00
|
|
|
|
|
|
|
static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);
|
|
|
|
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
|
|
|
static NS_DEFINE_IID(kIDOMLocationIID, NS_IDOMLOCATION_IID);
|
1999-08-05 22:18:28 +00:00
|
|
|
static NS_DEFINE_IID(kIDOMNSLocationIID, NS_IDOMNSLOCATION_IID);
|
1999-08-03 23:16:48 +00:00
|
|
|
static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID);
|
1998-08-13 04:34:53 +00:00
|
|
|
|
|
|
|
LocationImpl::LocationImpl(nsIWebShell *aWebShell)
|
|
|
|
{
|
1999-02-04 19:06:11 +00:00
|
|
|
NS_INIT_REFCNT();
|
1998-08-13 04:34:53 +00:00
|
|
|
mScriptObject = nsnull;
|
|
|
|
mWebShell = aWebShell;
|
|
|
|
}
|
|
|
|
|
|
|
|
LocationImpl::~LocationImpl()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(LocationImpl)
|
|
|
|
NS_IMPL_RELEASE(LocationImpl)
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
LocationImpl::QueryInterface(const nsIID& aIID,
|
|
|
|
void** aInstancePtrResult)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(nsnull != aInstancePtrResult, "null pointer");
|
|
|
|
if (nsnull == aInstancePtrResult) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(kIScriptObjectOwnerIID)) {
|
|
|
|
*aInstancePtrResult = (void*) ((nsIScriptObjectOwner*)this);
|
|
|
|
AddRef();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(kIDOMLocationIID)) {
|
|
|
|
*aInstancePtrResult = (void*) ((nsIDOMLocation*)this);
|
|
|
|
AddRef();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-08-05 22:18:28 +00:00
|
|
|
if (aIID.Equals(kIDOMNSLocationIID)) {
|
|
|
|
*aInstancePtrResult = (void*) ((nsIDOMNSLocation*)this);
|
|
|
|
AddRef();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-08-03 23:16:48 +00:00
|
|
|
if (aIID.Equals(kIJSScriptObjectIID)) {
|
|
|
|
*aInstancePtrResult = (void*)(nsISupports*)(nsIJSScriptObject*)this;
|
|
|
|
AddRef();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
if (aIID.Equals(kISupportsIID)) {
|
|
|
|
*aInstancePtrResult = (void*)(nsISupports*)(nsIScriptObjectOwner*)this;
|
|
|
|
AddRef();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1998-09-17 01:53:52 +00:00
|
|
|
LocationImpl::SetScriptObject(void *aScriptObject)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
1998-09-17 01:53:52 +00:00
|
|
|
mScriptObject = aScriptObject;
|
1998-08-13 04:34:53 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
LocationImpl::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject)
|
|
|
|
{
|
1999-12-03 19:02:31 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aScriptObject);
|
|
|
|
|
|
|
|
if (!mScriptObject) {
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> global(do_GetInterface(mWebShell));
|
|
|
|
NS_ENSURE_TRUE(global, NS_ERROR_FAILURE);
|
|
|
|
return NS_NewScriptLocation(aContext, NS_STATIC_CAST(nsIDOMLocation*, this),
|
|
|
|
global, &mScriptObject);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
*aScriptObject = mScriptObject;
|
1999-12-03 19:02:31 +00:00
|
|
|
return NS_OK;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
LocationImpl::SetWebShell(nsIWebShell *aWebShell)
|
|
|
|
{
|
|
|
|
//mWebShell isn't refcnt'd here. GlobalWindow calls SetWebShell(nsnull)
|
|
|
|
// when it's told that the WebShell is going to be deleted.
|
|
|
|
mWebShell = aWebShell;
|
|
|
|
}
|
|
|
|
|
1999-09-07 02:54:19 +00:00
|
|
|
nsresult
|
|
|
|
LocationImpl::CheckURL(nsIURI* aURL)
|
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
// Get JSContext from stack.
|
|
|
|
NS_WITH_SERVICE(nsIJSContextStack, stack, "nsThreadJSContextStack",
|
|
|
|
&result);
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
JSContext *cx;
|
|
|
|
if (NS_FAILED(stack->Peek(&cx)))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Get security manager.
|
|
|
|
nsIScriptContext *scriptCX = (nsIScriptContext *)JS_GetContextPrivate(cx);
|
|
|
|
nsCOMPtr<nsIScriptSecurityManager> secMan;
|
1999-11-16 05:07:31 +00:00
|
|
|
if (!scriptCX || NS_FAILED(scriptCX->GetSecurityManager(getter_AddRefs(secMan))))
|
1999-09-07 02:54:19 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Check to see if URI is allowed.
|
1999-11-16 05:07:31 +00:00
|
|
|
if (NS_FAILED(result = secMan->CheckLoadURIFromScript(scriptCX, aURL)))
|
|
|
|
return result;
|
1999-09-07 02:54:19 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-16 05:40:20 +00:00
|
|
|
nsresult
|
1999-06-23 03:29:44 +00:00
|
|
|
LocationImpl::SetURL(nsIURI* aURL)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
1998-11-23 01:09:27 +00:00
|
|
|
if (nsnull != mWebShell) {
|
1999-06-23 03:29:44 +00:00
|
|
|
char* spec;
|
|
|
|
aURL->GetSpec(&spec);
|
|
|
|
nsAutoString s = spec;
|
|
|
|
nsCRT::free(spec);
|
1999-09-07 02:54:19 +00:00
|
|
|
|
|
|
|
if (NS_FAILED(CheckURL(aURL)))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
1999-04-05 09:07:07 +00:00
|
|
|
return mWebShell->LoadURL(s.GetUnicode(), nsnull, PR_TRUE);
|
1998-11-23 01:09:27 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::GetHash(nsString& aHash)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 06:12:02 +00:00
|
|
|
nsIURI *uri;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&uri, href);
|
1999-06-18 17:34:08 +00:00
|
|
|
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
1999-06-23 03:29:44 +00:00
|
|
|
char *ref;
|
1999-06-23 06:12:02 +00:00
|
|
|
nsIURL* url;
|
|
|
|
result = uri->QueryInterface(nsIURL::GetIID(), (void**)&url);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = url->GetRef(&ref);
|
|
|
|
NS_RELEASE(url);
|
|
|
|
}
|
1998-12-16 05:40:20 +00:00
|
|
|
if (result == NS_OK && (nsnull != ref) && ('\0' != *ref)) {
|
1998-08-13 04:34:53 +00:00
|
|
|
aHash.SetString("#");
|
|
|
|
aHash.Append(ref);
|
1999-06-23 03:29:44 +00:00
|
|
|
nsCRT::free(ref);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
aHash.SetLength(0);
|
|
|
|
}
|
1999-06-23 06:12:02 +00:00
|
|
|
NS_RELEASE(uri);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::SetHash(const nsString& aHash)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 06:12:02 +00:00
|
|
|
nsIURI *uri;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&uri, href);
|
1999-06-18 17:34:08 +00:00
|
|
|
if (NS_FAILED(result)) return result;
|
1999-06-23 06:12:02 +00:00
|
|
|
nsIURL* url;
|
1999-06-23 03:29:44 +00:00
|
|
|
result = uri->QueryInterface(nsIURI::GetIID(), (void**)&url);
|
1999-06-18 17:34:08 +00:00
|
|
|
NS_RELEASE(uri);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
|
|
|
char *buf = aHash.ToNewCString();
|
1998-12-16 05:40:20 +00:00
|
|
|
url->SetRef(buf);
|
|
|
|
SetURL(url);
|
1999-09-06 05:21:29 +00:00
|
|
|
nsCRT::free(buf);
|
1999-06-23 06:12:02 +00:00
|
|
|
NS_RELEASE(url);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::GetHost(nsString& aHost)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 03:29:44 +00:00
|
|
|
nsIURI *url;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&url, href);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
1999-06-23 03:29:44 +00:00
|
|
|
char* host;
|
1998-12-16 05:40:20 +00:00
|
|
|
result = url->GetHost(&host);
|
|
|
|
if (result == NS_OK) {
|
|
|
|
aHost.SetString(host);
|
1999-06-23 03:29:44 +00:00
|
|
|
nsCRT::free(host);
|
|
|
|
PRInt32 port;
|
|
|
|
(void)url->GetPort(&port);
|
1998-12-16 05:40:20 +00:00
|
|
|
if (-1 != port) {
|
|
|
|
aHost.Append(":");
|
|
|
|
aHost.Append(port, 10);
|
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
1999-06-23 06:12:02 +00:00
|
|
|
NS_RELEASE(url);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::SetHost(const nsString& aHost)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 03:29:44 +00:00
|
|
|
nsIURI *url;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&url, href);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
|
|
|
char *buf = aHost.ToNewCString();
|
1998-12-16 05:40:20 +00:00
|
|
|
url->SetHost(buf);
|
|
|
|
SetURL(url);
|
1999-09-06 05:21:29 +00:00
|
|
|
nsCRT::free(buf);
|
1999-06-23 06:12:02 +00:00
|
|
|
NS_RELEASE(url);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::GetHostname(nsString& aHostname)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 03:29:44 +00:00
|
|
|
nsIURI *url;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&url, href);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
1999-06-23 03:29:44 +00:00
|
|
|
char* host;
|
1998-12-16 05:40:20 +00:00
|
|
|
result = url->GetHost(&host);
|
1999-06-23 03:29:44 +00:00
|
|
|
if (result == NS_OK) {
|
1998-12-16 05:40:20 +00:00
|
|
|
aHostname.SetString(host);
|
1999-06-23 03:29:44 +00:00
|
|
|
nsCRT::free(host);
|
|
|
|
}
|
1999-06-23 06:12:02 +00:00
|
|
|
NS_RELEASE(url);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::SetHostname(const nsString& aHostname)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 03:29:44 +00:00
|
|
|
nsIURI *url;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&url, href);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
|
|
|
char *buf = aHostname.ToNewCString();
|
1998-12-16 05:40:20 +00:00
|
|
|
url->SetHost(buf);
|
|
|
|
SetURL(url);
|
1999-09-06 05:21:29 +00:00
|
|
|
nsCRT::free(buf);
|
1999-06-23 06:12:02 +00:00
|
|
|
NS_RELEASE(url);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::GetHref(nsString& aHref)
|
|
|
|
{
|
1999-08-20 09:51:02 +00:00
|
|
|
// PRInt32 index;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
if (nsnull != mWebShell) {
|
1999-02-14 06:37:24 +00:00
|
|
|
const PRUnichar *href;
|
1999-08-15 00:20:58 +00:00
|
|
|
/* no need to use session history to get the url for the
|
|
|
|
* current document. Fix until webshell's generic session history
|
|
|
|
* is restored. S'd work even otherwise
|
|
|
|
*/
|
|
|
|
//mWebShell->GetHistoryIndex(index);
|
|
|
|
result = mWebShell->GetURL (&href);
|
1998-08-14 23:08:54 +00:00
|
|
|
aHref = href;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::SetHref(const nsString& aHref)
|
|
|
|
{
|
1999-08-03 23:16:48 +00:00
|
|
|
nsAutoString oldHref;
|
|
|
|
nsIURI *oldUrl;
|
1998-11-23 01:09:27 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(oldHref);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&oldUrl, oldHref);
|
1998-11-23 01:09:27 +00:00
|
|
|
if (NS_OK == result) {
|
1999-08-03 23:16:48 +00:00
|
|
|
result = SetHrefWithBase(aHref, oldUrl, PR_TRUE);
|
|
|
|
NS_RELEASE(oldUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
LocationImpl::SetHrefWithBase(const nsString& aHref,
|
|
|
|
nsIURI* aBase,
|
|
|
|
PRBool aReplace)
|
|
|
|
{
|
|
|
|
nsresult result;
|
1999-09-07 02:54:19 +00:00
|
|
|
nsCOMPtr<nsIURI> newUrl;
|
1999-08-03 23:16:48 +00:00
|
|
|
nsAutoString newHref;
|
|
|
|
|
1999-09-07 02:54:19 +00:00
|
|
|
result = NS_NewURI(getter_AddRefs(newUrl), aHref, aBase);
|
1999-08-03 23:16:48 +00:00
|
|
|
if (NS_OK == result) {
|
|
|
|
char* spec;
|
|
|
|
result = newUrl->GetSpec(&spec);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
newHref.SetString(spec);
|
|
|
|
nsCRT::free(spec);
|
1998-11-23 01:09:27 +00:00
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
1998-11-23 01:09:27 +00:00
|
|
|
if ((NS_OK == result) && (nsnull != mWebShell)) {
|
1999-09-01 00:54:35 +00:00
|
|
|
|
1999-09-07 02:54:19 +00:00
|
|
|
if (NS_FAILED(CheckURL(newUrl)))
|
1999-09-01 00:54:35 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-08-20 09:51:02 +00:00
|
|
|
|
1999-09-07 02:54:19 +00:00
|
|
|
// Load new URI.
|
1999-08-03 23:16:48 +00:00
|
|
|
result = mWebShell->LoadURL(newHref.GetUnicode(), nsnull, aReplace);
|
1998-11-23 01:09:27 +00:00
|
|
|
}
|
1999-08-03 23:16:48 +00:00
|
|
|
|
1998-11-23 01:09:27 +00:00
|
|
|
return result;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::GetPathname(nsString& aPathname)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 03:29:44 +00:00
|
|
|
nsIURI *url;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&url, href);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
1999-06-23 03:29:44 +00:00
|
|
|
char* file;
|
|
|
|
result = url->GetPath(&file);
|
|
|
|
if (result == NS_OK) {
|
1998-12-16 05:40:20 +00:00
|
|
|
aPathname.SetString(file);
|
1999-06-23 03:29:44 +00:00
|
|
|
nsCRT::free(file);
|
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
NS_IF_RELEASE(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::SetPathname(const nsString& aPathname)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 03:29:44 +00:00
|
|
|
nsIURI *url;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&url, href);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
|
|
|
char *buf = aPathname.ToNewCString();
|
1999-06-23 03:29:44 +00:00
|
|
|
url->SetPath(buf);
|
1998-12-16 05:40:20 +00:00
|
|
|
SetURL(url);
|
1999-09-06 05:21:29 +00:00
|
|
|
nsCRT::free(buf);
|
1999-07-29 21:16:27 +00:00
|
|
|
NS_RELEASE(url);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::GetPort(nsString& aPort)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 03:29:44 +00:00
|
|
|
nsIURI *url;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&url, href);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
|
|
|
aPort.SetLength(0);
|
1999-06-23 03:29:44 +00:00
|
|
|
PRInt32 port;
|
|
|
|
(void)url->GetPort(&port);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (-1 != port) {
|
|
|
|
aPort.Append(port, 10);
|
|
|
|
}
|
1999-06-23 06:12:02 +00:00
|
|
|
NS_RELEASE(url);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::SetPort(const nsString& aPort)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 03:29:44 +00:00
|
|
|
nsIURI *url;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&url, href);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
|
|
|
char *buf = aPort.ToNewCString();
|
|
|
|
PRInt32 port = -1;
|
|
|
|
|
|
|
|
if (buf) {
|
|
|
|
if (*buf == ':') {
|
|
|
|
port = atol(buf+1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
port = atol(buf);
|
|
|
|
}
|
|
|
|
}
|
1999-06-23 03:29:44 +00:00
|
|
|
url->SetPort(port);
|
1998-12-16 05:40:20 +00:00
|
|
|
SetURL(url);
|
1999-09-06 05:21:29 +00:00
|
|
|
nsCRT::free(buf);
|
1999-06-23 06:12:02 +00:00
|
|
|
NS_RELEASE(url);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::GetProtocol(nsString& aProtocol)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 03:29:44 +00:00
|
|
|
nsIURI *url;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&url, href);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
1999-06-23 03:29:44 +00:00
|
|
|
char* protocol;
|
|
|
|
result = url->GetScheme(&protocol);
|
1998-12-16 05:40:20 +00:00
|
|
|
if (result == NS_OK) {
|
|
|
|
aProtocol.SetString(protocol);
|
|
|
|
aProtocol.Append(":");
|
1999-06-23 03:29:44 +00:00
|
|
|
nsCRT::free(protocol);
|
1998-12-16 05:40:20 +00:00
|
|
|
}
|
1999-06-23 06:12:02 +00:00
|
|
|
NS_RELEASE(url);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::SetProtocol(const nsString& aProtocol)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 03:29:44 +00:00
|
|
|
nsIURI *url;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&url, href);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
|
|
|
char *buf = aProtocol.ToNewCString();
|
1999-06-23 03:29:44 +00:00
|
|
|
url->SetScheme(buf);
|
1998-12-16 05:40:20 +00:00
|
|
|
SetURL(url);
|
1999-09-06 05:21:29 +00:00
|
|
|
nsCRT::free(buf);
|
1999-07-29 21:16:27 +00:00
|
|
|
NS_RELEASE(url);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::GetSearch(nsString& aSearch)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 06:12:02 +00:00
|
|
|
nsIURI *uri;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&uri, href);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
1999-06-23 03:29:44 +00:00
|
|
|
char *search;
|
1999-06-23 06:12:02 +00:00
|
|
|
nsIURL* url;
|
|
|
|
result = uri->QueryInterface(nsIURL::GetIID(), (void**)&url);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = url->GetQuery(&search);
|
|
|
|
NS_RELEASE(url);
|
|
|
|
}
|
1998-12-16 05:40:20 +00:00
|
|
|
if (result == NS_OK && (nsnull != search) && ('\0' != *search)) {
|
1998-08-13 04:34:53 +00:00
|
|
|
aSearch.SetString("?");
|
|
|
|
aSearch.Append(search);
|
1999-06-23 03:29:44 +00:00
|
|
|
nsCRT::free(search);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
aSearch.SetLength(0);
|
|
|
|
}
|
1999-06-23 06:12:02 +00:00
|
|
|
NS_RELEASE(uri);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::SetSearch(const nsString& aSearch)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
1999-06-23 06:12:02 +00:00
|
|
|
nsIURI *uri;
|
1998-08-13 04:34:53 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(href);
|
|
|
|
if (NS_OK == result) {
|
1999-06-23 06:12:02 +00:00
|
|
|
result = NS_NewURI(&uri, href);
|
1998-08-13 04:34:53 +00:00
|
|
|
if (NS_OK == result) {
|
|
|
|
char *buf = aSearch.ToNewCString();
|
1999-06-23 06:12:02 +00:00
|
|
|
nsIURL* url;
|
|
|
|
result = uri->QueryInterface(nsIURL::GetIID(), (void**)&url);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = url->SetQuery(buf);
|
|
|
|
NS_RELEASE(url);
|
|
|
|
}
|
|
|
|
SetURL(uri);
|
1999-09-06 05:21:29 +00:00
|
|
|
nsCRT::free(buf);
|
1999-06-23 06:12:02 +00:00
|
|
|
NS_RELEASE(uri);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-08-03 23:16:48 +00:00
|
|
|
LocationImpl::Reload(PRBool aForceget)
|
1998-08-13 04:34:53 +00:00
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
if (nsnull != mWebShell) {
|
1999-06-28 12:31:56 +00:00
|
|
|
result = mWebShell->Reload(nsIChannel::LOAD_NORMAL);
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::Replace(const nsString& aUrl)
|
|
|
|
{
|
1999-08-03 23:16:48 +00:00
|
|
|
nsAutoString oldHref;
|
|
|
|
nsIURI *oldUrl;
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
result = GetHref(oldHref);
|
|
|
|
if (NS_OK == result) {
|
|
|
|
result = NS_NewURI(&oldUrl, oldHref);
|
|
|
|
if (NS_OK == result) {
|
|
|
|
result = SetHrefWithBase(aUrl, oldUrl, PR_FALSE);
|
|
|
|
NS_RELEASE(oldUrl);
|
|
|
|
}
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
1999-08-03 23:16:48 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::Reload(JSContext *cx, jsval *argv, PRUint32 argc)
|
|
|
|
{
|
|
|
|
// XXX Security manager needs to be called
|
|
|
|
JSBool force = JS_FALSE;
|
|
|
|
|
|
|
|
if (argc > 0) {
|
|
|
|
JS_ValueToBoolean(cx, argv[0], &force);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Reload(force ? PR_TRUE : PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::Replace(JSContext *cx, jsval *argv, PRUint32 argc)
|
|
|
|
{
|
|
|
|
// XXX Security manager needs to be called
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
if (argc > 0) {
|
|
|
|
nsIURI* base;
|
|
|
|
nsAutoString href;
|
|
|
|
|
|
|
|
// Get the parameter passed in
|
|
|
|
nsJSUtils::nsConvertJSValToString(href, cx, argv[0]);
|
|
|
|
|
|
|
|
// Get the source of the caller
|
|
|
|
result = GetSourceURL(cx, &base);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = SetHrefWithBase(href, base, PR_FALSE);
|
|
|
|
NS_RELEASE(base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
1998-08-13 04:34:53 +00:00
|
|
|
}
|
1999-01-06 17:22:34 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocationImpl::ToString(nsString& aReturn)
|
|
|
|
{
|
|
|
|
return GetHref(aReturn);
|
|
|
|
}
|
|
|
|
|
1999-08-03 23:16:48 +00:00
|
|
|
nsresult
|
|
|
|
LocationImpl::GetSourceURL(JSContext* cx,
|
|
|
|
nsIURI** sourceURL)
|
|
|
|
{
|
|
|
|
// XXX Code duplicated from nsHTMLDocument
|
|
|
|
// XXX Tom said this reminded him of the "Six Degrees of
|
|
|
|
// Kevin Bacon" game. We try to get from here to there using
|
|
|
|
// whatever connections possible. The problem is that this
|
|
|
|
// could break if any of the connections along the way change.
|
|
|
|
// I wish there were a better way.
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
nsIScriptContext* context = (nsIScriptContext*)JS_GetContextPrivate(cx);
|
|
|
|
|
|
|
|
if (nsnull != context) {
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> global;
|
|
|
|
|
|
|
|
global = dont_AddRef(context->GetGlobalObject());
|
|
|
|
if (global) {
|
|
|
|
nsCOMPtr<nsIWebShell> webShell;
|
|
|
|
|
|
|
|
global->GetWebShell(getter_AddRefs(webShell));
|
|
|
|
if (webShell) {
|
|
|
|
const PRUnichar* url;
|
|
|
|
|
|
|
|
// XXX Ughh - incorrect ownership rules for url?
|
|
|
|
webShell->GetURL(&url);
|
|
|
|
result = NS_NewURI(sourceURL, url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
LocationImpl::AddProperty(JSContext *aContext, jsval aID, jsval *aVp)
|
|
|
|
{
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
LocationImpl::DeleteProperty(JSContext *aContext, jsval aID, jsval *aVp)
|
|
|
|
{
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
LocationImpl::GetProperty(JSContext *aContext, jsval aID, jsval *aVp)
|
|
|
|
{
|
|
|
|
PRBool result = PR_TRUE;
|
|
|
|
|
|
|
|
// XXX Security manager needs to be called
|
|
|
|
if (JSVAL_IS_STRING(aID)) {
|
|
|
|
char* cString = JS_GetStringBytes(JS_ValueToString(aContext, aID));
|
|
|
|
if (PL_strcmp("href", cString) == 0) {
|
|
|
|
nsAutoString href;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(GetHref(href))) {
|
|
|
|
const PRUnichar* bytes = href.GetUnicode();
|
|
|
|
JSString* str = JS_NewUCStringCopyZ(aContext, (const jschar*)bytes);
|
|
|
|
if (str) {
|
|
|
|
*aVp = STRING_TO_JSVAL(str);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
LocationImpl::SetProperty(JSContext *aContext, jsval aID, jsval *aVp)
|
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
|
|
|
|
// XXX Security manager needs to be called
|
|
|
|
if (JSVAL_IS_STRING(aID)) {
|
|
|
|
char* cString = JS_GetStringBytes(JS_ValueToString(aContext, aID));
|
|
|
|
|
|
|
|
if (PL_strcmp("href", cString) == 0) {
|
|
|
|
nsIURI* base;
|
|
|
|
nsAutoString href;
|
|
|
|
|
|
|
|
// Get the parameter passed in
|
|
|
|
nsJSUtils::nsConvertJSValToString(href, aContext, *aVp);
|
|
|
|
|
|
|
|
// Get the source of the caller
|
|
|
|
result = GetSourceURL(aContext, &base);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = SetHrefWithBase(href, base, PR_TRUE);
|
|
|
|
NS_RELEASE(base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_SUCCEEDED(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
LocationImpl::EnumerateProperty(JSContext *aContext)
|
|
|
|
{
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
LocationImpl::Resolve(JSContext *aContext, jsval aID)
|
|
|
|
{
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
LocationImpl::Convert(JSContext *aContext, jsval aID)
|
|
|
|
{
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LocationImpl::Finalize(JSContext *aContext)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|