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 <comphelper/accessibleeventnotifier.hxx>
21 : #include <osl/diagnose.h>
22 : #include <rtl/instance.hxx>
23 : #include <comphelper/guarding.hxx>
24 :
25 : using namespace ::com::sun::star::uno;
26 : using namespace ::com::sun::star::lang;
27 : using namespace ::com::sun::star::accessibility;
28 : using namespace ::comphelper;
29 :
30 : //=====================================================================
31 : //= AccessibleEventNotifier
32 : //=====================================================================
33 : //---------------------------------------------------------------------
34 : namespace
35 : {
36 : struct lclMutex
37 : : public rtl::Static< ::osl::Mutex, lclMutex > {};
38 : struct Clients
39 : : public rtl::Static< AccessibleEventNotifier::ClientMap, Clients > {};
40 : }
41 :
42 : //.........................................................................
43 : namespace comphelper
44 : {
45 : //.........................................................................
46 :
47 : //---------------------------------------------------------------------
48 235 : AccessibleEventNotifier::TClientId AccessibleEventNotifier::generateId()
49 : {
50 235 : TClientId nBiggestUsedId = 0;
51 235 : TClientId nFreeId = 0;
52 :
53 : // look through all registered clients until we find a "gap" in the ids
54 :
55 : // Note that the following relies on the fact the elements in the map are traveled with
56 : // ascending keys (aka client ids)
57 235 : AccessibleEventNotifier::ClientMap &rClients = Clients::get();
58 23151 : for ( ClientMap::const_iterator aLookup = rClients.begin();
59 15434 : aLookup != rClients.end();
60 : ++aLookup
61 : )
62 : {
63 7496 : TClientId nCurrent = aLookup->first;
64 : OSL_ENSURE( nCurrent > nBiggestUsedId, "AccessibleEventNotifier::generateId: map is expected to be sorted ascending!" );
65 :
66 7496 : if ( nCurrent - nBiggestUsedId > 1 )
67 : { // found a "gap"
68 14 : nFreeId = nBiggestUsedId + 1;
69 14 : break;
70 : }
71 :
72 7482 : nBiggestUsedId = nCurrent;
73 : }
74 :
75 235 : if ( !nFreeId )
76 221 : nFreeId = nBiggestUsedId + 1;
77 :
78 : OSL_ENSURE( rClients.end() == rClients.find( nFreeId ),
79 : "AccessibleEventNotifier::generateId: algorithm broken!" );
80 :
81 235 : return nFreeId;
82 : }
83 :
84 : //---------------------------------------------------------------------
85 235 : AccessibleEventNotifier::TClientId AccessibleEventNotifier::registerClient( )
86 : {
87 235 : ::osl::MutexGuard aGuard( lclMutex::get() );
88 :
89 : // generate a new client id
90 235 : TClientId nNewClientId = generateId( );
91 :
92 : // the event listeners for the new client
93 235 : EventListeners* pNewListeners = new EventListeners( lclMutex::get() );
94 : // note that we're using our own mutex here, so the listener containers for all
95 : // our clients share this same mutex.
96 : // this is a reminiscense to the days where the notifier was asynchronous. Today this is
97 : // completely nonsense, and potentially slowing down the Office me thinks ...
98 :
99 : // add the client
100 235 : Clients::get().insert( ClientMap::value_type( nNewClientId, pNewListeners ) );
101 :
102 : // outta here
103 235 : return nNewClientId;
104 : }
105 :
106 : //---------------------------------------------------------------------
107 531 : sal_Bool AccessibleEventNotifier::implLookupClient( const TClientId _nClient, ClientMap::iterator& _rPos )
108 : {
109 : // look up this client
110 531 : AccessibleEventNotifier::ClientMap &rClients = Clients::get();
111 531 : _rPos = rClients.find( _nClient );
112 : OSL_ENSURE( rClients.end() != _rPos, "AccessibleEventNotifier::implLookupClient: invalid client id (did you register your client?)!" );
113 :
114 531 : return ( rClients.end() != _rPos );
115 : }
116 :
117 : //---------------------------------------------------------------------
118 65 : void AccessibleEventNotifier::revokeClient( const TClientId _nClient )
119 : {
120 65 : ::osl::MutexGuard aGuard( lclMutex::get() );
121 :
122 65 : ClientMap::iterator aClientPos;
123 65 : if ( !implLookupClient( _nClient, aClientPos ) )
124 : // already asserted in implLookupClient
125 67 : return;
126 :
127 : // remove it from the clients map
128 63 : delete aClientPos->second;
129 63 : Clients::get().erase( aClientPos );
130 : }
131 :
132 : //---------------------------------------------------------------------
133 55 : void AccessibleEventNotifier::revokeClientNotifyDisposing( const TClientId _nClient,
134 : const Reference< XInterface >& _rxEventSource ) SAL_THROW( ( ) )
135 : {
136 55 : ::osl::MutexGuard aGuard( lclMutex::get() );
137 :
138 55 : ClientMap::iterator aClientPos;
139 55 : if ( !implLookupClient( _nClient, aClientPos ) )
140 : // already asserted in implLookupClient
141 55 : return;
142 :
143 : // notify the "disposing" event for this client
144 110 : EventObject aDisposalEvent;
145 55 : aDisposalEvent.Source = _rxEventSource;
146 :
147 : // notify the listeners
148 55 : EventListeners* pListeners = aClientPos->second;
149 :
150 : // we do not need the entry in the clients map anymore
151 : // (do this before actually notifying, because some client implementations have re-entrance
152 : // problems and call into revokeClient while we are notifying from hereing)
153 55 : Clients::get().erase( aClientPos );
154 :
155 : // now really do the notification
156 55 : pListeners->disposeAndClear( aDisposalEvent );
157 110 : delete pListeners;
158 :
159 : }
160 :
161 : //---------------------------------------------------------------------
162 201 : sal_Int32 AccessibleEventNotifier::addEventListener(
163 : const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener ) SAL_THROW( ( ) )
164 : {
165 201 : ::osl::MutexGuard aGuard( lclMutex::get() );
166 :
167 201 : ClientMap::iterator aClientPos;
168 201 : if ( !implLookupClient( _nClient, aClientPos ) )
169 : // already asserted in implLookupClient
170 0 : return 0;
171 :
172 201 : if ( _rxListener.is() )
173 201 : aClientPos->second->addInterface( _rxListener );
174 :
175 201 : return aClientPos->second->getLength();
176 : }
177 :
178 : //---------------------------------------------------------------------
179 37 : sal_Int32 AccessibleEventNotifier::removeEventListener(
180 : const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener ) SAL_THROW( ( ) )
181 : {
182 37 : ::osl::MutexGuard aGuard( lclMutex::get() );
183 :
184 37 : ClientMap::iterator aClientPos;
185 37 : if ( !implLookupClient( _nClient, aClientPos ) )
186 : // already asserted in implLookupClient
187 2 : return 0;
188 :
189 35 : if ( _rxListener.is() )
190 35 : aClientPos->second->removeInterface( _rxListener );
191 :
192 35 : return aClientPos->second->getLength();
193 : }
194 :
195 : //---------------------------------------------------------------------
196 173 : void AccessibleEventNotifier::addEvent( const TClientId _nClient, const AccessibleEventObject& _rEvent ) SAL_THROW( ( ) )
197 : {
198 173 : Sequence< Reference< XInterface > > aListeners;
199 :
200 : // --- <mutex lock> -------------------------------
201 : {
202 173 : ::osl::MutexGuard aGuard( lclMutex::get() );
203 :
204 173 : ClientMap::iterator aClientPos;
205 173 : if ( !implLookupClient( _nClient, aClientPos ) )
206 : // already asserted in implLookupClient
207 173 : return;
208 :
209 : // since we're synchronous, again, we want to notify immediately
210 173 : aListeners = aClientPos->second->getElements();
211 : }
212 : // --- </mutex lock> ------------------------------
213 :
214 : // default handling: loop through all listeners, and notify them
215 173 : const Reference< XInterface >* pListeners = aListeners.getConstArray();
216 173 : const Reference< XInterface >* pListenersEnd = pListeners + aListeners.getLength();
217 544 : while ( pListeners != pListenersEnd )
218 : {
219 : try
220 : {
221 198 : static_cast< XAccessibleEventListener* >( pListeners->get() )->notifyEvent( _rEvent );
222 : }
223 0 : catch( const Exception& )
224 : {
225 : // no assertion, because a broken access remote bridge or something like this
226 : // can cause this exception
227 : }
228 198 : ++pListeners;
229 173 : }
230 : }
231 :
232 : //.........................................................................
233 : } // namespace comphelper
234 : //.........................................................................
235 :
236 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|