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

Generated by: LCOV version 1.11