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 "bridges/cpp_uno/shared/bridge.hxx"
22 :
23 : #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
24 : #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
25 :
26 : #include "com/sun/star/uno/XInterface.hpp"
27 : #include "osl/diagnose.h"
28 : #include "osl/interlck.h"
29 : #include "rtl/ustring.h"
30 : #include "sal/types.h"
31 : #include "typelib/typedescription.h"
32 : #include "uno/dispatcher.h"
33 : #include "uno/environment.h"
34 : #include "uno/mapping.h"
35 :
36 : namespace bridges { namespace cpp_uno { namespace shared {
37 :
38 0 : void freeMapping(uno_Mapping * pMapping)
39 : {
40 0 : delete static_cast< Bridge::Mapping * >( pMapping )->pBridge;
41 0 : }
42 :
43 0 : void acquireMapping(uno_Mapping * pMapping)
44 : {
45 0 : static_cast< Bridge::Mapping * >( pMapping )->pBridge->acquire();
46 0 : }
47 :
48 0 : void releaseMapping(uno_Mapping * pMapping)
49 : {
50 0 : static_cast< Bridge::Mapping * >( pMapping )->pBridge->release();
51 0 : }
52 :
53 0 : void cpp2unoMapping(
54 : uno_Mapping * pMapping, void ** ppUnoI, void * pCppI,
55 : typelib_InterfaceTypeDescription * pTypeDescr)
56 : {
57 : OSL_ENSURE( ppUnoI && pTypeDescr, "### null ptr!" );
58 0 : if (*ppUnoI)
59 : {
60 : (*reinterpret_cast< uno_Interface * >( *ppUnoI )->release)(
61 0 : reinterpret_cast< uno_Interface * >( *ppUnoI ) );
62 0 : *ppUnoI = 0;
63 : }
64 0 : if (pCppI)
65 : {
66 0 : Bridge * pBridge = static_cast< Bridge::Mapping * >( pMapping )->pBridge;
67 :
68 : // get object id of interface to be wrapped
69 0 : rtl_uString * pOId = 0;
70 : (*pBridge->pCppEnv->getObjectIdentifier)(
71 0 : pBridge->pCppEnv, &pOId, pCppI );
72 : OSL_ASSERT( pOId );
73 :
74 : // try to get any known interface from target environment
75 : (*pBridge->pUnoEnv->getRegisteredInterface)(
76 0 : pBridge->pUnoEnv, ppUnoI, pOId, pTypeDescr );
77 :
78 0 : if (! *ppUnoI) // no existing interface, register new proxy interface
79 : {
80 : // try to publish a new proxy (refcount initially 1)
81 : uno_Interface * pSurrogate
82 : = bridges::cpp_uno::shared::UnoInterfaceProxy::create(
83 : pBridge,
84 : static_cast< ::com::sun::star::uno::XInterface * >( pCppI ),
85 0 : pTypeDescr, pOId );
86 :
87 : // proxy may be exchanged during registration
88 : (*pBridge->pUnoEnv->registerProxyInterface)(
89 : pBridge->pUnoEnv, reinterpret_cast< void ** >( &pSurrogate ),
90 : freeUnoInterfaceProxy, pOId,
91 0 : pTypeDescr );
92 :
93 0 : *ppUnoI = pSurrogate;
94 : }
95 0 : ::rtl_uString_release( pOId );
96 : }
97 0 : }
98 :
99 0 : void uno2cppMapping(
100 : uno_Mapping * pMapping, void ** ppCppI, void * pUnoI,
101 : typelib_InterfaceTypeDescription * pTypeDescr)
102 : {
103 : OSL_ASSERT( ppCppI && pTypeDescr );
104 0 : if (*ppCppI)
105 : {
106 : static_cast< ::com::sun::star::uno::XInterface * >( *ppCppI )->
107 0 : release();
108 0 : *ppCppI = 0;
109 : }
110 0 : if (pUnoI)
111 : {
112 0 : Bridge * pBridge = static_cast< Bridge::Mapping * >( pMapping )->pBridge;
113 :
114 : // get object id of uno interface to be wrapped
115 0 : rtl_uString * pOId = 0;
116 : (*pBridge->pUnoEnv->getObjectIdentifier)(
117 0 : pBridge->pUnoEnv, &pOId, pUnoI );
118 : OSL_ASSERT( pOId );
119 :
120 : // try to get any known interface from target environment
121 : (*pBridge->pCppEnv->getRegisteredInterface)(
122 0 : pBridge->pCppEnv, ppCppI, pOId, pTypeDescr );
123 :
124 0 : if (! *ppCppI) // no existing interface, register new proxy interface
125 : {
126 : // try to publish a new proxy (ref count initially 1)
127 : com::sun::star::uno::XInterface * pProxy
128 : = bridges::cpp_uno::shared::CppInterfaceProxy::create(
129 : pBridge, static_cast< uno_Interface * >( pUnoI ),
130 0 : pTypeDescr, pOId );
131 :
132 : // proxy may be exchanged during registration
133 : (*pBridge->pCppEnv->registerProxyInterface)(
134 : pBridge->pCppEnv, reinterpret_cast< void ** >( &pProxy ),
135 : freeCppInterfaceProxy, pOId,
136 0 : pTypeDescr );
137 :
138 0 : *ppCppI = pProxy;
139 : }
140 0 : ::rtl_uString_release( pOId );
141 : }
142 0 : }
143 :
144 0 : uno_Mapping * Bridge::createMapping(
145 : uno_ExtEnvironment * pCppEnv, uno_ExtEnvironment * pUnoEnv,
146 : bool bExportCpp2Uno) SAL_THROW(())
147 : {
148 0 : Bridge * bridge = new Bridge(pCppEnv, pUnoEnv, bExportCpp2Uno);
149 0 : return bExportCpp2Uno ? &bridge->aCpp2Uno : &bridge->aUno2Cpp;
150 : }
151 :
152 0 : void Bridge::acquire() SAL_THROW(())
153 : {
154 0 : if (1 == osl_atomic_increment( &nRef ))
155 : {
156 0 : if (bExportCpp2Uno)
157 : {
158 0 : uno_Mapping * pMapping = &aCpp2Uno;
159 : ::uno_registerMapping(
160 : &pMapping, freeMapping, (uno_Environment *)pCppEnv,
161 0 : (uno_Environment *)pUnoEnv, 0 );
162 : }
163 : else
164 : {
165 0 : uno_Mapping * pMapping = &aUno2Cpp;
166 : ::uno_registerMapping(
167 : &pMapping, freeMapping, (uno_Environment *)pUnoEnv,
168 0 : (uno_Environment *)pCppEnv, 0 );
169 : }
170 : }
171 0 : }
172 :
173 0 : void Bridge::release() SAL_THROW(())
174 : {
175 0 : if (! osl_atomic_decrement( &nRef ))
176 : {
177 0 : ::uno_revokeMapping( bExportCpp2Uno ? &aCpp2Uno : &aUno2Cpp );
178 : }
179 0 : }
180 :
181 0 : Bridge::Bridge(
182 : uno_ExtEnvironment * pCppEnv_, uno_ExtEnvironment * pUnoEnv_,
183 : bool bExportCpp2Uno_) SAL_THROW(())
184 : : nRef( 1 )
185 : , pCppEnv( pCppEnv_ )
186 : , pUnoEnv( pUnoEnv_ )
187 0 : , bExportCpp2Uno( bExportCpp2Uno_ )
188 : {
189 0 : aCpp2Uno.pBridge = this;
190 0 : aCpp2Uno.acquire = acquireMapping;
191 0 : aCpp2Uno.release = releaseMapping;
192 0 : aCpp2Uno.mapInterface = cpp2unoMapping;
193 :
194 0 : aUno2Cpp.pBridge = this;
195 0 : aUno2Cpp.acquire = acquireMapping;
196 0 : aUno2Cpp.release = releaseMapping;
197 0 : aUno2Cpp.mapInterface = uno2cppMapping;
198 :
199 0 : (*((uno_Environment *)pCppEnv)->acquire)( (uno_Environment *)pCppEnv );
200 0 : (*((uno_Environment *)pUnoEnv)->acquire)( (uno_Environment *)pUnoEnv );
201 0 : }
202 :
203 0 : Bridge::~Bridge() SAL_THROW(())
204 : {
205 0 : (*((uno_Environment *)pUnoEnv)->release)( (uno_Environment *)pUnoEnv );
206 0 : (*((uno_Environment *)pCppEnv)->release)( (uno_Environment *)pCppEnv );
207 0 : }
208 :
209 : } } }
210 :
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|