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 <sal/main.h>
22 #include <sal/log.hxx>
23 #include <osl/diagnose.h>
24 #include <rtl/alloc.h>
25 #include <uno/environment.hxx>
26 #include <uno/lbnames.h>
27 #include <cppuhelper/servicefactory.hxx>
28 #include <cppuhelper/implbase.hxx>
29 #include <com/sun/star/uno/XCurrentContext.hpp>
30 #include <com/sun/star/lang/DisposedException.hpp>
31 #include <com/sun/star/lang/XComponent.hpp>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include <com/sun/star/registry/XSimpleRegistry.hpp>
34 #include <com/sun/star/registry/XImplementationRegistration.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/reflection/XProxyFactory.hpp>
37 
38 #include <stdio.h>
39 
40 
41 using namespace ::osl;
42 using namespace ::cppu;
43 using namespace ::com::sun::star;
44 using namespace css::uno;
45 
46 
47 typedef WeakImplHelper< lang::XServiceInfo,
48                          XCurrentContext,
49                          reflection::XProxyFactory > t_impl;
50 
51 
52 class TargetObject : public t_impl
53 {
54 public:
55     static int s_obj;
56 
~TargetObject()57     virtual ~TargetObject() {
58         --s_obj;
59         SAL_INFO("stoc", "~TargetObject()" );
60     }
TargetObject()61     TargetObject()
62         { ++s_obj; }
63 
64     Any SAL_CALL queryInterface( Type const & type )
65         throw (RuntimeException);
66 
67     // XServiceInfo
getImplementationName()68     virtual OUString SAL_CALL getImplementationName() throw (RuntimeException)
69         { return OUString("target"); }
supportsService(const OUString &)70     virtual sal_Bool SAL_CALL supportsService( const OUString & /*rServiceName*/ )
71         throw (RuntimeException)
72         { return sal_False; }
getSupportedServiceNames()73     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
74         throw (RuntimeException)
75         { return Sequence< OUString >(); }
76     // XProxyFactory
createProxy(const Reference<XInterface> & xTarget)77     virtual Reference< XAggregation > SAL_CALL createProxy(
78         const Reference< XInterface > & xTarget ) throw (RuntimeException)
79         { return Reference< XAggregation >( xTarget, UNO_QUERY ); }
80     // XCurrentContext
getValueByName(OUString const & name)81     virtual Any SAL_CALL getValueByName( OUString const & name )
82         throw (RuntimeException)
83         { return makeAny( name ); }
84 };
85 
86 
queryInterface(Type const & type)87 Any TargetObject::queryInterface( Type const & type )
88     throw (RuntimeException)
89 {
90     Any ret( t_impl::queryInterface( type ) );
91     if (ret.hasValue())
92         return ret;
93     throw lang::DisposedException(
94         OUString( "my test exception" ),
95         static_cast< OWeakObject * >(this) );
96 }
97 
98 int TargetObject::s_obj = 0;
99 
100 
101 class TestMaster : public WeakImplHelper< lang::XServiceInfo >
102 {
103     Reference< XAggregation > m_xProxyTarget;
104     Reference<lang::XServiceInfo> m_xOtherProxyTargetBeforeSetDelegator;
105 
TestMaster()106     inline TestMaster() { ++s_obj; }
107 public:
108     static int s_obj;
109     static Reference< XInterface > create(
110         Reference< reflection::XProxyFactory > const & xProxyFac );
111     static Reference< XInterface > create(
112         Reference< XInterface > const & xTarget,
113         Reference< reflection::XProxyFactory > const & xProxyFac );
114 
~TestMaster()115     virtual ~TestMaster() {
116         --s_obj;
117         SAL_INFO("stoc", "~TestMaster()" );
118     }
119 
queryInterface(const Type & rType)120     virtual Any SAL_CALL queryInterface( const Type & rType )
121         throw (RuntimeException)
122     {
123         Any aRet(
124             WeakImplHelper< lang::XServiceInfo >::queryInterface( rType ) );
125         if (aRet.hasValue())
126             return aRet;
127         return m_xProxyTarget->queryAggregation( rType );
128     }
129 
130     // XServiceInfo
getImplementationName()131     virtual OUString SAL_CALL getImplementationName() throw (RuntimeException)
132         { return OUString("master"); }
supportsService(const OUString &)133     virtual sal_Bool SAL_CALL supportsService( const OUString & /*rServiceName*/ )
134         throw (RuntimeException)
135         { return sal_False; }
getSupportedServiceNames()136     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
137         throw (RuntimeException)
138         { return Sequence< OUString >(); }
139 };
140 
141 int TestMaster::s_obj = 0;
142 
143 
create(Reference<XInterface> const & xTarget,Reference<reflection::XProxyFactory> const & xProxyFac)144 Reference< XInterface > TestMaster::create(
145     Reference< XInterface > const & xTarget,
146     Reference< reflection::XProxyFactory > const & xProxyFac )
147 {
148     TestMaster * that = new TestMaster;
149     Reference< XInterface > xRet( static_cast< OWeakObject * >( that ) );
150     {
151         Reference< XAggregation > xAgg( xProxyFac->createProxy( xTarget ) );
152         // ownership take over
153         that->m_xProxyTarget.set( xAgg, UNO_QUERY_THROW );
154         that->m_xOtherProxyTargetBeforeSetDelegator.set(
155             that->m_xProxyTarget, UNO_QUERY );
156     }
157     that->m_xProxyTarget->setDelegator( xRet );
158     return xRet;
159 }
160 
create(Reference<reflection::XProxyFactory> const & xProxyFac)161 Reference< XInterface > TestMaster::create(
162     Reference< reflection::XProxyFactory > const & xProxyFac )
163 {
164     return create(
165         static_cast< OWeakObject * >( new TargetObject ), xProxyFac );
166 }
167 
168 
test_proxyfac_(Reference<XInterface> const & xMaster,OUString const & test,Reference<reflection::XProxyFactory> const &)169 static void test_proxyfac_(
170     Reference< XInterface > const & xMaster, OUString const & test,
171     Reference< reflection::XProxyFactory > const & /*xProxyFac*/ )
172 {
173     (void)test;
174     Reference< lang::XServiceInfo > xMaster_XServiceInfo(
175         xMaster, UNO_QUERY_THROW );
176     OSL_ASSERT( xMaster_XServiceInfo->getImplementationName().equals( test ) );
177 
178     Reference< reflection::XProxyFactory > xTarget( xMaster, UNO_QUERY_THROW );
179     Reference< XCurrentContext > xTarget_XCurrentContext(
180         xTarget, UNO_QUERY_THROW );
181     Reference< XCurrentContext > xMaster_XCurrentContext(
182         xMaster, UNO_QUERY_THROW );
183 
184     OSL_ASSERT(
185         xTarget_XCurrentContext->getValueByName( test ) == makeAny( test ) );
186     OSL_ASSERT(
187         xMaster_XCurrentContext->getValueByName( test ) == makeAny( test ) );
188 
189     Reference< XAggregation > xFakeAgg( xTarget->createProxy( xTarget ) );
190     if (xFakeAgg.is())
191     {
192         OSL_ASSERT( xTarget == xFakeAgg );
193         OSL_ASSERT( xMaster == xFakeAgg );
194     }
195 
196     Reference< lang::XServiceInfo > xTarget_XServiceInfo(
197         xTarget, UNO_QUERY_THROW );
198     OSL_ASSERT( xTarget_XServiceInfo->getImplementationName().equals( test ) );
199     Reference< lang::XServiceInfo > xTarget_XServiceInfo2(
200         xTarget, UNO_QUERY_THROW );
201     OSL_ASSERT( xTarget_XServiceInfo2.get() == xTarget_XServiceInfo.get() );
202 
203     OSL_ASSERT( xTarget == xTarget_XCurrentContext );
204     OSL_ASSERT( xTarget_XCurrentContext == xMaster );
205     OSL_ASSERT(
206         xTarget_XCurrentContext.get() == xMaster_XCurrentContext.get() );
207     OSL_ASSERT( xTarget_XCurrentContext == xMaster );
208     OSL_ASSERT( xTarget == xMaster );
209     OSL_ASSERT( xTarget_XServiceInfo.get() == xMaster_XServiceInfo.get() );
210     OSL_ASSERT( xTarget_XServiceInfo == xMaster );
211     OSL_ASSERT( xMaster_XServiceInfo == xMaster );
212 
213     try
214     {
215         Reference< registry::XRegistryKey >(
216             xMaster, UNO_QUERY_THROW );
217     }
218     catch (const lang::DisposedException & exc)
219     {
220         if ( exc.Message != "my test exception" )
221             throw;
222     }
223 }
224 
test_proxyfac(Reference<XInterface> const & xMaster,OUString const & test,Reference<reflection::XProxyFactory> const & xProxyFac)225 static void test_proxyfac(
226     Reference< XInterface > const & xMaster, OUString const & test,
227     Reference< reflection::XProxyFactory > const & xProxyFac )
228 {
229     test_proxyfac_( xMaster, test, xProxyFac );
230     // proxy the proxy...
231     Reference< XInterface > xNew( TestMaster::create( xMaster, xProxyFac ) );
232     test_proxyfac_(
233         xNew, OUString( "master" ), xProxyFac );
234 }
235 
SAL_IMPLEMENT_MAIN()236 SAL_IMPLEMENT_MAIN()
237 {
238     bool success = true;
239 
240     Environment cpp_env;
241     OUString cpp( CPPU_CURRENT_LANGUAGE_BINDING_NAME );
242     uno_getEnvironment(
243         reinterpret_cast< uno_Environment ** >( &cpp_env ),
244         cpp.pData, 0 );
245     OSL_ENSURE( cpp_env.is(), "### cannot get C++ uno env!" );
246 
247     {
248         Reference< lang::XMultiServiceFactory > xMgr(
249             createRegistryServiceFactory(
250                 OUString( "stoctest.rdb" ) ) );
251 
252         try
253         {
254             Reference< registry::XImplementationRegistration > xImplReg(
255                 xMgr->createInstance( "com.sun.star.registry.ImplementationRegistration" ),
256                 UNO_QUERY );
257             OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
258 
259             OUString aLibName(
260                 "proxyfac.uno" SAL_DLLEXTENSION );
261             xImplReg->registerImplementation(
262                 OUString( "com.sun.star.loader.SharedLibrary" ),
263                 aLibName, Reference< registry::XSimpleRegistry >() );
264 
265             Reference< reflection::XProxyFactory > xProxyFac(
266                 xMgr->createInstance("com.sun.star.reflection.ProxyFactory"),
267                 UNO_QUERY_THROW );
268 
269             Reference< XAggregation > x(
270                 xProxyFac->createProxy(
271                     static_cast< OWeakObject * >( new TargetObject ) ) );
272             // no call
273 
274             {
275             Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
276             test_proxyfac(
277                 xMaster,
278                 OUString( "master" ),
279                 xProxyFac );
280             }
281             {
282             Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
283             // no call
284             }
285 
286             {
287             Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
288             Reference< reflection::XProxyFactory > xSlave_lives_alone(
289                 xMaster, UNO_QUERY_THROW );
290             xMaster.clear();
291             test_proxyfac(
292                 xSlave_lives_alone,
293                 OUString( "master" ),
294                 xProxyFac );
295             uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
296             }
297             {
298             Reference< XInterface > xMaster( TestMaster::create( xProxyFac ) );
299             Reference< reflection::XProxyFactory > xSlave_lives_alone(
300                 xMaster, UNO_QUERY_THROW );
301             // no call
302             }
303 
304             test_proxyfac(
305                 xProxyFac->createProxy(
306                     static_cast< OWeakObject * >( new TargetObject ) ),
307                 OUString( "target" ),
308                 xProxyFac );
309             uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
310         }
311         catch (const Exception & rExc)
312         {
313             (void)rExc;
314             OSL_ENSURE(
315                 ! __FILE__,
316                 OUStringToOString(
317                     rExc.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
318             success = false;
319         }
320 
321 
322         Reference< lang::XComponent > xComp;
323         Reference< beans::XPropertySet >(
324             xMgr, UNO_QUERY_THROW )->getPropertyValue(
325                 OUString( "DefaultContext" ) )
326                     >>= xComp;
327         xComp->dispose();
328     }
329 
330     if (TestMaster::s_obj != 0)
331         fprintf( stderr, "TestMaster objects: %d\n", TestMaster::s_obj );
332     if (TargetObject::s_obj != 0)
333         fprintf( stderr, "TargetObject objects: %d\n", TargetObject::s_obj );
334 
335     uno_dumpEnvironment( stdout, cpp_env.get(), 0 );
336     void ** ppInterfaces;
337     sal_Int32 len;
338     uno_ExtEnvironment * env = cpp_env.get()->pExtEnv;
339     (*env->getRegisteredInterfaces)(
340         env, &ppInterfaces, &len, rtl_allocateMemory );
341     if (len != 0)
342         fprintf( stderr, "%d registered C++ interfaces left!\n", len );
343 
344     success &= (TestMaster::s_obj == 0 &&
345                 TargetObject::s_obj == 0 &&
346                 len == 0);
347     if (success)
348     {
349         printf( "testproxyfac succeeded.\n" );
350         return 0;
351     }
352     else
353     {
354         fprintf( stderr, "testproxyfac failed!\n" );
355         return 1;
356     }
357 }
358 
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
360