1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: component.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_bridges.hxx"
34 #include "component.hxx"
36 #include "bridges/cpp_uno/shared/bridge.hxx"
38 #include "com/sun/star/uno/Reference.hxx"
39 #include "com/sun/star/uno/RuntimeException.hpp"
40 #include "com/sun/star/uno/XInterface.hpp"
41 #include "osl/diagnose.h"
42 #include "osl/mutex.hxx"
44 #include "rtl/process.h"
45 #include "rtl/unload.h"
46 #include "rtl/ustrbuf.hxx"
47 #include "rtl/ustring.h"
48 #include "rtl/ustring.hxx"
49 #include "sal/types.h"
50 #include "uno/environment.h"
51 #include "uno/lbnames.h"
52 #include "uno/mapping.h"
53 #include "cppu/EnvDcp.hxx"
55 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
57 rtl_StandardModuleCount g_moduleCount
= MODULE_COUNT_INIT
;
63 #if (defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500)) \
64 || (defined(__GNUC__) && defined(__APPLE__))
65 static ::rtl::OUString
* s_pStaticOidPart
= 0;
68 const ::rtl::OUString
& SAL_CALL
cppu_cppenv_getStaticOIdPart() SAL_THROW( () )
70 #if ! ((defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500)) \
71 || (defined(__GNUC__) && defined(__APPLE__)))
72 static ::rtl::OUString
* s_pStaticOidPart
= 0;
74 if (! s_pStaticOidPart
)
76 ::osl::MutexGuard
aGuard( ::osl::Mutex::getGlobalMutex() );
77 if (! s_pStaticOidPart
)
79 ::rtl::OUStringBuffer
aRet( 64 );
80 aRet
.appendAscii( RTL_CONSTASCII_STRINGPARAM("];") );
83 ::rtl_getGlobalProcessId( ar
);
84 for ( sal_Int32 i
= 0; i
< 16; ++i
)
86 aRet
.append( (sal_Int32
)ar
[i
], 16 );
88 #if (defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500)) \
89 || (defined(__GNUC__) && defined(__APPLE__))
90 s_pStaticOidPart
= new ::rtl::OUString( aRet
.makeStringAndClear() );
92 static ::rtl::OUString
s_aStaticOidPart(
93 aRet
.makeStringAndClear() );
94 s_pStaticOidPart
= &s_aStaticOidPart
;
98 return *s_pStaticOidPart
;
105 static void s_stub_computeObjectIdentifier(va_list * pParam
)
108 uno_ExtEnvironment
* pEnv
= va_arg(*pParam
, uno_ExtEnvironment
*);
109 rtl_uString
** ppOId
= va_arg(*pParam
, rtl_uString
**);
110 void * pInterface
= va_arg(*pParam
, void *);
113 OSL_ENSURE( pEnv
&& ppOId
&& pInterface
, "### null ptr!" );
114 if (pEnv
&& ppOId
&& pInterface
)
118 rtl_uString_release( *ppOId
);
124 ::com::sun::star::uno::Reference
<
125 ::com::sun::star::uno::XInterface
> xHome(
126 reinterpret_cast< ::com::sun::star::uno::XInterface
* >(
128 ::com::sun::star::uno::UNO_QUERY
);
129 OSL_ENSURE( xHome
.is(), "### query to XInterface failed!" );
133 ::rtl::OUStringBuffer
oid( 64 );
134 oid
.append( reinterpret_cast< sal_Int64
>(xHome
.get()), 16 );
135 oid
.append( (sal_Unicode
)';' );
136 // ;environment[context]
138 *reinterpret_cast< ::rtl::OUString
const * >(
139 &((uno_Environment
*) pEnv
)->pTypeName
) );
140 oid
.append( (sal_Unicode
)'[' );
142 reinterpret_cast< sal_Int64
>(
143 ((uno_Environment
*)pEnv
)->pContext
),
146 oid
.append( cppu_cppenv_getStaticOIdPart() );
147 ::rtl::OUString
aRet( oid
.makeStringAndClear() );
148 ::rtl_uString_acquire( *ppOId
= aRet
.pData
);
151 catch (::com::sun::star::uno::RuntimeException
&)
154 0, "### RuntimeException occured udring queryInterface()!" );
159 static void SAL_CALL
computeObjectIdentifier(
160 uno_ExtEnvironment
* pExtEnv
, rtl_uString
** ppOId
, void * pInterface
)
163 uno_Environment_invoke(&pExtEnv
->aBase
, s_stub_computeObjectIdentifier
, pExtEnv
, ppOId
, pInterface
);
166 static void s_stub_acquireInterface(va_list * pParam
)
169 /*uno_ExtEnvironment * pExtEnv = */va_arg(*pParam
, uno_ExtEnvironment
*);
170 void * pCppI
= va_arg(*pParam
, void *);
172 reinterpret_cast< ::com::sun::star::uno::XInterface
* >( pCppI
)->acquire();
175 static void SAL_CALL
acquireInterface( uno_ExtEnvironment
* pExtEnv
, void * pCppI
)
178 uno_Environment_invoke(&pExtEnv
->aBase
, s_stub_acquireInterface
, pExtEnv
, pCppI
);
181 static void s_stub_releaseInterface(va_list * pParam
)
184 /*uno_ExtEnvironment * pExtEnv = */va_arg(*pParam
, uno_ExtEnvironment
*);
185 void * pCppI
= va_arg(*pParam
, void *);
187 reinterpret_cast< ::com::sun::star::uno::XInterface
* >( pCppI
)->release();
190 static void SAL_CALL
releaseInterface( uno_ExtEnvironment
* pExtEnv
, void * pCppI
)
193 uno_Environment_invoke(&pExtEnv
->aBase
, s_stub_releaseInterface
, pExtEnv
, pCppI
);
196 static void SAL_CALL
environmentDisposing( uno_Environment
* ) SAL_THROW( () )
198 bridges::cpp_uno::shared::g_moduleCount
.modCnt
.release(
199 &bridges::cpp_uno::shared::g_moduleCount
.modCnt
);
202 sal_Bool SAL_CALL
component_canUnload(TimeValue
* pTime
) SAL_THROW_EXTERN_C() {
203 return bridges::cpp_uno::shared::g_moduleCount
.canUnload(
204 &bridges::cpp_uno::shared::g_moduleCount
, pTime
);
207 void SAL_CALL
uno_initEnvironment(uno_Environment
* pCppEnv
)
210 OSL_ENSURE( pCppEnv
->pExtEnv
, "### expected extended environment!" );
212 ::rtl_ustr_ascii_compare_WithLength(
213 pCppEnv
->pTypeName
->buffer
, rtl_str_getLength(CPPU_CURRENT_LANGUAGE_BINDING_NAME
), CPPU_CURRENT_LANGUAGE_BINDING_NAME
)
215 "### wrong environment type!" );
216 bridges::cpp_uno::shared::g_moduleCount
.modCnt
.acquire(
217 &bridges::cpp_uno::shared::g_moduleCount
.modCnt
);
218 ((uno_ExtEnvironment
*)pCppEnv
)->computeObjectIdentifier
219 = computeObjectIdentifier
;
220 ((uno_ExtEnvironment
*)pCppEnv
)->acquireInterface
= acquireInterface
;
221 ((uno_ExtEnvironment
*)pCppEnv
)->releaseInterface
= releaseInterface
;
222 pCppEnv
->environmentDisposing
= environmentDisposing
;
225 void SAL_CALL
uno_ext_getMapping(
226 uno_Mapping
** ppMapping
, uno_Environment
* pFrom
, uno_Environment
* pTo
)
229 OSL_ASSERT( ppMapping
&& pFrom
&& pTo
);
230 if (ppMapping
&& pFrom
&& pTo
&& pFrom
->pExtEnv
&& pTo
->pExtEnv
)
232 uno_Mapping
* pMapping
= 0;
234 rtl::OUString
from_envTypeName(cppu::EnvDcp::getTypeName(pFrom
->pTypeName
));
235 rtl::OUString
to_envTypeName(cppu::EnvDcp::getTypeName(pTo
->pTypeName
));
237 if (0 == rtl_ustr_ascii_compare(
238 from_envTypeName
.pData
->buffer
,
239 CPPU_CURRENT_LANGUAGE_BINDING_NAME
) &&
240 0 == rtl_ustr_ascii_compare(
241 to_envTypeName
.pData
->buffer
, UNO_LB_UNO
))
243 // ref count initially 1
244 pMapping
= bridges::cpp_uno::shared::Bridge::createMapping(
245 pFrom
->pExtEnv
, pTo
->pExtEnv
, sal_True
);
246 ::uno_registerMapping(
247 &pMapping
, bridges::cpp_uno::shared::freeMapping
,
248 (uno_Environment
*)pFrom
->pExtEnv
,
249 (uno_Environment
*)pTo
->pExtEnv
, 0 );
251 else if (0 == rtl_ustr_ascii_compare(
252 to_envTypeName
.pData
->buffer
,
253 CPPU_CURRENT_LANGUAGE_BINDING_NAME
) &&
254 0 == rtl_ustr_ascii_compare(
255 from_envTypeName
.pData
->buffer
, UNO_LB_UNO
))
257 // ref count initially 1
258 pMapping
= bridges::cpp_uno::shared::Bridge::createMapping(
259 pTo
->pExtEnv
, pFrom
->pExtEnv
, sal_False
);
260 ::uno_registerMapping(
261 &pMapping
, bridges::cpp_uno::shared::freeMapping
,
262 (uno_Environment
*)pFrom
->pExtEnv
,
263 (uno_Environment
*)pTo
->pExtEnv
, 0 );
268 (*(*ppMapping
)->release
)( *ppMapping
);
271 *ppMapping
= pMapping
;