1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_toolkit.hxx"
26 
27 #include "vcl/svapp.hxx"
28 #include "vos/mutex.hxx"
29 #include "sal/config.h"
30 #include "cppuhelper/factory.hxx"
31 #include "cppuhelper/implementationentry.hxx"
32 #include "cppuhelper/implbase2.hxx"
33 #include "com/sun/star/lang/XServiceInfo.hpp"
34 #include "com/sun/star/awt/XRequestCallback.hpp"
35 
36 
37 // component helper namespace
38 namespace comp_AsyncCallback {
39 
40 namespace css = ::com::sun::star;
41 
42 // component and service helper functions:
43 ::rtl::OUString SAL_CALL _getImplementationName();
44 css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames();
45 css::uno::Reference< css::uno::XInterface > SAL_CALL _create( css::uno::Reference< css::uno::XComponentContext > const & context );
46 
47 } // closing component helper namespace
48 
49 
50 
51 /// anonymous implementation namespace
52 namespace {
53 
54 namespace css = ::com::sun::star;
55 
56 class AsyncCallback:
57     public ::cppu::WeakImplHelper2<
58         css::lang::XServiceInfo,
59         css::awt::XRequestCallback>
60 {
61 public:
62     explicit AsyncCallback(css::uno::Reference< css::uno::XComponentContext > const & context);
63 
64     // ::com::sun::star::lang::XServiceInfo:
65     virtual ::rtl::OUString SAL_CALL getImplementationName() throw (css::uno::RuntimeException);
66     virtual ::sal_Bool SAL_CALL supportsService(const ::rtl::OUString & ServiceName) throw (css::uno::RuntimeException);
67     virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw (css::uno::RuntimeException);
68 
69     // ::com::sun::star::awt::XRequestCallback:
70     virtual void SAL_CALL addCallback(const css::uno::Reference< css::awt::XCallback > & xCallback, const ::com::sun::star::uno::Any & aData) throw (css::uno::RuntimeException);
71 
72 private:
73 
74     struct CallbackData
75     {
CallbackData__anon1c97f6920111::AsyncCallback::CallbackData76         CallbackData( const css::uno::Reference< css::awt::XCallback >& rCallback, const css::uno::Any& rAny ) :
77             xCallback( rCallback ), aData( rAny ) {}
78 
79         css::uno::Reference< css::awt::XCallback > xCallback;
80         css::uno::Any                              aData;
81     };
82 
83     DECL_STATIC_LINK( AsyncCallback, Notify_Impl, CallbackData* );
84 
85     AsyncCallback(AsyncCallback &); // not defined
86     void operator =(AsyncCallback &); // not defined
87 
~AsyncCallback()88     virtual ~AsyncCallback() {}
89 
90     css::uno::Reference< css::uno::XComponentContext > m_xContext;
91 };
92 
AsyncCallback(css::uno::Reference<css::uno::XComponentContext> const & context)93 AsyncCallback::AsyncCallback(css::uno::Reference< css::uno::XComponentContext > const & context) :
94     m_xContext(context)
95 {}
96 
97 // com.sun.star.uno.XServiceInfo:
getImplementationName()98 ::rtl::OUString SAL_CALL AsyncCallback::getImplementationName() throw (css::uno::RuntimeException)
99 {
100     return comp_AsyncCallback::_getImplementationName();
101 }
102 
supportsService(::rtl::OUString const & serviceName)103 ::sal_Bool SAL_CALL AsyncCallback::supportsService(::rtl::OUString const & serviceName) throw (css::uno::RuntimeException)
104 {
105     const css::uno::Sequence< ::rtl::OUString > serviceNames = comp_AsyncCallback::_getSupportedServiceNames();
106     for (::sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {
107         if (serviceNames[i] == serviceName)
108             return sal_True;
109     }
110     return sal_False;
111 }
112 
getSupportedServiceNames()113 css::uno::Sequence< ::rtl::OUString > SAL_CALL AsyncCallback::getSupportedServiceNames() throw (css::uno::RuntimeException)
114 {
115     return comp_AsyncCallback::_getSupportedServiceNames();
116 }
117 
118 // ::com::sun::star::awt::XRequestCallback:
addCallback(const css::uno::Reference<css::awt::XCallback> & xCallback,const::com::sun::star::uno::Any & aData)119 void SAL_CALL AsyncCallback::addCallback(const css::uno::Reference< css::awt::XCallback > & xCallback, const ::com::sun::star::uno::Any & aData) throw (css::uno::RuntimeException)
120 {
121     if ( Application::IsInMain() )
122     {
123         osl::Guard< vos::IMutex > aSolarGuard( Application::GetSolarMutex() );
124         CallbackData* pCallbackData = new CallbackData( xCallback, aData );
125         Application::PostUserEvent( STATIC_LINK( this, AsyncCallback, Notify_Impl ), pCallbackData );
126     }
127 }
128 
129 // private asynchronous link to call reference to the callback object
IMPL_STATIC_LINK_NOINSTANCE(AsyncCallback,Notify_Impl,CallbackData *,pCallbackData)130 IMPL_STATIC_LINK_NOINSTANCE( AsyncCallback, Notify_Impl, CallbackData*, pCallbackData )
131 {
132     try
133     {
134         // Asynchronous execution
135         // Check pointer and reference before!
136         if ( pCallbackData && pCallbackData->xCallback.is() )
137             pCallbackData->xCallback->notify( pCallbackData->aData );
138     }
139     catch ( css::uno::Exception& )
140     {
141     }
142 
143     delete pCallbackData;
144     return 0;
145 }
146 
147 } // closing anonymous implementation namespace
148 
149 
150 
151 // component helper namespace
152 namespace comp_AsyncCallback {
153 
_getImplementationName()154 ::rtl::OUString SAL_CALL _getImplementationName() {
155     return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
156         "com.sun.star.awt.comp.AsyncCallback"));
157 }
158 
_getSupportedServiceNames()159 css::uno::Sequence< ::rtl::OUString > SAL_CALL _getSupportedServiceNames()
160 {
161     css::uno::Sequence< ::rtl::OUString > s(1);
162     s[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
163         "com.sun.star.awt.AsyncCallback"));
164     return s;
165 }
166 
_create(const css::uno::Reference<css::uno::XComponentContext> & context)167 css::uno::Reference< css::uno::XInterface > SAL_CALL _create(
168     const css::uno::Reference< css::uno::XComponentContext > & context)
169         SAL_THROW((css::uno::Exception))
170 {
171     return static_cast< ::cppu::OWeakObject * >(new AsyncCallback(context));
172 }
173 
174 } // closing component helper namespace
175 
176 static ::cppu::ImplementationEntry const entries[] = {
177     { &comp_AsyncCallback::_create,
178       &comp_AsyncCallback::_getImplementationName,
179       &comp_AsyncCallback::_getSupportedServiceNames,
180       &::cppu::createSingleComponentFactory, 0, 0 },
181     { 0, 0, 0, 0, 0, 0 }
182 };
183 
comp_AsyncCallback_component_getFactory(const char * implName,void * serviceManager,void * registryKey)184 void * SAL_CALL comp_AsyncCallback_component_getFactory(
185     const char * implName, void * serviceManager, void * registryKey)
186 {
187     return ::cppu::component_getFactoryHelper(
188         implName, serviceManager, registryKey, entries);
189 }
190