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

Generated by: LCOV version 1.10