Back to index

lightning-sunbird  0.9+nobinonly
PyGModule.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Python XPCOM language bindings.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * ActiveState Tool Corp.
00018  * Portions created by the Initial Developer are Copyright (C) 2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Mark Hammond <mhammond@skippinet.com.au> (original author)
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 //
00039 // This code is part of the XPCOM extensions for Python.
00040 //
00041 // Written May 2000 by Mark Hammond.
00042 //
00043 // Based heavily on the Python COM support, which is
00044 // (c) Mark Hammond and Greg Stein.
00045 //
00046 // (c) 2000, ActiveState corp.
00047 
00048 // Unfortunately, we can not use an XPConnect object for
00049 // the nsiModule and nsiComponentLoader interfaces.
00050 //  As XPCOM shuts down, it shuts down the interface manager before 
00051 // it releases all the modules.  This is a bit of a problem for 
00052 // us, as it means we can't get runtime info on the interface at shutdown time.
00053 
00054 #include "PyXPCOM_std.h"
00055 #include <nsIModule.h>
00056 #include <nsIComponentLoader.h>
00057 
00058 class PyG_nsIModule : public PyG_Base, public nsIModule
00059 {
00060 public:
00061        PyG_nsIModule(PyObject *instance) : PyG_Base(instance, NS_GET_IID(nsIModule)) {;}
00062        PYGATEWAY_BASE_SUPPORT(nsIModule, PyG_Base);
00063 
00064        NS_DECL_NSIMODULE
00065 };
00066 
00067 PyG_Base *MakePyG_nsIModule(PyObject *instance)
00068 {
00069        return new PyG_nsIModule(instance);
00070 }
00071 
00072 
00073 // Create a factory object for creating instances of aClass.
00074 NS_IMETHODIMP
00075 PyG_nsIModule::GetClassObject(nsIComponentManager *aCompMgr,
00076                                 const nsCID& aClass,
00077                                 const nsIID& aIID,
00078                                 void** r_classObj)
00079 {
00080        NS_PRECONDITION(r_classObj, "null pointer");
00081        *r_classObj = nsnull;
00082        CEnterLeavePython _celp;
00083        PyObject *cm = Py_nsISupports::PyObjectFromInterface(aCompMgr, NS_GET_IID(nsIComponentManager), PR_TRUE);
00084        PyObject *iid = Py_nsIID::PyObjectFromIID(aIID);
00085        PyObject *clsid = Py_nsIID::PyObjectFromIID(aClass);
00086        const char *methodName = "getClassObject";
00087        PyObject *ret = NULL;
00088        nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "OOO", cm, clsid, iid);
00089        Py_XDECREF(cm);
00090        Py_XDECREF(iid);
00091        Py_XDECREF(clsid);
00092        if (NS_SUCCEEDED(nr)) {
00093               nr = Py_nsISupports::InterfaceFromPyObject(ret, aIID, (nsISupports **)r_classObj, PR_FALSE);
00094               if (PyErr_Occurred())
00095                      nr = HandleNativeGatewayError(methodName);
00096        }
00097        if (NS_FAILED(nr)) {
00098               NS_ABORT_IF_FALSE(*r_classObj==NULL, "returning error result with an interface - probable leak!");
00099        }
00100        Py_XDECREF(ret);
00101        return nr;
00102 }
00103 
00104 NS_IMETHODIMP
00105 PyG_nsIModule::RegisterSelf(nsIComponentManager *aCompMgr,
00106                               nsIFile* aPath,
00107                               const char* registryLocation,
00108                               const char* componentType)
00109 {
00110        NS_PRECONDITION(aCompMgr, "null pointer");
00111        NS_PRECONDITION(aPath, "null pointer");
00112        CEnterLeavePython _celp;
00113        PyObject *cm = Py_nsISupports::PyObjectFromInterface(aCompMgr, NS_GET_IID(nsIComponentManager), PR_TRUE);
00114        PyObject *path = Py_nsISupports::PyObjectFromInterface(aPath, NS_GET_IID(nsIFile), PR_TRUE);
00115        const char *methodName = "registerSelf";
00116        nsresult nr = InvokeNativeViaPolicy(methodName, NULL, "OOzz", cm, path, registryLocation, componentType);
00117        Py_XDECREF(cm);
00118        Py_XDECREF(path);
00119        return nr;
00120 }
00121 
00122 NS_IMETHODIMP
00123 PyG_nsIModule::UnregisterSelf(nsIComponentManager* aCompMgr,
00124                             nsIFile* aPath,
00125                             const char* registryLocation)
00126 {
00127        NS_PRECONDITION(aCompMgr, "null pointer");
00128        NS_PRECONDITION(aPath, "null pointer");
00129        CEnterLeavePython _celp;
00130        PyObject *cm = Py_nsISupports::PyObjectFromInterface(aCompMgr, NS_GET_IID(nsIComponentManager), PR_TRUE);
00131        PyObject *path = Py_nsISupports::PyObjectFromInterface(aPath, NS_GET_IID(nsIFile), PR_TRUE);
00132        const char *methodName = "unregisterSelf";
00133        nsresult nr = InvokeNativeViaPolicy(methodName, NULL, "OOz", cm, path, registryLocation);
00134        Py_XDECREF(cm);
00135        Py_XDECREF(path);
00136        return nr;
00137 }
00138 
00139 NS_IMETHODIMP
00140 PyG_nsIModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
00141 {
00142        NS_PRECONDITION(aCompMgr, "null pointer");
00143        NS_PRECONDITION(okToUnload, "null pointer");
00144        CEnterLeavePython _celp;
00145        // we are shutting down - don't ask for a nice wrapped object.
00146        PyObject *cm = Py_nsISupports::PyObjectFromInterface(aCompMgr, NS_GET_IID(nsIComponentManager), PR_TRUE, PR_FALSE);
00147        const char *methodName = "canUnload";
00148        PyObject *ret = NULL;
00149        nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "O", cm);
00150        Py_XDECREF(cm);
00151        if (NS_SUCCEEDED(nr)) {
00152               *okToUnload = PyInt_AsLong(ret);
00153               if (PyErr_Occurred())
00154                      nr = HandleNativeGatewayError(methodName);
00155        }
00156        Py_XDECREF(ret);
00157        return nr;
00158 }
00159 
00161 
00162 class PyG_nsIComponentLoader : public PyG_Base, public nsIComponentLoader
00163 {
00164 public:
00165        PyG_nsIComponentLoader(PyObject *instance) : PyG_Base(instance, NS_GET_IID(nsIComponentLoader)) {;}
00166        PYGATEWAY_BASE_SUPPORT(nsIComponentLoader, PyG_Base);
00167 
00168        NS_DECL_NSICOMPONENTLOADER
00169 };
00170 
00171 PyG_Base *MakePyG_nsIComponentLoader(PyObject *instance)
00172 {
00173        return new PyG_nsIComponentLoader(instance);
00174 }
00175 
00176 /* nsIFactory getFactory (in nsIIDRef aCID, in string aLocation, in string aType); */
00177 NS_IMETHODIMP PyG_nsIComponentLoader::GetFactory(const nsIID & aCID, const char *aLocation, const char *aType, nsIFactory **_retval)
00178 {
00179        CEnterLeavePython _celp;
00180        const char *methodName = "getFactory";
00181        PyObject *iid = Py_nsIID::PyObjectFromIID(aCID);
00182        PyObject *ret = NULL;
00183        nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "Ozz", 
00184                             iid,
00185                             aLocation,
00186                             aType);
00187        Py_XDECREF(iid);
00188        if (NS_SUCCEEDED(nr)) {
00189               Py_nsISupports::InterfaceFromPyObject(ret, NS_GET_IID(nsIFactory), (nsISupports **)_retval, PR_FALSE);
00190               if (PyErr_Occurred())
00191                      nr = HandleNativeGatewayError(methodName);
00192        }
00193        Py_XDECREF(ret);
00194        return nr;
00195 }
00196 
00197 /* void init (in nsIComponentManager aCompMgr, in nsISupports aRegistry); */
00198 NS_IMETHODIMP PyG_nsIComponentLoader::Init(nsIComponentManager *aCompMgr, nsISupports *aRegistry)
00199 {
00200        CEnterLeavePython _celp;
00201        const char *methodName = "init";
00202        PyObject *c = Py_nsISupports::PyObjectFromInterface(aCompMgr, NS_GET_IID(nsIComponentManager), PR_TRUE);
00203        PyObject *r = Py_nsISupports::PyObjectFromInterface(aRegistry, NS_GET_IID(nsISupports), PR_TRUE);
00204        nsresult nr = InvokeNativeViaPolicy(methodName, NULL, "OO", c, r);
00205        Py_XDECREF(c);
00206        Py_XDECREF(r);
00207        return nr;
00208 }
00209 
00210 /* void onRegister (in nsIIDRef aCID, in string aType, in string aClassName, in string aContractID, in string aLocation, in boolean aReplace, in boolean aPersist); */
00211 NS_IMETHODIMP PyG_nsIComponentLoader::OnRegister(const nsIID & aCID, const char *aType, const char *aClassName, const char *aContractID, const char *aLocation, PRBool aReplace, PRBool aPersist)
00212 {
00213        CEnterLeavePython _celp;
00214        const char *methodName = "onRegister";
00215        PyObject *iid = Py_nsIID::PyObjectFromIID(aCID);
00216        nsresult nr = InvokeNativeViaPolicy(methodName, NULL, "Ossssii", 
00217                             iid,
00218                             aType,
00219                             aClassName, 
00220                             aContractID, 
00221                             aLocation, 
00222                             aReplace,
00223                             aPersist);
00224        Py_XDECREF(iid);
00225        return nr;
00226 }
00227 
00228 /* void autoRegisterComponents (in long aWhen, in nsIFile aDirectory); */
00229 NS_IMETHODIMP PyG_nsIComponentLoader::AutoRegisterComponents(PRInt32 aWhen, nsIFile *aDirectory)
00230 {
00231        CEnterLeavePython _celp;
00232        const char *methodName = "autoRegisterComponents";
00233        PyObject *c = Py_nsISupports::PyObjectFromInterface(aDirectory, NS_GET_IID(nsIFile), PR_TRUE);
00234        nsresult nr = InvokeNativeViaPolicy(methodName, NULL, "iO", aWhen, c);
00235        Py_XDECREF(c);
00236        return nr;
00237 }
00238 
00239 /* boolean autoRegisterComponent (in long aWhen, in nsIFile aComponent); */
00240 NS_IMETHODIMP PyG_nsIComponentLoader::AutoRegisterComponent(PRInt32 aWhen, nsIFile *aComponent, PRBool *_retval)
00241 {
00242        CEnterLeavePython _celp;
00243        const char *methodName = "autoRegisterComponent";
00244        PyObject *ret = NULL;
00245        PyObject *c = Py_nsISupports::PyObjectFromInterface(aComponent, NS_GET_IID(nsIFile), PR_TRUE);
00246        nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "iO", aWhen, c);
00247        Py_XDECREF(c);
00248        if (NS_SUCCEEDED(nr)) {
00249               *_retval = PyInt_AsLong(ret);
00250               if (PyErr_Occurred())
00251                      nr = HandleNativeGatewayError(methodName);
00252        }
00253        Py_XDECREF(ret);
00254        return nr;
00255 }
00256 
00257 /* boolean autoUnregisterComponent (in long aWhen, in nsIFile aComponent); */
00258 NS_IMETHODIMP PyG_nsIComponentLoader::AutoUnregisterComponent(PRInt32 aWhen, nsIFile *aComponent, PRBool *_retval)
00259 {
00260        CEnterLeavePython _celp;
00261        const char *methodName = "autoUnregisterComponent";
00262        PyObject *ret = NULL;
00263        PyObject *c = Py_nsISupports::PyObjectFromInterface(aComponent, NS_GET_IID(nsIFile), PR_TRUE);
00264        nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "iO", aWhen, c);
00265        Py_XDECREF(c);
00266        if (NS_SUCCEEDED(nr)) {
00267               *_retval = PyInt_AsLong(ret);
00268               if (PyErr_Occurred())
00269                      nr = HandleNativeGatewayError(methodName);
00270        }
00271        Py_XDECREF(ret);
00272        return nr;
00273 }
00274 
00275 /* boolean registerDeferredComponents (in long aWhen); */
00276 NS_IMETHODIMP PyG_nsIComponentLoader::RegisterDeferredComponents(PRInt32 aWhen, PRBool *_retval)
00277 {
00278        CEnterLeavePython _celp;
00279        const char *methodName = "registerDeferredComponents";
00280        PyObject *ret = NULL;
00281        nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "i", aWhen);
00282        if (NS_SUCCEEDED(nr)) {
00283               *_retval = PyInt_AsLong(ret);
00284               if (PyErr_Occurred())
00285                      nr = HandleNativeGatewayError(methodName);
00286        }
00287        Py_XDECREF(ret);
00288        return nr;
00289 }
00290 
00291 /* void unloadAll (in long aWhen); */
00292 NS_IMETHODIMP PyG_nsIComponentLoader::UnloadAll(PRInt32 aWhen)
00293 {
00294        CEnterLeavePython _celp;
00295        const char *methodName = "unloadAll";
00296        return InvokeNativeViaPolicy(methodName, NULL, "i", aWhen);
00297 }