1*2c696243SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*2c696243SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*2c696243SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*2c696243SAndrew Rist * distributed with this work for additional information
6*2c696243SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*2c696243SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*2c696243SAndrew Rist * "License"); you may not use this file except in compliance
9*2c696243SAndrew Rist * with the License. You may obtain a copy of the License at
10*2c696243SAndrew Rist *
11*2c696243SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*2c696243SAndrew Rist *
13*2c696243SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*2c696243SAndrew Rist * software distributed under the License is distributed on an
15*2c696243SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2c696243SAndrew Rist * KIND, either express or implied. See the License for the
17*2c696243SAndrew Rist * specific language governing permissions and limitations
18*2c696243SAndrew Rist * under the License.
19*2c696243SAndrew Rist *
20*2c696243SAndrew Rist *************************************************************/
21*2c696243SAndrew Rist
22*2c696243SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_scripting.hxx"
26cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
27cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
28cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
29cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
30cdf0e10cSrcweir #include <util/scriptingconstants.hxx>
31cdf0e10cSrcweir #include <util/util.hxx>
32cdf0e10cSrcweir #include <util/MiscUtils.hxx>
33cdf0e10cSrcweir
34cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
35cdf0e10cSrcweir #include <com/sun/star/util/XMacroExpander.hpp>
36cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
37cdf0e10cSrcweir
38cdf0e10cSrcweir #include <com/sun/star/script/browse/BrowseNodeTypes.hpp>
39cdf0e10cSrcweir
40cdf0e10cSrcweir #include "MasterScriptProvider.hxx"
41cdf0e10cSrcweir #include "ActiveMSPList.hxx"
42cdf0e10cSrcweir
43cdf0e10cSrcweir #include <tools/diagnose_ex.h>
44cdf0e10cSrcweir
45cdf0e10cSrcweir using namespace com::sun::star;
46cdf0e10cSrcweir using namespace com::sun::star::uno;
47cdf0e10cSrcweir using namespace com::sun::star::script;
48cdf0e10cSrcweir using namespace ::sf_misc;
49cdf0e10cSrcweir
50cdf0e10cSrcweir namespace func_provider
51cdf0e10cSrcweir {
52cdf0e10cSrcweir
ActiveMSPList(const Reference<XComponentContext> & xContext)53cdf0e10cSrcweir ActiveMSPList::ActiveMSPList( const Reference< XComponentContext > & xContext ) : m_xContext( xContext )
54cdf0e10cSrcweir {
55cdf0e10cSrcweir userDirString = ::rtl::OUString::createFromAscii("user");
56cdf0e10cSrcweir shareDirString = ::rtl::OUString::createFromAscii("share");
57cdf0e10cSrcweir bundledDirString = ::rtl::OUString::createFromAscii("bundled");
58cdf0e10cSrcweir }
59cdf0e10cSrcweir
~ActiveMSPList()60cdf0e10cSrcweir ActiveMSPList::~ActiveMSPList()
61cdf0e10cSrcweir {
62cdf0e10cSrcweir }
63cdf0e10cSrcweir
64cdf0e10cSrcweir Reference< provider::XScriptProvider >
createNewMSP(const uno::Any & context)65cdf0e10cSrcweir ActiveMSPList::createNewMSP( const uno::Any& context )
66cdf0e10cSrcweir {
67cdf0e10cSrcweir ::rtl::OUString serviceName = ::rtl::OUString::createFromAscii("com.sun.star.script.provider.MasterScriptProvider");
68cdf0e10cSrcweir Sequence< Any > args( &context, 1 );
69cdf0e10cSrcweir
70cdf0e10cSrcweir Reference< provider::XScriptProvider > msp(
71cdf0e10cSrcweir m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
72cdf0e10cSrcweir serviceName, args, m_xContext ), UNO_QUERY );
73cdf0e10cSrcweir return msp;
74cdf0e10cSrcweir }
75cdf0e10cSrcweir
76cdf0e10cSrcweir Reference< provider::XScriptProvider >
getMSPFromAnyContext(const Any & aContext)77cdf0e10cSrcweir ActiveMSPList::getMSPFromAnyContext( const Any& aContext )
78cdf0e10cSrcweir SAL_THROW(( lang::IllegalArgumentException, RuntimeException ))
79cdf0e10cSrcweir {
80cdf0e10cSrcweir Reference< provider::XScriptProvider > msp;
81cdf0e10cSrcweir ::rtl::OUString sContext;
82cdf0e10cSrcweir if ( aContext >>= sContext )
83cdf0e10cSrcweir {
84cdf0e10cSrcweir msp = getMSPFromStringContext( sContext );
85cdf0e10cSrcweir return msp;
86cdf0e10cSrcweir }
87cdf0e10cSrcweir
88cdf0e10cSrcweir Reference< frame::XModel > xModel( aContext, UNO_QUERY );
89cdf0e10cSrcweir
90cdf0e10cSrcweir Reference< document::XScriptInvocationContext > xScriptContext( aContext, UNO_QUERY );
91cdf0e10cSrcweir if ( xScriptContext.is() )
92cdf0e10cSrcweir {
93cdf0e10cSrcweir try
94cdf0e10cSrcweir {
95cdf0e10cSrcweir // the component supports executing scripts embedded in a - possibly foreign document.
96cdf0e10cSrcweir // Check whether this other document its the component itself.
97cdf0e10cSrcweir if ( !xModel.is() || ( xModel != xScriptContext->getScriptContainer() ) )
98cdf0e10cSrcweir {
99cdf0e10cSrcweir msp = getMSPFromInvocationContext( xScriptContext );
100cdf0e10cSrcweir return msp;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir }
103cdf0e10cSrcweir catch( const lang::IllegalArgumentException& )
104cdf0e10cSrcweir {
105cdf0e10cSrcweir xModel.set( Reference< frame::XModel >() );
106cdf0e10cSrcweir }
107cdf0e10cSrcweir }
108cdf0e10cSrcweir
109cdf0e10cSrcweir if ( xModel.is() )
110cdf0e10cSrcweir {
111cdf0e10cSrcweir sContext = MiscUtils::xModelToTdocUrl( xModel, m_xContext );
112cdf0e10cSrcweir msp = getMSPFromStringContext( sContext );
113cdf0e10cSrcweir return msp;
114cdf0e10cSrcweir }
115cdf0e10cSrcweir
116cdf0e10cSrcweir createNonDocMSPs();
117cdf0e10cSrcweir return m_hMsps[ shareDirString ];
118cdf0e10cSrcweir }
119cdf0e10cSrcweir
120cdf0e10cSrcweir Reference< provider::XScriptProvider >
getMSPFromInvocationContext(const Reference<document::XScriptInvocationContext> & xContext)121cdf0e10cSrcweir ActiveMSPList::getMSPFromInvocationContext( const Reference< document::XScriptInvocationContext >& xContext )
122cdf0e10cSrcweir SAL_THROW(( lang::IllegalArgumentException, RuntimeException ))
123cdf0e10cSrcweir {
124cdf0e10cSrcweir Reference< provider::XScriptProvider > msp;
125cdf0e10cSrcweir
126cdf0e10cSrcweir Reference< document::XEmbeddedScripts > xScripts;
127cdf0e10cSrcweir if ( xContext.is() )
128cdf0e10cSrcweir xScripts.set( xContext->getScriptContainer() );
129cdf0e10cSrcweir if ( !xScripts.is() )
130cdf0e10cSrcweir {
131cdf0e10cSrcweir ::rtl::OUStringBuffer buf;
132cdf0e10cSrcweir buf.appendAscii( "Failed to create MasterScriptProvider for ScriptInvocationContext: " );
133cdf0e10cSrcweir buf.appendAscii( "Component supporting XEmbeddScripts interface not found." );
134cdf0e10cSrcweir throw lang::IllegalArgumentException( buf.makeStringAndClear(), NULL, 1 );
135cdf0e10cSrcweir }
136cdf0e10cSrcweir
137cdf0e10cSrcweir ::osl::MutexGuard guard( m_mutex );
138cdf0e10cSrcweir
139cdf0e10cSrcweir Reference< XInterface > xNormalized( xContext, UNO_QUERY );
140cdf0e10cSrcweir ScriptComponent_map::const_iterator pos = m_mScriptComponents.find( xNormalized );
141cdf0e10cSrcweir if ( pos == m_mScriptComponents.end() )
142cdf0e10cSrcweir {
143cdf0e10cSrcweir // TODO
144cdf0e10cSrcweir msp = createNewMSP( uno::makeAny( xContext ) );
145cdf0e10cSrcweir addActiveMSP( xNormalized, msp );
146cdf0e10cSrcweir }
147cdf0e10cSrcweir else
148cdf0e10cSrcweir {
149cdf0e10cSrcweir msp = pos->second;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir
152cdf0e10cSrcweir return msp;
153cdf0e10cSrcweir }
154cdf0e10cSrcweir
155cdf0e10cSrcweir Reference< provider::XScriptProvider >
getMSPFromStringContext(const::rtl::OUString & context)156cdf0e10cSrcweir ActiveMSPList::getMSPFromStringContext( const ::rtl::OUString& context )
157cdf0e10cSrcweir SAL_THROW(( lang::IllegalArgumentException, RuntimeException ))
158cdf0e10cSrcweir {
159cdf0e10cSrcweir Reference< provider::XScriptProvider > msp;
160cdf0e10cSrcweir try
161cdf0e10cSrcweir {
162cdf0e10cSrcweir if ( context.indexOf( OUSTR( "vnd.sun.star.tdoc" ) ) == 0 )
163cdf0e10cSrcweir {
164cdf0e10cSrcweir Reference< frame::XModel > xModel( MiscUtils::tDocUrlToModel( context ) );
165cdf0e10cSrcweir
166cdf0e10cSrcweir Reference< document::XEmbeddedScripts > xScripts( xModel, UNO_QUERY );
167cdf0e10cSrcweir Reference< document::XScriptInvocationContext > xScriptsContext( xModel, UNO_QUERY );
168cdf0e10cSrcweir if ( !xScripts.is() && !xScriptsContext.is() )
169cdf0e10cSrcweir {
170cdf0e10cSrcweir ::rtl::OUStringBuffer buf;
171cdf0e10cSrcweir buf.appendAscii( "Failed to create MasterScriptProvider for '" );
172cdf0e10cSrcweir buf.append ( context );
173cdf0e10cSrcweir buf.appendAscii( "': Either XEmbeddScripts or XScriptInvocationContext need to be supported by the document." );
174cdf0e10cSrcweir throw lang::IllegalArgumentException( buf.makeStringAndClear(), NULL, 1 );
175cdf0e10cSrcweir }
176cdf0e10cSrcweir
177cdf0e10cSrcweir ::osl::MutexGuard guard( m_mutex );
178cdf0e10cSrcweir Reference< XInterface > xNormalized( xModel, UNO_QUERY );
179cdf0e10cSrcweir ScriptComponent_map::const_iterator pos = m_mScriptComponents.find( xNormalized );
180cdf0e10cSrcweir if ( pos == m_mScriptComponents.end() )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir msp = createNewMSP( context );
183cdf0e10cSrcweir addActiveMSP( xNormalized, msp );
184cdf0e10cSrcweir }
185cdf0e10cSrcweir else
186cdf0e10cSrcweir {
187cdf0e10cSrcweir msp = pos->second;
188cdf0e10cSrcweir }
189cdf0e10cSrcweir }
190cdf0e10cSrcweir else
191cdf0e10cSrcweir {
192cdf0e10cSrcweir ::osl::MutexGuard guard( m_mutex );
193cdf0e10cSrcweir Msp_hash::iterator h_itEnd = m_hMsps.end();
194cdf0e10cSrcweir Msp_hash::const_iterator itr = m_hMsps.find( context );
195cdf0e10cSrcweir if ( itr == h_itEnd )
196cdf0e10cSrcweir {
197cdf0e10cSrcweir msp = createNewMSP( context );
198cdf0e10cSrcweir m_hMsps[ context ] = msp;
199cdf0e10cSrcweir }
200cdf0e10cSrcweir else
201cdf0e10cSrcweir {
202cdf0e10cSrcweir msp = m_hMsps[ context ];
203cdf0e10cSrcweir }
204cdf0e10cSrcweir }
205cdf0e10cSrcweir }
206cdf0e10cSrcweir catch( const lang::IllegalArgumentException& )
207cdf0e10cSrcweir {
208cdf0e10cSrcweir // allowed to leave
209cdf0e10cSrcweir }
210cdf0e10cSrcweir catch( const RuntimeException& )
211cdf0e10cSrcweir {
212cdf0e10cSrcweir // allowed to leave
213cdf0e10cSrcweir }
214cdf0e10cSrcweir catch( const Exception& )
215cdf0e10cSrcweir {
216cdf0e10cSrcweir ::rtl::OUStringBuffer aMessage;
217cdf0e10cSrcweir aMessage.appendAscii( "Failed to create MasterScriptProvider for context '" );
218cdf0e10cSrcweir aMessage.append ( context );
219cdf0e10cSrcweir aMessage.appendAscii( "'." );
220cdf0e10cSrcweir throw lang::WrappedTargetRuntimeException(
221cdf0e10cSrcweir aMessage.makeStringAndClear(), *this, ::cppu::getCaughtException() );
222cdf0e10cSrcweir }
223cdf0e10cSrcweir return msp;
224cdf0e10cSrcweir }
225cdf0e10cSrcweir
226cdf0e10cSrcweir void
addActiveMSP(const Reference<uno::XInterface> & xComponent,const Reference<provider::XScriptProvider> & msp)227cdf0e10cSrcweir ActiveMSPList::addActiveMSP( const Reference< uno::XInterface >& xComponent,
228cdf0e10cSrcweir const Reference< provider::XScriptProvider >& msp )
229cdf0e10cSrcweir {
230cdf0e10cSrcweir ::osl::MutexGuard guard( m_mutex );
231cdf0e10cSrcweir Reference< XInterface > xNormalized( xComponent, UNO_QUERY );
232cdf0e10cSrcweir ScriptComponent_map::const_iterator pos = m_mScriptComponents.find( xNormalized );
233cdf0e10cSrcweir if ( pos == m_mScriptComponents.end() )
234cdf0e10cSrcweir {
235cdf0e10cSrcweir m_mScriptComponents[ xNormalized ] = msp;
236cdf0e10cSrcweir
237cdf0e10cSrcweir // add self as listener for component disposal
238cdf0e10cSrcweir // should probably throw from this method!!, reexamine
239cdf0e10cSrcweir try
240cdf0e10cSrcweir {
241cdf0e10cSrcweir Reference< lang::XComponent > xBroadcaster =
242cdf0e10cSrcweir Reference< lang::XComponent >( xComponent, UNO_QUERY_THROW );
243cdf0e10cSrcweir xBroadcaster->addEventListener( this );
244cdf0e10cSrcweir }
245cdf0e10cSrcweir catch ( const Exception& )
246cdf0e10cSrcweir {
247cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
248cdf0e10cSrcweir }
249cdf0e10cSrcweir }
250cdf0e10cSrcweir }
251cdf0e10cSrcweir
252cdf0e10cSrcweir //*************************************************************************
253cdf0e10cSrcweir void SAL_CALL
disposing(const::com::sun::star::lang::EventObject & Source)254cdf0e10cSrcweir ActiveMSPList::disposing( const ::com::sun::star::lang::EventObject& Source )
255cdf0e10cSrcweir throw ( ::com::sun::star::uno::RuntimeException )
256cdf0e10cSrcweir
257cdf0e10cSrcweir {
258cdf0e10cSrcweir try
259cdf0e10cSrcweir {
260cdf0e10cSrcweir Reference< XInterface > xNormalized( Source.Source, UNO_QUERY );
261cdf0e10cSrcweir if ( xNormalized.is() )
262cdf0e10cSrcweir {
263cdf0e10cSrcweir ::osl::MutexGuard guard( m_mutex );
264cdf0e10cSrcweir ScriptComponent_map::iterator pos = m_mScriptComponents.find( xNormalized );
265cdf0e10cSrcweir if ( pos != m_mScriptComponents.end() )
266cdf0e10cSrcweir m_mScriptComponents.erase( pos );
267cdf0e10cSrcweir }
268cdf0e10cSrcweir }
269cdf0e10cSrcweir catch ( const Exception& )
270cdf0e10cSrcweir {
271cdf0e10cSrcweir // if we get an exception here, there is not much we can do about
272cdf0e10cSrcweir // it can't throw as it will screw up the model that is calling dispose
273cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
274cdf0e10cSrcweir }
275cdf0e10cSrcweir }
276cdf0e10cSrcweir
277cdf0e10cSrcweir
278cdf0e10cSrcweir void
createNonDocMSPs()279cdf0e10cSrcweir ActiveMSPList::createNonDocMSPs()
280cdf0e10cSrcweir {
281cdf0e10cSrcweir static bool created = false;
282cdf0e10cSrcweir if ( created )
283cdf0e10cSrcweir {
284cdf0e10cSrcweir return;
285cdf0e10cSrcweir }
286cdf0e10cSrcweir else
287cdf0e10cSrcweir {
288cdf0e10cSrcweir ::osl::MutexGuard guard( m_mutex );
289cdf0e10cSrcweir if ( created )
290cdf0e10cSrcweir {
291cdf0e10cSrcweir return;
292cdf0e10cSrcweir }
293cdf0e10cSrcweir // do creation of user and share MSPs here
294cdf0e10cSrcweir ::rtl::OUString serviceName = ::rtl::OUString::createFromAscii("com.sun.star.script.provider.MasterScriptProvider");
295cdf0e10cSrcweir Sequence< Any > args(1);
296cdf0e10cSrcweir
297cdf0e10cSrcweir args[ 0 ] <<= userDirString;
298cdf0e10cSrcweir Reference< provider::XScriptProvider > userMsp( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( serviceName, args, m_xContext ), UNO_QUERY );
299cdf0e10cSrcweir // should check if provider reference is valid
300cdf0e10cSrcweir m_hMsps[ userDirString ] = userMsp;
301cdf0e10cSrcweir
302cdf0e10cSrcweir args[ 0 ] <<= shareDirString;
303cdf0e10cSrcweir Reference< provider::XScriptProvider > shareMsp( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( serviceName, args, m_xContext ), UNO_QUERY );
304cdf0e10cSrcweir // should check if provider reference is valid
305cdf0e10cSrcweir m_hMsps[ shareDirString ] = shareMsp;
306cdf0e10cSrcweir
307cdf0e10cSrcweir args[ 0 ] <<= bundledDirString;
308cdf0e10cSrcweir Reference< provider::XScriptProvider > bundledMsp( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext( serviceName, args, m_xContext ), UNO_QUERY );
309cdf0e10cSrcweir // should check if provider reference is valid
310cdf0e10cSrcweir m_hMsps[ bundledDirString ] = bundledMsp;
311cdf0e10cSrcweir
312cdf0e10cSrcweir created = true;
313cdf0e10cSrcweir }
314cdf0e10cSrcweir
315cdf0e10cSrcweir }
316cdf0e10cSrcweir
317cdf0e10cSrcweir
318cdf0e10cSrcweir } // namespace func_provider
319cdf0e10cSrcweir
320