LibreOffice Module comphelper (master) 1
accessibleeventnotifier.cxx
Go to the documentation of this file.
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
21#include <com/sun/star/accessibility/XAccessibleEventListener.hpp>
23
24#include <limits>
25#include <map>
26#include <memory>
27#include <unordered_map>
28
29using namespace ::com::sun::star::uno;
30using namespace ::com::sun::star::lang;
31using namespace ::com::sun::star::accessibility;
32using namespace ::comphelper;
33
34namespace {
35
36typedef std::pair< AccessibleEventNotifier::TClientId,
37 AccessibleEventObject > ClientEvent;
38
39typedef ::comphelper::OInterfaceContainerHelper4<XAccessibleEventListener> ListenerContainer;
40typedef std::unordered_map< AccessibleEventNotifier::TClientId, ListenerContainer > ClientMap;
41
43typedef std::map<AccessibleEventNotifier::TClientId,
44 AccessibleEventNotifier::TClientId> IntervalMap;
45
46std::mutex& GetLocalMutex()
47{
48 static std::mutex MUTEX;
49 return MUTEX;
50}
51
52ClientMap gaClients;
53
54IntervalMap& GetFreeIntervals()
55{
56 static IntervalMap MAP =
57 []()
58 {
59 IntervalMap map;
60 map.insert(std::make_pair(
61 std::numeric_limits<AccessibleEventNotifier::TClientId>::max(), 1));
62 return map;
63 }();
64 return MAP;
65}
66
67void releaseId(AccessibleEventNotifier::TClientId const nId)
68{
69 IntervalMap & rFreeIntervals(GetFreeIntervals());
70 IntervalMap::iterator const upper(rFreeIntervals.upper_bound(nId));
71 assert(upper != rFreeIntervals.end());
72 assert(nId < upper->second); // second is start of the interval!
73 if (nId + 1 == upper->second)
74 {
75 --upper->second; // add nId to existing interval
76 }
77 else
78 {
79 IntervalMap::iterator const lower(rFreeIntervals.lower_bound(nId));
80 if (lower != rFreeIntervals.end() && lower->first == nId - 1)
81 {
82 // add nId by replacing lower with new merged entry
83 rFreeIntervals.insert(std::make_pair(nId, lower->second));
84 rFreeIntervals.erase(lower);
85 }
86 else // otherwise just add new 1-element interval
87 {
88 rFreeIntervals.insert(std::make_pair(nId, nId));
89 }
90 }
91 // currently it's not checked whether intervals can be merged now
92 // hopefully that won't be a problem in practice
93}
94
96AccessibleEventNotifier::TClientId generateId()
97{
98 IntervalMap & rFreeIntervals(GetFreeIntervals());
99 assert(!rFreeIntervals.empty());
100 IntervalMap::iterator const iter(rFreeIntervals.begin());
101 AccessibleEventNotifier::TClientId const nFirst = iter->first;
102 AccessibleEventNotifier::TClientId const nFreeId = iter->second;
103 assert(nFreeId <= nFirst);
104 if (nFreeId != nFirst)
105 {
106 ++iter->second; // remove nFreeId from interval
107 }
108 else
109 {
110 rFreeIntervals.erase(iter); // remove 1-element interval
111 }
112
113 assert(gaClients.end() == gaClients.find(nFreeId));
114
115 return nFreeId;
116}
117
133bool implLookupClient(
134 const AccessibleEventNotifier::TClientId nClient,
135 ClientMap::iterator& rPos )
136{
137 // look up this client
138 ClientMap &rClients = gaClients;
139 rPos = rClients.find( nClient );
140 assert( rClients.end() != rPos &&
141 "AccessibleEventNotifier::implLookupClient: invalid client id "
142 "(did you register your client?)!" );
143
144 return ( rClients.end() != rPos );
145}
146
147} // anonymous namespace
148
149namespace comphelper {
150
152{
153 std::scoped_lock aGuard( GetLocalMutex() );
154
155 // generate a new client id
156 TClientId nNewClientId = generateId( );
157
158 // add the client
159 gaClients.emplace( nNewClientId, ListenerContainer{} );
160
161 // outta here
162 return nNewClientId;
163}
164
166{
167 std::scoped_lock aGuard( GetLocalMutex() );
168
169 ClientMap::iterator aClientPos;
170 if ( !implLookupClient( _nClient, aClientPos ) )
171 // already asserted in implLookupClient
172 return;
173
174 // remove it from the clients map
175 gaClients.erase( aClientPos );
176 releaseId(_nClient);
177}
178
180 const TClientId _nClient, const Reference< XInterface >& _rxEventSource )
181{
182 std::unique_lock aGuard( GetLocalMutex() );
183
184 ClientMap::iterator aClientPos;
185 if (!implLookupClient(_nClient, aClientPos))
186 // already asserted in implLookupClient
187 return;
188
189 // notify the listeners
190 ListenerContainer aListeners(std::move(aClientPos->second));
191
192 // we do not need the entry in the clients map anymore
193 // (do this before actually notifying, because some client
194 // implementations have re-entrance problems and call into
195 // revokeClient while we are notifying from here)
196 gaClients.erase(aClientPos);
197 releaseId(_nClient);
198
199 // notify the "disposing" event for this client
200 EventObject aDisposalEvent;
201 aDisposalEvent.Source = _rxEventSource;
202
203 // now really do the notification
204 aListeners.disposeAndClear( aGuard, aDisposalEvent );
205}
206
208 const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener )
209{
210 std::unique_lock aGuard( GetLocalMutex() );
211
212 ClientMap::iterator aClientPos;
213 if ( !implLookupClient( _nClient, aClientPos ) )
214 // already asserted in implLookupClient
215 return 0;
216
217 if ( _rxListener.is() )
218 aClientPos->second.addInterface( aGuard, _rxListener );
219
220 return aClientPos->second.getLength(aGuard);
221}
222
224 const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener )
225{
226 std::unique_lock aGuard( GetLocalMutex() );
227
228 ClientMap::iterator aClientPos;
229 if ( !implLookupClient( _nClient, aClientPos ) )
230 // already asserted in implLookupClient
231 return 0;
232
233 if ( _rxListener.is() )
234 aClientPos->second.removeInterface( aGuard, _rxListener );
235
236 return aClientPos->second.getLength(aGuard);
237}
238
239void AccessibleEventNotifier::addEvent( const TClientId _nClient, const AccessibleEventObject& _rEvent )
240{
241 std::unique_lock aGuard( GetLocalMutex() );
242
243 ClientMap::iterator aClientPos;
244 if ( !implLookupClient( _nClient, aClientPos ) )
245 // already asserted in implLookupClient
246 return;
247
248 // since we're synchronous, again, we want to notify immediately
249 OInterfaceIteratorHelper4 aIt(aGuard, aClientPos->second);
250 // no need to hold lock here, and we don't want to hold lock while calling listeners
251 aGuard.unlock();
252 while (aIt.hasMoreElements())
253 {
254 try
255 {
256 aIt.next()->notifyEvent(_rEvent);
257 }
258 catch (Exception&)
259 {
260 // no assertion, because a broken access remote bridge or something like this
261 // can cause this exception
262 }
263 }
264}
265
267{
268 gaClients.clear();
269}
270
271} // namespace comphelper
272
273/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static sal_Int32 addEventListener(const TClientId _nClient, const css::uno::Reference< css::accessibility::XAccessibleEventListener > &_rxListener)
registers a listener for the given client
static void addEvent(const TClientId _nClient, const css::accessibility::AccessibleEventObject &_rEvent)
adds an event, which is to be broadcasted, to the queue
static sal_Int32 removeEventListener(const TClientId _nClient, const css::uno::Reference< css::accessibility::XAccessibleEventListener > &_rxListener)
revokes a listener for the given client
static TClientId registerClient()
registers a client of this class, means a broadcaster of AccessibleEvents
static void revokeClient(const TClientId _nClient)
revokes a broadcaster of AccessibleEvents
static void revokeClientNotifyDisposing(const TClientId _nClient, const css::uno::Reference< css::uno::XInterface > &_rxEventSource)
revokes a client, with additionally notifying a disposing event to all listeners registered for this ...
This is the iterator of an OInterfaceContainerHelper4.
bool hasMoreElements() const
Return true, if there are more elements in the iterator.
css::uno::Reference< ListenerT > const & next()
Return the next element of the iterator.
Listeners aListeners
@ Exception
std::mutex mutex
Definition: random.cxx:41
std::map< OUString, rtl::Reference< Entity > > map
#define MAP(name, prefix, token, type, context)