LCOV - code coverage report
Current view: top level - dbaccess/source/core/dataaccess - documenteventnotifier.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 84 100 84.0 %
Date: 2012-08-25 Functions: 22 26 84.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 52 104 50.0 %

           Branch data     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                 :            : 
      21                 :            : #include "documenteventnotifier.hxx"
      22                 :            : 
      23                 :            : #include <com/sun/star/frame/DoubleInitializationException.hpp>
      24                 :            : 
      25                 :            : #include <comphelper/asyncnotification.hxx>
      26                 :            : #include <cppuhelper/interfacecontainer.hxx>
      27                 :            : #include <cppuhelper/weak.hxx>
      28                 :            : #include <tools/diagnose_ex.h>
      29                 :            : 
      30                 :            : namespace dbaccess
      31                 :            : {
      32                 :            : 
      33                 :            :     /** === begin UNO using === **/
      34                 :            :     using ::com::sun::star::uno::Reference;
      35                 :            :     using ::com::sun::star::uno::XInterface;
      36                 :            :     using ::com::sun::star::uno::UNO_QUERY;
      37                 :            :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      38                 :            :     using ::com::sun::star::uno::UNO_SET_THROW;
      39                 :            :     using ::com::sun::star::uno::Exception;
      40                 :            :     using ::com::sun::star::uno::RuntimeException;
      41                 :            :     using ::com::sun::star::uno::Any;
      42                 :            :     using ::com::sun::star::uno::makeAny;
      43                 :            :     using ::com::sun::star::uno::Sequence;
      44                 :            :     using ::com::sun::star::uno::Type;
      45                 :            :     using ::com::sun::star::frame::DoubleInitializationException;
      46                 :            :     using ::com::sun::star::document::XDocumentEventListener;
      47                 :            :     using ::com::sun::star::document::DocumentEvent;
      48                 :            :     using ::com::sun::star::frame::XController2;
      49                 :            :     /** === end UNO using === **/
      50                 :            :     using namespace ::com::sun::star;
      51                 :            : 
      52                 :            :     //==================================================================
      53                 :            :     //= DocumentEventHolder
      54                 :            :     //==================================================================
      55                 :            :     typedef ::comphelper::EventHolder< DocumentEvent >  DocumentEventHolder;
      56                 :            : 
      57                 :            :     //====================================================================
      58                 :            :     //= DocumentEventNotifier_Impl
      59                 :            :     //====================================================================
      60                 :            :     class DocumentEventNotifier_Impl : public ::comphelper::IEventProcessor
      61                 :            :     {
      62                 :            :         oslInterlockedCount                                     m_refCount;
      63                 :            :         ::cppu::OWeakObject&                                    m_rDocument;
      64                 :            :         ::osl::Mutex&                                           m_rMutex;
      65                 :            :         bool                                                    m_bInitialized;
      66                 :            :         bool                                                    m_bDisposed;
      67                 :            :         ::rtl::Reference< ::comphelper::AsyncEventNotifier >    m_pEventBroadcaster;
      68                 :            :         ::cppu::OInterfaceContainerHelper                       m_aLegacyEventListeners;
      69                 :            :         ::cppu::OInterfaceContainerHelper                       m_aDocumentEventListeners;
      70                 :            : 
      71                 :            :     public:
      72                 :        186 :         DocumentEventNotifier_Impl( ::cppu::OWeakObject& _rBroadcasterDocument, ::osl::Mutex& _rMutex )
      73                 :            :             :m_refCount( 0 )
      74                 :            :             ,m_rDocument( _rBroadcasterDocument )
      75                 :            :             ,m_rMutex( _rMutex )
      76                 :            :             ,m_bInitialized( false )
      77                 :            :             ,m_bDisposed( false )
      78                 :            :             ,m_aLegacyEventListeners( _rMutex )
      79 [ +  - ][ +  - ]:        186 :             ,m_aDocumentEventListeners( _rMutex )
      80                 :            :         {
      81                 :        186 :         }
      82                 :            : 
      83                 :            :         // IReference
      84                 :            :         virtual void SAL_CALL acquire();
      85                 :            :         virtual void SAL_CALL release();
      86                 :            : 
      87                 :          2 :         void addLegacyEventListener( const Reference< document::XEventListener >& _Listener )
      88                 :            :         {
      89                 :          2 :             m_aLegacyEventListeners.addInterface( _Listener );
      90                 :          2 :         }
      91                 :            : 
      92                 :          0 :         void removeLegacyEventListener( const Reference< document::XEventListener >& _Listener )
      93                 :            :         {
      94                 :          0 :             m_aLegacyEventListeners.removeInterface( _Listener );
      95                 :          0 :         }
      96                 :            : 
      97                 :        372 :         void addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
      98                 :            :         {
      99                 :        372 :             m_aDocumentEventListeners.addInterface( _Listener );
     100                 :        372 :         }
     101                 :            : 
     102                 :          0 :         void removeDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
     103                 :            :         {
     104                 :          0 :             m_aDocumentEventListeners.removeInterface( _Listener );
     105                 :          0 :         }
     106                 :            : 
     107                 :            :         void disposing();
     108                 :            : 
     109                 :            :         void onDocumentInitialized();
     110                 :            : 
     111                 :        560 :         void    notifyDocumentEvent( const ::rtl::OUString& _EventName, const Reference< XController2 >& _ViewController,
     112                 :            :                     const Any& _Supplement )
     113                 :            :         {
     114                 :            :             impl_notifyEvent_nothrow( DocumentEvent(
     115 [ +  - ][ +  - ]:        560 :                 m_rDocument, _EventName, _ViewController, _Supplement ) );
                 [ +  - ]
     116                 :        560 :         }
     117                 :            : 
     118                 :        376 :         void    notifyDocumentEventAsync( const ::rtl::OUString& _EventName, const Reference< XController2 >& _ViewController,
     119                 :            :                     const Any& _Supplement )
     120                 :            :         {
     121                 :            :             impl_notifyEventAsync_nothrow( DocumentEvent(
     122 [ +  - ][ +  - ]:        376 :                 m_rDocument, _EventName, _ViewController, _Supplement ) );
                 [ +  - ]
     123                 :        376 :         }
     124                 :            : 
     125                 :            :     protected:
     126                 :        372 :         virtual ~DocumentEventNotifier_Impl()
     127 [ +  - ][ +  - ]:        186 :         {
                 [ +  - ]
     128         [ -  + ]:        372 :         }
     129                 :            : 
     130                 :            :         // IEventProcessor
     131                 :            :         virtual void processEvent( const ::comphelper::AnyEvent& _rEvent );
     132                 :            : 
     133                 :            :     private:
     134                 :            :         void impl_notifyEvent_nothrow( const DocumentEvent& _rEvent );
     135                 :            :         void impl_notifyEventAsync_nothrow( const DocumentEvent& _rEvent );
     136                 :            :     };
     137                 :            : 
     138                 :       2410 :     void SAL_CALL DocumentEventNotifier_Impl::acquire()
     139                 :            :     {
     140                 :       2410 :         osl_incrementInterlockedCount( &m_refCount );
     141                 :       2410 :     }
     142                 :            : 
     143                 :       2410 :     void SAL_CALL DocumentEventNotifier_Impl::release()
     144                 :            :     {
     145         [ +  + ]:       2410 :         if ( 0 == osl_decrementInterlockedCount( &m_refCount ) )
     146         [ +  - ]:        186 :             delete this;
     147                 :       2410 :     }
     148                 :            : 
     149                 :        186 :     void DocumentEventNotifier_Impl::disposing()
     150                 :            :     {
     151                 :            :         // SYNCHRONIZED ->
     152                 :            :         // cancel any pending asynchronous events
     153         [ +  - ]:        186 :         ::osl::ResettableMutexGuard aGuard( m_rMutex );
     154         [ +  + ]:        186 :         if ( m_pEventBroadcaster.is() )
     155                 :            :         {
     156 [ +  - ][ +  - ]:        172 :             m_pEventBroadcaster->removeEventsForProcessor( this );
                 [ +  - ]
     157         [ +  - ]:        172 :             m_pEventBroadcaster->terminate();
     158                 :            :                 //TODO: a protocol is missing how to join with the thread before
     159                 :            :                 // exit(3), to ensure the thread is no longer relying on any
     160                 :            :                 // infrastructure while that infrastructure is being shut down
     161                 :            :                 // in atexit handlers; simply calling join here leads to
     162                 :            :                 // deadlock, as this thread holds the solar mutex while the
     163                 :            :                 // other thread is typically blocked waiting for the solar mutex
     164         [ +  - ]:        172 :             m_pEventBroadcaster.clear();
     165                 :            :         }
     166                 :            : 
     167 [ +  - ][ +  - ]:        186 :         lang::EventObject aEvent( m_rDocument );
     168         [ +  - ]:        186 :         aGuard.clear();
     169                 :            :         // <-- SYNCHRONIZED
     170                 :            : 
     171         [ +  - ]:        186 :         m_aLegacyEventListeners.disposeAndClear( aEvent );
     172         [ +  - ]:        186 :         m_aDocumentEventListeners.disposeAndClear( aEvent );
     173                 :            : 
     174                 :            :         // SYNCHRONIZED ->
     175         [ +  - ]:        186 :         aGuard.reset();
     176 [ +  - ][ +  - ]:        186 :         m_bDisposed = true;
     177                 :            :         // <-- SYNCHRONIZED
     178                 :        186 :     }
     179                 :            : 
     180                 :        186 :     void DocumentEventNotifier_Impl::onDocumentInitialized()
     181                 :            :     {
     182         [ -  + ]:        186 :         if ( m_bInitialized )
     183         [ #  # ]:          0 :             throw DoubleInitializationException();
     184                 :            : 
     185                 :        186 :         m_bInitialized = true;
     186         [ -  + ]:        186 :         if ( m_pEventBroadcaster.is() )
     187                 :            :             // there are already pending asynchronous events
     188                 :          0 :             m_pEventBroadcaster->launch();
     189                 :        186 :     }
     190                 :            : 
     191                 :        936 :     void DocumentEventNotifier_Impl::impl_notifyEvent_nothrow( const DocumentEvent& _rEvent )
     192                 :            :     {
     193                 :            :         OSL_PRECOND( m_bInitialized,
     194                 :            :             "DocumentEventNotifier_Impl::impl_notifyEvent_nothrow: only to be called when the document is already initialized!" );
     195                 :            :         try
     196                 :            :         {
     197         [ +  - ]:        936 :             document::EventObject aLegacyEvent( _rEvent.Source, _rEvent.EventName );
     198 [ +  - ][ +  - ]:        936 :             m_aLegacyEventListeners.notifyEach( &document::XEventListener::notifyEvent, aLegacyEvent );
                 [ #  # ]
     199                 :            :         }
     200         [ #  # ]:          0 :         catch(const Exception&)
     201                 :            :         {
     202                 :            :             DBG_UNHANDLED_EXCEPTION();
     203                 :            :         }
     204                 :            :         try
     205                 :            :         {
     206         [ +  - ]:        936 :             m_aDocumentEventListeners.notifyEach( &XDocumentEventListener::documentEventOccured, _rEvent );
     207                 :            :         }
     208                 :          0 :         catch( const Exception& )
     209                 :            :         {
     210                 :            :             DBG_UNHANDLED_EXCEPTION();
     211                 :            :         }
     212                 :        936 :     }
     213                 :            : 
     214                 :        376 :     void DocumentEventNotifier_Impl::impl_notifyEventAsync_nothrow( const DocumentEvent& _rEvent )
     215                 :            :     {
     216         [ +  + ]:        376 :         if ( !m_pEventBroadcaster.is() )
     217                 :            :         {
     218                 :            :             m_pEventBroadcaster.set(
     219         [ +  - ]:        172 :                 new ::comphelper::AsyncEventNotifier("DocumentEventNotifier"));
     220         [ +  - ]:        172 :             if ( m_bInitialized )
     221                 :            :                 // start processing the events if and only if we (our document, respectively) are
     222                 :            :                 // already initialized
     223                 :        172 :                 m_pEventBroadcaster->launch();
     224                 :            :         }
     225 [ +  - ][ +  - ]:        376 :         m_pEventBroadcaster->addEvent( new DocumentEventHolder( _rEvent ), this );
         [ +  - ][ +  - ]
                 [ +  - ]
     226                 :        376 :     }
     227                 :            : 
     228                 :        376 :     void DocumentEventNotifier_Impl::processEvent( const ::comphelper::AnyEvent& _rEvent )
     229                 :            :     {
     230                 :            :         // beware, this is called from the notification thread
     231                 :            :         {
     232         [ +  - ]:        376 :             ::osl::MutexGuard aGuard( m_rMutex );
     233         [ -  + ]:        376 :             if  ( m_bDisposed )
     234 [ +  - ][ +  - ]:        752 :                 return;
     235                 :            :         }
     236         [ +  - ]:        376 :         const DocumentEventHolder& rEventHolder = dynamic_cast< const DocumentEventHolder& >( _rEvent );
     237                 :        376 :         impl_notifyEvent_nothrow( rEventHolder.getEventObject() );
     238                 :            :     }
     239                 :            : 
     240                 :            :     //====================================================================
     241                 :            :     //= DocumentEventNotifier
     242                 :            :     //====================================================================
     243                 :        186 :     DocumentEventNotifier::DocumentEventNotifier( ::cppu::OWeakObject& _rBroadcasterDocument, ::osl::Mutex& _rMutex )
     244         [ +  - ]:        186 :         :m_pImpl( new DocumentEventNotifier_Impl( _rBroadcasterDocument, _rMutex ) )
     245                 :            :     {
     246                 :        186 :     }
     247                 :            : 
     248                 :        186 :     DocumentEventNotifier::~DocumentEventNotifier()
     249                 :            :     {
     250                 :        186 :     }
     251                 :            : 
     252                 :        186 :     void DocumentEventNotifier::disposing()
     253                 :            :     {
     254                 :        186 :         m_pImpl->disposing();
     255                 :        186 :     }
     256                 :            : 
     257                 :        186 :     void DocumentEventNotifier::onDocumentInitialized()
     258                 :            :     {
     259                 :        186 :         m_pImpl->onDocumentInitialized();
     260                 :        186 :     }
     261                 :            : 
     262                 :          2 :     void DocumentEventNotifier::addLegacyEventListener( const Reference< document::XEventListener >& _Listener )
     263                 :            :     {
     264                 :          2 :         m_pImpl->addLegacyEventListener( _Listener );
     265                 :          2 :     }
     266                 :            : 
     267                 :          0 :     void DocumentEventNotifier::removeLegacyEventListener( const Reference< document::XEventListener >& _Listener )
     268                 :            :     {
     269                 :          0 :         m_pImpl->removeLegacyEventListener( _Listener );
     270                 :          0 :     }
     271                 :            : 
     272                 :        372 :     void DocumentEventNotifier::addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
     273                 :            :     {
     274                 :        372 :         m_pImpl->addDocumentEventListener( _Listener );
     275                 :        372 :     }
     276                 :            : 
     277                 :          0 :     void DocumentEventNotifier::removeDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
     278                 :            :     {
     279                 :          0 :         m_pImpl->removeDocumentEventListener( _Listener );
     280                 :          0 :     }
     281                 :            : 
     282                 :        560 :     void DocumentEventNotifier::notifyDocumentEvent( const ::rtl::OUString& _EventName,
     283                 :            :         const Reference< XController2 >& _ViewController, const Any& _Supplement )
     284                 :            :     {
     285                 :        560 :         m_pImpl->notifyDocumentEvent( _EventName, _ViewController, _Supplement );
     286                 :        560 :     }
     287                 :            : 
     288                 :        376 :     void DocumentEventNotifier::notifyDocumentEventAsync( const ::rtl::OUString& _EventName,
     289                 :            :         const Reference< XController2 >& _ViewController, const Any& _Supplement )
     290                 :            :     {
     291                 :        376 :         m_pImpl->notifyDocumentEventAsync( _EventName, _ViewController, _Supplement );
     292                 :        376 :     }
     293                 :            : 
     294                 :            : } // namespace dbaccess
     295                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10