diff --git a/mozilla/xpfe/appshell/public/MANIFEST b/mozilla/xpfe/appshell/public/MANIFEST index 7bf1704c9c5..044ad5b7d1a 100644 --- a/mozilla/xpfe/appshell/public/MANIFEST +++ b/mozilla/xpfe/appshell/public/MANIFEST @@ -8,3 +8,4 @@ nsICmdLineService.h nsIWebShellWindow.h nsIWidgetController.h nsFileLocations.h +nsIDOMXPConnectFactory.h diff --git a/mozilla/xpfe/appshell/public/Makefile.in b/mozilla/xpfe/appshell/public/Makefile.in index 305f43a68a5..3f4fb760ddb 100644 --- a/mozilla/xpfe/appshell/public/Makefile.in +++ b/mozilla/xpfe/appshell/public/Makefile.in @@ -31,6 +31,7 @@ EXPORTS = \ nsIWidgetController.h \ nsIXULWindowCallbacks.h \ nsFileLocations.h \ + nsIDOMXPConnectFactory.h \ $(NULL) EXPORTS := $(addprefix $(srcdir)/, $(EXPORTS)) diff --git a/mozilla/xpfe/appshell/public/makefile.win b/mozilla/xpfe/appshell/public/makefile.win index 9d783640cf5..623a9752698 100644 --- a/mozilla/xpfe/appshell/public/makefile.win +++ b/mozilla/xpfe/appshell/public/makefile.win @@ -25,9 +25,10 @@ EXPORTS = \ nsIWebShellWindow.h \ nsAppShellCIDs.h \ nsIAppShellService.h \ - nsICmdLineService.h \ + nsICmdLineService.h \ nsIWidgetController.h \ nsIXULWindowCallbacks.h \ + nsIDOMXPConnectFactory.h \ nsFileLocations.h \ $(NULL) diff --git a/mozilla/xpfe/appshell/public/nsAppShellCIDs.h b/mozilla/xpfe/appshell/public/nsAppShellCIDs.h index c9559280519..effa184de7c 100644 --- a/mozilla/xpfe/appshell/public/nsAppShellCIDs.h +++ b/mozilla/xpfe/appshell/public/nsAppShellCIDs.h @@ -32,6 +32,10 @@ { 0x106b6f40, 0xd79e, 0x11d2, \ {0x99, 0xdb, 0x00, 0x80, 0xc7, 0xcb, 0x10, 0x80} } +// a6852d40-cd6f-11d2-92b6-00105a1b0d64 +#define NS_XPCONNECTFACTORY_CID \ +{ 0xa6852d40, 0xcd6f, 0x11d2, \ + {0x92, 0xb6, 0x00, 0x10, 0x5a, 0x1b, 0x0d, 0x64} } #endif /* nsAppShellCIDs_h__ */ diff --git a/mozilla/xpfe/appshell/public/nsIDOMXPConnectFactory.h b/mozilla/xpfe/appshell/public/nsIDOMXPConnectFactory.h new file mode 100644 index 00000000000..6ccd462357a --- /dev/null +++ b/mozilla/xpfe/appshell/public/nsIDOMXPConnectFactory.h @@ -0,0 +1,52 @@ +/* -*- 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 nsIDOMXPConnectFactory_h__ +#define nsIDOMXPConnectFactory_h__ + +#include "nsISupports.h" +#include "nsString.h" +#include "nsIScriptContext.h" + +#define NS_IDOMXPCONNECTFACTORY_IID \ + { 0x12221f90, 0xcd53, 0x11d2, \ + {0x92, 0xb6, 0x00, 0x10, 0x5a, 0x1b, 0x0d, 0x64}} + +class nsIDOMXPConnectFactory : public nsISupports { +public: + static const nsIID& IID() { static nsIID iid = NS_IDOMXPCONNECTFACTORY_IID; return iid; } + + NS_IMETHOD CreateInstance(const nsString& aProgID, nsISupports** aReturn)=0; +}; + + +#define NS_DECL_IDOMXPCONNECTFACTORY \ + NS_IMETHOD CreateInstance(const nsString& aProgID, nsISupports** aReturn); \ + + + +#define NS_FORWARD_IDOMXPCONNECTFACTORY(_to) \ + NS_IMETHOD CreateInstance(const nsString& aProgID, nsISupports** aReturn) { return _to##CreateInstance(aProgID, aReturn); } \ + + +extern "C" NS_APPSHELL nsresult NS_InitXPConnectFactoryClass(nsIScriptContext *aContext, void **aPrototype); + +extern "C" NS_APPSHELL nsresult NS_NewScriptXPConnectFactory(nsIScriptContext *aContext, nsISupports *aSupports, nsISupports *aParent, void **aReturn); + +#endif // nsIDOMXPConnectFactory_h__ diff --git a/mozilla/xpfe/appshell/src/nsAppShellFactory.cpp b/mozilla/xpfe/appshell/src/nsAppShellFactory.cpp index a00577f57c1..917aa1529fa 100644 --- a/mozilla/xpfe/appshell/src/nsAppShellFactory.cpp +++ b/mozilla/xpfe/appshell/src/nsAppShellFactory.cpp @@ -17,42 +17,57 @@ * Netscape Communications Corporation. All Rights Reserved. */ #include "nsIFactory.h" +#include "nsRepository.h" #include "nscore.h" #include "nsIComponentManager.h" #include "nsAppShellCIDs.h" #include "nsICmdLineService.h" -/* extern the factory entry points... */ +/* extern the factory entry points for each component... */ nsresult NS_NewAppShellServiceFactory(nsIFactory** aFactory); +nsresult NS_NewXPConnectFactoryFactory(nsIFactory** aResult); #if 0 nsresult NS_NewDefaultProtocolHelperFactory(nsIFactory** aResult); #endif + static NS_DEFINE_IID(kAppShellServiceCID, NS_APPSHELL_SERVICE_CID); static NS_DEFINE_IID(kCmdLineServiceCID, NS_COMMANDLINE_SERVICE_CID); static NS_DEFINE_IID(kProtocolHelperCID, NS_PROTOCOL_HELPER_CID); +static NS_DEFINE_IID(kXPConnectFactoryCID, NS_XPCONNECTFACTORY_CID); - - +/* + * Global entry point to register all components in the registry... + */ extern "C" NS_EXPORT nsresult NSRegisterSelf(nsISupports* serviceMgr, const char *path) { nsComponentManager::RegisterComponent(kAppShellServiceCID, NULL, NULL, path, PR_TRUE, PR_TRUE); nsComponentManager::RegisterComponent(kCmdLineServiceCID, NULL, NULL, path, PR_TRUE, PR_TRUE); nsComponentManager::RegisterComponent(kProtocolHelperCID, NULL, NULL, path, PR_TRUE, PR_TRUE); + nsComponentManager::RegisterComponent(kXPConnectFactoryCID, NULL, NULL, path, PR_TRUE, PR_TRUE); return NS_OK; } +/* + * Global entry point to unregister all components in the registry... + */ extern "C" NS_EXPORT nsresult NSUnregisterSelf(nsISupports* serviceMgr, const char *path) { nsComponentManager::UnregisterFactory(kAppShellServiceCID, path); nsComponentManager::UnregisterFactory(kCmdLineServiceCID, path); nsComponentManager::UnregisterFactory(kProtocolHelperCID, path); + nsComponentManager::UnregisterComponent(kXPConnectFactoryCID, path); return NS_OK; } + +/* + * Global entry point to create class factories for the components + * available withing the DLL... + */ #if defined(XP_MAC) && defined(MAC_STATIC) extern "C" NS_APPSHELL nsresult NSGetFactory_APPSHELL_DLL(nsISupports* serviceMgr, @@ -81,13 +96,16 @@ NSGetFactory(nsISupports* serviceMgr, else if (aClass.Equals(kCmdLineServiceCID)) { rv = NS_NewCmdLineServiceFactory(aFactory); } + else if (aClass.Equals(kXPConnectFactoryCID)) { + rv = NS_NewXPConnectFactoryFactory(aFactory); + } + #if 0 else if (aClass.Equals(kProtocolHelperCID)) { rv = NS_NewDefaultProtocolHelperFactory(aFactory); } #endif - return rv; } diff --git a/mozilla/xpfe/appshell/src/nsAppShellNameSet.cpp b/mozilla/xpfe/appshell/src/nsAppShellNameSet.cpp new file mode 100644 index 00000000000..b5ad61c393a --- /dev/null +++ b/mozilla/xpfe/appshell/src/nsAppShellNameSet.cpp @@ -0,0 +1,73 @@ + +/* -*- 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 "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/ + * + * 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. + * + * The Original Code is Mozilla Communicator client code. + * + * The Initial Developer of the Original Code is Netscape Communications + * Corporation. Portions created by Netscape are Copyright (C) 1998 + * Netscape Communications Corporation. All Rights Reserved. + */ + +#include "nsAppshellNameSet.h" +#include "nsIScriptContext.h" +#include "nsIScriptNameSpaceManager.h" +#include "nsIDOMXPConnectFactory.h" +#include "nsAppShellCIDs.h" + + +static NS_DEFINE_IID(kIScriptExternalNameSetIID, NS_ISCRIPTEXTERNALNAMESET_IID); +static NS_DEFINE_IID(kXPConnectFactoryCID, NS_XPCONNECTFACTORY_CID); + +nsAppShellNameSet::nsAppShellNameSet() +{ + NS_INIT_REFCNT(); +} + +nsAppShellNameSet::~nsAppShellNameSet() +{ +} + +NS_IMPL_ISUPPORTS(nsAppShellNameSet, kIScriptExternalNameSetIID); + + + + +NS_IMETHODIMP +nsAppShellNameSet::InitializeClasses(nsIScriptContext* aScriptContext) +{ + nsresult rv; + + rv = NS_InitXPConnectFactoryClass(aScriptContext, nsnull); + + return rv; +} + + + + +NS_IMETHODIMP +nsAppShellNameSet::AddNameSet(nsIScriptContext* aScriptContext) +{ + nsresult rv; + nsIScriptNameSpaceManager* manager; + + rv = aScriptContext->GetNameSpaceManager(&manager); + if (NS_SUCCEEDED(rv)) { + rv = manager->RegisterGlobalName("XPComFactory", + kXPConnectFactoryCID, + PR_FALSE); + NS_RELEASE(manager); + } + return rv; +} + diff --git a/mozilla/xpfe/appshell/src/nsAppShellNameSet.h b/mozilla/xpfe/appshell/src/nsAppShellNameSet.h new file mode 100644 index 00000000000..2aae686827f --- /dev/null +++ b/mozilla/xpfe/appshell/src/nsAppShellNameSet.h @@ -0,0 +1,42 @@ +/* -*- 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. + */ +#ifndef nsAppShellNameSet_h___ +#define nsAppShellNameSet_h___ + +#include "nsISupports.h" +#include "nsIScriptExternalNameSet.h" + +class nsIScriptContext; + +class nsAppShellNameSet : public nsIScriptExternalNameSet +{ +public: + nsAppShellNameSet(); + + // nsISupports interface... + NS_DECL_ISUPPORTS + + // nsIScriptExternalNameSet interface... + NS_IMETHOD InitializeClasses(nsIScriptContext* aScriptContext); + NS_IMETHOD AddNameSet(nsIScriptContext* aScriptContext); + +protected: + virtual ~nsAppShellNameSet(); +}; + +#endif // nsAppShellNameSet_h___ diff --git a/mozilla/xpfe/appshell/src/nsAppShellService.cpp b/mozilla/xpfe/appshell/src/nsAppShellService.cpp index bf85da96355..cecdfb9b77d 100644 --- a/mozilla/xpfe/appshell/src/nsAppShellService.cpp +++ b/mozilla/xpfe/appshell/src/nsAppShellService.cpp @@ -32,6 +32,11 @@ #include "nsIWebShellWindow.h" #include "nsWebShellWindow.h" +/* For Javascript Namespace Access */ +#include "nsDOMCID.h" +#include "nsIScriptNameSetRegistry.h" +#include "nsAppShellNameset.h" + #include "nsWidgetsCID.h" #include "nsIStreamObserver.h" @@ -42,6 +47,7 @@ /* Define Class IDs */ static NS_DEFINE_IID(kAppShellCID, NS_APPSHELL_CID); static NS_DEFINE_IID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); +static NS_DEFINE_IID(kCScriptNameSetRegistryCID, NS_SCRIPT_NAMESET_REGISTRY_CID); /* Define Interface IDs */ @@ -50,6 +56,7 @@ static NS_DEFINE_IID(kIEventQueueServiceIID, NS_IEVENTQUEUESERVICE_IID); static NS_DEFINE_IID(kIAppShellServiceIID, NS_IAPPSHELL_SERVICE_IID); static NS_DEFINE_IID(kIAppShellIID, NS_IAPPSHELL_IID); static NS_DEFINE_IID(kIWebShellWindowIID, NS_IWEBSHELL_WINDOW_IID); +static NS_DEFINE_IID(kIScriptNameSetRegistryIID, NS_ISCRIPTNAMESETREGISTRY_IID); @@ -120,15 +127,30 @@ nsAppShellService::Initialize(void) FCInitialize(); #endif // Create the Event Queue for the UI thread... - nsIEventQueueService* mEventQService; + nsIEventQueueService* eventQService; rv = nsServiceManager::GetService(kEventQueueServiceCID, kIEventQueueServiceIID, - (nsISupports **)&mEventQService); + (nsISupports **)&eventQService); if (NS_OK == rv) { // XXX: What if this fails? - rv = mEventQService->CreateThreadEventQueue(); + rv = eventQService->CreateThreadEventQueue(); } + // Register the nsAppShellNameSet with the global nameset registry... + nsIScriptNameSetRegistry *registry; + rv = nsServiceManager::GetService(kCScriptNameSetRegistryCID, + kIScriptNameSetRegistryIID, + (nsISupports **)®istry); + if (NS_FAILED(rv)) { + goto done; + } + + nsAppShellNameSet* nameSet; + nameSet = new nsAppShellNameSet(); + registry->AddExternalNameSet(nameSet); + /* FIX - do we need to release this service? When we do, it get deleted,and our name is lost. */ + + // Create the toplevel window list... rv = NS_NewISupportsArray(&mWindowList); if (NS_FAILED(rv)) { goto done; diff --git a/mozilla/xpfe/appshell/src/nsJSXPConnectFactory.cpp b/mozilla/xpfe/appshell/src/nsJSXPConnectFactory.cpp new file mode 100644 index 00000000000..f1d654990ea --- /dev/null +++ b/mozilla/xpfe/appshell/src/nsJSXPConnectFactory.cpp @@ -0,0 +1,304 @@ +/* -*- 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 "nsJSUtils.h" +#include "nscore.h" +#include "nsIScriptContext.h" +#include "nsIJSScriptObject.h" +#include "nsIScriptObjectOwner.h" +#include "nsIScriptGlobalObject.h" +#include "nsIPtr.h" +#include "nsString.h" +#include "nsIDOMXPConnectFactory.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(kIXPConnectFactoryIID, NS_IDOMXPCONNECTFACTORY_IID); +NS_DEF_PTR(nsIDOMXPConnectFactory); + + +/***********************************************************************/ +// +// XPConnectFactory Properties Getter +// +PR_STATIC_CALLBACK(JSBool) +GetXPConnectFactoryProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) +{ + nsIDOMXPConnectFactory *a = (nsIDOMXPConnectFactory*)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: + return nsJSUtils::nsCallJSScriptObjectGetProperty(a, cx, id, vp); + } + } + else { + return nsJSUtils::nsCallJSScriptObjectGetProperty(a, cx, id, vp); + } + + return PR_TRUE; +} + +/***********************************************************************/ +// +// XPConnectFactory Properties Setter +// +PR_STATIC_CALLBACK(JSBool) +SetXPConnectFactoryProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) +{ + nsIDOMXPConnectFactory *a = (nsIDOMXPConnectFactory*)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: + return nsJSUtils::nsCallJSScriptObjectSetProperty(a, cx, id, vp); + } + } + else { + return nsJSUtils::nsCallJSScriptObjectSetProperty(a, cx, id, vp); + } + + return PR_TRUE; +} + + +// +// XPConnectFactory finalizer +// +PR_STATIC_CALLBACK(void) +FinalizeXPConnectFactory(JSContext *cx, JSObject *obj) +{ + nsJSUtils::nsGenericFinalize(cx, obj); +} + + +// +// XPConnectFactory enumerate +// +PR_STATIC_CALLBACK(JSBool) +EnumerateXPConnectFactory(JSContext *cx, JSObject *obj) +{ + return nsJSUtils::nsGenericEnumerate(cx, obj); +} + + +// +// XPConnectFactory resolve +// +PR_STATIC_CALLBACK(JSBool) +ResolveXPConnectFactory(JSContext *cx, JSObject *obj, jsval id) +{ + return nsJSUtils::nsGenericResolve(cx, obj, id); +} + + +// +// Native method CreateInstance +// +PR_STATIC_CALLBACK(JSBool) +XPConnectFactoryCreateInstance(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMXPConnectFactory *nativeThis = (nsIDOMXPConnectFactory*)JS_GetPrivate(cx, obj); + JSBool rBool = JS_FALSE; + nsISupports* nativeRet; + 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) { + + nsJSUtils::nsConvertJSValToString(b0, cx, argv[0]); + + if (NS_OK != nativeThis->CreateInstance(b0, &nativeRet)) { + return JS_FALSE; + } + + nsJSUtils::nsConvertObjectToJSVal(nativeRet, cx, rval); + } + else { + JS_ReportError(cx, "Function createInstance requires 1 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + +/***********************************************************************/ +// +// class for XPConnectFactory +// +JSClass XPConnectFactoryClass = { + "XPConnectFactory", + JSCLASS_HAS_PRIVATE, + JS_PropertyStub, + JS_PropertyStub, + GetXPConnectFactoryProperty, + SetXPConnectFactoryProperty, + EnumerateXPConnectFactory, + ResolveXPConnectFactory, + JS_ConvertStub, + FinalizeXPConnectFactory +}; + + +// +// XPConnectFactory class properties +// +static JSPropertySpec XPConnectFactoryProperties[] = +{ + {0} +}; + + +// +// XPConnectFactory class methods +// +static JSFunctionSpec XPConnectFactoryMethods[] = +{ + {"createInstance", XPConnectFactoryCreateInstance, 1}, + {0} +}; + + +// +// XPConnectFactory constructor +// +PR_STATIC_CALLBACK(JSBool) +XPConnectFactory(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + return JS_FALSE; +} + + +// +// XPConnectFactory class initialization +// +extern "C" NS_APPSHELL nsresult NS_InitXPConnectFactoryClass(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, "XPConnectFactory", &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 + &XPConnectFactoryClass, // JSClass + XPConnectFactory, // JSNative ctor + 0, // ctor args + XPConnectFactoryProperties, // proto props + XPConnectFactoryMethods, // 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 XPConnectFactory JavaScript object +// +extern "C" NS_APPSHELL nsresult NS_NewScriptXPConnectFactory(nsIScriptContext *aContext, nsISupports *aSupports, nsISupports *aParent, void **aReturn) +{ + NS_PRECONDITION(nsnull != aContext && nsnull != aSupports && nsnull != aReturn, "null argument to NS_NewScriptXPConnectFactory"); + JSObject *proto; + JSObject *parent; + nsIScriptObjectOwner *owner; + JSContext *jscontext = (JSContext *)aContext->GetNativeContext(); + nsresult result = NS_OK; + nsIDOMXPConnectFactory *aXPConnectFactory; + + 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_InitXPConnectFactoryClass(aContext, (void **)&proto)) { + return NS_ERROR_FAILURE; + } + + result = aSupports->QueryInterface(kIXPConnectFactoryIID, (void **)&aXPConnectFactory); + if (NS_OK != result) { + return result; + } + + // create a js object for this class + *aReturn = JS_NewObject(jscontext, &XPConnectFactoryClass, proto, parent); + if (nsnull != *aReturn) { + // connect the native object to the js object + JS_SetPrivate(jscontext, (JSObject *)*aReturn, aXPConnectFactory); + } + else { + NS_RELEASE(aXPConnectFactory); + return NS_ERROR_FAILURE; + } + + return NS_OK; +} diff --git a/mozilla/xpfe/appshell/src/nsXPConnectFactory.cpp b/mozilla/xpfe/appshell/src/nsXPConnectFactory.cpp new file mode 100644 index 00000000000..b1457e31916 --- /dev/null +++ b/mozilla/xpfe/appshell/src/nsXPConnectFactory.cpp @@ -0,0 +1,193 @@ +/* -*- 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 "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/ + * + * 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. + * + * The Original Code is Mozilla Communicator client code. + * + * The Initial Developer of the Original Code is Netscape Communications + * Corporation. Portions created by Netscape are Copyright (C) 1998 + * Netscape Communications Corporation. All Rights Reserved. + */ + +#include "nsRepository.h" +#include "nsXPComFactory.h" +#include "nsIScriptObjectOwner.h" +#include "nsIScriptGlobalObject.h" +#include "nsIDOMXPConnectFactory.h" + +// Declare IIDs... +static NS_DEFINE_IID(kIDOMXPConnectFactoryIID, NS_IDOMXPCONNECTFACTORY_IID); +static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); +static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); + + +class XPConnectFactoryImpl : public nsIDOMXPConnectFactory, + public nsIScriptObjectOwner +{ +public: + XPConnectFactoryImpl(); + + NS_DECL_ISUPPORTS + + // nsIXPConnectFactory interface... + NS_IMETHOD CreateInstance(const nsString &progID, nsISupports**_retval); + + // nsIScriptObjectOwner interface... + NS_IMETHOD GetScriptObject(nsIScriptContext *aContext, void** aScriptObject); + NS_IMETHOD SetScriptObject(void *aScriptObject); + +protected: + virtual ~XPConnectFactoryImpl(); + +private: + void *mScriptObject; +}; + + +XPConnectFactoryImpl::XPConnectFactoryImpl() +{ + NS_INIT_REFCNT(); + + mScriptObject = nsnull; +} + +XPConnectFactoryImpl::~XPConnectFactoryImpl() +{ +} + +// ----- +// Implementation of nsISupports interface methods... +// ----- + +NS_IMPL_ADDREF(XPConnectFactoryImpl) +NS_IMPL_RELEASE(XPConnectFactoryImpl) + +NS_IMETHODIMP +XPConnectFactoryImpl::QueryInterface(REFNSIID aIID,void** aInstancePtr) +{ + if (aInstancePtr == NULL) { + return NS_ERROR_NULL_POINTER; + } + + // Always NULL result, in case of failure + *aInstancePtr = NULL; + + if ( aIID.Equals(kIDOMXPConnectFactoryIID) ) { + nsIDOMXPConnectFactory* tmp = this; + *aInstancePtr = tmp; + AddRef(); + return NS_OK; + } + else if ( aIID.Equals(kIScriptObjectOwnerIID)) { + nsIScriptObjectOwner* tmp = this; + *aInstancePtr = tmp; + AddRef(); + return NS_OK; + } + else if ( aIID.Equals(kISupportsIID) ) { + nsISupports* tmp = (nsIDOMXPConnectFactory*)this; + *aInstancePtr = tmp; + AddRef(); + return NS_OK; + } + + return NS_NOINTERFACE; +} + +// ----- +// Implementation of nsIXPConnectFactory interface methods... +// ----- + +NS_IMETHODIMP +XPConnectFactoryImpl::CreateInstance(const nsString &progID, nsISupports**_retval) +{ + nsresult rv; + nsCID cid; + char cidBuffer[48]; + + // Argument validation... + if (!_retval) { + rv = NS_ERROR_NULL_POINTER; + goto done; + } + + // Convert the unicode string into a char* + progID.ToCString(cidBuffer, sizeof(cidBuffer)); + + // Parse the CID string... + // + // XXX: The progID should *not* be treated as a raw CID... + // + if (cid.Parse(cidBuffer)) { + rv = nsRepository::CreateInstance(cid, nsnull, kISupportsIID, (void**)_retval); + if (NS_SUCCEEDED(rv)) { + } + } else { + // the progID does not represent a valid IID... + rv = NS_ERROR_INVALID_ARG; + } + +done: + return rv; +} + +// ----- +// Implementation of nsIScriptObjectOwner interface methods... +// ----- + +NS_IMETHODIMP +XPConnectFactoryImpl::GetScriptObject(nsIScriptContext *aContext, void **aScriptObject) +{ + nsresult rv = NS_OK; + + // Argument validation... + if (!aScriptObject) { + rv = NS_ERROR_NULL_POINTER; + } else { + if (!mScriptObject) { + nsIScriptGlobalObject *global = aContext->GetGlobalObject(); + rv = NS_NewScriptXPConnectFactory(aContext, (nsISupports *)(nsIDOMXPConnectFactory*)this, global, (void**)&mScriptObject); + NS_IF_RELEASE(global); + } + + *aScriptObject = mScriptObject; + } + + return rv; +} + +NS_IMETHODIMP +XPConnectFactoryImpl::SetScriptObject(void *aScriptObject) +{ + mScriptObject = aScriptObject; + return NS_OK; +} + + + +// ----- +// Entry point to create nsXPConnectFactory factory instances... +// ----- +NS_DEF_FACTORY(XPConnectFactory, XPConnectFactoryImpl) + +nsresult NS_NewXPConnectFactoryFactory(nsIFactory** aResult) +{ + nsresult rv = NS_OK; + nsIFactory* inst = new nsXPConnectFactoryFactory(); + if (nsnull == inst) { + rv = NS_ERROR_OUT_OF_MEMORY; + } + else { + NS_ADDREF(inst); + } + *aResult = inst; + return rv; +}