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_bridges.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "component.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "bridges/cpp_uno/shared/bridge.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "com/sun/star/uno/Reference.hxx"
36*cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
37*cdf0e10cSrcweir #include "com/sun/star/uno/XInterface.hpp"
38*cdf0e10cSrcweir #include "osl/diagnose.h"
39*cdf0e10cSrcweir #include "osl/mutex.hxx"
40*cdf0e10cSrcweir #include "osl/time.h"
41*cdf0e10cSrcweir #include "rtl/process.h"
42*cdf0e10cSrcweir #include "rtl/unload.h"
43*cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
44*cdf0e10cSrcweir #include "rtl/ustring.h"
45*cdf0e10cSrcweir #include "rtl/ustring.hxx"
46*cdf0e10cSrcweir #include "sal/types.h"
47*cdf0e10cSrcweir #include "uno/environment.h"
48*cdf0e10cSrcweir #include "uno/lbnames.h"
49*cdf0e10cSrcweir #include "uno/mapping.h"
50*cdf0e10cSrcweir #include "cppu/EnvDcp.hxx"
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir namespace bridges { namespace cpp_uno { namespace shared {
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir } } }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir namespace {
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #if (defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500)) \
61*cdf0e10cSrcweir     || (defined(__GNUC__) && defined(__APPLE__))
62*cdf0e10cSrcweir static ::rtl::OUString * s_pStaticOidPart = 0;
63*cdf0e10cSrcweir #endif
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir const ::rtl::OUString & SAL_CALL cppu_cppenv_getStaticOIdPart() SAL_THROW( () )
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir #if ! ((defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500)) \
68*cdf0e10cSrcweir     || (defined(__GNUC__) && defined(__APPLE__)))
69*cdf0e10cSrcweir     static ::rtl::OUString * s_pStaticOidPart = 0;
70*cdf0e10cSrcweir #endif
71*cdf0e10cSrcweir     if (! s_pStaticOidPart)
72*cdf0e10cSrcweir     {
73*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
74*cdf0e10cSrcweir         if (! s_pStaticOidPart)
75*cdf0e10cSrcweir         {
76*cdf0e10cSrcweir             ::rtl::OUStringBuffer aRet( 64 );
77*cdf0e10cSrcweir             aRet.appendAscii( RTL_CONSTASCII_STRINGPARAM("];") );
78*cdf0e10cSrcweir             // good guid
79*cdf0e10cSrcweir             sal_uInt8 ar[16];
80*cdf0e10cSrcweir             ::rtl_getGlobalProcessId( ar );
81*cdf0e10cSrcweir             for ( sal_Int32 i = 0; i < 16; ++i )
82*cdf0e10cSrcweir             {
83*cdf0e10cSrcweir                 aRet.append( (sal_Int32)ar[i], 16 );
84*cdf0e10cSrcweir             }
85*cdf0e10cSrcweir #if (defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500)) \
86*cdf0e10cSrcweir     || (defined(__GNUC__) && defined(__APPLE__))
87*cdf0e10cSrcweir             s_pStaticOidPart = new ::rtl::OUString( aRet.makeStringAndClear() );
88*cdf0e10cSrcweir #else
89*cdf0e10cSrcweir             static ::rtl::OUString s_aStaticOidPart(
90*cdf0e10cSrcweir                 aRet.makeStringAndClear() );
91*cdf0e10cSrcweir             s_pStaticOidPart = &s_aStaticOidPart;
92*cdf0e10cSrcweir #endif
93*cdf0e10cSrcweir         }
94*cdf0e10cSrcweir     }
95*cdf0e10cSrcweir     return *s_pStaticOidPart;
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir extern "C" {
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir static void s_stub_computeObjectIdentifier(va_list * pParam)
103*cdf0e10cSrcweir     SAL_THROW( () )
104*cdf0e10cSrcweir {
105*cdf0e10cSrcweir 	uno_ExtEnvironment  * pEnv       = va_arg(*pParam, uno_ExtEnvironment *);
106*cdf0e10cSrcweir 	rtl_uString        ** ppOId      = va_arg(*pParam, rtl_uString **);
107*cdf0e10cSrcweir 	void                * pInterface = va_arg(*pParam, void *);
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     OSL_ENSURE( pEnv && ppOId && pInterface, "### null ptr!" );
111*cdf0e10cSrcweir     if (pEnv && ppOId && pInterface)
112*cdf0e10cSrcweir     {
113*cdf0e10cSrcweir         if (*ppOId)
114*cdf0e10cSrcweir         {
115*cdf0e10cSrcweir             rtl_uString_release( *ppOId );
116*cdf0e10cSrcweir             *ppOId = 0;
117*cdf0e10cSrcweir         }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir         try
120*cdf0e10cSrcweir         {
121*cdf0e10cSrcweir             ::com::sun::star::uno::Reference<
122*cdf0e10cSrcweir                   ::com::sun::star::uno::XInterface > xHome(
123*cdf0e10cSrcweir                       reinterpret_cast< ::com::sun::star::uno::XInterface * >(
124*cdf0e10cSrcweir                           pInterface ),
125*cdf0e10cSrcweir                       ::com::sun::star::uno::UNO_QUERY );
126*cdf0e10cSrcweir             OSL_ENSURE( xHome.is(), "### query to XInterface failed!" );
127*cdf0e10cSrcweir             if (xHome.is())
128*cdf0e10cSrcweir             {
129*cdf0e10cSrcweir                 // interface
130*cdf0e10cSrcweir                 ::rtl::OUStringBuffer oid( 64 );
131*cdf0e10cSrcweir                 oid.append( reinterpret_cast< sal_Int64 >(xHome.get()), 16 );
132*cdf0e10cSrcweir                 oid.append( (sal_Unicode)';' );
133*cdf0e10cSrcweir                 // ;environment[context]
134*cdf0e10cSrcweir                 oid.append(
135*cdf0e10cSrcweir                     *reinterpret_cast< ::rtl::OUString const * >(
136*cdf0e10cSrcweir                         &((uno_Environment *) pEnv)->pTypeName ) );
137*cdf0e10cSrcweir                 oid.append( (sal_Unicode)'[' );
138*cdf0e10cSrcweir                 oid.append(
139*cdf0e10cSrcweir                     reinterpret_cast< sal_Int64 >(
140*cdf0e10cSrcweir                         ((uno_Environment *)pEnv)->pContext),
141*cdf0e10cSrcweir                     16 );
142*cdf0e10cSrcweir                 // ];good guid
143*cdf0e10cSrcweir                 oid.append( cppu_cppenv_getStaticOIdPart() );
144*cdf0e10cSrcweir                 ::rtl::OUString aRet( oid.makeStringAndClear() );
145*cdf0e10cSrcweir                 ::rtl_uString_acquire( *ppOId = aRet.pData );
146*cdf0e10cSrcweir             }
147*cdf0e10cSrcweir         }
148*cdf0e10cSrcweir         catch (::com::sun::star::uno::RuntimeException &)
149*cdf0e10cSrcweir         {
150*cdf0e10cSrcweir             OSL_ENSURE(
151*cdf0e10cSrcweir                 0, "### RuntimeException occured udring queryInterface()!" );
152*cdf0e10cSrcweir         }
153*cdf0e10cSrcweir     }
154*cdf0e10cSrcweir }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir static void SAL_CALL computeObjectIdentifier(
157*cdf0e10cSrcweir     uno_ExtEnvironment * pExtEnv, rtl_uString ** ppOId, void * pInterface )
158*cdf0e10cSrcweir     SAL_THROW( () )
159*cdf0e10cSrcweir {
160*cdf0e10cSrcweir 	uno_Environment_invoke(&pExtEnv->aBase, s_stub_computeObjectIdentifier, pExtEnv, ppOId, pInterface);
161*cdf0e10cSrcweir }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir static void s_stub_acquireInterface(va_list * pParam)
164*cdf0e10cSrcweir     SAL_THROW( () )
165*cdf0e10cSrcweir {
166*cdf0e10cSrcweir 	/*uno_ExtEnvironment * pExtEnv = */va_arg(*pParam, uno_ExtEnvironment *);
167*cdf0e10cSrcweir 	void               * pCppI   = va_arg(*pParam, void *);
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     reinterpret_cast< ::com::sun::star::uno::XInterface * >( pCppI )->acquire();
170*cdf0e10cSrcweir }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir static void SAL_CALL acquireInterface( uno_ExtEnvironment * pExtEnv, void * pCppI )
173*cdf0e10cSrcweir     SAL_THROW( () )
174*cdf0e10cSrcweir {
175*cdf0e10cSrcweir 	uno_Environment_invoke(&pExtEnv->aBase, s_stub_acquireInterface, pExtEnv, pCppI);
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir static void s_stub_releaseInterface(va_list * pParam)
179*cdf0e10cSrcweir     SAL_THROW( () )
180*cdf0e10cSrcweir {
181*cdf0e10cSrcweir 	/*uno_ExtEnvironment * pExtEnv = */va_arg(*pParam, uno_ExtEnvironment *);
182*cdf0e10cSrcweir 	void               * pCppI   = va_arg(*pParam, void *);
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     reinterpret_cast< ::com::sun::star::uno::XInterface * >( pCppI )->release();
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir static void SAL_CALL releaseInterface( uno_ExtEnvironment * pExtEnv, void * pCppI )
188*cdf0e10cSrcweir     SAL_THROW( () )
189*cdf0e10cSrcweir {
190*cdf0e10cSrcweir 	uno_Environment_invoke(&pExtEnv->aBase, s_stub_releaseInterface, pExtEnv, pCppI);
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir static void SAL_CALL environmentDisposing( uno_Environment * ) SAL_THROW( () )
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir     bridges::cpp_uno::shared::g_moduleCount.modCnt.release(
196*cdf0e10cSrcweir         &bridges::cpp_uno::shared::g_moduleCount.modCnt );
197*cdf0e10cSrcweir }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir sal_Bool SAL_CALL component_canUnload(TimeValue * pTime) SAL_THROW_EXTERN_C() {
200*cdf0e10cSrcweir     return bridges::cpp_uno::shared::g_moduleCount.canUnload(
201*cdf0e10cSrcweir         &bridges::cpp_uno::shared::g_moduleCount, pTime);
202*cdf0e10cSrcweir }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir void SAL_CALL uno_initEnvironment(uno_Environment * pCppEnv)
205*cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
206*cdf0e10cSrcweir {
207*cdf0e10cSrcweir     OSL_ENSURE( pCppEnv->pExtEnv, "### expected extended environment!" );
208*cdf0e10cSrcweir     OSL_ENSURE(
209*cdf0e10cSrcweir 		::rtl_ustr_ascii_compare_WithLength(
210*cdf0e10cSrcweir              pCppEnv->pTypeName->buffer, rtl_str_getLength(CPPU_CURRENT_LANGUAGE_BINDING_NAME), CPPU_CURRENT_LANGUAGE_BINDING_NAME )
211*cdf0e10cSrcweir         == 0,
212*cdf0e10cSrcweir         "### wrong environment type!" );
213*cdf0e10cSrcweir     bridges::cpp_uno::shared::g_moduleCount.modCnt.acquire(
214*cdf0e10cSrcweir         &bridges::cpp_uno::shared::g_moduleCount.modCnt );
215*cdf0e10cSrcweir     ((uno_ExtEnvironment *)pCppEnv)->computeObjectIdentifier
216*cdf0e10cSrcweir         = computeObjectIdentifier;
217*cdf0e10cSrcweir     ((uno_ExtEnvironment *)pCppEnv)->acquireInterface = acquireInterface;
218*cdf0e10cSrcweir     ((uno_ExtEnvironment *)pCppEnv)->releaseInterface = releaseInterface;
219*cdf0e10cSrcweir     pCppEnv->environmentDisposing = environmentDisposing;
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir void SAL_CALL uno_ext_getMapping(
223*cdf0e10cSrcweir     uno_Mapping ** ppMapping, uno_Environment * pFrom, uno_Environment * pTo)
224*cdf0e10cSrcweir     SAL_THROW_EXTERN_C()
225*cdf0e10cSrcweir {
226*cdf0e10cSrcweir 	OSL_ASSERT( ppMapping && pFrom && pTo );
227*cdf0e10cSrcweir 	if (ppMapping && pFrom && pTo && pFrom->pExtEnv && pTo->pExtEnv)
228*cdf0e10cSrcweir 	{
229*cdf0e10cSrcweir 		uno_Mapping * pMapping = 0;
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 		rtl::OUString from_envTypeName(cppu::EnvDcp::getTypeName(pFrom->pTypeName));
232*cdf0e10cSrcweir 		rtl::OUString to_envTypeName(cppu::EnvDcp::getTypeName(pTo->pTypeName));
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 		if (0 == rtl_ustr_ascii_compare(
235*cdf0e10cSrcweir 				from_envTypeName.pData->buffer,
236*cdf0e10cSrcweir                 CPPU_CURRENT_LANGUAGE_BINDING_NAME ) &&
237*cdf0e10cSrcweir             0 == rtl_ustr_ascii_compare(
238*cdf0e10cSrcweir                 to_envTypeName.pData->buffer, UNO_LB_UNO ))
239*cdf0e10cSrcweir 		{
240*cdf0e10cSrcweir 			// ref count initially 1
241*cdf0e10cSrcweir 			pMapping = bridges::cpp_uno::shared::Bridge::createMapping(
242*cdf0e10cSrcweir                 pFrom->pExtEnv, pTo->pExtEnv, sal_True );
243*cdf0e10cSrcweir 			::uno_registerMapping(
244*cdf0e10cSrcweir                 &pMapping, bridges::cpp_uno::shared::freeMapping,
245*cdf0e10cSrcweir                 (uno_Environment *)pFrom->pExtEnv,
246*cdf0e10cSrcweir                 (uno_Environment *)pTo->pExtEnv, 0 );
247*cdf0e10cSrcweir 		}
248*cdf0e10cSrcweir 		else if (0 == rtl_ustr_ascii_compare(
249*cdf0e10cSrcweir                      to_envTypeName.pData->buffer,
250*cdf0e10cSrcweir                      CPPU_CURRENT_LANGUAGE_BINDING_NAME ) &&
251*cdf0e10cSrcweir                  0 == rtl_ustr_ascii_compare(
252*cdf0e10cSrcweir                      from_envTypeName.pData->buffer, UNO_LB_UNO ))
253*cdf0e10cSrcweir 		{
254*cdf0e10cSrcweir 			// ref count initially 1
255*cdf0e10cSrcweir 			pMapping = bridges::cpp_uno::shared::Bridge::createMapping(
256*cdf0e10cSrcweir                 pTo->pExtEnv, pFrom->pExtEnv, sal_False );
257*cdf0e10cSrcweir 			::uno_registerMapping(
258*cdf0e10cSrcweir                 &pMapping, bridges::cpp_uno::shared::freeMapping,
259*cdf0e10cSrcweir                 (uno_Environment *)pFrom->pExtEnv,
260*cdf0e10cSrcweir                 (uno_Environment *)pTo->pExtEnv, 0 );
261*cdf0e10cSrcweir 		}
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir 		if (*ppMapping)
264*cdf0e10cSrcweir         {
265*cdf0e10cSrcweir 			(*(*ppMapping)->release)( *ppMapping );
266*cdf0e10cSrcweir         }
267*cdf0e10cSrcweir         if (pMapping)
268*cdf0e10cSrcweir 		*ppMapping = pMapping;
269*cdf0e10cSrcweir 	}
270*cdf0e10cSrcweir }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir }
273