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