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