xref: /trunk/main/embeddedobj/test/MainThreadExecutor/xexecutor.cxx (revision 45c5a00162811bd03b87fcb4fe9996782f2b59ec)
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 
33 void MainThreadExecutor_Impl::execute()
34 {
35     Application::PostUserEvent( LINK( this, MainThreadExecutor_Impl, executor ), NULL );
36 }
37 
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 
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 //-------------------------------------------------------------------------
62 uno::Any SAL_CALL UNOMainThreadExecutor::execute( const uno::Sequence< beans::NamedValue >& aArguments )
63 {
64     uno::Reference< task::XJob > xJob;
65 
66     if ( aArguments.getLength() > 0 && aArguments[0].Name.equalsAscii( "JobToExecute" ) )
67         aArguments[0].Value >>= xJob;
68 
69     if ( !xJob.is() )
70         throw lang::IllegalArgumentException();
71 
72     uno::Sequence< beans::NamedValue > aArgsForJob;
73     if ( aArguments.getLength() > 1 )
74         aArgsForJob = uno::Sequence< beans::NamedValue >( aArguments.getConstArray() + 1, aArguments.getLength() - 1 );
75 
76     MainThreadExecutor_Impl* pExecutor = new MainThreadExecutor_Impl( xJob, aArgsForJob );
77     pExecutor->execute();
78 
79     // it is not a main thread, so it can be blocked
80     // while( !pExecutor->isExecuted() )
81     //  ::osl::Thread::yield();
82 
83     // TODO: implement transferring of the return values and exceptions
84 
85     return uno::Any();
86 }
87 
88 //-------------------------------------------------------------------------
89 uno::Sequence< ::rtl::OUString > SAL_CALL UNOMainThreadExecutor::impl_staticGetSupportedServiceNames()
90 {
91     uno::Sequence< ::rtl::OUString > aRet(1);
92     aRet[0] = ::rtl::OUString::createFromAscii( "com.sun.star.comp.thread.MainThreadExecutor" );
93     return aRet;
94 }
95 
96 //-------------------------------------------------------------------------
97 ::rtl::OUString SAL_CALL UNOMainThreadExecutor::impl_staticGetImplementationName()
98 {
99     return ::rtl::OUString::createFromAscii( "com.sun.star.comp.thread.MainThreadExecutor" );
100 }
101 
102 //-------------------------------------------------------------------------
103 uno::Reference< uno::XInterface > SAL_CALL UNOMainThreadExecutor::impl_staticCreateSelfInstance(
104             const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
105 {
106     return uno::Reference< uno::XInterface >( *new UNOMainThreadExecutor( xServiceManager ) );
107 }
108 
109 //-------------------------------------------------------------------------
110 ::rtl::OUString SAL_CALL UNOMainThreadExecutor::getImplementationName()
111 {
112     return impl_staticGetImplementationName();
113 }
114 
115 //-------------------------------------------------------------------------
116 sal_Bool SAL_CALL UNOMainThreadExecutor::supportsService( const ::rtl::OUString& ServiceName )
117 {
118     uno::Sequence< ::rtl::OUString > aSeq = impl_staticGetSupportedServiceNames();
119 
120     for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
121         if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
122             return sal_True;
123 
124     return sal_False;
125 }
126 
127 //-------------------------------------------------------------------------
128 uno::Sequence< ::rtl::OUString > SAL_CALL UNOMainThreadExecutor::getSupportedServiceNames()
129 {
130     return impl_staticGetSupportedServiceNames();
131 }
132