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

Generated by: LCOV version 1.10