LCOV - code coverage report
Current view: top level - dbaccess/source/core/dataaccess - documenteventnotifier.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 84 100 84.0 %
Date: 2014-11-03 Functions: 22 26 84.6 %
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         210 :         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         210 :             ,m_aDocumentEventListeners( _rMutex )
      74             :         {
      75         210 :         }
      76             : 
      77             :         // IEventProcessor
      78             :         virtual void SAL_CALL acquire() throw () SAL_OVERRIDE;
      79             :         virtual void SAL_CALL release() throw () SAL_OVERRIDE;
      80             : 
      81          22 :         void addLegacyEventListener( const Reference< document::XEventListener >& _Listener )
      82             :         {
      83          22 :             m_aLegacyEventListeners.addInterface( _Listener );
      84          22 :         }
      85             : 
      86           0 :         void removeLegacyEventListener( const Reference< document::XEventListener >& _Listener )
      87             :         {
      88           0 :             m_aLegacyEventListeners.removeInterface( _Listener );
      89           0 :         }
      90             : 
      91         424 :         void addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
      92             :         {
      93         424 :             m_aDocumentEventListeners.addInterface( _Listener );
      94         424 :         }
      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         642 :         void    notifyDocumentEvent( const OUString& _EventName, const Reference< XController2 >& _ViewController,
     106             :                     const Any& _Supplement )
     107             :         {
     108             :             impl_notifyEvent_nothrow( DocumentEvent(
     109         642 :                 m_rDocument, _EventName, _ViewController, _Supplement ) );
     110         642 :         }
     111             : 
     112         428 :         void    notifyDocumentEventAsync( const OUString& _EventName, const Reference< XController2 >& _ViewController,
     113             :                     const Any& _Supplement )
     114             :         {
     115             :             impl_notifyEventAsync_nothrow( DocumentEvent(
     116         428 :                 m_rDocument, _EventName, _ViewController, _Supplement ) );
     117         428 :         }
     118             : 
     119             :     protected:
     120         392 :         virtual ~DocumentEventNotifier_Impl()
     121         196 :         {
     122         392 :         }
     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        2114 :     void SAL_CALL DocumentEventNotifier_Impl::acquire() throw ()
     133             :     {
     134        2114 :         osl_atomic_increment( &m_refCount );
     135        2114 :     }
     136             : 
     137        2098 :     void SAL_CALL DocumentEventNotifier_Impl::release() throw ()
     138             :     {
     139        2098 :         if ( 0 == osl_atomic_decrement( &m_refCount ) )
     140         196 :             delete this;
     141        2098 :     }
     142             : 
     143         210 :     void DocumentEventNotifier_Impl::disposing()
     144             :     {
     145             :         // SYNCHRONIZED ->
     146             :         // cancel any pending asynchronous events
     147         210 :         ::osl::ResettableMutexGuard aGuard( m_rMutex );
     148         210 :         if ( m_pEventBroadcaster.is() )
     149             :         {
     150         192 :             m_pEventBroadcaster->removeEventsForProcessor( this );
     151         192 :             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         192 :             m_pEventBroadcaster.clear();
     159             :         }
     160             : 
     161         420 :         lang::EventObject aEvent( m_rDocument );
     162         210 :         aGuard.clear();
     163             :         // <-- SYNCHRONIZED
     164             : 
     165         210 :         m_aLegacyEventListeners.disposeAndClear( aEvent );
     166         210 :         m_aDocumentEventListeners.disposeAndClear( aEvent );
     167             : 
     168             :         // SYNCHRONIZED ->
     169         210 :         aGuard.reset();
     170         420 :         m_bDisposed = true;
     171             :         // <-- SYNCHRONIZED
     172         210 :     }
     173             : 
     174         210 :     void DocumentEventNotifier_Impl::onDocumentInitialized()
     175             :     {
     176         210 :         if ( m_bInitialized )
     177           0 :             throw DoubleInitializationException();
     178             : 
     179         210 :         m_bInitialized = true;
     180         210 :         if ( m_pEventBroadcaster.is() )
     181             :             // there are already pending asynchronous events
     182           0 :             m_pEventBroadcaster->launch();
     183         210 :     }
     184             : 
     185        1070 :     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        1070 :             document::EventObject aLegacyEvent( _rEvent.Source, _rEvent.EventName );
     192        1070 :             m_aLegacyEventListeners.notifyEach( &document::XEventListener::notifyEvent, aLegacyEvent );
     193             :         }
     194           0 :         catch(const Exception&)
     195             :         {
     196             :             DBG_UNHANDLED_EXCEPTION();
     197             :         }
     198             :         try
     199             :         {
     200        1070 :             m_aDocumentEventListeners.notifyEach( &XDocumentEventListener::documentEventOccured, _rEvent );
     201             :         }
     202           0 :         catch( const Exception& )
     203             :         {
     204             :             DBG_UNHANDLED_EXCEPTION();
     205             :         }
     206        1068 :     }
     207             : 
     208         428 :     void DocumentEventNotifier_Impl::impl_notifyEventAsync_nothrow( const DocumentEvent& _rEvent )
     209             :     {
     210         428 :         if ( !m_pEventBroadcaster.is() )
     211             :         {
     212             :             m_pEventBroadcaster.set(
     213         192 :                 new ::comphelper::AsyncEventNotifier("DocumentEventNotifier"));
     214         192 :             if ( m_bInitialized )
     215             :                 // start processing the events if and only if we (our document, respectively) are
     216             :                 // already initialized
     217         192 :                 m_pEventBroadcaster->launch();
     218             :         }
     219         428 :         m_pEventBroadcaster->addEvent( new DocumentEventHolder( _rEvent ), this );
     220         428 :     }
     221             : 
     222         428 :     void DocumentEventNotifier_Impl::processEvent( const ::comphelper::AnyEvent& _rEvent )
     223             :     {
     224             :         // beware, this is called from the notification thread
     225             :         {
     226         428 :             ::osl::MutexGuard aGuard( m_rMutex );
     227         428 :             if  ( m_bDisposed )
     228         426 :                 return;
     229             :         }
     230         428 :         const DocumentEventHolder& rEventHolder = dynamic_cast< const DocumentEventHolder& >( _rEvent );
     231         428 :         impl_notifyEvent_nothrow( rEventHolder.getEventObject() );
     232             :     }
     233             : 
     234             :     // DocumentEventNotifier
     235         210 :     DocumentEventNotifier::DocumentEventNotifier( ::cppu::OWeakObject& _rBroadcasterDocument, ::osl::Mutex& _rMutex )
     236         210 :         :m_pImpl( new DocumentEventNotifier_Impl( _rBroadcasterDocument, _rMutex ) )
     237             :     {
     238         210 :     }
     239             : 
     240         196 :     DocumentEventNotifier::~DocumentEventNotifier()
     241             :     {
     242         196 :     }
     243             : 
     244         210 :     void DocumentEventNotifier::disposing()
     245             :     {
     246         210 :         m_pImpl->disposing();
     247         210 :     }
     248             : 
     249         210 :     void DocumentEventNotifier::onDocumentInitialized()
     250             :     {
     251         210 :         m_pImpl->onDocumentInitialized();
     252         210 :     }
     253             : 
     254          22 :     void DocumentEventNotifier::addLegacyEventListener( const Reference< document::XEventListener >& _Listener )
     255             :     {
     256          22 :         m_pImpl->addLegacyEventListener( _Listener );
     257          22 :     }
     258             : 
     259           0 :     void DocumentEventNotifier::removeLegacyEventListener( const Reference< document::XEventListener >& _Listener )
     260             :     {
     261           0 :         m_pImpl->removeLegacyEventListener( _Listener );
     262           0 :     }
     263             : 
     264         424 :     void DocumentEventNotifier::addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
     265             :     {
     266         424 :         m_pImpl->addDocumentEventListener( _Listener );
     267         424 :     }
     268             : 
     269           0 :     void DocumentEventNotifier::removeDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
     270             :     {
     271           0 :         m_pImpl->removeDocumentEventListener( _Listener );
     272           0 :     }
     273             : 
     274         642 :     void DocumentEventNotifier::notifyDocumentEvent( const OUString& _EventName,
     275             :         const Reference< XController2 >& _ViewController, const Any& _Supplement )
     276             :     {
     277         642 :         m_pImpl->notifyDocumentEvent( _EventName, _ViewController, _Supplement );
     278         642 :     }
     279             : 
     280         428 :     void DocumentEventNotifier::notifyDocumentEventAsync( const OUString& _EventName,
     281             :         const Reference< XController2 >& _ViewController, const Any& _Supplement )
     282             :     {
     283         428 :         m_pImpl->notifyDocumentEventAsync( _EventName, _ViewController, _Supplement );
     284         428 :     }
     285             : 
     286             : } // namespace dbaccess
     287             : 
     288             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10