Update ooo320-m1
[ooovba.git] / bridges / source / remote / static / mapping.cxx
blob84239bf1fd3c9c9c240a36f3a311994950bd4103
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mapping.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_bridges.hxx"
33 #include <osl/diagnose.h>
35 #include <bridges/remote/proxy.hxx>
36 #include <bridges/remote/stub.hxx>
37 #include <bridges/remote/counter.hxx>
38 #include <bridges/remote/mapping.hxx>
40 using namespace bridges_remote;
42 extern "C" {
44 static void SAL_CALL thisAcquire( uno_Mapping *pMap )
46 RemoteMapping *p = SAL_REINTERPRET_CAST( RemoteMapping * , pMap );
47 if( 1 == osl_incrementInterlockedCount( &(p->m_nRef) ) )
49 if( remoteToUno == pMap->mapInterface )
51 uno_registerMapping( &pMap ,
52 freeRemoteMapping,
53 p->pEnvRemote ,
54 p->pEnvUno ,
55 p->m_sPurpose.pData );
57 else
59 uno_registerMapping( &pMap ,
60 freeRemoteMapping,
61 p->pEnvUno ,
62 p->pEnvRemote ,
63 p->m_sPurpose.pData );
69 static void SAL_CALL thisRelease( uno_Mapping *pMap )
71 RemoteMapping *p = SAL_REINTERPRET_CAST( RemoteMapping * , pMap );
72 if (! osl_decrementInterlockedCount( &(p->m_nRef) ))
74 uno_revokeMapping( pMap );
80 namespace bridges_remote {
82 void remoteToUno( uno_Mapping *pMapping, void **ppUnoI, void *pRemoteI,
83 typelib_InterfaceTypeDescription *pTypeDescr )
85 remote_Mapping *pRemoteMapping = ( remote_Mapping * ) pMapping;
87 OSL_ASSERT( ppUnoI && pTypeDescr );
88 if (*ppUnoI)
90 ((uno_Interface *)*ppUnoI)->release( (uno_Interface *)*ppUnoI );
91 *ppUnoI = 0;
94 if (pRemoteI && pTypeDescr)
96 // get object id of interface to be wrapped
97 rtl_uString * pOid = 0;
98 pRemoteMapping->pEnvRemote->pExtEnv->getObjectIdentifier(
99 pRemoteMapping->pEnvRemote->pExtEnv,
100 &pOid,
101 pRemoteI );
103 OSL_ASSERT(pOid);
104 if( ! pOid )
106 return;
109 // try to get any known interface from target environment
110 pRemoteMapping->pEnvUno->pExtEnv->getRegisteredInterface(
111 pRemoteMapping->pEnvUno->pExtEnv,
112 ppUnoI,
113 pOid,
114 pTypeDescr);
116 if ( ! *ppUnoI) // already existing interface
118 // try to publish a new proxy; proxy may be exchanged during registration
119 *ppUnoI = new Remote2UnoProxy(
120 ( remote_Interface * ) pRemoteI,
121 pOid,
122 pTypeDescr ,
123 pRemoteMapping->pEnvUno,
124 pRemoteMapping->pEnvRemote);
126 pRemoteMapping->pEnvUno->pExtEnv->registerProxyInterface(
127 pRemoteMapping->pEnvUno->pExtEnv,
128 ppUnoI,
129 freeRemote2UnoProxy,
130 pOid,
131 pTypeDescr );
133 OSL_ASSERT( *ppUnoI );
135 rtl_uString_release( pOid );
139 void unoToRemote( uno_Mapping *pMapping, void **ppRemoteI, void *pUnoI,
140 typelib_InterfaceTypeDescription *pTypeDescr )
142 remote_Mapping *pRemoteMapping = ( remote_Mapping * ) pMapping;
143 OSL_ASSERT( ppRemoteI && pTypeDescr );
144 if (*ppRemoteI)
146 ((remote_Interface *)*ppRemoteI)->release( (remote_Interface *)*ppRemoteI);
147 *ppRemoteI = 0;
149 if (pUnoI && pTypeDescr)
151 // get object id of interface to be wrapped
152 rtl_uString * pOid = 0;
153 pRemoteMapping->pEnvUno->pExtEnv->getObjectIdentifier(
154 pRemoteMapping->pEnvUno->pExtEnv,
155 &pOid,
156 pUnoI );
158 OSL_ASSERT( pOid );
159 if( ! pOid )
161 return;
164 pRemoteMapping->pEnvRemote->pExtEnv->getRegisteredInterface(
165 pRemoteMapping->pEnvRemote->pExtEnv,
166 (void**)ppRemoteI,
167 pOid,
168 pTypeDescr );
170 if( !*ppRemoteI )
172 // try to publish a new proxy;
173 *ppRemoteI = new Uno2RemoteStub(
174 ( uno_Interface * ) pUnoI,
175 pOid,
176 pTypeDescr,
177 pRemoteMapping->pEnvUno,
178 pRemoteMapping->pEnvRemote );
180 // note : ppRemoteI may change during registration
181 pRemoteMapping->pEnvRemote->pExtEnv->registerProxyInterface(
182 pRemoteMapping->pEnvRemote->pExtEnv,
183 (void**) ppRemoteI,
184 freeUno2RemoteStub,
185 pOid,
186 pTypeDescr );
189 rtl_uString_release( pOid );
193 void freeRemoteMapping(uno_Mapping * mapping) {
194 delete reinterpret_cast< RemoteMapping * >(mapping);
197 RemoteMapping::RemoteMapping( uno_Environment *pEnvUno_ ,
198 uno_Environment *pEnvRemote_,
199 uno_MapInterfaceFunc func,
200 const ::rtl::OUString sPurpose) :
201 m_nRef( 1 ),
202 m_sPurpose( sPurpose )
204 pEnvUno = pEnvUno_;
205 pEnvRemote = pEnvRemote_;
207 pEnvUno->acquire( pEnvUno );
208 pEnvRemote->acquire( pEnvRemote );
210 aBase.mapInterface = func;
211 aBase.acquire = thisAcquire;
212 aBase.release = thisRelease;
215 RemoteMapping::~RemoteMapping( )
217 pEnvUno->release( pEnvUno );
218 pEnvRemote->release( pEnvRemote );