1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_bridges.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "bridges/cpp_uno/shared/bridge.hxx" 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include "component.hxx" 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx" 36*cdf0e10cSrcweir #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx" 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #include "com/sun/star/uno/XInterface.hpp" 39*cdf0e10cSrcweir #include "osl/diagnose.h" 40*cdf0e10cSrcweir #include "osl/interlck.h" 41*cdf0e10cSrcweir #include "rtl/ustring.h" 42*cdf0e10cSrcweir #include "sal/types.h" 43*cdf0e10cSrcweir #include "typelib/typedescription.h" 44*cdf0e10cSrcweir #include "uno/dispatcher.h" 45*cdf0e10cSrcweir #include "uno/environment.h" 46*cdf0e10cSrcweir #include "uno/mapping.h" 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir namespace bridges { namespace cpp_uno { namespace shared { 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir void freeMapping(uno_Mapping * pMapping) 51*cdf0e10cSrcweir { 52*cdf0e10cSrcweir delete static_cast< Bridge::Mapping * >( pMapping )->pBridge; 53*cdf0e10cSrcweir } 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir void acquireMapping(uno_Mapping * pMapping) 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir static_cast< Bridge::Mapping * >( pMapping )->pBridge->acquire(); 58*cdf0e10cSrcweir } 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir void releaseMapping(uno_Mapping * pMapping) 61*cdf0e10cSrcweir { 62*cdf0e10cSrcweir static_cast< Bridge::Mapping * >( pMapping )->pBridge->release(); 63*cdf0e10cSrcweir } 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir void cpp2unoMapping( 66*cdf0e10cSrcweir uno_Mapping * pMapping, void ** ppUnoI, void * pCppI, 67*cdf0e10cSrcweir typelib_InterfaceTypeDescription * pTypeDescr) 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir OSL_ENSURE( ppUnoI && pTypeDescr, "### null ptr!" ); 70*cdf0e10cSrcweir if (*ppUnoI) 71*cdf0e10cSrcweir { 72*cdf0e10cSrcweir (*reinterpret_cast< uno_Interface * >( *ppUnoI )->release)( 73*cdf0e10cSrcweir reinterpret_cast< uno_Interface * >( *ppUnoI ) ); 74*cdf0e10cSrcweir *ppUnoI = 0; 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir if (pCppI) 77*cdf0e10cSrcweir { 78*cdf0e10cSrcweir Bridge * pBridge = static_cast< Bridge::Mapping * >( pMapping )->pBridge; 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir // get object id of interface to be wrapped 81*cdf0e10cSrcweir rtl_uString * pOId = 0; 82*cdf0e10cSrcweir (*pBridge->pCppEnv->getObjectIdentifier)( 83*cdf0e10cSrcweir pBridge->pCppEnv, &pOId, pCppI ); 84*cdf0e10cSrcweir OSL_ASSERT( pOId ); 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir // try to get any known interface from target environment 87*cdf0e10cSrcweir (*pBridge->pUnoEnv->getRegisteredInterface)( 88*cdf0e10cSrcweir pBridge->pUnoEnv, ppUnoI, pOId, pTypeDescr ); 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir if (! *ppUnoI) // no existing interface, register new proxy interface 91*cdf0e10cSrcweir { 92*cdf0e10cSrcweir // try to publish a new proxy (refcount initially 1) 93*cdf0e10cSrcweir uno_Interface * pSurrogate 94*cdf0e10cSrcweir = bridges::cpp_uno::shared::UnoInterfaceProxy::create( 95*cdf0e10cSrcweir pBridge, 96*cdf0e10cSrcweir static_cast< ::com::sun::star::uno::XInterface * >( pCppI ), 97*cdf0e10cSrcweir pTypeDescr, pOId ); 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir // proxy may be exchanged during registration 100*cdf0e10cSrcweir (*pBridge->pUnoEnv->registerProxyInterface)( 101*cdf0e10cSrcweir pBridge->pUnoEnv, reinterpret_cast< void ** >( &pSurrogate ), 102*cdf0e10cSrcweir freeUnoInterfaceProxy, pOId, 103*cdf0e10cSrcweir pTypeDescr ); 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir *ppUnoI = pSurrogate; 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir ::rtl_uString_release( pOId ); 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir } 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir void uno2cppMapping( 112*cdf0e10cSrcweir uno_Mapping * pMapping, void ** ppCppI, void * pUnoI, 113*cdf0e10cSrcweir typelib_InterfaceTypeDescription * pTypeDescr) 114*cdf0e10cSrcweir { 115*cdf0e10cSrcweir OSL_ASSERT( ppCppI && pTypeDescr ); 116*cdf0e10cSrcweir if (*ppCppI) 117*cdf0e10cSrcweir { 118*cdf0e10cSrcweir static_cast< ::com::sun::star::uno::XInterface * >( *ppCppI )-> 119*cdf0e10cSrcweir release(); 120*cdf0e10cSrcweir *ppCppI = 0; 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir if (pUnoI) 123*cdf0e10cSrcweir { 124*cdf0e10cSrcweir Bridge * pBridge = static_cast< Bridge::Mapping * >( pMapping )->pBridge; 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir // get object id of uno interface to be wrapped 127*cdf0e10cSrcweir rtl_uString * pOId = 0; 128*cdf0e10cSrcweir (*pBridge->pUnoEnv->getObjectIdentifier)( 129*cdf0e10cSrcweir pBridge->pUnoEnv, &pOId, pUnoI ); 130*cdf0e10cSrcweir OSL_ASSERT( pOId ); 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir // try to get any known interface from target environment 133*cdf0e10cSrcweir (*pBridge->pCppEnv->getRegisteredInterface)( 134*cdf0e10cSrcweir pBridge->pCppEnv, ppCppI, pOId, pTypeDescr ); 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir if (! *ppCppI) // no existing interface, register new proxy interface 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir // try to publish a new proxy (ref count initially 1) 139*cdf0e10cSrcweir com::sun::star::uno::XInterface * pProxy 140*cdf0e10cSrcweir = bridges::cpp_uno::shared::CppInterfaceProxy::create( 141*cdf0e10cSrcweir pBridge, static_cast< uno_Interface * >( pUnoI ), 142*cdf0e10cSrcweir pTypeDescr, pOId ); 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir // proxy may be exchanged during registration 145*cdf0e10cSrcweir (*pBridge->pCppEnv->registerProxyInterface)( 146*cdf0e10cSrcweir pBridge->pCppEnv, reinterpret_cast< void ** >( &pProxy ), 147*cdf0e10cSrcweir freeCppInterfaceProxy, pOId, 148*cdf0e10cSrcweir pTypeDescr ); 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir *ppCppI = pProxy; 151*cdf0e10cSrcweir } 152*cdf0e10cSrcweir ::rtl_uString_release( pOId ); 153*cdf0e10cSrcweir } 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir uno_Mapping * Bridge::createMapping( 157*cdf0e10cSrcweir uno_ExtEnvironment * pCppEnv, uno_ExtEnvironment * pUnoEnv, 158*cdf0e10cSrcweir bool bExportCpp2Uno) SAL_THROW(()) 159*cdf0e10cSrcweir { 160*cdf0e10cSrcweir Bridge * bridge = new Bridge(pCppEnv, pUnoEnv, bExportCpp2Uno); 161*cdf0e10cSrcweir return bExportCpp2Uno ? &bridge->aCpp2Uno : &bridge->aUno2Cpp; 162*cdf0e10cSrcweir } 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir void Bridge::acquire() SAL_THROW(()) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir if (1 == osl_incrementInterlockedCount( &nRef )) 167*cdf0e10cSrcweir { 168*cdf0e10cSrcweir if (bExportCpp2Uno) 169*cdf0e10cSrcweir { 170*cdf0e10cSrcweir uno_Mapping * pMapping = &aCpp2Uno; 171*cdf0e10cSrcweir ::uno_registerMapping( 172*cdf0e10cSrcweir &pMapping, freeMapping, (uno_Environment *)pCppEnv, 173*cdf0e10cSrcweir (uno_Environment *)pUnoEnv, 0 ); 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir else 176*cdf0e10cSrcweir { 177*cdf0e10cSrcweir uno_Mapping * pMapping = &aUno2Cpp; 178*cdf0e10cSrcweir ::uno_registerMapping( 179*cdf0e10cSrcweir &pMapping, freeMapping, (uno_Environment *)pUnoEnv, 180*cdf0e10cSrcweir (uno_Environment *)pCppEnv, 0 ); 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir } 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir void Bridge::release() SAL_THROW(()) 186*cdf0e10cSrcweir { 187*cdf0e10cSrcweir if (! osl_decrementInterlockedCount( &nRef )) 188*cdf0e10cSrcweir { 189*cdf0e10cSrcweir ::uno_revokeMapping( bExportCpp2Uno ? &aCpp2Uno : &aUno2Cpp ); 190*cdf0e10cSrcweir } 191*cdf0e10cSrcweir } 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir Bridge::Bridge( 194*cdf0e10cSrcweir uno_ExtEnvironment * pCppEnv_, uno_ExtEnvironment * pUnoEnv_, 195*cdf0e10cSrcweir bool bExportCpp2Uno_) SAL_THROW(()) 196*cdf0e10cSrcweir : nRef( 1 ) 197*cdf0e10cSrcweir , pCppEnv( pCppEnv_ ) 198*cdf0e10cSrcweir , pUnoEnv( pUnoEnv_ ) 199*cdf0e10cSrcweir , bExportCpp2Uno( bExportCpp2Uno_ ) 200*cdf0e10cSrcweir { 201*cdf0e10cSrcweir bridges::cpp_uno::shared::g_moduleCount.modCnt.acquire( 202*cdf0e10cSrcweir &bridges::cpp_uno::shared::g_moduleCount.modCnt ); 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir aCpp2Uno.pBridge = this; 205*cdf0e10cSrcweir aCpp2Uno.acquire = acquireMapping; 206*cdf0e10cSrcweir aCpp2Uno.release = releaseMapping; 207*cdf0e10cSrcweir aCpp2Uno.mapInterface = cpp2unoMapping; 208*cdf0e10cSrcweir 209*cdf0e10cSrcweir aUno2Cpp.pBridge = this; 210*cdf0e10cSrcweir aUno2Cpp.acquire = acquireMapping; 211*cdf0e10cSrcweir aUno2Cpp.release = releaseMapping; 212*cdf0e10cSrcweir aUno2Cpp.mapInterface = uno2cppMapping; 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir (*((uno_Environment *)pCppEnv)->acquire)( (uno_Environment *)pCppEnv ); 215*cdf0e10cSrcweir (*((uno_Environment *)pUnoEnv)->acquire)( (uno_Environment *)pUnoEnv ); 216*cdf0e10cSrcweir } 217*cdf0e10cSrcweir 218*cdf0e10cSrcweir Bridge::~Bridge() SAL_THROW(()) 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir (*((uno_Environment *)pUnoEnv)->release)( (uno_Environment *)pUnoEnv ); 221*cdf0e10cSrcweir (*((uno_Environment *)pCppEnv)->release)( (uno_Environment *)pCppEnv ); 222*cdf0e10cSrcweir bridges::cpp_uno::shared::g_moduleCount.modCnt.release( 223*cdf0e10cSrcweir &bridges::cpp_uno::shared::g_moduleCount.modCnt ); 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir } } } 227