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/XCloseBroadcaster.hpp>
22 #include <com/sun/star/util/XCloseable.hpp>
23 #include <com/sun/star/lang/DisposedException.hpp>
24 #include <com/sun/star/lang/IllegalArgumentException.hpp>
25 #include <com/sun/star/lang/XComponent.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <com/sun/star/frame/XFrame.hpp>
28 #include <com/sun/star/awt/XVclWindowPeer.hpp>
29 #include <comphelper/processfactory.hxx>
30 #include <cppuhelper/implbase2.hxx>
31 #include <cppuhelper/interfacecontainer.h>
32 #include <cppuhelper/supportsservice.hxx>
33 #include <osl/mutex.hxx>
34 #include <osl/thread.hxx>
35 #include <rtl/ref.hxx>
36 #include <vcl/svapp.hxx>
37 #include <vcl/dialog.hxx>
38 #include <tools/link.hxx>
39 #include <toolkit/helper/vclunohelper.hxx>
41 using namespace ::com::sun::star
;
45 // the service is implemented as a wrapper to be able to die by refcount
46 // the disposing mechanics is required for java related scenarios
47 class ODocumentCloser
: public ::cppu::WeakImplHelper2
< ::com::sun::star::lang::XComponent
,
48 ::com::sun::star::lang::XServiceInfo
>
50 ::osl::Mutex m_aMutex
;
51 ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XFrame
> m_xFrame
;
52 ::cppu::OInterfaceContainerHelper
* m_pListenersContainer
; // list of listeners
57 ODocumentCloser(const css::uno::Sequence
< css::uno::Any
>& aArguments
);
58 virtual ~ODocumentCloser();
61 virtual void SAL_CALL
dispose() throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
62 virtual void SAL_CALL
addEventListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
>& xListener
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
63 virtual void SAL_CALL
removeEventListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XEventListener
>& aListener
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
66 virtual OUString SAL_CALL
getImplementationName( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
67 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
68 virtual ::com::sun::star::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
71 class MainThreadFrameCloserRequest
73 uno::Reference
< frame::XFrame
> m_xFrame
;
76 MainThreadFrameCloserRequest( const uno::Reference
< frame::XFrame
>& xFrame
)
80 DECL_STATIC_LINK( MainThreadFrameCloserRequest
, worker
, MainThreadFrameCloserRequest
* );
82 static void Start( MainThreadFrameCloserRequest
* pRequest
);
86 void MainThreadFrameCloserRequest::Start( MainThreadFrameCloserRequest
* pMTRequest
)
90 if ( Application::GetMainThreadIdentifier() == osl::Thread::getCurrentIdentifier() )
92 // this is the main thread
93 worker( NULL
, pMTRequest
);
96 Application::PostUserEvent( LINK( NULL
, MainThreadFrameCloserRequest
, worker
), pMTRequest
);
101 IMPL_STATIC_LINK( MainThreadFrameCloserRequest
, worker
, MainThreadFrameCloserRequest
*, pMTRequest
)
105 if ( pMTRequest
->m_xFrame
.is() )
107 // this is the main thread, the solar mutex must be locked
108 SolarMutexGuard aGuard
;
112 uno::Reference
< awt::XWindow
> xWindow
= pMTRequest
->m_xFrame
->getContainerWindow();
113 uno::Reference
< awt::XVclWindowPeer
> xWinPeer( xWindow
, uno::UNO_QUERY_THROW
);
115 xWindow
->setVisible( sal_False
);
117 // reparent the window
118 xWinPeer
->setProperty( OUString( "PluginParent" ),
119 uno::makeAny( (sal_Int64
) 0 ) );
121 vcl::Window
* pWindow
= VCLUnoHelper::GetWindow( xWindow
);
123 Dialog::EndAllDialogs( pWindow
);
125 catch( uno::Exception
& )
127 // ignore all the errors
132 uno::Reference
< util::XCloseable
> xCloseable( pMTRequest
->m_xFrame
, uno::UNO_QUERY_THROW
);
133 xCloseable
->close( sal_True
);
135 catch( uno::Exception
& )
137 // ignore all the errors
147 ODocumentCloser::ODocumentCloser(const css::uno::Sequence
< css::uno::Any
>& aArguments
)
148 : m_pListenersContainer( NULL
)
149 , m_bDisposed( false )
151 ::osl::MutexGuard
aGuard( m_aMutex
);
153 throw uno::RuntimeException(); // the object must be refcounted already!
155 sal_Int32 nLen
= aArguments
.getLength();
157 throw lang::IllegalArgumentException(
158 OUString("Wrong count of parameters!" ),
159 uno::Reference
< uno::XInterface
>(),
162 if ( !( aArguments
[0] >>= m_xFrame
) || !m_xFrame
.is() )
163 throw lang::IllegalArgumentException(
164 OUString("Nonempty reference is expected as the first argument!" ),
165 uno::Reference
< uno::XInterface
>(),
170 ODocumentCloser::~ODocumentCloser()
172 if ( m_pListenersContainer
)
174 delete m_pListenersContainer
;
175 m_pListenersContainer
= NULL
;
181 void SAL_CALL
ODocumentCloser::dispose()
182 throw (uno::RuntimeException
, std::exception
)
184 ::osl::MutexGuard
aGuard( m_aMutex
);
187 throw lang::DisposedException();
189 lang::EventObject
aSource( static_cast< ::cppu::OWeakObject
* >(this) );
190 if ( m_pListenersContainer
)
191 m_pListenersContainer
->disposeAndClear( aSource
);
193 // TODO: trigger a main thread execution to close the frame
196 // the created object will be deleted after thread execution
197 MainThreadFrameCloserRequest
* pCloser
= new MainThreadFrameCloserRequest( m_xFrame
);
198 MainThreadFrameCloserRequest::Start( pCloser
);
205 void SAL_CALL
ODocumentCloser::addEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
206 throw (uno::RuntimeException
, std::exception
)
208 ::osl::MutexGuard
aGuard( m_aMutex
);
210 throw lang::DisposedException(); // TODO
212 if ( !m_pListenersContainer
)
213 m_pListenersContainer
= new ::cppu::OInterfaceContainerHelper( m_aMutex
);
215 m_pListenersContainer
->addInterface( xListener
);
219 void SAL_CALL
ODocumentCloser::removeEventListener( const uno::Reference
< lang::XEventListener
>& xListener
)
220 throw (uno::RuntimeException
, std::exception
)
222 ::osl::MutexGuard
aGuard( m_aMutex
);
223 if ( m_pListenersContainer
)
224 m_pListenersContainer
->removeInterface( xListener
);
228 OUString SAL_CALL
ODocumentCloser::getImplementationName( )
229 throw (uno::RuntimeException
, std::exception
)
231 return OUString( "com.sun.star.comp.embed.DocumentCloser" );
234 sal_Bool SAL_CALL
ODocumentCloser::supportsService( const OUString
& ServiceName
)
235 throw (uno::RuntimeException
, std::exception
)
237 return cppu::supportsService(this, ServiceName
);
240 uno::Sequence
< OUString
> SAL_CALL
ODocumentCloser::getSupportedServiceNames()
241 throw (uno::RuntimeException
, std::exception
)
243 const OUString
aServiceName( "com.sun.star.embed.DocumentCloser" );
244 return uno::Sequence
< OUString
>( &aServiceName
, 1 );
249 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
250 com_sun_star_comp_embed_DocumentCloser_get_implementation(
251 SAL_UNUSED_PARAMETER
css::uno::XComponentContext
*,
252 css::uno::Sequence
<css::uno::Any
> const &arguments
)
254 return cppu::acquire(new ODocumentCloser(arguments
));
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */