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