LibreOffice Module toolkit (master) 1
documentcloser.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 <com/sun/star/uno/XComponentContext.hpp>
21#include <com/sun/star/util/XCloseable.hpp>
22#include <com/sun/star/lang/DisposedException.hpp>
23#include <com/sun/star/lang/IllegalArgumentException.hpp>
24#include <com/sun/star/lang/XComponent.hpp>
25#include <com/sun/star/lang/XServiceInfo.hpp>
26#include <com/sun/star/frame/XFrame.hpp>
27#include <com/sun/star/awt/XVclWindowPeer.hpp>
31#include <utility>
32#include <vcl/svapp.hxx>
33#include <vcl/dialoghelper.hxx>
34#include <vcl/window.hxx>
35#include <tools/link.hxx>
37
38using namespace ::com::sun::star;
39
40namespace {
41
42// the service is implemented as a wrapper to be able to die by refcount
43// the disposing mechanics is required for java related scenarios
44class ODocumentCloser : public ::cppu::WeakImplHelper< css::lang::XComponent,
45 css::lang::XServiceInfo >
46{
47 std::mutex m_aMutex;
48 css::uno::Reference< css::frame::XFrame > m_xFrame;
49 ::comphelper::OInterfaceContainerHelper4<lang::XEventListener> m_aListenersContainer; // list of listeners
50
51 bool m_bDisposed;
52
53public:
54 explicit ODocumentCloser(const css::uno::Sequence< css::uno::Any >& aArguments);
55
56// XComponent
57 virtual void SAL_CALL dispose() override;
58 virtual void SAL_CALL addEventListener( const css::uno::Reference< css::lang::XEventListener >& xListener ) override;
59 virtual void SAL_CALL removeEventListener( const css::uno::Reference< css::lang::XEventListener >& aListener ) override;
60
61// XServiceInfo
62 virtual OUString SAL_CALL getImplementationName( ) override;
63 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
64 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override;
65};
66
67class MainThreadFrameCloserRequest
68{
69 uno::Reference< frame::XFrame > m_xFrame;
70
71 public:
72 explicit MainThreadFrameCloserRequest( uno::Reference< frame::XFrame > xFrame )
73 : m_xFrame(std::move( xFrame ))
74 {}
75
76 DECL_STATIC_LINK( MainThreadFrameCloserRequest, worker, void*, void );
77
78 static void Start( MainThreadFrameCloserRequest* pRequest );
79};
80
81
82void MainThreadFrameCloserRequest::Start( MainThreadFrameCloserRequest* pMTRequest )
83{
84 if ( pMTRequest )
85 {
87 {
88 // this is the main thread
89 worker( nullptr, pMTRequest );
90 }
91 else
92 Application::PostUserEvent( LINK( nullptr, MainThreadFrameCloserRequest, worker ), pMTRequest );
93 }
94}
95
96
97IMPL_STATIC_LINK( MainThreadFrameCloserRequest, worker, void*, p, void )
98{
99 MainThreadFrameCloserRequest* pMTRequest = static_cast<MainThreadFrameCloserRequest*>(p);
100 if ( !pMTRequest )
101 return;
102
103 if ( pMTRequest->m_xFrame.is() )
104 {
105 // this is the main thread, the solar mutex must be locked
106 SolarMutexGuard aGuard;
107
108 try
109 {
110 uno::Reference< awt::XWindow > xWindow = pMTRequest->m_xFrame->getContainerWindow();
111 uno::Reference< awt::XVclWindowPeer > xWinPeer( xWindow, uno::UNO_QUERY_THROW );
112
113 xWindow->setVisible( false );
114
115 // reparent the window
116 xWinPeer->setProperty( "PluginParent", uno::Any( sal_Int64(0) ) );
117
118 VclPtr<vcl::Window> pWindow = VCLUnoHelper::GetWindow( xWindow );
119 if (pWindow)
120 vcl::EndAllDialogs(pWindow);
121 }
122 catch( uno::Exception& )
123 {
124 // ignore all the errors
125 }
126
127 try
128 {
129 uno::Reference< util::XCloseable > xCloseable( pMTRequest->m_xFrame, uno::UNO_QUERY_THROW );
130 xCloseable->close( true );
131 }
132 catch( uno::Exception& )
133 {
134 // ignore all the errors
135 }
136 }
137
138 delete pMTRequest;
139}
140
141ODocumentCloser::ODocumentCloser(const css::uno::Sequence< css::uno::Any >& aArguments)
142: m_bDisposed( false )
143{
144 std::unique_lock aGuard( m_aMutex );
145 if ( !m_refCount )
146 throw uno::RuntimeException(); // the object must be refcounted already!
147
148 sal_Int32 nLen = aArguments.getLength();
149 if ( nLen != 1 )
150 throw lang::IllegalArgumentException(
151 "Wrong count of parameters!",
152 uno::Reference< uno::XInterface >(),
153 0 );
154
155 if ( !( aArguments[0] >>= m_xFrame ) || !m_xFrame.is() )
156 throw lang::IllegalArgumentException(
157 "Nonempty reference is expected as the first argument!",
158 uno::Reference< uno::XInterface >(),
159 0 );
160}
161
162
163// XComponent
164
165void SAL_CALL ODocumentCloser::dispose()
166{
167 std::unique_lock aGuard( m_aMutex );
168
169 if ( m_bDisposed )
170 return;
171
172 lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
173 m_aListenersContainer.disposeAndClear( aGuard, aSource );
174
175 // TODO: trigger a main thread execution to close the frame
176 if ( m_xFrame.is() )
177 {
178 // the created object will be deleted after thread execution
179 MainThreadFrameCloserRequest* pCloser = new MainThreadFrameCloserRequest( m_xFrame );
180 MainThreadFrameCloserRequest::Start( pCloser );
181 }
182
183 m_bDisposed = true;
184}
185
186
187void SAL_CALL ODocumentCloser::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
188{
189 std::unique_lock aGuard( m_aMutex );
190 if ( m_bDisposed )
191 throw lang::DisposedException(); // TODO
192
193 m_aListenersContainer.addInterface( aGuard, xListener );
194}
195
196
197void SAL_CALL ODocumentCloser::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
198{
199 std::unique_lock aGuard( m_aMutex );
200 m_aListenersContainer.removeInterface( aGuard, xListener );
201}
202
203// XServiceInfo
204OUString SAL_CALL ODocumentCloser::getImplementationName( )
205{
206 return "com.sun.star.comp.embed.DocumentCloser";
207}
208
209sal_Bool SAL_CALL ODocumentCloser::supportsService( const OUString& ServiceName )
210{
211 return cppu::supportsService(this, ServiceName);
212}
213
214uno::Sequence< OUString > SAL_CALL ODocumentCloser::getSupportedServiceNames()
215{
216 return { "com.sun.star.embed.DocumentCloser" };
217}
218
219}
220
221extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
223 SAL_UNUSED_PARAMETER css::uno::XComponentContext *,
224 css::uno::Sequence<css::uno::Any> const &arguments)
225{
226 return cppu::acquire(new ODocumentCloser(arguments));
227}
228
229/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::lang::XComponent > m_xFrame
static bool IsMainThread()
static ImplSVEvent * PostUserEvent(const Link< void *, void > &rLink, void *pCaller=nullptr, bool bReferenceLink=false)
static vcl::Window * GetWindow(const css::uno::Reference< css::awt::XWindow > &rxWindow)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_embed_DocumentCloser_get_implementation(SAL_UNUSED_PARAMETER css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &arguments)
bool m_bDisposed
std::mutex m_aMutex
Sequence< PropertyValue > aArguments
void * p
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
void dispose()
void EndAllDialogs(vcl::Window const *pParent)
IMPL_STATIC_LINK(SwRetrievedInputStreamDataManager, LinkedInputStreamReady, void *, p, void)
Reference< XFrame > xFrame
unsigned char sal_Bool