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