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