1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_embeddedobj.hxx"
30 
31 #include "xexecutor.hxx"
32 #include <vcl/svapp.hxx>
33 #include <osl/thread.hxx>
34 
35 using namespace ::com::sun::star;
36 
37 void MainThreadExecutor_Impl::execute()
38 {
39 	Application::PostUserEvent( LINK( this, MainThreadExecutor_Impl, executor ), NULL );
40 }
41 
42 IMPL_LINK( MainThreadExecutor_Impl, executor, void*, pDummyParam )
43 {
44 	if ( m_xJob.is() )
45 	{
46 		try {
47 			m_xJob->execute( m_aArgs );
48 		} catch( uno::Exception& ) {}
49 	}
50 
51 	m_bExecuted = sal_True;
52 	delete this;
53 
54 	return 0;
55 }
56 
57 MainThreadExecutor_Impl::MainThreadExecutor_Impl( const uno::Reference< task::XJob >& xJob,
58 												  const uno::Sequence< beans::NamedValue >& aArguments )
59 : m_xJob( xJob )
60 , m_aArgs( aArguments )
61 , m_bExecuted( sal_False )
62 {
63 }
64 
65 //-------------------------------------------------------------------------
66 uno::Any SAL_CALL UNOMainThreadExecutor::execute( const uno::Sequence< beans::NamedValue >& aArguments )
67 	throw ( lang::IllegalArgumentException,
68 			uno::Exception,
69 			uno::RuntimeException )
70 {
71 	uno::Reference< task::XJob > xJob;
72 
73 	if ( aArguments.getLength() > 0 && aArguments[0].Name.equalsAscii( "JobToExecute" ) )
74 		aArguments[0].Value >>= xJob;
75 
76 	if ( !xJob.is() )
77 		throw lang::IllegalArgumentException();
78 
79 	uno::Sequence< beans::NamedValue > aArgsForJob;
80 	if ( aArguments.getLength() > 1 )
81 		aArgsForJob = uno::Sequence< beans::NamedValue >( aArguments.getConstArray() + 1, aArguments.getLength() - 1 );
82 
83 	MainThreadExecutor_Impl* pExecutor = new MainThreadExecutor_Impl( xJob, aArgsForJob );
84 	pExecutor->execute();
85 
86 	// it is not a main thread, so it can be blocked
87 	// while( !pExecutor->isExecuted() )
88 	//	::osl::Thread::yield();
89 
90 	// TODO: implement transfering of the return values and exceptions
91 
92 	return uno::Any();
93 }
94 
95 //-------------------------------------------------------------------------
96 uno::Sequence< ::rtl::OUString > SAL_CALL UNOMainThreadExecutor::impl_staticGetSupportedServiceNames()
97 {
98     uno::Sequence< ::rtl::OUString > aRet(1);
99     aRet[0] = ::rtl::OUString::createFromAscii( "com.sun.star.comp.thread.MainThreadExecutor" );
100     return aRet;
101 }
102 
103 //-------------------------------------------------------------------------
104 ::rtl::OUString SAL_CALL UNOMainThreadExecutor::impl_staticGetImplementationName()
105 {
106     return ::rtl::OUString::createFromAscii( "com.sun.star.comp.thread.MainThreadExecutor" );
107 }
108 
109 //-------------------------------------------------------------------------
110 uno::Reference< uno::XInterface > SAL_CALL UNOMainThreadExecutor::impl_staticCreateSelfInstance(
111 			const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
112 {
113 	return uno::Reference< uno::XInterface >( *new UNOMainThreadExecutor( xServiceManager ) );
114 }
115 
116 //-------------------------------------------------------------------------
117 ::rtl::OUString SAL_CALL UNOMainThreadExecutor::getImplementationName()
118 	throw ( uno::RuntimeException )
119 {
120 	return impl_staticGetImplementationName();
121 }
122 
123 //-------------------------------------------------------------------------
124 sal_Bool SAL_CALL UNOMainThreadExecutor::supportsService( const ::rtl::OUString& ServiceName )
125 	throw ( uno::RuntimeException )
126 {
127 	uno::Sequence< ::rtl::OUString > aSeq = impl_staticGetSupportedServiceNames();
128 
129 	for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
130     	if ( ServiceName.compareTo( aSeq[nInd] ) == 0 )
131         	return sal_True;
132 
133 	return sal_False;
134 }
135 
136 //-------------------------------------------------------------------------
137 uno::Sequence< ::rtl::OUString > SAL_CALL UNOMainThreadExecutor::getSupportedServiceNames()
138 	throw ( uno::RuntimeException )
139 {
140 	return impl_staticGetSupportedServiceNames();
141 }
142 
143