build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / dbaccess / source / core / dataaccess / documenteventnotifier.cxx
blobaca027e159d9f2e354b48118f3ca007ce4d7b6d7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "documenteventnotifier.hxx"
22 #include <com/sun/star/frame/DoubleInitializationException.hpp>
24 #include <comphelper/asyncnotification.hxx>
25 #include <comphelper/interfacecontainer2.hxx>
26 #include <cppuhelper/weak.hxx>
27 #include <tools/diagnose_ex.h>
29 namespace dbaccess
32 using ::com::sun::star::uno::Reference;
33 using ::com::sun::star::uno::Exception;
34 using ::com::sun::star::uno::Any;
35 using ::com::sun::star::frame::DoubleInitializationException;
36 using ::com::sun::star::document::XDocumentEventListener;
37 using ::com::sun::star::document::DocumentEvent;
38 using ::com::sun::star::frame::XController2;
40 using namespace ::com::sun::star;
42 // DocumentEventHolder
43 typedef ::comphelper::EventHolder< DocumentEvent > DocumentEventHolder;
45 // DocumentEventNotifier_Impl
46 class DocumentEventNotifier_Impl : public ::comphelper::IEventProcessor
48 oslInterlockedCount m_refCount;
49 ::cppu::OWeakObject& m_rDocument;
50 ::osl::Mutex& m_rMutex;
51 bool m_bInitialized;
52 bool m_bDisposed;
53 ::std::shared_ptr<::comphelper::AsyncEventNotifierAutoJoin> m_pEventBroadcaster;
54 ::comphelper::OInterfaceContainerHelper2 m_aLegacyEventListeners;
55 ::comphelper::OInterfaceContainerHelper2 m_aDocumentEventListeners;
57 public:
58 DocumentEventNotifier_Impl( ::cppu::OWeakObject& _rBroadcasterDocument, ::osl::Mutex& _rMutex )
59 :m_refCount( 0 )
60 ,m_rDocument( _rBroadcasterDocument )
61 ,m_rMutex( _rMutex )
62 ,m_bInitialized( false )
63 ,m_bDisposed( false )
64 ,m_aLegacyEventListeners( _rMutex )
65 ,m_aDocumentEventListeners( _rMutex )
69 // IEventProcessor
70 virtual void SAL_CALL acquire() throw () override;
71 virtual void SAL_CALL release() throw () override;
73 void addLegacyEventListener( const Reference< document::XEventListener >& Listener )
75 m_aLegacyEventListeners.addInterface( Listener );
78 void removeLegacyEventListener( const Reference< document::XEventListener >& Listener )
80 m_aLegacyEventListeners.removeInterface( Listener );
83 void addDocumentEventListener( const Reference< XDocumentEventListener >& Listener )
85 m_aDocumentEventListeners.addInterface( Listener );
88 void removeDocumentEventListener( const Reference< XDocumentEventListener >& Listener )
90 m_aDocumentEventListeners.removeInterface( Listener );
93 void disposing();
95 void onDocumentInitialized();
97 void notifyDocumentEvent( const OUString& EventName, const Reference< XController2 >& ViewController,
98 const Any& Supplement )
100 impl_notifyEvent_nothrow( DocumentEvent(
101 m_rDocument, EventName, ViewController, Supplement ) );
104 void notifyDocumentEventAsync( const OUString& EventName, const Reference< XController2 >& ViewController,
105 const Any& Supplement )
107 impl_notifyEventAsync_nothrow( DocumentEvent(
108 m_rDocument, EventName, ViewController, Supplement ) );
111 protected:
112 virtual ~DocumentEventNotifier_Impl()
116 // IEventProcessor
117 virtual void processEvent( const ::comphelper::AnyEvent& _rEvent ) override;
119 private:
120 void impl_notifyEvent_nothrow( const DocumentEvent& _rEvent );
121 void impl_notifyEventAsync_nothrow( const DocumentEvent& _rEvent );
124 void SAL_CALL DocumentEventNotifier_Impl::acquire() throw ()
126 osl_atomic_increment( &m_refCount );
129 void SAL_CALL DocumentEventNotifier_Impl::release() throw ()
131 if ( 0 == osl_atomic_decrement( &m_refCount ) )
132 delete this;
135 void DocumentEventNotifier_Impl::disposing()
137 // SYNCHRONIZED ->
138 // cancel any pending asynchronous events
139 ::osl::ResettableMutexGuard aGuard( m_rMutex );
140 if (m_pEventBroadcaster)
142 m_pEventBroadcaster->removeEventsForProcessor( this );
143 m_pEventBroadcaster->terminate();
144 //TODO: a protocol is missing how to join with the thread before
145 // exit(3), to ensure the thread is no longer relying on any
146 // infrastructure while that infrastructure is being shut down
147 // in atexit handlers; simply calling join here leads to
148 // deadlock, as this thread holds the solar mutex while the
149 // other thread is typically blocked waiting for the solar mutex
150 // For now, use newAutoJoinAsyncEventNotifier which is
151 // better than nothing.
152 m_pEventBroadcaster.reset();
155 lang::EventObject aEvent( m_rDocument );
156 aGuard.clear();
157 // <-- SYNCHRONIZED
159 m_aLegacyEventListeners.disposeAndClear( aEvent );
160 m_aDocumentEventListeners.disposeAndClear( aEvent );
162 // SYNCHRONIZED ->
163 aGuard.reset();
164 m_bDisposed = true;
165 // <-- SYNCHRONIZED
168 void DocumentEventNotifier_Impl::onDocumentInitialized()
170 if ( m_bInitialized )
171 throw DoubleInitializationException();
173 m_bInitialized = true;
174 if (m_pEventBroadcaster)
176 // there are already pending asynchronous events
177 ::comphelper::AsyncEventNotifierAutoJoin::launch(m_pEventBroadcaster);
181 void DocumentEventNotifier_Impl::impl_notifyEvent_nothrow( const DocumentEvent& _rEvent )
183 OSL_PRECOND( m_bInitialized,
184 "DocumentEventNotifier_Impl::impl_notifyEvent_nothrow: only to be called when the document is already initialized!" );
187 document::EventObject aLegacyEvent( _rEvent.Source, _rEvent.EventName );
188 m_aLegacyEventListeners.notifyEach( &document::XEventListener::notifyEvent, aLegacyEvent );
190 catch(const Exception&)
192 DBG_UNHANDLED_EXCEPTION();
196 m_aDocumentEventListeners.notifyEach( &XDocumentEventListener::documentEventOccured, _rEvent );
198 catch( const Exception& )
200 DBG_UNHANDLED_EXCEPTION();
204 void DocumentEventNotifier_Impl::impl_notifyEventAsync_nothrow( const DocumentEvent& _rEvent )
206 if (!m_pEventBroadcaster)
208 m_pEventBroadcaster = ::comphelper::AsyncEventNotifierAutoJoin
209 ::newAsyncEventNotifierAutoJoin("DocumentEventNotifier");
210 if ( m_bInitialized )
212 // start processing the events if and only if we (our document, respectively) are
213 // already initialized
214 ::comphelper::AsyncEventNotifierAutoJoin::launch(m_pEventBroadcaster);
217 m_pEventBroadcaster->addEvent( new DocumentEventHolder( _rEvent ), this );
220 void DocumentEventNotifier_Impl::processEvent( const ::comphelper::AnyEvent& _rEvent )
222 // beware, this is called from the notification thread
224 ::osl::MutexGuard aGuard( m_rMutex );
225 if ( m_bDisposed )
226 return;
228 const DocumentEventHolder& rEventHolder = dynamic_cast< const DocumentEventHolder& >( _rEvent );
229 impl_notifyEvent_nothrow( rEventHolder.getEventObject() );
232 // DocumentEventNotifier
233 DocumentEventNotifier::DocumentEventNotifier( ::cppu::OWeakObject& _rBroadcasterDocument, ::osl::Mutex& _rMutex )
234 :m_pImpl( new DocumentEventNotifier_Impl( _rBroadcasterDocument, _rMutex ) )
238 DocumentEventNotifier::~DocumentEventNotifier()
242 void DocumentEventNotifier::disposing()
244 m_pImpl->disposing();
247 void DocumentEventNotifier::onDocumentInitialized()
249 m_pImpl->onDocumentInitialized();
252 void DocumentEventNotifier::addLegacyEventListener( const Reference< document::XEventListener >& Listener )
254 m_pImpl->addLegacyEventListener( Listener );
257 void DocumentEventNotifier::removeLegacyEventListener( const Reference< document::XEventListener >& Listener )
259 m_pImpl->removeLegacyEventListener( Listener );
262 void DocumentEventNotifier::addDocumentEventListener( const Reference< XDocumentEventListener >& Listener )
264 m_pImpl->addDocumentEventListener( Listener );
267 void DocumentEventNotifier::removeDocumentEventListener( const Reference< XDocumentEventListener >& Listener )
269 m_pImpl->removeDocumentEventListener( Listener );
272 void DocumentEventNotifier::notifyDocumentEvent( const OUString& EventName,
273 const Reference< XController2 >& ViewController, const Any& Supplement )
275 m_pImpl->notifyDocumentEvent( EventName, ViewController, Supplement );
278 void DocumentEventNotifier::notifyDocumentEventAsync( const OUString& EventName,
279 const Reference< XController2 >& ViewController, const Any& Supplement )
281 m_pImpl->notifyDocumentEventAsync( EventName, ViewController, Supplement );
284 } // namespace dbaccess
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */