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 <cppuhelper/interfacecontainer.h>
24 : #include <comphelper/guarding.hxx>
25 :
26 : #include <map>
27 : #include <limits>
28 :
29 : using namespace ::com::sun::star::uno;
30 : using namespace ::com::sun::star::lang;
31 : using namespace ::com::sun::star::accessibility;
32 : using namespace ::comphelper;
33 :
34 : namespace
35 : {
36 : typedef ::std::pair< AccessibleEventNotifier::TClientId,
37 : AccessibleEventObject > ClientEvent;
38 :
39 : typedef ::std::map< AccessibleEventNotifier::TClientId,
40 : ::cppu::OInterfaceContainerHelper*,
41 : ::std::less< AccessibleEventNotifier::TClientId > > ClientMap;
42 :
43 : /// key is the end of the interval, value is the start of the interval
44 : typedef ::std::map<AccessibleEventNotifier::TClientId,
45 : AccessibleEventNotifier::TClientId> IntervalMap;
46 :
47 : struct lclMutex
48 : : public rtl::Static< ::osl::Mutex, lclMutex > {};
49 : struct Clients
50 : : public rtl::Static< ClientMap, Clients > {};
51 : struct FreeIntervals
52 : : public rtl::StaticWithInit<IntervalMap, FreeIntervals> {
53 7 : IntervalMap operator() () {
54 7 : IntervalMap map;
55 : map.insert(::std::make_pair(
56 7 : ::std::numeric_limits<AccessibleEventNotifier::TClientId>::max(), 1));
57 7 : return map;
58 : }
59 : };
60 :
61 121 : static void releaseId(AccessibleEventNotifier::TClientId const nId)
62 : {
63 121 : IntervalMap & rFreeIntervals(FreeIntervals::get());
64 121 : IntervalMap::iterator const upper(rFreeIntervals.upper_bound(nId));
65 : assert(upper != rFreeIntervals.end());
66 : assert(nId < upper->second); // second is start of the interval!
67 121 : if (nId + 1 == upper->second)
68 : {
69 62 : --upper->second; // add nId to existing interval
70 : }
71 : else
72 : {
73 59 : IntervalMap::iterator const lower(rFreeIntervals.lower_bound(nId));
74 59 : if (lower != rFreeIntervals.end() && lower->first == nId - 1)
75 : {
76 : // add nId by replacing lower with new merged entry
77 0 : rFreeIntervals.insert(::std::make_pair(nId, lower->second));
78 0 : rFreeIntervals.erase(lower);
79 : }
80 : else // otherwise just add new 1-element interval
81 : {
82 59 : rFreeIntervals.insert(::std::make_pair(nId, nId));
83 : }
84 : }
85 : // currently it's not checked whether intervals can be merged now
86 : // hopefully that won't be a problem in practice
87 121 : }
88 :
89 : /// generates a new client id
90 255 : static AccessibleEventNotifier::TClientId generateId()
91 : {
92 255 : IntervalMap & rFreeIntervals(FreeIntervals::get());
93 : assert(!rFreeIntervals.empty());
94 255 : IntervalMap::iterator const iter(rFreeIntervals.begin());
95 255 : AccessibleEventNotifier::TClientId const nFirst = iter->first;
96 255 : AccessibleEventNotifier::TClientId const nFreeId = iter->second;
97 : assert(nFreeId <= nFirst);
98 255 : if (nFreeId != nFirst)
99 : {
100 218 : ++iter->second; // remove nFreeId from interval
101 : }
102 : else
103 : {
104 37 : rFreeIntervals.erase(iter); // remove 1-element interval
105 : }
106 :
107 : assert(Clients::get().end() == Clients::get().find(nFreeId));
108 :
109 255 : return nFreeId;
110 : }
111 :
112 : /** looks up a client in our client map, asserts if it cannot find it or
113 : no event thread is present
114 :
115 : @precond
116 : to be called with our mutex locked
117 :
118 : @param nClient
119 : the id of the client to loopup
120 : @param rPos
121 : out-parameter for the position of the client in the client map
122 :
123 : @return
124 : <TRUE/> if and only if the client could be found and
125 : <arg>rPos</arg> has been filled with it's position
126 : */
127 553 : static bool implLookupClient(
128 : const AccessibleEventNotifier::TClientId nClient,
129 : ClientMap::iterator& rPos )
130 : {
131 : // look up this client
132 553 : ClientMap &rClients = Clients::get();
133 553 : rPos = rClients.find( nClient );
134 : OSL_ENSURE( rClients.end() != rPos,
135 : "AccessibleEventNotifier::implLookupClient: invalid client id "
136 : "(did you register your client?)!" );
137 :
138 553 : return ( rClients.end() != rPos );
139 : }
140 : }
141 :
142 :
143 : namespace comphelper
144 : {
145 :
146 :
147 :
148 255 : AccessibleEventNotifier::TClientId AccessibleEventNotifier::registerClient( )
149 : {
150 255 : ::osl::MutexGuard aGuard( lclMutex::get() );
151 :
152 : // generate a new client id
153 255 : TClientId nNewClientId = generateId( );
154 :
155 : // the event listeners for the new client
156 : ::cppu::OInterfaceContainerHelper *const pNewListeners =
157 255 : new ::cppu::OInterfaceContainerHelper( lclMutex::get() );
158 : // note that we're using our own mutex here, so the listener containers for all
159 : // our clients share this same mutex.
160 : // this is a reminiscense to the days where the notifier was asynchronous. Today this is
161 : // completely nonsense, and potentially slowing down the Office me thinks ...
162 :
163 : // add the client
164 255 : Clients::get().insert( ClientMap::value_type( nNewClientId, pNewListeners ) );
165 :
166 : // outta here
167 255 : return nNewClientId;
168 : }
169 :
170 :
171 64 : void AccessibleEventNotifier::revokeClient( const TClientId _nClient )
172 : {
173 64 : ::osl::MutexGuard aGuard( lclMutex::get() );
174 :
175 64 : ClientMap::iterator aClientPos;
176 64 : if ( !implLookupClient( _nClient, aClientPos ) )
177 : // already asserted in implLookupClient
178 66 : return;
179 :
180 : // remove it from the clients map
181 62 : delete aClientPos->second;
182 62 : Clients::get().erase( aClientPos );
183 62 : releaseId(_nClient);
184 : }
185 :
186 :
187 59 : void AccessibleEventNotifier::revokeClientNotifyDisposing( const TClientId _nClient,
188 : const Reference< XInterface >& _rxEventSource )
189 : {
190 59 : ::cppu::OInterfaceContainerHelper* pListeners(0);
191 :
192 : {
193 : // rhbz#1001768 drop the mutex before calling disposeAndClear
194 59 : ::osl::MutexGuard aGuard( lclMutex::get() );
195 :
196 59 : ClientMap::iterator aClientPos;
197 59 : if (!implLookupClient(_nClient, aClientPos))
198 : // already asserted in implLookupClient
199 59 : return;
200 :
201 : // notify the listeners
202 59 : pListeners = aClientPos->second;
203 :
204 : // we do not need the entry in the clients map anymore
205 : // (do this before actually notifying, because some client
206 : // implementations have re-entrance problems and call into
207 : // revokeClient while we are notifying from here)
208 59 : Clients::get().erase(aClientPos);
209 59 : releaseId(_nClient);
210 : }
211 :
212 : // notify the "disposing" event for this client
213 59 : EventObject aDisposalEvent;
214 59 : aDisposalEvent.Source = _rxEventSource;
215 :
216 : // now really do the notification
217 59 : pListeners->disposeAndClear( aDisposalEvent );
218 59 : delete pListeners;
219 : }
220 :
221 :
222 211 : sal_Int32 AccessibleEventNotifier::addEventListener(
223 : const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener )
224 : {
225 211 : ::osl::MutexGuard aGuard( lclMutex::get() );
226 :
227 211 : ClientMap::iterator aClientPos;
228 211 : if ( !implLookupClient( _nClient, aClientPos ) )
229 : // already asserted in implLookupClient
230 0 : return 0;
231 :
232 211 : if ( _rxListener.is() )
233 211 : aClientPos->second->addInterface( _rxListener );
234 :
235 211 : return aClientPos->second->getLength();
236 : }
237 :
238 :
239 35 : sal_Int32 AccessibleEventNotifier::removeEventListener(
240 : const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener )
241 : {
242 35 : ::osl::MutexGuard aGuard( lclMutex::get() );
243 :
244 35 : ClientMap::iterator aClientPos;
245 35 : if ( !implLookupClient( _nClient, aClientPos ) )
246 : // already asserted in implLookupClient
247 2 : return 0;
248 :
249 33 : if ( _rxListener.is() )
250 33 : aClientPos->second->removeInterface( _rxListener );
251 :
252 33 : return aClientPos->second->getLength();
253 : }
254 :
255 :
256 184 : void AccessibleEventNotifier::addEvent( const TClientId _nClient, const AccessibleEventObject& _rEvent )
257 : {
258 184 : Sequence< Reference< XInterface > > aListeners;
259 :
260 : // --- <mutex lock> -------------------------------
261 : {
262 184 : ::osl::MutexGuard aGuard( lclMutex::get() );
263 :
264 184 : ClientMap::iterator aClientPos;
265 184 : if ( !implLookupClient( _nClient, aClientPos ) )
266 : // already asserted in implLookupClient
267 185 : return;
268 :
269 : // since we're synchronous, again, we want to notify immediately
270 183 : aListeners = aClientPos->second->getElements();
271 : }
272 : // --- </mutex lock> ------------------------------
273 :
274 : // default handling: loop through all listeners, and notify them
275 183 : const Reference< XInterface >* pListeners = aListeners.getConstArray();
276 183 : const Reference< XInterface >* pListenersEnd = pListeners + aListeners.getLength();
277 539 : while ( pListeners != pListenersEnd )
278 : {
279 : try
280 : {
281 173 : static_cast< XAccessibleEventListener* >( pListeners->get() )->notifyEvent( _rEvent );
282 : }
283 0 : catch( const Exception& )
284 : {
285 : // no assertion, because a broken access remote bridge or something like this
286 : // can cause this exception
287 : }
288 173 : ++pListeners;
289 183 : }
290 : }
291 :
292 :
293 : } // namespace comphelper
294 :
295 :
296 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|