LibreOffice Module vcl (master) 1
session.cxx
Go to the documentation of this file.
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 <memory>
21#include <sal/config.h>
22#include <sal/log.hxx>
23
26
28#include <utility>
29#include <vcl/svapp.hxx>
30
31#include <factory.hxx>
32#include <svdata.hxx>
33#include <salinst.hxx>
34#include <salsession.hxx>
35
36#include <com/sun/star/frame/XSessionManagerClient.hpp>
37#include <com/sun/star/lang/XMultiServiceFactory.hpp>
38#include <com/sun/star/frame/XSessionManagerListener2.hpp>
39
40#include <vector>
41
42using namespace com::sun::star::uno;
43using namespace com::sun::star::lang;
44using namespace com::sun::star::frame;
45
47{
48}
49
50namespace {
51
52class VCLSession:
53 private cppu::BaseMutex,
54 public cppu::WeakComponentImplHelper < XSessionManagerClient >
55{
56 struct Listener
57 {
58 css::uno::Reference< XSessionManagerListener > m_xListener;
59 bool m_bInteractionRequested;
60 bool m_bInteractionDone;
61 bool m_bSaveDone;
62
63 explicit Listener( css::uno::Reference< XSessionManagerListener > xListener )
64 : m_xListener(std::move( xListener )),
65 m_bInteractionRequested( false ),
66 m_bInteractionDone( false ),
67 m_bSaveDone( false )
68 {}
69 };
70
71 std::vector< Listener > m_aListeners;
72 std::unique_ptr< SalSession > m_xSession;
73 bool m_bInteractionRequested;
74 bool m_bInteractionGranted;
75 bool m_bInteractionDone;
76 bool m_bSaveDone;
77
78 static void SalSessionEventProc( void* pData, SalSessionEvent* pEvent );
79
80 virtual ~VCLSession() override {}
81
82 virtual void SAL_CALL addSessionManagerListener( const css::uno::Reference< XSessionManagerListener >& xListener ) override;
83 virtual void SAL_CALL removeSessionManagerListener( const css::uno::Reference< XSessionManagerListener>& xListener ) override;
84 virtual void SAL_CALL queryInteraction( const css::uno::Reference< XSessionManagerListener >& xListener ) override;
85 virtual void SAL_CALL interactionDone( const css::uno::Reference< XSessionManagerListener >& xListener ) override;
86 virtual void SAL_CALL saveDone( const css::uno::Reference< XSessionManagerListener >& xListener ) override;
87 virtual sal_Bool SAL_CALL cancelShutdown() override;
88
89 void SAL_CALL disposing() override;
90
91 void callSaveRequested( bool bShutdown );
92 void callShutdownCancelled();
93 void callInteractionGranted( bool bGranted );
94 void callQuit();
95
96public:
97 VCLSession();
98};
99
100}
101
102VCLSession::VCLSession()
103 : cppu::WeakComponentImplHelper< XSessionManagerClient >( m_aMutex ),
104 m_xSession( ImplGetSVData()->mpDefInst->CreateSalSession() ),
105 m_bInteractionRequested( false ),
106 m_bInteractionGranted( false ),
107 m_bInteractionDone( false ),
108 m_bSaveDone( false )
109{
110 SAL_INFO("vcl.se", "VCLSession::VCLSession" );
111
112 if (m_xSession)
113 m_xSession->SetCallback( SalSessionEventProc, this );
114}
115
116void VCLSession::callSaveRequested( bool bShutdown )
117{
118 SAL_INFO("vcl.se", "VCLSession::callSaveRequested" );
119
120 std::vector< Listener > aListeners;
121 {
122 osl::MutexGuard aGuard( m_aMutex );
123 // reset listener states
124 for (auto & listener : m_aListeners) {
125 listener.m_bSaveDone = listener.m_bInteractionRequested = listener.m_bInteractionDone = false;
126 }
127
128 // copy listener vector since calling a listener may remove it.
130 // set back interaction state
131 m_bSaveDone = false;
132 m_bInteractionDone = false;
133 // without session we assume UI is always possible,
134 // so it was requested and granted
135 m_bInteractionRequested = m_bInteractionGranted = !m_xSession;
136
137 // answer the session manager even if no listeners available anymore
138 SAL_WARN_IF( aListeners.empty(), "vcl.se", "saveRequested but no listeners !" );
139
140 SAL_INFO("vcl.se.debug", " aListeners.empty() = " << (aListeners.empty() ? "true" : "false") <<
141 ", bShutdown = " << (bShutdown ? "true" : "false"));
142 if( aListeners.empty() )
143 {
144 if (m_xSession)
145 m_xSession->saveDone();
146 return;
147 }
148 }
149
150 SolarMutexReleaser aReleaser;
151 for (auto const & listener: aListeners)
152 listener.m_xListener->doSave( bShutdown, false/*bCancelable*/ );
153}
154
155void VCLSession::callInteractionGranted( bool bInteractionGranted )
156{
157 SAL_INFO("vcl.se", "VCLSession::callInteractionGranted" );
158
159 std::vector< Listener > aListeners;
160 {
161 osl::MutexGuard aGuard( m_aMutex );
162 // copy listener vector since calling a listener may remove it.
163 for (auto const & listener: m_aListeners)
164 if( listener.m_bInteractionRequested )
165 aListeners.push_back( listener );
166
167 m_bInteractionGranted = bInteractionGranted;
168
169 // answer the session manager even if no listeners available anymore
170 SAL_WARN_IF( aListeners.empty(), "vcl.se", "interactionGranted but no listeners !" );
171
172 SAL_INFO("vcl.se.debug", " aListeners.empty() = " << (aListeners.empty() ? "true" : "false") <<
173 ", bInteractionGranted = " << (bInteractionGranted ? "true" : "false"));
174 if( aListeners.empty() )
175 {
176 if (m_xSession)
177 m_xSession->interactionDone();
178 return;
179 }
180 }
181
182 SolarMutexReleaser aReleaser;
183 for (auto const & listener: aListeners)
184 listener.m_xListener->approveInteraction( bInteractionGranted );
185}
186
187void VCLSession::callShutdownCancelled()
188{
189 SAL_INFO("vcl.se", "VCLSession::callShutdownCancelled");
190
191 std::vector< Listener > aListeners;
192 {
193 osl::MutexGuard aGuard( m_aMutex );
194 // copy listener vector since calling a listener may remove it.
196 // set back interaction state
197 m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
198 }
199
200 SolarMutexReleaser aReleaser;
201 for (auto const & listener: aListeners)
202 listener.m_xListener->shutdownCanceled();
203}
204
205void VCLSession::callQuit()
206{
207 SAL_INFO("vcl.se", "VCLSession::callQuit");
208
209 std::vector< Listener > aListeners;
210 {
211 osl::MutexGuard aGuard( m_aMutex );
212 // copy listener vector since calling a listener may remove it.
214 // set back interaction state
215 m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
216 }
217
218 SolarMutexReleaser aReleaser;
219 for (auto const & listener: aListeners)
220 {
221 css::uno::Reference< XSessionManagerListener2 > xListener2( listener.m_xListener, UNO_QUERY );
222 if( xListener2.is() )
223 xListener2->doQuit();
224 }
225}
226
227void VCLSession::SalSessionEventProc( void* pData, SalSessionEvent* pEvent )
228{
229 SAL_INFO("vcl.se", "VCLSession::SalSessionEventProc");
230
231 VCLSession * pThis = static_cast< VCLSession * >( pData );
232 switch( pEvent->m_eType )
233 {
234 case Interaction:
235 {
236 SAL_INFO("vcl.se.debug", " EventProcType = Interaction");
237 SalSessionInteractionEvent* pIEv = static_cast<SalSessionInteractionEvent*>(pEvent);
238 pThis->callInteractionGranted( pIEv->m_bInteractionGranted );
239 }
240 break;
241 case SaveRequest:
242 {
243 SAL_INFO("vcl.se.debug", " EventProcType = SaveRequest");
244 SalSessionSaveRequestEvent* pSEv = static_cast<SalSessionSaveRequestEvent*>(pEvent);
245 pThis->callSaveRequested( pSEv->m_bShutdown );
246 }
247 break;
248 case ShutdownCancel:
249 SAL_INFO("vcl.se.debug", " EventProcType = ShutdownCancel");
250 pThis->callShutdownCancelled();
251 break;
252 case Quit:
253 SAL_INFO("vcl.se.debug", " EventProcType = Quit");
254 pThis->callQuit();
255 break;
256 }
257}
258
259void SAL_CALL VCLSession::addSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener )
260{
261 SAL_INFO("vcl.se", "VCLSession::addSessionManagerListener" );
262
263 osl::MutexGuard aGuard( m_aMutex );
264
265 SAL_INFO("vcl.se.debug", " m_aListeners.size() = " << m_aListeners.size() );
266 m_aListeners.emplace_back( xListener );
267}
268
269void SAL_CALL VCLSession::removeSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener )
270{
271 SAL_INFO("vcl.se", "VCLSession::removeSessionManagerListener" );
272
273 osl::MutexGuard aGuard( m_aMutex );
274
275 SAL_INFO("vcl.se.debug", " m_aListeners.size() = " << m_aListeners.size() );
276
277 m_aListeners.erase(std::remove_if(m_aListeners.begin(), m_aListeners.end(), [&](Listener& listener) {return xListener == listener.m_xListener;}), m_aListeners.end());
278}
279
280void SAL_CALL VCLSession::queryInteraction( const css::uno::Reference<XSessionManagerListener>& xListener )
281{
282 SAL_INFO("vcl.se", "VCLSession::queryInteraction");
283
284 SAL_INFO("vcl.se.debug", " m_bInteractionGranted = " << (m_bInteractionGranted ? "true" : "false") <<
285 ", m_bInteractionRequested = "<< (m_bInteractionRequested ? "true" : "false"));
286 if( m_bInteractionGranted )
287 {
288 if( m_bInteractionDone )
289 xListener->approveInteraction( false );
290 else
291 xListener->approveInteraction( true );
292 return;
293 }
294
295 osl::MutexGuard aGuard( m_aMutex );
296 if( ! m_bInteractionRequested )
297 {
298 m_xSession->queryInteraction();
299 m_bInteractionRequested = true;
300 }
301 for (auto & listener: m_aListeners)
302 {
303 if( listener.m_xListener == xListener )
304 {
305 SAL_INFO("vcl.se.debug", " listener.m_xListener == xListener");
306 listener.m_bInteractionRequested = true;
307 listener.m_bInteractionDone = false;
308 }
309 }
310}
311
312void SAL_CALL VCLSession::interactionDone( const css::uno::Reference< XSessionManagerListener >& xListener )
313{
314 SAL_INFO("vcl.se", "VCLSession::interactionDone");
315
316 osl::MutexGuard aGuard( m_aMutex );
317 int nRequested = 0, nDone = 0;
318 for (auto & listener: m_aListeners)
319 {
320 if( listener.m_bInteractionRequested )
321 {
322 nRequested++;
323 if( xListener == listener.m_xListener )
324 listener.m_bInteractionDone = true;
325 }
326 if( listener.m_bInteractionDone )
327 nDone++;
328 }
329
330 SAL_INFO("vcl.se.debug", " nDone = " << nDone <<
331 ", nRequested =" << nRequested);
332 if( nDone == nRequested && nDone > 0 )
333 {
334 m_bInteractionDone = true;
335 if (m_xSession)
336 m_xSession->interactionDone();
337 }
338}
339
340void SAL_CALL VCLSession::saveDone( const css::uno::Reference< XSessionManagerListener >& xListener )
341{
342 SAL_INFO("vcl.se", "VCLSession::saveDone");
343
344 osl::MutexGuard aGuard( m_aMutex );
345
346 bool bSaveDone = true;
347 for (auto & listener: m_aListeners)
348 {
349 if( listener.m_xListener == xListener )
350 listener.m_bSaveDone = true;
351 if( ! listener.m_bSaveDone )
352 bSaveDone = false;
353 }
354
355 SAL_INFO("vcl.se.debug", " bSaveDone = " << (bSaveDone ? "true" : "false"));
356
357 if( bSaveDone && !m_bSaveDone )
358 {
359 m_bSaveDone = true;
360 if (m_xSession)
361 m_xSession->saveDone();
362 }
363}
364
365sal_Bool SAL_CALL VCLSession::cancelShutdown()
366{
367 SAL_INFO("vcl.se", "VCLSession::cancelShutdown");
368
369 return m_xSession && m_xSession->cancelShutdown();
370}
371
372void VCLSession::disposing() {
373 SAL_INFO("vcl.se", "VCLSession::disposing");
374
375 std::vector<Listener> vector;
376 {
377 osl::MutexGuard g(m_aMutex);
378 vector.swap(m_aListeners);
379 }
380 css::lang::EventObject src(static_cast<OWeakObject *>(this));
381 for (auto const & listener: vector) {
382 try {
383 listener.m_xListener->disposing(src);
384 SAL_INFO("vcl.se.debug", " call Listener disposing");
385 } catch (css::uno::RuntimeException &) {
386 TOOLS_WARN_EXCEPTION("vcl.se", "ignoring");
387 }
388 }
389}
390
391// service implementation
392
393extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
395 css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
396{
397 return cppu::acquire(new VCLSession);
398}
399
400/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual ~SalSession()
Definition: session.cxx:46
A helper class that calls Application::ReleaseSolarMutex() in its constructor and restores the mutex ...
Definition: svapp.hxx:1454
Listeners aListeners
#define TOOLS_WARN_EXCEPTION(area, stream)
std::vector< Reference< css::datatransfer::clipboard::XClipboardListener > > m_aListeners
Definition: dtranscomp.cxx:65
Reference< XTextListener > m_xListener
std::mutex m_aMutex
#define SAL_WARN_IF(condition, area, stream)
#define SAL_INFO(area, stream)
std::unique_ptr< sal_Int32[]> pData
@ ShutdownCancel
Definition: salsession.hxx:29
@ SaveRequest
Definition: salsession.hxx:28
@ Interaction
Definition: salsession.hxx:27
@ Quit
Definition: salsession.hxx:30
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_frame_VCLSessionManagerClient_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Definition: session.cxx:394
SalSessionEventType m_eType
Definition: salsession.hxx:35
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:76
unsigned char sal_Bool