Line data Source code
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/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>
40 :
41 : using namespace ::com::sun::star;
42 :
43 : namespace {
44 :
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 >
49 : {
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
53 :
54 : bool m_bDisposed;
55 :
56 : public:
57 : explicit ODocumentCloser(const css::uno::Sequence< css::uno::Any >& aArguments);
58 : virtual ~ODocumentCloser();
59 :
60 : // XComponent
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;
64 :
65 : // XServiceInfo
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;
69 : };
70 :
71 0 : class MainThreadFrameCloserRequest
72 : {
73 : uno::Reference< frame::XFrame > m_xFrame;
74 :
75 : public:
76 0 : explicit MainThreadFrameCloserRequest( const uno::Reference< frame::XFrame >& xFrame )
77 0 : : m_xFrame( xFrame )
78 0 : {}
79 :
80 : DECL_STATIC_LINK( MainThreadFrameCloserRequest, worker, MainThreadFrameCloserRequest* );
81 :
82 : static void Start( MainThreadFrameCloserRequest* pRequest );
83 : };
84 :
85 :
86 0 : void MainThreadFrameCloserRequest::Start( MainThreadFrameCloserRequest* pMTRequest )
87 : {
88 0 : if ( pMTRequest )
89 : {
90 0 : if ( Application::GetMainThreadIdentifier() == osl::Thread::getCurrentIdentifier() )
91 : {
92 : // this is the main thread
93 0 : worker( NULL, pMTRequest );
94 : }
95 : else
96 0 : Application::PostUserEvent( LINK( NULL, MainThreadFrameCloserRequest, worker ), pMTRequest );
97 : }
98 0 : }
99 :
100 :
101 0 : IMPL_STATIC_LINK( MainThreadFrameCloserRequest, worker, MainThreadFrameCloserRequest*, pMTRequest )
102 : {
103 0 : if ( pMTRequest )
104 : {
105 0 : if ( pMTRequest->m_xFrame.is() )
106 : {
107 : // this is the main thread, the solar mutex must be locked
108 0 : SolarMutexGuard aGuard;
109 :
110 : try
111 : {
112 0 : uno::Reference< awt::XWindow > xWindow = pMTRequest->m_xFrame->getContainerWindow();
113 0 : uno::Reference< awt::XVclWindowPeer > xWinPeer( xWindow, uno::UNO_QUERY_THROW );
114 :
115 0 : xWindow->setVisible( sal_False );
116 :
117 : // reparent the window
118 0 : xWinPeer->setProperty( OUString( "PluginParent" ),
119 0 : uno::makeAny( (sal_Int64) 0 ) );
120 :
121 0 : vcl::Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
122 0 : if ( pWindow )
123 0 : Dialog::EndAllDialogs( pWindow );
124 : }
125 0 : catch( uno::Exception& )
126 : {
127 : // ignore all the errors
128 : }
129 :
130 : try
131 : {
132 0 : uno::Reference< util::XCloseable > xCloseable( pMTRequest->m_xFrame, uno::UNO_QUERY_THROW );
133 0 : xCloseable->close( sal_True );
134 : }
135 0 : catch( uno::Exception& )
136 : {
137 : // ignore all the errors
138 0 : }
139 : }
140 :
141 0 : delete pMTRequest;
142 : }
143 :
144 0 : return 0;
145 : }
146 :
147 0 : ODocumentCloser::ODocumentCloser(const css::uno::Sequence< css::uno::Any >& aArguments)
148 : : m_pListenersContainer( NULL )
149 0 : , m_bDisposed( false )
150 : {
151 0 : ::osl::MutexGuard aGuard( m_aMutex );
152 0 : if ( !m_refCount )
153 0 : throw uno::RuntimeException(); // the object must be refcounted already!
154 :
155 0 : sal_Int32 nLen = aArguments.getLength();
156 0 : if ( nLen != 1 )
157 : throw lang::IllegalArgumentException(
158 : OUString("Wrong count of parameters!" ),
159 : uno::Reference< uno::XInterface >(),
160 0 : 0 );
161 :
162 0 : 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 >(),
166 0 : 0 );
167 0 : }
168 :
169 :
170 0 : ODocumentCloser::~ODocumentCloser()
171 : {
172 0 : if ( m_pListenersContainer )
173 : {
174 0 : delete m_pListenersContainer;
175 0 : m_pListenersContainer = NULL;
176 : }
177 0 : }
178 :
179 : // XComponent
180 :
181 0 : void SAL_CALL ODocumentCloser::dispose()
182 : throw (uno::RuntimeException, std::exception)
183 : {
184 0 : ::osl::MutexGuard aGuard( m_aMutex );
185 :
186 0 : if ( m_bDisposed )
187 0 : throw lang::DisposedException();
188 :
189 0 : lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
190 0 : if ( m_pListenersContainer )
191 0 : m_pListenersContainer->disposeAndClear( aSource );
192 :
193 : // TODO: trigger a main thread execution to close the frame
194 0 : if ( m_xFrame.is() )
195 : {
196 : // the created object will be deleted after thread execution
197 0 : MainThreadFrameCloserRequest* pCloser = new MainThreadFrameCloserRequest( m_xFrame );
198 0 : MainThreadFrameCloserRequest::Start( pCloser );
199 : }
200 :
201 0 : m_bDisposed = true;
202 0 : }
203 :
204 :
205 0 : void SAL_CALL ODocumentCloser::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
206 : throw (uno::RuntimeException, std::exception)
207 : {
208 0 : ::osl::MutexGuard aGuard( m_aMutex );
209 0 : if ( m_bDisposed )
210 0 : throw lang::DisposedException(); // TODO
211 :
212 0 : if ( !m_pListenersContainer )
213 0 : m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
214 :
215 0 : m_pListenersContainer->addInterface( xListener );
216 0 : }
217 :
218 :
219 0 : void SAL_CALL ODocumentCloser::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
220 : throw (uno::RuntimeException, std::exception)
221 : {
222 0 : ::osl::MutexGuard aGuard( m_aMutex );
223 0 : if ( m_pListenersContainer )
224 0 : m_pListenersContainer->removeInterface( xListener );
225 0 : }
226 :
227 : // XServiceInfo
228 0 : OUString SAL_CALL ODocumentCloser::getImplementationName( )
229 : throw (uno::RuntimeException, std::exception)
230 : {
231 0 : return OUString( "com.sun.star.comp.embed.DocumentCloser" );
232 : }
233 :
234 0 : sal_Bool SAL_CALL ODocumentCloser::supportsService( const OUString& ServiceName )
235 : throw (uno::RuntimeException, std::exception)
236 : {
237 0 : return cppu::supportsService(this, ServiceName);
238 : }
239 :
240 0 : uno::Sequence< OUString > SAL_CALL ODocumentCloser::getSupportedServiceNames()
241 : throw (uno::RuntimeException, std::exception)
242 : {
243 0 : const OUString aServiceName( "com.sun.star.embed.DocumentCloser" );
244 0 : return uno::Sequence< OUString >( &aServiceName, 1 );
245 : }
246 :
247 : }
248 :
249 : extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
250 0 : com_sun_star_comp_embed_DocumentCloser_get_implementation(
251 : SAL_UNUSED_PARAMETER css::uno::XComponentContext *,
252 : css::uno::Sequence<css::uno::Any> const &arguments)
253 : {
254 0 : return cppu::acquire(new ODocumentCloser(arguments));
255 : }
256 :
257 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|