1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cppuhelper.hxx"
26 #include <rtl/string.hxx>
27 #include <osl/diagnose.h>
28 #include <cppuhelper/component.hxx>
29 #include <cppuhelper/queryinterface.hxx>
30 #include <cppuhelper/typeprovider.hxx>
31 #include "com/sun/star/uno/RuntimeException.hpp"
32
33 using namespace osl;
34 using namespace rtl;
35 using namespace com::sun::star;
36 using namespace com::sun::star::uno;
37 using namespace com::sun::star::lang;
38
39 namespace cppu
40 {
41
42 // ----------------------------------------------------
43 // class OComponentHelper
44 // ----------------------------------------------------
45
OComponentHelper(Mutex & rMutex)46 OComponentHelper::OComponentHelper( Mutex & rMutex ) SAL_THROW( () )
47 : rBHelper( rMutex )
48 {
49 }
~OComponentHelper()50 OComponentHelper::~OComponentHelper()
51 {
52 }
53
queryInterface(Type const & rType)54 Any OComponentHelper::queryInterface( Type const & rType )
55 {
56 return OWeakAggObject::queryInterface( rType );
57 }
queryAggregation(Type const & rType)58 Any OComponentHelper::queryAggregation( Type const & rType )
59 {
60 if (rType == ::getCppuType( (Reference< lang::XComponent > const *)0 ))
61 {
62 void * p = static_cast< lang::XComponent * >( this );
63 return Any( &p, rType );
64 }
65 else if (rType == ::getCppuType( (Reference< lang::XTypeProvider > const *)0 ))
66 {
67 void * p = static_cast< lang::XTypeProvider * >( this );
68 return Any( &p, rType );
69 }
70 return OWeakAggObject::queryAggregation( rType );
71 }
acquire()72 void OComponentHelper::acquire() throw ()
73 {
74 OWeakAggObject::acquire();
75 }
76
release()77 void OComponentHelper::release() throw()
78 {
79 Reference<XInterface > x( xDelegator );
80 if (! x.is())
81 {
82 if (osl_decrementInterlockedCount( &m_refCount ) == 0)
83 {
84 if (! rBHelper.bDisposed)
85 {
86 // *before* again incrementing our ref count, ensure that our weak connection point
87 // will not create references to us anymore (via XAdapter::queryAdapted)
88 disposeWeakConnectionPoint();
89
90 Reference<XInterface > xHoldAlive( *this );
91 // First dispose
92 try
93 {
94 dispose();
95 }
96 catch (::com::sun::star::uno::RuntimeException & exc)
97 {
98 // release should not throw exceptions
99 #if OSL_DEBUG_LEVEL > 0
100 OString msg( OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
101 OSL_ENSURE( 0, msg.getStr() );
102 #else
103 (void) exc; // avoid warning about unused variable
104 #endif
105 }
106
107 // only the alive ref holds the object
108 OSL_ASSERT( m_refCount == 1 );
109 // destroy the object if xHoldAlive decrement the refcount to 0
110 return;
111 }
112 }
113 // restore the reference count
114 osl_incrementInterlockedCount( &m_refCount );
115 }
116 OWeakAggObject::release();
117 }
118
getTypes()119 Sequence< Type > OComponentHelper::getTypes()
120 {
121 static OTypeCollection * s_pTypes = 0;
122 if (! s_pTypes)
123 {
124 MutexGuard aGuard( Mutex::getGlobalMutex() );
125 if (! s_pTypes)
126 {
127 static OTypeCollection s_aTypes(
128 ::getCppuType( (const Reference< lang::XComponent > *)0 ),
129 ::getCppuType( (const Reference< lang::XTypeProvider > *)0 ),
130 ::getCppuType( (const Reference< XAggregation > *)0 ),
131 ::getCppuType( (const Reference< XWeak > *)0 ) );
132 s_pTypes = &s_aTypes;
133 }
134 }
135 return s_pTypes->getTypes();
136 }
137
138 // XComponent
disposing()139 void OComponentHelper::disposing()
140 {
141 }
142
143 // XComponent
dispose()144 void OComponentHelper::dispose()
145 {
146 // An frequently programming error is to release the last
147 // reference to this object in the disposing message.
148 // Make it robust, hold a self Reference.
149 Reference<XComponent > xSelf( this );
150
151 // Guard dispose against multiple threading
152 // Remark: It is an error to call dispose more than once
153 sal_Bool bDoDispose = sal_False;
154 {
155 MutexGuard aGuard( rBHelper.rMutex );
156 if( !rBHelper.bDisposed && !rBHelper.bInDispose )
157 {
158 // only one call go into this section
159 rBHelper.bInDispose = sal_True;
160 bDoDispose = sal_True;
161 }
162 }
163
164 // Do not hold the mutex because we are broadcasting
165 if( bDoDispose )
166 {
167 // Create an event with this as sender
168 try
169 {
170 try
171 {
172 Reference<XInterface > xSource(
173 Reference<XInterface >::query( (XComponent *)this ) );
174 EventObject aEvt;
175 aEvt.Source = xSource;
176 // inform all listeners to release this object
177 // The listener container are automatically cleared
178 rBHelper.aLC.disposeAndClear( aEvt );
179 // notify subclasses to do their dispose
180 disposing();
181 }
182 catch (...)
183 {
184 MutexGuard aGuard( rBHelper.rMutex );
185 // bDispose and bInDisposing must be set in this order:
186 rBHelper.bDisposed = sal_True;
187 rBHelper.bInDispose = sal_False;
188 throw;
189 }
190 MutexGuard aGuard( rBHelper.rMutex );
191 // bDispose and bInDisposing must be set in this order:
192 rBHelper.bDisposed = sal_True;
193 rBHelper.bInDispose = sal_False;
194 }
195 catch (RuntimeException &)
196 {
197 throw;
198 }
199 catch (Exception & exc)
200 {
201 throw RuntimeException(
202 OUString( RTL_CONSTASCII_USTRINGPARAM(
203 "unexpected UNO exception caught: ") ) +
204 exc.Message, Reference< XInterface >() );
205 }
206 }
207 else
208 {
209 // in a multithreaded environment, it can't be avoided,
210 // that dispose is called twice.
211 // However this condition is traced, because it MAY indicate an error.
212 OSL_TRACE( "OComponentHelper::dispose() - dispose called twice" );
213 }
214 }
215
216 // XComponent
addEventListener(const Reference<XEventListener> & rxListener)217 void OComponentHelper::addEventListener(
218 const Reference<XEventListener > & rxListener )
219 {
220 ClearableMutexGuard aGuard( rBHelper.rMutex );
221 if (rBHelper.bDisposed || rBHelper.bInDispose)
222 {
223 aGuard.clear();
224 Reference< XInterface > x( (XComponent *)this, UNO_QUERY );
225 rxListener->disposing( EventObject( x ) );
226 }
227 else
228 {
229 rBHelper.addListener( ::getCppuType( &rxListener ) , rxListener );
230 }
231 }
232
233 // XComponent
removeEventListener(const Reference<XEventListener> & rxListener)234 void OComponentHelper::removeEventListener(
235 const Reference<XEventListener > & rxListener )
236 {
237 rBHelper.removeListener( ::getCppuType( &rxListener ) , rxListener );
238 }
239
240 }
241