1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*dde7d3faSAndrew Rist  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*dde7d3faSAndrew Rist  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19*dde7d3faSAndrew Rist  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir #include <comphelper/accessibleeventnotifier.hxx>
27cdf0e10cSrcweir #include <osl/diagnose.h>
28cdf0e10cSrcweir #include <rtl/instance.hxx>
29cdf0e10cSrcweir #include <comphelper/guarding.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using namespace ::com::sun::star::uno;
32cdf0e10cSrcweir using namespace ::com::sun::star::lang;
33cdf0e10cSrcweir using namespace ::com::sun::star::accessibility;
34cdf0e10cSrcweir using namespace ::comphelper;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir //=====================================================================
37cdf0e10cSrcweir //= AccessibleEventNotifier
38cdf0e10cSrcweir //=====================================================================
39cdf0e10cSrcweir //---------------------------------------------------------------------
40cdf0e10cSrcweir namespace
41cdf0e10cSrcweir {
42cdf0e10cSrcweir     struct lclMutex
43cdf0e10cSrcweir         : public rtl::Static< ::osl::Mutex, lclMutex > {};
44cdf0e10cSrcweir     struct Clients
45cdf0e10cSrcweir         : public rtl::Static< AccessibleEventNotifier::ClientMap, Clients > {};
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //.........................................................................
49cdf0e10cSrcweir namespace comphelper
50cdf0e10cSrcweir {
51cdf0e10cSrcweir //.........................................................................
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 	//---------------------------------------------------------------------
generateId()54cdf0e10cSrcweir 	AccessibleEventNotifier::TClientId AccessibleEventNotifier::generateId()
55cdf0e10cSrcweir 	{
56cdf0e10cSrcweir 		TClientId nBiggestUsedId = 0;
57cdf0e10cSrcweir 		TClientId nFreeId = 0;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 		// look through all registered clients until we find a "gap" in the ids
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 		// Note that the following relies on the fact the elements in the map are traveled with
62cdf0e10cSrcweir 		// ascending keys (aka client ids)
63cdf0e10cSrcweir         AccessibleEventNotifier::ClientMap &rClients = Clients::get();
64cdf0e10cSrcweir 		for	(	ClientMap::const_iterator aLookup = rClients.begin();
65cdf0e10cSrcweir 				aLookup != rClients.end();
66cdf0e10cSrcweir 				++aLookup
67cdf0e10cSrcweir 			)
68cdf0e10cSrcweir 		{
69cdf0e10cSrcweir 			TClientId nCurrent = aLookup->first;
70cdf0e10cSrcweir 			OSL_ENSURE( nCurrent > nBiggestUsedId, "AccessibleEventNotifier::generateId: map is expected to be sorted ascending!" );
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 			if ( nCurrent - nBiggestUsedId > 1 )
73cdf0e10cSrcweir 			{	// found a "gap"
74cdf0e10cSrcweir 				nFreeId = nBiggestUsedId + 1;
75cdf0e10cSrcweir 				break;
76cdf0e10cSrcweir 			}
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 			nBiggestUsedId = nCurrent;
79cdf0e10cSrcweir 		}
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 		if ( !nFreeId )
82cdf0e10cSrcweir 			nFreeId = nBiggestUsedId + 1;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 		OSL_ENSURE( rClients.end() == rClients.find( nFreeId ),
85cdf0e10cSrcweir 			"AccessibleEventNotifier::generateId: algorithm broken!" );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 		return nFreeId;
88cdf0e10cSrcweir 	}
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 	//---------------------------------------------------------------------
registerClient()91cdf0e10cSrcweir 	AccessibleEventNotifier::TClientId AccessibleEventNotifier::registerClient( )
92cdf0e10cSrcweir 	{
93cdf0e10cSrcweir 		::osl::MutexGuard aGuard( lclMutex::get() );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 		// generate a new client id
96cdf0e10cSrcweir 		TClientId nNewClientId = generateId( );
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 		// the event listeners for the new client
99cdf0e10cSrcweir 		EventListeners* pNewListeners = new EventListeners( lclMutex::get() );
100cdf0e10cSrcweir 			// note that we're using our own mutex here, so the listener containers for all
101cdf0e10cSrcweir 			// our clients share this same mutex.
102cdf0e10cSrcweir 			// this is a reminiscense to the days where the notifier was asynchronous. Today this is
103cdf0e10cSrcweir 			// completely nonsense, and potentially slowing down the Office me thinks ...
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 		// add the client
106cdf0e10cSrcweir 		Clients::get().insert( ClientMap::value_type( nNewClientId, pNewListeners ) );
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 		// outta here
109cdf0e10cSrcweir 		return nNewClientId;
110cdf0e10cSrcweir 	}
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 	//---------------------------------------------------------------------
implLookupClient(const TClientId _nClient,ClientMap::iterator & _rPos)113cdf0e10cSrcweir 	sal_Bool AccessibleEventNotifier::implLookupClient( const TClientId _nClient, ClientMap::iterator& _rPos )
114cdf0e10cSrcweir 	{
115cdf0e10cSrcweir 		// look up this client
116cdf0e10cSrcweir         AccessibleEventNotifier::ClientMap &rClients = Clients::get();
117cdf0e10cSrcweir 		_rPos = rClients.find( _nClient );
118cdf0e10cSrcweir 		OSL_ENSURE( rClients.end() != _rPos, "AccessibleEventNotifier::implLookupClient: invalid client id (did you register your client?)!" );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 		return ( rClients.end() != _rPos );
121cdf0e10cSrcweir 	}
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 	//---------------------------------------------------------------------
revokeClient(const TClientId _nClient)124cdf0e10cSrcweir 	void AccessibleEventNotifier::revokeClient( const TClientId _nClient )
125cdf0e10cSrcweir 	{
126cdf0e10cSrcweir 		::osl::MutexGuard aGuard( lclMutex::get() );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 		ClientMap::iterator aClientPos;
129cdf0e10cSrcweir 		if ( !implLookupClient( _nClient, aClientPos ) )
130cdf0e10cSrcweir 			// already asserted in implLookupClient
131cdf0e10cSrcweir 			return;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 		// remove it from the clients map
134cdf0e10cSrcweir 		delete aClientPos->second;
135cdf0e10cSrcweir 		Clients::get().erase( aClientPos );
136cdf0e10cSrcweir 	}
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 	//---------------------------------------------------------------------
revokeClientNotifyDisposing(const TClientId _nClient,const Reference<XInterface> & _rxEventSource)139cdf0e10cSrcweir 	void AccessibleEventNotifier::revokeClientNotifyDisposing( const TClientId _nClient,
140cdf0e10cSrcweir 			const Reference< XInterface >& _rxEventSource ) SAL_THROW( ( ) )
141cdf0e10cSrcweir 	{
142cdf0e10cSrcweir 		::osl::MutexGuard aGuard( lclMutex::get() );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 		ClientMap::iterator aClientPos;
145cdf0e10cSrcweir 		if ( !implLookupClient( _nClient, aClientPos ) )
146cdf0e10cSrcweir 			// already asserted in implLookupClient
147cdf0e10cSrcweir 			return;
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 		// notify the "disposing" event for this client
150cdf0e10cSrcweir 		EventObject aDisposalEvent;
151cdf0e10cSrcweir 		aDisposalEvent.Source = _rxEventSource;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 		// notify the listeners
154cdf0e10cSrcweir 		EventListeners* pListeners = aClientPos->second;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 		// we do not need the entry in the clients map anymore
157cdf0e10cSrcweir 		// (do this before actually notifying, because some client implementations have re-entrance
158cdf0e10cSrcweir 		// problems and call into revokeClient while we are notifying from hereing)
159cdf0e10cSrcweir 		Clients::get().erase( aClientPos );
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 		// now really do the notification
162cdf0e10cSrcweir 		pListeners->disposeAndClear( aDisposalEvent );
163cdf0e10cSrcweir 		delete pListeners;
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 	}
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 	//---------------------------------------------------------------------
addEventListener(const TClientId _nClient,const Reference<XAccessibleEventListener> & _rxListener)168cdf0e10cSrcweir 	sal_Int32 AccessibleEventNotifier::addEventListener(
169cdf0e10cSrcweir 		const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener ) SAL_THROW( ( ) )
170cdf0e10cSrcweir 	{
171cdf0e10cSrcweir 		::osl::MutexGuard aGuard( lclMutex::get() );
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 		ClientMap::iterator aClientPos;
174cdf0e10cSrcweir 		if ( !implLookupClient( _nClient, aClientPos ) )
175cdf0e10cSrcweir 			// already asserted in implLookupClient
176cdf0e10cSrcweir 			return 0;
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 		if ( _rxListener.is() )
179cdf0e10cSrcweir 			aClientPos->second->addInterface( _rxListener );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 		return aClientPos->second->getLength();
182cdf0e10cSrcweir 	}
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 	//---------------------------------------------------------------------
removeEventListener(const TClientId _nClient,const Reference<XAccessibleEventListener> & _rxListener)185cdf0e10cSrcweir 	sal_Int32 AccessibleEventNotifier::removeEventListener(
186cdf0e10cSrcweir 		const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener ) SAL_THROW( ( ) )
187cdf0e10cSrcweir 	{
188cdf0e10cSrcweir 		::osl::MutexGuard aGuard( lclMutex::get() );
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 		ClientMap::iterator aClientPos;
191cdf0e10cSrcweir 		if ( !implLookupClient( _nClient, aClientPos ) )
192cdf0e10cSrcweir 			// already asserted in implLookupClient
193cdf0e10cSrcweir 			return 0;
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 		if ( _rxListener.is() )
196cdf0e10cSrcweir 			aClientPos->second->removeInterface( _rxListener );
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 		return aClientPos->second->getLength();
199cdf0e10cSrcweir 	}
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 	//---------------------------------------------------------------------
getEventListeners(const TClientId _nClient)202cdf0e10cSrcweir 	Sequence< Reference< XInterface > > AccessibleEventNotifier::getEventListeners( const TClientId _nClient ) SAL_THROW( ( ) )
203cdf0e10cSrcweir 	{
204cdf0e10cSrcweir 		Sequence< Reference< XInterface > > aListeners;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 		::osl::MutexGuard aGuard( lclMutex::get() );
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 		ClientMap::iterator aClientPos;
209cdf0e10cSrcweir 		if ( implLookupClient( _nClient, aClientPos ) )
210cdf0e10cSrcweir 			aListeners = aClientPos->second->getElements();
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 		return aListeners;
213cdf0e10cSrcweir 	}
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 	//---------------------------------------------------------------------
addEvent(const TClientId _nClient,const AccessibleEventObject & _rEvent)216cdf0e10cSrcweir 	void AccessibleEventNotifier::addEvent( const TClientId _nClient, const AccessibleEventObject& _rEvent ) SAL_THROW( ( ) )
217cdf0e10cSrcweir 	{
218cdf0e10cSrcweir 		Sequence< Reference< XInterface > > aListeners;
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 		// --- <mutex lock> -------------------------------
221cdf0e10cSrcweir 		{
222cdf0e10cSrcweir 			::osl::MutexGuard aGuard( lclMutex::get() );
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 			ClientMap::iterator aClientPos;
225cdf0e10cSrcweir 			if ( !implLookupClient( _nClient, aClientPos ) )
226cdf0e10cSrcweir 				// already asserted in implLookupClient
227cdf0e10cSrcweir 				return;
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 			// since we're synchronous, again, we want to notify immediately
230cdf0e10cSrcweir 			aListeners = aClientPos->second->getElements();
231cdf0e10cSrcweir 		}
232cdf0e10cSrcweir 		// --- </mutex lock> ------------------------------
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 			// default handling: loop through all listeners, and notify them
235cdf0e10cSrcweir 		const Reference< XInterface >* pListeners = aListeners.getConstArray();
236cdf0e10cSrcweir 		const Reference< XInterface >* pListenersEnd = pListeners + aListeners.getLength();
237cdf0e10cSrcweir 		while ( pListeners != pListenersEnd )
238cdf0e10cSrcweir 		{
239cdf0e10cSrcweir 			try
240cdf0e10cSrcweir 			{
241cdf0e10cSrcweir 				static_cast< XAccessibleEventListener* >( pListeners->get() )->notifyEvent( _rEvent );
242cdf0e10cSrcweir 			}
243cdf0e10cSrcweir 			catch( const Exception& )
244cdf0e10cSrcweir 			{
245cdf0e10cSrcweir 				// no assertion, because a broken access remote bridge or something like this
246cdf0e10cSrcweir 				// can cause this exception
247cdf0e10cSrcweir 			}
248cdf0e10cSrcweir 			++pListeners;
249cdf0e10cSrcweir 		}
250cdf0e10cSrcweir 	}
251cdf0e10cSrcweir 
252cdf0e10cSrcweir //.........................................................................
253cdf0e10cSrcweir }	// namespace comphelper
254cdf0e10cSrcweir //.........................................................................
255cdf0e10cSrcweir 
256