LCOV - code coverage report
Current view: top level - scripting/source/protocolhandler - scripthandler.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 177 0.0 %
Date: 2014-04-14 Functions: 0 22 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10