1*b557fc96SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*b557fc96SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*b557fc96SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*b557fc96SAndrew Rist * distributed with this work for additional information
6*b557fc96SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*b557fc96SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*b557fc96SAndrew Rist * "License"); you may not use this file except in compliance
9*b557fc96SAndrew Rist * with the License. You may obtain a copy of the License at
10*b557fc96SAndrew Rist *
11*b557fc96SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*b557fc96SAndrew Rist *
13*b557fc96SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*b557fc96SAndrew Rist * software distributed under the License is distributed on an
15*b557fc96SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b557fc96SAndrew Rist * KIND, either express or implied. See the License for the
17*b557fc96SAndrew Rist * specific language governing permissions and limitations
18*b557fc96SAndrew Rist * under the License.
19*b557fc96SAndrew Rist *
20*b557fc96SAndrew Rist *************************************************************/
21*b557fc96SAndrew Rist
22*b557fc96SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_fpicker.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir //------------------------------------------------------------------------
28cdf0e10cSrcweir // includes
29cdf0e10cSrcweir //------------------------------------------------------------------------
30cdf0e10cSrcweir #include <osl/diagnose.h>
31cdf0e10cSrcweir #include "asynceventnotifier.hxx"
32cdf0e10cSrcweir #include <com/sun/star/uno/RuntimeException.hpp>
33cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
34cdf0e10cSrcweir
35cdf0e10cSrcweir #include <process.h>
36cdf0e10cSrcweir #include <memory>
37cdf0e10cSrcweir #include "SolarMutex.hxx"
38cdf0e10cSrcweir
39cdf0e10cSrcweir //------------------------------------------------
40cdf0e10cSrcweir //
41cdf0e10cSrcweir //------------------------------------------------
42cdf0e10cSrcweir
43cdf0e10cSrcweir using namespace com::sun::star;
44cdf0e10cSrcweir using ::com::sun::star::ui::dialogs::XFilePickerListener;
45cdf0e10cSrcweir
46cdf0e10cSrcweir //------------------------------------------------
47cdf0e10cSrcweir //
48cdf0e10cSrcweir //------------------------------------------------
49cdf0e10cSrcweir
CAsyncEventNotifier(cppu::OBroadcastHelper & rBroadcastHelper)50cdf0e10cSrcweir CAsyncEventNotifier::CAsyncEventNotifier(cppu::OBroadcastHelper& rBroadcastHelper) :
51cdf0e10cSrcweir m_hThread(0),
52cdf0e10cSrcweir m_bRun(false),
53cdf0e10cSrcweir m_ThreadId(0),
54cdf0e10cSrcweir m_rBroadcastHelper(rBroadcastHelper),
55cdf0e10cSrcweir m_NotifyEvent(m_hEvents[0]),
56cdf0e10cSrcweir m_ResumeNotifying(m_hEvents[1])
57cdf0e10cSrcweir {
58cdf0e10cSrcweir // m_NotifyEvent
59cdf0e10cSrcweir m_hEvents[0] = CreateEvent(0, /* no security */
60cdf0e10cSrcweir true, /* manual reset */
61cdf0e10cSrcweir false, /* initial state not signaled */
62cdf0e10cSrcweir 0); /* automatic name */
63cdf0e10cSrcweir
64cdf0e10cSrcweir // m_ResumeNotifying
65cdf0e10cSrcweir m_hEvents[1] = CreateEvent(0, /* no security */
66cdf0e10cSrcweir true, /* manual reset */
67cdf0e10cSrcweir false, /* initial state not signaled */
68cdf0e10cSrcweir 0); /* automatic name */
69cdf0e10cSrcweir }
70cdf0e10cSrcweir
71cdf0e10cSrcweir //------------------------------------------------
72cdf0e10cSrcweir //
73cdf0e10cSrcweir //------------------------------------------------
74cdf0e10cSrcweir
~CAsyncEventNotifier()75cdf0e10cSrcweir CAsyncEventNotifier::~CAsyncEventNotifier()
76cdf0e10cSrcweir {
77cdf0e10cSrcweir OSL_ENSURE(0 == m_hThread,"Thread not stopped, destroying this instance leads to desaster");
78cdf0e10cSrcweir
79cdf0e10cSrcweir CloseHandle(m_hEvents[0]);
80cdf0e10cSrcweir CloseHandle(m_hEvents[1]);
81cdf0e10cSrcweir }
82cdf0e10cSrcweir
83cdf0e10cSrcweir //------------------------------------------------
84cdf0e10cSrcweir //
85cdf0e10cSrcweir //------------------------------------------------
86cdf0e10cSrcweir
addListener(const uno::Type & aType,const uno::Reference<uno::XInterface> & xListener)87cdf0e10cSrcweir void SAL_CALL CAsyncEventNotifier::addListener(const uno::Type& aType ,
88cdf0e10cSrcweir const uno::Reference< uno::XInterface >& xListener)
89cdf0e10cSrcweir {
90cdf0e10cSrcweir if ( m_rBroadcastHelper.bDisposed )
91cdf0e10cSrcweir throw lang::DisposedException(
92cdf0e10cSrcweir ::rtl::OUString::createFromAscii( "FilePicker is already disposed" ),
93cdf0e10cSrcweir uno::Reference< uno::XInterface >() );
94cdf0e10cSrcweir
95cdf0e10cSrcweir if ( m_rBroadcastHelper.bInDispose )
96cdf0e10cSrcweir throw lang::DisposedException(
97cdf0e10cSrcweir ::rtl::OUString::createFromAscii( "FilePicker will be disposed now." ),
98cdf0e10cSrcweir uno::Reference< uno::XInterface >() );
99cdf0e10cSrcweir
100cdf0e10cSrcweir m_rBroadcastHelper.aLC.addInterface( aType, xListener );
101cdf0e10cSrcweir }
102cdf0e10cSrcweir
103cdf0e10cSrcweir //------------------------------------------------
104cdf0e10cSrcweir //
105cdf0e10cSrcweir //------------------------------------------------
106cdf0e10cSrcweir
removeListener(const uno::Type & aType,const uno::Reference<uno::XInterface> & xListener)107cdf0e10cSrcweir void SAL_CALL CAsyncEventNotifier::removeListener(const uno::Type& aType ,
108cdf0e10cSrcweir const uno::Reference< uno::XInterface >& xListener)
109cdf0e10cSrcweir {
110cdf0e10cSrcweir if ( m_rBroadcastHelper.bDisposed )
111cdf0e10cSrcweir throw lang::DisposedException(
112cdf0e10cSrcweir ::rtl::OUString::createFromAscii( "FilePicker is already disposed." ),
113cdf0e10cSrcweir uno::Reference< uno::XInterface >() );
114cdf0e10cSrcweir
115cdf0e10cSrcweir m_rBroadcastHelper.aLC.removeInterface( aType, xListener );
116cdf0e10cSrcweir }
117cdf0e10cSrcweir
118cdf0e10cSrcweir //------------------------------------------------
119cdf0e10cSrcweir //
120cdf0e10cSrcweir //------------------------------------------------
121cdf0e10cSrcweir
startup(bool bCreateSuspended)122cdf0e10cSrcweir bool SAL_CALL CAsyncEventNotifier::startup(bool bCreateSuspended)
123cdf0e10cSrcweir {
124cdf0e10cSrcweir osl::MutexGuard aGuard(m_Mutex);
125cdf0e10cSrcweir
126cdf0e10cSrcweir // m_bRun may already be false because of a
127cdf0e10cSrcweir // call to stop but the thread did not yet
128cdf0e10cSrcweir // terminate so m_hEventNotifierThread is
129cdf0e10cSrcweir // yet a valid thread handle that should
130cdf0e10cSrcweir // not be overwritten
131cdf0e10cSrcweir if (!m_bRun)
132cdf0e10cSrcweir {
133cdf0e10cSrcweir if (!bCreateSuspended)
134cdf0e10cSrcweir SetEvent(m_ResumeNotifying);
135cdf0e10cSrcweir
136cdf0e10cSrcweir m_hThread = (HANDLE)_beginthreadex(
137cdf0e10cSrcweir NULL, 0, CAsyncEventNotifier::ThreadProc, this, 0, &m_ThreadId);
138cdf0e10cSrcweir
139cdf0e10cSrcweir OSL_ASSERT(0 != m_hThread);
140cdf0e10cSrcweir
141cdf0e10cSrcweir if (m_hThread)
142cdf0e10cSrcweir m_bRun = true;
143cdf0e10cSrcweir }
144cdf0e10cSrcweir
145cdf0e10cSrcweir OSL_POSTCOND(m_bRun,"Could not start event notifier!");
146cdf0e10cSrcweir
147cdf0e10cSrcweir return m_bRun;
148cdf0e10cSrcweir }
149cdf0e10cSrcweir
150cdf0e10cSrcweir //------------------------------------------------
151cdf0e10cSrcweir //
152cdf0e10cSrcweir //------------------------------------------------
153cdf0e10cSrcweir
shutdown()154cdf0e10cSrcweir void SAL_CALL CAsyncEventNotifier::shutdown()
155cdf0e10cSrcweir {
156cdf0e10cSrcweir unsigned nThreadId = GetCurrentThreadId();
157cdf0e10cSrcweir
158cdf0e10cSrcweir OSL_PRECOND(nThreadId != m_ThreadId, "Method called in wrong thread context!");
159cdf0e10cSrcweir
160cdf0e10cSrcweir osl::ResettableMutexGuard aGuard(m_Mutex);
161cdf0e10cSrcweir
162cdf0e10cSrcweir OSL_PRECOND(m_bRun,"Event notifier does not run!");
163cdf0e10cSrcweir
164cdf0e10cSrcweir m_bRun = false;
165cdf0e10cSrcweir m_EventList.clear();
166cdf0e10cSrcweir
167cdf0e10cSrcweir // awake the the notifier thread
168cdf0e10cSrcweir SetEvent(m_ResumeNotifying);
169cdf0e10cSrcweir SetEvent(m_NotifyEvent);
170cdf0e10cSrcweir
171cdf0e10cSrcweir // releas the mutex here because the event
172cdf0e10cSrcweir // notifier thread may need it to finish
173cdf0e10cSrcweir aGuard.clear();
174cdf0e10cSrcweir
175cdf0e10cSrcweir // we are waiting infinite, so error will
176cdf0e10cSrcweir // be better detected in form of deadlocks
177cdf0e10cSrcweir if (WaitForSingleObject(m_hThread, INFINITE) == WAIT_FAILED) {
178cdf0e10cSrcweir OSL_ENSURE(false, "Waiting for thread termination failed!");
179cdf0e10cSrcweir }
180cdf0e10cSrcweir
181cdf0e10cSrcweir // lock mutex again to reset m_hThread
182cdf0e10cSrcweir // and prevent a race with start()
183cdf0e10cSrcweir aGuard.reset();
184cdf0e10cSrcweir
185cdf0e10cSrcweir CloseHandle(m_hThread);
186cdf0e10cSrcweir m_hThread = 0;
187cdf0e10cSrcweir }
188cdf0e10cSrcweir
189cdf0e10cSrcweir //------------------------------------------------
190cdf0e10cSrcweir //
191cdf0e10cSrcweir //------------------------------------------------
192cdf0e10cSrcweir
suspend()193cdf0e10cSrcweir void CAsyncEventNotifier::suspend()
194cdf0e10cSrcweir {
195cdf0e10cSrcweir ResetEvent(m_ResumeNotifying);
196cdf0e10cSrcweir }
197cdf0e10cSrcweir
198cdf0e10cSrcweir //------------------------------------------------
199cdf0e10cSrcweir //
200cdf0e10cSrcweir //------------------------------------------------
201cdf0e10cSrcweir
resume()202cdf0e10cSrcweir void CAsyncEventNotifier::resume()
203cdf0e10cSrcweir {
204cdf0e10cSrcweir SetEvent(m_ResumeNotifying);
205cdf0e10cSrcweir }
206cdf0e10cSrcweir
207cdf0e10cSrcweir //------------------------------------------------
208cdf0e10cSrcweir //
209cdf0e10cSrcweir //------------------------------------------------
210cdf0e10cSrcweir
notifyEvent(CEventNotification * EventNotification)211cdf0e10cSrcweir void SAL_CALL CAsyncEventNotifier::notifyEvent(CEventNotification* EventNotification)
212cdf0e10cSrcweir {
213cdf0e10cSrcweir osl::MutexGuard aGuard(m_Mutex);
214cdf0e10cSrcweir
215cdf0e10cSrcweir OSL_ENSURE(m_bRun,"Event notifier is not running!");
216cdf0e10cSrcweir
217cdf0e10cSrcweir if (m_bRun)
218cdf0e10cSrcweir {
219cdf0e10cSrcweir m_EventList.push_back(EventNotification);
220cdf0e10cSrcweir SetEvent(m_NotifyEvent);
221cdf0e10cSrcweir }
222cdf0e10cSrcweir }
223cdf0e10cSrcweir
224cdf0e10cSrcweir //------------------------------------------------
225cdf0e10cSrcweir //
226cdf0e10cSrcweir //------------------------------------------------
227cdf0e10cSrcweir
getEventListSize()228cdf0e10cSrcweir size_t SAL_CALL CAsyncEventNotifier::getEventListSize()
229cdf0e10cSrcweir {
230cdf0e10cSrcweir osl::MutexGuard aGuard(m_Mutex);
231cdf0e10cSrcweir return m_EventList.size();
232cdf0e10cSrcweir }
233cdf0e10cSrcweir
234cdf0e10cSrcweir //------------------------------------------------
235cdf0e10cSrcweir //
236cdf0e10cSrcweir //------------------------------------------------
237cdf0e10cSrcweir
resetNotifyEvent()238cdf0e10cSrcweir void SAL_CALL CAsyncEventNotifier::resetNotifyEvent()
239cdf0e10cSrcweir {
240cdf0e10cSrcweir osl::MutexGuard aGuard(m_Mutex);
241cdf0e10cSrcweir if (0 == m_EventList.size())
242cdf0e10cSrcweir ResetEvent(m_NotifyEvent);
243cdf0e10cSrcweir }
244cdf0e10cSrcweir
245cdf0e10cSrcweir //------------------------------------------------
246cdf0e10cSrcweir //
247cdf0e10cSrcweir //------------------------------------------------
248cdf0e10cSrcweir
getNextEventRecord()249cdf0e10cSrcweir CEventNotification* SAL_CALL CAsyncEventNotifier::getNextEventRecord()
250cdf0e10cSrcweir {
251cdf0e10cSrcweir osl::MutexGuard aGuard(m_Mutex);
252cdf0e10cSrcweir return m_EventList.front();
253cdf0e10cSrcweir }
254cdf0e10cSrcweir
255cdf0e10cSrcweir //------------------------------------------------
256cdf0e10cSrcweir //
257cdf0e10cSrcweir //------------------------------------------------
258cdf0e10cSrcweir
removeNextEventRecord()259cdf0e10cSrcweir void SAL_CALL CAsyncEventNotifier::removeNextEventRecord()
260cdf0e10cSrcweir {
261cdf0e10cSrcweir osl::MutexGuard aGuard(m_Mutex);
262cdf0e10cSrcweir m_EventList.pop_front();
263cdf0e10cSrcweir }
264cdf0e10cSrcweir
265cdf0e10cSrcweir //------------------------------------------------
266cdf0e10cSrcweir //
267cdf0e10cSrcweir //------------------------------------------------
268cdf0e10cSrcweir
run()269cdf0e10cSrcweir void SAL_CALL CAsyncEventNotifier::run()
270cdf0e10cSrcweir {
271cdf0e10cSrcweir while (m_bRun)
272cdf0e10cSrcweir {
273cdf0e10cSrcweir WaitForMultipleObjects(2, m_hEvents, true, INFINITE);
274cdf0e10cSrcweir
275cdf0e10cSrcweir if (m_bRun)
276cdf0e10cSrcweir {
277cdf0e10cSrcweir while (getEventListSize() > 0)
278cdf0e10cSrcweir {
279cdf0e10cSrcweir std::auto_ptr<CEventNotification> EventNotification(getNextEventRecord());
280cdf0e10cSrcweir removeNextEventRecord();
281cdf0e10cSrcweir
282cdf0e10cSrcweir ::cppu::OInterfaceContainerHelper* pICHelper =
283cdf0e10cSrcweir m_rBroadcastHelper.getContainer(getCppuType((uno::Reference<XFilePickerListener>*)0));
284cdf0e10cSrcweir
285cdf0e10cSrcweir if (pICHelper)
286cdf0e10cSrcweir {
287cdf0e10cSrcweir ::cppu::OInterfaceIteratorHelper iter(*pICHelper);
288cdf0e10cSrcweir
289cdf0e10cSrcweir while(iter.hasMoreElements())
290cdf0e10cSrcweir {
291cdf0e10cSrcweir try
292cdf0e10cSrcweir {
293cdf0e10cSrcweir EventNotification->notifyEventListener(iter.next());
294cdf0e10cSrcweir }
295cdf0e10cSrcweir catch(uno::RuntimeException&)
296cdf0e10cSrcweir {
297cdf0e10cSrcweir OSL_ENSURE(sal_False,"RuntimeException during event dispatching");
298cdf0e10cSrcweir }
299cdf0e10cSrcweir }
300cdf0e10cSrcweir }
301cdf0e10cSrcweir
302cdf0e10cSrcweir } // while(getEventListSize() > 0)
303cdf0e10cSrcweir
304cdf0e10cSrcweir resetNotifyEvent();
305cdf0e10cSrcweir
306cdf0e10cSrcweir } // if (m_bRun)
307cdf0e10cSrcweir
308cdf0e10cSrcweir } // while(m_bRun)
309cdf0e10cSrcweir }
310cdf0e10cSrcweir
311cdf0e10cSrcweir //------------------------------------------------
312cdf0e10cSrcweir //
313cdf0e10cSrcweir //------------------------------------------------
314cdf0e10cSrcweir
ThreadProc(LPVOID pParam)315cdf0e10cSrcweir unsigned int WINAPI CAsyncEventNotifier::ThreadProc(LPVOID pParam)
316cdf0e10cSrcweir {
317cdf0e10cSrcweir CAsyncEventNotifier* pInst = reinterpret_cast< CAsyncEventNotifier* >(pParam);
318cdf0e10cSrcweir OSL_ASSERT(pInst);
319cdf0e10cSrcweir
320cdf0e10cSrcweir pInst->run();
321cdf0e10cSrcweir
322cdf0e10cSrcweir return 0;
323cdf0e10cSrcweir }
324