1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
28 #include <cppuhelper/implbase.hxx>
29 #include <comphelper/interfacecontainer4.hxx>
30 #include <cppuhelper/supportsservice.hxx>
32 #include <vcl/svapp.hxx>
33 #include <vcl/dialoghelper.hxx>
34 #include <vcl/window.hxx>
35 #include <tools/link.hxx>
36 #include <toolkit/helper/vclunohelper.hxx>
38 using namespace ::com::sun::star
;
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
44 class ODocumentCloser
: public ::cppu::WeakImplHelper
< css::lang::XComponent
,
45 css::lang::XServiceInfo
>
48 css::uno::Reference
< css::frame::XFrame
> m_xFrame
;
49 ::comphelper::OInterfaceContainerHelper4
<lang::XEventListener
> m_aListenersContainer
; // list of listeners
54 explicit ODocumentCloser(const css::uno::Sequence
< css::uno::Any
>& aArguments
);
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
;
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
;
67 class MainThreadFrameCloserRequest
69 uno::Reference
< frame::XFrame
> m_xFrame
;
72 explicit MainThreadFrameCloserRequest( uno::Reference
< frame::XFrame
> xFrame
)
73 : m_xFrame(std::move( xFrame
))
76 DECL_STATIC_LINK( MainThreadFrameCloserRequest
, worker
, void*, void );
78 static void Start( MainThreadFrameCloserRequest
* pRequest
);
82 void MainThreadFrameCloserRequest::Start( MainThreadFrameCloserRequest
* pMTRequest
)
86 if ( Application::IsMainThread() )
88 // this is the main thread
89 worker( nullptr, pMTRequest
);
92 Application::PostUserEvent( LINK( nullptr, MainThreadFrameCloserRequest
, worker
), pMTRequest
);
97 IMPL_STATIC_LINK( MainThreadFrameCloserRequest
, worker
, void*, p
, void )
99 MainThreadFrameCloserRequest
* pMTRequest
= static_cast<MainThreadFrameCloserRequest
*>(p
);
103 if ( pMTRequest
->m_xFrame
.is() )
105 // this is the main thread, the solar mutex must be locked
106 SolarMutexGuard aGuard
;
110 uno::Reference
< awt::XWindow
> xWindow
= pMTRequest
->m_xFrame
->getContainerWindow();
111 uno::Reference
< awt::XVclWindowPeer
> xWinPeer( xWindow
, uno::UNO_QUERY
);
114 xWindow
->setVisible( false );
116 // reparent the window
117 xWinPeer
->setProperty( u
"PluginParent"_ustr
, uno::Any( sal_Int64(0) ) );
119 VclPtr
<vcl::Window
> pWindow
= VCLUnoHelper::GetWindow( xWindow
);
121 vcl::EndAllDialogs(pWindow
);
124 catch( uno::Exception
& )
126 // ignore all the errors
131 uno::Reference
< util::XCloseable
> xCloseable( pMTRequest
->m_xFrame
, uno::UNO_QUERY
);
133 xCloseable
->close( true );
135 catch( uno::Exception
& )
137 // ignore all the errors
144 ODocumentCloser::ODocumentCloser(const css::uno::Sequence
< css::uno::Any
>& aArguments
)
145 : m_bDisposed( false )
147 std::unique_lock
aGuard( m_aMutex
);
149 throw uno::RuntimeException(); // the object must be refcounted already!
151 sal_Int32 nLen
= aArguments
.getLength();
153 throw lang::IllegalArgumentException(
154 u
"Wrong count of parameters!"_ustr
,
155 uno::Reference
< uno::XInterface
>(),
158 if ( !( aArguments
[0] >>= m_xFrame
) || !m_xFrame
.is() )
159 throw lang::IllegalArgumentException(
160 u
"Nonempty reference is expected as the first argument!"_ustr
,
161 uno::Reference
< uno::XInterface
>(),
168 void SAL_CALL
ODocumentCloser::dispose()
170 std::unique_lock
aGuard( m_aMutex
);
175 lang::EventObject
aSource( getXWeak() );
176 m_aListenersContainer
.disposeAndClear( aGuard
, aSource
);
178 // TODO: trigger a main thread execution to close the frame
181 // the created object will be deleted after thread execution
182 MainThreadFrameCloserRequest
* pCloser
= new MainThreadFrameCloserRequest( m_xFrame
);
183 MainThreadFrameCloserRequest::Start( pCloser
);
190 void SAL_CALL
ODocumentCloser::addEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
192 std::unique_lock
aGuard( m_aMutex
);
194 throw lang::DisposedException(); // TODO
196 m_aListenersContainer
.addInterface( aGuard
, xListener
);
200 void SAL_CALL
ODocumentCloser::removeEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
202 std::unique_lock
aGuard( m_aMutex
);
203 m_aListenersContainer
.removeInterface( aGuard
, xListener
);
207 OUString SAL_CALL
ODocumentCloser::getImplementationName( )
209 return u
"com.sun.star.comp.embed.DocumentCloser"_ustr
;
212 sal_Bool SAL_CALL
ODocumentCloser::supportsService( const OUString
& ServiceName
)
214 return cppu::supportsService(this, ServiceName
);
217 uno::Sequence
< OUString
> SAL_CALL
ODocumentCloser::getSupportedServiceNames()
219 return { u
"com.sun.star.embed.DocumentCloser"_ustr
};
224 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
225 com_sun_star_comp_embed_DocumentCloser_get_implementation(
226 SAL_UNUSED_PARAMETER
css::uno::XComponentContext
*,
227 css::uno::Sequence
<css::uno::Any
> const &arguments
)
229 return cppu::acquire(new ODocumentCloser(arguments
));
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */