LibreOffice Module stoc (master) 1
proxyfac.cxx
Go to the documentation of this file.
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 <osl/diagnose.h>
22#include <osl/interlck.h>
23#include <rtl/ref.hxx>
24#include <uno/dispatcher.hxx>
25#include <uno/data.h>
26#include <uno/lbnames.h>
27#include <uno/mapping.hxx>
28#include <uno/environment.hxx>
29#include <typelib/typedescription.hxx>
33#include <cppuhelper/weak.hxx>
35#include <com/sun/star/lang/XServiceInfo.hpp>
36#include <com/sun/star/reflection/XProxyFactory.hpp>
37#include <com/sun/star/uno/RuntimeException.hpp>
38#include <com/sun/star/uno/XComponentContext.hpp>
39#include <utility>
40
41
42using namespace ::com::sun::star;
43using namespace css::uno;
44
45
46namespace
47{
48
49struct FactoryImpl : public ::cppu::WeakImplHelper< lang::XServiceInfo,
50 reflection::XProxyFactory >
51{
52 Environment m_uno_env;
53 Environment m_cpp_env;
54 Mapping m_uno2cpp;
55 Mapping m_cpp2uno;
56
57 UnoInterfaceReference binuno_queryInterface(
58 UnoInterfaceReference const & unoI,
59 typelib_InterfaceTypeDescription * pTypeDescr );
60
61 FactoryImpl();
62
63 // XServiceInfo
64 virtual OUString SAL_CALL getImplementationName() override;
65 virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName ) override;
66 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
67
68 // XProxyFactory
69 virtual Reference< XAggregation > SAL_CALL createProxy(
70 Reference< XInterface > const & xTarget ) override;
71};
72
73
74UnoInterfaceReference FactoryImpl::binuno_queryInterface(
75 UnoInterfaceReference const & unoI,
76 typelib_InterfaceTypeDescription * pTypeDescr )
77{
78 // init queryInterface() td
79 static typelib_TypeDescription* s_pQITD = []() {
80 typelib_TypeDescription* pTXInterfaceDescr = nullptr;
81 TYPELIB_DANGER_GET(&pTXInterfaceDescr, cppu::UnoType<XInterface>::get().getTypeLibType());
82 typelib_TypeDescription* pQITD = nullptr;
84 &pQITD, reinterpret_cast<typelib_InterfaceTypeDescription*>(pTXInterfaceDescr)
85 ->ppAllMembers[0]);
86 TYPELIB_DANGER_RELEASE(pTXInterfaceDescr);
87 return pQITD;
88 }();
89
90 void * args[ 1 ];
91 args[ 0 ] = &reinterpret_cast< typelib_TypeDescription * >(
92 pTypeDescr )->pWeakRef;
93 uno_Any ret_val, exc_space;
94 uno_Any * exc = &exc_space;
95
96 unoI.dispatch( s_pQITD, &ret_val, args, &exc );
97
98 if (exc == nullptr)
99 {
100 UnoInterfaceReference ret;
101 if (ret_val.pType->eTypeClass == typelib_TypeClass_INTERFACE)
102 {
103 ret.set( *static_cast< uno_Interface ** >(ret_val.pData),
104 SAL_NO_ACQUIRE );
106 }
107 else
108 {
109 uno_any_destruct( &ret_val, nullptr );
110 }
111 return ret;
112 }
113 else
114 {
115 // exception occurred:
116 OSL_ENSURE(
118 exc->pType ),
119 "### RuntimeException expected!" );
120 Any cpp_exc;
122 &cpp_exc, exc, cppu::UnoType<decltype(cpp_exc)>::get().getTypeLibType(),
123 m_uno2cpp.get() );
124 uno_any_destruct( exc, nullptr );
125 ::cppu::throwException( cpp_exc );
126 OSL_ASSERT( false ); // way of no return
127 return UnoInterfaceReference(); // for dummy
128 }
129}
130
131
132struct ProxyRoot : public ::cppu::OWeakAggObject
133{
134 // XAggregation
135 virtual Any SAL_CALL queryAggregation( Type const & rType ) override;
136
137 ProxyRoot( ::rtl::Reference< FactoryImpl > factory,
138 Reference< XInterface > const & xTarget );
139
141
142private:
143 UnoInterfaceReference m_target;
144};
145
146
147struct binuno_Proxy : public uno_Interface
148{
149 oslInterlockedCount m_nRefCount;
151 UnoInterfaceReference m_target;
152 OUString m_oid;
153 TypeDescription m_typeDescr;
154
155 binuno_Proxy(
157 UnoInterfaceReference target,
158 OUString oid, TypeDescription typeDescr );
159};
160
161extern "C"
162{
163
164
165static void binuno_proxy_free(
166 uno_ExtEnvironment * pEnv, void * pProxy )
167{
168 binuno_Proxy * proxy = static_cast< binuno_Proxy * >(
169 static_cast< uno_Interface * >( pProxy ) );
170 OSL_ASSERT( proxy->m_root->m_factory->m_uno_env.get()->pExtEnv == pEnv );
171 delete proxy;
172}
173
174
175static void binuno_proxy_acquire( uno_Interface * pUnoI )
176{
177 binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
178 if (osl_atomic_increment( &that->m_nRefCount ) != 1)
179 return;
180
181 // rebirth of zombie
182 uno_ExtEnvironment * uno_env =
183 that->m_root->m_factory->m_uno_env.get()->pExtEnv;
184 OSL_ASSERT( uno_env != nullptr );
185 (*uno_env->registerProxyInterface)(
186 uno_env, reinterpret_cast< void ** >( &pUnoI ), binuno_proxy_free,
187 that->m_oid.pData,
188 reinterpret_cast< typelib_InterfaceTypeDescription * >(
189 that->m_typeDescr.get() ) );
190 OSL_ASSERT( that == static_cast< binuno_Proxy * >( pUnoI ) );
191}
192
193
194static void binuno_proxy_release( uno_Interface * pUnoI )
195{
196 binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
197 if (osl_atomic_decrement( &that->m_nRefCount ) == 0)
198 {
199 uno_ExtEnvironment * uno_env =
200 that->m_root->m_factory->m_uno_env.get()->pExtEnv;
201 OSL_ASSERT( uno_env != nullptr );
202 (*uno_env->revokeInterface)( uno_env, pUnoI );
203 }
204}
205
206
207static void binuno_proxy_dispatch(
208 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberType,
209 void * pReturn, void * pArgs [], uno_Any ** ppException )
210{
211 binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
212 switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription const * >(
213 pMemberType )->nPosition)
214 {
215 case 0: // queryInterface()
216 {
217 try
218 {
219 Type const & rType =
220 *static_cast< Type const * >( pArgs[ 0 ] );
221 Any ret( that->m_root->queryInterface( rType ) );
223 pReturn, &ret, cppu::UnoType<decltype(ret)>::get().getTypeLibType(),
224 that->m_root->m_factory->m_cpp2uno.get() );
225 *ppException = nullptr; // no exc
226 }
227 catch (RuntimeException &)
228 {
229 Any exc( ::cppu::getCaughtException() );
231 *ppException, const_cast< void * >(exc.getValue()),
232 exc.getValueTypeRef(),
233 that->m_root->m_factory->m_cpp2uno.get() );
234 }
235 break;
236 }
237 case 1: // acquire()
238 binuno_proxy_acquire( pUnoI );
239 *ppException = nullptr; // no exc
240 break;
241 case 2: // release()
242 binuno_proxy_release( pUnoI );
243 *ppException = nullptr; // no exc
244 break;
245 default:
246 that->m_target.dispatch( pMemberType, pReturn, pArgs, ppException );
247 break;
248 }
249}
250
251}
252
253
254binuno_Proxy::binuno_Proxy(
256 UnoInterfaceReference target,
257 OUString oid, TypeDescription typeDescr )
258 : m_nRefCount( 1 ),
259 m_root(std::move( root )),
260 m_target(std::move( target )),
261 m_oid(std::move( oid )),
262 m_typeDescr(std::move( typeDescr ))
263{
264 uno_Interface::acquire = binuno_proxy_acquire;
265 uno_Interface::release = binuno_proxy_release;
266 uno_Interface::pDispatcher = binuno_proxy_dispatch;
267}
268
269ProxyRoot::ProxyRoot(
271 Reference< XInterface > const & xTarget )
272 : m_factory(std::move( factory ))
273{
274 m_factory->m_cpp2uno.mapInterface(
275 reinterpret_cast< void ** >( &m_target.m_pUnoI ), xTarget.get(),
276 cppu::UnoType<decltype(xTarget)>::get() );
277 OSL_ENSURE( m_target.is(), "### mapping interface failed!" );
278}
279
280
281Any ProxyRoot::queryAggregation( Type const & rType )
282{
283 Any ret( OWeakAggObject::queryAggregation( rType ) );
284 if (! ret.hasValue())
285 {
286 typelib_TypeDescription * pTypeDescr = nullptr;
287 TYPELIB_DANGER_GET( &pTypeDescr, rType.getTypeLibType() );
288 try
289 {
290 Reference< XInterface > xProxy;
291 uno_ExtEnvironment * cpp_env = m_factory->m_cpp_env.get()->pExtEnv;
292 OSL_ASSERT( cpp_env != nullptr );
293
294 // mind a new delegator, calculate current root:
295 Reference< XInterface > xRoot(getXWeak());
296 OUString oid;
297 (*cpp_env->getObjectIdentifier)( cpp_env, &oid.pData, xRoot.get() );
298 OSL_ASSERT( !oid.isEmpty() );
299
300 (*cpp_env->getRegisteredInterface)(
301 cpp_env, reinterpret_cast< void ** >( &xProxy ),
302 oid.pData, reinterpret_cast<
303 typelib_InterfaceTypeDescription * >(pTypeDescr) );
304 if (! xProxy.is())
305 {
306 // perform query on target:
307 UnoInterfaceReference proxy_target(
308 m_factory->binuno_queryInterface(
309 m_target, reinterpret_cast<
310 typelib_InterfaceTypeDescription * >(pTypeDescr) ) );
311 if (proxy_target.is())
312 {
313 // ensure root's object entries:
314 UnoInterfaceReference root;
315 m_factory->m_cpp2uno.mapInterface(
316 reinterpret_cast< void ** >( &root.m_pUnoI ),
317 xRoot.get(), cppu::UnoType<decltype(xRoot)>::get() );
318
319 UnoInterfaceReference proxy(
320 // ref count initially 1:
321 new binuno_Proxy( this, proxy_target, oid, pTypeDescr ),
322 SAL_NO_ACQUIRE );
323 uno_ExtEnvironment * uno_env =
324 m_factory->m_uno_env.get()->pExtEnv;
325 OSL_ASSERT( uno_env != nullptr );
326 (*uno_env->registerProxyInterface)(
327 uno_env, reinterpret_cast< void ** >( &proxy.m_pUnoI ),
328 binuno_proxy_free, oid.pData,
329 reinterpret_cast< typelib_InterfaceTypeDescription * >(
330 pTypeDescr ) );
331
332 m_factory->m_uno2cpp.mapInterface(
333 reinterpret_cast< void ** >( &xProxy ),
334 proxy.get(), pTypeDescr );
335 }
336 }
337 if (xProxy.is())
338 ret.setValue( &xProxy, pTypeDescr );
339 }
340 catch (...) // finally
341 {
342 TYPELIB_DANGER_RELEASE( pTypeDescr );
343 throw;
344 }
345 TYPELIB_DANGER_RELEASE( pTypeDescr );
346 }
347 return ret;
348}
349
350
351FactoryImpl::FactoryImpl()
352{
353 OUString uno = UNO_LB_UNO;
354 OUString cpp = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
355
357 reinterpret_cast< uno_Environment ** >( &m_uno_env ), uno.pData, nullptr );
358 OSL_ENSURE( m_uno_env.is(), "### cannot get binary uno env!" );
359
361 reinterpret_cast< uno_Environment ** >( &m_cpp_env ), cpp.pData, nullptr );
362 OSL_ENSURE( m_cpp_env.is(), "### cannot get C++ uno env!" );
363
365 reinterpret_cast< uno_Mapping ** >( &m_uno2cpp ),
366 m_uno_env.get(), m_cpp_env.get(), nullptr );
367 OSL_ENSURE( m_uno2cpp.is(), "### cannot get bridge uno <-> C++!" );
368
370 reinterpret_cast< uno_Mapping ** >( &m_cpp2uno ),
371 m_cpp_env.get(), m_uno_env.get(), nullptr );
372 OSL_ENSURE( m_cpp2uno.is(), "### cannot get bridge C++ <-> uno!" );
373}
374
375// XProxyFactory
376
377Reference< XAggregation > FactoryImpl::createProxy(
378 Reference< XInterface > const & xTarget )
379{
380 return new ProxyRoot( this, xTarget );
381}
382
383// XServiceInfo
384
385OUString FactoryImpl::getImplementationName()
386{
387 return "com.sun.star.comp.reflection.ProxyFactory";
388}
389
390sal_Bool FactoryImpl::supportsService( const OUString & rServiceName )
391{
392 return cppu::supportsService(this, rServiceName);
393}
394
395Sequence< OUString > FactoryImpl::getSupportedServiceNames()
396{
397 return { "com.sun.star.reflection.ProxyFactory" };
398}
399
400extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
401stoc_FactoryImpl_get_implementation(
402 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
403{
404 return cppu::acquire(new FactoryImpl);
405}
406
407
408}
409
410/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SAL_CALL uno_type_any_constructAndConvert(uno_Any *pDest, void *pSource, typelib_TypeDescriptionReference *pType, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
void SAL_CALL uno_any_destruct(uno_Any *pValue, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
virtual css::uno::Any SAL_CALL queryAggregation(const css::uno::Type &rType) SAL_OVERRIDE
css::uno::Type const & get()
void SAL_CALL uno_type_copyAndConvertData(void *pDest, void *pSource, typelib_TypeDescriptionReference *pType, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
Reference< XInterface > xTarget
struct _uno_Environment uno_Environment
OUString m_oid
void SAL_CALL uno_getEnvironment(uno_Environment **ppEnv, rtl_uString *pEnvDcp, void *pContext) SAL_THROW_EXTERN_C()
void SAL_CALL uno_getMapping(uno_Mapping **ppMapping, uno_Environment *pFrom, uno_Environment *pTo, rtl_uString *pAddPurpose) SAL_THROW_EXTERN_C()
OUString m_target
Definition: mergekeys.cxx:42
struct _uno_Mapping uno_Mapping
struct _typelib_TypeDescription typelib_TypeDescription
struct _uno_Any uno_Any
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
Type
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
void * binuno_queryInterface(void *pUnoI, typelib_TypeDescriptionReference *pDestType)
args
void SAL_CALL typelib_typedescriptionreference_getDescription(typelib_TypeDescription **ppRet, typelib_TypeDescriptionReference *pRef) SAL_THROW_EXTERN_C()
sal_Bool SAL_CALL typelib_typedescriptionreference_isAssignableFrom(typelib_TypeDescriptionReference *pAssignable, typelib_TypeDescriptionReference *pFrom) SAL_THROW_EXTERN_C()
void SAL_CALL typelib_typedescriptionreference_release(typelib_TypeDescriptionReference *pRef) SAL_THROW_EXTERN_C()
unsigned char sal_Bool