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