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 "EventThread.hxx"
21 #include <comphelper/guarding.hxx>
22 #include <tools/debug.hxx>
23 #include <cppuhelper/queryinterface.hxx>
24 
25 #include <memory>
26 
27 namespace frm
28 {
29 
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::awt;
32 using namespace ::com::sun::star::lang;
33 
OComponentEventThread(::cppu::OComponentHelper * pCompImpl)34 OComponentEventThread::OComponentEventThread( ::cppu::OComponentHelper* pCompImpl ) :
35     m_xComp( pCompImpl )
36 {
37 
38     osl_atomic_increment(&m_refCount);
39 
40     // and add us at the Control
41     {
42         Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
43         m_xComp->addEventListener( xEvtLstnr );
44     }
45 
46     osl_atomic_decrement(&m_refCount);
47 }
48 
~OComponentEventThread()49 OComponentEventThread::~OComponentEventThread()
50 {
51 
52     DBG_ASSERT( m_aEvents.empty(),
53         "OComponentEventThread::~OComponentEventThread: Didn't call dispose?" );
54 
55     impl_clearEventQueue();
56 }
57 
queryInterface(const Type & _rType)58 Any SAL_CALL OComponentEventThread::queryInterface(const Type& _rType)
59 {
60     Any aReturn = OWeakObject::queryInterface(_rType);
61 
62     if (!aReturn.hasValue())
63         aReturn = ::cppu::queryInterface(_rType,
64             static_cast<XEventListener*>(this)
65         );
66 
67     return aReturn;
68 }
69 
impl_clearEventQueue()70 void OComponentEventThread::impl_clearEventQueue()
71 {
72     m_aEvents.clear();
73     m_aControls.clear();
74     m_aFlags.clear();
75 }
76 
disposing(const EventObject & evt)77 void OComponentEventThread::disposing( const EventObject& evt )
78 {
79     if( evt.Source == static_cast<XWeak*>(m_xComp.get()) )
80     {
81         ::osl::MutexGuard aGuard( m_aMutex );
82 
83         // Remove EventListener
84         Reference<XEventListener>  xEvtLstnr = static_cast<XEventListener*>(this);
85         m_xComp->removeEventListener( xEvtLstnr );
86 
87         // Clear EventQueue
88         impl_clearEventQueue();
89 
90         // Free the Control and set pCompImpl to 0,
91         // so that the thread knows, that it should terminate.
92         m_xComp.clear();
93 
94         // Wake up the thread and terminate
95         m_aCond.set();
96         terminate();
97     }
98 }
99 
addEvent(std::unique_ptr<EventObject> _pEvt)100 void OComponentEventThread::addEvent( std::unique_ptr<EventObject> _pEvt )
101 {
102     Reference<XControl>  xTmp;
103     addEvent( std::move(_pEvt), xTmp );
104 }
105 
addEvent(std::unique_ptr<EventObject> _pEvt,const Reference<XControl> & rControl,bool bFlag)106 void OComponentEventThread::addEvent( std::unique_ptr<EventObject> _pEvt,
107                                    const Reference<XControl>& rControl,
108                                    bool bFlag )
109 {
110     ::osl::MutexGuard aGuard( m_aMutex );
111 
112     // Put data into the queue
113     m_aEvents.push_back( std::move( _pEvt ) );
114 
115     Reference<XWeak>        xWeakControl(rControl, UNO_QUERY);
116     Reference<XAdapter> xControlAdapter = xWeakControl.is() ? xWeakControl->queryAdapter() : Reference<XAdapter>();
117     m_aControls.push_back( xControlAdapter );
118 
119     m_aFlags.push_back( bFlag );
120 
121     // Wake up thread
122     m_aCond.set();
123 }
124 
onTerminated()125 void SAL_CALL OComponentEventThread::onTerminated()
126 {
127     OComponentEventThread_TBASE::onTerminated();
128 
129     release( );
130 }
131 
run()132 void OComponentEventThread::run()
133 {
134     osl_setThreadName("frm::OComponentEventThread");
135 
136     acquire( );
137 
138     // Hold on to ourselves, so that we're not deleted if a dispose is called at some point in time
139     css::uno::Reference<css::uno::XInterface> xThis(static_cast<XWeak*>(this));
140 
141     do
142     {
143         ::osl::MutexGuard aGuard(m_aMutex);
144 
145         while( !m_aEvents.empty() )
146         {
147             // Get the Control and hold on to it so that it cannot be deleted during actionPerformed
148             rtl::Reference<::cppu::OComponentHelper> xComp = m_xComp;
149 
150             ThreadEvents::iterator firstEvent( m_aEvents.begin() );
151             std::unique_ptr<EventObject> pEvt = std::move(*firstEvent);
152             m_aEvents.erase( firstEvent );
153 
154             ThreadObjects::iterator firstControl( m_aControls.begin() );
155             Reference<XAdapter> xControlAdapter = *firstControl;
156             m_aControls.erase( firstControl );
157 
158             auto firstFlag( m_aFlags.begin() );
159             bool bFlag = *firstFlag;
160             m_aFlags.erase( firstFlag );
161 
162             {
163                 MutexRelease aReleaseOnce(m_aMutex);
164                 // Because a queryHardRef can throw an Exception, it should not be called when
165                 // the mutex is locked.
166                 Reference<XControl>  xControl;
167                 if ( xControlAdapter.is() )
168                     xControl.set(
169                         xControlAdapter->queryAdapted(), css::uno::UNO_QUERY);
170 
171                 if( xComp.is() )
172                     processEvent( xComp.get(), pEvt.get(), xControl, bFlag );
173             }
174         }
175 
176         // After a Dispose, we do not know the Control anymore.
177         // Thus, we must not wait either.
178         if( !m_xComp.is() )
179             return;
180 
181         // Reset waiting condition
182         m_aCond.reset();
183         {
184             MutexRelease aReleaseOnce(m_aMutex);
185             // And wait ... if, in the meantime, an Event came in after all
186             m_aCond.wait();
187         }
188     }
189     while( true );
190 }
191 
192 
193 }   // namespace frm
194 
195 
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
197