1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_scripting.hxx"
30*cdf0e10cSrcweir #include "scripthandler.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <osl/mutex.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <com/sun/star/frame/DispatchResultEvent.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/frame/DispatchResultState.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/frame/XController.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <com/sun/star/document/XEmbeddedScripts.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/document/XScriptInvocationContext.hpp>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/script/provider/XScriptProviderFactory.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #include <sfx2/objsh.hxx>
49*cdf0e10cSrcweir #include <sfx2/frame.hxx>
50*cdf0e10cSrcweir #include <sfx2/sfxdlg.hxx>
51*cdf0e10cSrcweir #include <vcl/abstdlg.hxx>
52*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
55*cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
56*cdf0e10cSrcweir #include <util/util.hxx>
57*cdf0e10cSrcweir #include <framework/documentundoguard.hxx>
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
60*cdf0e10cSrcweir #include "com/sun/star/uri/XUriReference.hpp"
61*cdf0e10cSrcweir #include "com/sun/star/uri/XUriReferenceFactory.hpp"
62*cdf0e10cSrcweir #include "com/sun/star/uri/XVndSunStarScriptUrl.hpp"
63*cdf0e10cSrcweir #include "com/sun/star/beans/XPropertySet.hpp"
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir using namespace ::com::sun::star;
66*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
67*cdf0e10cSrcweir using namespace ::com::sun::star::frame;
68*cdf0e10cSrcweir using namespace ::com::sun::star::util;
69*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
70*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
71*cdf0e10cSrcweir using namespace ::com::sun::star::script;
72*cdf0e10cSrcweir using namespace ::com::sun::star::script::provider;
73*cdf0e10cSrcweir using namespace ::com::sun::star::document;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir namespace scripting_protocolhandler
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir const sal_Char * const MYSERVICENAME = "com.sun.star.frame.ProtocolHandler";
79*cdf0e10cSrcweir const sal_Char * const MYIMPLNAME = "com.sun.star.comp.ScriptProtocolHandler";
80*cdf0e10cSrcweir const sal_Char * MYSCHEME = "vnd.sun.star.script";
81*cdf0e10cSrcweir const sal_Int32 MYSCHEME_LEN = 20;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::initialize(
84*cdf0e10cSrcweir     const css::uno::Sequence < css::uno::Any >& aArguments )
85*cdf0e10cSrcweir     throw ( css::uno::Exception )
86*cdf0e10cSrcweir {
87*cdf0e10cSrcweir     if ( m_bInitialised )
88*cdf0e10cSrcweir     {
89*cdf0e10cSrcweir         return ;
90*cdf0e10cSrcweir     }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     // first argument contains a reference to the frame (may be empty or the desktop,
93*cdf0e10cSrcweir     // but usually it's a "real" frame)
94*cdf0e10cSrcweir     if ( aArguments.getLength() &&
95*cdf0e10cSrcweir          sal_False == ( aArguments[ 0 ] >>= m_xFrame ) )
96*cdf0e10cSrcweir     {
97*cdf0e10cSrcweir         ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::initialize: could not extract reference to the frame" );
98*cdf0e10cSrcweir         throw RuntimeException( temp, Reference< XInterface >() );
99*cdf0e10cSrcweir     }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     ENSURE_OR_THROW( m_xFactory.is(), "ScriptProtocolHandler::initialize: No Service Manager available" );
102*cdf0e10cSrcweir     m_bInitialised = true;
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir Reference< XDispatch > SAL_CALL ScriptProtocolHandler::queryDispatch(
106*cdf0e10cSrcweir     const URL& aURL, const ::rtl::OUString& sTargetFrameName, sal_Int32 nSearchFlags )
107*cdf0e10cSrcweir     throw( ::com::sun::star::uno::RuntimeException )
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir 	(void)sTargetFrameName;
110*cdf0e10cSrcweir 	(void)nSearchFlags;
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir     Reference< XDispatch > xDispatcher;
113*cdf0e10cSrcweir     // get scheme of url
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     Reference< uri::XUriReferenceFactory > xFac (
116*cdf0e10cSrcweir          m_xFactory->createInstance( rtl::OUString::createFromAscii(
117*cdf0e10cSrcweir             "com.sun.star.uri.UriReferenceFactory") ) , UNO_QUERY );
118*cdf0e10cSrcweir     if ( xFac.is() )
119*cdf0e10cSrcweir     {
120*cdf0e10cSrcweir         Reference<  uri::XUriReference > uriRef(
121*cdf0e10cSrcweir             xFac->parse( aURL.Complete ), UNO_QUERY );
122*cdf0e10cSrcweir         if ( uriRef.is() )
123*cdf0e10cSrcweir         {
124*cdf0e10cSrcweir             if ( uriRef->getScheme().equals( ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYSCHEME ) ) )
125*cdf0e10cSrcweir             {
126*cdf0e10cSrcweir                 xDispatcher = this;
127*cdf0e10cSrcweir             }
128*cdf0e10cSrcweir         }
129*cdf0e10cSrcweir     }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     return xDispatcher;
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir Sequence< Reference< XDispatch > > SAL_CALL
135*cdf0e10cSrcweir ScriptProtocolHandler::queryDispatches(
136*cdf0e10cSrcweir const Sequence < DispatchDescriptor >& seqDescriptor )
137*cdf0e10cSrcweir throw( RuntimeException )
138*cdf0e10cSrcweir {
139*cdf0e10cSrcweir     sal_Int32 nCount = seqDescriptor.getLength();
140*cdf0e10cSrcweir     Sequence< Reference< XDispatch > > lDispatcher( nCount );
141*cdf0e10cSrcweir     for ( sal_Int32 i = 0; i < nCount; ++i )
142*cdf0e10cSrcweir     {
143*cdf0e10cSrcweir         lDispatcher[ i ] = this->queryDispatch( seqDescriptor[ i ].FeatureURL,
144*cdf0e10cSrcweir                                                 seqDescriptor[ i ].FrameName,
145*cdf0e10cSrcweir                                                 seqDescriptor[ i ].SearchFlags );
146*cdf0e10cSrcweir     }
147*cdf0e10cSrcweir     return lDispatcher;
148*cdf0e10cSrcweir }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::dispatchWithNotification(
151*cdf0e10cSrcweir     const URL& aURL, const Sequence < PropertyValue >& lArgs,
152*cdf0e10cSrcweir     const Reference< XDispatchResultListener >& xListener )
153*cdf0e10cSrcweir     throw ( RuntimeException )
154*cdf0e10cSrcweir {
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir     sal_Bool bSuccess = sal_False;
157*cdf0e10cSrcweir     Any invokeResult;
158*cdf0e10cSrcweir 	bool bCaughtException = sal_False;
159*cdf0e10cSrcweir 	Any aException;
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir     if ( m_bInitialised )
162*cdf0e10cSrcweir     {
163*cdf0e10cSrcweir         try
164*cdf0e10cSrcweir         {
165*cdf0e10cSrcweir             bool bIsDocumentScript = ( aURL.Complete.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "document" ) ) !=-1 );
166*cdf0e10cSrcweir                 // TODO: isn't this somewhat strange? This should be a test for a location=document parameter, shouldn't it?
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir             if ( bIsDocumentScript )
169*cdf0e10cSrcweir             {
170*cdf0e10cSrcweir                 // obtain the component for our security check
171*cdf0e10cSrcweir                 Reference< XEmbeddedScripts > xDocumentScripts;
172*cdf0e10cSrcweir                 if ( getScriptInvocation() )
173*cdf0e10cSrcweir                     xDocumentScripts.set( m_xScriptInvocation->getScriptContainer(), UNO_SET_THROW );
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir                 OSL_ENSURE( xDocumentScripts.is(), "ScriptProtocolHandler::dispatchWithNotification: can't do the security check!" );
176*cdf0e10cSrcweir                 if ( !xDocumentScripts.is() || !xDocumentScripts->getAllowMacroExecution() )
177*cdf0e10cSrcweir                     return;
178*cdf0e10cSrcweir             }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir             // Creates a ScriptProvider ( if one is not created allready )
181*cdf0e10cSrcweir             createScriptProvider();
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir             Reference< provider::XScript > xFunc =
184*cdf0e10cSrcweir                 m_xScriptProvider->getScript( aURL.Complete );
185*cdf0e10cSrcweir             ENSURE_OR_THROW( xFunc.is(),
186*cdf0e10cSrcweir                 "ScriptProtocolHandler::dispatchWithNotification: validate xFunc - unable to obtain XScript interface" );
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir             Sequence< Any > inArgs( 0 );
190*cdf0e10cSrcweir             Sequence< Any > outArgs( 0 );
191*cdf0e10cSrcweir             Sequence< sal_Int16 > outIndex;
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir             if ( lArgs.getLength() > 0 )
194*cdf0e10cSrcweir             {
195*cdf0e10cSrcweir                int argCount = 0;
196*cdf0e10cSrcweir                for ( int index = 0; index < lArgs.getLength(); index++ )
197*cdf0e10cSrcweir                {
198*cdf0e10cSrcweir                    // Sometimes we get a propertyval with name = "Referer"
199*cdf0e10cSrcweir                    // this is not an argument to be passed to script, so
200*cdf0e10cSrcweir                    // ignore.
201*cdf0e10cSrcweir                    if ( lArgs[ index ].Name.compareToAscii("Referer") != 0  ||
202*cdf0e10cSrcweir                         lArgs[ index ].Name.getLength() == 0 )
203*cdf0e10cSrcweir                    {
204*cdf0e10cSrcweir                        inArgs.realloc( ++argCount );
205*cdf0e10cSrcweir                        inArgs[ argCount - 1 ] = lArgs[ index ].Value;
206*cdf0e10cSrcweir                    }
207*cdf0e10cSrcweir                }
208*cdf0e10cSrcweir             }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir             // attempt to protect the document against the script tampering with its Undo Context
211*cdf0e10cSrcweir             ::std::auto_ptr< ::framework::DocumentUndoGuard > pUndoGuard;
212*cdf0e10cSrcweir             if ( bIsDocumentScript )
213*cdf0e10cSrcweir                 pUndoGuard.reset( new ::framework::DocumentUndoGuard( m_xScriptInvocation ) );
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir             bSuccess = sal_False;
216*cdf0e10cSrcweir             while ( !bSuccess )
217*cdf0e10cSrcweir             {
218*cdf0e10cSrcweir                 Any aFirstCaughtException;
219*cdf0e10cSrcweir                 try
220*cdf0e10cSrcweir                 {
221*cdf0e10cSrcweir                     invokeResult = xFunc->invoke( inArgs, outIndex, outArgs );
222*cdf0e10cSrcweir                     bSuccess = sal_True;
223*cdf0e10cSrcweir                 }
224*cdf0e10cSrcweir                 catch( const provider::ScriptFrameworkErrorException& se )
225*cdf0e10cSrcweir                 {
226*cdf0e10cSrcweir                     if  ( !aFirstCaughtException.hasValue() )
227*cdf0e10cSrcweir                         aFirstCaughtException = ::cppu::getCaughtException();
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir                     if ( se.errorType != provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT )
230*cdf0e10cSrcweir                         // the only condition which allows us to retry is if there is no method with the
231*cdf0e10cSrcweir                         // given name/signature
232*cdf0e10cSrcweir                         ::cppu::throwException( aFirstCaughtException );
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir                     if ( inArgs.getLength() == 0 )
235*cdf0e10cSrcweir                         // no chance to retry if we can't strip more in-args
236*cdf0e10cSrcweir                         ::cppu::throwException( aFirstCaughtException );
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir                     // strip one argument, then retry
239*cdf0e10cSrcweir                     inArgs.realloc( inArgs.getLength() - 1 );
240*cdf0e10cSrcweir                 }
241*cdf0e10cSrcweir             }
242*cdf0e10cSrcweir         }
243*cdf0e10cSrcweir         // Office doesn't handle exceptions rethrown here very well, it cores,
244*cdf0e10cSrcweir         // all we can is log them and then set fail for the dispatch event!
245*cdf0e10cSrcweir         // (if there is a listener of course)
246*cdf0e10cSrcweir         catch ( const Exception & e )
247*cdf0e10cSrcweir         {
248*cdf0e10cSrcweir             aException = ::cppu::getCaughtException();
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir             ::rtl::OUString reason = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ScriptProtocolHandler::dispatch: caught " ) );
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir             invokeResult <<= reason.concat( aException.getValueTypeName() ).concat( e.Message );
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir 			bCaughtException = sal_True;
255*cdf0e10cSrcweir         }
256*cdf0e10cSrcweir     }
257*cdf0e10cSrcweir     else
258*cdf0e10cSrcweir     {
259*cdf0e10cSrcweir         ::rtl::OUString reason = ::rtl::OUString::createFromAscii(
260*cdf0e10cSrcweir         "ScriptProtocolHandler::dispatchWithNotification failed, ScriptProtocolHandler not initialised"
261*cdf0e10cSrcweir         );
262*cdf0e10cSrcweir         invokeResult <<= reason;
263*cdf0e10cSrcweir     }
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir 	if ( bCaughtException )
266*cdf0e10cSrcweir 	{
267*cdf0e10cSrcweir 		SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir 		if ( pFact != NULL )
270*cdf0e10cSrcweir 		{
271*cdf0e10cSrcweir 			VclAbstractDialog* pDlg =
272*cdf0e10cSrcweir 				pFact->CreateScriptErrorDialog( NULL, aException );
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir 			if ( pDlg != NULL )
275*cdf0e10cSrcweir 			{
276*cdf0e10cSrcweir 				pDlg->Execute();
277*cdf0e10cSrcweir 				delete pDlg;
278*cdf0e10cSrcweir 			}
279*cdf0e10cSrcweir 		}
280*cdf0e10cSrcweir    	}
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir     if ( xListener.is() )
283*cdf0e10cSrcweir     {
284*cdf0e10cSrcweir         // always call dispatchFinished(), because we didn't load a document but
285*cdf0e10cSrcweir         // executed a macro instead!
286*cdf0e10cSrcweir         ::com::sun::star::frame::DispatchResultEvent aEvent;
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir         aEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
289*cdf0e10cSrcweir         aEvent.Result = invokeResult;
290*cdf0e10cSrcweir         if ( bSuccess )
291*cdf0e10cSrcweir         {
292*cdf0e10cSrcweir             aEvent.State = ::com::sun::star::frame::DispatchResultState::SUCCESS;
293*cdf0e10cSrcweir         }
294*cdf0e10cSrcweir         else
295*cdf0e10cSrcweir         {
296*cdf0e10cSrcweir             aEvent.State = ::com::sun::star::frame::DispatchResultState::FAILURE;
297*cdf0e10cSrcweir         }
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir         try
300*cdf0e10cSrcweir         {
301*cdf0e10cSrcweir             xListener->dispatchFinished( aEvent ) ;
302*cdf0e10cSrcweir         }
303*cdf0e10cSrcweir         catch(RuntimeException & e)
304*cdf0e10cSrcweir         {
305*cdf0e10cSrcweir             OSL_TRACE(
306*cdf0e10cSrcweir             "ScriptProtocolHandler::dispatchWithNotification: caught RuntimeException"
307*cdf0e10cSrcweir             "while dispatchFinished %s",
308*cdf0e10cSrcweir             ::rtl::OUStringToOString( e.Message,
309*cdf0e10cSrcweir             RTL_TEXTENCODING_ASCII_US ).pData->buffer );
310*cdf0e10cSrcweir         }
311*cdf0e10cSrcweir     }
312*cdf0e10cSrcweir }
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::dispatch(
315*cdf0e10cSrcweir const URL& aURL, const Sequence< PropertyValue >& lArgs )
316*cdf0e10cSrcweir throw ( RuntimeException )
317*cdf0e10cSrcweir {
318*cdf0e10cSrcweir     dispatchWithNotification( aURL, lArgs, Reference< XDispatchResultListener >() );
319*cdf0e10cSrcweir }
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::addStatusListener(
322*cdf0e10cSrcweir const Reference< XStatusListener >& xControl, const URL& aURL )
323*cdf0e10cSrcweir throw ( RuntimeException )
324*cdf0e10cSrcweir {
325*cdf0e10cSrcweir 	(void)xControl;
326*cdf0e10cSrcweir 	(void)aURL;
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir     // implement if status is supported
329*cdf0e10cSrcweir }
330*cdf0e10cSrcweir 
331*cdf0e10cSrcweir void SAL_CALL ScriptProtocolHandler::removeStatusListener(
332*cdf0e10cSrcweir const Reference< XStatusListener >& xControl, const URL& aURL )
333*cdf0e10cSrcweir throw ( RuntimeException )
334*cdf0e10cSrcweir {
335*cdf0e10cSrcweir 	(void)xControl;
336*cdf0e10cSrcweir 	(void)aURL;
337*cdf0e10cSrcweir }
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir bool
340*cdf0e10cSrcweir ScriptProtocolHandler::getScriptInvocation()
341*cdf0e10cSrcweir {
342*cdf0e10cSrcweir     if ( !m_xScriptInvocation.is() && m_xFrame.is() )
343*cdf0e10cSrcweir     {
344*cdf0e10cSrcweir         Reference< XController > xController = m_xFrame->getController();
345*cdf0e10cSrcweir         if ( xController .is() )
346*cdf0e10cSrcweir         {
347*cdf0e10cSrcweir             // try to obtain an XScriptInvocationContext interface, preferred from the
348*cdf0e10cSrcweir             // mode, then from the controller
349*cdf0e10cSrcweir             if ( !m_xScriptInvocation.set( xController->getModel(), UNO_QUERY ) )
350*cdf0e10cSrcweir                 m_xScriptInvocation.set( xController, UNO_QUERY );
351*cdf0e10cSrcweir         }
352*cdf0e10cSrcweir     }
353*cdf0e10cSrcweir     return m_xScriptInvocation.is();
354*cdf0e10cSrcweir }
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir void ScriptProtocolHandler::createScriptProvider()
357*cdf0e10cSrcweir {
358*cdf0e10cSrcweir     if ( m_xScriptProvider.is() )
359*cdf0e10cSrcweir         return;
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir     try
362*cdf0e10cSrcweir     {
363*cdf0e10cSrcweir         // first, ask the component supporting the XScriptInvocationContext interface
364*cdf0e10cSrcweir         // (if there is one) for a script provider
365*cdf0e10cSrcweir         if ( getScriptInvocation() )
366*cdf0e10cSrcweir         {
367*cdf0e10cSrcweir             Reference< XScriptProviderSupplier > xSPS( m_xScriptInvocation, UNO_QUERY );
368*cdf0e10cSrcweir             if ( xSPS.is() )
369*cdf0e10cSrcweir                 m_xScriptProvider = xSPS->getScriptProvider();
370*cdf0e10cSrcweir         }
371*cdf0e10cSrcweir 
372*cdf0e10cSrcweir         // second, ask the model in our frame
373*cdf0e10cSrcweir         if ( !m_xScriptProvider.is() && m_xFrame.is() )
374*cdf0e10cSrcweir         {
375*cdf0e10cSrcweir             Reference< XController > xController = m_xFrame->getController();
376*cdf0e10cSrcweir             if ( xController .is() )
377*cdf0e10cSrcweir             {
378*cdf0e10cSrcweir                 Reference< XScriptProviderSupplier > xSPS( xController->getModel(), UNO_QUERY );
379*cdf0e10cSrcweir                 if ( xSPS.is() )
380*cdf0e10cSrcweir                     m_xScriptProvider = xSPS->getScriptProvider();
381*cdf0e10cSrcweir             }
382*cdf0e10cSrcweir         }
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir         // as a fallback, ask the controller
386*cdf0e10cSrcweir         if ( !m_xScriptProvider.is() && m_xFrame.is() )
387*cdf0e10cSrcweir         {
388*cdf0e10cSrcweir             Reference< XScriptProviderSupplier > xSPS( m_xFrame->getController(), UNO_QUERY );
389*cdf0e10cSrcweir             if ( xSPS.is() )
390*cdf0e10cSrcweir                 m_xScriptProvider = xSPS->getScriptProvider();
391*cdf0e10cSrcweir         }
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir         // if nothing of this is successful, use the master script provider
394*cdf0e10cSrcweir         if ( !m_xScriptProvider.is() )
395*cdf0e10cSrcweir         {
396*cdf0e10cSrcweir             Reference< XPropertySet > xProps( m_xFactory, UNO_QUERY_THROW );
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir             ::rtl::OUString dc(
399*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) );
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir             Reference< XComponentContext > xCtx(
402*cdf0e10cSrcweir                 xProps->getPropertyValue( dc ), UNO_QUERY_THROW );
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir             ::rtl::OUString tmspf = ::rtl::OUString::createFromAscii(
405*cdf0e10cSrcweir                 "/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory");
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir             Reference< provider::XScriptProviderFactory > xFac(
408*cdf0e10cSrcweir                 xCtx->getValueByName( tmspf ), UNO_QUERY_THROW );
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir             Any aContext;
411*cdf0e10cSrcweir             if ( getScriptInvocation() )
412*cdf0e10cSrcweir                 aContext = makeAny( m_xScriptInvocation );
413*cdf0e10cSrcweir             m_xScriptProvider = Reference< provider::XScriptProvider > (
414*cdf0e10cSrcweir                 xFac->createScriptProvider( aContext ), UNO_QUERY_THROW );
415*cdf0e10cSrcweir         }
416*cdf0e10cSrcweir     }
417*cdf0e10cSrcweir     catch ( RuntimeException & e )
418*cdf0e10cSrcweir     {
419*cdf0e10cSrcweir         ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::createScriptProvider(),  " );
420*cdf0e10cSrcweir         throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
421*cdf0e10cSrcweir     }
422*cdf0e10cSrcweir     catch ( Exception & e )
423*cdf0e10cSrcweir     {
424*cdf0e10cSrcweir         ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::createScriptProvider: " );
425*cdf0e10cSrcweir         throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() );
426*cdf0e10cSrcweir     }
427*cdf0e10cSrcweir }
428*cdf0e10cSrcweir 
429*cdf0e10cSrcweir ScriptProtocolHandler::ScriptProtocolHandler(
430*cdf0e10cSrcweir Reference< css::lang::XMultiServiceFactory > const& rFact ) :
431*cdf0e10cSrcweir m_bInitialised( false ), m_xFactory( rFact )
432*cdf0e10cSrcweir {
433*cdf0e10cSrcweir }
434*cdf0e10cSrcweir 
435*cdf0e10cSrcweir ScriptProtocolHandler::~ScriptProtocolHandler()
436*cdf0e10cSrcweir {
437*cdf0e10cSrcweir }
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir /* XServiceInfo */
440*cdf0e10cSrcweir ::rtl::OUString SAL_CALL ScriptProtocolHandler::getImplementationName( )
441*cdf0e10cSrcweir throw( RuntimeException )
442*cdf0e10cSrcweir {
443*cdf0e10cSrcweir     return impl_getStaticImplementationName();
444*cdf0e10cSrcweir }
445*cdf0e10cSrcweir 
446*cdf0e10cSrcweir /* XServiceInfo */
447*cdf0e10cSrcweir sal_Bool SAL_CALL ScriptProtocolHandler::supportsService(
448*cdf0e10cSrcweir const ::rtl::OUString& sServiceName )
449*cdf0e10cSrcweir throw( RuntimeException )
450*cdf0e10cSrcweir {
451*cdf0e10cSrcweir     Sequence< ::rtl::OUString > seqServiceNames = getSupportedServiceNames();
452*cdf0e10cSrcweir     const ::rtl::OUString* pArray = seqServiceNames.getConstArray();
453*cdf0e10cSrcweir     for ( sal_Int32 nCounter = 0; nCounter < seqServiceNames.getLength(); nCounter++ )
454*cdf0e10cSrcweir     {
455*cdf0e10cSrcweir         if ( pArray[ nCounter ] == sServiceName )
456*cdf0e10cSrcweir         {
457*cdf0e10cSrcweir             return sal_True ;
458*cdf0e10cSrcweir         }
459*cdf0e10cSrcweir     }
460*cdf0e10cSrcweir 
461*cdf0e10cSrcweir     return sal_False ;
462*cdf0e10cSrcweir }
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir /* XServiceInfo */
465*cdf0e10cSrcweir Sequence< ::rtl::OUString > SAL_CALL ScriptProtocolHandler::getSupportedServiceNames()
466*cdf0e10cSrcweir throw( RuntimeException )
467*cdf0e10cSrcweir {
468*cdf0e10cSrcweir     return impl_getStaticSupportedServiceNames();
469*cdf0e10cSrcweir }
470*cdf0e10cSrcweir 
471*cdf0e10cSrcweir /* Helper for XServiceInfo */
472*cdf0e10cSrcweir Sequence< ::rtl::OUString > ScriptProtocolHandler::impl_getStaticSupportedServiceNames()
473*cdf0e10cSrcweir {
474*cdf0e10cSrcweir     ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
475*cdf0e10cSrcweir     Sequence< ::rtl::OUString > seqServiceNames( 1 );
476*cdf0e10cSrcweir     seqServiceNames.getArray() [ 0 ] =
477*cdf0e10cSrcweir         ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYSERVICENAME );
478*cdf0e10cSrcweir     return seqServiceNames ;
479*cdf0e10cSrcweir }
480*cdf0e10cSrcweir 
481*cdf0e10cSrcweir /* Helper for XServiceInfo */
482*cdf0e10cSrcweir ::rtl::OUString ScriptProtocolHandler::impl_getStaticImplementationName()
483*cdf0e10cSrcweir {
484*cdf0e10cSrcweir     return ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYIMPLNAME );
485*cdf0e10cSrcweir }
486*cdf0e10cSrcweir 
487*cdf0e10cSrcweir /* Helper for registry */
488*cdf0e10cSrcweir Reference< XInterface > SAL_CALL ScriptProtocolHandler::impl_createInstance(
489*cdf0e10cSrcweir const Reference< css::lang::XMultiServiceFactory >& xServiceManager )
490*cdf0e10cSrcweir throw( RuntimeException )
491*cdf0e10cSrcweir {
492*cdf0e10cSrcweir     return Reference< XInterface > ( *new ScriptProtocolHandler( xServiceManager ) );
493*cdf0e10cSrcweir }
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir /* Factory for registration */
496*cdf0e10cSrcweir Reference< XSingleServiceFactory > ScriptProtocolHandler::impl_createFactory(
497*cdf0e10cSrcweir const Reference< XMultiServiceFactory >& xServiceManager )
498*cdf0e10cSrcweir {
499*cdf0e10cSrcweir     Reference< XSingleServiceFactory > xReturn (
500*cdf0e10cSrcweir         cppu::createSingleFactory( xServiceManager,
501*cdf0e10cSrcweir             ScriptProtocolHandler::impl_getStaticImplementationName(),
502*cdf0e10cSrcweir             ScriptProtocolHandler::impl_createInstance,
503*cdf0e10cSrcweir             ScriptProtocolHandler::impl_getStaticSupportedServiceNames() )
504*cdf0e10cSrcweir     );
505*cdf0e10cSrcweir     return xReturn;
506*cdf0e10cSrcweir }
507*cdf0e10cSrcweir 
508*cdf0e10cSrcweir } // namespace scripting_protocolhandler
509*cdf0e10cSrcweir 
510*cdf0e10cSrcweir /* exported functions for registration */
511*cdf0e10cSrcweir extern "C"
512*cdf0e10cSrcweir {
513*cdf0e10cSrcweir 
514*cdf0e10cSrcweir #undef css
515*cdf0e10cSrcweir #define css ::com::sun::star
516*cdf0e10cSrcweir 
517*cdf0e10cSrcweir     void SAL_CALL component_getImplementationEnvironment(
518*cdf0e10cSrcweir         const sal_Char** ppEnvironmentTypeName, uno_Environment** ppEnvironment )
519*cdf0e10cSrcweir     {
520*cdf0e10cSrcweir 		(void)ppEnvironment;
521*cdf0e10cSrcweir 
522*cdf0e10cSrcweir         *ppEnvironmentTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME ;
523*cdf0e10cSrcweir     }
524*cdf0e10cSrcweir 
525*cdf0e10cSrcweir     void* SAL_CALL component_getFactory( const sal_Char * pImplementationName ,
526*cdf0e10cSrcweir                                          void * pServiceManager ,
527*cdf0e10cSrcweir                                          void * pRegistryKey )
528*cdf0e10cSrcweir     {
529*cdf0e10cSrcweir 		(void)pRegistryKey;
530*cdf0e10cSrcweir 
531*cdf0e10cSrcweir         // Set default return value for this operation - if it failed.
532*cdf0e10cSrcweir         void * pReturn = NULL ;
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir         if (
535*cdf0e10cSrcweir             ( pImplementationName != NULL ) &&
536*cdf0e10cSrcweir             ( pServiceManager != NULL )
537*cdf0e10cSrcweir         )
538*cdf0e10cSrcweir         {
539*cdf0e10cSrcweir             // Define variables which are used in following macros.
540*cdf0e10cSrcweir             ::com::sun::star::uno::Reference<
541*cdf0e10cSrcweir             ::com::sun::star::lang::XSingleServiceFactory > xFactory ;
542*cdf0e10cSrcweir             ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >
543*cdf0e10cSrcweir             xServiceManager( reinterpret_cast<
544*cdf0e10cSrcweir             ::com::sun::star::lang::XMultiServiceFactory* >( pServiceManager ) ) ;
545*cdf0e10cSrcweir 
546*cdf0e10cSrcweir             if ( ::scripting_protocolhandler::ScriptProtocolHandler::impl_getStaticImplementationName().equals(
547*cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii( pImplementationName ) ) )
548*cdf0e10cSrcweir             {
549*cdf0e10cSrcweir                 xFactory = ::scripting_protocolhandler::ScriptProtocolHandler::impl_createFactory( xServiceManager );
550*cdf0e10cSrcweir             }
551*cdf0e10cSrcweir 
552*cdf0e10cSrcweir             // Factory is valid - service was found.
553*cdf0e10cSrcweir             if ( xFactory.is() )
554*cdf0e10cSrcweir             {
555*cdf0e10cSrcweir                 xFactory->acquire();
556*cdf0e10cSrcweir                 pReturn = xFactory.get();
557*cdf0e10cSrcweir             }
558*cdf0e10cSrcweir         }
559*cdf0e10cSrcweir 
560*cdf0e10cSrcweir         // Return with result of this operation.
561*cdf0e10cSrcweir         return pReturn ;
562*cdf0e10cSrcweir     }
563*cdf0e10cSrcweir } // extern "C"
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir 
566