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 SERVICE_NAME "com.sun.star.reflection.ProxyFactory"
41 : #define IMPL_NAME "com.sun.star.comp.reflection.ProxyFactory"
42 :
43 :
44 : using namespace ::com::sun::star;
45 : using namespace ::com::sun::star::uno;
46 :
47 :
48 : namespace
49 : {
50 :
51 16 : static OUString proxyfac_getImplementationName()
52 : {
53 16 : return OUString(IMPL_NAME);
54 : }
55 :
56 16 : static Sequence< OUString > proxyfac_getSupportedServiceNames()
57 : {
58 16 : OUString str_name = SERVICE_NAME;
59 16 : return Sequence< OUString >( &str_name, 1 );
60 : }
61 :
62 : //==============================================================================
63 : struct FactoryImpl : public ::cppu::WeakImplHelper2< lang::XServiceInfo,
64 : reflection::XProxyFactory >
65 : {
66 : Environment m_uno_env;
67 : Environment m_cpp_env;
68 : Mapping m_uno2cpp;
69 : Mapping m_cpp2uno;
70 :
71 : UnoInterfaceReference binuno_queryInterface(
72 : UnoInterfaceReference const & unoI,
73 : typelib_InterfaceTypeDescription * pTypeDescr );
74 :
75 : FactoryImpl();
76 : virtual ~FactoryImpl();
77 :
78 : // XServiceInfo
79 : virtual OUString SAL_CALL getImplementationName()
80 : throw (RuntimeException);
81 : virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName )
82 : throw (RuntimeException);
83 : virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
84 : throw (RuntimeException);
85 :
86 : // XProxyFactory
87 : virtual Reference< XAggregation > SAL_CALL createProxy(
88 : Reference< XInterface > const & xTarget )
89 : throw (RuntimeException);
90 : };
91 :
92 : //______________________________________________________________________________
93 3431 : UnoInterfaceReference FactoryImpl::binuno_queryInterface(
94 : UnoInterfaceReference const & unoI,
95 : typelib_InterfaceTypeDescription * pTypeDescr )
96 : {
97 : // init queryInterface() td
98 : static typelib_TypeDescription * s_pQITD = 0;
99 3431 : if (s_pQITD == 0)
100 : {
101 8 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
102 8 : if (s_pQITD == 0)
103 : {
104 8 : typelib_TypeDescription * pTXInterfaceDescr = 0;
105 : TYPELIB_DANGER_GET(
106 : &pTXInterfaceDescr,
107 : ::getCppuType( reinterpret_cast< Reference< XInterface >
108 8 : const * >(0) ).getTypeLibType() );
109 8 : typelib_TypeDescription * pQITD = 0;
110 : typelib_typedescriptionreference_getDescription(
111 : &pQITD, reinterpret_cast< typelib_InterfaceTypeDescription * >(
112 8 : pTXInterfaceDescr )->ppAllMembers[ 0 ] );
113 8 : TYPELIB_DANGER_RELEASE( pTXInterfaceDescr );
114 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
115 8 : s_pQITD = pQITD;
116 8 : }
117 : }
118 : else
119 : {
120 : OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
121 : }
122 :
123 : void * args[ 1 ];
124 : args[ 0 ] = &reinterpret_cast< typelib_TypeDescription * >(
125 3431 : pTypeDescr )->pWeakRef;
126 : uno_Any ret_val, exc_space;
127 3431 : uno_Any * exc = &exc_space;
128 :
129 3431 : unoI.dispatch( s_pQITD, &ret_val, args, &exc );
130 :
131 3431 : if (exc == 0)
132 : {
133 3431 : UnoInterfaceReference ret;
134 3431 : if (ret_val.pType->eTypeClass == typelib_TypeClass_INTERFACE)
135 : {
136 : ret.set( *reinterpret_cast< uno_Interface ** >(ret_val.pData),
137 2097 : SAL_NO_ACQUIRE );
138 2097 : typelib_typedescriptionreference_release( ret_val.pType );
139 : }
140 : else
141 : {
142 1334 : uno_any_destruct( &ret_val, 0 );
143 : }
144 3431 : return ret;
145 : }
146 : else
147 : {
148 : // exception occurred:
149 : OSL_ENSURE(
150 : typelib_typedescriptionreference_isAssignableFrom(
151 : ::getCppuType( reinterpret_cast<
152 : RuntimeException const * >(0) ).getTypeLibType(),
153 : exc->pType ),
154 : "### RuntimeException expected!" );
155 0 : Any cpp_exc;
156 : uno_type_copyAndConvertData(
157 0 : &cpp_exc, exc, ::getCppuType( &cpp_exc ).getTypeLibType(),
158 0 : m_uno2cpp.get() );
159 0 : uno_any_destruct( exc, 0 );
160 0 : ::cppu::throwException( cpp_exc );
161 : OSL_ASSERT( 0 ); // way of no return
162 0 : return UnoInterfaceReference(); // for dummy
163 : }
164 : }
165 :
166 : //==============================================================================
167 : struct ProxyRoot : public ::cppu::OWeakAggObject
168 : {
169 : // XAggregation
170 : virtual Any SAL_CALL queryAggregation( Type const & rType )
171 : throw (RuntimeException);
172 :
173 : virtual ~ProxyRoot();
174 : inline ProxyRoot( ::rtl::Reference< FactoryImpl > const & factory,
175 : Reference< XInterface > const & xTarget );
176 :
177 : ::rtl::Reference< FactoryImpl > m_factory;
178 :
179 : private:
180 : UnoInterfaceReference m_target;
181 : };
182 :
183 : //==============================================================================
184 1836 : struct binuno_Proxy : public uno_Interface
185 : {
186 : oslInterlockedCount m_nRefCount;
187 : ::rtl::Reference< ProxyRoot > m_root;
188 : UnoInterfaceReference m_target;
189 : OUString m_oid;
190 : TypeDescription m_typeDescr;
191 :
192 : inline binuno_Proxy(
193 : ::rtl::Reference< ProxyRoot > const & root,
194 : UnoInterfaceReference const & target,
195 : OUString const & oid, TypeDescription const & typeDescr );
196 : };
197 :
198 : extern "C"
199 : {
200 :
201 : //------------------------------------------------------------------------------
202 1836 : static void SAL_CALL binuno_proxy_free(
203 : uno_ExtEnvironment * pEnv, void * pProxy )
204 : {
205 : (void) pEnv; // avoid warning about unused parameter
206 : binuno_Proxy * proxy = static_cast< binuno_Proxy * >(
207 1836 : reinterpret_cast< uno_Interface * >( pProxy ) );
208 : OSL_ASSERT( proxy->m_root->m_factory->m_uno_env.get()->pExtEnv == pEnv );
209 1836 : delete proxy;
210 1836 : }
211 :
212 : //------------------------------------------------------------------------------
213 5953 : static void SAL_CALL binuno_proxy_acquire( uno_Interface * pUnoI )
214 : {
215 5953 : binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
216 5953 : if (osl_atomic_increment( &that->m_nRefCount ) == 1)
217 : {
218 : // rebirth of zombie
219 : uno_ExtEnvironment * uno_env =
220 235 : that->m_root->m_factory->m_uno_env.get()->pExtEnv;
221 : OSL_ASSERT( uno_env != 0 );
222 : (*uno_env->registerProxyInterface)(
223 : uno_env, reinterpret_cast< void ** >( &pUnoI ), binuno_proxy_free,
224 : that->m_oid.pData,
225 : reinterpret_cast< typelib_InterfaceTypeDescription * >(
226 235 : that->m_typeDescr.get() ) );
227 : OSL_ASSERT( that == static_cast< binuno_Proxy * >( pUnoI ) );
228 : }
229 5953 : }
230 :
231 : //------------------------------------------------------------------------------
232 7571 : static void SAL_CALL binuno_proxy_release( uno_Interface * pUnoI )
233 : {
234 7571 : binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
235 7571 : if (osl_atomic_decrement( &that->m_nRefCount ) == 0)
236 : {
237 : uno_ExtEnvironment * uno_env =
238 1853 : that->m_root->m_factory->m_uno_env.get()->pExtEnv;
239 : OSL_ASSERT( uno_env != 0 );
240 1853 : (*uno_env->revokeInterface)( uno_env, pUnoI );
241 : }
242 7571 : }
243 :
244 : //------------------------------------------------------------------------------
245 4380 : static void SAL_CALL binuno_proxy_dispatch(
246 : uno_Interface * pUnoI, const typelib_TypeDescription * pMemberType,
247 : void * pReturn, void * pArgs [], uno_Any ** ppException )
248 : {
249 4380 : binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
250 4380 : switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription const * >(
251 : pMemberType )->nPosition)
252 : {
253 : case 0: // queryInterface()
254 : {
255 : try
256 : {
257 : Type const & rType =
258 2250 : *reinterpret_cast< Type const * >( pArgs[ 0 ] );
259 2250 : Any ret( that->m_root->queryInterface( rType ) );
260 : uno_type_copyAndConvertData(
261 2250 : pReturn, &ret, ::getCppuType( &ret ).getTypeLibType(),
262 4500 : that->m_root->m_factory->m_cpp2uno.get() );
263 2250 : *ppException = 0; // no exc
264 : }
265 0 : catch (RuntimeException &)
266 : {
267 0 : Any exc( ::cppu::getCaughtException() );
268 : uno_type_any_constructAndConvert(
269 0 : *ppException, const_cast< void * >(exc.getValue()),
270 : exc.getValueTypeRef(),
271 0 : that->m_root->m_factory->m_cpp2uno.get() );
272 : }
273 2250 : break;
274 : }
275 : case 1: // acquire()
276 0 : binuno_proxy_acquire( pUnoI );
277 0 : *ppException = 0; // no exc
278 0 : break;
279 : case 2: // release()
280 0 : binuno_proxy_release( pUnoI );
281 0 : *ppException = 0; // no exc
282 0 : break;
283 : default:
284 2130 : that->m_target.dispatch( pMemberType, pReturn, pArgs, ppException );
285 2130 : break;
286 : }
287 4380 : }
288 :
289 : }
290 :
291 : //______________________________________________________________________________
292 2097 : inline binuno_Proxy::binuno_Proxy(
293 : ::rtl::Reference< ProxyRoot > const & root,
294 : UnoInterfaceReference const & target,
295 : OUString const & oid, TypeDescription const & typeDescr )
296 : : m_nRefCount( 1 ),
297 : m_root( root ),
298 : m_target( target ),
299 : m_oid( oid ),
300 2097 : m_typeDescr( typeDescr )
301 : {
302 2097 : uno_Interface::acquire = binuno_proxy_acquire;
303 2097 : uno_Interface::release = binuno_proxy_release;
304 2097 : uno_Interface::pDispatcher = binuno_proxy_dispatch;
305 2097 : }
306 :
307 : //______________________________________________________________________________
308 776 : ProxyRoot::~ProxyRoot()
309 : {
310 776 : }
311 :
312 : //______________________________________________________________________________
313 669 : inline ProxyRoot::ProxyRoot(
314 : ::rtl::Reference< FactoryImpl > const & factory,
315 : Reference< XInterface > const & xTarget )
316 669 : : m_factory( factory )
317 : {
318 669 : m_factory->m_cpp2uno.mapInterface(
319 669 : reinterpret_cast< void ** >( &m_target.m_pUnoI ), xTarget.get(),
320 2007 : ::getCppuType( &xTarget ) );
321 : OSL_ENSURE( m_target.is(), "### mapping interface failed!" );
322 669 : }
323 :
324 : //______________________________________________________________________________
325 12620 : Any ProxyRoot::queryAggregation( Type const & rType )
326 : throw (RuntimeException)
327 : {
328 12620 : Any ret( OWeakAggObject::queryAggregation( rType ) );
329 12620 : if (! ret.hasValue())
330 : {
331 11486 : typelib_TypeDescription * pTypeDescr = 0;
332 11486 : TYPELIB_DANGER_GET( &pTypeDescr, rType.getTypeLibType() );
333 : try
334 : {
335 11486 : Reference< XInterface > xProxy;
336 11486 : uno_ExtEnvironment * cpp_env = m_factory->m_cpp_env.get()->pExtEnv;
337 : OSL_ASSERT( cpp_env != 0 );
338 :
339 : // mind a new delegator, calculate current root:
340 : Reference< XInterface > xRoot(
341 22972 : static_cast< OWeakObject * >(this), UNO_QUERY_THROW );
342 22972 : OUString oid;
343 11486 : (*cpp_env->getObjectIdentifier)( cpp_env, &oid.pData, xRoot.get() );
344 : OSL_ASSERT( !oid.isEmpty() );
345 :
346 : (*cpp_env->getRegisteredInterface)(
347 : cpp_env, reinterpret_cast< void ** >( &xProxy ),
348 : oid.pData, reinterpret_cast<
349 11486 : typelib_InterfaceTypeDescription * >(pTypeDescr) );
350 11486 : if (! xProxy.is())
351 : {
352 : // perform query on target:
353 : UnoInterfaceReference proxy_target(
354 : m_factory->binuno_queryInterface(
355 : m_target, reinterpret_cast<
356 3431 : typelib_InterfaceTypeDescription * >(pTypeDescr) ) );
357 3431 : if (proxy_target.is())
358 : {
359 : // ensure root's object entries:
360 2097 : UnoInterfaceReference root;
361 2097 : m_factory->m_cpp2uno.mapInterface(
362 : reinterpret_cast< void ** >( &root.m_pUnoI ),
363 4194 : xRoot.get(), ::getCppuType( &xRoot ) );
364 :
365 : UnoInterfaceReference proxy(
366 : // ref count initially 1:
367 2097 : new binuno_Proxy( this, proxy_target, oid, pTypeDescr ),
368 6291 : SAL_NO_ACQUIRE );
369 : uno_ExtEnvironment * uno_env =
370 2097 : m_factory->m_uno_env.get()->pExtEnv;
371 : OSL_ASSERT( uno_env != 0 );
372 : (*uno_env->registerProxyInterface)(
373 : uno_env, reinterpret_cast< void ** >( &proxy.m_pUnoI ),
374 : binuno_proxy_free, oid.pData,
375 : reinterpret_cast< typelib_InterfaceTypeDescription * >(
376 2097 : pTypeDescr ) );
377 :
378 2097 : m_factory->m_uno2cpp.mapInterface(
379 : reinterpret_cast< void ** >( &xProxy ),
380 6291 : proxy.get(), pTypeDescr );
381 3431 : }
382 : }
383 11486 : if (xProxy.is())
384 21638 : ret.setValue( &xProxy, pTypeDescr );
385 : }
386 0 : catch (...) // finally
387 : {
388 0 : TYPELIB_DANGER_RELEASE( pTypeDescr );
389 0 : throw;
390 : }
391 11486 : TYPELIB_DANGER_RELEASE( pTypeDescr );
392 : }
393 12620 : return ret;
394 : }
395 :
396 : //##############################################################################
397 :
398 : //______________________________________________________________________________
399 52 : FactoryImpl::FactoryImpl()
400 : {
401 52 : OUString uno = UNO_LB_UNO;
402 104 : OUString cpp = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
403 :
404 : uno_getEnvironment(
405 52 : reinterpret_cast< uno_Environment ** >( &m_uno_env ), uno.pData, 0 );
406 : OSL_ENSURE( m_uno_env.is(), "### cannot get binary uno env!" );
407 :
408 : uno_getEnvironment(
409 52 : reinterpret_cast< uno_Environment ** >( &m_cpp_env ), cpp.pData, 0 );
410 : OSL_ENSURE( m_cpp_env.is(), "### cannot get C++ uno env!" );
411 :
412 : uno_getMapping(
413 : reinterpret_cast< uno_Mapping ** >( &m_uno2cpp ),
414 52 : m_uno_env.get(), m_cpp_env.get(), 0 );
415 : OSL_ENSURE( m_uno2cpp.is(), "### cannot get bridge uno <-> C++!" );
416 :
417 : uno_getMapping(
418 : reinterpret_cast< uno_Mapping ** >( &m_cpp2uno ),
419 52 : m_cpp_env.get(), m_uno_env.get(), 0 );
420 52 : OSL_ENSURE( m_cpp2uno.is(), "### cannot get bridge C++ <-> uno!" );
421 52 : }
422 :
423 : //______________________________________________________________________________
424 88 : FactoryImpl::~FactoryImpl() {}
425 :
426 : // XProxyFactory
427 : //______________________________________________________________________________
428 669 : Reference< XAggregation > FactoryImpl::createProxy(
429 : Reference< XInterface > const & xTarget )
430 : throw (RuntimeException)
431 : {
432 669 : return new ProxyRoot( this, xTarget );
433 : }
434 :
435 : // XServiceInfo
436 : //______________________________________________________________________________
437 0 : OUString FactoryImpl::getImplementationName()
438 : throw (RuntimeException)
439 : {
440 0 : return proxyfac_getImplementationName();
441 : }
442 :
443 : //______________________________________________________________________________
444 0 : sal_Bool FactoryImpl::supportsService( const OUString & rServiceName )
445 : throw (RuntimeException)
446 : {
447 0 : Sequence< OUString > const & rSNL = getSupportedServiceNames();
448 0 : OUString const * pArray = rSNL.getConstArray();
449 0 : for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
450 : {
451 0 : if (rServiceName.equals( pArray[ nPos ] ))
452 0 : return true;
453 : }
454 0 : return false;
455 : }
456 :
457 : //______________________________________________________________________________
458 0 : Sequence< OUString > FactoryImpl::getSupportedServiceNames()
459 : throw(::com::sun::star::uno::RuntimeException)
460 : {
461 0 : return proxyfac_getSupportedServiceNames();
462 : }
463 :
464 : //==============================================================================
465 672 : static Reference< XInterface > SAL_CALL proxyfac_create(
466 : SAL_UNUSED_PARAMETER Reference< XComponentContext > const & )
467 : throw (Exception)
468 : {
469 672 : Reference< XInterface > xRet;
470 : {
471 672 : ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
472 672 : static WeakReference < XInterface > rwInstance;
473 672 : xRet = rwInstance;
474 :
475 672 : if (! xRet.is())
476 : {
477 52 : xRet = static_cast< ::cppu::OWeakObject * >(new FactoryImpl);
478 52 : rwInstance = xRet;
479 672 : }
480 : }
481 672 : return xRet;
482 : }
483 :
484 : static ::cppu::ImplementationEntry g_entries [] =
485 : {
486 : {
487 : proxyfac_create, proxyfac_getImplementationName,
488 : proxyfac_getSupportedServiceNames, ::cppu::createSingleComponentFactory,
489 : 0, 0
490 : },
491 : { 0, 0, 0, 0, 0, 0 }
492 : };
493 :
494 : }
495 :
496 16 : extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL proxyfac_component_getFactory(
497 : const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
498 : {
499 : return ::cppu::component_getFactoryHelper(
500 16 : pImplName, pServiceManager, pRegistryKey, g_entries );
501 : }
502 :
503 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|