LCOV - code coverage report
Current view: top level - stoc/source/proxy_factory - proxyfac.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 121 146 82.9 %
Date: 2014-11-03 Functions: 19 22 86.4 %
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          42 : static OUString proxyfac_getImplementationName()
      54             : {
      55          42 :     return OUString(IMPL_NAME);
      56             : }
      57             : 
      58          42 : static Sequence< OUString > proxyfac_getSupportedServiceNames()
      59             : {
      60          42 :     OUString str_name = SERVICE_NAME;
      61          42 :     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        7636 : 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        7636 :     if (s_pQITD == 0)
     102             :     {
     103          24 :         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
     104          24 :         if (s_pQITD == 0)
     105             :         {
     106          24 :             typelib_TypeDescription * pTXInterfaceDescr = 0;
     107             :             TYPELIB_DANGER_GET(
     108             :                 &pTXInterfaceDescr,
     109          24 :                 cppu::UnoType<XInterface>::get().getTypeLibType() );
     110          24 :             typelib_TypeDescription * pQITD = 0;
     111             :             typelib_typedescriptionreference_getDescription(
     112             :                 &pQITD, reinterpret_cast< typelib_InterfaceTypeDescription * >(
     113          24 :                     pTXInterfaceDescr )->ppAllMembers[ 0 ] );
     114          24 :             TYPELIB_DANGER_RELEASE( pTXInterfaceDescr );
     115             :             OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
     116          24 :             s_pQITD = pQITD;
     117          24 :         }
     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        7636 :         pTypeDescr )->pWeakRef;
     127             :     uno_Any ret_val, exc_space;
     128        7636 :     uno_Any * exc = &exc_space;
     129             : 
     130        7636 :     unoI.dispatch( s_pQITD, &ret_val, args, &exc );
     131             : 
     132        7636 :     if (exc == 0)
     133             :     {
     134        7636 :         UnoInterfaceReference ret;
     135        7636 :         if (ret_val.pType->eTypeClass == typelib_TypeClass_INTERFACE)
     136             :         {
     137             :             ret.set( *reinterpret_cast< uno_Interface ** >(ret_val.pData),
     138        4628 :                      SAL_NO_ACQUIRE );
     139        4628 :             typelib_typedescriptionreference_release( ret_val.pType );
     140             :         }
     141             :         else
     142             :         {
     143        3008 :             uno_any_destruct( &ret_val, 0 );
     144             :         }
     145        7636 :         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, ::getCppuType( &cpp_exc ).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        3888 : 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        3888 : 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        3888 :         reinterpret_cast< uno_Interface * >( pProxy ) );
     207             :     OSL_ASSERT( proxy->m_root->m_factory->m_uno_env.get()->pExtEnv == pEnv );
     208        3888 :     delete proxy;
     209        3888 : }
     210             : 
     211             : 
     212       12810 : static void SAL_CALL binuno_proxy_acquire( uno_Interface * pUnoI )
     213             : {
     214       12810 :     binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
     215       12810 :     if (osl_atomic_increment( &that->m_nRefCount ) == 1)
     216             :     {
     217             :         // rebirth of zombie
     218             :         uno_ExtEnvironment * uno_env =
     219         446 :             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         446 :                 that->m_typeDescr.get() ) );
     226             :         OSL_ASSERT( that == static_cast< binuno_Proxy * >( pUnoI ) );
     227             :     }
     228       12810 : }
     229             : 
     230             : 
     231       16260 : static void SAL_CALL binuno_proxy_release( uno_Interface * pUnoI )
     232             : {
     233       16260 :     binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
     234       16260 :     if (osl_atomic_decrement( &that->m_nRefCount ) == 0)
     235             :     {
     236             :         uno_ExtEnvironment * uno_env =
     237        3898 :             that->m_root->m_factory->m_uno_env.get()->pExtEnv;
     238             :         OSL_ASSERT( uno_env != 0 );
     239        3898 :         (*uno_env->revokeInterface)( uno_env, pUnoI );
     240             :     }
     241       16260 : }
     242             : 
     243             : 
     244        9182 : 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        9182 :     binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
     249        9182 :     switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription const * >(
     250             :                 pMemberType )->nPosition)
     251             :     {
     252             :     case 0: // queryInterface()
     253             :     {
     254             :         try
     255             :         {
     256             :             Type const & rType =
     257        4602 :                 *reinterpret_cast< Type const * >( pArgs[ 0 ] );
     258        4602 :             Any ret( that->m_root->queryInterface( rType ) );
     259             :             uno_type_copyAndConvertData(
     260        4602 :                 pReturn, &ret, ::getCppuType( &ret ).getTypeLibType(),
     261        9204 :                 that->m_root->m_factory->m_cpp2uno.get() );
     262        4602 :             *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        4602 :         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        4580 :         that->m_target.dispatch( pMemberType, pReturn, pArgs, ppException );
     284        4580 :         break;
     285             :     }
     286        9182 : }
     287             : 
     288             : }
     289             : 
     290             : 
     291        4628 : 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        4628 :       m_typeDescr( typeDescr )
     300             : {
     301        4628 :     uno_Interface::acquire = binuno_proxy_acquire;
     302        4628 :     uno_Interface::release = binuno_proxy_release;
     303        4628 :     uno_Interface::pDispatcher = binuno_proxy_dispatch;
     304        4628 : }
     305             : 
     306             : 
     307        1860 : ProxyRoot::~ProxyRoot()
     308             : {
     309        1860 : }
     310             : 
     311             : 
     312        1656 : inline ProxyRoot::ProxyRoot(
     313             :     ::rtl::Reference< FactoryImpl > const & factory,
     314             :     Reference< XInterface > const & xTarget )
     315        1656 :     : m_factory( factory )
     316             : {
     317        1656 :     m_factory->m_cpp2uno.mapInterface(
     318        1656 :         reinterpret_cast< void ** >( &m_target.m_pUnoI ), xTarget.get(),
     319        4968 :         ::getCppuType( &xTarget ) );
     320             :     OSL_ENSURE( m_target.is(), "### mapping interface failed!" );
     321        1656 : }
     322             : 
     323             : 
     324       26984 : Any ProxyRoot::queryAggregation( Type const & rType )
     325             :     throw (RuntimeException, std::exception)
     326             : {
     327       26984 :     Any ret( OWeakAggObject::queryAggregation( rType ) );
     328       26984 :     if (! ret.hasValue())
     329             :     {
     330       23744 :         typelib_TypeDescription * pTypeDescr = 0;
     331       23744 :         TYPELIB_DANGER_GET( &pTypeDescr, rType.getTypeLibType() );
     332             :         try
     333             :         {
     334       23744 :             Reference< XInterface > xProxy;
     335       23744 :             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       47488 :                 static_cast< OWeakObject * >(this), UNO_QUERY_THROW );
     341       47488 :             OUString oid;
     342       23744 :             (*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       23744 :                 typelib_InterfaceTypeDescription * >(pTypeDescr) );
     349       23744 :             if (! xProxy.is())
     350             :             {
     351             :                 // perform query on target:
     352             :                 UnoInterfaceReference proxy_target(
     353             :                     m_factory->binuno_queryInterface(
     354             :                         m_target, reinterpret_cast<
     355        7636 :                         typelib_InterfaceTypeDescription * >(pTypeDescr) ) );
     356        7636 :                 if (proxy_target.is())
     357             :                 {
     358             :                     // ensure root's object entries:
     359        4628 :                     UnoInterfaceReference root;
     360        4628 :                     m_factory->m_cpp2uno.mapInterface(
     361             :                         reinterpret_cast< void ** >( &root.m_pUnoI ),
     362        9256 :                         xRoot.get(), ::getCppuType( &xRoot ) );
     363             : 
     364             :                     UnoInterfaceReference proxy(
     365             :                         // ref count initially 1:
     366        4628 :                         new binuno_Proxy( this, proxy_target, oid, pTypeDescr ),
     367       13884 :                         SAL_NO_ACQUIRE );
     368             :                     uno_ExtEnvironment * uno_env =
     369        4628 :                         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        4628 :                             pTypeDescr ) );
     376             : 
     377        4628 :                     m_factory->m_uno2cpp.mapInterface(
     378             :                         reinterpret_cast< void ** >( &xProxy ),
     379       13884 :                         proxy.get(), pTypeDescr );
     380        7636 :                 }
     381             :             }
     382       23744 :             if (xProxy.is())
     383       44480 :                 ret.setValue( &xProxy, pTypeDescr );
     384             :         }
     385           0 :         catch (...) // finally
     386             :         {
     387           0 :             TYPELIB_DANGER_RELEASE( pTypeDescr );
     388           0 :             throw;
     389             :         }
     390       23744 :         TYPELIB_DANGER_RELEASE( pTypeDescr );
     391             :     }
     392       26984 :     return ret;
     393             : }
     394             : 
     395             : 
     396             : 
     397             : 
     398         484 : FactoryImpl::FactoryImpl()
     399             : {
     400         484 :     OUString uno = UNO_LB_UNO;
     401         968 :     OUString cpp = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
     402             : 
     403             :     uno_getEnvironment(
     404         484 :         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         484 :         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         484 :         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         484 :         m_cpp_env.get(), m_uno_env.get(), 0 );
     419         484 :     OSL_ENSURE( m_cpp2uno.is(), "### cannot get bridge C++ <-> uno!" );
     420         484 : }
     421             : 
     422             : 
     423         924 : FactoryImpl::~FactoryImpl() {}
     424             : 
     425             : // XProxyFactory
     426             : 
     427        1656 : Reference< XAggregation > FactoryImpl::createProxy(
     428             :     Reference< XInterface > const & xTarget )
     429             :     throw (RuntimeException, std::exception)
     430             : {
     431        1656 :     return new ProxyRoot( this, xTarget );
     432             : }
     433             : 
     434             : // XServiceInfo
     435             : 
     436           0 : OUString FactoryImpl::getImplementationName()
     437             :     throw (RuntimeException, std::exception)
     438             : {
     439           0 :     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           0 : Sequence< OUString > FactoryImpl::getSupportedServiceNames()
     449             :     throw(css::uno::RuntimeException, std::exception)
     450             : {
     451           0 :     return proxyfac_getSupportedServiceNames();
     452             : }
     453             : 
     454             : 
     455        1672 : static Reference< XInterface > SAL_CALL proxyfac_create(
     456             :     SAL_UNUSED_PARAMETER Reference< XComponentContext > const & )
     457             :     throw (Exception)
     458             : {
     459        1672 :     Reference< XInterface > xRet;
     460             :     {
     461        1672 :     ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
     462        1672 :     static WeakReference < XInterface > rwInstance;
     463        1672 :     xRet = rwInstance;
     464             : 
     465        1672 :     if (! xRet.is())
     466             :     {
     467         484 :         xRet = static_cast< ::cppu::OWeakObject * >(new FactoryImpl);
     468         484 :         rwInstance = xRet;
     469        1672 :     }
     470             :     }
     471        1672 :     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          42 : 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          42 :         pImplName, pServiceManager, pRegistryKey, g_entries );
     491             : }
     492             : 
     493             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10