VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/python/src/PyGModule.cpp

Last change on this file was 103176, checked in by vboxsync, 3 months ago

libs/xpcom/python: Some cleanup, bugref:3409

  • Property svn:eol-style set to native
File size: 10.4 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is the Python XPCOM language bindings.
15 *
16 * The Initial Developer of the Original Code is
17 * ActiveState Tool Corp.
18 * Portions created by the Initial Developer are Copyright (C) 2000
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Mark Hammond <mhammond@skippinet.com.au> (original author)
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38//
39// This code is part of the XPCOM extensions for Python.
40//
41// Written May 2000 by Mark Hammond.
42//
43// Based heavily on the Python COM support, which is
44// (c) Mark Hammond and Greg Stein.
45//
46// (c) 2000, ActiveState corp.
47
48// Unfortunately, we can not use an XPConnect object for
49// the nsiModule and nsiComponentLoader interfaces.
50// As XPCOM shuts down, it shuts down the interface manager before
51// it releases all the modules. This is a bit of a problem for
52// us, as it means we can't get runtime info on the interface at shutdown time.
53
54#include "PyXPCOM_std.h"
55#include <nsIModule.h>
56#include <nsIComponentLoader.h>
57
58class PyG_nsIModule : public PyG_Base, public nsIModule
59{
60public:
61 PyG_nsIModule(PyObject *instance) : PyG_Base(instance, NS_GET_IID(nsIModule)) {;}
62 PYGATEWAY_BASE_SUPPORT(nsIModule, PyG_Base);
63
64 NS_DECL_NSIMODULE
65};
66
67DECLHIDDEN(PyG_Base *) MakePyG_nsIModule(PyObject *instance)
68{
69 return new PyG_nsIModule(instance);
70}
71
72
73// Create a factory object for creating instances of aClass.
74NS_IMETHODIMP
75PyG_nsIModule::GetClassObject(nsIComponentManager *aCompMgr,
76 const nsCID& aClass,
77 const nsIID& aIID,
78 void** r_classObj)
79{
80 NS_PRECONDITION(r_classObj, "null pointer");
81 *r_classObj = nsnull;
82 CEnterLeavePython _celp;
83 PyObject *cm = PyObject_FromNSInterface(aCompMgr, NS_GET_IID(nsIComponentManager));
84 PyObject *iid = Py_nsIID::PyObjectFromIID(aIID);
85 PyObject *clsid = Py_nsIID::PyObjectFromIID(aClass);
86 const char *methodName = "getClassObject";
87 PyObject *ret = NULL;
88 nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "OOO", cm, clsid, iid);
89 Py_XDECREF(cm);
90 Py_XDECREF(iid);
91 Py_XDECREF(clsid);
92 if (NS_SUCCEEDED(nr)) {
93 nr = Py_nsISupports::InterfaceFromPyObject(ret, aIID, (nsISupports **)r_classObj, PR_FALSE);
94 if (PyErr_Occurred())
95 nr = HandleNativeGatewayError(methodName);
96 }
97 if (NS_FAILED(nr)) {
98 NS_ABORT_IF_FALSE(*r_classObj==NULL, "returning error result with an interface - probable leak!");
99 }
100 Py_XDECREF(ret);
101 return nr;
102}
103
104NS_IMETHODIMP
105PyG_nsIModule::RegisterSelf(nsIComponentManager *aCompMgr,
106 nsIFile* aPath,
107 const char* registryLocation,
108 const char* componentType)
109{
110 NS_PRECONDITION(aCompMgr, "null pointer");
111 NS_PRECONDITION(aPath, "null pointer");
112 CEnterLeavePython _celp;
113 PyObject *cm = PyObject_FromNSInterface(aCompMgr, NS_GET_IID(nsIComponentManager));
114 PyObject *path = PyObject_FromNSInterface(aPath, NS_GET_IID(nsIFile));
115 const char *methodName = "registerSelf";
116 nsresult nr = InvokeNativeViaPolicy(methodName, NULL, "OOzz", cm, path, registryLocation, componentType);
117 Py_XDECREF(cm);
118 Py_XDECREF(path);
119 return nr;
120}
121
122NS_IMETHODIMP
123PyG_nsIModule::UnregisterSelf(nsIComponentManager* aCompMgr,
124 nsIFile* aPath,
125 const char* registryLocation)
126{
127 NS_PRECONDITION(aCompMgr, "null pointer");
128 NS_PRECONDITION(aPath, "null pointer");
129 CEnterLeavePython _celp;
130 PyObject *cm = PyObject_FromNSInterface(aCompMgr, NS_GET_IID(nsIComponentManager));
131 PyObject *path = PyObject_FromNSInterface(aPath, NS_GET_IID(nsIFile));
132 const char *methodName = "unregisterSelf";
133 nsresult nr = InvokeNativeViaPolicy(methodName, NULL, "OOz", cm, path, registryLocation);
134 Py_XDECREF(cm);
135 Py_XDECREF(path);
136 return nr;
137}
138
139NS_IMETHODIMP
140PyG_nsIModule::CanUnload(nsIComponentManager *aCompMgr, PRBool *okToUnload)
141{
142 NS_PRECONDITION(aCompMgr, "null pointer");
143 NS_PRECONDITION(okToUnload, "null pointer");
144 CEnterLeavePython _celp;
145 // we are shutting down - don't ask for a nice wrapped object.
146 PyObject *cm = PyObject_FromNSInterface(aCompMgr, NS_GET_IID(nsIComponentManager), PR_FALSE);
147 const char *methodName = "canUnload";
148 PyObject *ret = NULL;
149 nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "O", cm);
150 Py_XDECREF(cm);
151 if (NS_SUCCEEDED(nr)) {
152 *okToUnload = PyInt_AsLong(ret);
153 if (PyErr_Occurred())
154 nr = HandleNativeGatewayError(methodName);
155 }
156 Py_XDECREF(ret);
157 return nr;
158}
159
160///////////////////////////////////////////////////////////////////////////////////
161
162class PyG_nsIComponentLoader : public PyG_Base, public nsIComponentLoader
163{
164public:
165 PyG_nsIComponentLoader(PyObject *instance) : PyG_Base(instance, NS_GET_IID(nsIComponentLoader)) {;}
166 PYGATEWAY_BASE_SUPPORT(nsIComponentLoader, PyG_Base);
167
168 NS_DECL_NSICOMPONENTLOADER
169};
170
171DECLHIDDEN(PyG_Base *) MakePyG_nsIComponentLoader(PyObject *instance)
172{
173 return new PyG_nsIComponentLoader(instance);
174}
175
176/* nsIFactory getFactory (in nsIIDRef aCID, in string aLocation, in string aType); */
177NS_IMETHODIMP PyG_nsIComponentLoader::GetFactory(const nsIID & aCID, const char *aLocation, const char *aType, nsIFactory **_retval)
178{
179 CEnterLeavePython _celp;
180 const char *methodName = "getFactory";
181 PyObject *iid = Py_nsIID::PyObjectFromIID(aCID);
182 PyObject *ret = NULL;
183 nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "Ozz",
184 iid,
185 aLocation,
186 aType);
187 Py_XDECREF(iid);
188 if (NS_SUCCEEDED(nr)) {
189 Py_nsISupports::InterfaceFromPyObject(ret, NS_GET_IID(nsIFactory), (nsISupports **)_retval, PR_FALSE);
190 if (PyErr_Occurred())
191 nr = HandleNativeGatewayError(methodName);
192 }
193 Py_XDECREF(ret);
194 return nr;
195}
196
197/* void init (in nsIComponentManager aCompMgr, in nsISupports aRegistry); */
198NS_IMETHODIMP PyG_nsIComponentLoader::Init(nsIComponentManager *aCompMgr, nsISupports *aRegistry)
199{
200 CEnterLeavePython _celp;
201 const char *methodName = "init";
202 PyObject *c = PyObject_FromNSInterface(aCompMgr, NS_GET_IID(nsIComponentManager));
203 PyObject *r = PyObject_FromNSInterface(aRegistry, NS_GET_IID(nsISupports));
204 nsresult nr = InvokeNativeViaPolicy(methodName, NULL, "OO", c, r);
205 Py_XDECREF(c);
206 Py_XDECREF(r);
207 return nr;
208}
209
210/* void onRegister (in nsIIDRef aCID, in string aType, in string aClassName, in string aContractID, in string aLocation, in boolean aReplace, in boolean aPersist); */
211NS_IMETHODIMP PyG_nsIComponentLoader::OnRegister(const nsIID & aCID, const char *aType, const char *aClassName, const char *aContractID, const char *aLocation, PRBool aReplace, PRBool aPersist)
212{
213 CEnterLeavePython _celp;
214 const char *methodName = "onRegister";
215 PyObject *iid = Py_nsIID::PyObjectFromIID(aCID);
216 nsresult nr = InvokeNativeViaPolicy(methodName, NULL, "Ossssii",
217 iid,
218 aType,
219 aClassName,
220 aContractID,
221 aLocation,
222 aReplace,
223 aPersist);
224 Py_XDECREF(iid);
225 return nr;
226}
227
228/* void autoRegisterComponents (in long aWhen, in nsIFile aDirectory); */
229NS_IMETHODIMP PyG_nsIComponentLoader::AutoRegisterComponents(PRInt32 aWhen, nsIFile *aDirectory)
230{
231 CEnterLeavePython _celp;
232 const char *methodName = "autoRegisterComponents";
233 PyObject *c = PyObject_FromNSInterface(aDirectory, NS_GET_IID(nsIFile));
234 nsresult nr = InvokeNativeViaPolicy(methodName, NULL, "iO", aWhen, c);
235 Py_XDECREF(c);
236 return nr;
237}
238
239/* boolean autoRegisterComponent (in long aWhen, in nsIFile aComponent); */
240NS_IMETHODIMP PyG_nsIComponentLoader::AutoRegisterComponent(PRInt32 aWhen, nsIFile *aComponent, PRBool *_retval)
241{
242 CEnterLeavePython _celp;
243 const char *methodName = "autoRegisterComponent";
244 PyObject *ret = NULL;
245 PyObject *c = PyObject_FromNSInterface(aComponent, NS_GET_IID(nsIFile));
246 nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "iO", aWhen, c);
247 Py_XDECREF(c);
248 if (NS_SUCCEEDED(nr)) {
249 *_retval = PyInt_AsLong(ret);
250 if (PyErr_Occurred())
251 nr = HandleNativeGatewayError(methodName);
252 }
253 Py_XDECREF(ret);
254 return nr;
255}
256
257/* boolean autoUnregisterComponent (in long aWhen, in nsIFile aComponent); */
258NS_IMETHODIMP PyG_nsIComponentLoader::AutoUnregisterComponent(PRInt32 aWhen, nsIFile *aComponent, PRBool *_retval)
259{
260 CEnterLeavePython _celp;
261 const char *methodName = "autoUnregisterComponent";
262 PyObject *ret = NULL;
263 PyObject *c = PyObject_FromNSInterface(aComponent, NS_GET_IID(nsIFile));
264 nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "iO", aWhen, c);
265 Py_XDECREF(c);
266 if (NS_SUCCEEDED(nr)) {
267 *_retval = PyInt_AsLong(ret);
268 if (PyErr_Occurred())
269 nr = HandleNativeGatewayError(methodName);
270 }
271 Py_XDECREF(ret);
272 return nr;
273}
274
275/* boolean registerDeferredComponents (in long aWhen); */
276NS_IMETHODIMP PyG_nsIComponentLoader::RegisterDeferredComponents(PRInt32 aWhen, PRBool *_retval)
277{
278 CEnterLeavePython _celp;
279 const char *methodName = "registerDeferredComponents";
280 PyObject *ret = NULL;
281 nsresult nr = InvokeNativeViaPolicy(methodName, &ret, "i", aWhen);
282 if (NS_SUCCEEDED(nr)) {
283 *_retval = PyInt_AsLong(ret);
284 if (PyErr_Occurred())
285 nr = HandleNativeGatewayError(methodName);
286 }
287 Py_XDECREF(ret);
288 return nr;
289}
290
291/* void unloadAll (in long aWhen); */
292NS_IMETHODIMP PyG_nsIComponentLoader::UnloadAll(PRInt32 aWhen)
293{
294 CEnterLeavePython _celp;
295 const char *methodName = "unloadAll";
296 return InvokeNativeViaPolicy(methodName, NULL, "i", aWhen);
297}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use