LCOV - code coverage report
Current view: top level - stoc/source/proxy_factory - proxyfac.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 125 146 85.6 %
Date: 2015-06-13 12:38:46 Functions: 21 22 95.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11