1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_bridges.hxx"
30 
31 #include "component.hxx"
32 
33 #include "bridges/cpp_uno/shared/bridge.hxx"
34 
35 #include "com/sun/star/uno/Reference.hxx"
36 #include "com/sun/star/uno/RuntimeException.hpp"
37 #include "com/sun/star/uno/XInterface.hpp"
38 #include "osl/diagnose.h"
39 #include "osl/mutex.hxx"
40 #include "osl/time.h"
41 #include "rtl/process.h"
42 #include "rtl/unload.h"
43 #include "rtl/ustrbuf.hxx"
44 #include "rtl/ustring.h"
45 #include "rtl/ustring.hxx"
46 #include "sal/types.h"
47 #include "uno/environment.h"
48 #include "uno/lbnames.h"
49 #include "uno/mapping.h"
50 #include "cppu/EnvDcp.hxx"
51 
52 namespace bridges { namespace cpp_uno { namespace shared {
53 
54 rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
55 
56 } } }
57 
58 namespace {
59 
60 #if (defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500)) \
61     || (defined(__GNUC__) && defined(__APPLE__))
62 static ::rtl::OUString * s_pStaticOidPart = 0;
63 #endif
64 
65 const ::rtl::OUString & SAL_CALL cppu_cppenv_getStaticOIdPart() SAL_THROW( () )
66 {
67 #if ! ((defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500)) \
68     || (defined(__GNUC__) && defined(__APPLE__)))
69     static ::rtl::OUString * s_pStaticOidPart = 0;
70 #endif
71     if (! s_pStaticOidPart)
72     {
73         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
74         if (! s_pStaticOidPart)
75         {
76             ::rtl::OUStringBuffer aRet( 64 );
77             aRet.appendAscii( RTL_CONSTASCII_STRINGPARAM("];") );
78             // good guid
79             sal_uInt8 ar[16];
80             ::rtl_getGlobalProcessId( ar );
81             for ( sal_Int32 i = 0; i < 16; ++i )
82             {
83                 aRet.append( (sal_Int32)ar[i], 16 );
84             }
85 #if (defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500)) \
86     || (defined(__GNUC__) && defined(__APPLE__))
87             s_pStaticOidPart = new ::rtl::OUString( aRet.makeStringAndClear() );
88 #else
89             static ::rtl::OUString s_aStaticOidPart(
90                 aRet.makeStringAndClear() );
91             s_pStaticOidPart = &s_aStaticOidPart;
92 #endif
93         }
94     }
95     return *s_pStaticOidPart;
96 }
97 
98 }
99 
100 extern "C" {
101 
102 static void s_stub_computeObjectIdentifier(va_list * pParam)
103     SAL_THROW( () )
104 {
105 	uno_ExtEnvironment  * pEnv       = va_arg(*pParam, uno_ExtEnvironment *);
106 	rtl_uString        ** ppOId      = va_arg(*pParam, rtl_uString **);
107 	void                * pInterface = va_arg(*pParam, void *);
108 
109 
110     OSL_ENSURE( pEnv && ppOId && pInterface, "### null ptr!" );
111     if (pEnv && ppOId && pInterface)
112     {
113         if (*ppOId)
114         {
115             rtl_uString_release( *ppOId );
116             *ppOId = 0;
117         }
118 
119         try
120         {
121             ::com::sun::star::uno::Reference<
122                   ::com::sun::star::uno::XInterface > xHome(
123                       reinterpret_cast< ::com::sun::star::uno::XInterface * >(
124                           pInterface ),
125                       ::com::sun::star::uno::UNO_QUERY );
126             OSL_ENSURE( xHome.is(), "### query to XInterface failed!" );
127             if (xHome.is())
128             {
129                 // interface
130                 ::rtl::OUStringBuffer oid( 64 );
131                 oid.append( reinterpret_cast< sal_Int64 >(xHome.get()), 16 );
132                 oid.append( (sal_Unicode)';' );
133                 // ;environment[context]
134                 oid.append(
135                     *reinterpret_cast< ::rtl::OUString const * >(
136                         &((uno_Environment *) pEnv)->pTypeName ) );
137                 oid.append( (sal_Unicode)'[' );
138                 oid.append(
139                     reinterpret_cast< sal_Int64 >(
140                         ((uno_Environment *)pEnv)->pContext),
141                     16 );
142                 // ];good guid
143                 oid.append( cppu_cppenv_getStaticOIdPart() );
144                 ::rtl::OUString aRet( oid.makeStringAndClear() );
145                 ::rtl_uString_acquire( *ppOId = aRet.pData );
146             }
147         }
148         catch (::com::sun::star::uno::RuntimeException &)
149         {
150             OSL_ENSURE(
151                 0, "### RuntimeException occured udring queryInterface()!" );
152         }
153     }
154 }
155 
156 static void SAL_CALL computeObjectIdentifier(
157     uno_ExtEnvironment * pExtEnv, rtl_uString ** ppOId, void * pInterface )
158     SAL_THROW( () )
159 {
160 	uno_Environment_invoke(&pExtEnv->aBase, s_stub_computeObjectIdentifier, pExtEnv, ppOId, pInterface);
161 }
162 
163 static void s_stub_acquireInterface(va_list * pParam)
164     SAL_THROW( () )
165 {
166 	/*uno_ExtEnvironment * pExtEnv = */va_arg(*pParam, uno_ExtEnvironment *);
167 	void               * pCppI   = va_arg(*pParam, void *);
168 
169     reinterpret_cast< ::com::sun::star::uno::XInterface * >( pCppI )->acquire();
170 }
171 
172 static void SAL_CALL acquireInterface( uno_ExtEnvironment * pExtEnv, void * pCppI )
173     SAL_THROW( () )
174 {
175 	uno_Environment_invoke(&pExtEnv->aBase, s_stub_acquireInterface, pExtEnv, pCppI);
176 }
177 
178 static void s_stub_releaseInterface(va_list * pParam)
179     SAL_THROW( () )
180 {
181 	/*uno_ExtEnvironment * pExtEnv = */va_arg(*pParam, uno_ExtEnvironment *);
182 	void               * pCppI   = va_arg(*pParam, void *);
183 
184     reinterpret_cast< ::com::sun::star::uno::XInterface * >( pCppI )->release();
185 }
186 
187 static void SAL_CALL releaseInterface( uno_ExtEnvironment * pExtEnv, void * pCppI )
188     SAL_THROW( () )
189 {
190 	uno_Environment_invoke(&pExtEnv->aBase, s_stub_releaseInterface, pExtEnv, pCppI);
191 }
192 
193 static void SAL_CALL environmentDisposing( uno_Environment * ) SAL_THROW( () )
194 {
195     bridges::cpp_uno::shared::g_moduleCount.modCnt.release(
196         &bridges::cpp_uno::shared::g_moduleCount.modCnt );
197 }
198 
199 sal_Bool SAL_CALL component_canUnload(TimeValue * pTime) SAL_THROW_EXTERN_C() {
200     return bridges::cpp_uno::shared::g_moduleCount.canUnload(
201         &bridges::cpp_uno::shared::g_moduleCount, pTime);
202 }
203 
204 void SAL_CALL uno_initEnvironment(uno_Environment * pCppEnv)
205     SAL_THROW_EXTERN_C()
206 {
207     OSL_ENSURE( pCppEnv->pExtEnv, "### expected extended environment!" );
208     OSL_ENSURE(
209 		::rtl_ustr_ascii_compare_WithLength(
210              pCppEnv->pTypeName->buffer, rtl_str_getLength(CPPU_CURRENT_LANGUAGE_BINDING_NAME), CPPU_CURRENT_LANGUAGE_BINDING_NAME )
211         == 0,
212         "### wrong environment type!" );
213     bridges::cpp_uno::shared::g_moduleCount.modCnt.acquire(
214         &bridges::cpp_uno::shared::g_moduleCount.modCnt );
215     ((uno_ExtEnvironment *)pCppEnv)->computeObjectIdentifier
216         = computeObjectIdentifier;
217     ((uno_ExtEnvironment *)pCppEnv)->acquireInterface = acquireInterface;
218     ((uno_ExtEnvironment *)pCppEnv)->releaseInterface = releaseInterface;
219     pCppEnv->environmentDisposing = environmentDisposing;
220 }
221 
222 void SAL_CALL uno_ext_getMapping(
223     uno_Mapping ** ppMapping, uno_Environment * pFrom, uno_Environment * pTo)
224     SAL_THROW_EXTERN_C()
225 {
226 	OSL_ASSERT( ppMapping && pFrom && pTo );
227 	if (ppMapping && pFrom && pTo && pFrom->pExtEnv && pTo->pExtEnv)
228 	{
229 		uno_Mapping * pMapping = 0;
230 
231 		rtl::OUString from_envTypeName(cppu::EnvDcp::getTypeName(pFrom->pTypeName));
232 		rtl::OUString to_envTypeName(cppu::EnvDcp::getTypeName(pTo->pTypeName));
233 
234 		if (0 == rtl_ustr_ascii_compare(
235 				from_envTypeName.pData->buffer,
236                 CPPU_CURRENT_LANGUAGE_BINDING_NAME ) &&
237             0 == rtl_ustr_ascii_compare(
238                 to_envTypeName.pData->buffer, UNO_LB_UNO ))
239 		{
240 			// ref count initially 1
241 			pMapping = bridges::cpp_uno::shared::Bridge::createMapping(
242                 pFrom->pExtEnv, pTo->pExtEnv, sal_True );
243 			::uno_registerMapping(
244                 &pMapping, bridges::cpp_uno::shared::freeMapping,
245                 (uno_Environment *)pFrom->pExtEnv,
246                 (uno_Environment *)pTo->pExtEnv, 0 );
247 		}
248 		else if (0 == rtl_ustr_ascii_compare(
249                      to_envTypeName.pData->buffer,
250                      CPPU_CURRENT_LANGUAGE_BINDING_NAME ) &&
251                  0 == rtl_ustr_ascii_compare(
252                      from_envTypeName.pData->buffer, UNO_LB_UNO ))
253 		{
254 			// ref count initially 1
255 			pMapping = bridges::cpp_uno::shared::Bridge::createMapping(
256                 pTo->pExtEnv, pFrom->pExtEnv, sal_False );
257 			::uno_registerMapping(
258                 &pMapping, bridges::cpp_uno::shared::freeMapping,
259                 (uno_Environment *)pFrom->pExtEnv,
260                 (uno_Environment *)pTo->pExtEnv, 0 );
261 		}
262 
263 		if (*ppMapping)
264         {
265 			(*(*ppMapping)->release)( *ppMapping );
266         }
267         if (pMapping)
268 		*ppMapping = pMapping;
269 	}
270 }
271 
272 }
273