1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_cppu.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "Proxy.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "sal/alloca.h"
34*cdf0e10cSrcweir #include "uno/dispatcher.h"
35*cdf0e10cSrcweir #include "typelib/typedescription.hxx"
36*cdf0e10cSrcweir #include "cppu/EnvDcp.hxx"
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir //#define LOG_LIFECYCLE_Proxy
40*cdf0e10cSrcweir #ifdef LOG_LIFECYCLE_Proxy
41*cdf0e10cSrcweir #  include <iostream>
42*cdf0e10cSrcweir #  define LOG_LIFECYCLE_Proxy_emit(x) x
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #else
45*cdf0e10cSrcweir #  define LOG_LIFECYCLE_Proxy_emit(x)
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #endif
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir using namespace com::sun::star;
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir static bool relatesToInterface(typelib_TypeDescription * pTypeDescr)
54*cdf0e10cSrcweir 	SAL_THROW( () )
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir 	switch (pTypeDescr->eTypeClass)
57*cdf0e10cSrcweir 	{
58*cdf0e10cSrcweir //  	case typelib_TypeClass_TYPEDEF:
59*cdf0e10cSrcweir 	case typelib_TypeClass_SEQUENCE:
60*cdf0e10cSrcweir 	{
61*cdf0e10cSrcweir 		switch (((typelib_IndirectTypeDescription *)pTypeDescr)->pType->eTypeClass)
62*cdf0e10cSrcweir 		{
63*cdf0e10cSrcweir 		case typelib_TypeClass_INTERFACE:
64*cdf0e10cSrcweir 		case typelib_TypeClass_UNION: // might relate to interface
65*cdf0e10cSrcweir 		case typelib_TypeClass_ANY: // might relate to interface
66*cdf0e10cSrcweir 			return true;
67*cdf0e10cSrcweir 		case typelib_TypeClass_SEQUENCE:
68*cdf0e10cSrcweir 		case typelib_TypeClass_STRUCT:
69*cdf0e10cSrcweir 		case typelib_TypeClass_EXCEPTION:
70*cdf0e10cSrcweir 		{
71*cdf0e10cSrcweir 			typelib_TypeDescription * pTD = 0;
72*cdf0e10cSrcweir 			TYPELIB_DANGER_GET( &pTD, ((typelib_IndirectTypeDescription *)pTypeDescr)->pType );
73*cdf0e10cSrcweir 			bool bRel = relatesToInterface( pTD );
74*cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pTD );
75*cdf0e10cSrcweir 			return bRel;
76*cdf0e10cSrcweir 		}
77*cdf0e10cSrcweir         default:
78*cdf0e10cSrcweir             ;
79*cdf0e10cSrcweir 		}
80*cdf0e10cSrcweir 		return false;
81*cdf0e10cSrcweir 	}
82*cdf0e10cSrcweir 	case typelib_TypeClass_STRUCT:
83*cdf0e10cSrcweir 	case typelib_TypeClass_EXCEPTION:
84*cdf0e10cSrcweir 	{
85*cdf0e10cSrcweir 		// ...optimized... to avoid getDescription() calls!
86*cdf0e10cSrcweir 		typelib_CompoundTypeDescription * pComp    = (typelib_CompoundTypeDescription *)pTypeDescr;
87*cdf0e10cSrcweir 		typelib_TypeDescriptionReference ** pTypes = pComp->ppTypeRefs;
88*cdf0e10cSrcweir 		for ( sal_Int32 nPos = pComp->nMembers; nPos--; )
89*cdf0e10cSrcweir 		{
90*cdf0e10cSrcweir 			switch (pTypes[nPos]->eTypeClass)
91*cdf0e10cSrcweir 			{
92*cdf0e10cSrcweir 			case typelib_TypeClass_INTERFACE:
93*cdf0e10cSrcweir 			case typelib_TypeClass_UNION: // might relate to interface
94*cdf0e10cSrcweir 			case typelib_TypeClass_ANY: // might relate to interface
95*cdf0e10cSrcweir 				return true;
96*cdf0e10cSrcweir //  			case typelib_TypeClass_TYPEDEF:
97*cdf0e10cSrcweir 			case typelib_TypeClass_SEQUENCE:
98*cdf0e10cSrcweir 			case typelib_TypeClass_STRUCT:
99*cdf0e10cSrcweir 			case typelib_TypeClass_EXCEPTION:
100*cdf0e10cSrcweir 			{
101*cdf0e10cSrcweir 				typelib_TypeDescription * pTD = 0;
102*cdf0e10cSrcweir 				TYPELIB_DANGER_GET( &pTD, pTypes[nPos] );
103*cdf0e10cSrcweir 				bool bRel = relatesToInterface( pTD );
104*cdf0e10cSrcweir 				TYPELIB_DANGER_RELEASE( pTD );
105*cdf0e10cSrcweir 				if (bRel)
106*cdf0e10cSrcweir 					return true;
107*cdf0e10cSrcweir 			}
108*cdf0e10cSrcweir             default:
109*cdf0e10cSrcweir                 ;
110*cdf0e10cSrcweir 			}
111*cdf0e10cSrcweir 		}
112*cdf0e10cSrcweir 		if (pComp->pBaseTypeDescription)
113*cdf0e10cSrcweir 			return relatesToInterface( (typelib_TypeDescription *)pComp->pBaseTypeDescription );
114*cdf0e10cSrcweir 		break;
115*cdf0e10cSrcweir 	}
116*cdf0e10cSrcweir 	case typelib_TypeClass_UNION: // might relate to interface
117*cdf0e10cSrcweir 	case typelib_TypeClass_ANY: // might relate to interface
118*cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE:
119*cdf0e10cSrcweir 		return true;
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     default:
122*cdf0e10cSrcweir         ;
123*cdf0e10cSrcweir 	}
124*cdf0e10cSrcweir 	return false;
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir extern "C" { static void SAL_CALL s_Proxy_dispatch(
128*cdf0e10cSrcweir     uno_Interface                 * pUnoI,
129*cdf0e10cSrcweir     typelib_TypeDescription const * pMemberType,
130*cdf0e10cSrcweir     void                          * pReturn,
131*cdf0e10cSrcweir     void                          * pArgs[],
132*cdf0e10cSrcweir     uno_Any                      ** ppException)
133*cdf0e10cSrcweir 	SAL_THROW_EXTERN_C()
134*cdf0e10cSrcweir {
135*cdf0e10cSrcweir     Proxy * pThis = static_cast<Proxy *>(pUnoI);
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir     typelib_MethodParameter            param;
138*cdf0e10cSrcweir     sal_Int32                          nParams = 0;
139*cdf0e10cSrcweir     typelib_MethodParameter          * pParams = 0;
140*cdf0e10cSrcweir     typelib_TypeDescriptionReference * pReturnTypeRef = 0;
141*cdf0e10cSrcweir     // sal_Int32                          nOutParams = 0;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     switch (pMemberType->eTypeClass)
144*cdf0e10cSrcweir     {
145*cdf0e10cSrcweir     case typelib_TypeClass_INTERFACE_ATTRIBUTE:
146*cdf0e10cSrcweir         if (pReturn)
147*cdf0e10cSrcweir         {
148*cdf0e10cSrcweir             pReturnTypeRef =
149*cdf0e10cSrcweir                 ((typelib_InterfaceAttributeTypeDescription *)
150*cdf0e10cSrcweir                  pMemberType)->pAttributeTypeRef;
151*cdf0e10cSrcweir             nParams = 0;
152*cdf0e10cSrcweir             pParams = NULL;
153*cdf0e10cSrcweir         }
154*cdf0e10cSrcweir         else
155*cdf0e10cSrcweir         {
156*cdf0e10cSrcweir             param.pTypeRef = ((typelib_InterfaceAttributeTypeDescription *)
157*cdf0e10cSrcweir                               pMemberType)->pAttributeTypeRef;
158*cdf0e10cSrcweir             param.bIn = sal_True;
159*cdf0e10cSrcweir             param.bOut = sal_False;
160*cdf0e10cSrcweir             nParams = 1;
161*cdf0e10cSrcweir             pParams = &param;
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir         break;
164*cdf0e10cSrcweir     case typelib_TypeClass_INTERFACE_METHOD:
165*cdf0e10cSrcweir     {
166*cdf0e10cSrcweir         typelib_InterfaceMethodTypeDescription * method_td =
167*cdf0e10cSrcweir             (typelib_InterfaceMethodTypeDescription *) pMemberType;
168*cdf0e10cSrcweir         pReturnTypeRef = method_td->pReturnTypeRef;
169*cdf0e10cSrcweir         nParams = method_td->nParams;
170*cdf0e10cSrcweir         pParams = method_td->pParams;
171*cdf0e10cSrcweir         break;
172*cdf0e10cSrcweir     }
173*cdf0e10cSrcweir     default:
174*cdf0e10cSrcweir         OSL_ENSURE( sal_False, "### illegal member typeclass!" );
175*cdf0e10cSrcweir         abort();
176*cdf0e10cSrcweir     }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir     pThis->dispatch( pReturnTypeRef,
179*cdf0e10cSrcweir                      pParams,
180*cdf0e10cSrcweir                      nParams,
181*cdf0e10cSrcweir                      pMemberType,
182*cdf0e10cSrcweir                      pReturn,
183*cdf0e10cSrcweir                      pArgs,
184*cdf0e10cSrcweir                      ppException );
185*cdf0e10cSrcweir }}
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir extern "C" void SAL_CALL Proxy_free(uno_ExtEnvironment * /*pEnv*/, void * pProxy) SAL_THROW_EXTERN_C()
188*cdf0e10cSrcweir {
189*cdf0e10cSrcweir     Proxy * pThis = static_cast<Proxy * >(reinterpret_cast<uno_Interface *>(pProxy));
190*cdf0e10cSrcweir 	delete pThis;
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir extern "C" {
194*cdf0e10cSrcweir static void SAL_CALL s_Proxy_acquire(uno_Interface * pUnoI) SAL_THROW_EXTERN_C()
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir 	Proxy * pProxy = static_cast<Proxy *>(pUnoI);
197*cdf0e10cSrcweir 	pProxy->acquire();
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir static void SAL_CALL s_Proxy_release(uno_Interface * pUnoI) SAL_THROW_EXTERN_C()
201*cdf0e10cSrcweir {
202*cdf0e10cSrcweir 	Proxy * pProxy = static_cast<Proxy *>(pUnoI);
203*cdf0e10cSrcweir 	pProxy->release();
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir static void s_acquireAndRegister_v(va_list * pParam)
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir 	uno_Interface                    * pUnoI      = va_arg(*pParam, uno_Interface *);
209*cdf0e10cSrcweir 	rtl_uString                      * pOid       = va_arg(*pParam, rtl_uString *);
210*cdf0e10cSrcweir 	typelib_InterfaceTypeDescription * pTypeDescr = va_arg(*pParam, typelib_InterfaceTypeDescription *);
211*cdf0e10cSrcweir 	uno_ExtEnvironment               * pEnv       = va_arg(*pParam, uno_ExtEnvironment *);
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 	pUnoI->acquire(pUnoI);
214*cdf0e10cSrcweir  	pEnv->registerInterface(pEnv, reinterpret_cast<void **>(&pUnoI), pOid, pTypeDescr);
215*cdf0e10cSrcweir }
216*cdf0e10cSrcweir }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir Proxy::Proxy(uno::Mapping                  const & to_from,
219*cdf0e10cSrcweir 			 uno_Environment                     * pTo,
220*cdf0e10cSrcweir 			 uno_Environment                     * pFrom,
221*cdf0e10cSrcweir 			 uno_Interface                       * pUnoI,
222*cdf0e10cSrcweir 			 typelib_InterfaceTypeDescription    * pTypeDescr,
223*cdf0e10cSrcweir 			 rtl::OUString                 const & rOId,
224*cdf0e10cSrcweir 			 cppu::helper::purpenv::ProbeFun     * probeFun,
225*cdf0e10cSrcweir 			 void                                * pProbeContext
226*cdf0e10cSrcweir )
227*cdf0e10cSrcweir     SAL_THROW(())
228*cdf0e10cSrcweir 		: m_nRef         (1),
229*cdf0e10cSrcweir 		  m_from         (pFrom),
230*cdf0e10cSrcweir 		  m_to           (pTo),
231*cdf0e10cSrcweir 		  m_from_to      (pFrom, pTo),
232*cdf0e10cSrcweir 		  m_to_from      (to_from),
233*cdf0e10cSrcweir 		  m_pUnoI        (pUnoI),
234*cdf0e10cSrcweir 		  m_pTypeDescr   (pTypeDescr),
235*cdf0e10cSrcweir 		  m_aOId         (rOId),
236*cdf0e10cSrcweir 		  m_probeFun     (probeFun),
237*cdf0e10cSrcweir 		  m_pProbeContext(pProbeContext)
238*cdf0e10cSrcweir {
239*cdf0e10cSrcweir 	LOG_LIFECYCLE_Proxy_emit(fprintf(stderr, "LIFE: %s -> %p\n", "Proxy::Proxy(<>)", this));
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir     typelib_typedescription_acquire((typelib_TypeDescription *)m_pTypeDescr);
242*cdf0e10cSrcweir     if (!((typelib_TypeDescription *)m_pTypeDescr)->bComplete)
243*cdf0e10cSrcweir 		typelib_typedescription_complete((typelib_TypeDescription **)&m_pTypeDescr);
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir     OSL_ENSURE(((typelib_TypeDescription *)m_pTypeDescr)->bComplete, "### type is incomplete!");
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir 	uno_Environment_invoke(m_to.get(), s_acquireAndRegister_v, m_pUnoI, rOId.pData, pTypeDescr, m_to.get());
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 	// uno_Interface
250*cdf0e10cSrcweir     uno_Interface::acquire     = s_Proxy_acquire;
251*cdf0e10cSrcweir     uno_Interface::release     = s_Proxy_release;
252*cdf0e10cSrcweir     uno_Interface::pDispatcher = s_Proxy_dispatch;
253*cdf0e10cSrcweir }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir extern "C" { static void s_releaseAndRevoke_v(va_list * pParam)
256*cdf0e10cSrcweir {
257*cdf0e10cSrcweir 	uno_ExtEnvironment * pEnv  = va_arg(*pParam, uno_ExtEnvironment *);
258*cdf0e10cSrcweir 	uno_Interface      * pUnoI = va_arg(*pParam, uno_Interface *);
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 	pEnv->revokeInterface(pEnv, reinterpret_cast<void *>(pUnoI));
261*cdf0e10cSrcweir     pUnoI->release(pUnoI);
262*cdf0e10cSrcweir }}
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir Proxy::~Proxy()
265*cdf0e10cSrcweir {
266*cdf0e10cSrcweir 	LOG_LIFECYCLE_Proxy_emit(fprintf(stderr, "LIFE: %s -> %p\n", "Proxy::~Proxy()", this));
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 	uno_Environment_invoke(m_to.get(), s_releaseAndRevoke_v, m_to.get(), m_pUnoI);
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir 	typelib_typedescription_release((typelib_TypeDescription *)m_pTypeDescr);
271*cdf0e10cSrcweir }
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir static uno::TypeDescription getAcquireMethod(void)
274*cdf0e10cSrcweir {
275*cdf0e10cSrcweir 	typelib_TypeDescriptionReference * type_XInterface =
276*cdf0e10cSrcweir 		* typelib_static_type_getByTypeClass(typelib_TypeClass_INTERFACE);
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir     typelib_TypeDescription * pTXInterfaceDescr = 0;
279*cdf0e10cSrcweir     TYPELIB_DANGER_GET    (&pTXInterfaceDescr, type_XInterface);
280*cdf0e10cSrcweir     uno::TypeDescription acquire(
281*cdf0e10cSrcweir         reinterpret_cast< typelib_InterfaceTypeDescription * >(
282*cdf0e10cSrcweir             pTXInterfaceDescr)->ppAllMembers[1]);
283*cdf0e10cSrcweir     TYPELIB_DANGER_RELEASE(pTXInterfaceDescr);
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 	return acquire;
286*cdf0e10cSrcweir }
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir static uno::TypeDescription getReleaseMethod(void)
289*cdf0e10cSrcweir {
290*cdf0e10cSrcweir 	typelib_TypeDescriptionReference * type_XInterface =
291*cdf0e10cSrcweir 		* typelib_static_type_getByTypeClass(typelib_TypeClass_INTERFACE);
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir     typelib_TypeDescription * pTXInterfaceDescr = 0;
294*cdf0e10cSrcweir     TYPELIB_DANGER_GET    (&pTXInterfaceDescr, type_XInterface);
295*cdf0e10cSrcweir     uno::TypeDescription release(
296*cdf0e10cSrcweir         reinterpret_cast< typelib_InterfaceTypeDescription * >(
297*cdf0e10cSrcweir             pTXInterfaceDescr)->ppAllMembers[2]);
298*cdf0e10cSrcweir     TYPELIB_DANGER_RELEASE(pTXInterfaceDescr);
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir     return release;
301*cdf0e10cSrcweir }
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir static uno::TypeDescription s_acquireMethod(getAcquireMethod());
304*cdf0e10cSrcweir static uno::TypeDescription s_releaseMethod(getReleaseMethod());
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir void Proxy::acquire(void)
307*cdf0e10cSrcweir {
308*cdf0e10cSrcweir 	if (m_probeFun)
309*cdf0e10cSrcweir 		m_probeFun(true,
310*cdf0e10cSrcweir 				   this,
311*cdf0e10cSrcweir 				   m_pProbeContext,
312*cdf0e10cSrcweir 				   *typelib_static_type_getByTypeClass(typelib_TypeClass_VOID),
313*cdf0e10cSrcweir 				   NULL,
314*cdf0e10cSrcweir 				   0,
315*cdf0e10cSrcweir 				   s_acquireMethod.get(),
316*cdf0e10cSrcweir 				   NULL,
317*cdf0e10cSrcweir 				   NULL,
318*cdf0e10cSrcweir 				   NULL);
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir     if (osl_incrementInterlockedCount(&m_nRef) == 1)
321*cdf0e10cSrcweir     {
322*cdf0e10cSrcweir         // rebirth of proxy zombie
323*cdf0e10cSrcweir 		void * pThis = this;
324*cdf0e10cSrcweir         m_from.get()->pExtEnv->registerProxyInterface(m_from.get()->pExtEnv,
325*cdf0e10cSrcweir 													  &pThis,
326*cdf0e10cSrcweir 													  Proxy_free,
327*cdf0e10cSrcweir 													  m_aOId.pData,
328*cdf0e10cSrcweir 													  m_pTypeDescr);
329*cdf0e10cSrcweir         OSL_ASSERT(pThis == this);
330*cdf0e10cSrcweir     }
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir 	if (m_probeFun)
333*cdf0e10cSrcweir 		m_probeFun(false,
334*cdf0e10cSrcweir 				   this,
335*cdf0e10cSrcweir 				   m_pProbeContext,
336*cdf0e10cSrcweir 				   *typelib_static_type_getByTypeClass(typelib_TypeClass_VOID),
337*cdf0e10cSrcweir 				   NULL,
338*cdf0e10cSrcweir 				   0,
339*cdf0e10cSrcweir 				   s_acquireMethod.get(),
340*cdf0e10cSrcweir 				   NULL,
341*cdf0e10cSrcweir 				   NULL,
342*cdf0e10cSrcweir 				   NULL);
343*cdf0e10cSrcweir 
344*cdf0e10cSrcweir }
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir void Proxy::release(void)
347*cdf0e10cSrcweir {
348*cdf0e10cSrcweir 	cppu::helper::purpenv::ProbeFun * probeFun = m_probeFun;
349*cdf0e10cSrcweir 	void                            * pProbeContext = m_pProbeContext;
350*cdf0e10cSrcweir 
351*cdf0e10cSrcweir 	if (m_probeFun)
352*cdf0e10cSrcweir 		m_probeFun(true,
353*cdf0e10cSrcweir 				   this,
354*cdf0e10cSrcweir 				   m_pProbeContext,
355*cdf0e10cSrcweir 				   *typelib_static_type_getByTypeClass(typelib_TypeClass_VOID),
356*cdf0e10cSrcweir 				   NULL,
357*cdf0e10cSrcweir 				   0,
358*cdf0e10cSrcweir 				   s_releaseMethod.get(),
359*cdf0e10cSrcweir 				   NULL,
360*cdf0e10cSrcweir 				   NULL,
361*cdf0e10cSrcweir 				   NULL);
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir     if (osl_decrementInterlockedCount(&m_nRef) == 0)
364*cdf0e10cSrcweir         m_from.get()->pExtEnv->revokeInterface(m_from.get()->pExtEnv, this);
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir 	if (probeFun)
367*cdf0e10cSrcweir 		probeFun(false,
368*cdf0e10cSrcweir 				 this,
369*cdf0e10cSrcweir 				 pProbeContext,
370*cdf0e10cSrcweir 				 *typelib_static_type_getByTypeClass(typelib_TypeClass_VOID),
371*cdf0e10cSrcweir 				 NULL,
372*cdf0e10cSrcweir 				 0,
373*cdf0e10cSrcweir 				 s_releaseMethod.get(),
374*cdf0e10cSrcweir 				 NULL,
375*cdf0e10cSrcweir 				 NULL,
376*cdf0e10cSrcweir 				 NULL);
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir }
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir extern "C" {
382*cdf0e10cSrcweir static void s_type_destructData_v(va_list * pParam)
383*cdf0e10cSrcweir {
384*cdf0e10cSrcweir 	void * ret = va_arg(*pParam, void *);
385*cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pReturnTypeRef = va_arg(*pParam, typelib_TypeDescriptionReference *);
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir 	uno_type_destructData(ret, pReturnTypeRef, 0);
388*cdf0e10cSrcweir }
389*cdf0e10cSrcweir 
390*cdf0e10cSrcweir static void s_dispatcher_v(va_list * pParam)
391*cdf0e10cSrcweir {
392*cdf0e10cSrcweir 	uno_Interface                 * pUnoI       = va_arg(*pParam, uno_Interface *);
393*cdf0e10cSrcweir 	typelib_TypeDescription const * pMemberType = va_arg(*pParam, typelib_TypeDescription const *);
394*cdf0e10cSrcweir 	void                          * pReturn     = va_arg(*pParam, void *);
395*cdf0e10cSrcweir 	void                         ** pArgs       = va_arg(*pParam, void **);
396*cdf0e10cSrcweir 	uno_Any                      ** ppException = va_arg(*pParam, uno_Any **);
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir 	pUnoI->pDispatcher(pUnoI, pMemberType, pReturn, pArgs, ppException);
399*cdf0e10cSrcweir }
400*cdf0e10cSrcweir }
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir void Proxy::dispatch(typelib_TypeDescriptionReference * pReturnTypeRef,
403*cdf0e10cSrcweir 					 typelib_MethodParameter          * pParams,
404*cdf0e10cSrcweir 					 sal_Int32                          nParams,
405*cdf0e10cSrcweir 					 typelib_TypeDescription    const * pMemberType,
406*cdf0e10cSrcweir 					 void                             * pReturn,
407*cdf0e10cSrcweir 					 void                             * pArgs[],
408*cdf0e10cSrcweir 					 uno_Any                         ** ppException)
409*cdf0e10cSrcweir {
410*cdf0e10cSrcweir 	if (m_probeFun)
411*cdf0e10cSrcweir 		m_probeFun(true,
412*cdf0e10cSrcweir 				   this,
413*cdf0e10cSrcweir 				   m_pProbeContext,
414*cdf0e10cSrcweir 				   pReturnTypeRef,
415*cdf0e10cSrcweir 				   pParams,
416*cdf0e10cSrcweir 				   nParams,
417*cdf0e10cSrcweir 				   pMemberType,
418*cdf0e10cSrcweir 				   pReturn,
419*cdf0e10cSrcweir 				   pArgs,
420*cdf0e10cSrcweir 				   ppException);
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir     void ** args = (void **) alloca( sizeof (void *) * nParams );
423*cdf0e10cSrcweir 
424*cdf0e10cSrcweir     typelib_TypeDescription * return_td = 0;
425*cdf0e10cSrcweir     void * ret = pReturn;
426*cdf0e10cSrcweir 	if (pReturnTypeRef)
427*cdf0e10cSrcweir 	{
428*cdf0e10cSrcweir 		TYPELIB_DANGER_GET(&return_td, pReturnTypeRef);
429*cdf0e10cSrcweir 
430*cdf0e10cSrcweir 		if (relatesToInterface(return_td))
431*cdf0e10cSrcweir 			ret = alloca(return_td->nSize);
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir 		TYPELIB_DANGER_RELEASE(return_td);
434*cdf0e10cSrcweir 	}
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir     for (sal_Int32 nPos = 0; nPos < nParams; ++ nPos)
437*cdf0e10cSrcweir     {
438*cdf0e10cSrcweir         typelib_MethodParameter const & param = pParams[nPos];
439*cdf0e10cSrcweir         typelib_TypeDescription * td = 0;
440*cdf0e10cSrcweir         TYPELIB_DANGER_GET( &td, param.pTypeRef );
441*cdf0e10cSrcweir         if (relatesToInterface(td))
442*cdf0e10cSrcweir         {
443*cdf0e10cSrcweir             args[nPos] = alloca(td->nSize);
444*cdf0e10cSrcweir             if (param.bIn)
445*cdf0e10cSrcweir             {
446*cdf0e10cSrcweir                 uno_copyAndConvertData(args[nPos], pArgs[nPos], td, m_from_to.get());
447*cdf0e10cSrcweir             }
448*cdf0e10cSrcweir         }
449*cdf0e10cSrcweir         else
450*cdf0e10cSrcweir         {
451*cdf0e10cSrcweir             args[nPos] = pArgs[nPos];
452*cdf0e10cSrcweir         }
453*cdf0e10cSrcweir         TYPELIB_DANGER_RELEASE( td );
454*cdf0e10cSrcweir     }
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir     uno_Any exc_data;
457*cdf0e10cSrcweir     uno_Any * exc = &exc_data;
458*cdf0e10cSrcweir 
459*cdf0e10cSrcweir 	// do the UNO call...
460*cdf0e10cSrcweir 	uno_Environment_invoke(m_to.get(), s_dispatcher_v, m_pUnoI, pMemberType, ret, args, &exc);
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir     if (exc == 0)
463*cdf0e10cSrcweir     {
464*cdf0e10cSrcweir         for (sal_Int32 nPos = 0; nPos < nParams; ++ nPos)
465*cdf0e10cSrcweir         {
466*cdf0e10cSrcweir             if (args[nPos] != pArgs[nPos])
467*cdf0e10cSrcweir             {
468*cdf0e10cSrcweir                 typelib_MethodParameter const & param = pParams[nPos];
469*cdf0e10cSrcweir                 if (param.bOut)
470*cdf0e10cSrcweir                 {
471*cdf0e10cSrcweir                     if (param.bIn) // is inout
472*cdf0e10cSrcweir                     {
473*cdf0e10cSrcweir                         uno_type_destructData(pArgs[nPos], param.pTypeRef, 0);
474*cdf0e10cSrcweir                     }
475*cdf0e10cSrcweir                     uno_type_copyAndConvertData(pArgs[ nPos ],
476*cdf0e10cSrcweir 												args[ nPos ],
477*cdf0e10cSrcweir 												param.pTypeRef,
478*cdf0e10cSrcweir 												m_to_from.get());
479*cdf0e10cSrcweir                 }
480*cdf0e10cSrcweir 				uno_Environment_invoke(m_to.get(), s_type_destructData_v, args[nPos], param.pTypeRef, 0);
481*cdf0e10cSrcweir             }
482*cdf0e10cSrcweir         }
483*cdf0e10cSrcweir         if (ret != pReturn)
484*cdf0e10cSrcweir         {
485*cdf0e10cSrcweir             uno_type_copyAndConvertData(pReturn,
486*cdf0e10cSrcweir 										ret,
487*cdf0e10cSrcweir 										pReturnTypeRef,
488*cdf0e10cSrcweir 										m_to_from.get());
489*cdf0e10cSrcweir 
490*cdf0e10cSrcweir 			uno_Environment_invoke(m_to.get(), s_type_destructData_v, ret, pReturnTypeRef, 0);
491*cdf0e10cSrcweir         }
492*cdf0e10cSrcweir 
493*cdf0e10cSrcweir         *ppException = 0;
494*cdf0e10cSrcweir     }
495*cdf0e10cSrcweir     else // exception occured
496*cdf0e10cSrcweir     {
497*cdf0e10cSrcweir         for (sal_Int32 nPos = 0; nPos < nParams; ++ nPos)
498*cdf0e10cSrcweir         {
499*cdf0e10cSrcweir             if (args[nPos] != pArgs[nPos])
500*cdf0e10cSrcweir             {
501*cdf0e10cSrcweir                 typelib_MethodParameter const & param = pParams[nPos];
502*cdf0e10cSrcweir                 if (param.bIn)
503*cdf0e10cSrcweir                 {
504*cdf0e10cSrcweir 					uno_Environment_invoke(m_to.get(), s_type_destructData_v, args[nPos], param.pTypeRef, 0);
505*cdf0e10cSrcweir                 }
506*cdf0e10cSrcweir             }
507*cdf0e10cSrcweir         }
508*cdf0e10cSrcweir 
509*cdf0e10cSrcweir         uno_type_any_constructAndConvert(*ppException,
510*cdf0e10cSrcweir 										 exc->pData,
511*cdf0e10cSrcweir 										 exc->pType,
512*cdf0e10cSrcweir 										 m_to_from.get());
513*cdf0e10cSrcweir 
514*cdf0e10cSrcweir 		// FIXME: need to destruct in m_to
515*cdf0e10cSrcweir 		uno_any_destruct(exc, 0);
516*cdf0e10cSrcweir     }
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir 	if (m_probeFun)
519*cdf0e10cSrcweir 		m_probeFun(false,
520*cdf0e10cSrcweir 				   this,
521*cdf0e10cSrcweir 				   m_pProbeContext,
522*cdf0e10cSrcweir 				   pReturnTypeRef,
523*cdf0e10cSrcweir 				   pParams,
524*cdf0e10cSrcweir 				   nParams,
525*cdf0e10cSrcweir 				   pMemberType,
526*cdf0e10cSrcweir 				   pReturn,
527*cdf0e10cSrcweir 				   pArgs,
528*cdf0e10cSrcweir 				   ppException);
529*cdf0e10cSrcweir }
530*cdf0e10cSrcweir 
531