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 16677 : void freeMapping(uno_Mapping * pMapping)
39 : {
40 16677 : delete static_cast< Bridge::Mapping * >( pMapping )->pBridge;
41 16677 : }
42 :
43 112939 : void acquireMapping(uno_Mapping * pMapping)
44 : {
45 112939 : static_cast< Bridge::Mapping * >( pMapping )->pBridge->acquire();
46 112939 : }
47 :
48 129586 : void releaseMapping(uno_Mapping * pMapping)
49 : {
50 129586 : static_cast< Bridge::Mapping * >( pMapping )->pBridge->release();
51 129586 : }
52 :
53 216385 : void cpp2unoMapping(
54 : uno_Mapping * pMapping, void ** ppUnoI, void * pCppI,
55 : typelib_InterfaceTypeDescription * pTypeDescr)
56 : {
57 : OSL_ENSURE( ppUnoI && pTypeDescr, "### null ptr!" );
58 216385 : if (*ppUnoI)
59 : {
60 : (*reinterpret_cast< uno_Interface * >( *ppUnoI )->release)(
61 0 : reinterpret_cast< uno_Interface * >( *ppUnoI ) );
62 0 : *ppUnoI = 0;
63 : }
64 216385 : if (pCppI)
65 : {
66 216381 : Bridge * pBridge = static_cast< Bridge::Mapping * >( pMapping )->pBridge;
67 :
68 : // get object id of interface to be wrapped
69 216381 : rtl_uString * pOId = 0;
70 : (*pBridge->pCppEnv->getObjectIdentifier)(
71 216381 : pBridge->pCppEnv, &pOId, pCppI );
72 : OSL_ASSERT( pOId );
73 :
74 : // try to get any known interface from target environment
75 : (*pBridge->pUnoEnv->getRegisteredInterface)(
76 216381 : pBridge->pUnoEnv, ppUnoI, pOId, pTypeDescr );
77 :
78 216381 : 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 165183 : 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 165183 : pTypeDescr );
92 :
93 165183 : *ppUnoI = pSurrogate;
94 : }
95 216381 : ::rtl_uString_release( pOId );
96 : }
97 216385 : }
98 :
99 112024 : void uno2cppMapping(
100 : uno_Mapping * pMapping, void ** ppCppI, void * pUnoI,
101 : typelib_InterfaceTypeDescription * pTypeDescr)
102 : {
103 : OSL_ASSERT( ppCppI && pTypeDescr );
104 112024 : if (*ppCppI)
105 : {
106 : static_cast< ::com::sun::star::uno::XInterface * >( *ppCppI )->
107 0 : release();
108 0 : *ppCppI = 0;
109 : }
110 112024 : if (pUnoI)
111 : {
112 112024 : Bridge * pBridge = static_cast< Bridge::Mapping * >( pMapping )->pBridge;
113 :
114 : // get object id of uno interface to be wrapped
115 112024 : rtl_uString * pOId = 0;
116 : (*pBridge->pUnoEnv->getObjectIdentifier)(
117 112024 : pBridge->pUnoEnv, &pOId, pUnoI );
118 : OSL_ASSERT( pOId );
119 :
120 : // try to get any known interface from target environment
121 : (*pBridge->pCppEnv->getRegisteredInterface)(
122 112024 : pBridge->pCppEnv, ppCppI, pOId, pTypeDescr );
123 :
124 112024 : 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 46008 : 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 46008 : pTypeDescr );
137 :
138 46008 : *ppCppI = pProxy;
139 : }
140 112024 : ::rtl_uString_release( pOId );
141 : }
142 112024 : }
143 :
144 16815 : uno_Mapping * Bridge::createMapping(
145 : uno_ExtEnvironment * pCppEnv, uno_ExtEnvironment * pUnoEnv,
146 : bool bExportCpp2Uno)
147 : {
148 16815 : Bridge * bridge = new Bridge(pCppEnv, pUnoEnv, bExportCpp2Uno);
149 16815 : return bExportCpp2Uno ? &bridge->aCpp2Uno : &bridge->aUno2Cpp;
150 : }
151 :
152 324130 : void Bridge::acquire()
153 : {
154 324130 : 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 324130 : }
172 :
173 338291 : void Bridge::release()
174 : {
175 338291 : if (! osl_atomic_decrement( &nRef ))
176 : {
177 16677 : ::uno_revokeMapping( bExportCpp2Uno ? &aCpp2Uno : &aUno2Cpp );
178 : }
179 338291 : }
180 :
181 16815 : Bridge::Bridge(
182 : uno_ExtEnvironment * pCppEnv_, uno_ExtEnvironment * pUnoEnv_,
183 : bool bExportCpp2Uno_)
184 : : nRef( 1 )
185 : , pCppEnv( pCppEnv_ )
186 : , pUnoEnv( pUnoEnv_ )
187 16815 : , bExportCpp2Uno( bExportCpp2Uno_ )
188 : {
189 16815 : aCpp2Uno.pBridge = this;
190 16815 : aCpp2Uno.acquire = acquireMapping;
191 16815 : aCpp2Uno.release = releaseMapping;
192 16815 : aCpp2Uno.mapInterface = cpp2unoMapping;
193 :
194 16815 : aUno2Cpp.pBridge = this;
195 16815 : aUno2Cpp.acquire = acquireMapping;
196 16815 : aUno2Cpp.release = releaseMapping;
197 16815 : aUno2Cpp.mapInterface = uno2cppMapping;
198 :
199 16815 : (*((uno_Environment *)pCppEnv)->acquire)( (uno_Environment *)pCppEnv );
200 16815 : (*((uno_Environment *)pUnoEnv)->acquire)( (uno_Environment *)pUnoEnv );
201 16815 : }
202 :
203 16677 : Bridge::~Bridge()
204 : {
205 16677 : (*((uno_Environment *)pUnoEnv)->release)( (uno_Environment *)pUnoEnv );
206 16677 : (*((uno_Environment *)pCppEnv)->release)( (uno_Environment *)pCppEnv );
207 16677 : }
208 :
209 : } } }
210 :
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|