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 0 : 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 0 : ,m_aDocumentEventListeners( _rMutex )
80 : {
81 0 : }
82 :
83 : // IReference
84 : virtual void SAL_CALL acquire();
85 : virtual void SAL_CALL release();
86 :
87 0 : void addLegacyEventListener( const Reference< document::XEventListener >& _Listener )
88 : {
89 0 : m_aLegacyEventListeners.addInterface( _Listener );
90 0 : }
91 :
92 0 : void removeLegacyEventListener( const Reference< document::XEventListener >& _Listener )
93 : {
94 0 : m_aLegacyEventListeners.removeInterface( _Listener );
95 0 : }
96 :
97 0 : void addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
98 : {
99 0 : m_aDocumentEventListeners.addInterface( _Listener );
100 0 : }
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 0 : void notifyDocumentEvent( const ::rtl::OUString& _EventName, const Reference< XController2 >& _ViewController,
112 : const Any& _Supplement )
113 : {
114 : impl_notifyEvent_nothrow( DocumentEvent(
115 0 : m_rDocument, _EventName, _ViewController, _Supplement ) );
116 0 : }
117 :
118 0 : void notifyDocumentEventAsync( const ::rtl::OUString& _EventName, const Reference< XController2 >& _ViewController,
119 : const Any& _Supplement )
120 : {
121 : impl_notifyEventAsync_nothrow( DocumentEvent(
122 0 : m_rDocument, _EventName, _ViewController, _Supplement ) );
123 0 : }
124 :
125 : protected:
126 0 : virtual ~DocumentEventNotifier_Impl()
127 0 : {
128 0 : }
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 0 : void SAL_CALL DocumentEventNotifier_Impl::acquire()
139 : {
140 0 : osl_atomic_increment( &m_refCount );
141 0 : }
142 :
143 0 : void SAL_CALL DocumentEventNotifier_Impl::release()
144 : {
145 0 : if ( 0 == osl_atomic_decrement( &m_refCount ) )
146 0 : delete this;
147 0 : }
148 :
149 0 : void DocumentEventNotifier_Impl::disposing()
150 : {
151 : // SYNCHRONIZED ->
152 : // cancel any pending asynchronous events
153 0 : ::osl::ResettableMutexGuard aGuard( m_rMutex );
154 0 : if ( m_pEventBroadcaster.is() )
155 : {
156 0 : m_pEventBroadcaster->removeEventsForProcessor( this );
157 0 : 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 0 : m_pEventBroadcaster.clear();
165 : }
166 :
167 0 : lang::EventObject aEvent( m_rDocument );
168 0 : aGuard.clear();
169 : // <-- SYNCHRONIZED
170 :
171 0 : m_aLegacyEventListeners.disposeAndClear( aEvent );
172 0 : m_aDocumentEventListeners.disposeAndClear( aEvent );
173 :
174 : // SYNCHRONIZED ->
175 0 : aGuard.reset();
176 0 : m_bDisposed = true;
177 : // <-- SYNCHRONIZED
178 0 : }
179 :
180 0 : void DocumentEventNotifier_Impl::onDocumentInitialized()
181 : {
182 0 : if ( m_bInitialized )
183 0 : throw DoubleInitializationException();
184 :
185 0 : m_bInitialized = true;
186 0 : if ( m_pEventBroadcaster.is() )
187 : // there are already pending asynchronous events
188 0 : m_pEventBroadcaster->launch();
189 0 : }
190 :
191 0 : 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 0 : document::EventObject aLegacyEvent( _rEvent.Source, _rEvent.EventName );
198 0 : m_aLegacyEventListeners.notifyEach( &document::XEventListener::notifyEvent, aLegacyEvent );
199 : }
200 0 : catch(const Exception&)
201 : {
202 : DBG_UNHANDLED_EXCEPTION();
203 : }
204 : try
205 : {
206 0 : m_aDocumentEventListeners.notifyEach( &XDocumentEventListener::documentEventOccured, _rEvent );
207 : }
208 0 : catch( const Exception& )
209 : {
210 : DBG_UNHANDLED_EXCEPTION();
211 : }
212 0 : }
213 :
214 0 : void DocumentEventNotifier_Impl::impl_notifyEventAsync_nothrow( const DocumentEvent& _rEvent )
215 : {
216 0 : if ( !m_pEventBroadcaster.is() )
217 : {
218 : m_pEventBroadcaster.set(
219 0 : new ::comphelper::AsyncEventNotifier("DocumentEventNotifier"));
220 0 : if ( m_bInitialized )
221 : // start processing the events if and only if we (our document, respectively) are
222 : // already initialized
223 0 : m_pEventBroadcaster->launch();
224 : }
225 0 : m_pEventBroadcaster->addEvent( new DocumentEventHolder( _rEvent ), this );
226 0 : }
227 :
228 0 : void DocumentEventNotifier_Impl::processEvent( const ::comphelper::AnyEvent& _rEvent )
229 : {
230 : // beware, this is called from the notification thread
231 : {
232 0 : ::osl::MutexGuard aGuard( m_rMutex );
233 0 : if ( m_bDisposed )
234 0 : return;
235 : }
236 0 : const DocumentEventHolder& rEventHolder = dynamic_cast< const DocumentEventHolder& >( _rEvent );
237 0 : impl_notifyEvent_nothrow( rEventHolder.getEventObject() );
238 : }
239 :
240 : //====================================================================
241 : //= DocumentEventNotifier
242 : //====================================================================
243 0 : DocumentEventNotifier::DocumentEventNotifier( ::cppu::OWeakObject& _rBroadcasterDocument, ::osl::Mutex& _rMutex )
244 0 : :m_pImpl( new DocumentEventNotifier_Impl( _rBroadcasterDocument, _rMutex ) )
245 : {
246 0 : }
247 :
248 0 : DocumentEventNotifier::~DocumentEventNotifier()
249 : {
250 0 : }
251 :
252 0 : void DocumentEventNotifier::disposing()
253 : {
254 0 : m_pImpl->disposing();
255 0 : }
256 :
257 0 : void DocumentEventNotifier::onDocumentInitialized()
258 : {
259 0 : m_pImpl->onDocumentInitialized();
260 0 : }
261 :
262 0 : void DocumentEventNotifier::addLegacyEventListener( const Reference< document::XEventListener >& _Listener )
263 : {
264 0 : m_pImpl->addLegacyEventListener( _Listener );
265 0 : }
266 :
267 0 : void DocumentEventNotifier::removeLegacyEventListener( const Reference< document::XEventListener >& _Listener )
268 : {
269 0 : m_pImpl->removeLegacyEventListener( _Listener );
270 0 : }
271 :
272 0 : void DocumentEventNotifier::addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
273 : {
274 0 : m_pImpl->addDocumentEventListener( _Listener );
275 0 : }
276 :
277 0 : void DocumentEventNotifier::removeDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
278 : {
279 0 : m_pImpl->removeDocumentEventListener( _Listener );
280 0 : }
281 :
282 0 : void DocumentEventNotifier::notifyDocumentEvent( const ::rtl::OUString& _EventName,
283 : const Reference< XController2 >& _ViewController, const Any& _Supplement )
284 : {
285 0 : m_pImpl->notifyDocumentEvent( _EventName, _ViewController, _Supplement );
286 0 : }
287 :
288 0 : void DocumentEventNotifier::notifyDocumentEventAsync( const ::rtl::OUString& _EventName,
289 : const Reference< XController2 >& _ViewController, const Any& _Supplement )
290 : {
291 0 : m_pImpl->notifyDocumentEventAsync( _EventName, _ViewController, _Supplement );
292 0 : }
293 :
294 : } // namespace dbaccess
295 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|