1129fa3d1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3129fa3d1SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4129fa3d1SAndrew Rist * or more contributor license agreements. See the NOTICE file
5129fa3d1SAndrew Rist * distributed with this work for additional information
6129fa3d1SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7129fa3d1SAndrew Rist * to you under the Apache License, Version 2.0 (the
8129fa3d1SAndrew Rist * "License"); you may not use this file except in compliance
9129fa3d1SAndrew Rist * with the License. You may obtain a copy of the License at
10129fa3d1SAndrew Rist *
11129fa3d1SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12129fa3d1SAndrew Rist *
13129fa3d1SAndrew Rist * Unless required by applicable law or agreed to in writing,
14129fa3d1SAndrew Rist * software distributed under the License is distributed on an
15129fa3d1SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16129fa3d1SAndrew Rist * KIND, either express or implied. See the License for the
17129fa3d1SAndrew Rist * specific language governing permissions and limitations
18129fa3d1SAndrew Rist * under the License.
19129fa3d1SAndrew Rist *
20129fa3d1SAndrew Rist *************************************************************/
21129fa3d1SAndrew Rist
22129fa3d1SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cppu.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "IdentityMapping.hxx"
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <hash_map>
30cdf0e10cSrcweir #include <set>
31cdf0e10cSrcweir #include <algorithm>
32cdf0e10cSrcweir
33cdf0e10cSrcweir #include "rtl/unload.h"
34cdf0e10cSrcweir #include "rtl/ustring.hxx"
35cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
36cdf0e10cSrcweir #include "osl/module.h"
37cdf0e10cSrcweir #include "osl/diagnose.h"
38cdf0e10cSrcweir #include "osl/mutex.hxx"
39cdf0e10cSrcweir #include "osl/interlck.h"
40cdf0e10cSrcweir
41cdf0e10cSrcweir #include "uno/dispatcher.h"
42cdf0e10cSrcweir #include "uno/mapping.h"
43cdf0e10cSrcweir #include "uno/lbnames.h"
44cdf0e10cSrcweir #include "uno/environment.hxx"
45cdf0e10cSrcweir
46cdf0e10cSrcweir #include "typelib/typedescription.h"
47cdf0e10cSrcweir
48cdf0e10cSrcweir #include "cppu/EnvDcp.hxx"
49cdf0e10cSrcweir #include "cascade_mapping.hxx"
50cdf0e10cSrcweir #include "IdentityMapping.hxx"
51cdf0e10cSrcweir #include "loadmodule.hxx"
52cdf0e10cSrcweir
53cdf0e10cSrcweir using namespace std;
54cdf0e10cSrcweir using namespace osl;
55cdf0e10cSrcweir using namespace rtl;
56cdf0e10cSrcweir using namespace com::sun::star::uno;
57cdf0e10cSrcweir
58cdf0e10cSrcweir
59cdf0e10cSrcweir namespace cppu
60cdf0e10cSrcweir {
61cdf0e10cSrcweir
62cdf0e10cSrcweir class Mapping
63cdf0e10cSrcweir {
64cdf0e10cSrcweir uno_Mapping * _pMapping;
65cdf0e10cSrcweir
66cdf0e10cSrcweir public:
67cdf0e10cSrcweir inline Mapping( uno_Mapping * pMapping = 0 ) SAL_THROW( () );
68cdf0e10cSrcweir inline Mapping( const Mapping & rMapping ) SAL_THROW( () );
69cdf0e10cSrcweir inline ~Mapping() SAL_THROW( () );
70cdf0e10cSrcweir inline Mapping & SAL_CALL operator = ( uno_Mapping * pMapping ) SAL_THROW( () );
operator =(const Mapping & rMapping)71cdf0e10cSrcweir inline Mapping & SAL_CALL operator = ( const Mapping & rMapping ) SAL_THROW( () )
72cdf0e10cSrcweir { return operator = ( rMapping._pMapping ); }
get() const73cdf0e10cSrcweir inline uno_Mapping * SAL_CALL get() const SAL_THROW( () )
74cdf0e10cSrcweir { return _pMapping; }
is() const75cdf0e10cSrcweir inline sal_Bool SAL_CALL is() const SAL_THROW( () )
76cdf0e10cSrcweir { return (_pMapping != 0); }
77cdf0e10cSrcweir };
78cdf0e10cSrcweir //__________________________________________________________________________________________________
Mapping(uno_Mapping * pMapping)79cdf0e10cSrcweir inline Mapping::Mapping( uno_Mapping * pMapping ) SAL_THROW( () )
80cdf0e10cSrcweir : _pMapping( pMapping )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir if (_pMapping)
83cdf0e10cSrcweir (*_pMapping->acquire)( _pMapping );
84cdf0e10cSrcweir }
85cdf0e10cSrcweir //__________________________________________________________________________________________________
Mapping(const Mapping & rMapping)86cdf0e10cSrcweir inline Mapping::Mapping( const Mapping & rMapping ) SAL_THROW( () )
87cdf0e10cSrcweir : _pMapping( rMapping._pMapping )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir if (_pMapping)
90cdf0e10cSrcweir (*_pMapping->acquire)( _pMapping );
91cdf0e10cSrcweir }
92cdf0e10cSrcweir //__________________________________________________________________________________________________
~Mapping()93cdf0e10cSrcweir inline Mapping::~Mapping() SAL_THROW( () )
94cdf0e10cSrcweir {
95cdf0e10cSrcweir if (_pMapping)
96cdf0e10cSrcweir (*_pMapping->release)( _pMapping );
97cdf0e10cSrcweir }
98cdf0e10cSrcweir //__________________________________________________________________________________________________
operator =(uno_Mapping * pMapping)99cdf0e10cSrcweir inline Mapping & Mapping::operator = ( uno_Mapping * pMapping ) SAL_THROW( () )
100cdf0e10cSrcweir {
101cdf0e10cSrcweir if (pMapping)
102cdf0e10cSrcweir (*pMapping->acquire)( pMapping );
103cdf0e10cSrcweir if (_pMapping)
104cdf0e10cSrcweir (*_pMapping->release)( _pMapping );
105cdf0e10cSrcweir _pMapping = pMapping;
106cdf0e10cSrcweir return *this;
107cdf0e10cSrcweir }
108cdf0e10cSrcweir
109cdf0e10cSrcweir //==================================================================================================
110cdf0e10cSrcweir struct MappingEntry
111cdf0e10cSrcweir {
112cdf0e10cSrcweir sal_Int32 nRef;
113cdf0e10cSrcweir uno_Mapping * pMapping;
114cdf0e10cSrcweir uno_freeMappingFunc freeMapping;
115cdf0e10cSrcweir OUString aMappingName;
116cdf0e10cSrcweir
MappingEntrycppu::MappingEntry117cdf0e10cSrcweir MappingEntry(
118cdf0e10cSrcweir uno_Mapping * pMapping_, uno_freeMappingFunc freeMapping_,
119cdf0e10cSrcweir const OUString & rMappingName_ )
120cdf0e10cSrcweir SAL_THROW( () )
121cdf0e10cSrcweir : nRef( 1 )
122cdf0e10cSrcweir , pMapping( pMapping_ )
123cdf0e10cSrcweir , freeMapping( freeMapping_ )
124cdf0e10cSrcweir , aMappingName( rMappingName_ )
125cdf0e10cSrcweir {}
126cdf0e10cSrcweir };
127cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
128cdf0e10cSrcweir struct FctOUStringHash : public unary_function< const OUString &, size_t >
129cdf0e10cSrcweir {
operator ()cppu::FctOUStringHash130cdf0e10cSrcweir size_t operator()( const OUString & rKey ) const SAL_THROW( () )
131cdf0e10cSrcweir { return (size_t)rKey.hashCode(); }
132cdf0e10cSrcweir };
133cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
134cdf0e10cSrcweir struct FctPtrHash : public unary_function< uno_Mapping *, size_t >
135cdf0e10cSrcweir {
operator ()cppu::FctPtrHash136cdf0e10cSrcweir size_t operator()( uno_Mapping * pKey ) const SAL_THROW( () )
137cdf0e10cSrcweir { return (size_t)pKey; }
138cdf0e10cSrcweir };
139cdf0e10cSrcweir
140cdf0e10cSrcweir typedef hash_map<
141cdf0e10cSrcweir OUString, MappingEntry *, FctOUStringHash, equal_to< OUString > > t_OUString2Entry;
142cdf0e10cSrcweir typedef hash_map<
143cdf0e10cSrcweir uno_Mapping *, MappingEntry *, FctPtrHash, equal_to< uno_Mapping * > > t_Mapping2Entry;
144cdf0e10cSrcweir
145cdf0e10cSrcweir typedef set< uno_getMappingFunc > t_CallbackSet;
146cdf0e10cSrcweir typedef set< OUString > t_OUStringSet;
147cdf0e10cSrcweir
148cdf0e10cSrcweir //==================================================================================================
149cdf0e10cSrcweir struct MappingsData
150cdf0e10cSrcweir {
151cdf0e10cSrcweir Mutex aMappingsMutex;
152cdf0e10cSrcweir t_OUString2Entry aName2Entry;
153cdf0e10cSrcweir t_Mapping2Entry aMapping2Entry;
154cdf0e10cSrcweir
155cdf0e10cSrcweir Mutex aCallbacksMutex;
156cdf0e10cSrcweir t_CallbackSet aCallbacks;
157cdf0e10cSrcweir
158cdf0e10cSrcweir Mutex aNegativeLibsMutex;
159cdf0e10cSrcweir t_OUStringSet aNegativeLibs;
160cdf0e10cSrcweir };
161cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
getMappingsData()162cdf0e10cSrcweir static MappingsData & getMappingsData() SAL_THROW( () )
163cdf0e10cSrcweir {
164cdf0e10cSrcweir static MappingsData * s_p = 0;
165cdf0e10cSrcweir if (! s_p)
166cdf0e10cSrcweir {
167cdf0e10cSrcweir MutexGuard aGuard( Mutex::getGlobalMutex() );
168cdf0e10cSrcweir if (! s_p)
169cdf0e10cSrcweir {
170cdf0e10cSrcweir //TODO This memory is leaked; see #i63473# for when this should be
171cdf0e10cSrcweir // changed again:
172cdf0e10cSrcweir s_p = new MappingsData;
173cdf0e10cSrcweir }
174cdf0e10cSrcweir }
175cdf0e10cSrcweir return *s_p;
176cdf0e10cSrcweir }
177cdf0e10cSrcweir
178cdf0e10cSrcweir /**
179cdf0e10cSrcweir * This class mediates two different mapping via uno, e.g. form any language to uno,
180cdf0e10cSrcweir * then from uno to any other language.
181cdf0e10cSrcweir */
182cdf0e10cSrcweir struct uno_Mediate_Mapping : public uno_Mapping
183cdf0e10cSrcweir {
184cdf0e10cSrcweir sal_Int32 nRef;
185cdf0e10cSrcweir
186cdf0e10cSrcweir Environment aFrom;
187cdf0e10cSrcweir Environment aTo;
188cdf0e10cSrcweir
189cdf0e10cSrcweir Mapping aFrom2Uno;
190cdf0e10cSrcweir Mapping aUno2To;
191cdf0e10cSrcweir
192cdf0e10cSrcweir OUString aAddPurpose;
193cdf0e10cSrcweir
194cdf0e10cSrcweir uno_Mediate_Mapping(
195cdf0e10cSrcweir const Environment & rFrom_, const Environment & rTo_,
196cdf0e10cSrcweir const Mapping & rFrom2Uno_, const Mapping & rUno2To_,
197cdf0e10cSrcweir const OUString & rAddPurpose )
198cdf0e10cSrcweir SAL_THROW( () );
199cdf0e10cSrcweir };
200cdf0e10cSrcweir extern "C"
201cdf0e10cSrcweir {
202cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
mediate_free(uno_Mapping * pMapping)203cdf0e10cSrcweir static void SAL_CALL mediate_free( uno_Mapping * pMapping )
204cdf0e10cSrcweir SAL_THROW( () )
205cdf0e10cSrcweir {
206cdf0e10cSrcweir delete static_cast< uno_Mediate_Mapping * >( pMapping );
207cdf0e10cSrcweir }
208cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
mediate_acquire(uno_Mapping * pMapping)209cdf0e10cSrcweir static void SAL_CALL mediate_acquire( uno_Mapping * pMapping )
210cdf0e10cSrcweir SAL_THROW( () )
211cdf0e10cSrcweir {
212cdf0e10cSrcweir if (1 == ::osl_incrementInterlockedCount(
213cdf0e10cSrcweir & static_cast< uno_Mediate_Mapping * >( pMapping )->nRef ))
214cdf0e10cSrcweir {
215cdf0e10cSrcweir uno_registerMapping(
216cdf0e10cSrcweir &pMapping, mediate_free,
217cdf0e10cSrcweir static_cast< uno_Mediate_Mapping * >( pMapping )->aFrom.get(),
218cdf0e10cSrcweir static_cast< uno_Mediate_Mapping * >( pMapping )->aTo.get(),
219cdf0e10cSrcweir static_cast< uno_Mediate_Mapping * >( pMapping )->aAddPurpose.pData );
220cdf0e10cSrcweir }
221cdf0e10cSrcweir }
222cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
mediate_release(uno_Mapping * pMapping)223cdf0e10cSrcweir static void SAL_CALL mediate_release( uno_Mapping * pMapping )
224cdf0e10cSrcweir SAL_THROW( () )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir if (! ::osl_decrementInterlockedCount(
227cdf0e10cSrcweir & static_cast< uno_Mediate_Mapping * >( pMapping )->nRef ))
228cdf0e10cSrcweir {
229cdf0e10cSrcweir uno_revokeMapping( pMapping );
230cdf0e10cSrcweir }
231cdf0e10cSrcweir }
232cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
mediate_mapInterface(uno_Mapping * pMapping,void ** ppOut,void * pInterface,typelib_InterfaceTypeDescription * pInterfaceTypeDescr)233cdf0e10cSrcweir static void SAL_CALL mediate_mapInterface(
234cdf0e10cSrcweir uno_Mapping * pMapping,
235cdf0e10cSrcweir void ** ppOut, void * pInterface,
236cdf0e10cSrcweir typelib_InterfaceTypeDescription * pInterfaceTypeDescr )
237cdf0e10cSrcweir SAL_THROW( () )
238cdf0e10cSrcweir {
239cdf0e10cSrcweir OSL_ENSURE( pMapping && ppOut, "### null ptr!" );
240cdf0e10cSrcweir if (pMapping && ppOut)
241cdf0e10cSrcweir {
242cdf0e10cSrcweir uno_Mediate_Mapping * that = static_cast< uno_Mediate_Mapping * >( pMapping );
243cdf0e10cSrcweir uno_Mapping * pFrom2Uno = that->aFrom2Uno.get();
244cdf0e10cSrcweir
245cdf0e10cSrcweir uno_Interface * pUnoI = 0;
246cdf0e10cSrcweir (*pFrom2Uno->mapInterface)( pFrom2Uno, (void **) &pUnoI, pInterface, pInterfaceTypeDescr );
247cdf0e10cSrcweir if (0 == pUnoI)
248cdf0e10cSrcweir {
249cdf0e10cSrcweir void * pOut = *ppOut;
250cdf0e10cSrcweir if (0 != pOut)
251cdf0e10cSrcweir {
252cdf0e10cSrcweir uno_ExtEnvironment * pTo = that->aTo.get()->pExtEnv;
253cdf0e10cSrcweir OSL_ENSURE( 0 != pTo, "### cannot release out interface: leaking!" );
254cdf0e10cSrcweir if (0 != pTo)
255cdf0e10cSrcweir (*pTo->releaseInterface)( pTo, pOut );
256*07a3d7f1SPedro Giffuni *ppOut = 0; // set to 0 anyway, because mapping was not successful!
257cdf0e10cSrcweir }
258cdf0e10cSrcweir }
259cdf0e10cSrcweir else
260cdf0e10cSrcweir {
261cdf0e10cSrcweir uno_Mapping * pUno2To = that->aUno2To.get();
262cdf0e10cSrcweir (*pUno2To->mapInterface)( pUno2To, ppOut, pUnoI, pInterfaceTypeDescr );
263cdf0e10cSrcweir (*pUnoI->release)( pUnoI );
264cdf0e10cSrcweir }
265cdf0e10cSrcweir }
266cdf0e10cSrcweir }
267cdf0e10cSrcweir }
268cdf0e10cSrcweir //__________________________________________________________________________________________________
uno_Mediate_Mapping(const Environment & rFrom_,const Environment & rTo_,const Mapping & rFrom2Uno_,const Mapping & rUno2To_,const OUString & rAddPurpose_)269cdf0e10cSrcweir uno_Mediate_Mapping::uno_Mediate_Mapping(
270cdf0e10cSrcweir const Environment & rFrom_, const Environment & rTo_,
271cdf0e10cSrcweir const Mapping & rFrom2Uno_, const Mapping & rUno2To_,
272cdf0e10cSrcweir const OUString & rAddPurpose_ )
273cdf0e10cSrcweir SAL_THROW( () )
274cdf0e10cSrcweir : nRef( 1 )
275cdf0e10cSrcweir , aFrom( rFrom_ )
276cdf0e10cSrcweir , aTo( rTo_ )
277cdf0e10cSrcweir , aFrom2Uno( rFrom2Uno_ )
278cdf0e10cSrcweir , aUno2To( rUno2To_ )
279cdf0e10cSrcweir , aAddPurpose( rAddPurpose_ )
280cdf0e10cSrcweir {
281cdf0e10cSrcweir uno_Mapping::acquire = mediate_acquire;
282cdf0e10cSrcweir uno_Mapping::release = mediate_release;
283cdf0e10cSrcweir uno_Mapping::mapInterface = mediate_mapInterface;
284cdf0e10cSrcweir }
285cdf0e10cSrcweir
286cdf0e10cSrcweir //==================================================================================================
getMappingName(const Environment & rFrom,const Environment & rTo,const OUString & rAddPurpose)287cdf0e10cSrcweir static inline OUString getMappingName(
288cdf0e10cSrcweir const Environment & rFrom, const Environment & rTo, const OUString & rAddPurpose )
289cdf0e10cSrcweir SAL_THROW( () )
290cdf0e10cSrcweir {
291cdf0e10cSrcweir OUStringBuffer aKey( 64 );
292cdf0e10cSrcweir aKey.append( rAddPurpose );
293cdf0e10cSrcweir aKey.append( (sal_Unicode)';' );
294cdf0e10cSrcweir aKey.append( rFrom.getTypeName() );
295cdf0e10cSrcweir aKey.append( (sal_Unicode)'[' );
296cdf0e10cSrcweir aKey.append( reinterpret_cast< sal_IntPtr >(rFrom.get()), 16 );
297cdf0e10cSrcweir aKey.appendAscii( RTL_CONSTASCII_STRINGPARAM("];") );
298cdf0e10cSrcweir aKey.append( rTo.getTypeName() );
299cdf0e10cSrcweir aKey.append( (sal_Unicode)'[' );
300cdf0e10cSrcweir aKey.append( reinterpret_cast< sal_IntPtr >(rTo.get()), 16 );
301cdf0e10cSrcweir aKey.append( (sal_Unicode)']' );
302cdf0e10cSrcweir return aKey.makeStringAndClear();
303cdf0e10cSrcweir }
304cdf0e10cSrcweir //==================================================================================================
getBridgeName(const Environment & rFrom,const Environment & rTo,const OUString & rAddPurpose)305cdf0e10cSrcweir static inline OUString getBridgeName(
306cdf0e10cSrcweir const Environment & rFrom, const Environment & rTo, const OUString & rAddPurpose )
307cdf0e10cSrcweir SAL_THROW( () )
308cdf0e10cSrcweir {
309cdf0e10cSrcweir OUStringBuffer aBridgeName( 16 );
310cdf0e10cSrcweir if (rAddPurpose.getLength())
311cdf0e10cSrcweir {
312cdf0e10cSrcweir aBridgeName.append( rAddPurpose );
313cdf0e10cSrcweir aBridgeName.append( (sal_Unicode)'_' );
314cdf0e10cSrcweir }
315cdf0e10cSrcweir aBridgeName.append( EnvDcp::getTypeName(rFrom.getTypeName()) );
316cdf0e10cSrcweir aBridgeName.append( (sal_Unicode)'_' );
317cdf0e10cSrcweir aBridgeName.append( EnvDcp::getTypeName(rTo.getTypeName()) );
318cdf0e10cSrcweir return aBridgeName.makeStringAndClear();
319cdf0e10cSrcweir }
320cdf0e10cSrcweir //==================================================================================================
setNegativeBridge(const OUString & rBridgeName)321cdf0e10cSrcweir static inline void setNegativeBridge( const OUString & rBridgeName )
322cdf0e10cSrcweir SAL_THROW( () )
323cdf0e10cSrcweir {
324cdf0e10cSrcweir MappingsData & rData = getMappingsData();
325cdf0e10cSrcweir MutexGuard aGuard( rData.aNegativeLibsMutex );
326cdf0e10cSrcweir rData.aNegativeLibs.insert( rBridgeName );
327cdf0e10cSrcweir }
328cdf0e10cSrcweir //==================================================================================================
loadModule(const OUString & rBridgeName)329cdf0e10cSrcweir static inline oslModule loadModule( const OUString & rBridgeName )
330cdf0e10cSrcweir SAL_THROW( () )
331cdf0e10cSrcweir {
332cdf0e10cSrcweir sal_Bool bNeg;
333cdf0e10cSrcweir {
334cdf0e10cSrcweir MappingsData & rData = getMappingsData();
335cdf0e10cSrcweir MutexGuard aGuard( rData.aNegativeLibsMutex );
336cdf0e10cSrcweir const t_OUStringSet::const_iterator iFind( rData.aNegativeLibs.find( rBridgeName ) );
337cdf0e10cSrcweir bNeg = (iFind != rData.aNegativeLibs.end());
338cdf0e10cSrcweir }
339cdf0e10cSrcweir
340cdf0e10cSrcweir if (! bNeg)
341cdf0e10cSrcweir {
342cdf0e10cSrcweir oslModule hModule = cppu::detail::loadModule( rBridgeName );
343cdf0e10cSrcweir
344cdf0e10cSrcweir if (hModule)
345cdf0e10cSrcweir return hModule;
346cdf0e10cSrcweir
347cdf0e10cSrcweir setNegativeBridge( rBridgeName ); // no load again
348cdf0e10cSrcweir }
349cdf0e10cSrcweir return 0;
350cdf0e10cSrcweir }
351cdf0e10cSrcweir //==================================================================================================
loadExternalMapping(const Environment & rFrom,const Environment & rTo,const OUString & rAddPurpose)352cdf0e10cSrcweir static Mapping loadExternalMapping(
353cdf0e10cSrcweir const Environment & rFrom, const Environment & rTo, const OUString & rAddPurpose )
354cdf0e10cSrcweir SAL_THROW( () )
355cdf0e10cSrcweir {
356cdf0e10cSrcweir OSL_ASSERT( rFrom.is() && rTo.is() );
357cdf0e10cSrcweir if (rFrom.is() && rTo.is())
358cdf0e10cSrcweir {
359cdf0e10cSrcweir // find proper lib
360cdf0e10cSrcweir oslModule hModule = 0;
361cdf0e10cSrcweir OUString aName;
362cdf0e10cSrcweir
363cdf0e10cSrcweir if (EnvDcp::getTypeName(rFrom.getTypeName()).equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(UNO_LB_UNO) ))
364cdf0e10cSrcweir hModule = loadModule( aName = getBridgeName( rTo, rFrom, rAddPurpose ) );
365cdf0e10cSrcweir if (! hModule)
366cdf0e10cSrcweir hModule = loadModule( aName = getBridgeName( rFrom, rTo, rAddPurpose ) );
367cdf0e10cSrcweir if (! hModule)
368cdf0e10cSrcweir hModule = loadModule( aName = getBridgeName( rTo, rFrom, rAddPurpose ) );
369cdf0e10cSrcweir
370cdf0e10cSrcweir if (hModule)
371cdf0e10cSrcweir {
372cdf0e10cSrcweir OUString aSymbolName( RTL_CONSTASCII_USTRINGPARAM(UNO_EXT_GETMAPPING) );
373cdf0e10cSrcweir uno_ext_getMappingFunc fpGetMapFunc =
374cdf0e10cSrcweir (uno_ext_getMappingFunc)::osl_getFunctionSymbol(
375cdf0e10cSrcweir hModule, aSymbolName.pData );
376cdf0e10cSrcweir
377cdf0e10cSrcweir if (fpGetMapFunc)
378cdf0e10cSrcweir {
379cdf0e10cSrcweir Mapping aExt;
380cdf0e10cSrcweir (*fpGetMapFunc)( (uno_Mapping **)&aExt, rFrom.get(), rTo.get() );
381cdf0e10cSrcweir OSL_ASSERT( aExt.is() );
382cdf0e10cSrcweir if (aExt.is())
383cdf0e10cSrcweir {
384cdf0e10cSrcweir ::rtl_registerModuleForUnloading( hModule );
385cdf0e10cSrcweir return aExt;
386cdf0e10cSrcweir }
387cdf0e10cSrcweir }
388cdf0e10cSrcweir ::osl_unloadModule( hModule );
389cdf0e10cSrcweir setNegativeBridge( aName );
390cdf0e10cSrcweir }
391cdf0e10cSrcweir }
392cdf0e10cSrcweir return Mapping();
393cdf0e10cSrcweir }
394cdf0e10cSrcweir
395cdf0e10cSrcweir //==================================================================================================
getDirectMapping(const Environment & rFrom,const Environment & rTo,const OUString & rAddPurpose=OUString ())396cdf0e10cSrcweir static Mapping getDirectMapping(
397cdf0e10cSrcweir const Environment & rFrom, const Environment & rTo, const OUString & rAddPurpose = OUString() )
398cdf0e10cSrcweir SAL_THROW( () )
399cdf0e10cSrcweir {
400cdf0e10cSrcweir OSL_ASSERT( rFrom.is() && rTo.is() );
401cdf0e10cSrcweir if (rFrom.is() && rTo.is())
402cdf0e10cSrcweir {
403cdf0e10cSrcweir MappingsData & rData = getMappingsData();
404cdf0e10cSrcweir ClearableMutexGuard aGuard( rData.aMappingsMutex );
405cdf0e10cSrcweir
406cdf0e10cSrcweir // try to find registered mapping
407cdf0e10cSrcweir const t_OUString2Entry::const_iterator iFind( rData.aName2Entry.find(
408cdf0e10cSrcweir getMappingName( rFrom, rTo, rAddPurpose ) ) );
409cdf0e10cSrcweir
410cdf0e10cSrcweir if (iFind == rData.aName2Entry.end())
411cdf0e10cSrcweir {
412cdf0e10cSrcweir aGuard.clear();
413cdf0e10cSrcweir return loadExternalMapping( rFrom, rTo, rAddPurpose );
414cdf0e10cSrcweir }
415cdf0e10cSrcweir else
416cdf0e10cSrcweir {
417cdf0e10cSrcweir return Mapping( (*iFind).second->pMapping );
418cdf0e10cSrcweir }
419cdf0e10cSrcweir }
420cdf0e10cSrcweir return Mapping();
421cdf0e10cSrcweir }
422cdf0e10cSrcweir
423cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
createMediateMapping(const Environment & rFrom,const Environment & rTo,const Mapping & rFrom2Uno,const Mapping & rUno2To,const OUString & rAddPurpose)424cdf0e10cSrcweir static inline Mapping createMediateMapping(
425cdf0e10cSrcweir const Environment & rFrom, const Environment & rTo,
426cdf0e10cSrcweir const Mapping & rFrom2Uno, const Mapping & rUno2To,
427cdf0e10cSrcweir const OUString & rAddPurpose )
428cdf0e10cSrcweir SAL_THROW( () )
429cdf0e10cSrcweir {
430cdf0e10cSrcweir uno_Mapping * pRet = new uno_Mediate_Mapping(
431cdf0e10cSrcweir rFrom, rTo, rFrom2Uno, rUno2To, rAddPurpose ); // ref count initially 1
432cdf0e10cSrcweir uno_registerMapping(
433cdf0e10cSrcweir &pRet, mediate_free, rFrom.get(), rTo.get(), rAddPurpose.pData );
434cdf0e10cSrcweir Mapping aRet( pRet );
435cdf0e10cSrcweir (*pRet->release)( pRet );
436cdf0e10cSrcweir return aRet;
437cdf0e10cSrcweir }
438cdf0e10cSrcweir //==================================================================================================
getMediateMapping(const Environment & rFrom,const Environment & rTo,const OUString & rAddPurpose)439cdf0e10cSrcweir static Mapping getMediateMapping(
440cdf0e10cSrcweir const Environment & rFrom, const Environment & rTo, const OUString & rAddPurpose )
441cdf0e10cSrcweir SAL_THROW( () )
442cdf0e10cSrcweir {
443cdf0e10cSrcweir Environment aUno;
444cdf0e10cSrcweir Mapping aUno2To;
445cdf0e10cSrcweir
446cdf0e10cSrcweir // backwards: from dest to source of mapping chain
447cdf0e10cSrcweir
448cdf0e10cSrcweir // connect to uno
449cdf0e10cSrcweir OUString aUnoEnvTypeName( RTL_CONSTASCII_USTRINGPARAM(UNO_LB_UNO) );
450cdf0e10cSrcweir if (rTo.getTypeName() == aUnoEnvTypeName) // to is uno
451cdf0e10cSrcweir {
452cdf0e10cSrcweir aUno = rTo;
453cdf0e10cSrcweir // no Uno2To mapping necessary
454cdf0e10cSrcweir }
455cdf0e10cSrcweir else
456cdf0e10cSrcweir {
457cdf0e10cSrcweir // get registered uno env
458cdf0e10cSrcweir ::uno_getEnvironment( (uno_Environment **)&aUno, aUnoEnvTypeName.pData, 0 );
459cdf0e10cSrcweir
460cdf0e10cSrcweir aUno2To = getDirectMapping( aUno, rTo );
461cdf0e10cSrcweir // : uno <-> to
462cdf0e10cSrcweir if (! aUno2To.is())
463cdf0e10cSrcweir return Mapping();
464cdf0e10cSrcweir }
465cdf0e10cSrcweir
466cdf0e10cSrcweir // connect to uno
467cdf0e10cSrcweir if (rAddPurpose.getLength()) // insert purpose mapping between new ano_uno <-> uno
468cdf0e10cSrcweir {
469cdf0e10cSrcweir // create anonymous uno env
470cdf0e10cSrcweir Environment aAnUno;
471cdf0e10cSrcweir ::uno_createEnvironment( (uno_Environment **)&aAnUno, aUnoEnvTypeName.pData, 0 );
472cdf0e10cSrcweir
473cdf0e10cSrcweir Mapping aAnUno2Uno( getDirectMapping( aAnUno, aUno, rAddPurpose ) );
474cdf0e10cSrcweir if (! aAnUno2Uno.is())
475cdf0e10cSrcweir return Mapping();
476cdf0e10cSrcweir
477cdf0e10cSrcweir if (aUno2To.is()) // to is not uno
478cdf0e10cSrcweir {
479cdf0e10cSrcweir // create another purposed mediate mapping
480cdf0e10cSrcweir aUno2To = createMediateMapping( aAnUno, rTo, aAnUno2Uno, aUno2To, rAddPurpose );
481cdf0e10cSrcweir // : ano_uno <-> uno <-> to
482cdf0e10cSrcweir }
483cdf0e10cSrcweir else
484cdf0e10cSrcweir {
485cdf0e10cSrcweir aUno2To = aAnUno2Uno;
486cdf0e10cSrcweir // : ano_uno <-> to (i.e., uno)
487cdf0e10cSrcweir }
488cdf0e10cSrcweir aUno = aAnUno;
489cdf0e10cSrcweir }
490cdf0e10cSrcweir
491cdf0e10cSrcweir Mapping aFrom2Uno( getDirectMapping( rFrom, aUno ) );
492cdf0e10cSrcweir if (aFrom2Uno.is() && aUno2To.is())
493cdf0e10cSrcweir {
494cdf0e10cSrcweir return createMediateMapping( rFrom, rTo, aFrom2Uno, aUno2To, rAddPurpose );
495cdf0e10cSrcweir // : from <-> some uno ...
496cdf0e10cSrcweir }
497cdf0e10cSrcweir
498cdf0e10cSrcweir return Mapping();
499cdf0e10cSrcweir }
500cdf0e10cSrcweir }
501cdf0e10cSrcweir
502cdf0e10cSrcweir using namespace ::cppu;
503cdf0e10cSrcweir
504cdf0e10cSrcweir extern "C"
505cdf0e10cSrcweir {
506cdf0e10cSrcweir //##################################################################################################
uno_getMapping(uno_Mapping ** ppMapping,uno_Environment * pFrom,uno_Environment * pTo,rtl_uString * pAddPurpose)507cdf0e10cSrcweir void SAL_CALL uno_getMapping(
508cdf0e10cSrcweir uno_Mapping ** ppMapping, uno_Environment * pFrom, uno_Environment * pTo,
509cdf0e10cSrcweir rtl_uString * pAddPurpose )
510cdf0e10cSrcweir SAL_THROW_EXTERN_C()
511cdf0e10cSrcweir {
512cdf0e10cSrcweir OSL_ENSURE( ppMapping && pFrom && pTo, "### null ptr!" );
513cdf0e10cSrcweir if (*ppMapping)
514cdf0e10cSrcweir {
515cdf0e10cSrcweir (*(*ppMapping)->release)( *ppMapping );
516cdf0e10cSrcweir *ppMapping = 0;
517cdf0e10cSrcweir }
518cdf0e10cSrcweir
519cdf0e10cSrcweir Mapping aRet;
520cdf0e10cSrcweir Environment aFrom( pFrom ), aTo( pTo );
521cdf0e10cSrcweir
522cdf0e10cSrcweir OUString aAddPurpose;
523cdf0e10cSrcweir if (pAddPurpose)
524cdf0e10cSrcweir aAddPurpose = pAddPurpose;
525cdf0e10cSrcweir
526cdf0e10cSrcweir MappingsData & rData = getMappingsData();
527cdf0e10cSrcweir
528cdf0e10cSrcweir // try registered mapping
529cdf0e10cSrcweir {
530cdf0e10cSrcweir MutexGuard aGuard( rData.aMappingsMutex );
531cdf0e10cSrcweir const t_OUString2Entry::const_iterator iFind( rData.aName2Entry.find(
532cdf0e10cSrcweir getMappingName( aFrom, aTo, aAddPurpose ) ) );
533cdf0e10cSrcweir if (iFind != rData.aName2Entry.end())
534cdf0e10cSrcweir aRet = (*iFind).second->pMapping;
535cdf0e10cSrcweir }
536cdf0e10cSrcweir
537cdf0e10cSrcweir // See if an identity mapping does fit.
538cdf0e10cSrcweir if (!aRet.is() && pFrom == pTo && !aAddPurpose.getLength())
539cdf0e10cSrcweir aRet = createIdentityMapping(pFrom);
540cdf0e10cSrcweir
541cdf0e10cSrcweir if (!aRet.is())
542cdf0e10cSrcweir {
543cdf0e10cSrcweir getCascadeMapping(ppMapping, pFrom, pTo, pAddPurpose);
544cdf0e10cSrcweir
545cdf0e10cSrcweir if (*ppMapping)
546cdf0e10cSrcweir return;
547cdf0e10cSrcweir }
548cdf0e10cSrcweir
549cdf0e10cSrcweir if (! aRet.is()) // try callback chain
550cdf0e10cSrcweir {
551cdf0e10cSrcweir MutexGuard aGuard( rData.aCallbacksMutex );
552cdf0e10cSrcweir for ( t_CallbackSet::const_iterator iPos( rData.aCallbacks.begin() );
553cdf0e10cSrcweir iPos != rData.aCallbacks.end(); ++iPos )
554cdf0e10cSrcweir {
555cdf0e10cSrcweir (**iPos)( ppMapping, pFrom, pTo, aAddPurpose.pData );
556cdf0e10cSrcweir if (*ppMapping)
557cdf0e10cSrcweir return;
558cdf0e10cSrcweir }
559cdf0e10cSrcweir }
560cdf0e10cSrcweir
561cdf0e10cSrcweir if (! aRet.is())
562cdf0e10cSrcweir {
563cdf0e10cSrcweir aRet = loadExternalMapping( aFrom, aTo, aAddPurpose ); // direct try
564cdf0e10cSrcweir if (! aRet.is())
565cdf0e10cSrcweir aRet = getMediateMapping( aFrom, aTo, aAddPurpose ); // try via uno
566cdf0e10cSrcweir }
567cdf0e10cSrcweir
568cdf0e10cSrcweir if (aRet.is())
569cdf0e10cSrcweir {
570cdf0e10cSrcweir (*aRet.get()->acquire)( aRet.get() );
571cdf0e10cSrcweir *ppMapping = aRet.get();
572cdf0e10cSrcweir }
573cdf0e10cSrcweir }
574cdf0e10cSrcweir //##################################################################################################
uno_getMappingByName(uno_Mapping ** ppMapping,rtl_uString * pFrom,rtl_uString * pTo,rtl_uString * pAddPurpose)575cdf0e10cSrcweir void SAL_CALL uno_getMappingByName(
576cdf0e10cSrcweir uno_Mapping ** ppMapping, rtl_uString * pFrom, rtl_uString * pTo,
577cdf0e10cSrcweir rtl_uString * pAddPurpose )
578cdf0e10cSrcweir SAL_THROW_EXTERN_C()
579cdf0e10cSrcweir {
580cdf0e10cSrcweir OSL_ENSURE( ppMapping && pFrom && pTo, "### null ptr!" );
581cdf0e10cSrcweir if (*ppMapping)
582cdf0e10cSrcweir {
583cdf0e10cSrcweir (*(*ppMapping)->release)( *ppMapping );
584cdf0e10cSrcweir *ppMapping = 0;
585cdf0e10cSrcweir }
586cdf0e10cSrcweir
587cdf0e10cSrcweir uno_Environment * pEFrom = 0;
588cdf0e10cSrcweir uno_getEnvironment( &pEFrom, pFrom, 0 );
589cdf0e10cSrcweir OSL_ENSURE( pEFrom, "### cannot get source environment!" );
590cdf0e10cSrcweir if (pEFrom)
591cdf0e10cSrcweir {
592cdf0e10cSrcweir uno_Environment * pETo = 0;
593cdf0e10cSrcweir uno_getEnvironment( &pETo, pTo, 0 );
594cdf0e10cSrcweir OSL_ENSURE( pETo, "### cannot get target environment!" );
595cdf0e10cSrcweir if (pETo)
596cdf0e10cSrcweir {
597cdf0e10cSrcweir ::uno_getMapping( ppMapping, pEFrom, pETo, pAddPurpose );
598cdf0e10cSrcweir (*pETo->release)( pETo );
599cdf0e10cSrcweir }
600cdf0e10cSrcweir (*pEFrom->release)( pEFrom );
601cdf0e10cSrcweir }
602cdf0e10cSrcweir }
603cdf0e10cSrcweir
604cdf0e10cSrcweir //##################################################################################################
uno_registerMapping(uno_Mapping ** ppMapping,uno_freeMappingFunc freeMapping,uno_Environment * pFrom,uno_Environment * pTo,rtl_uString * pAddPurpose)605cdf0e10cSrcweir void SAL_CALL uno_registerMapping(
606cdf0e10cSrcweir uno_Mapping ** ppMapping, uno_freeMappingFunc freeMapping,
607cdf0e10cSrcweir uno_Environment * pFrom, uno_Environment * pTo, rtl_uString * pAddPurpose )
608cdf0e10cSrcweir SAL_THROW_EXTERN_C()
609cdf0e10cSrcweir {
610cdf0e10cSrcweir MappingsData & rData = getMappingsData();
611cdf0e10cSrcweir ClearableMutexGuard aGuard( rData.aMappingsMutex );
612cdf0e10cSrcweir
613cdf0e10cSrcweir const t_Mapping2Entry::const_iterator iFind( rData.aMapping2Entry.find( *ppMapping ) );
614cdf0e10cSrcweir if (iFind == rData.aMapping2Entry.end())
615cdf0e10cSrcweir {
616cdf0e10cSrcweir OUString aMappingName(
617cdf0e10cSrcweir getMappingName( pFrom, pTo, pAddPurpose ? OUString(pAddPurpose) : OUString() ) );
618cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
619cdf0e10cSrcweir OString cstr( OUStringToOString( aMappingName, RTL_TEXTENCODING_ASCII_US ) );
620cdf0e10cSrcweir OSL_TRACE( "> inserting new mapping: %s", cstr.getStr() );
621cdf0e10cSrcweir #endif
622cdf0e10cSrcweir // count initially 1
623cdf0e10cSrcweir MappingEntry * pEntry = new MappingEntry( *ppMapping, freeMapping, aMappingName );
624cdf0e10cSrcweir rData.aName2Entry[ aMappingName ] = pEntry;
625cdf0e10cSrcweir rData.aMapping2Entry[ *ppMapping ] = pEntry;
626cdf0e10cSrcweir }
627cdf0e10cSrcweir else
628cdf0e10cSrcweir {
629cdf0e10cSrcweir MappingEntry * pEntry = (*iFind).second;
630cdf0e10cSrcweir ++pEntry->nRef;
631cdf0e10cSrcweir
632cdf0e10cSrcweir if (pEntry->pMapping != *ppMapping) // exchange mapping to be registered
633cdf0e10cSrcweir {
634cdf0e10cSrcweir (*pEntry->pMapping->acquire)( pEntry->pMapping );
635cdf0e10cSrcweir --pEntry->nRef; // correct count; kill mapping to be registered
636cdf0e10cSrcweir aGuard.clear();
637cdf0e10cSrcweir (*freeMapping)( *ppMapping );
638cdf0e10cSrcweir *ppMapping = pEntry->pMapping;
639cdf0e10cSrcweir }
640cdf0e10cSrcweir }
641cdf0e10cSrcweir }
642cdf0e10cSrcweir //##################################################################################################
uno_revokeMapping(uno_Mapping * pMapping)643cdf0e10cSrcweir void SAL_CALL uno_revokeMapping(
644cdf0e10cSrcweir uno_Mapping * pMapping )
645cdf0e10cSrcweir SAL_THROW_EXTERN_C()
646cdf0e10cSrcweir {
647cdf0e10cSrcweir MappingsData & rData = getMappingsData();
648cdf0e10cSrcweir ClearableMutexGuard aGuard( rData.aMappingsMutex );
649cdf0e10cSrcweir
650cdf0e10cSrcweir const t_Mapping2Entry::const_iterator iFind( rData.aMapping2Entry.find( pMapping ) );
651cdf0e10cSrcweir OSL_ASSERT( iFind != rData.aMapping2Entry.end() );
652cdf0e10cSrcweir MappingEntry * pEntry = (*iFind).second;
653cdf0e10cSrcweir if (! --pEntry->nRef)
654cdf0e10cSrcweir {
655cdf0e10cSrcweir rData.aMapping2Entry.erase( pEntry->pMapping );
656cdf0e10cSrcweir rData.aName2Entry.erase( pEntry->aMappingName );
657cdf0e10cSrcweir aGuard.clear();
658cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
659cdf0e10cSrcweir OString cstr( OUStringToOString( pEntry->aMappingName, RTL_TEXTENCODING_ASCII_US ) );
660cdf0e10cSrcweir OSL_TRACE( "> revoking mapping %s", cstr.getStr() );
661cdf0e10cSrcweir #endif
662cdf0e10cSrcweir (*pEntry->freeMapping)( pEntry->pMapping );
663cdf0e10cSrcweir delete pEntry;
664cdf0e10cSrcweir }
665cdf0e10cSrcweir }
666cdf0e10cSrcweir
667cdf0e10cSrcweir //##################################################################################################
uno_registerMappingCallback(uno_getMappingFunc pCallback)668cdf0e10cSrcweir void SAL_CALL uno_registerMappingCallback(
669cdf0e10cSrcweir uno_getMappingFunc pCallback )
670cdf0e10cSrcweir SAL_THROW_EXTERN_C()
671cdf0e10cSrcweir {
672cdf0e10cSrcweir OSL_ENSURE( pCallback, "### null ptr!" );
673cdf0e10cSrcweir MappingsData & rData = getMappingsData();
674cdf0e10cSrcweir MutexGuard aGuard( rData.aCallbacksMutex );
675cdf0e10cSrcweir rData.aCallbacks.insert( pCallback );
676cdf0e10cSrcweir }
677cdf0e10cSrcweir //##################################################################################################
uno_revokeMappingCallback(uno_getMappingFunc pCallback)678cdf0e10cSrcweir void SAL_CALL uno_revokeMappingCallback(
679cdf0e10cSrcweir uno_getMappingFunc pCallback )
680cdf0e10cSrcweir SAL_THROW_EXTERN_C()
681cdf0e10cSrcweir {
682cdf0e10cSrcweir OSL_ENSURE( pCallback, "### null ptr!" );
683cdf0e10cSrcweir MappingsData & rData = getMappingsData();
684cdf0e10cSrcweir MutexGuard aGuard( rData.aCallbacksMutex );
685cdf0e10cSrcweir rData.aCallbacks.erase( pCallback );
686cdf0e10cSrcweir }
687cdf0e10cSrcweir } // extern "C"
688cdf0e10cSrcweir
689