1*d48fe848SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d48fe848SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d48fe848SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d48fe848SAndrew Rist  * distributed with this work for additional information
6*d48fe848SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d48fe848SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d48fe848SAndrew Rist  * "License"); you may not use this file except in compliance
9*d48fe848SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d48fe848SAndrew Rist  *
11*d48fe848SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d48fe848SAndrew Rist  *
13*d48fe848SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d48fe848SAndrew Rist  * software distributed under the License is distributed on an
15*d48fe848SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d48fe848SAndrew Rist  * KIND, either express or implied.  See the License for the
17*d48fe848SAndrew Rist  * specific language governing permissions and limitations
18*d48fe848SAndrew Rist  * under the License.
19*d48fe848SAndrew Rist  *
20*d48fe848SAndrew Rist  *************************************************************/
21*d48fe848SAndrew Rist 
22*d48fe848SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_testtools.hxx"
26cdf0e10cSrcweir #include <osl/diagnose.h>
27cdf0e10cSrcweir #include <osl/interlck.h>
28cdf0e10cSrcweir #include <rtl/ustring.hxx>
29cdf0e10cSrcweir #include <typelib/typedescription.h>
30cdf0e10cSrcweir #include <uno/dispatcher.h>
31cdf0e10cSrcweir #include <uno/environment.h>
32cdf0e10cSrcweir #include <uno/mapping.h>
33cdf0e10cSrcweir #include <uno/lbnames.h>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace rtl;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 
38cdf0e10cSrcweir namespace pseudo_uno
39cdf0e10cSrcweir {
40cdf0e10cSrcweir 
41cdf0e10cSrcweir //==================================================================================================
42cdf0e10cSrcweir struct pseudo_Mapping : public uno_Mapping
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	oslInterlockedCount		nRef;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 	uno_ExtEnvironment *	pFrom;
47cdf0e10cSrcweir 	uno_ExtEnvironment *	pTo;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 	pseudo_Mapping( uno_ExtEnvironment * pFrom_, uno_ExtEnvironment * pTo_ );
50cdf0e10cSrcweir 	~pseudo_Mapping();
51cdf0e10cSrcweir };
52cdf0e10cSrcweir 
53cdf0e10cSrcweir //==== a uno pseudo proxy =============================================================================
54cdf0e10cSrcweir struct pseudo_unoInterfaceProxy : public uno_Interface
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 	oslInterlockedCount					nRef;
57cdf0e10cSrcweir 	pseudo_Mapping *					pPseudoMapping;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 	// mapping information
60cdf0e10cSrcweir 	uno_Interface *						pUnoI; // wrapped interface
61cdf0e10cSrcweir 	typelib_InterfaceTypeDescription *	pTypeDescr;
62cdf0e10cSrcweir 	OUString							oid;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	// ctor
65cdf0e10cSrcweir 	inline pseudo_unoInterfaceProxy( pseudo_Mapping * pPseudoMapping_,
66cdf0e10cSrcweir 									 uno_Interface * pUnoI_,
67cdf0e10cSrcweir 									 typelib_InterfaceTypeDescription * pTypeDescr_,
68cdf0e10cSrcweir 									 const OUString & rOId_ );
69cdf0e10cSrcweir };
70cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
pseudo_unoInterfaceProxy_dispatch(uno_Interface * pUnoI,const typelib_TypeDescription * pMemberType,void * pReturn,void * pArgs[],uno_Any ** ppException)71cdf0e10cSrcweir static void SAL_CALL pseudo_unoInterfaceProxy_dispatch(
72cdf0e10cSrcweir 	uno_Interface * pUnoI,
73cdf0e10cSrcweir 	const typelib_TypeDescription * pMemberType,
74cdf0e10cSrcweir 	void * pReturn,
75cdf0e10cSrcweir 	void * pArgs[],
76cdf0e10cSrcweir 	uno_Any ** ppException )
77cdf0e10cSrcweir {
78cdf0e10cSrcweir 	pseudo_unoInterfaceProxy * pThis = static_cast< pseudo_unoInterfaceProxy * >( pUnoI );
79cdf0e10cSrcweir 	(*pThis->pUnoI->pDispatcher)( pThis->pUnoI, pMemberType, pReturn, pArgs, ppException );
80cdf0e10cSrcweir }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
pseudo_unoInterfaceProxy_free(uno_ExtEnvironment * pEnv,void * pProxy)83cdf0e10cSrcweir static void SAL_CALL pseudo_unoInterfaceProxy_free( uno_ExtEnvironment * pEnv, void * pProxy )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir 	pseudo_unoInterfaceProxy * pThis =
86cdf0e10cSrcweir 		static_cast< pseudo_unoInterfaceProxy * >(
87cdf0e10cSrcweir 			reinterpret_cast< uno_Interface * >( pProxy ) );
88cdf0e10cSrcweir 	OSL_ASSERT( pEnv == pThis->pPseudoMapping->pTo );
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	(*pThis->pPseudoMapping->pFrom->revokeInterface)( pThis->pPseudoMapping->pFrom, pThis->pUnoI );
91cdf0e10cSrcweir 	(*pThis->pUnoI->release)( pThis->pUnoI );
92cdf0e10cSrcweir 	typelib_typedescription_release( (typelib_TypeDescription *)pThis->pTypeDescr );
93cdf0e10cSrcweir 	(*pThis->pPseudoMapping->release)( pThis->pPseudoMapping );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
96cdf0e10cSrcweir 	*(int *)pProxy = 0xdeadbabe;
97cdf0e10cSrcweir #endif
98cdf0e10cSrcweir 	delete pThis;
99cdf0e10cSrcweir }
100cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
pseudo_unoInterfaceProxy_acquire(uno_Interface * pUnoI)101cdf0e10cSrcweir static void SAL_CALL pseudo_unoInterfaceProxy_acquire( uno_Interface * pUnoI )
102cdf0e10cSrcweir {
103cdf0e10cSrcweir 	if (1 == osl_incrementInterlockedCount( &static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->nRef ))
104cdf0e10cSrcweir 	{
105cdf0e10cSrcweir 		// rebirth of proxy zombie
106cdf0e10cSrcweir 		// register at uno env
107cdf0e10cSrcweir 		void * pThis = static_cast< uno_Interface * >( pUnoI );
108cdf0e10cSrcweir 		(*static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo->registerProxyInterface)(
109cdf0e10cSrcweir 			static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo,
110cdf0e10cSrcweir 			&pThis, pseudo_unoInterfaceProxy_free,
111cdf0e10cSrcweir 			static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->oid.pData,
112cdf0e10cSrcweir 			static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pTypeDescr );
113cdf0e10cSrcweir 		OSL_ASSERT( pThis == static_cast< uno_Interface * >( pUnoI ) );
114cdf0e10cSrcweir 	}
115cdf0e10cSrcweir }
116cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
pseudo_unoInterfaceProxy_release(uno_Interface * pUnoI)117cdf0e10cSrcweir static void SAL_CALL pseudo_unoInterfaceProxy_release( uno_Interface * pUnoI )
118cdf0e10cSrcweir {
119cdf0e10cSrcweir 	if (! osl_decrementInterlockedCount( & static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->nRef ))
120cdf0e10cSrcweir 	{
121cdf0e10cSrcweir 		// revoke from uno env on last release
122cdf0e10cSrcweir 		(*static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo->revokeInterface)(
123cdf0e10cSrcweir 			static_cast< pseudo_unoInterfaceProxy * >( pUnoI )->pPseudoMapping->pTo, pUnoI );
124cdf0e10cSrcweir 	}
125cdf0e10cSrcweir }
126cdf0e10cSrcweir //__________________________________________________________________________________________________
pseudo_unoInterfaceProxy(pseudo_Mapping * pPseudoMapping_,uno_Interface * pUnoI_,typelib_InterfaceTypeDescription * pTypeDescr_,const OUString & rOId_)127cdf0e10cSrcweir inline pseudo_unoInterfaceProxy::pseudo_unoInterfaceProxy(
128cdf0e10cSrcweir 	pseudo_Mapping * pPseudoMapping_, uno_Interface * pUnoI_,
129cdf0e10cSrcweir 	typelib_InterfaceTypeDescription * pTypeDescr_, const OUString & rOId_ )
130cdf0e10cSrcweir 	: nRef( 1 )
131cdf0e10cSrcweir 	, pPseudoMapping( pPseudoMapping_ )
132cdf0e10cSrcweir 	, pUnoI( pUnoI_ )
133cdf0e10cSrcweir 	, pTypeDescr( pTypeDescr_ )
134cdf0e10cSrcweir 	, oid( rOId_ )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir 	(*pPseudoMapping->acquire)( pPseudoMapping );
137cdf0e10cSrcweir 	typelib_typedescription_acquire( (typelib_TypeDescription *)pTypeDescr );
138cdf0e10cSrcweir 	(*pPseudoMapping->pFrom->registerInterface)(
139cdf0e10cSrcweir 		pPseudoMapping->pFrom, reinterpret_cast< void ** >( &pUnoI ), oid.pData, pTypeDescr );
140cdf0e10cSrcweir 	(*pUnoI->acquire)( pUnoI );
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 	// uno_Interface
143cdf0e10cSrcweir 	uno_Interface::acquire = pseudo_unoInterfaceProxy_acquire;
144cdf0e10cSrcweir 	uno_Interface::release = pseudo_unoInterfaceProxy_release;
145cdf0e10cSrcweir 	uno_Interface::pDispatcher = pseudo_unoInterfaceProxy_dispatch;
146cdf0e10cSrcweir }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
pseudo_Mapping_mapInterface(uno_Mapping * pMapping,void ** ppOut,void * pUnoI,typelib_InterfaceTypeDescription * pTypeDescr)149cdf0e10cSrcweir static void SAL_CALL pseudo_Mapping_mapInterface(
150cdf0e10cSrcweir 	uno_Mapping * pMapping, void ** ppOut,
151cdf0e10cSrcweir 	void * pUnoI, typelib_InterfaceTypeDescription * pTypeDescr )
152cdf0e10cSrcweir {
153cdf0e10cSrcweir 	OSL_ASSERT( ppOut && pTypeDescr );
154cdf0e10cSrcweir 	if (*ppOut)
155cdf0e10cSrcweir 	{
156cdf0e10cSrcweir 		(*reinterpret_cast< uno_Interface * >( *ppOut )->release)(
157cdf0e10cSrcweir 			reinterpret_cast< uno_Interface * >( *ppOut ) );
158cdf0e10cSrcweir 		*ppOut = 0;
159cdf0e10cSrcweir 	}
160cdf0e10cSrcweir 	if (pUnoI && pTypeDescr)
161cdf0e10cSrcweir 	{
162cdf0e10cSrcweir 		// get object id of uno interface to be wrapped
163cdf0e10cSrcweir 		rtl_uString * pOId = 0;
164cdf0e10cSrcweir 		(*static_cast< pseudo_Mapping * >( pMapping )->pFrom->getObjectIdentifier)(
165cdf0e10cSrcweir 			static_cast< pseudo_Mapping * >( pMapping )->pFrom, &pOId, pUnoI );
166cdf0e10cSrcweir 		OSL_ASSERT( pOId );
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 		if (pOId)
169cdf0e10cSrcweir 		{
170cdf0e10cSrcweir 			// try to get any known interface from target environment
171cdf0e10cSrcweir 			(*static_cast< pseudo_Mapping * >( pMapping )->pTo->getRegisteredInterface)(
172cdf0e10cSrcweir 				static_cast< pseudo_Mapping * >( pMapping )->pTo, ppOut, pOId, pTypeDescr );
173cdf0e10cSrcweir 			if (! *ppOut) // no existing interface, register new proxy interface
174cdf0e10cSrcweir 			{
175cdf0e10cSrcweir 				// try to publish a new proxy (ref count initially 1)
176cdf0e10cSrcweir 				void * pProxy = new pseudo_unoInterfaceProxy(
177cdf0e10cSrcweir 					static_cast< pseudo_Mapping * >( pMapping ),
178cdf0e10cSrcweir 					reinterpret_cast< uno_Interface * >( pUnoI ), pTypeDescr, pOId );
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 				// proxy may be exchanged during registration
181cdf0e10cSrcweir 				(*static_cast< pseudo_Mapping * >( pMapping )->pTo->registerProxyInterface)(
182cdf0e10cSrcweir 					static_cast< pseudo_Mapping * >( pMapping )->pTo,
183cdf0e10cSrcweir 					&pProxy, pseudo_unoInterfaceProxy_free, pOId, pTypeDescr );
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 				*ppOut = pProxy;
186cdf0e10cSrcweir 			}
187cdf0e10cSrcweir 			rtl_uString_release( pOId );
188cdf0e10cSrcweir 		}
189cdf0e10cSrcweir 	}
190cdf0e10cSrcweir }
191cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
pseudo_Mapping_free(uno_Mapping * pMapping)192cdf0e10cSrcweir static void SAL_CALL pseudo_Mapping_free( uno_Mapping * pMapping )
193cdf0e10cSrcweir {
194cdf0e10cSrcweir 	delete static_cast< pseudo_Mapping * >( pMapping );
195cdf0e10cSrcweir }
196cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
pseudo_Mapping_acquire(uno_Mapping * pMapping)197cdf0e10cSrcweir static void SAL_CALL pseudo_Mapping_acquire( uno_Mapping * pMapping )
198cdf0e10cSrcweir {
199cdf0e10cSrcweir 	if (1 == osl_incrementInterlockedCount( & static_cast< pseudo_Mapping * >( pMapping )->nRef ))
200cdf0e10cSrcweir 	{
201cdf0e10cSrcweir 		OUString aMappingPurpose( RTL_CONSTASCII_USTRINGPARAM("pseudo") );
202cdf0e10cSrcweir 		uno_registerMapping( &pMapping,
203cdf0e10cSrcweir 							 pseudo_Mapping_free,
204cdf0e10cSrcweir 							 (uno_Environment *)((pseudo_Mapping *)pMapping)->pFrom,
205cdf0e10cSrcweir 							 (uno_Environment *)((pseudo_Mapping *)pMapping)->pTo,
206cdf0e10cSrcweir 							 aMappingPurpose.pData );
207cdf0e10cSrcweir 	}
208cdf0e10cSrcweir }
209cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
pseudo_Mapping_release(uno_Mapping * pMapping)210cdf0e10cSrcweir static void SAL_CALL pseudo_Mapping_release( uno_Mapping * pMapping )
211cdf0e10cSrcweir {
212cdf0e10cSrcweir 	if (! osl_decrementInterlockedCount( & static_cast< pseudo_Mapping * >( pMapping )->nRef ))
213cdf0e10cSrcweir 	{
214cdf0e10cSrcweir 		uno_revokeMapping( pMapping );
215cdf0e10cSrcweir 	}
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir //__________________________________________________________________________________________________
pseudo_Mapping(uno_ExtEnvironment * pFrom_,uno_ExtEnvironment * pTo_)219cdf0e10cSrcweir pseudo_Mapping::pseudo_Mapping( uno_ExtEnvironment * pFrom_, uno_ExtEnvironment * pTo_ )
220cdf0e10cSrcweir 	: nRef( 1 )
221cdf0e10cSrcweir 	, pFrom( pFrom_ )
222cdf0e10cSrcweir 	, pTo( pTo_ )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir 	(*((uno_Environment *)pFrom)->acquire)( (uno_Environment *)pFrom );
225cdf0e10cSrcweir 	(*((uno_Environment *)pTo)->acquire)( (uno_Environment *)pTo );
226cdf0e10cSrcweir 	//
227cdf0e10cSrcweir 	uno_Mapping::acquire = pseudo_Mapping_acquire;
228cdf0e10cSrcweir 	uno_Mapping::release = pseudo_Mapping_release;
229cdf0e10cSrcweir 	uno_Mapping::mapInterface = pseudo_Mapping_mapInterface;
230cdf0e10cSrcweir }
231cdf0e10cSrcweir //__________________________________________________________________________________________________
~pseudo_Mapping()232cdf0e10cSrcweir pseudo_Mapping::~pseudo_Mapping()
233cdf0e10cSrcweir {
234cdf0e10cSrcweir 	(*((uno_Environment *)pTo)->release)( (uno_Environment *)pTo );
235cdf0e10cSrcweir 	(*((uno_Environment *)pFrom)->release)( (uno_Environment *)pFrom );
236cdf0e10cSrcweir }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir }
239cdf0e10cSrcweir 
240cdf0e10cSrcweir //##################################################################################################
uno_initEnvironment(uno_Environment * pUnoEnv)241cdf0e10cSrcweir extern "C" void SAL_CALL uno_initEnvironment( uno_Environment * pUnoEnv )
242cdf0e10cSrcweir {
243cdf0e10cSrcweir 	OSL_ENSURE( sal_False, "### no impl: unexpected call!" );
244cdf0e10cSrcweir }
245cdf0e10cSrcweir //##################################################################################################
uno_ext_getMapping(uno_Mapping ** ppMapping,uno_Environment * pFrom,uno_Environment * pTo)246cdf0e10cSrcweir extern "C" void SAL_CALL uno_ext_getMapping(
247cdf0e10cSrcweir 	uno_Mapping ** ppMapping, uno_Environment * pFrom, uno_Environment * pTo )
248cdf0e10cSrcweir {
249cdf0e10cSrcweir 	OSL_ASSERT( ppMapping && pFrom && pTo );
250cdf0e10cSrcweir 	if (ppMapping && pFrom && pTo && pFrom->pExtEnv && pTo->pExtEnv)
251cdf0e10cSrcweir 	{
252cdf0e10cSrcweir 		uno_Mapping * pMapping = 0;
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 		if (0 == rtl_ustr_ascii_compare( pFrom->pTypeName->buffer, UNO_LB_UNO ) &&
255cdf0e10cSrcweir 			0 == rtl_ustr_ascii_compare( pTo->pTypeName->buffer, UNO_LB_UNO ))
256cdf0e10cSrcweir 		{
257cdf0e10cSrcweir 			OUString aMappingPurpose( RTL_CONSTASCII_USTRINGPARAM("pseudo") );
258cdf0e10cSrcweir 			// ref count is initially 1
259cdf0e10cSrcweir 			pMapping = new pseudo_uno::pseudo_Mapping( pFrom->pExtEnv, pTo->pExtEnv );
260cdf0e10cSrcweir 			uno_registerMapping( &pMapping, pseudo_uno::pseudo_Mapping_free,
261cdf0e10cSrcweir 								 (uno_Environment *)pFrom->pExtEnv,
262cdf0e10cSrcweir 								 (uno_Environment *)pTo->pExtEnv,
263cdf0e10cSrcweir 								 aMappingPurpose.pData );
264cdf0e10cSrcweir 		}
265cdf0e10cSrcweir 
266cdf0e10cSrcweir 		if (*ppMapping)
267cdf0e10cSrcweir 			(*(*ppMapping)->release)( *ppMapping );
268cdf0e10cSrcweir 		*ppMapping = pMapping;
269cdf0e10cSrcweir 	}
270cdf0e10cSrcweir }
271