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>
30 #include <cppuhelper/exc_hlp.hxx>
31 #include <cppuhelper/implbase.hxx>
32 #include <cppuhelper/supportsservice.hxx>
33 #include <cppuhelper/weak.hxx>
34 #include <cppuhelper/weakagg.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 
40 
41 using namespace ::com::sun::star;
42 using namespace css::uno;
43 
44 
45 namespace
46 {
47 
48 struct FactoryImpl : public ::cppu::WeakImplHelper< lang::XServiceInfo,
49                                                      reflection::XProxyFactory >
50 {
51     Environment m_uno_env;
52     Environment m_cpp_env;
53     Mapping m_uno2cpp;
54     Mapping m_cpp2uno;
55 
56     UnoInterfaceReference binuno_queryInterface(
57         UnoInterfaceReference const & unoI,
58         typelib_InterfaceTypeDescription * pTypeDescr );
59 
60     FactoryImpl();
61 
62     // XServiceInfo
63     virtual OUString SAL_CALL getImplementationName() override;
64     virtual sal_Bool SAL_CALL supportsService( const OUString & rServiceName ) override;
65     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
66 
67     // XProxyFactory
68     virtual Reference< XAggregation > SAL_CALL createProxy(
69         Reference< XInterface > const & xTarget ) override;
70 };
71 
72 
binuno_queryInterface(UnoInterfaceReference const & unoI,typelib_InterfaceTypeDescription * pTypeDescr)73 UnoInterfaceReference FactoryImpl::binuno_queryInterface(
74     UnoInterfaceReference const & unoI,
75     typelib_InterfaceTypeDescription * pTypeDescr )
76 {
77     // init queryInterface() td
78     static typelib_TypeDescription* s_pQITD = []() {
79         typelib_TypeDescription* pTXInterfaceDescr = nullptr;
80         TYPELIB_DANGER_GET(&pTXInterfaceDescr, cppu::UnoType<XInterface>::get().getTypeLibType());
81         typelib_TypeDescription* pQITD = nullptr;
82         typelib_typedescriptionreference_getDescription(
83             &pQITD, reinterpret_cast<typelib_InterfaceTypeDescription*>(pTXInterfaceDescr)
84                         ->ppAllMembers[0]);
85         TYPELIB_DANGER_RELEASE(pTXInterfaceDescr);
86         return pQITD;
87     }();
88 
89     void * args[ 1 ];
90     args[ 0 ] = &reinterpret_cast< typelib_TypeDescription * >(
91         pTypeDescr )->pWeakRef;
92     uno_Any ret_val, exc_space;
93     uno_Any * exc = &exc_space;
94 
95     unoI.dispatch( s_pQITD, &ret_val, args, &exc );
96 
97     if (exc == nullptr)
98     {
99         UnoInterfaceReference ret;
100         if (ret_val.pType->eTypeClass == typelib_TypeClass_INTERFACE)
101         {
102             ret.set( *static_cast< uno_Interface ** >(ret_val.pData),
103                      SAL_NO_ACQUIRE );
104             typelib_typedescriptionreference_release( ret_val.pType );
105         }
106         else
107         {
108             uno_any_destruct( &ret_val, nullptr );
109         }
110         return ret;
111     }
112     else
113     {
114         // exception occurred:
115         OSL_ENSURE(
116             typelib_typedescriptionreference_isAssignableFrom( cppu::UnoType<RuntimeException>::get().getTypeLibType(),
117                 exc->pType ),
118             "### RuntimeException expected!" );
119         Any cpp_exc;
120         uno_type_copyAndConvertData(
121             &cpp_exc, exc, cppu::UnoType<decltype(cpp_exc)>::get().getTypeLibType(),
122             m_uno2cpp.get() );
123         uno_any_destruct( exc, nullptr );
124         ::cppu::throwException( cpp_exc );
125         OSL_ASSERT( false ); // way of no return
126         return UnoInterfaceReference(); // for dummy
127     }
128 }
129 
130 
131 struct ProxyRoot : public ::cppu::OWeakAggObject
132 {
133     // XAggregation
134     virtual Any SAL_CALL queryAggregation( Type const & rType ) override;
135 
136     ProxyRoot( ::rtl::Reference< FactoryImpl > const & factory,
137                       Reference< XInterface > const & xTarget );
138 
139     ::rtl::Reference< FactoryImpl > m_factory;
140 
141 private:
142     UnoInterfaceReference m_target;
143 };
144 
145 
146 struct binuno_Proxy : public uno_Interface
147 {
148     oslInterlockedCount m_nRefCount;
149     ::rtl::Reference< ProxyRoot > m_root;
150     UnoInterfaceReference m_target;
151     OUString m_oid;
152     TypeDescription m_typeDescr;
153 
154     binuno_Proxy(
155         ::rtl::Reference< ProxyRoot > const & root,
156         UnoInterfaceReference const & target,
157         OUString const & oid, TypeDescription const & typeDescr );
158 };
159 
160 extern "C"
161 {
162 
163 
binuno_proxy_free(uno_ExtEnvironment * pEnv,void * pProxy)164 static void binuno_proxy_free(
165     uno_ExtEnvironment * pEnv, void * pProxy )
166 {
167     binuno_Proxy * proxy = static_cast< binuno_Proxy * >(
168         static_cast< uno_Interface * >( pProxy ) );
169     OSL_ASSERT( proxy->m_root->m_factory->m_uno_env.get()->pExtEnv == pEnv );
170     delete proxy;
171 }
172 
173 
binuno_proxy_acquire(uno_Interface * pUnoI)174 static void binuno_proxy_acquire( uno_Interface * pUnoI )
175 {
176     binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
177     if (osl_atomic_increment( &that->m_nRefCount ) != 1)
178         return;
179 
180     // rebirth of zombie
181     uno_ExtEnvironment * uno_env =
182         that->m_root->m_factory->m_uno_env.get()->pExtEnv;
183     OSL_ASSERT( uno_env != nullptr );
184     (*uno_env->registerProxyInterface)(
185         uno_env, reinterpret_cast< void ** >( &pUnoI ), binuno_proxy_free,
186         that->m_oid.pData,
187         reinterpret_cast< typelib_InterfaceTypeDescription * >(
188             that->m_typeDescr.get() ) );
189     OSL_ASSERT( that == static_cast< binuno_Proxy * >( pUnoI ) );
190 }
191 
192 
binuno_proxy_release(uno_Interface * pUnoI)193 static void binuno_proxy_release( uno_Interface * pUnoI )
194 {
195     binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
196     if (osl_atomic_decrement( &that->m_nRefCount ) == 0)
197     {
198         uno_ExtEnvironment * uno_env =
199             that->m_root->m_factory->m_uno_env.get()->pExtEnv;
200         OSL_ASSERT( uno_env != nullptr );
201         (*uno_env->revokeInterface)( uno_env, pUnoI );
202     }
203 }
204 
205 
binuno_proxy_dispatch(uno_Interface * pUnoI,const typelib_TypeDescription * pMemberType,void * pReturn,void * pArgs[],uno_Any ** ppException)206 static void binuno_proxy_dispatch(
207     uno_Interface * pUnoI, const typelib_TypeDescription * pMemberType,
208     void * pReturn, void * pArgs [], uno_Any ** ppException )
209 {
210     binuno_Proxy * that = static_cast< binuno_Proxy * >( pUnoI );
211     switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription const * >(
212                 pMemberType )->nPosition)
213     {
214     case 0: // queryInterface()
215     {
216         try
217         {
218             Type const & rType =
219                 *static_cast< Type const * >( pArgs[ 0 ] );
220             Any ret( that->m_root->queryInterface( rType ) );
221             uno_type_copyAndConvertData(
222                 pReturn, &ret, cppu::UnoType<decltype(ret)>::get().getTypeLibType(),
223                 that->m_root->m_factory->m_cpp2uno.get() );
224             *ppException = nullptr; // no exc
225         }
226         catch (RuntimeException &)
227         {
228             Any exc( ::cppu::getCaughtException() );
229             uno_type_any_constructAndConvert(
230                 *ppException, const_cast< void * >(exc.getValue()),
231                 exc.getValueTypeRef(),
232                 that->m_root->m_factory->m_cpp2uno.get() );
233         }
234         break;
235     }
236     case 1: // acquire()
237         binuno_proxy_acquire( pUnoI );
238         *ppException = nullptr; // no exc
239         break;
240     case 2: // release()
241         binuno_proxy_release( pUnoI );
242         *ppException = nullptr; // no exc
243         break;
244     default:
245         that->m_target.dispatch( pMemberType, pReturn, pArgs, ppException );
246         break;
247     }
248 }
249 
250 }
251 
252 
binuno_Proxy(::rtl::Reference<ProxyRoot> const & root,UnoInterfaceReference const & target,OUString const & oid,TypeDescription const & typeDescr)253 binuno_Proxy::binuno_Proxy(
254     ::rtl::Reference< ProxyRoot > const & root,
255     UnoInterfaceReference const & target,
256     OUString const & oid, TypeDescription const & typeDescr )
257     : m_nRefCount( 1 ),
258       m_root( root ),
259       m_target( target ),
260       m_oid( oid ),
261       m_typeDescr( typeDescr )
262 {
263     uno_Interface::acquire = binuno_proxy_acquire;
264     uno_Interface::release = binuno_proxy_release;
265     uno_Interface::pDispatcher = binuno_proxy_dispatch;
266 }
267 
ProxyRoot(::rtl::Reference<FactoryImpl> const & factory,Reference<XInterface> const & xTarget)268 ProxyRoot::ProxyRoot(
269     ::rtl::Reference< FactoryImpl > const & factory,
270     Reference< XInterface > const & xTarget )
271     : m_factory( factory )
272 {
273     m_factory->m_cpp2uno.mapInterface(
274         reinterpret_cast< void ** >( &m_target.m_pUnoI ), xTarget.get(),
275         cppu::UnoType<decltype(xTarget)>::get() );
276     OSL_ENSURE( m_target.is(), "### mapping interface failed!" );
277 }
278 
279 
queryAggregation(Type const & rType)280 Any ProxyRoot::queryAggregation( Type const & rType )
281 {
282     Any ret( OWeakAggObject::queryAggregation( rType ) );
283     if (! ret.hasValue())
284     {
285         typelib_TypeDescription * pTypeDescr = nullptr;
286         TYPELIB_DANGER_GET( &pTypeDescr, rType.getTypeLibType() );
287         try
288         {
289             Reference< XInterface > xProxy;
290             uno_ExtEnvironment * cpp_env = m_factory->m_cpp_env.get()->pExtEnv;
291             OSL_ASSERT( cpp_env != nullptr );
292 
293             // mind a new delegator, calculate current root:
294             Reference< XInterface > xRoot(
295                 static_cast< OWeakObject * >(this), UNO_QUERY_THROW );
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 
FactoryImpl()351 FactoryImpl::FactoryImpl()
352 {
353     OUString uno = UNO_LB_UNO;
354     OUString cpp = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
355 
356     uno_getEnvironment(
357         reinterpret_cast< uno_Environment ** >( &m_uno_env ), uno.pData, nullptr );
358     OSL_ENSURE( m_uno_env.is(), "### cannot get binary uno env!" );
359 
360     uno_getEnvironment(
361         reinterpret_cast< uno_Environment ** >( &m_cpp_env ), cpp.pData, nullptr );
362     OSL_ENSURE( m_cpp_env.is(), "### cannot get C++ uno env!" );
363 
364     uno_getMapping(
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 
369     uno_getMapping(
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 
createProxy(Reference<XInterface> const & xTarget)377 Reference< XAggregation > FactoryImpl::createProxy(
378     Reference< XInterface > const & xTarget )
379 {
380     return new ProxyRoot( this, xTarget );
381 }
382 
383 // XServiceInfo
384 
getImplementationName()385 OUString FactoryImpl::getImplementationName()
386 {
387     return "com.sun.star.comp.reflection.ProxyFactory";
388 }
389 
supportsService(const OUString & rServiceName)390 sal_Bool FactoryImpl::supportsService( const OUString & rServiceName )
391 {
392     return cppu::supportsService(this, rServiceName);
393 }
394 
getSupportedServiceNames()395 Sequence< OUString > FactoryImpl::getSupportedServiceNames()
396 {
397     return { "com.sun.star.reflection.ProxyFactory" };
398 }
399 
400 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
stoc_FactoryImpl_get_implementation(css::uno::XComponentContext *,css::uno::Sequence<css::uno::Any> const &)401 stoc_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: */
411