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 | // PyXPTStub - the stub for implementing interfaces.
|
---|
39 | //
|
---|
40 | // This code is part of the XPCOM extensions for Python.
|
---|
41 | //
|
---|
42 | // Written May 2000 by Mark Hammond.
|
---|
43 | //
|
---|
44 | // Based heavily on the Python COM support, which is
|
---|
45 | // (c) Mark Hammond and Greg Stein.
|
---|
46 | //
|
---|
47 | // (c) 2000, ActiveState corp.
|
---|
48 |
|
---|
49 | #include "PyXPCOM_std.h"
|
---|
50 | #include <nsIInterfaceInfoManager.h>
|
---|
51 |
|
---|
52 | void *PyXPCOM_XPTStub::ThisAsIID(const nsIID &iid)
|
---|
53 | {
|
---|
54 | if (iid.Equals(NS_GET_IID(nsISupports)))
|
---|
55 | return (nsISupports *)(nsXPTCStubBase *)this;
|
---|
56 | else if (iid.Equals(m_iid))
|
---|
57 | return (nsISupports *)(nsXPTCStubBase *)this;
|
---|
58 | else
|
---|
59 | return PyG_Base::ThisAsIID(iid);
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | NS_IMETHODIMP
|
---|
64 | PyXPCOM_XPTStub::GetInterfaceInfo(nsIInterfaceInfo** info)
|
---|
65 | {
|
---|
66 | NS_PRECONDITION(info, "NULL pointer");
|
---|
67 | if (info==nsnull)
|
---|
68 | return NS_ERROR_NULL_POINTER;
|
---|
69 | // Simply get the XPCOM runtime to provide this
|
---|
70 | // (but there must be some reason why they dont get it themselves!?
|
---|
71 | // Maybe because they dont know the IID?
|
---|
72 | nsCOMPtr<nsIInterfaceInfoManager> iim(do_GetService(
|
---|
73 | NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID));
|
---|
74 | NS_ABORT_IF_FALSE(iim != nsnull, "Cant get interface from IIM!");
|
---|
75 | if (iim==nsnull)
|
---|
76 | return NS_ERROR_FAILURE;
|
---|
77 |
|
---|
78 | return iim->GetInfoForIID( &m_iid, info);
|
---|
79 | }
|
---|
80 |
|
---|
81 | // call this method and return result
|
---|
82 | NS_IMETHODIMP
|
---|
83 | PyXPCOM_XPTStub::CallMethod(PRUint16 methodIndex,
|
---|
84 | const nsXPTMethodInfo* info,
|
---|
85 | nsXPTCMiniVariant* params)
|
---|
86 | {
|
---|
87 | nsresult rc = NS_ERROR_FAILURE;
|
---|
88 | NS_PRECONDITION(info, "NULL methodinfo pointer");
|
---|
89 | NS_PRECONDITION(params, "NULL variant pointer");
|
---|
90 | CEnterLeavePython _celp;
|
---|
91 | PyObject *obParams = NULL;
|
---|
92 | PyObject *result = NULL;
|
---|
93 | PyObject *obThisObject = NULL;
|
---|
94 | PyObject *obMI = PyObject_FromXPTMethodDescriptor(info);
|
---|
95 | PyXPCOM_GatewayVariantHelper arg_helper(this, methodIndex, info, params);
|
---|
96 | if (obMI==NULL)
|
---|
97 | goto done;
|
---|
98 | // base object is passed raw.
|
---|
99 | obThisObject = PyObject_FromNSInterface((nsXPTCStubBase *)this,
|
---|
100 | m_iid, PR_FALSE);
|
---|
101 | obParams = arg_helper.MakePyArgs();
|
---|
102 | if (obParams==NULL)
|
---|
103 | goto done;
|
---|
104 | result = PyObject_CallMethod(m_pPyObject,
|
---|
105 | (char*)"_CallMethod_",
|
---|
106 | (char*)"OiOO",
|
---|
107 | obThisObject,
|
---|
108 | (int)methodIndex,
|
---|
109 | obMI,
|
---|
110 | obParams);
|
---|
111 | if (result!=NULL) {
|
---|
112 | rc = arg_helper.ProcessPythonResult(result);
|
---|
113 | // Use an xor to check failure && pyerr, or !failure && !pyerr.
|
---|
114 | NS_ABORT_IF_FALSE( ((NS_FAILED(rc)!=0)^(PyErr_Occurred()!=0)) == 0, "We must have failure with a Python error, or success without a Python error.");
|
---|
115 | }
|
---|
116 | done:
|
---|
117 | if (PyErr_Occurred()) {
|
---|
118 | // The error handling - fairly involved, but worth it as
|
---|
119 | // good error reporting is critical for users to know WTF
|
---|
120 | // is going on - especially with TypeErrors etc in their
|
---|
121 | // return values (ie, after the Python code has successfully
|
---|
122 | // exited, but we encountered errors unpacking the
|
---|
123 | // result values for the COM caller - there is literally no
|
---|
124 | // way to catch these exceptions from Python code, as their
|
---|
125 | // is no Python function on the call-stack)
|
---|
126 |
|
---|
127 | // First line of attack in an error is to call-back on the policy.
|
---|
128 | // If the callback of the error handler succeeds and returns an
|
---|
129 | // integer (for the nsresult), we take no further action.
|
---|
130 |
|
---|
131 | // If this callback fails, we log _2_ exceptions - the error handler
|
---|
132 | // error, and the original error.
|
---|
133 |
|
---|
134 | PRBool bProcessMainError = PR_TRUE; // set to false if our exception handler does its thing!
|
---|
135 | PyObject *exc_typ, *exc_val, *exc_tb;
|
---|
136 | PyErr_Fetch(&exc_typ, &exc_val, &exc_tb);
|
---|
137 | PyErr_NormalizeException( &exc_typ, &exc_val, &exc_tb);
|
---|
138 |
|
---|
139 | PyObject *err_result = PyObject_CallMethod(m_pPyObject,
|
---|
140 | (char*)"_CallMethodException_",
|
---|
141 | (char*)"OiOO(OOO)",
|
---|
142 | obThisObject,
|
---|
143 | (int)methodIndex,
|
---|
144 | obMI,
|
---|
145 | obParams,
|
---|
146 | exc_typ ? exc_typ : Py_None, // should never be NULL, but defensive programming...
|
---|
147 | exc_val ? exc_val : Py_None, // may well be NULL.
|
---|
148 | exc_tb ? exc_tb : Py_None); // may well be NULL.
|
---|
149 | if (err_result == NULL) {
|
---|
150 | PyXPCOM_LogError("The exception handler _CallMethodException_ failed!\n");
|
---|
151 | } else if (err_result == Py_None) {
|
---|
152 | // The exception handler has chosen not to do anything with
|
---|
153 | // this error, so we still need to print it!
|
---|
154 | ;
|
---|
155 | } else if (PyInt_Check(err_result)) {
|
---|
156 | // The exception handler has given us the nresult.
|
---|
157 | rc = PyInt_AsLong(err_result);
|
---|
158 | bProcessMainError = PR_FALSE;
|
---|
159 | } else {
|
---|
160 | // The exception handler succeeded, but returned other than
|
---|
161 | // int or None.
|
---|
162 | PyXPCOM_LogError("The _CallMethodException_ handler returned object of type '%s' - None or an integer expected\n", PyXPCOM_ObTypeName(err_result));
|
---|
163 | }
|
---|
164 | Py_XDECREF(err_result);
|
---|
165 | PyErr_Restore(exc_typ, exc_val, exc_tb);
|
---|
166 | if (bProcessMainError) {
|
---|
167 | PyXPCOM_LogError("The function '%s' failed\n", info->GetName());
|
---|
168 | rc = PyXPCOM_SetCOMErrorFromPyException();
|
---|
169 | }
|
---|
170 | // else everything is already setup,
|
---|
171 | // just clear the Python error state.
|
---|
172 | PyErr_Clear();
|
---|
173 | }
|
---|
174 |
|
---|
175 | Py_XDECREF(obMI);
|
---|
176 | Py_XDECREF(obParams);
|
---|
177 | Py_XDECREF(obThisObject);
|
---|
178 | Py_XDECREF(result);
|
---|
179 | return rc;
|
---|
180 | }
|
---|