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_embeddedobj.hxx"
26 
27 #include "xexecutor.hxx"
28 #include <vcl/svapp.hxx>
29 #include <osl/thread.hxx>
30 
31 using namespace ::com::sun::star;
32 
execute()33 void MainThreadExecutor_Impl::execute()
34 {
35 	Application::PostUserEvent( LINK( this, MainThreadExecutor_Impl, executor ), NULL );
36 }
37 
IMPL_LINK(MainThreadExecutor_Impl,executor,void *,pDummyParam)38 IMPL_LINK( MainThreadExecutor_Impl, executor, void*, pDummyParam )
39 {
40 	if ( m_xJob.is() )
41 	{
42 		try {
43 			m_xJob->execute( m_aArgs );
44 		} catch( uno::Exception& ) {}
45 	}
46 
47 	m_bExecuted = sal_True;
48 	delete this;
49 
50 	return 0;
51 }
52 
MainThreadExecutor_Impl(const uno::Reference<task::XJob> & xJob,const uno::Sequence<beans::NamedValue> & aArguments)53 MainThreadExecutor_Impl::MainThreadExecutor_Impl( const uno::Reference< task::XJob >& xJob,
54 												  const uno::Sequence< beans::NamedValue >& aArguments )
55 : m_xJob( xJob )
56 , m_aArgs( aArguments )
57 , m_bExecuted( sal_False )
58 {
59 }
60 
61 //-------------------------------------------------------------------------
execute(const uno::Sequence<beans::NamedValue> & aArguments)62 uno::Any SAL_CALL UNOMainThreadExecutor::execute( const uno::Sequence< beans::NamedValue >& aArguments )
63 	throw ( lang::IllegalArgumentException,
64 			uno::Exception,
65 			uno::RuntimeException )
66 {
67 	uno::Reference< task::XJob > xJob;
68 
69 	if ( aArguments.getLength() > 0 && aArguments[0].Name.equalsAscii( "JobToExecute" ) )
70 		aArguments[0].Value >>= xJob;
71 
72 	if ( !xJob.is() )
73 		throw lang::IllegalArgumentException();
74 
75 	uno::Sequence< beans::NamedValue > aArgsForJob;
76 	if ( aArguments.getLength() > 1 )
77 		aArgsForJob = uno::Sequence< beans::NamedValue >( aArguments.getConstArray() + 1, aArguments.getLength() - 1 );
78 
79 	MainThreadExecutor_Impl* pExecutor = new MainThreadExecutor_Impl( xJob, aArgsForJob );
80 	pExecutor->execute();
81 
82 	// it is not a main thread, so it can be blocked
83 	// while( !pExecutor->isExecuted() )
84 	//	::osl::Thread::yield();
85 
86 	// TODO: implement transferring of the return values and exceptions
87 
88 	return uno::Any();
89 }
90 
91 //-------------------------------------------------------------------------
impl_staticGetSupportedServiceNames()92 uno::Sequence< ::rtl::OUString > SAL_CALL UNOMainThreadExecutor::impl_staticGetSupportedServiceNames()
93 {
94     uno::Sequence< ::rtl::OUString > aRet(1);
95     aRet[0] = ::rtl::OUString::createFromAscii( "com.sun.star.comp.thread.MainThreadExecutor" );
96     return aRet;
97 }
98 
99 //-------------------------------------------------------------------------
impl_staticGetImplementationName()100 ::rtl::OUString SAL_CALL UNOMainThreadExecutor::impl_staticGetImplementationName()
101 {
102     return ::rtl::OUString::createFromAscii( "com.sun.star.comp.thread.MainThreadExecutor" );
103 }
104 
105 //-------------------------------------------------------------------------
impl_staticCreateSelfInstance(const uno::Reference<lang::XMultiServiceFactory> & xServiceManager)106 uno::Reference< uno::XInterface > SAL_CALL UNOMainThreadExecutor::impl_staticCreateSelfInstance(
107 			const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
108 {
109 	return uno::Reference< uno::XInterface >( *new UNOMainThreadExecutor( xServiceManager ) );
110 }
111 
112 //-------------------------------------------------------------------------
getImplementationName()113 ::rtl::OUString SAL_CALL UNOMainThreadExecutor::getImplementationName()
114 	throw ( uno::RuntimeException )
115 {
116 	return impl_staticGetImplementationName();
117 }
118 
119 //-------------------------------------------------------------------------
supportsService(const::rtl::OUString & ServiceName)120 sal_Bool SAL_CALL UNOMainThreadExecutor::supportsService( const ::rtl::OUString& ServiceName )
121 	throw ( uno::RuntimeException )
122 {
123 	uno::Sequence< ::rtl::OUString > aSeq = impl_staticGetSupportedServiceNames();
124 
125 	for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
126     	if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
127         	return sal_True;
128 
129 	return sal_False;
130 }
131 
132 //-------------------------------------------------------------------------
getSupportedServiceNames()133 uno::Sequence< ::rtl::OUString > SAL_CALL UNOMainThreadExecutor::getSupportedServiceNames()
134 	throw ( uno::RuntimeException )
135 {
136 	return impl_staticGetSupportedServiceNames();
137 }
138 
139