Line data Source code
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 : #include "sal/config.h"
21 :
22 : #include <cassert>
23 :
24 : #include <comphelper/proxyaggregation.hxx>
25 : #include <com/sun/star/reflection/ProxyFactory.hpp>
26 :
27 :
28 : namespace comphelper
29 : {
30 :
31 :
32 : using namespace ::com::sun::star::uno;
33 : using namespace ::com::sun::star::lang;
34 : using namespace ::com::sun::star::reflection;
35 :
36 :
37 : //= OProxyAggregation
38 :
39 :
40 0 : OProxyAggregation::OProxyAggregation( const Reference< XComponentContext >& _rxContext )
41 0 : :m_xContext( _rxContext )
42 : {
43 0 : }
44 :
45 :
46 0 : void OProxyAggregation::baseAggregateProxyFor( const Reference< XInterface >& _rxComponent, oslInterlockedCount& _rRefCount,
47 : ::cppu::OWeakObject& _rDelegator )
48 : {
49 : // first a factory for the proxy
50 0 : Reference< XProxyFactory > xFactory = ProxyFactory::create( m_xContext );
51 :
52 : // then the proxy itself
53 : { // i36686 OJ: achieve the desctruction of the temporary -> otherwise it leads to _rRefCount -= 2
54 0 : m_xProxyAggregate = xFactory->createProxy( _rxComponent );
55 : }
56 0 : if ( m_xProxyAggregate.is() )
57 0 : m_xProxyAggregate->queryAggregation( ::getCppuType( &m_xProxyTypeAccess ) ) >>= m_xProxyTypeAccess;
58 :
59 : // aggregate the proxy
60 0 : osl_atomic_increment( &_rRefCount );
61 0 : if ( m_xProxyAggregate.is() )
62 : {
63 : // At this point in time, the proxy has a ref count of exactly two - in m_xControlContextProxy,
64 : // and in m_xProxyTypeAccess.
65 : // Remember to _not_ reset these members unless the delegator of the proxy has been reset, too!
66 0 : m_xProxyAggregate->setDelegator( _rDelegator );
67 : }
68 0 : osl_atomic_decrement( &_rRefCount );
69 0 : }
70 :
71 :
72 0 : Any SAL_CALL OProxyAggregation::queryAggregation( const Type& _rType ) throw (RuntimeException)
73 : {
74 0 : return m_xProxyAggregate.is() ? m_xProxyAggregate->queryAggregation( _rType ) : Any();
75 : }
76 :
77 :
78 0 : Sequence< Type > SAL_CALL OProxyAggregation::getTypes( ) throw (RuntimeException)
79 : {
80 0 : Sequence< Type > aTypes;
81 0 : if ( m_xProxyAggregate.is() )
82 : {
83 0 : if ( m_xProxyTypeAccess.is() )
84 0 : aTypes = m_xProxyTypeAccess->getTypes();
85 : }
86 0 : return aTypes;
87 : }
88 :
89 :
90 0 : OProxyAggregation::~OProxyAggregation()
91 : {
92 0 : if ( m_xProxyAggregate.is() )
93 0 : m_xProxyAggregate->setDelegator( NULL );
94 0 : m_xProxyAggregate.clear();
95 0 : m_xProxyTypeAccess.clear();
96 : // this should remove the _two_only_ "real" references (means not delegated to
97 : // ourself) to this proxy, and thus delete it
98 0 : }
99 :
100 :
101 : //= OComponentProxyAggregationHelper
102 :
103 :
104 0 : OComponentProxyAggregationHelper::OComponentProxyAggregationHelper( const Reference< XComponentContext >& _rxContext,
105 : ::cppu::OBroadcastHelper& _rBHelper )
106 : :OProxyAggregation( _rxContext )
107 0 : ,m_rBHelper( _rBHelper )
108 : {
109 : OSL_ENSURE( _rxContext.is(), "OComponentProxyAggregationHelper::OComponentProxyAggregationHelper: invalid arguments!" );
110 0 : }
111 :
112 :
113 0 : void OComponentProxyAggregationHelper::componentAggregateProxyFor(
114 : const Reference< XComponent >& _rxComponent, oslInterlockedCount& _rRefCount,
115 : ::cppu::OWeakObject& _rDelegator )
116 : {
117 : OSL_ENSURE( _rxComponent.is(), "OComponentProxyAggregationHelper::componentAggregateProxyFor: invalid inner component!" );
118 0 : m_xInner = _rxComponent;
119 :
120 : // aggregate a proxy for the object
121 0 : baseAggregateProxyFor( m_xInner.get(), _rRefCount, _rDelegator );
122 :
123 : // add as event listener to the inner context, because we want to be notified of disposals
124 0 : osl_atomic_increment( &_rRefCount );
125 : {
126 0 : if ( m_xInner.is() )
127 0 : m_xInner->addEventListener( this );
128 : }
129 0 : osl_atomic_decrement( &_rRefCount );
130 0 : }
131 :
132 :
133 0 : Any SAL_CALL OComponentProxyAggregationHelper::queryInterface( const Type& _rType ) throw (RuntimeException, std::exception)
134 : {
135 0 : Any aReturn( BASE::queryInterface( _rType ) );
136 0 : if ( !aReturn.hasValue() )
137 0 : aReturn = OProxyAggregation::queryAggregation( _rType );
138 0 : return aReturn;
139 : }
140 :
141 :
142 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( OComponentProxyAggregationHelper, BASE, OProxyAggregation )
143 :
144 :
145 0 : OComponentProxyAggregationHelper::~OComponentProxyAggregationHelper( )
146 : {
147 : OSL_ENSURE( m_rBHelper.bDisposed, "OComponentProxyAggregationHelper::~OComponentProxyAggregationHelper: you should dispose your derived class in the dtor, if necessary!" );
148 : // if this asserts, add the following to your derived class dtor:
149 :
150 : // if ( !m_rBHelper.bDisposed )
151 : // {
152 : // acquire(); // to prevent duplicate dtor calls
153 : // dispose();
154 : // }
155 :
156 0 : m_xInner.clear();
157 0 : }
158 :
159 :
160 0 : void SAL_CALL OComponentProxyAggregationHelper::disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception)
161 : {
162 0 : if ( _rSource.Source == m_xInner )
163 : { // it's our inner context which is dying -> dispose ourself
164 0 : if ( !m_rBHelper.bDisposed && !m_rBHelper.bInDispose )
165 : { // (if necessary only, of course)
166 0 : dispose();
167 : }
168 : }
169 0 : }
170 :
171 :
172 0 : void SAL_CALL OComponentProxyAggregationHelper::dispose() throw( RuntimeException, std::exception )
173 : {
174 0 : ::osl::MutexGuard aGuard( m_rBHelper.rMutex );
175 :
176 : // dispose our inner context
177 : // before we do this, remove ourself as listener - else in disposing( EventObject ), we
178 : // would dispose ourself a second time
179 0 : Reference< XComponent > xComp( m_xInner, UNO_QUERY );
180 0 : if ( xComp.is() )
181 : {
182 0 : xComp->removeEventListener( this );
183 0 : xComp->dispose();
184 0 : xComp.clear();
185 0 : }
186 0 : }
187 :
188 :
189 : //= OComponentProxyAggregation
190 :
191 :
192 0 : OComponentProxyAggregation::OComponentProxyAggregation( const Reference< XComponentContext >& _rxContext,
193 : const Reference< XComponent >& _rxComponent )
194 : :WeakComponentImplHelperBase( m_aMutex )
195 0 : ,OComponentProxyAggregationHelper( _rxContext, rBHelper )
196 : {
197 : OSL_ENSURE( _rxComponent.is(), "OComponentProxyAggregation::OComponentProxyAggregation: accessible is no XComponent!" );
198 0 : if ( _rxComponent.is() )
199 0 : componentAggregateProxyFor( _rxComponent, m_refCount, *this );
200 0 : }
201 :
202 :
203 0 : OComponentProxyAggregation::~OComponentProxyAggregation()
204 : {
205 0 : implEnsureDisposeInDtor( );
206 0 : }
207 :
208 :
209 0 : IMPLEMENT_FORWARD_XINTERFACE2( OComponentProxyAggregation, WeakComponentImplHelperBase, OComponentProxyAggregationHelper )
210 :
211 :
212 0 : IMPLEMENT_GET_IMPLEMENTATION_ID( OComponentProxyAggregation )
213 :
214 :
215 0 : Sequence< Type > SAL_CALL OComponentProxyAggregation::getTypes( ) throw (RuntimeException, std::exception)
216 : {
217 0 : Sequence< Type > aTypes( OComponentProxyAggregationHelper::getTypes() );
218 :
219 : // append XComponent, coming from WeakComponentImplHelperBase
220 0 : sal_Int32 nLen = aTypes.getLength();
221 0 : aTypes.realloc( nLen + 1 );
222 0 : aTypes[ nLen ] = ::getCppuType( static_cast< Reference< XComponent >* >( NULL ) );
223 :
224 0 : return aTypes;
225 : }
226 :
227 :
228 0 : void OComponentProxyAggregation::implEnsureDisposeInDtor( )
229 : {
230 0 : if ( !rBHelper.bDisposed )
231 : {
232 0 : acquire(); // to prevent duplicate dtor calls
233 0 : dispose();
234 : }
235 0 : }
236 :
237 :
238 0 : void SAL_CALL OComponentProxyAggregation::disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception)
239 : {
240 : // Simply disambiguate---this is necessary for MSVC to distinguish
241 : // "disposing(EventObject)" from "disposing()"; but it is also a good
242 : // place to check for recursive calls that would be caused by an object
243 : // being registered as an XEventListener at itself (cf. rhbz#928568):
244 : assert(_rSource.Source != static_cast< cppu::OWeakObject * >(this));
245 0 : OComponentProxyAggregationHelper::disposing( _rSource );
246 0 : }
247 :
248 :
249 0 : void SAL_CALL OComponentProxyAggregation::disposing() throw (RuntimeException)
250 : {
251 : // call the dispose-functionality of the base, which will dispose our aggregated component
252 0 : OComponentProxyAggregationHelper::dispose();
253 0 : }
254 :
255 :
256 0 : void SAL_CALL OComponentProxyAggregation::dispose() throw( RuntimeException, std::exception )
257 : {
258 : // simply disambiguate
259 0 : WeakComponentImplHelperBase::dispose();
260 0 : }
261 :
262 :
263 :
264 : } // namespace comphelper
265 :
266 :
267 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|