LCOV - code coverage report
Current view: top level - comphelper/source/misc - accessibleeventnotifier.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 85 0.0 %
Date: 2014-04-14 Functions: 0 10 0.0 %
Legend: Lines: hit not hit

          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           0 :             IntervalMap operator() () {
      58           0 :                 IntervalMap map;
      59             :                 map.insert(::std::make_pair(
      60           0 :                     ::std::numeric_limits<AccessibleEventNotifier::TClientId>::max(), 1));
      61           0 :                 return map;
      62             :             }
      63             :         };
      64             : 
      65           0 :     static void releaseId(AccessibleEventNotifier::TClientId const nId)
      66             :     {
      67           0 :         IntervalMap & rFreeIntervals(FreeIntervals::get());
      68           0 :         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           0 :         if (nId + 1 == upper->second)
      72             :         {
      73           0 :             --upper->second; // add nId to existing interval
      74             :         }
      75             :         else
      76             :         {
      77           0 :             IntervalMap::iterator const lower(rFreeIntervals.lower_bound(nId));
      78           0 :             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           0 :                 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           0 :     }
      92             : 
      93             :     /// generates a new client id
      94           0 :     static AccessibleEventNotifier::TClientId generateId()
      95             :     {
      96           0 :         IntervalMap & rFreeIntervals(FreeIntervals::get());
      97             :         assert(!rFreeIntervals.empty());
      98           0 :         IntervalMap::iterator const iter(rFreeIntervals.begin());
      99           0 :         AccessibleEventNotifier::TClientId const nFirst = iter->first;
     100           0 :         AccessibleEventNotifier::TClientId const nFreeId = iter->second;
     101             :         assert(nFreeId <= nFirst);
     102           0 :         if (nFreeId != nFirst)
     103             :         {
     104           0 :             ++iter->second; // remove nFreeId from interval
     105             :         }
     106             :         else
     107             :         {
     108           0 :             rFreeIntervals.erase(iter); // remove 1-element interval
     109             :         }
     110             : 
     111             :         assert(Clients::get().end() == Clients::get().find(nFreeId));
     112             : 
     113           0 :         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           0 :     static bool implLookupClient(
     132             :             const AccessibleEventNotifier::TClientId nClient,
     133             :             ClientMap::iterator& rPos )
     134             :     {
     135             :         // look up this client
     136           0 :         ClientMap &rClients = Clients::get();
     137           0 :         rPos = rClients.find( nClient );
     138             :         OSL_ENSURE( rClients.end() != rPos,
     139             :             "AccessibleEventNotifier::implLookupClient: invalid client id "
     140             :             "(did you register your client?)!" );
     141             : 
     142           0 :         return ( rClients.end() != rPos );
     143             :     }
     144             : }
     145             : 
     146             : 
     147             : namespace comphelper
     148             : {
     149             : 
     150             : 
     151             : 
     152           0 :     AccessibleEventNotifier::TClientId AccessibleEventNotifier::registerClient( )
     153             :     {
     154           0 :         ::osl::MutexGuard aGuard( lclMutex::get() );
     155             : 
     156             :         // generate a new client id
     157           0 :         TClientId nNewClientId = generateId( );
     158             : 
     159             :         // the event listeners for the new client
     160             :         ::cppu::OInterfaceContainerHelper *const pNewListeners =
     161           0 :             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           0 :         Clients::get().insert( ClientMap::value_type( nNewClientId, pNewListeners ) );
     169             : 
     170             :         // outta here
     171           0 :         return nNewClientId;
     172             :     }
     173             : 
     174             : 
     175           0 :     void AccessibleEventNotifier::revokeClient( const TClientId _nClient )
     176             :     {
     177           0 :         ::osl::MutexGuard aGuard( lclMutex::get() );
     178             : 
     179           0 :         ClientMap::iterator aClientPos;
     180           0 :         if ( !implLookupClient( _nClient, aClientPos ) )
     181             :             // already asserted in implLookupClient
     182           0 :             return;
     183             : 
     184             :         // remove it from the clients map
     185           0 :         delete aClientPos->second;
     186           0 :         Clients::get().erase( aClientPos );
     187           0 :         releaseId(_nClient);
     188             :     }
     189             : 
     190             : 
     191           0 :     void AccessibleEventNotifier::revokeClientNotifyDisposing( const TClientId _nClient,
     192             :             const Reference< XInterface >& _rxEventSource ) SAL_THROW( ( ) )
     193             :     {
     194           0 :         ::cppu::OInterfaceContainerHelper* pListeners(0);
     195             : 
     196             :         {
     197             :             // rhbz#1001768 drop the mutex before calling disposeAndClear
     198           0 :             ::osl::MutexGuard aGuard( lclMutex::get() );
     199             : 
     200           0 :             ClientMap::iterator aClientPos;
     201           0 :             if (!implLookupClient(_nClient, aClientPos))
     202             :                 // already asserted in implLookupClient
     203           0 :                 return;
     204             : 
     205             :             // notify the listeners
     206           0 :             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           0 :             Clients::get().erase(aClientPos);
     213           0 :             releaseId(_nClient);
     214             :         }
     215             : 
     216             :         // notify the "disposing" event for this client
     217           0 :         EventObject aDisposalEvent;
     218           0 :         aDisposalEvent.Source = _rxEventSource;
     219             : 
     220             :         // now really do the notification
     221           0 :         pListeners->disposeAndClear( aDisposalEvent );
     222           0 :         delete pListeners;
     223             :     }
     224             : 
     225             : 
     226           0 :     sal_Int32 AccessibleEventNotifier::addEventListener(
     227             :         const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener ) SAL_THROW( ( ) )
     228             :     {
     229           0 :         ::osl::MutexGuard aGuard( lclMutex::get() );
     230             : 
     231           0 :         ClientMap::iterator aClientPos;
     232           0 :         if ( !implLookupClient( _nClient, aClientPos ) )
     233             :             // already asserted in implLookupClient
     234           0 :             return 0;
     235             : 
     236           0 :         if ( _rxListener.is() )
     237           0 :             aClientPos->second->addInterface( _rxListener );
     238             : 
     239           0 :         return aClientPos->second->getLength();
     240             :     }
     241             : 
     242             : 
     243           0 :     sal_Int32 AccessibleEventNotifier::removeEventListener(
     244             :         const TClientId _nClient, const Reference< XAccessibleEventListener >& _rxListener ) SAL_THROW( ( ) )
     245             :     {
     246           0 :         ::osl::MutexGuard aGuard( lclMutex::get() );
     247             : 
     248           0 :         ClientMap::iterator aClientPos;
     249           0 :         if ( !implLookupClient( _nClient, aClientPos ) )
     250             :             // already asserted in implLookupClient
     251           0 :             return 0;
     252             : 
     253           0 :         if ( _rxListener.is() )
     254           0 :             aClientPos->second->removeInterface( _rxListener );
     255             : 
     256           0 :         return aClientPos->second->getLength();
     257             :     }
     258             : 
     259             : 
     260           0 :     void AccessibleEventNotifier::addEvent( const TClientId _nClient, const AccessibleEventObject& _rEvent ) SAL_THROW( ( ) )
     261             :     {
     262           0 :         Sequence< Reference< XInterface > > aListeners;
     263             : 
     264             :         // --- <mutex lock> -------------------------------
     265             :         {
     266           0 :             ::osl::MutexGuard aGuard( lclMutex::get() );
     267             : 
     268           0 :             ClientMap::iterator aClientPos;
     269           0 :             if ( !implLookupClient( _nClient, aClientPos ) )
     270             :                 // already asserted in implLookupClient
     271           0 :                 return;
     272             : 
     273             :             // since we're synchronous, again, we want to notify immediately
     274           0 :             aListeners = aClientPos->second->getElements();
     275             :         }
     276             :         // --- </mutex lock> ------------------------------
     277             : 
     278             :             // default handling: loop through all listeners, and notify them
     279           0 :         const Reference< XInterface >* pListeners = aListeners.getConstArray();
     280           0 :         const Reference< XInterface >* pListenersEnd = pListeners + aListeners.getLength();
     281           0 :         while ( pListeners != pListenersEnd )
     282             :         {
     283             :             try
     284             :             {
     285           0 :                 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           0 :             ++pListeners;
     293           0 :         }
     294             :     }
     295             : 
     296             : 
     297             : }   // namespace comphelper
     298             : 
     299             : 
     300             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10