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