This commit is contained in:
vidur%netscape.com 1998-08-13 04:34:53 +00:00
Родитель 4f975179e7
Коммит ee732521f4
16 изменённых файлов: 1452 добавлений и 31 удалений

Просмотреть файл

@ -20,6 +20,8 @@ nsIScriptContext.h
nsIJSScriptObject.h
nsIScriptObjectOwner.h
nsIScriptContextOwner.h
nsIJSScriptObject.h
nsIScriptGlobalObject.h
nsIScriptEventListener.h
nsIDOMWindow.h
nsIDOMLocation.h
nsIDOMNavigator.h

Просмотреть файл

@ -30,6 +30,7 @@ EXPORTS = \
nsIScriptGlobalObject.h \
nsIDOMWindow.h \
nsIDOMNavigator.h \
nsIDOMLocation.h \
$(NULL)
MODULE = dom

Просмотреть файл

Просмотреть файл

@ -38,7 +38,7 @@ IDLSRCS = \
XPCOM_DESTDIR=$(DEPTH)\dom\public\coreDom
JSSTUB_DESTDIR=$(DEPTH)\dom\src\core
JSSTUB_DESTDIR=$(DEPTH)\dom\src\coreDom
GENXDIR=genx
GENJSDIR=genjs

Просмотреть файл

@ -22,7 +22,8 @@ DIRS=coreDom html events
MODULE=raptor
IDLSRCS = \
Navigator.idl
Navigator.idl \
Location.idl
GLOBAL_IDLSRC = Window.idl

Просмотреть файл

@ -22,7 +22,7 @@ DIRS=coreDom coreEvents events html
DEFINES=-D_IMPL_NS_DOM
EXPORTS=nsIScriptContext.h nsIJSScriptObject.h nsIScriptObjectOwner.h \
nsIScriptGlobalObject.h nsIDOMWindow.h nsIScriptContextOwner.h \
nsIDOMNavigator.h nsIScriptEventListener.h
nsIDOMNavigator.h nsIScriptEventListener.h nsIDOMLocation.h
MODULE=dom

113
dom/public/nsIDOMLocation.h Normal file
Просмотреть файл

@ -0,0 +1,113 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
/* AUTO-GENERATED. DO NOT EDIT!!! */
#ifndef nsIDOMLocation_h__
#define nsIDOMLocation_h__
#include "nsISupports.h"
#include "nsString.h"
#include "nsIScriptContext.h"
#include "jsapi.h"
#define NS_IDOMLOCATION_IID \
{ 0x6f76532d, 0xee43, 0x11d1, \
{ 0x9b, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
class nsIDOMLocation : public nsISupports {
public:
NS_IMETHOD GetHash(nsString& aHash)=0;
NS_IMETHOD SetHash(const nsString& aHash)=0;
NS_IMETHOD GetHost(nsString& aHost)=0;
NS_IMETHOD SetHost(const nsString& aHost)=0;
NS_IMETHOD GetHostname(nsString& aHostname)=0;
NS_IMETHOD SetHostname(const nsString& aHostname)=0;
NS_IMETHOD GetHref(nsString& aHref)=0;
NS_IMETHOD SetHref(const nsString& aHref)=0;
NS_IMETHOD GetPathname(nsString& aPathname)=0;
NS_IMETHOD SetPathname(const nsString& aPathname)=0;
NS_IMETHOD GetPort(nsString& aPort)=0;
NS_IMETHOD SetPort(const nsString& aPort)=0;
NS_IMETHOD GetProtocol(nsString& aProtocol)=0;
NS_IMETHOD SetProtocol(const nsString& aProtocol)=0;
NS_IMETHOD GetSearch(nsString& aSearch)=0;
NS_IMETHOD SetSearch(const nsString& aSearch)=0;
NS_IMETHOD Reload(JSContext *cx, jsval *argv, PRUint32 argc)=0;
NS_IMETHOD Replace(const nsString& aUrl)=0;
};
#define NS_DECL_IDOMLOCATION \
NS_IMETHOD GetHash(nsString& aHash); \
NS_IMETHOD SetHash(const nsString& aHash); \
NS_IMETHOD GetHost(nsString& aHost); \
NS_IMETHOD SetHost(const nsString& aHost); \
NS_IMETHOD GetHostname(nsString& aHostname); \
NS_IMETHOD SetHostname(const nsString& aHostname); \
NS_IMETHOD GetHref(nsString& aHref); \
NS_IMETHOD SetHref(const nsString& aHref); \
NS_IMETHOD GetPathname(nsString& aPathname); \
NS_IMETHOD SetPathname(const nsString& aPathname); \
NS_IMETHOD GetPort(nsString& aPort); \
NS_IMETHOD SetPort(const nsString& aPort); \
NS_IMETHOD GetProtocol(nsString& aProtocol); \
NS_IMETHOD SetProtocol(const nsString& aProtocol); \
NS_IMETHOD GetSearch(nsString& aSearch); \
NS_IMETHOD SetSearch(const nsString& aSearch); \
NS_IMETHOD Reload(JSContext *cx, jsval *argv, PRUint32 argc); \
NS_IMETHOD Replace(const nsString& aUrl); \
#define NS_FORWARD_IDOMLOCATION(_to) \
NS_IMETHOD GetHash(nsString& aHash) { return _to##GetHash(aHash); } \
NS_IMETHOD SetHash(const nsString& aHash) { return _to##SetHash(aHash); } \
NS_IMETHOD GetHost(nsString& aHost) { return _to##GetHost(aHost); } \
NS_IMETHOD SetHost(const nsString& aHost) { return _to##SetHost(aHost); } \
NS_IMETHOD GetHostname(nsString& aHostname) { return _to##GetHostname(aHostname); } \
NS_IMETHOD SetHostname(const nsString& aHostname) { return _to##SetHostname(aHostname); } \
NS_IMETHOD GetHref(nsString& aHref) { return _to##GetHref(aHref); } \
NS_IMETHOD SetHref(const nsString& aHref) { return _to##SetHref(aHref); } \
NS_IMETHOD GetPathname(nsString& aPathname) { return _to##GetPathname(aPathname); } \
NS_IMETHOD SetPathname(const nsString& aPathname) { return _to##SetPathname(aPathname); } \
NS_IMETHOD GetPort(nsString& aPort) { return _to##GetPort(aPort); } \
NS_IMETHOD SetPort(const nsString& aPort) { return _to##SetPort(aPort); } \
NS_IMETHOD GetProtocol(nsString& aProtocol) { return _to##GetProtocol(aProtocol); } \
NS_IMETHOD SetProtocol(const nsString& aProtocol) { return _to##SetProtocol(aProtocol); } \
NS_IMETHOD GetSearch(nsString& aSearch) { return _to##GetSearch(aSearch); } \
NS_IMETHOD SetSearch(const nsString& aSearch) { return _to##SetSearch(aSearch); } \
NS_IMETHOD Reload(JSContext *cx, jsval *argv, PRUint32 argc) { return _to##Reload(cx, argv, argc); } \
NS_IMETHOD Replace(const nsString& aUrl) { return _to##Replace(aUrl); } \
extern nsresult NS_InitLocationClass(nsIScriptContext *aContext, void **aPrototype);
extern "C" NS_DOM nsresult NS_NewScriptLocation(nsIScriptContext *aContext, nsIDOMLocation *aSupports, nsISupports *aParent, void **aReturn);
#endif // nsIDOMLocation_h__

Просмотреть файл

@ -26,7 +26,7 @@
#define NS_IDOMNAVIGATOR_IID \
{ 0x6f76532d, 0xee43, 0x11d1, \
{ 0x6f76532e, 0xee43, 0x11d1, \
{ 0x9b, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
class nsIDOMNavigator : public nsISupports {

Просмотреть файл

@ -30,7 +30,7 @@ class nsIDOMDocument;
class nsIDOMWindow;
#define NS_IDOMWINDOW_IID \
{ 0x6f76532e, 0xee43, 0x11d1, \
{ 0x6f76532f, 0xee43, 0x11d1, \
{ 0x9b, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
class nsIDOMWindow : public nsISupports {

Просмотреть файл

@ -29,7 +29,9 @@ CPPSRCS = \
nsJSEnvironment.cpp \
nsJSWindow.cpp \
nsJSNavigator.cpp \
nsJSLocation.cpp \
nsGlobalWindow.cpp \
nsLocation.cpp \
$(NULL)
include $(DEPTH)/config/config.mk

Просмотреть файл

@ -26,7 +26,9 @@ CPPSRCS = \
nsJSEnvironment.cpp \
nsJSWindow.cpp \
nsJSNavigator.cpp \
nsJSLocation.cpp \
nsGlobalWindow.cpp \
nsLocation.cpp \
$(NULL)
MODULE=raptor
@ -38,6 +40,8 @@ CPP_OBJS= \
.\$(OBJDIR)\nsJSWindow.obj \
.\$(OBJDIR)\nsGlobalWindow.obj \
.\$(OBJDIR)\nsJSNavigator.obj \
.\$(OBJDIR)\nsLocation.obj \
.\$(OBJDIR)\nsJSLocation.obj \
$(NULL)
LINCS=-I$(XPDIST)\public\xpcom -I$(XPDIST)\public\raptor \

Просмотреть файл

@ -72,6 +72,7 @@ GlobalWindowImpl::GlobalWindowImpl()
mScriptObject = nsnull;
mDocument = nsnull;
mNavigator = nsnull;
mLocation = nsnull;
mTimeouts = nsnull;
mTimeoutInsertionPoint = nsnull;
@ -96,6 +97,7 @@ GlobalWindowImpl::~GlobalWindowImpl()
}
NS_IF_RELEASE(mNavigator);
NS_IF_RELEASE(mLocation);
NS_IF_RELEASE(mListenerManager);
}
@ -215,6 +217,9 @@ GlobalWindowImpl::SetWebShell(nsIWebShell *aWebShell)
{
//mWebShell isn't refcnt'd here. WebShell calls SetWebShell(nsnull) when deleted.
mWebShell = aWebShell;
if (nsnull != mLocation) {
mLocation->SetWebShell(aWebShell);
}
}
NS_IMETHODIMP
@ -258,6 +263,20 @@ GlobalWindowImpl::GetNavigator(nsIDOMNavigator** aNavigator)
return NS_OK;
}
NS_IMETHODIMP
GlobalWindowImpl::GetLocation(nsIDOMLocation** aLocation)
{
if (nsnull == mLocation) {
mLocation = new LocationImpl(mWebShell);
NS_IF_ADDREF(mLocation);
}
*aLocation = mLocation;
NS_IF_ADDREF(mLocation);
return NS_OK;
}
NS_IMETHODIMP
GlobalWindowImpl::GetOpener(nsIDOMWindow** aOpener)
{
@ -718,7 +737,6 @@ GlobalWindowImpl::Open(JSContext *cx,
PRUint32 mChrome = 0;
nsString mURL, mName;
JSString* str;
char *url_string;
*aReturn = JSVAL_NULL;
if (argc > 0) {
@ -869,7 +887,8 @@ GlobalWindowImpl::Open(JSContext *cx,
return NS_OK;
}
nsresult GlobalWindowImpl::CheckWindowName(JSContext *cx, nsString& aName)
nsresult
GlobalWindowImpl::CheckWindowName(JSContext *cx, nsString& aName)
{
PRInt32 index;
PRUnichar mChar;
@ -886,7 +905,8 @@ nsresult GlobalWindowImpl::CheckWindowName(JSContext *cx, nsString& aName)
return NS_OK;
}
int32 GlobalWindowImpl::WinHasOption(char *options, char *name)
int32
GlobalWindowImpl::WinHasOption(char *options, char *name)
{
char *comma, *equal;
int32 found = 0;
@ -911,7 +931,8 @@ int32 GlobalWindowImpl::WinHasOption(char *options, char *name)
return found;
}
nsresult GlobalWindowImpl::GetBrowserWindowInterface(nsIBrowserWindow*& aBrowser)
nsresult
GlobalWindowImpl::GetBrowserWindowInterface(nsIBrowserWindow*& aBrowser)
{
nsresult ret;
@ -929,26 +950,59 @@ nsresult GlobalWindowImpl::GetBrowserWindowInterface(nsIBrowserWindow*& aBrowser
return ret;
}
PRBool GlobalWindowImpl::AddProperty(JSContext *aContext, jsval aID, jsval *aVp)
PRBool
GlobalWindowImpl::AddProperty(JSContext *aContext, jsval aID, jsval *aVp)
{
return PR_TRUE;
}
PRBool GlobalWindowImpl::DeleteProperty(JSContext *aContext, jsval aID, jsval *aVp)
PRBool
GlobalWindowImpl::DeleteProperty(JSContext *aContext, jsval aID, jsval *aVp)
{
return PR_TRUE;
}
PRBool GlobalWindowImpl::GetProperty(JSContext *aContext, jsval aID, jsval *aVp)
PRBool
GlobalWindowImpl::GetProperty(JSContext *aContext, jsval aID, jsval *aVp)
{
if (JSVAL_IS_STRING(aID) &&
PL_strcmp("location", JS_GetStringBytes(JS_ValueToString(aContext, aID))) == 0) {
nsIDOMLocation *location;
if (NS_OK == GetLocation(&location)) {
if (location != nsnull) {
nsIScriptObjectOwner *owner = nsnull;
if (NS_OK == location->QueryInterface(kIScriptObjectOwnerIID,
(void**)&owner)) {
JSObject *object = nsnull;
nsIScriptContext *script_cx = (nsIScriptContext *)JS_GetContextPrivate(aContext);
if (NS_OK == owner->GetScriptObject(script_cx, (void**)&object)) {
// set the return value
*aVp = OBJECT_TO_JSVAL(object);
}
NS_RELEASE(owner);
}
NS_RELEASE(location);
}
else {
*aVp = JSVAL_NULL;
}
}
else {
return PR_FALSE;
}
}
return PR_TRUE;
}
PRBool GlobalWindowImpl::SetProperty(JSContext *aContext, jsval aID, jsval *aVp)
PRBool
GlobalWindowImpl::SetProperty(JSContext *aContext, jsval aID, jsval *aVp)
{
if (JS_TypeOfValue(aContext, *aVp) == JSTYPE_FUNCTION && JSVAL_IS_STRING(aID)) {
nsAutoString mPropName, mPrefix;
mPropName.SetString(JS_GetStringChars(JS_ValueToString(aContext, aID)));
mPropName.SetString(JS_GetStringBytes(JS_ValueToString(aContext, aID)));
mPrefix.SetString(mPropName, 2);
if (mPrefix == "on") {
nsIEventListenerManager *mManager = nsnull;
@ -1012,29 +1066,56 @@ PRBool GlobalWindowImpl::SetProperty(JSContext *aContext, jsval aID, jsval *a
NS_IF_RELEASE(mManager);
}
}
else if (JSVAL_IS_STRING(aID) &&
PL_strcmp("location", JS_GetStringBytes(JS_ValueToString(aContext, aID))) == 0) {
JSString *jsstring = JS_ValueToString(aContext, *aVp);
if (nsnull != jsstring) {
nsIDOMLocation *location;
nsAutoString locationStr;
locationStr.SetString(JS_GetStringChars(jsstring));
if (NS_OK == GetLocation(&location)) {
if (NS_OK != location->SetHref(locationStr)) {
NS_RELEASE(location);
return PR_FALSE;
}
NS_RELEASE(location);
}
else {
return PR_FALSE;
}
}
}
return PR_TRUE;
}
PRBool GlobalWindowImpl::EnumerateProperty(JSContext *aContext)
PRBool
GlobalWindowImpl::EnumerateProperty(JSContext *aContext)
{
return PR_TRUE;
}
PRBool GlobalWindowImpl::Resolve(JSContext *aContext, jsval aID)
PRBool
GlobalWindowImpl::Resolve(JSContext *aContext, jsval aID)
{
return PR_TRUE;
}
PRBool GlobalWindowImpl::Convert(JSContext *aContext, jsval aID)
PRBool
GlobalWindowImpl::Convert(JSContext *aContext, jsval aID)
{
return PR_TRUE;
}
void GlobalWindowImpl::Finalize(JSContext *aContext)
void
GlobalWindowImpl::Finalize(JSContext *aContext)
{
}
nsresult GlobalWindowImpl::GetListenerManager(nsIEventListenerManager **aInstancePtrResult)
nsresult
GlobalWindowImpl::GetListenerManager(nsIEventListenerManager **aInstancePtrResult)
{
if (nsnull != mListenerManager) {
return mListenerManager->QueryInterface(kIEventListenerManagerIID, (void**) aInstancePtrResult);;
@ -1054,16 +1135,18 @@ nsresult GlobalWindowImpl::GetListenerManager(nsIEventListenerManager **aInstanc
}
//XXX I need another way around the circular link problem.
nsresult GlobalWindowImpl::GetNewListenerManager(nsIEventListenerManager **aInstancePtrResult)
nsresult
GlobalWindowImpl::GetNewListenerManager(nsIEventListenerManager **aInstancePtrResult)
{
return NS_ERROR_FAILURE;
}
nsresult GlobalWindowImpl::HandleDOMEvent(nsIPresContext& aPresContext,
nsEvent* aEvent,
nsIDOMEvent** aDOMEvent,
PRUint32 aFlags,
nsEventStatus& aEventStatus)
nsresult
GlobalWindowImpl::HandleDOMEvent(nsIPresContext& aPresContext,
nsEvent* aEvent,
nsIDOMEvent** aDOMEvent,
PRUint32 aFlags,
nsEventStatus& aEventStatus)
{
nsresult mRet = NS_OK;
nsIDOMEvent* mDOMEvent = nsnull;
@ -1107,7 +1190,8 @@ nsresult GlobalWindowImpl::HandleDOMEvent(nsIPresContext& aPresContext,
return mRet;
}
nsresult GlobalWindowImpl::AddEventListener(nsIDOMEventListener *aListener, const nsIID& aIID)
nsresult
GlobalWindowImpl::AddEventListener(nsIDOMEventListener *aListener, const nsIID& aIID)
{
nsIEventListenerManager *mManager;
@ -1119,7 +1203,8 @@ nsresult GlobalWindowImpl::AddEventListener(nsIDOMEventListener *aListener, cons
return NS_ERROR_FAILURE;
}
nsresult GlobalWindowImpl::RemoveEventListener(nsIDOMEventListener *aListener, const nsIID& aIID)
nsresult
GlobalWindowImpl::RemoveEventListener(nsIDOMEventListener *aListener, const nsIID& aIID)
{
if (nsnull != mListenerManager) {
mListenerManager->RemoveEventListener(aListener, aIID);
@ -1128,7 +1213,8 @@ nsresult GlobalWindowImpl::RemoveEventListener(nsIDOMEventListener *aListener, c
return NS_ERROR_FAILURE;
}
nsresult GlobalWindowImpl::CaptureEvent(nsIDOMEventListener *aListener)
nsresult
GlobalWindowImpl::CaptureEvent(nsIDOMEventListener *aListener)
{
nsIEventListenerManager *mManager;
@ -1140,7 +1226,8 @@ nsresult GlobalWindowImpl::CaptureEvent(nsIDOMEventListener *aListener)
return NS_ERROR_FAILURE;
}
nsresult GlobalWindowImpl::ReleaseEvent(nsIDOMEventListener *aListener)
nsresult
GlobalWindowImpl::ReleaseEvent(nsIDOMEventListener *aListener)
{
if (nsnull != mListenerManager) {
mListenerManager->ReleaseEvent(aListener);

Просмотреть файл

@ -26,6 +26,7 @@
#include "nsIScriptGlobalObject.h"
#include "nsIDOMWindow.h"
#include "nsIDOMNavigator.h"
#include "nsIDOMLocation.h"
#include "nsITimer.h"
#include "nsIJSScriptObject.h"
#include "nsIDOMEventCapturer.h"
@ -41,6 +42,9 @@ class nsIBrowserWindow;
typedef struct nsTimeoutImpl nsTimeoutImpl;
class LocationImpl;
class NavigatorImpl;
// Global object for scripting
class GlobalWindowImpl : public nsIScriptObjectOwner, public nsIScriptGlobalObject, public nsIDOMWindow,
public nsIJSScriptObject, public nsIDOMEventCapturer
@ -63,6 +67,7 @@ public:
NS_IMETHOD GetDocument(nsIDOMDocument** aDocument);
NS_IMETHOD GetNavigator(nsIDOMNavigator** aNavigator);
NS_IMETHOD GetOpener(nsIDOMWindow** aOpener);
NS_IMETHOD GetLocation(nsIDOMLocation** aLocation);
NS_IMETHOD Dump(const nsString& aStr);
NS_IMETHOD Alert(const nsString& aStr);
NS_IMETHOD ClearTimeout(PRInt32 aTimerID);
@ -118,7 +123,8 @@ protected:
nsIScriptContext *mContext;
void *mScriptObject;
nsIDOMDocument *mDocument;
nsIDOMNavigator *mNavigator;
NavigatorImpl *mNavigator;
LocationImpl *mLocation;
nsIWebShell *mWebShell;
nsTimeoutImpl *mTimeouts;
@ -183,4 +189,46 @@ protected:
void *mScriptObject;
};
class LocationImpl : public nsIScriptObjectOwner, public nsIDOMLocation {
protected:
public:
LocationImpl(nsIWebShell *aWebShell);
~LocationImpl();
NS_DECL_ISUPPORTS
NS_IMETHOD GetScriptObject(nsIScriptContext *aContext, void** aScriptObject);
NS_IMETHOD ResetScriptObject();
NS_IMETHOD_(void) SetWebShell(nsIWebShell *aWebShell);
NS_IMETHOD GetHash(nsString& aHash);
NS_IMETHOD SetHash(const nsString& aHash);
NS_IMETHOD GetHost(nsString& aHost);
NS_IMETHOD SetHost(const nsString& aHost);
NS_IMETHOD GetHostname(nsString& aHostname);
NS_IMETHOD SetHostname(const nsString& aHostname);
NS_IMETHOD GetHref(nsString& aHref);
NS_IMETHOD SetHref(const nsString& aHref);
NS_IMETHOD GetPathname(nsString& aPathname);
NS_IMETHOD SetPathname(const nsString& aPathname);
NS_IMETHOD GetPort(nsString& aPort);
NS_IMETHOD SetPort(const nsString& aPort);
NS_IMETHOD GetProtocol(nsString& aProtocol);
NS_IMETHOD SetProtocol(const nsString& aProtocol);
NS_IMETHOD GetSearch(nsString& aSearch);
NS_IMETHOD SetSearch(const nsString& aSearch);
NS_IMETHOD Reload(JSContext *cx, jsval *argv, PRUint32 argc);
NS_IMETHOD Replace(const nsString& aUrl);
protected:
void ConcatenateAndSet(const char *aProtocol, const char *aHost,
PRInt32 aPort, const char *aFile,
const char *aRef, const char *aSearch);
nsIWebShell *mWebShell;
void *mScriptObject;
};
#endif /* nsGlobalWindow_h___ */

Просмотреть файл

@ -0,0 +1,639 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
/* AUTO-GENERATED. DO NOT EDIT!!! */
#include "jsapi.h"
#include "nscore.h"
#include "nsIScriptContext.h"
#include "nsIJSScriptObject.h"
#include "nsIScriptObjectOwner.h"
#include "nsIScriptGlobalObject.h"
#include "nsIPtr.h"
#include "nsString.h"
#include "nsIDOMLocation.h"
static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);
static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID);
static NS_DEFINE_IID(kIScriptGlobalObjectIID, NS_ISCRIPTGLOBALOBJECT_IID);
static NS_DEFINE_IID(kILocationIID, NS_IDOMLOCATION_IID);
NS_DEF_PTR(nsIDOMLocation);
//
// Location property ids
//
enum Location_slots {
LOCATION_HASH = -11,
LOCATION_HOST = -12,
LOCATION_HOSTNAME = -13,
LOCATION_HREF = -14,
LOCATION_PATHNAME = -15,
LOCATION_PORT = -16,
LOCATION_PROTOCOL = -17,
LOCATION_SEARCH = -18
};
/***********************************************************************/
//
// Location Properties Getter
//
PR_STATIC_CALLBACK(JSBool)
GetLocationProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
nsIDOMLocation *a = (nsIDOMLocation*)JS_GetPrivate(cx, obj);
// If there's no private data, this must be the prototype, so ignore
if (nsnull == a) {
return JS_TRUE;
}
if (JSVAL_IS_INT(id)) {
switch(JSVAL_TO_INT(id)) {
case LOCATION_HASH:
{
nsAutoString prop;
if (NS_OK == a->GetHash(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
case LOCATION_HOST:
{
nsAutoString prop;
if (NS_OK == a->GetHost(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
case LOCATION_HOSTNAME:
{
nsAutoString prop;
if (NS_OK == a->GetHostname(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
case LOCATION_HREF:
{
nsAutoString prop;
if (NS_OK == a->GetHref(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
case LOCATION_PATHNAME:
{
nsAutoString prop;
if (NS_OK == a->GetPathname(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
case LOCATION_PORT:
{
nsAutoString prop;
if (NS_OK == a->GetPort(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
case LOCATION_PROTOCOL:
{
nsAutoString prop;
if (NS_OK == a->GetProtocol(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
case LOCATION_SEARCH:
{
nsAutoString prop;
if (NS_OK == a->GetSearch(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
default:
{
nsIJSScriptObject *object;
if (NS_OK == a->QueryInterface(kIJSScriptObjectIID, (void**)&object)) {
PRBool rval;
rval = object->GetProperty(cx, id, vp);
NS_RELEASE(object);
return rval;
}
}
}
}
else {
nsIJSScriptObject *object;
if (NS_OK == a->QueryInterface(kIJSScriptObjectIID, (void**)&object)) {
PRBool rval;
rval = object->GetProperty(cx, id, vp);
NS_RELEASE(object);
return rval;
}
}
return PR_TRUE;
}
/***********************************************************************/
//
// Location Properties Setter
//
PR_STATIC_CALLBACK(JSBool)
SetLocationProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
nsIDOMLocation *a = (nsIDOMLocation*)JS_GetPrivate(cx, obj);
// If there's no private data, this must be the prototype, so ignore
if (nsnull == a) {
return JS_TRUE;
}
if (JSVAL_IS_INT(id)) {
switch(JSVAL_TO_INT(id)) {
case LOCATION_HASH:
{
nsAutoString prop;
JSString *jsstring;
if ((jsstring = JS_ValueToString(cx, *vp)) != nsnull) {
prop.SetString(JS_GetStringChars(jsstring));
}
else {
prop.SetString((const char *)nsnull);
}
a->SetHash(prop);
break;
}
case LOCATION_HOST:
{
nsAutoString prop;
JSString *jsstring;
if ((jsstring = JS_ValueToString(cx, *vp)) != nsnull) {
prop.SetString(JS_GetStringChars(jsstring));
}
else {
prop.SetString((const char *)nsnull);
}
a->SetHost(prop);
break;
}
case LOCATION_HOSTNAME:
{
nsAutoString prop;
JSString *jsstring;
if ((jsstring = JS_ValueToString(cx, *vp)) != nsnull) {
prop.SetString(JS_GetStringChars(jsstring));
}
else {
prop.SetString((const char *)nsnull);
}
a->SetHostname(prop);
break;
}
case LOCATION_HREF:
{
nsAutoString prop;
JSString *jsstring;
if ((jsstring = JS_ValueToString(cx, *vp)) != nsnull) {
prop.SetString(JS_GetStringChars(jsstring));
}
else {
prop.SetString((const char *)nsnull);
}
a->SetHref(prop);
break;
}
case LOCATION_PATHNAME:
{
nsAutoString prop;
JSString *jsstring;
if ((jsstring = JS_ValueToString(cx, *vp)) != nsnull) {
prop.SetString(JS_GetStringChars(jsstring));
}
else {
prop.SetString((const char *)nsnull);
}
a->SetPathname(prop);
break;
}
case LOCATION_PORT:
{
nsAutoString prop;
JSString *jsstring;
if ((jsstring = JS_ValueToString(cx, *vp)) != nsnull) {
prop.SetString(JS_GetStringChars(jsstring));
}
else {
prop.SetString((const char *)nsnull);
}
a->SetPort(prop);
break;
}
case LOCATION_PROTOCOL:
{
nsAutoString prop;
JSString *jsstring;
if ((jsstring = JS_ValueToString(cx, *vp)) != nsnull) {
prop.SetString(JS_GetStringChars(jsstring));
}
else {
prop.SetString((const char *)nsnull);
}
a->SetProtocol(prop);
break;
}
case LOCATION_SEARCH:
{
nsAutoString prop;
JSString *jsstring;
if ((jsstring = JS_ValueToString(cx, *vp)) != nsnull) {
prop.SetString(JS_GetStringChars(jsstring));
}
else {
prop.SetString((const char *)nsnull);
}
a->SetSearch(prop);
break;
}
default:
{
nsIJSScriptObject *object;
if (NS_OK == a->QueryInterface(kIJSScriptObjectIID, (void**)&object)) {
PRBool rval;
rval = object->SetProperty(cx, id, vp);
NS_RELEASE(object);
return rval;
}
}
}
}
else {
nsIJSScriptObject *object;
if (NS_OK == a->QueryInterface(kIJSScriptObjectIID, (void**)&object)) {
PRBool rval;
rval = object->SetProperty(cx, id, vp);
NS_RELEASE(object);
return rval;
}
}
return PR_TRUE;
}
//
// Location finalizer
//
PR_STATIC_CALLBACK(void)
FinalizeLocation(JSContext *cx, JSObject *obj)
{
nsIDOMLocation *a = (nsIDOMLocation*)JS_GetPrivate(cx, obj);
if (nsnull != a) {
// get the js object
nsIScriptObjectOwner *owner = nsnull;
if (NS_OK == a->QueryInterface(kIScriptObjectOwnerIID, (void**)&owner)) {
owner->ResetScriptObject();
NS_RELEASE(owner);
}
NS_RELEASE(a);
}
}
//
// Location enumerate
//
PR_STATIC_CALLBACK(JSBool)
EnumerateLocation(JSContext *cx, JSObject *obj)
{
nsIDOMLocation *a = (nsIDOMLocation*)JS_GetPrivate(cx, obj);
if (nsnull != a) {
// get the js object
nsIJSScriptObject *object;
if (NS_OK == a->QueryInterface(kIJSScriptObjectIID, (void**)&object)) {
object->EnumerateProperty(cx);
NS_RELEASE(object);
}
}
return JS_TRUE;
}
//
// Location resolve
//
PR_STATIC_CALLBACK(JSBool)
ResolveLocation(JSContext *cx, JSObject *obj, jsval id)
{
nsIDOMLocation *a = (nsIDOMLocation*)JS_GetPrivate(cx, obj);
if (nsnull != a) {
// get the js object
nsIJSScriptObject *object;
if (NS_OK == a->QueryInterface(kIJSScriptObjectIID, (void**)&object)) {
object->Resolve(cx, id);
NS_RELEASE(object);
}
}
return JS_TRUE;
}
//
// Native method Reload
//
PR_STATIC_CALLBACK(JSBool)
LocationReload(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
nsIDOMLocation *nativeThis = (nsIDOMLocation*)JS_GetPrivate(cx, obj);
JSBool rBool = JS_FALSE;
*rval = JSVAL_NULL;
// If there's no private data, this must be the prototype, so ignore
if (nsnull == nativeThis) {
return JS_TRUE;
}
if (argc >= 0) {
if (NS_OK != nativeThis->Reload(cx, argv+0, argc-0)) {
return JS_FALSE;
}
*rval = JSVAL_VOID;
}
else {
JS_ReportError(cx, "Function reload requires 0 parameters");
return JS_FALSE;
}
return JS_TRUE;
}
//
// Native method Replace
//
PR_STATIC_CALLBACK(JSBool)
LocationReplace(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
nsIDOMLocation *nativeThis = (nsIDOMLocation*)JS_GetPrivate(cx, obj);
JSBool rBool = JS_FALSE;
nsAutoString b0;
*rval = JSVAL_NULL;
// If there's no private data, this must be the prototype, so ignore
if (nsnull == nativeThis) {
return JS_TRUE;
}
if (argc >= 1) {
JSString *jsstring0 = JS_ValueToString(cx, argv[0]);
if (nsnull != jsstring0) {
b0.SetString(JS_GetStringChars(jsstring0));
}
else {
b0.SetString(""); // Should this really be null??
}
if (NS_OK != nativeThis->Replace(b0)) {
return JS_FALSE;
}
*rval = JSVAL_VOID;
}
else {
JS_ReportError(cx, "Function replace requires 1 parameters");
return JS_FALSE;
}
return JS_TRUE;
}
/***********************************************************************/
//
// class for Location
//
JSClass LocationClass = {
"Location",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub,
JS_PropertyStub,
GetLocationProperty,
SetLocationProperty,
EnumerateLocation,
ResolveLocation,
JS_ConvertStub,
FinalizeLocation
};
//
// Location class properties
//
static JSPropertySpec LocationProperties[] =
{
{"hash", LOCATION_HASH, JSPROP_ENUMERATE},
{"host", LOCATION_HOST, JSPROP_ENUMERATE},
{"hostname", LOCATION_HOSTNAME, JSPROP_ENUMERATE},
{"href", LOCATION_HREF, JSPROP_ENUMERATE},
{"pathname", LOCATION_PATHNAME, JSPROP_ENUMERATE},
{"port", LOCATION_PORT, JSPROP_ENUMERATE},
{"protocol", LOCATION_PROTOCOL, JSPROP_ENUMERATE},
{"search", LOCATION_SEARCH, JSPROP_ENUMERATE},
{0}
};
//
// Location class methods
//
static JSFunctionSpec LocationMethods[] =
{
{"reload", LocationReload, 0},
{"replace", LocationReplace, 1},
{0}
};
//
// Location constructor
//
PR_STATIC_CALLBACK(JSBool)
Location(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
return JS_TRUE;
}
//
// Location class initialization
//
nsresult NS_InitLocationClass(nsIScriptContext *aContext, void **aPrototype)
{
JSContext *jscontext = (JSContext *)aContext->GetNativeContext();
JSObject *proto = nsnull;
JSObject *constructor = nsnull;
JSObject *parent_proto = nsnull;
JSObject *global = JS_GetGlobalObject(jscontext);
jsval vp;
if ((PR_TRUE != JS_LookupProperty(jscontext, global, "Location", &vp)) ||
!JSVAL_IS_OBJECT(vp) ||
((constructor = JSVAL_TO_OBJECT(vp)) == nsnull) ||
(PR_TRUE != JS_LookupProperty(jscontext, JSVAL_TO_OBJECT(vp), "prototype", &vp)) ||
!JSVAL_IS_OBJECT(vp)) {
proto = JS_InitClass(jscontext, // context
global, // global object
parent_proto, // parent proto
&LocationClass, // JSClass
Location, // JSNative ctor
0, // ctor args
LocationProperties, // proto props
LocationMethods, // proto funcs
nsnull, // ctor props (static)
nsnull); // ctor funcs (static)
if (nsnull == proto) {
return NS_ERROR_FAILURE;
}
}
else if ((nsnull != constructor) && JSVAL_IS_OBJECT(vp)) {
proto = JSVAL_TO_OBJECT(vp);
}
else {
return NS_ERROR_FAILURE;
}
if (aPrototype) {
*aPrototype = proto;
}
return NS_OK;
}
//
// Method for creating a new Location JavaScript object
//
extern "C" NS_DOM nsresult NS_NewScriptLocation(nsIScriptContext *aContext, nsIDOMLocation *aSupports, nsISupports *aParent, void **aReturn)
{
NS_PRECONDITION(nsnull != aContext && nsnull != aSupports && nsnull != aReturn, "null argument to NS_NewScriptLocation");
JSObject *proto;
JSObject *parent;
nsIScriptObjectOwner *owner;
JSContext *jscontext = (JSContext *)aContext->GetNativeContext();
if (nsnull == aParent) {
parent = nsnull;
}
else if (NS_OK == aParent->QueryInterface(kIScriptObjectOwnerIID, (void**)&owner)) {
if (NS_OK != owner->GetScriptObject(aContext, (void **)&parent)) {
NS_RELEASE(owner);
return NS_ERROR_FAILURE;
}
NS_RELEASE(owner);
}
else {
return NS_ERROR_FAILURE;
}
if (NS_OK != NS_InitLocationClass(aContext, (void **)&proto)) {
return NS_ERROR_FAILURE;
}
// create a js object for this class
*aReturn = JS_NewObject(jscontext, &LocationClass, proto, parent);
if (nsnull != *aReturn) {
// connect the native object to the js object
JS_SetPrivate(jscontext, (JSObject *)*aReturn, aSupports);
NS_ADDREF(aSupports);
}
else {
return NS_ERROR_FAILURE;
}
return NS_OK;
}

522
dom/src/base/nsLocation.cpp Normal file
Просмотреть файл

@ -0,0 +1,522 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#include "nsGlobalWindow.h"
#include "nsIWebShell.h"
#include "nsIURL.h"
#include "plstr.h"
#include "prmem.h"
static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kIDOMLocationIID, NS_IDOMLOCATION_IID);
LocationImpl::LocationImpl(nsIWebShell *aWebShell)
{
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;
}
if (aIID.Equals(kISupportsIID)) {
*aInstancePtrResult = (void*)(nsISupports*)(nsIScriptObjectOwner*)this;
AddRef();
return NS_OK;
}
return NS_NOINTERFACE;
}
nsresult
LocationImpl::ResetScriptObject()
{
mScriptObject = nsnull;
return NS_OK;
}
nsresult
LocationImpl::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject)
{
NS_PRECONDITION(nsnull != aScriptObject, "null arg");
nsresult res = NS_OK;
if (nsnull == mScriptObject) {
nsIScriptGlobalObject *global = aContext->GetGlobalObject();
res = NS_NewScriptLocation(aContext, this, global, &mScriptObject);
NS_IF_RELEASE(global);
}
*aScriptObject = mScriptObject;
return res;
}
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;
}
void
LocationImpl::ConcatenateAndSet(const char *aProtocol,
const char *aHost,
PRInt32 aPort,
const char *aFile,
const char *aRef,
const char *aSearch)
{
nsAutoString href;
href.SetString(aProtocol);
href.Append("://");
if (nsnull != aHost) {
href.Append(aHost);
if (0 < aPort) {
href.Append(':');
href.Append(aPort, 10);
}
}
href.Append(aFile);
if (nsnull != aRef) {
if ('#' != *aRef) {
href.Append('#');
}
href.Append(aRef);
}
if (nsnull != aSearch) {
if ('?' != *aSearch) {
href.Append('?');
}
href.Append(aSearch);
}
SetHref(href);
}
NS_IMETHODIMP
LocationImpl::GetHash(nsString& aHash)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
const char *ref;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
ref = url->GetRef();
if ((nsnull != ref) && ('\0' != *ref)) {
aHash.SetString("#");
aHash.Append(ref);
}
else {
aHash.SetLength(0);
}
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::SetHash(const nsString& aHash)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
char *buf = aHash.ToNewCString();
ConcatenateAndSet(url->GetProtocol(), url->GetHost(),
url->GetPort(), url->GetFile(),
buf, url->GetSearch());
delete buf;
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::GetHost(nsString& aHost)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
aHost.SetString(url->GetHost());
PRInt32 port = url->GetPort();
if (-1 != port) {
aHost.Append(":");
aHost.Append(port, 10);
}
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::SetHost(const nsString& aHost)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
char *buf = aHost.ToNewCString();
ConcatenateAndSet(url->GetProtocol(), buf,
-1, url->GetFile(),
url->GetRef(), url->GetSearch());
delete buf;
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::GetHostname(nsString& aHostname)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
aHostname.SetString(url->GetHost());
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::SetHostname(const nsString& aHostname)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
char *buf = aHostname.ToNewCString();
ConcatenateAndSet(url->GetProtocol(), buf,
url->GetPort(), url->GetFile(),
url->GetRef(), url->GetSearch());
delete buf;
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::GetHref(nsString& aHref)
{
PRInt32 index;
nsresult result = NS_OK;
if (nsnull != mWebShell) {
mWebShell->GetHistoryIndex(index);
result = mWebShell->GetURL(index, aHref);
}
return result;
}
NS_IMETHODIMP
LocationImpl::SetHref(const nsString& aHref)
{
if (nsnull != mWebShell) {
return mWebShell->LoadURL(aHref, nsnull, PR_TRUE);
}
return NS_OK;
}
NS_IMETHODIMP
LocationImpl::GetPathname(nsString& aPathname)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
aPathname.SetString(url->GetFile());
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::SetPathname(const nsString& aPathname)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
char *buf = aPathname.ToNewCString();
ConcatenateAndSet(url->GetProtocol(), url->GetHost(),
url->GetPort(), buf,
url->GetRef(), url->GetSearch());
delete buf;
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::GetPort(nsString& aPort)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
aPort.SetLength(0);
PRInt32 port = url->GetPort();
if (-1 != port) {
aPort.Append(port, 10);
}
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::SetPort(const nsString& aPort)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
char *buf = aPort.ToNewCString();
PRInt32 port = -1;
if (buf) {
if (*buf == ':') {
port = atol(buf+1);
}
else {
port = atol(buf);
}
}
ConcatenateAndSet(url->GetProtocol(), url->GetHost(),
port, url->GetFile(),
url->GetRef(), url->GetSearch());
delete buf;
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::GetProtocol(nsString& aProtocol)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
aProtocol.SetString(url->GetProtocol());
aProtocol.Append(":");
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::SetProtocol(const nsString& aProtocol)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
char *buf = aProtocol.ToNewCString();
ConcatenateAndSet(buf, url->GetHost(),
url->GetPort(), url->GetFile(),
url->GetRef(), url->GetSearch());
delete buf;
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::GetSearch(nsString& aSearch)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
const char *search = url->GetSearch();
if ((nsnull != search) && ('\0' != *search)) {
aSearch.SetString("?");
aSearch.Append(search);
}
else {
aSearch.SetLength(0);
}
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::SetSearch(const nsString& aSearch)
{
nsAutoString href;
nsIURL *url;
nsresult result = NS_OK;
result = GetHref(href);
if (NS_OK == result) {
result = NS_NewURL(&url, href);
if (NS_OK == result) {
char *buf = aSearch.ToNewCString();
ConcatenateAndSet(url->GetProtocol(), url->GetHost(),
url->GetPort(), url->GetFile(),
url->GetRef(), buf);
delete buf;
NS_IF_RELEASE(url);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::Reload(JSContext *cx, jsval *argv, PRUint32 argc)
{
nsAutoString url;
PRInt32 index;
nsresult result = NS_OK;
if (nsnull != mWebShell) {
mWebShell->GetHistoryIndex(index);
result = mWebShell->GetURL(index, url);
if (NS_OK == result) {
result = mWebShell->LoadURL(url, nsnull, PR_FALSE);
}
}
return result;
}
NS_IMETHODIMP
LocationImpl::Replace(const nsString& aUrl)
{
if (nsnull != mWebShell) {
return mWebShell->LoadURL(aUrl, nsnull, PR_FALSE);
}
return NS_OK;
}

Просмотреть файл

@ -18,6 +18,7 @@
#include "nsJSEnvironment.h"
#include "nsIScriptGlobalObject.h"
#include "nsIDOMNavigator.h"
#include "nsIDOMLocation.h"
#include "nsIDOMHTMLDocument.h"
#include "nsIDOMHTMLCollection.h"
#include "nsIDOMHTMLImageElement.h"
@ -33,6 +34,7 @@ void XXXDomNeverCalled()
nsJSContext* jcx = new nsJSContext(0);
NS_NewScriptGlobalObject(0);
NS_NewScriptNavigator(0, 0, 0, 0);
NS_NewScriptLocation(0, 0, 0, 0);
NS_NewScriptHTMLDocument(0, 0, 0, 0);
NS_NewScriptHTMLCollection(0, 0, 0, 0);
NS_NewScriptEventListener(0, 0, 0);