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