1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_stoc.hxx"
26
27 #include "osl/diagnose.h"
28 #include "osl/interlck.h"
29 #include "osl/doublecheckedlocking.h"
30 #include "osl/mutex.hxx"
31 #include "rtl/ref.hxx"
32 #include "uno/dispatcher.hxx"
33 #include "uno/data.h"
34 #include "uno/mapping.hxx"
35 #include "uno/environment.hxx"
36 #include "typelib/typedescription.hxx"
37 #include "cppuhelper/exc_hlp.hxx"
38 #include "cppuhelper/implbase2.hxx"
39 #include "cppuhelper/implementationentry.hxx"
40 #include "cppuhelper/factory.hxx"
41 #include "com/sun/star/lang/XServiceInfo.hpp"
42 #include "com/sun/star/registry/XRegistryKey.hpp"
43 #include "com/sun/star/reflection/XProxyFactory.hpp"
44 #include "com/sun/star/uno/RuntimeException.hpp"
45
46 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
47 #define SERVICE_NAME "com.sun.star.reflection.ProxyFactory"
48 #define IMPL_NAME "com.sun.star.comp.reflection.ProxyFactory"
49
50
51 using namespace ::com::sun::star;
52 using namespace ::com::sun::star::uno;
53 using ::rtl::OUString;
54
55
56 namespace
57 {
58
59 static rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
60
proxyfac_getImplementationName()61 static OUString proxyfac_getImplementationName()
62 {
63 return OUSTR(IMPL_NAME);
64 }
65
proxyfac_getSupportedServiceNames()66 static Sequence< OUString > proxyfac_getSupportedServiceNames()
67 {
68 OUString str_name = OUSTR(SERVICE_NAME);
69 return Sequence< OUString >( &str_name, 1 );
70 }
71
72 //==============================================================================
73 struct FactoryImpl : public ::cppu::WeakImplHelper2< lang::XServiceInfo,
74 reflection::XProxyFactory >
75 {
76 Environment m_uno_env;
77 Environment m_cpp_env;
78 Mapping m_uno2cpp;
79 Mapping m_cpp2uno;
80
81 UnoInterfaceReference binuno_queryInterface(
82 UnoInterfaceReference const & unoI,
83 typelib_InterfaceTypeDescription * pTypeDescr );
84
85 FactoryImpl();
86 virtual ~FactoryImpl();
87
88 // XServiceInfo
89 virtual OUString SAL_CALL getImplementationName();
90 virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName );
91 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames();
92
93 // XProxyFactory
94 virtual Reference< XAggregation > SAL_CALL createProxy(
95 Reference< XInterface > const & xTarget );
96 };
97
98 //______________________________________________________________________________
binuno_queryInterface(UnoInterfaceReference const & unoI,typelib_InterfaceTypeDescription * pTypeDescr)99 UnoInterfaceReference FactoryImpl::binuno_queryInterface(
100 UnoInterfaceReference const & unoI,
101 typelib_InterfaceTypeDescription * pTypeDescr )
102 {
103 // init queryInterface() td
104 static typelib_TypeDescription * s_pQITD = 0;
105 if (s_pQITD == 0)
106 {
107 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
108 if (s_pQITD == 0)
109 {
110 typelib_TypeDescription * pTXInterfaceDescr = 0;
111 TYPELIB_DANGER_GET(
112 &pTXInterfaceDescr,
113 ::getCppuType( reinterpret_cast< Reference< XInterface >
114 const * >(0) ).getTypeLibType() );
115 typelib_TypeDescription * pQITD = 0;
116 typelib_typedescriptionreference_getDescription(
117 &pQITD, reinterpret_cast< typelib_InterfaceTypeDescription * >(
118 pTXInterfaceDescr )->ppAllMembers[ 0 ] );
119 TYPELIB_DANGER_RELEASE( pTXInterfaceDescr );
120 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
121 s_pQITD = pQITD;
122 }
123 }
124 else
125 {
126 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
127 }
128
129 void * args[ 1 ];
130 args[ 0 ] = &reinterpret_cast< typelib_TypeDescription * >(
131 pTypeDescr )->pWeakRef;
132 uno_Any ret_val, exc_space;
133 uno_Any * exc = &exc_space;
134
135 unoI.dispatch( s_pQITD, &ret_val, args, &exc );
136
137 if (exc == 0)
138 {
139 UnoInterfaceReference ret;
140 if (ret_val.pType->eTypeClass == typelib_TypeClass_INTERFACE)
141 {
142 ret.set( *reinterpret_cast< uno_Interface ** >(ret_val.pData),
143 SAL_NO_ACQUIRE );
144 typelib_typedescriptionreference_release( ret_val.pType );
145 }
146 else
147 {
148 uno_any_destruct( &ret_val, 0 );
149 }
150 return ret;
151 }
152 else
153 {
154 // exception occurred:
155 OSL_ENSURE(
156 typelib_typedescriptionreference_isAssignableFrom(
157 ::getCppuType( reinterpret_cast<
158 RuntimeException const * >(0) ).getTypeLibType(),
159 exc->pType ),
160 "### RuntimeException expected!" );
161 Any cpp_exc;
162 uno_type_copyAndConvertData(
163 &cpp_exc, exc, ::getCppuType( &cpp_exc ).getTypeLibType(),
164 m_uno2cpp.get() );
165 uno_any_destruct( exc, 0 );
166 ::cppu::throwException( cpp_exc );
167 OSL_ASSERT( 0 ); // way of no return
168 return UnoInterfaceReference(); // for dummy
169 }
170 }
171
172 //==============================================================================
173 struct ProxyRoot : public ::cppu::OWeakAggObject
174 {
175 // XAggregation
176 virtual Any SAL_CALL queryAggregation( Type const & rType );
177
178 virtual ~ProxyRoot();
179 inline ProxyRoot( ::rtl::Reference< FactoryImpl > const & factory,
180 Reference< XInterface > const & xTarget );
181
182 ::rtl::Reference< FactoryImpl > m_factory;
183
184 private:
185 UnoInterfaceReference m_target;
186 };
187
188 //==============================================================================
189 struct binuno_Proxy : public uno_Interface
190 {
191 oslInterlockedCount m_nRefCount;
192 ::rtl::Reference< ProxyRoot > m_root;
193 UnoInterfaceReference m_target;
194 OUString m_oid;
195 TypeDescription m_typeDescr;
196
197 inline binuno_Proxy(
198 ::rtl::Reference< ProxyRoot > const & root,
199 UnoInterfaceReference const & target,
200 OUString const & oid, TypeDescription const & typeDescr );
201 };
202
203 extern "C"
204 {
205
206 //------------------------------------------------------------------------------
binuno_proxy_free(uno_ExtEnvironment * pEnv,void * pProxy)207 static void SAL_CALL binuno_proxy_free(
208 uno_ExtEnvironment * pEnv, void * pProxy )
209 {
210 (void) pEnv; // avoid warning about unused parameter
211 binuno_Proxy * proxy = static_cast< binuno_Proxy * >(
212 reinterpret_cast< uno_Interface * >( pProxy ) );
213 OSL_ASSERT( proxy->m_root->m_factory->m_uno_env.get()->pExtEnv == pEnv );
214 delete proxy;
215 }
216
217 //------------------------------------------------------------------------------
binuno_proxy_acquire(uno_Interface * pUnoI)218 static void SAL_CALL binuno_proxy_acquire( uno_Interface * pUnoI )
219 {
220 binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
221 if (osl_incrementInterlockedCount( &that->m_nRefCount ) == 1)
222 {
223 // rebirth of zombie
224 uno_ExtEnvironment * uno_env =
225 that->m_root->m_factory->m_uno_env.get()->pExtEnv;
226 OSL_ASSERT( uno_env != 0 );
227 (*uno_env->registerProxyInterface)(
228 uno_env, reinterpret_cast< void ** >( &pUnoI ), binuno_proxy_free,
229 that->m_oid.pData,
230 reinterpret_cast< typelib_InterfaceTypeDescription * >(
231 that->m_typeDescr.get() ) );
232 OSL_ASSERT( that == static_cast< binuno_Proxy * >( pUnoI ) );
233 }
234 }
235
236 //------------------------------------------------------------------------------
binuno_proxy_release(uno_Interface * pUnoI)237 static void SAL_CALL binuno_proxy_release( uno_Interface * pUnoI )
238 {
239 binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
240 if (osl_decrementInterlockedCount( &that->m_nRefCount ) == 0)
241 {
242 uno_ExtEnvironment * uno_env =
243 that->m_root->m_factory->m_uno_env.get()->pExtEnv;
244 OSL_ASSERT( uno_env != 0 );
245 (*uno_env->revokeInterface)( uno_env, pUnoI );
246 }
247 }
248
249 //------------------------------------------------------------------------------
binuno_proxy_dispatch(uno_Interface * pUnoI,const typelib_TypeDescription * pMemberType,void * pReturn,void * pArgs[],uno_Any ** ppException)250 static void SAL_CALL binuno_proxy_dispatch(
251 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberType,
252 void * pReturn, void * pArgs [], uno_Any ** ppException )
253 {
254 binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
255 switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription const * >(
256 pMemberType )->nPosition)
257 {
258 case 0: // queryInterface()
259 {
260 try
261 {
262 Type const & rType =
263 *reinterpret_cast< Type const * >( pArgs[ 0 ] );
264 Any ret( that->m_root->queryInterface( rType ) );
265 uno_type_copyAndConvertData(
266 pReturn, &ret, ::getCppuType( &ret ).getTypeLibType(),
267 that->m_root->m_factory->m_cpp2uno.get() );
268 *ppException = 0; // no exc
269 }
270 catch (RuntimeException &)
271 {
272 Any exc( ::cppu::getCaughtException() );
273 uno_type_any_constructAndConvert(
274 *ppException, const_cast< void * >(exc.getValue()),
275 exc.getValueTypeRef(),
276 that->m_root->m_factory->m_cpp2uno.get() );
277 }
278 break;
279 }
280 case 1: // acquire()
281 binuno_proxy_acquire( pUnoI );
282 *ppException = 0; // no exc
283 break;
284 case 2: // release()
285 binuno_proxy_release( pUnoI );
286 *ppException = 0; // no exc
287 break;
288 default:
289 that->m_target.dispatch( pMemberType, pReturn, pArgs, ppException );
290 break;
291 }
292 }
293
294 }
295
296 //______________________________________________________________________________
binuno_Proxy(::rtl::Reference<ProxyRoot> const & root,UnoInterfaceReference const & target,OUString const & oid,TypeDescription const & typeDescr)297 inline binuno_Proxy::binuno_Proxy(
298 ::rtl::Reference< ProxyRoot > const & root,
299 UnoInterfaceReference const & target,
300 OUString const & oid, TypeDescription const & typeDescr )
301 : m_nRefCount( 1 ),
302 m_root( root ),
303 m_target( target ),
304 m_oid( oid ),
305 m_typeDescr( typeDescr )
306 {
307 uno_Interface::acquire = binuno_proxy_acquire;
308 uno_Interface::release = binuno_proxy_release;
309 uno_Interface::pDispatcher = binuno_proxy_dispatch;
310 }
311
312 //______________________________________________________________________________
~ProxyRoot()313 ProxyRoot::~ProxyRoot()
314 {
315 }
316
317 //______________________________________________________________________________
ProxyRoot(::rtl::Reference<FactoryImpl> const & factory,Reference<XInterface> const & xTarget)318 inline ProxyRoot::ProxyRoot(
319 ::rtl::Reference< FactoryImpl > const & factory,
320 Reference< XInterface > const & xTarget )
321 : m_factory( factory )
322 {
323 m_factory->m_cpp2uno.mapInterface(
324 reinterpret_cast< void ** >( &m_target.m_pUnoI ), xTarget.get(),
325 ::getCppuType( &xTarget ) );
326 OSL_ENSURE( m_target.is(), "### mapping interface failed!" );
327 }
328
329 //______________________________________________________________________________
queryAggregation(Type const & rType)330 Any ProxyRoot::queryAggregation( Type const & rType )
331 {
332 Any ret( OWeakAggObject::queryAggregation( rType ) );
333 if (! ret.hasValue())
334 {
335 typelib_TypeDescription * pTypeDescr = 0;
336 TYPELIB_DANGER_GET( &pTypeDescr, rType.getTypeLibType() );
337 try
338 {
339 Reference< XInterface > xProxy;
340 uno_ExtEnvironment * cpp_env = m_factory->m_cpp_env.get()->pExtEnv;
341 OSL_ASSERT( cpp_env != 0 );
342
343 // mind a new delegator, calculate current root:
344 Reference< XInterface > xRoot(
345 static_cast< OWeakObject * >(this), UNO_QUERY_THROW );
346 OUString oid;
347 (*cpp_env->getObjectIdentifier)( cpp_env, &oid.pData, xRoot.get() );
348 OSL_ASSERT( oid.getLength() > 0 );
349
350 (*cpp_env->getRegisteredInterface)(
351 cpp_env, reinterpret_cast< void ** >( &xProxy ),
352 oid.pData, reinterpret_cast<
353 typelib_InterfaceTypeDescription * >(pTypeDescr) );
354 if (! xProxy.is())
355 {
356 // perform query on target:
357 UnoInterfaceReference proxy_target(
358 m_factory->binuno_queryInterface(
359 m_target, reinterpret_cast<
360 typelib_InterfaceTypeDescription * >(pTypeDescr) ) );
361 if (proxy_target.is())
362 {
363 // ensure root's object entries:
364 UnoInterfaceReference root;
365 m_factory->m_cpp2uno.mapInterface(
366 reinterpret_cast< void ** >( &root.m_pUnoI ),
367 xRoot.get(), ::getCppuType( &xRoot ) );
368
369 UnoInterfaceReference proxy(
370 // ref count initially 1:
371 new binuno_Proxy( this, proxy_target, oid, pTypeDescr ),
372 SAL_NO_ACQUIRE );
373 uno_ExtEnvironment * uno_env =
374 m_factory->m_uno_env.get()->pExtEnv;
375 OSL_ASSERT( uno_env != 0 );
376 (*uno_env->registerProxyInterface)(
377 uno_env, reinterpret_cast< void ** >( &proxy.m_pUnoI ),
378 binuno_proxy_free, oid.pData,
379 reinterpret_cast< typelib_InterfaceTypeDescription * >(
380 pTypeDescr ) );
381
382 m_factory->m_uno2cpp.mapInterface(
383 reinterpret_cast< void ** >( &xProxy ),
384 proxy.get(), pTypeDescr );
385 }
386 }
387 if (xProxy.is())
388 ret.setValue( &xProxy, pTypeDescr );
389 }
390 catch (...) // finally
391 {
392 TYPELIB_DANGER_RELEASE( pTypeDescr );
393 throw;
394 }
395 TYPELIB_DANGER_RELEASE( pTypeDescr );
396 }
397 return ret;
398 }
399
400 //##############################################################################
401
402 //______________________________________________________________________________
FactoryImpl()403 FactoryImpl::FactoryImpl()
404 {
405 OUString uno = OUSTR(UNO_LB_UNO);
406 OUString cpp = OUSTR(CPPU_CURRENT_LANGUAGE_BINDING_NAME);
407
408 uno_getEnvironment(
409 reinterpret_cast< uno_Environment ** >( &m_uno_env ), uno.pData, 0 );
410 OSL_ENSURE( m_uno_env.is(), "### cannot get binary uno env!" );
411
412 uno_getEnvironment(
413 reinterpret_cast< uno_Environment ** >( &m_cpp_env ), cpp.pData, 0 );
414 OSL_ENSURE( m_cpp_env.is(), "### cannot get C++ uno env!" );
415
416 uno_getMapping(
417 reinterpret_cast< uno_Mapping ** >( &m_uno2cpp ),
418 m_uno_env.get(), m_cpp_env.get(), 0 );
419 OSL_ENSURE( m_uno2cpp.is(), "### cannot get bridge uno <-> C++!" );
420
421 uno_getMapping(
422 reinterpret_cast< uno_Mapping ** >( &m_cpp2uno ),
423 m_cpp_env.get(), m_uno_env.get(), 0 );
424 OSL_ENSURE( m_cpp2uno.is(), "### cannot get bridge C++ <-> uno!" );
425
426 g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
427 }
428
429 //______________________________________________________________________________
~FactoryImpl()430 FactoryImpl::~FactoryImpl()
431 {
432 g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
433 }
434
435 // XProxyFactory
436 //______________________________________________________________________________
createProxy(Reference<XInterface> const & xTarget)437 Reference< XAggregation > FactoryImpl::createProxy(
438 Reference< XInterface > const & xTarget )
439 {
440 return new ProxyRoot( this, xTarget );
441 }
442
443 // XServiceInfo
444 //______________________________________________________________________________
getImplementationName()445 OUString FactoryImpl::getImplementationName()
446 {
447 return proxyfac_getImplementationName();
448 }
449
450 //______________________________________________________________________________
supportsService(const OUString & rServiceName)451 sal_Bool FactoryImpl::supportsService( const OUString & rServiceName )
452 {
453 Sequence< OUString > const & rSNL = getSupportedServiceNames();
454 OUString const * pArray = rSNL.getConstArray();
455 for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
456 {
457 if (rServiceName.equals( pArray[ nPos ] ))
458 return true;
459 }
460 return false;
461 }
462
463 //______________________________________________________________________________
getSupportedServiceNames()464 Sequence< OUString > FactoryImpl::getSupportedServiceNames()
465 {
466 return proxyfac_getSupportedServiceNames();
467 }
468
469 //==============================================================================
proxyfac_create(Reference<XComponentContext> const &)470 static Reference< XInterface > SAL_CALL proxyfac_create(
471 Reference< XComponentContext > const & )
472 {
473 Reference< XInterface > xRet;
474 {
475 ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
476 static WeakReference < XInterface > rwInstance;
477 xRet = rwInstance;
478
479 if (! xRet.is())
480 {
481 xRet = static_cast< ::cppu::OWeakObject * >(new FactoryImpl);
482 rwInstance = xRet;
483 }
484 }
485 return xRet;
486 }
487
488 static ::cppu::ImplementationEntry g_entries [] =
489 {
490 {
491 proxyfac_create, proxyfac_getImplementationName,
492 proxyfac_getSupportedServiceNames, ::cppu::createSingleComponentFactory,
493 &g_moduleCount.modCnt, 0
494 },
495 { 0, 0, 0, 0, 0, 0 }
496 };
497
498 }
499
500 extern "C"
501 {
502
component_canUnload(TimeValue * pTime)503 sal_Bool SAL_CALL component_canUnload( TimeValue * pTime )
504 {
505 return g_moduleCount.canUnload( &g_moduleCount, pTime );
506 }
507
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)508 void SAL_CALL component_getImplementationEnvironment(
509 const sal_Char ** ppEnvTypeName, uno_Environment ** )
510 {
511 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
512 }
513
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)514 void * SAL_CALL component_getFactory(
515 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
516 {
517 return ::cppu::component_getFactoryHelper(
518 pImplName, pServiceManager, pRegistryKey, g_entries );
519 }
520
521 }
522