Added window.navigator and Navigator interface

This commit is contained in:
vidur 1998-07-01 23:55:40 +00:00
parent ffb283d109
commit e611df65a1
19 changed files with 937 additions and 15 deletions

View File

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

View File

@ -32,7 +32,7 @@ EXPORTS = \
nsIDOMNodeIterator.h \
nsIDOMPI.h \
nsIDOMText.h \
nsIDOMTreeIterator.h
nsIDOMTreeIterator.h \
$(NULL)
MODULE = dom

View File

@ -23,6 +23,7 @@ EXPORTS=nsIDOMAttribute.h nsIDOMAttributeList.h nsIDOMComment.h \
nsIDOMDOM.h nsIDOMDocument.h nsIDOMDocumentContext.h \
nsIDOMDocumentFragment.h nsIDOMElement.h nsIDOMNode.h \
nsIDOMNodeIterator.h nsIDOMPI.h nsIDOMText.h nsIDOMTreeIterator.h
MODULE=dom
include <$(DEPTH)\config\rules.mak>

View File

@ -28,7 +28,7 @@ class nsIDOMNodeIterator;
class nsIDOMNode;
#define NS_IDOMNODE_IID \
{ 0x6f7652e8, 0xee43, 0x11d1, \
{ 0x6f7652e9, 0xee43, 0x11d1, \
{ 0x9b, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
class nsIDOMNode : public nsISupports {

View File

@ -28,7 +28,7 @@ class nsIDOMNodeIterator;
class nsIDOMNode;
#define NS_IDOMNODEITERATOR_IID \
{ 0x6f7652e9, 0xee43, 0x11d1, \
{ 0x6f7652ea, 0xee43, 0x11d1, \
{ 0x9b, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
class nsIDOMNodeIterator : public nsISupports {

View File

@ -28,7 +28,7 @@
class nsIDOMPI;
#define NS_IDOMPI_IID \
{ 0x6f7652ea, 0xee43, 0x11d1, \
{ 0x6f7652eb, 0xee43, 0x11d1, \
{ 0x9b, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
class nsIDOMPI : public nsIDOMNode {

View File

@ -29,7 +29,7 @@ class nsIDOMElement;
class nsIDOMText;
#define NS_IDOMTEXT_IID \
{ 0x6f7652eb, 0xee43, 0x11d1, \
{ 0x6f7652ec, 0xee43, 0x11d1, \
{ 0x9b, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
class nsIDOMText : public nsIDOMNode {

View File

@ -29,7 +29,7 @@ class nsIDOMTreeIterator;
class nsIDOMNode;
#define NS_IDOMTREEITERATOR_IID \
{ 0x6f7652ec, 0xee43, 0x11d1, \
{ 0x6f7652ed, 0xee43, 0x11d1, \
{ 0x9b, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
class nsIDOMTreeIterator : public nsIDOMNodeIterator {

View File

@ -0,0 +1,11 @@
interface Navigator {
readonly attribute wstring userAgent;
readonly attribute wstring appCodeName;
readonly attribute wstring appVersion;
readonly attribute wstring appName;
readonly attribute wstring language;
readonly attribute wstring platform;
readonly attribute wstring securityPolicy;
boolean javaEnabled();
};

View File

@ -1,4 +1,7 @@
interface Window {
readonly attribute Window window;
readonly attribute Document document;
readonly attribute Navigator navigator;
void dump(in wstring str);
void alert(in wstring str);
};

View File

@ -29,6 +29,7 @@ IDLSRCS = \
DocumentContext.idl \
DocumentFragment.idl \
Element.idl \
Navigator.idl \
Node.idl \
NodeIterator.idl \
PI.idl \

View File

@ -21,7 +21,8 @@ IGNORE_MANIFEST=1
DIRS=coreDom coreEvents events
DEFINES=-D_IMPL_NS_DOM
EXPORTS=nsIScriptContext.h nsIJSScriptObject.h nsIScriptObjectOwner.h \
nsIScriptGlobalObject.h nsIDOMWindow.h nsIScriptContextOwner.h
nsIScriptGlobalObject.h nsIDOMWindow.h nsIScriptContextOwner.h \
nsIDOMNavigator.h
MODULE=dom

View File

@ -0,0 +1,57 @@
/* -*- 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 nsIDOMNavigator_h__
#define nsIDOMNavigator_h__
#include "nsISupports.h"
#include "nsString.h"
#include "nsIScriptContext.h"
class nsIDOMNavigator;
#define NS_IDOMNAVIGATOR_IID \
{ 0x6f7652e8, 0xee43, 0x11d1, \
{ 0x9b, 0xc3, 0x00, 0x60, 0x08, 0x8c, 0xa6, 0xb3 } }
class nsIDOMNavigator : public nsISupports {
public:
NS_IMETHOD GetUserAgent(nsString& aUserAgent)=0;
NS_IMETHOD GetAppCodeName(nsString& aAppCodeName)=0;
NS_IMETHOD GetAppVersion(nsString& aAppVersion)=0;
NS_IMETHOD GetAppName(nsString& aAppName)=0;
NS_IMETHOD GetLanguage(nsString& aLanguage)=0;
NS_IMETHOD GetPlatform(nsString& aPlatform)=0;
NS_IMETHOD GetSecurityPolicy(nsString& aSecurityPolicy)=0;
NS_IMETHOD JavaEnabled(PRBool* aReturn)=0;
};
extern nsresult NS_InitNavigatorClass(nsIScriptContext *aContext, void **aPrototype);
extern "C" NS_DOM NS_NewScriptNavigator(nsIScriptContext *aContext, nsIDOMNavigator *aSupports, nsISupports *aParent, void **aReturn);
#endif // nsIDOMNavigator_h__

View File

@ -21,7 +21,9 @@
#include "nsISupports.h"
#include "nsString.h"
#include "nsIScriptContext.h"
class nsIDOMNavigator;
class nsIDOMDocument;
class nsIDOMWindow;
@ -32,9 +34,15 @@ class nsIDOMWindow;
class nsIDOMWindow : public nsISupports {
public:
NS_IMETHOD GetWindow(nsIDOMWindow** aWindow)=0;
NS_IMETHOD GetDocument(nsIDOMDocument** aDocument)=0;
NS_IMETHOD GetNavigator(nsIDOMNavigator** aNavigator)=0;
NS_IMETHOD Dump(nsString& aStr)=0;
NS_IMETHOD Alert(nsString& aStr)=0;
};
extern nsresult NS_InitWindowClass(nsIScriptContext *aContext, nsIScriptGlobalObject *aGlobalObject);

View File

@ -32,12 +32,13 @@ CPPSRCS = \
nsJSAttribute.cpp \
nsJSAttributeList.cpp\
nsJSWindow.cpp\
nsJSNavigator.cpp \
nsGlobalWindow.cpp \
$(NULL)
MODULE = raptor
REQUIRES = xpcom raptor dom js
REQUIRES = xpcom raptor dom js netlib
include $(DEPTH)/config/config.mk

View File

@ -34,20 +34,22 @@ CPPSRCS = \
nsJSAttribute.cpp \
nsJSAttributeList.cpp \
nsJSWindow.cpp \
nsJSNavigator.cpp \
nsGlobalWindow.cpp \
$(NULL)
MODULE=raptor
REQUIRES=xpcom raptor js
REQUIRES=xpcom raptor js netlib
CPP_OBJS= .\$(OBJDIR)\nsJSEnvironment.obj .\$(OBJDIR)\nsJSNode.obj .\$(OBJDIR)\nsJSDocument.obj \
.\$(OBJDIR)\nsJSElement.obj .\$(OBJDIR)\nsJSText.obj \
.\$(OBJDIR)\nsJSNodeIterator.obj .\$(OBJDIR)\nsJSAttribute.obj \
.\$(OBJDIR)\nsJSAttributeList.obj .\$(OBJDIR)\nsJSWindow.obj \
.\$(OBJDIR)\nsGlobalWindow.obj .\$(OBJDIR)\nsJSDocumentFragment.obj
.\$(OBJDIR)\nsGlobalWindow.obj .\$(OBJDIR)\nsJSDocumentFragment.obj \
.\$(OBJDIR)\nsJSNavigator.obj
LINCS=-I$(XPDIST)\public\xpcom -I$(XPDIST)\public\raptor -I$(XPDIST)\public\dom -I$(XPDIST)\public\js
LINCS=-I$(XPDIST)\public\xpcom -I$(XPDIST)\public\raptor -I$(XPDIST)\public\dom -I$(XPDIST)\public\js -I$(PUBLIC)\netlib
LCFLAGS = \
$(LCFLAGS) \
@ -57,6 +59,7 @@ LCFLAGS = \
LLIBS= \
$(DIST)\lib\xpcom32.lib \
$(DIST)\lib\raptorbase.lib \
$(DIST)\lib\netlib.lib \
$(DIST)\lib\js3240.lib \
$(LIBNSPR)

View File

@ -24,7 +24,9 @@
#include "nsIScriptGlobalObject.h"
#include "nsIDOMWindow.h"
#include "nsIDOMDocument.h"
#include "nsHashtable.h"
#include "nsIDOMNavigator.h"
#include "nsINetService.h"
#include "nsINetContainerApplication.h"
#include "jsapi.h"
@ -32,6 +34,7 @@ static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kIScriptGlobalObjectIID, NS_ISCRIPTGLOBALOBJECT_IID);
static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);
static NS_DEFINE_IID(kIDOMWindowIID, NS_IDOMWINDOW_IID);
static NS_DEFINE_IID(kIDOMNavigatorIID, NS_IDOMNAVIGATOR_IID);
// Global object for scripting
class GlobalWindowImpl : public nsIScriptObjectOwner, public nsIScriptGlobalObject, public nsIDOMWindow
@ -48,20 +51,57 @@ public:
NS_IMETHOD_(void) SetContext(nsIScriptContext *aContext);
NS_IMETHOD_(void) SetNewDocument(nsIDOMDocument *aDocument);
NS_IMETHOD GetWindow(nsIDOMWindow** aWindow);
NS_IMETHOD GetDocument(nsIDOMDocument** aDocument);
NS_IMETHOD GetNavigator(nsIDOMNavigator** aNavigator);
NS_IMETHOD Dump(nsString& aStr);
NS_IMETHOD Alert(nsString& aStr);
protected:
nsIScriptContext *mContext;
void *mScriptObject;
nsIDOMDocument *mDocument;
nsIDOMNavigator *mNavigator;
};
// Script "navigator" object
class NavigatorImpl : public nsIScriptObjectOwner, public nsIDOMNavigator {
public:
NavigatorImpl();
~NavigatorImpl();
NS_DECL_ISUPPORTS
NS_IMETHOD GetScriptObject(nsIScriptContext *aContext, void** aScriptObject);
NS_IMETHOD ResetScriptObject();
NS_IMETHOD GetUserAgent(nsString& aUserAgent);
NS_IMETHOD GetAppCodeName(nsString& aAppCodeName);
NS_IMETHOD GetAppVersion(nsString& aAppVersion);
NS_IMETHOD GetAppName(nsString& aAppName);
NS_IMETHOD GetLanguage(nsString& aLanguage);
NS_IMETHOD GetPlatform(nsString& aPlatform);
NS_IMETHOD GetSecurityPolicy(nsString& aSecurityPolicy);
NS_IMETHOD JavaEnabled(PRBool* aReturn);
protected:
void *mScriptObject;
};
GlobalWindowImpl::GlobalWindowImpl()
{
mContext = nsnull;
mScriptObject = nsnull;
mDocument = nsnull;
mNavigator = nsnull;
}
GlobalWindowImpl::~GlobalWindowImpl()
@ -78,6 +118,8 @@ GlobalWindowImpl::~GlobalWindowImpl()
if (nsnull != mDocument) {
NS_RELEASE(mDocument);
}
NS_IF_RELEASE(mNavigator);
}
NS_IMPL_ADDREF(GlobalWindowImpl)
@ -173,6 +215,15 @@ GlobalWindowImpl::SetNewDocument(nsIDOMDocument *aDocument)
}
}
NS_IMETHODIMP
GlobalWindowImpl::GetWindow(nsIDOMWindow** aWindow)
{
*aWindow = this;
NS_ADDREF(this);
return NS_OK;
}
NS_IMETHODIMP
GlobalWindowImpl::GetDocument(nsIDOMDocument** aDocument)
{
@ -182,6 +233,20 @@ GlobalWindowImpl::GetDocument(nsIDOMDocument** aDocument)
return NS_OK;
}
NS_IMETHODIMP
GlobalWindowImpl::GetNavigator(nsIDOMNavigator** aNavigator)
{
if (nsnull == mNavigator) {
mNavigator = new NavigatorImpl();
NS_IF_ADDREF(mNavigator);
}
*aNavigator = mNavigator;
NS_IF_ADDREF(mNavigator);
return NS_OK;
}
NS_IMETHODIMP
GlobalWindowImpl::Dump(nsString& aStr)
{
@ -195,6 +260,12 @@ GlobalWindowImpl::Dump(nsString& aStr)
return NS_OK;
}
NS_IMETHODIMP
GlobalWindowImpl::Alert(nsString& aStr)
{
return NS_OK;
}
extern "C" NS_DOM
NS_NewScriptGlobalObject(nsIScriptGlobalObject **aResult)
{
@ -211,3 +282,213 @@ NS_NewScriptGlobalObject(nsIScriptGlobalObject **aResult)
return global->QueryInterface(kIScriptGlobalObjectIID, (void **)aResult);
}
//
// Navigator class implementation
//
NavigatorImpl::NavigatorImpl()
{
mScriptObject = nsnull;
}
NavigatorImpl::~NavigatorImpl()
{
}
NS_IMPL_ADDREF(NavigatorImpl)
NS_IMPL_RELEASE(NavigatorImpl)
nsresult
NavigatorImpl::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(kIDOMNavigatorIID)) {
*aInstancePtrResult = (void*) ((nsIDOMNavigator*)this);
AddRef();
return NS_OK;
}
if (aIID.Equals(kISupportsIID)) {
*aInstancePtrResult = (void*)(nsISupports*)(nsIScriptObjectOwner*)this;
AddRef();
return NS_OK;
}
return NS_NOINTERFACE;
}
nsresult
NavigatorImpl::ResetScriptObject()
{
mScriptObject = nsnull;
return NS_OK;
}
nsresult
NavigatorImpl::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject)
{
NS_PRECONDITION(nsnull != aScriptObject, "null arg");
nsresult res = NS_OK;
if (nsnull == mScriptObject) {
nsIScriptGlobalObject *global = aContext->GetGlobalObject();
res = NS_NewScriptNavigator(aContext, this, global, &mScriptObject);
NS_IF_RELEASE(global);
}
*aScriptObject = mScriptObject;
return res;
}
NS_IMETHODIMP
NavigatorImpl::GetUserAgent(nsString& aUserAgent)
{
nsINetService *service;
nsresult res = NS_OK;
res = NS_NewINetService(&service, nsnull);
if ((NS_OK == res) && (nsnull != service)) {
nsINetContainerApplication *container;
res = service->GetContainerApplication(&container);
if ((NS_OK == res) && (nsnull != container)) {
nsAutoString appVersion;
container->GetAppCodeName(aUserAgent);
container->GetAppVersion(appVersion);
aUserAgent.Append('/');
aUserAgent.Append(appVersion);
NS_RELEASE(container);
}
NS_RELEASE(service);
}
return res;
}
NS_IMETHODIMP
NavigatorImpl::GetAppCodeName(nsString& aAppCodeName)
{
nsINetService *service;
nsresult res = NS_OK;
res = NS_NewINetService(&service, nsnull);
if ((NS_OK == res) && (nsnull != service)) {
nsINetContainerApplication *container;
res = service->GetContainerApplication(&container);
if ((NS_OK == res) && (nsnull != container)) {
res = container->GetAppCodeName(aAppCodeName);
NS_RELEASE(container);
}
NS_RELEASE(service);
}
return res;
}
NS_IMETHODIMP
NavigatorImpl::GetAppVersion(nsString& aAppVersion)
{
nsINetService *service;
nsresult res = NS_OK;
res = NS_NewINetService(&service, nsnull);
if ((NS_OK == res) && (nsnull != service)) {
nsINetContainerApplication *container;
res = service->GetContainerApplication(&container);
if ((NS_OK == res) && (nsnull != container)) {
res = container->GetAppVersion(aAppVersion);
NS_RELEASE(container);
}
NS_RELEASE(service);
}
return res;
}
NS_IMETHODIMP
NavigatorImpl::GetAppName(nsString& aAppName)
{
nsINetService *service;
nsresult res = NS_OK;
res = NS_NewINetService(&service, nsnull);
if ((NS_OK == res) && (nsnull != service)) {
nsINetContainerApplication *container;
res = service->GetContainerApplication(&container);
if ((NS_OK == res) && (nsnull != container)) {
res = container->GetAppName(aAppName);
NS_RELEASE(container);
}
NS_RELEASE(service);
}
return res;
}
NS_IMETHODIMP
NavigatorImpl::GetLanguage(nsString& aLanguage)
{
nsINetService *service;
nsresult res = NS_OK;
res = NS_NewINetService(&service, nsnull);
if ((NS_OK == res) && (nsnull != service)) {
nsINetContainerApplication *container;
res = service->GetContainerApplication(&container);
if ((NS_OK == res) && (nsnull != container)) {
res = container->GetLanguage(aLanguage);
NS_RELEASE(container);
}
NS_RELEASE(service);
}
return res;
}
NS_IMETHODIMP
NavigatorImpl::GetPlatform(nsString& aPlatform)
{
nsINetService *service;
nsresult res = NS_OK;
res = NS_NewINetService(&service, nsnull);
if ((NS_OK == res) && (nsnull != service)) {
nsINetContainerApplication *container;
res = service->GetContainerApplication(&container);
if ((NS_OK == res) && (nsnull != container)) {
res = container->GetPlatform(aPlatform);
NS_RELEASE(container);
}
NS_RELEASE(service);
}
return res;
}
NS_IMETHODIMP
NavigatorImpl::GetSecurityPolicy(nsString& aSecurityPolicy)
{
return NS_OK;
}
NS_IMETHODIMP
NavigatorImpl::JavaEnabled(PRBool* aReturn)
{
*aReturn = PR_FALSE;
return NS_OK;
}

444
dom/src/nsJSNavigator.cpp Normal file
View File

@ -0,0 +1,444 @@
/* -*- 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 "nsIDOMNavigator.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(kINavigatorIID, NS_IDOMNAVIGATOR_IID);
NS_DEF_PTR(nsIDOMNavigator);
//
// Navigator property ids
//
enum Navigator_slots {
NAVIGATOR_USERAGENT = -11,
NAVIGATOR_APPCODENAME = -12,
NAVIGATOR_APPVERSION = -13,
NAVIGATOR_APPNAME = -14,
NAVIGATOR_LANGUAGE = -15,
NAVIGATOR_PLATFORM = -16,
NAVIGATOR_SECURITYPOLICY = -17
};
/***********************************************************************/
//
// Navigator Properties Getter
//
PR_STATIC_CALLBACK(JSBool)
GetNavigatorProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
nsIDOMNavigator *a = (nsIDOMNavigator*)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 NAVIGATOR_USERAGENT:
{
nsAutoString prop;
if (NS_OK == a->GetUserAgent(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
case NAVIGATOR_APPCODENAME:
{
nsAutoString prop;
if (NS_OK == a->GetAppCodeName(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
case NAVIGATOR_APPVERSION:
{
nsAutoString prop;
if (NS_OK == a->GetAppVersion(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
case NAVIGATOR_APPNAME:
{
nsAutoString prop;
if (NS_OK == a->GetAppName(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
case NAVIGATOR_LANGUAGE:
{
nsAutoString prop;
if (NS_OK == a->GetLanguage(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
case NAVIGATOR_PLATFORM:
{
nsAutoString prop;
if (NS_OK == a->GetPlatform(prop)) {
JSString *jsstring = JS_NewUCStringCopyN(cx, prop, prop.Length());
// set the return value
*vp = STRING_TO_JSVAL(jsstring);
}
else {
return JS_FALSE;
}
break;
}
case NAVIGATOR_SECURITYPOLICY:
{
nsAutoString prop;
if (NS_OK == a->GetSecurityPolicy(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;
}
}
}
}
return PR_TRUE;
}
/***********************************************************************/
//
// Navigator Properties Setter
//
PR_STATIC_CALLBACK(JSBool)
SetNavigatorProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
nsIDOMNavigator *a = (nsIDOMNavigator*)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 0:
default:
{
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;
}
//
// Navigator finalizer
//
PR_STATIC_CALLBACK(void)
FinalizeNavigator(JSContext *cx, JSObject *obj)
{
nsIDOMNavigator *a = (nsIDOMNavigator*)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);
}
}
//
// Navigator enumerate
//
PR_STATIC_CALLBACK(JSBool)
EnumerateNavigator(JSContext *cx, JSObject *obj)
{
nsIDOMNavigator *a = (nsIDOMNavigator*)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;
}
//
// Navigator resolve
//
PR_STATIC_CALLBACK(JSBool)
ResolveNavigator(JSContext *cx, JSObject *obj, jsval id)
{
nsIDOMNavigator *a = (nsIDOMNavigator*)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 JavaEnabled
//
PR_STATIC_CALLBACK(JSBool)
NavigatorJavaEnabled(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
nsIDOMNavigator *nativeThis = (nsIDOMNavigator*)JS_GetPrivate(cx, obj);
JSBool rBool = JS_FALSE;
PRBool nativeRet;
*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->JavaEnabled(&nativeRet)) {
return JS_FALSE;
}
*rval = BOOLEAN_TO_JSVAL(nativeRet);
}
else {
return JS_FALSE;
}
return JS_TRUE;
}
/***********************************************************************/
//
// class for Navigator
//
JSClass NavigatorClass = {
"Navigator",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub,
JS_PropertyStub,
GetNavigatorProperty,
SetNavigatorProperty,
EnumerateNavigator,
ResolveNavigator,
JS_ConvertStub,
FinalizeNavigator
};
//
// Navigator class properties
//
static JSPropertySpec NavigatorProperties[] =
{
{"userAgent", NAVIGATOR_USERAGENT, JSPROP_ENUMERATE | JSPROP_READONLY},
{"appCodeName", NAVIGATOR_APPCODENAME, JSPROP_ENUMERATE | JSPROP_READONLY},
{"appVersion", NAVIGATOR_APPVERSION, JSPROP_ENUMERATE | JSPROP_READONLY},
{"appName", NAVIGATOR_APPNAME, JSPROP_ENUMERATE | JSPROP_READONLY},
{"language", NAVIGATOR_LANGUAGE, JSPROP_ENUMERATE | JSPROP_READONLY},
{"platform", NAVIGATOR_PLATFORM, JSPROP_ENUMERATE | JSPROP_READONLY},
{"securityPolicy", NAVIGATOR_SECURITYPOLICY, JSPROP_ENUMERATE | JSPROP_READONLY},
{0}
};
//
// Navigator class methods
//
static JSFunctionSpec NavigatorMethods[] =
{
{"javaEnabled", NavigatorJavaEnabled, 0},
{0}
};
//
// Navigator constructor
//
PR_STATIC_CALLBACK(JSBool)
Navigator(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
return JS_TRUE;
}
//
// Navigator class initialization
//
nsresult NS_InitNavigatorClass(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, "Navigator", &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
&NavigatorClass, // JSClass
Navigator, // JSNative ctor
0, // ctor args
NavigatorProperties, // proto props
NavigatorMethods, // 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 Navigator JavaScript object
//
extern "C" NS_DOM NS_NewScriptNavigator(nsIScriptContext *aContext, nsIDOMNavigator *aSupports, nsISupports *aParent, void **aReturn)
{
NS_PRECONDITION(nsnull != aContext && nsnull != aSupports && nsnull != aReturn, "null argument to NS_NewScriptNavigator");
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_InitNavigatorClass(aContext, (void **)&proto)) {
return NS_ERROR_FAILURE;
}
// create a js object for this class
*aReturn = JS_NewObject(jscontext, &NavigatorClass, 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;
}

View File

@ -24,6 +24,7 @@
#include "nsIScriptGlobalObject.h"
#include "nsIPtr.h"
#include "nsString.h"
#include "nsIDOMNavigator.h"
#include "nsIDOMDocument.h"
#include "nsIDOMWindow.h"
@ -31,9 +32,11 @@
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(kINavigatorIID, NS_IDOMNAVIGATOR_IID);
static NS_DEFINE_IID(kIDocumentIID, NS_IDOMDOCUMENT_IID);
static NS_DEFINE_IID(kIWindowIID, NS_IDOMWINDOW_IID);
NS_DEF_PTR(nsIDOMNavigator);
NS_DEF_PTR(nsIDOMDocument);
NS_DEF_PTR(nsIDOMWindow);
@ -41,7 +44,9 @@ NS_DEF_PTR(nsIDOMWindow);
// Window property ids
//
enum Window_slots {
WINDOW_DOCUMENT = -11
WINDOW_WINDOW = -11,
WINDOW_DOCUMENT = -12,
WINDOW_NAVIGATOR = -13
};
/***********************************************************************/
@ -52,10 +57,41 @@ PR_STATIC_CALLBACK(JSBool)
GetWindowProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
nsIDOMWindow *a = (nsIDOMWindow*)JS_GetPrivate(cx, obj);
NS_ASSERTION(nsnull != a, "null pointer");
// 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 WINDOW_WINDOW:
{
nsIDOMWindow* prop;
if (NS_OK == a->GetWindow(&prop)) {
// get the js object
if (prop != nsnull) {
nsIScriptObjectOwner *owner = nsnull;
if (NS_OK == prop->QueryInterface(kIScriptObjectOwnerIID, (void**)&owner)) {
JSObject *object = nsnull;
nsIScriptContext *script_cx = (nsIScriptContext *)JS_GetContextPrivate(cx);
if (NS_OK == owner->GetScriptObject(script_cx, (void**)&object)) {
// set the return value
*vp = OBJECT_TO_JSVAL(object);
}
NS_RELEASE(owner);
}
NS_RELEASE(prop);
}
else {
*vp = JSVAL_NULL;
}
}
else {
return JS_FALSE;
}
break;
}
case WINDOW_DOCUMENT:
{
nsIDOMDocument* prop;
@ -83,6 +119,33 @@ GetWindowProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
}
break;
}
case WINDOW_NAVIGATOR:
{
nsIDOMNavigator* prop;
if (NS_OK == a->GetNavigator(&prop)) {
// get the js object
if (prop != nsnull) {
nsIScriptObjectOwner *owner = nsnull;
if (NS_OK == prop->QueryInterface(kIScriptObjectOwnerIID, (void**)&owner)) {
JSObject *object = nsnull;
nsIScriptContext *script_cx = (nsIScriptContext *)JS_GetContextPrivate(cx);
if (NS_OK == owner->GetScriptObject(script_cx, (void**)&object)) {
// set the return value
*vp = OBJECT_TO_JSVAL(object);
}
NS_RELEASE(owner);
}
NS_RELEASE(prop);
}
else {
*vp = JSVAL_NULL;
}
}
else {
return JS_FALSE;
}
break;
}
default:
{
nsIJSScriptObject *object;
@ -107,7 +170,11 @@ PR_STATIC_CALLBACK(JSBool)
SetWindowProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
{
nsIDOMWindow *a = (nsIDOMWindow*)JS_GetPrivate(cx, obj);
NS_ASSERTION(nsnull != a, "null pointer");
// 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)) {
@ -214,6 +281,46 @@ WindowDump(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
}
//
// Native method Alert
//
PR_STATIC_CALLBACK(JSBool)
WindowAlert(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
nsIDOMWindow *nativeThis = (nsIDOMWindow*)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->Alert(b0)) {
return JS_FALSE;
}
*rval = JSVAL_VOID;
}
else {
return JS_FALSE;
}
return JS_TRUE;
}
/***********************************************************************/
//
// class for Window
@ -237,7 +344,9 @@ JSClass WindowClass = {
//
static JSPropertySpec WindowProperties[] =
{
{"window", WINDOW_WINDOW, JSPROP_ENUMERATE | JSPROP_READONLY},
{"document", WINDOW_DOCUMENT, JSPROP_ENUMERATE | JSPROP_READONLY},
{"navigator", WINDOW_NAVIGATOR, JSPROP_ENUMERATE | JSPROP_READONLY},
{0}
};
@ -248,6 +357,7 @@ static JSPropertySpec WindowProperties[] =
static JSFunctionSpec WindowMethods[] =
{
{"dump", WindowDump, 1},
{"alert", WindowAlert, 1},
{0}
};