Coverage Report

Created: 2025-12-31 10:39

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