nss: upgrade to release 3.73
[LibreOffice.git] / dbaccess / source / core / dataaccess / documenteventnotifier.cxx
blob13295523ef5f45ead0c18070390dab02ee17dd08
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>
28 #include <vcl/svapp.hxx>
30 namespace dbaccess
33 using ::com::sun::star::uno::Reference;
34 using ::com::sun::star::uno::Exception;
35 using ::com::sun::star::uno::Any;
36 using ::com::sun::star::frame::DoubleInitializationException;
37 using ::com::sun::star::document::XDocumentEventListener;
38 using ::com::sun::star::document::DocumentEvent;
39 using ::com::sun::star::frame::XController2;
41 using namespace ::com::sun::star;
43 // DocumentEventHolder
44 typedef ::comphelper::EventHolder< DocumentEvent > DocumentEventHolder;
46 // DocumentEventNotifier_Impl
47 class DocumentEventNotifier_Impl : public ::comphelper::IEventProcessor
49 oslInterlockedCount m_refCount;
50 ::cppu::OWeakObject& m_rDocument;
51 ::osl::Mutex& m_rMutex;
52 bool m_bInitialized;
53 bool m_bDisposed;
54 std::shared_ptr<::comphelper::AsyncEventNotifierAutoJoin> m_pEventBroadcaster;
55 ::comphelper::OInterfaceContainerHelper2 m_aLegacyEventListeners;
56 ::comphelper::OInterfaceContainerHelper2 m_aDocumentEventListeners;
58 public:
59 DocumentEventNotifier_Impl( ::cppu::OWeakObject& _rBroadcasterDocument, ::osl::Mutex& _rMutex )
60 :m_refCount( 0 )
61 ,m_rDocument( _rBroadcasterDocument )
62 ,m_rMutex( _rMutex )
63 ,m_bInitialized( false )
64 ,m_bDisposed( false )
65 ,m_aLegacyEventListeners( _rMutex )
66 ,m_aDocumentEventListeners( _rMutex )
70 // IEventProcessor
71 virtual void SAL_CALL acquire() throw () override;
72 virtual void SAL_CALL release() throw () override;
74 void addLegacyEventListener( const Reference< document::XEventListener >& Listener )
76 m_aLegacyEventListeners.addInterface( Listener );
79 void removeLegacyEventListener( const Reference< document::XEventListener >& Listener )
81 m_aLegacyEventListeners.removeInterface( Listener );
84 void addDocumentEventListener( const Reference< XDocumentEventListener >& Listener )
86 m_aDocumentEventListeners.addInterface( Listener );
89 void removeDocumentEventListener( const Reference< XDocumentEventListener >& Listener )
91 m_aDocumentEventListeners.removeInterface( Listener );
94 void disposing();
96 void onDocumentInitialized();
98 void notifyDocumentEvent( const OUString& EventName, const Reference< XController2 >& ViewController,
99 const Any& Supplement )
101 impl_notifyEvent_nothrow( DocumentEvent(
102 m_rDocument, EventName, ViewController, Supplement ) );
105 void notifyDocumentEventAsync( const OUString& EventName, const Reference< XController2 >& ViewController,
106 const Any& Supplement )
108 impl_notifyEventAsync_nothrow( DocumentEvent(
109 m_rDocument, EventName, ViewController, Supplement ) );
112 protected:
113 virtual ~DocumentEventNotifier_Impl()
117 // IEventProcessor
118 virtual void processEvent( const ::comphelper::AnyEvent& _rEvent ) override;
120 private:
121 void impl_notifyEvent_nothrow( const DocumentEvent& _rEvent );
122 void impl_notifyEventAsync_nothrow( const DocumentEvent& _rEvent );
125 void SAL_CALL DocumentEventNotifier_Impl::acquire() throw ()
127 osl_atomic_increment( &m_refCount );
130 void SAL_CALL DocumentEventNotifier_Impl::release() throw ()
132 if ( 0 == osl_atomic_decrement( &m_refCount ) )
133 delete this;
136 void DocumentEventNotifier_Impl::disposing()
138 // SYNCHRONIZED ->
139 // cancel any pending asynchronous events
140 ::osl::ResettableMutexGuard aGuard( m_rMutex );
141 if (m_pEventBroadcaster)
143 m_pEventBroadcaster->removeEventsForProcessor( this );
144 m_pEventBroadcaster->terminate();
147 auto xEventBroadcaster = std::exchange(m_pEventBroadcaster, {});
149 lang::EventObject aEvent( m_rDocument );
150 aGuard.clear();
151 // <-- SYNCHRONIZED
153 if (xEventBroadcaster)
155 comphelper::SolarMutex& rSolarMutex = Application::GetSolarMutex();
156 // unblock threads blocked on that so we can join
157 sal_uInt32 nLockCount = (rSolarMutex.IsCurrentThread()) ? rSolarMutex.release(true) : 0;
158 xEventBroadcaster->join();
159 if (nLockCount)
160 rSolarMutex.acquire(nLockCount);
161 xEventBroadcaster.reset();
163 m_aLegacyEventListeners.disposeAndClear( aEvent );
164 m_aDocumentEventListeners.disposeAndClear( aEvent );
166 // SYNCHRONIZED ->
167 aGuard.reset();
168 m_bDisposed = true;
169 // <-- SYNCHRONIZED
172 void DocumentEventNotifier_Impl::onDocumentInitialized()
174 if ( m_bInitialized )
175 throw DoubleInitializationException();
177 m_bInitialized = true;
178 if (m_pEventBroadcaster)
180 // there are already pending asynchronous events
181 ::comphelper::AsyncEventNotifierAutoJoin::launch(m_pEventBroadcaster);
185 void DocumentEventNotifier_Impl::impl_notifyEvent_nothrow( const DocumentEvent& _rEvent )
187 OSL_PRECOND( m_bInitialized,
188 "DocumentEventNotifier_Impl::impl_notifyEvent_nothrow: only to be called when the document is already initialized!" );
191 document::EventObject aLegacyEvent( _rEvent.Source, _rEvent.EventName );
192 m_aLegacyEventListeners.notifyEach( &document::XEventListener::notifyEvent, aLegacyEvent );
194 catch(const Exception&)
196 DBG_UNHANDLED_EXCEPTION("dbaccess");
200 m_aDocumentEventListeners.notifyEach( &XDocumentEventListener::documentEventOccured, _rEvent );
202 catch( const Exception& )
204 DBG_UNHANDLED_EXCEPTION("dbaccess");
208 void DocumentEventNotifier_Impl::impl_notifyEventAsync_nothrow( const DocumentEvent& _rEvent )
210 if (!m_pEventBroadcaster)
212 m_pEventBroadcaster = ::comphelper::AsyncEventNotifierAutoJoin
213 ::newAsyncEventNotifierAutoJoin("DocumentEventNotifier");
214 if ( m_bInitialized )
216 // start processing the events if and only if we (our document, respectively) are
217 // already initialized
218 ::comphelper::AsyncEventNotifierAutoJoin::launch(m_pEventBroadcaster);
221 m_pEventBroadcaster->addEvent( new DocumentEventHolder( _rEvent ), this );
224 void DocumentEventNotifier_Impl::processEvent( const ::comphelper::AnyEvent& _rEvent )
226 // beware, this is called from the notification thread
228 ::osl::MutexGuard aGuard( m_rMutex );
229 if ( m_bDisposed )
230 return;
232 const DocumentEventHolder& rEventHolder = dynamic_cast< const DocumentEventHolder& >( _rEvent );
233 impl_notifyEvent_nothrow( rEventHolder.getEventObject() );
236 // DocumentEventNotifier
237 DocumentEventNotifier::DocumentEventNotifier( ::cppu::OWeakObject& _rBroadcasterDocument, ::osl::Mutex& _rMutex )
238 :m_pImpl( new DocumentEventNotifier_Impl( _rBroadcasterDocument, _rMutex ) )
242 DocumentEventNotifier::~DocumentEventNotifier()
246 void DocumentEventNotifier::disposing()
248 m_pImpl->disposing();
251 void DocumentEventNotifier::onDocumentInitialized()
253 m_pImpl->onDocumentInitialized();
256 void DocumentEventNotifier::addLegacyEventListener( const Reference< document::XEventListener >& Listener )
258 m_pImpl->addLegacyEventListener( Listener );
261 void DocumentEventNotifier::removeLegacyEventListener( const Reference< document::XEventListener >& Listener )
263 m_pImpl->removeLegacyEventListener( Listener );
266 void DocumentEventNotifier::addDocumentEventListener( const Reference< XDocumentEventListener >& Listener )
268 m_pImpl->addDocumentEventListener( Listener );
271 void DocumentEventNotifier::removeDocumentEventListener( const Reference< XDocumentEventListener >& Listener )
273 m_pImpl->removeDocumentEventListener( Listener );
276 void DocumentEventNotifier::notifyDocumentEvent( const OUString& EventName,
277 const Reference< XController2 >& ViewController, const Any& Supplement )
279 m_pImpl->notifyDocumentEvent( EventName, ViewController, Supplement );
282 void DocumentEventNotifier::notifyDocumentEventAsync( const OUString& EventName,
283 const Reference< XController2 >& ViewController, const Any& Supplement )
285 m_pImpl->notifyDocumentEventAsync( EventName, ViewController, Supplement );
288 } // namespace dbaccess
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */