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 0 : 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 0 : ,m_aDocumentEventListeners( _rMutex )
74 : {
75 0 : }
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 0 : void addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
92 : {
93 0 : m_aDocumentEventListeners.addInterface( _Listener );
94 0 : }
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 0 : void notifyDocumentEvent( const OUString& _EventName, const Reference< XController2 >& _ViewController,
106 : const Any& _Supplement )
107 : {
108 : impl_notifyEvent_nothrow( DocumentEvent(
109 0 : m_rDocument, _EventName, _ViewController, _Supplement ) );
110 0 : }
111 :
112 0 : void notifyDocumentEventAsync( const OUString& _EventName, const Reference< XController2 >& _ViewController,
113 : const Any& _Supplement )
114 : {
115 : impl_notifyEventAsync_nothrow( DocumentEvent(
116 0 : m_rDocument, _EventName, _ViewController, _Supplement ) );
117 0 : }
118 :
119 : protected:
120 0 : virtual ~DocumentEventNotifier_Impl()
121 0 : {
122 0 : }
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 0 : void SAL_CALL DocumentEventNotifier_Impl::acquire() throw ()
133 : {
134 0 : osl_atomic_increment( &m_refCount );
135 0 : }
136 :
137 0 : void SAL_CALL DocumentEventNotifier_Impl::release() throw ()
138 : {
139 0 : if ( 0 == osl_atomic_decrement( &m_refCount ) )
140 0 : delete this;
141 0 : }
142 :
143 0 : void DocumentEventNotifier_Impl::disposing()
144 : {
145 : // SYNCHRONIZED ->
146 : // cancel any pending asynchronous events
147 0 : ::osl::ResettableMutexGuard aGuard( m_rMutex );
148 0 : if ( m_pEventBroadcaster.is() )
149 : {
150 0 : m_pEventBroadcaster->removeEventsForProcessor( this );
151 0 : 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 0 : m_pEventBroadcaster.clear();
159 : }
160 :
161 0 : lang::EventObject aEvent( m_rDocument );
162 0 : aGuard.clear();
163 : // <-- SYNCHRONIZED
164 :
165 0 : m_aLegacyEventListeners.disposeAndClear( aEvent );
166 0 : m_aDocumentEventListeners.disposeAndClear( aEvent );
167 :
168 : // SYNCHRONIZED ->
169 0 : aGuard.reset();
170 0 : m_bDisposed = true;
171 : // <-- SYNCHRONIZED
172 0 : }
173 :
174 0 : void DocumentEventNotifier_Impl::onDocumentInitialized()
175 : {
176 0 : if ( m_bInitialized )
177 0 : throw DoubleInitializationException();
178 :
179 0 : m_bInitialized = true;
180 0 : if ( m_pEventBroadcaster.is() )
181 : // there are already pending asynchronous events
182 0 : m_pEventBroadcaster->launch();
183 0 : }
184 :
185 0 : 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 0 : document::EventObject aLegacyEvent( _rEvent.Source, _rEvent.EventName );
192 0 : m_aLegacyEventListeners.notifyEach( &document::XEventListener::notifyEvent, aLegacyEvent );
193 : }
194 0 : catch(const Exception&)
195 : {
196 : DBG_UNHANDLED_EXCEPTION();
197 : }
198 : try
199 : {
200 0 : m_aDocumentEventListeners.notifyEach( &XDocumentEventListener::documentEventOccured, _rEvent );
201 : }
202 0 : catch( const Exception& )
203 : {
204 : DBG_UNHANDLED_EXCEPTION();
205 : }
206 0 : }
207 :
208 0 : void DocumentEventNotifier_Impl::impl_notifyEventAsync_nothrow( const DocumentEvent& _rEvent )
209 : {
210 0 : if ( !m_pEventBroadcaster.is() )
211 : {
212 : m_pEventBroadcaster.set(
213 0 : new ::comphelper::AsyncEventNotifier("DocumentEventNotifier"));
214 0 : if ( m_bInitialized )
215 : // start processing the events if and only if we (our document, respectively) are
216 : // already initialized
217 0 : m_pEventBroadcaster->launch();
218 : }
219 0 : m_pEventBroadcaster->addEvent( new DocumentEventHolder( _rEvent ), this );
220 0 : }
221 :
222 0 : void DocumentEventNotifier_Impl::processEvent( const ::comphelper::AnyEvent& _rEvent )
223 : {
224 : // beware, this is called from the notification thread
225 : {
226 0 : ::osl::MutexGuard aGuard( m_rMutex );
227 0 : if ( m_bDisposed )
228 0 : return;
229 : }
230 0 : const DocumentEventHolder& rEventHolder = dynamic_cast< const DocumentEventHolder& >( _rEvent );
231 0 : impl_notifyEvent_nothrow( rEventHolder.getEventObject() );
232 : }
233 :
234 : // DocumentEventNotifier
235 0 : DocumentEventNotifier::DocumentEventNotifier( ::cppu::OWeakObject& _rBroadcasterDocument, ::osl::Mutex& _rMutex )
236 0 : :m_pImpl( new DocumentEventNotifier_Impl( _rBroadcasterDocument, _rMutex ) )
237 : {
238 0 : }
239 :
240 0 : DocumentEventNotifier::~DocumentEventNotifier()
241 : {
242 0 : }
243 :
244 0 : void DocumentEventNotifier::disposing()
245 : {
246 0 : m_pImpl->disposing();
247 0 : }
248 :
249 0 : void DocumentEventNotifier::onDocumentInitialized()
250 : {
251 0 : m_pImpl->onDocumentInitialized();
252 0 : }
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 0 : void DocumentEventNotifier::addDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
265 : {
266 0 : m_pImpl->addDocumentEventListener( _Listener );
267 0 : }
268 :
269 0 : void DocumentEventNotifier::removeDocumentEventListener( const Reference< XDocumentEventListener >& _Listener )
270 : {
271 0 : m_pImpl->removeDocumentEventListener( _Listener );
272 0 : }
273 :
274 0 : void DocumentEventNotifier::notifyDocumentEvent( const OUString& _EventName,
275 : const Reference< XController2 >& _ViewController, const Any& _Supplement )
276 : {
277 0 : m_pImpl->notifyDocumentEvent( _EventName, _ViewController, _Supplement );
278 0 : }
279 :
280 0 : void DocumentEventNotifier::notifyDocumentEventAsync( const OUString& _EventName,
281 : const Reference< XController2 >& _ViewController, const Any& _Supplement )
282 : {
283 0 : m_pImpl->notifyDocumentEventAsync( _EventName, _ViewController, _Supplement );
284 0 : }
285 :
286 : } // namespace dbaccess
287 :
288 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|