xref: /trunk/main/comphelper/source/misc/proxyaggregation.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
30*cdf0e10cSrcweir #include <comphelper/proxyaggregation.hxx>
31*cdf0e10cSrcweir #include <com/sun/star/reflection/XProxyFactory.hpp>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir //.............................................................................
34*cdf0e10cSrcweir namespace comphelper
35*cdf0e10cSrcweir {
36*cdf0e10cSrcweir //.............................................................................
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir     using namespace ::com::sun::star::uno;
39*cdf0e10cSrcweir     using namespace ::com::sun::star::lang;
40*cdf0e10cSrcweir     using namespace ::com::sun::star::reflection;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir     //=========================================================================
43*cdf0e10cSrcweir     //= OProxyAggregation
44*cdf0e10cSrcweir     //=========================================================================
45*cdf0e10cSrcweir     //-------------------------------------------------------------------------
46*cdf0e10cSrcweir     OProxyAggregation::OProxyAggregation( const Reference< XMultiServiceFactory >& _rxORB )
47*cdf0e10cSrcweir         :m_xORB( _rxORB )
48*cdf0e10cSrcweir     {
49*cdf0e10cSrcweir     }
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir     //-------------------------------------------------------------------------
52*cdf0e10cSrcweir     void OProxyAggregation::baseAggregateProxyFor( const Reference< XInterface >& _rxComponent, oslInterlockedCount& _rRefCount,
53*cdf0e10cSrcweir             ::cppu::OWeakObject& _rDelegator )
54*cdf0e10cSrcweir     {
55*cdf0e10cSrcweir         // first a factory for the proxy
56*cdf0e10cSrcweir         Reference< XProxyFactory > xFactory(
57*cdf0e10cSrcweir             m_xORB->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.reflection.ProxyFactory" ) ) ),
58*cdf0e10cSrcweir             UNO_QUERY
59*cdf0e10cSrcweir         );
60*cdf0e10cSrcweir         OSL_ENSURE( xFactory.is(), "OProxyAggregation::baseAggregateProxyFor: could not create a proxy factory!" );
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir         // then the proxy itself
63*cdf0e10cSrcweir         if ( xFactory.is() )
64*cdf0e10cSrcweir         {
65*cdf0e10cSrcweir             { // i36686 OJ: achieve the desctruction of the tempoary -> otherwise it leads to _rRefCount -= 2
66*cdf0e10cSrcweir                 m_xProxyAggregate = xFactory->createProxy( _rxComponent );
67*cdf0e10cSrcweir             }
68*cdf0e10cSrcweir             if ( m_xProxyAggregate.is() )
69*cdf0e10cSrcweir                 m_xProxyAggregate->queryAggregation( ::getCppuType( &m_xProxyTypeAccess ) ) >>= m_xProxyTypeAccess;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir             // aggregate the proxy
72*cdf0e10cSrcweir             osl_incrementInterlockedCount( &_rRefCount );
73*cdf0e10cSrcweir             if ( m_xProxyAggregate.is() )
74*cdf0e10cSrcweir             {
75*cdf0e10cSrcweir                 // At this point in time, the proxy has a ref count of exactly two - in m_xControlContextProxy,
76*cdf0e10cSrcweir                 // and in m_xProxyTypeAccess.
77*cdf0e10cSrcweir                 // Remember to _not_ reset these members unless the delegator of the proxy has been reset, too!
78*cdf0e10cSrcweir                 m_xProxyAggregate->setDelegator( _rDelegator );
79*cdf0e10cSrcweir             }
80*cdf0e10cSrcweir             osl_decrementInterlockedCount( &_rRefCount );
81*cdf0e10cSrcweir         }
82*cdf0e10cSrcweir     }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     //-------------------------------------------------------------------------
85*cdf0e10cSrcweir     Any SAL_CALL OProxyAggregation::queryAggregation( const Type& _rType ) throw (RuntimeException)
86*cdf0e10cSrcweir     {
87*cdf0e10cSrcweir         return m_xProxyAggregate.is() ? m_xProxyAggregate->queryAggregation( _rType ) : Any();
88*cdf0e10cSrcweir     }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     //-------------------------------------------------------------------------
91*cdf0e10cSrcweir     Sequence< Type > SAL_CALL OProxyAggregation::getTypes(  ) throw (RuntimeException)
92*cdf0e10cSrcweir     {
93*cdf0e10cSrcweir         Sequence< Type > aTypes;
94*cdf0e10cSrcweir         if ( m_xProxyAggregate.is() )
95*cdf0e10cSrcweir         {
96*cdf0e10cSrcweir             if ( m_xProxyTypeAccess.is() )
97*cdf0e10cSrcweir                 aTypes = m_xProxyTypeAccess->getTypes();
98*cdf0e10cSrcweir         }
99*cdf0e10cSrcweir         return aTypes;
100*cdf0e10cSrcweir     }
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir     //-------------------------------------------------------------------------
103*cdf0e10cSrcweir     OProxyAggregation::~OProxyAggregation()
104*cdf0e10cSrcweir     {
105*cdf0e10cSrcweir         if ( m_xProxyAggregate.is() )
106*cdf0e10cSrcweir             m_xProxyAggregate->setDelegator( NULL );
107*cdf0e10cSrcweir         m_xProxyAggregate.clear();
108*cdf0e10cSrcweir         m_xProxyTypeAccess.clear();
109*cdf0e10cSrcweir             // this should remove the _two_only_ "real" references (means not delegated to
110*cdf0e10cSrcweir             // ourself) to this proxy, and thus delete it
111*cdf0e10cSrcweir     }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir     //=========================================================================
114*cdf0e10cSrcweir     //= OComponentProxyAggregationHelper
115*cdf0e10cSrcweir     //=========================================================================
116*cdf0e10cSrcweir     //-------------------------------------------------------------------------
117*cdf0e10cSrcweir     OComponentProxyAggregationHelper::OComponentProxyAggregationHelper( const Reference< XMultiServiceFactory >& _rxORB,
118*cdf0e10cSrcweir         ::cppu::OBroadcastHelper& _rBHelper )
119*cdf0e10cSrcweir         :OProxyAggregation( _rxORB )
120*cdf0e10cSrcweir         ,m_rBHelper( _rBHelper )
121*cdf0e10cSrcweir     {
122*cdf0e10cSrcweir         OSL_ENSURE( _rxORB.is(), "OComponentProxyAggregationHelper::OComponentProxyAggregationHelper: invalid arguments!" );
123*cdf0e10cSrcweir     }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     //-------------------------------------------------------------------------
126*cdf0e10cSrcweir     void OComponentProxyAggregationHelper::componentAggregateProxyFor(
127*cdf0e10cSrcweir         const Reference< XComponent >& _rxComponent, oslInterlockedCount& _rRefCount,
128*cdf0e10cSrcweir         ::cppu::OWeakObject& _rDelegator )
129*cdf0e10cSrcweir     {
130*cdf0e10cSrcweir         OSL_ENSURE( _rxComponent.is(), "OComponentProxyAggregationHelper::componentAggregateProxyFor: invalid inner component!" );
131*cdf0e10cSrcweir         m_xInner = _rxComponent;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir         // aggregate a proxy for the object
134*cdf0e10cSrcweir         baseAggregateProxyFor( m_xInner.get(), _rRefCount, _rDelegator );
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         // add as event listener to the inner context, because we want to be notified of disposals
137*cdf0e10cSrcweir         osl_incrementInterlockedCount( &_rRefCount );
138*cdf0e10cSrcweir         {
139*cdf0e10cSrcweir             if ( m_xInner.is() )
140*cdf0e10cSrcweir                 m_xInner->addEventListener( this );
141*cdf0e10cSrcweir         }
142*cdf0e10cSrcweir         osl_decrementInterlockedCount( &_rRefCount );
143*cdf0e10cSrcweir     }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     //-------------------------------------------------------------------------
146*cdf0e10cSrcweir     Any SAL_CALL OComponentProxyAggregationHelper::queryInterface( const Type& _rType ) throw (RuntimeException)
147*cdf0e10cSrcweir     {
148*cdf0e10cSrcweir         Any aReturn( BASE::queryInterface( _rType ) );
149*cdf0e10cSrcweir         if ( !aReturn.hasValue() )
150*cdf0e10cSrcweir             aReturn = OProxyAggregation::queryAggregation( _rType );
151*cdf0e10cSrcweir         return aReturn;
152*cdf0e10cSrcweir     }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir     //-------------------------------------------------------------------------
155*cdf0e10cSrcweir     IMPLEMENT_FORWARD_XTYPEPROVIDER2( OComponentProxyAggregationHelper, BASE, OProxyAggregation )
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     //-------------------------------------------------------------------------
158*cdf0e10cSrcweir     OComponentProxyAggregationHelper::~OComponentProxyAggregationHelper( )
159*cdf0e10cSrcweir     {
160*cdf0e10cSrcweir         OSL_ENSURE( m_rBHelper.bDisposed, "OComponentProxyAggregationHelper::~OComponentProxyAggregationHelper: you should dispose your derived class in the dtor, if necessary!" );
161*cdf0e10cSrcweir             // if this asserts, add the following to your derived class dtor:
162*cdf0e10cSrcweir             //
163*cdf0e10cSrcweir             // if ( !m_rBHelper.bDisposed )
164*cdf0e10cSrcweir             // {
165*cdf0e10cSrcweir             //   acquire(); // to prevent duplicate dtor calls
166*cdf0e10cSrcweir             //   dispose();
167*cdf0e10cSrcweir             // }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         m_xInner.clear();
170*cdf0e10cSrcweir     }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir     //-------------------------------------------------------------------------
173*cdf0e10cSrcweir     void SAL_CALL OComponentProxyAggregationHelper::disposing( const EventObject& _rSource ) throw (RuntimeException)
174*cdf0e10cSrcweir     {
175*cdf0e10cSrcweir         if ( _rSource.Source == m_xInner )
176*cdf0e10cSrcweir         {   // it's our inner context which is dying -> dispose ourself
177*cdf0e10cSrcweir             if ( !m_rBHelper.bDisposed && !m_rBHelper.bInDispose )
178*cdf0e10cSrcweir             {   // (if necessary only, of course)
179*cdf0e10cSrcweir                 dispose();
180*cdf0e10cSrcweir             }
181*cdf0e10cSrcweir         }
182*cdf0e10cSrcweir     }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     //-------------------------------------------------------------------------
185*cdf0e10cSrcweir     void SAL_CALL OComponentProxyAggregationHelper::dispose() throw( RuntimeException )
186*cdf0e10cSrcweir     {
187*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_rBHelper.rMutex );
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir         // dispose our inner context
190*cdf0e10cSrcweir         // before we do this, remove ourself as listener - else in disposing( EventObject ), we
191*cdf0e10cSrcweir         // would dispose ourself a second time
192*cdf0e10cSrcweir         Reference< XComponent > xComp( m_xInner, UNO_QUERY );
193*cdf0e10cSrcweir         if ( xComp.is() )
194*cdf0e10cSrcweir         {
195*cdf0e10cSrcweir             xComp->removeEventListener( this );
196*cdf0e10cSrcweir             xComp->dispose();
197*cdf0e10cSrcweir             xComp.clear();
198*cdf0e10cSrcweir         }
199*cdf0e10cSrcweir     }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     //=========================================================================
202*cdf0e10cSrcweir     //= OComponentProxyAggregation
203*cdf0e10cSrcweir     //=========================================================================
204*cdf0e10cSrcweir     //-------------------------------------------------------------------------
205*cdf0e10cSrcweir     OComponentProxyAggregation::OComponentProxyAggregation( const Reference< XMultiServiceFactory >& _rxORB,
206*cdf0e10cSrcweir             const Reference< XComponent >& _rxComponent )
207*cdf0e10cSrcweir         :OComponentProxyAggregation_CBase( m_aMutex )
208*cdf0e10cSrcweir         ,OComponentProxyAggregationHelper( _rxORB, rBHelper )
209*cdf0e10cSrcweir     {
210*cdf0e10cSrcweir         OSL_ENSURE( _rxComponent.is(), "OComponentProxyAggregation::OComponentProxyAggregation: accessible is no XComponent!" );
211*cdf0e10cSrcweir         if ( _rxComponent.is() )
212*cdf0e10cSrcweir             componentAggregateProxyFor( _rxComponent, m_refCount, *this );
213*cdf0e10cSrcweir     }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     //-------------------------------------------------------------------------
216*cdf0e10cSrcweir     OComponentProxyAggregation::~OComponentProxyAggregation()
217*cdf0e10cSrcweir     {
218*cdf0e10cSrcweir         implEnsureDisposeInDtor( );
219*cdf0e10cSrcweir     }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir     //-------------------------------------------------------------------------
222*cdf0e10cSrcweir     IMPLEMENT_FORWARD_XINTERFACE2( OComponentProxyAggregation, OComponentProxyAggregation_CBase, OComponentProxyAggregationHelper )
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir     //-------------------------------------------------------------------------
225*cdf0e10cSrcweir     IMPLEMENT_GET_IMPLEMENTATION_ID( OComponentProxyAggregation )
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir     //-------------------------------------------------------------------------
228*cdf0e10cSrcweir     Sequence< Type > SAL_CALL OComponentProxyAggregation::getTypes(  ) throw (RuntimeException)
229*cdf0e10cSrcweir     {
230*cdf0e10cSrcweir         Sequence< Type > aTypes( OComponentProxyAggregationHelper::getTypes() );
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir         // append XComponent, coming from OComponentProxyAggregation_CBase
233*cdf0e10cSrcweir         sal_Int32 nLen = aTypes.getLength();
234*cdf0e10cSrcweir         aTypes.realloc( nLen + 1 );
235*cdf0e10cSrcweir         aTypes[ nLen ] = ::getCppuType( static_cast< Reference< XComponent >* >( NULL ) );
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir         return aTypes;
238*cdf0e10cSrcweir     }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir     //-------------------------------------------------------------------------
241*cdf0e10cSrcweir     void OComponentProxyAggregation::implEnsureDisposeInDtor( )
242*cdf0e10cSrcweir     {
243*cdf0e10cSrcweir         if ( !rBHelper.bDisposed )
244*cdf0e10cSrcweir         {
245*cdf0e10cSrcweir             acquire();  // to prevent duplicate dtor calls
246*cdf0e10cSrcweir             dispose();
247*cdf0e10cSrcweir         }
248*cdf0e10cSrcweir     }
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir     //--------------------------------------------------------------------
251*cdf0e10cSrcweir     void SAL_CALL OComponentProxyAggregation::disposing( const EventObject& _rSource ) throw (RuntimeException)
252*cdf0e10cSrcweir     {
253*cdf0e10cSrcweir         // simly disambiguate - this is necessary for MSVC to distinguish
254*cdf0e10cSrcweir         // "disposing( EventObject )" from "disposing()"
255*cdf0e10cSrcweir         OComponentProxyAggregationHelper::disposing( _rSource );
256*cdf0e10cSrcweir     }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir     //--------------------------------------------------------------------
259*cdf0e10cSrcweir     void SAL_CALL OComponentProxyAggregation::disposing()  throw (RuntimeException)
260*cdf0e10cSrcweir     {
261*cdf0e10cSrcweir         // call the dispose-functionality of the base, which will dispose our aggregated component
262*cdf0e10cSrcweir         OComponentProxyAggregationHelper::dispose();
263*cdf0e10cSrcweir     }
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir     //--------------------------------------------------------------------
266*cdf0e10cSrcweir     void SAL_CALL OComponentProxyAggregation::dispose() throw( RuntimeException )
267*cdf0e10cSrcweir     {
268*cdf0e10cSrcweir         // simply disambiguate
269*cdf0e10cSrcweir         OComponentProxyAggregation_CBase::dispose();
270*cdf0e10cSrcweir     }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir //.............................................................................
274*cdf0e10cSrcweir }   // namespace comphelper
275*cdf0e10cSrcweir //.............................................................................
276*cdf0e10cSrcweir 
277