LCOV - code coverage report
Current view: top level - canvas/source/factory - cf_service.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 109 138 79.0 %
Date: 2015-06-13 12:38:46 Functions: 12 17 70.6 %
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             : 
      21             : #include <osl/mutex.hxx>
      22             : #include <osl/process.h>
      23             : #include <cppuhelper/implementationentry.hxx>
      24             : #include <cppuhelper/factory.hxx>
      25             : #include <cppuhelper/implbase3.hxx>
      26             : #include <cppuhelper/supportsservice.hxx>
      27             : 
      28             : #include <com/sun/star/uno/XComponentContext.hpp>
      29             : #include <com/sun/star/lang/IllegalArgumentException.hpp>
      30             : #include <com/sun/star/lang/XServiceInfo.hpp>
      31             : #include <com/sun/star/lang/XSingleComponentFactory.hpp>
      32             : #include <com/sun/star/configuration/theDefaultProvider.hpp>
      33             : #include <com/sun/star/container/XContentEnumerationAccess.hpp>
      34             : #include <com/sun/star/container/XNameAccess.hpp>
      35             : #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
      36             : #include <com/sun/star/beans/PropertyValue.hpp>
      37             : 
      38             : #include <boost/bind.hpp>
      39             : #include <vector>
      40             : #include <utility>
      41             : #include <o3tl/compat_functional.hxx>
      42             : #include <algorithm>
      43             : 
      44             : 
      45             : using namespace ::com::sun::star;
      46             : using namespace ::com::sun::star::uno;
      47             : 
      48             : 
      49             : namespace
      50             : {
      51             : 
      52             : class CanvasFactory
      53             :     : public ::cppu::WeakImplHelper3< lang::XServiceInfo,
      54             :                                       lang::XMultiComponentFactory,
      55             :                                       lang::XMultiServiceFactory >
      56             : {
      57             :     typedef std::pair<OUString,Sequence<OUString> > AvailPair;
      58             :     typedef std::pair<OUString,OUString>            CachePair;
      59             :     typedef std::vector< AvailPair >                AvailVector;
      60             :     typedef std::vector< CachePair >                CacheVector;
      61             : 
      62             : 
      63             :     mutable ::osl::Mutex              m_mutex;
      64             :     Reference<XComponentContext>      m_xContext;
      65             :     Reference<container::XNameAccess> m_xCanvasConfigNameAccess;
      66             :     AvailVector                       m_aAvailableImplementations;
      67             :     AvailVector                       m_aAcceleratedImplementations;
      68             :     AvailVector                       m_aAAImplementations;
      69             :     mutable CacheVector               m_aCachedImplementations;
      70             :     mutable bool                      m_bCacheHasForcedLastImpl;
      71             :     mutable bool                      m_bCacheHasUseAcceleratedEntry;
      72             :     mutable bool                      m_bCacheHasUseAAEntry;
      73             : 
      74             :     void checkConfigFlag( bool& r_bFlag,
      75             :                           bool& r_CacheFlag,
      76             :                           const OUString& nodeName ) const;
      77             :     Reference<XInterface> use(
      78             :         OUString const & serviceName,
      79             :         Sequence<Any> const & args,
      80             :         Reference<XComponentContext> const & xContext ) const;
      81             :     Reference<XInterface> lookupAndUse(
      82             :         OUString const & serviceName, Sequence<Any> const & args,
      83             :         Reference<XComponentContext> const & xContext ) const;
      84             : 
      85             : public:
      86             :     virtual ~CanvasFactory();
      87             :     explicit CanvasFactory( Reference<XComponentContext> const & xContext );
      88             : 
      89             :     // XServiceInfo
      90             :     virtual OUString SAL_CALL getImplementationName() throw (RuntimeException, std::exception) SAL_OVERRIDE;
      91             :     virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
      92             :         throw (RuntimeException, std::exception) SAL_OVERRIDE;
      93             :     virtual Sequence<OUString> SAL_CALL getSupportedServiceNames()
      94             :         throw (RuntimeException, std::exception) SAL_OVERRIDE;
      95             : 
      96             :     // XMultiComponentFactory
      97             :     virtual Sequence<OUString> SAL_CALL getAvailableServiceNames()
      98             :         throw (RuntimeException, std::exception) SAL_OVERRIDE;
      99             :     virtual Reference<XInterface> SAL_CALL createInstanceWithContext(
     100             :         OUString const & name,
     101             :         Reference<XComponentContext> const & xContext ) throw (Exception, std::exception) SAL_OVERRIDE;
     102             :     virtual Reference<XInterface> SAL_CALL
     103             :     createInstanceWithArgumentsAndContext(
     104             :         OUString const & name,
     105             :         Sequence<Any> const & args,
     106             :         Reference<XComponentContext> const & xContext ) throw (Exception, std::exception) SAL_OVERRIDE;
     107             : 
     108             :     // XMultiServiceFactory
     109             :     virtual Reference<XInterface> SAL_CALL createInstance(
     110             :         OUString const & name )
     111             :         throw (Exception, std::exception) SAL_OVERRIDE;
     112             :     virtual Reference<XInterface> SAL_CALL createInstanceWithArguments(
     113             :         OUString const & name, Sequence<Any> const & args )
     114             :         throw (Exception, std::exception) SAL_OVERRIDE;
     115             : };
     116             : 
     117           9 : CanvasFactory::CanvasFactory( Reference<XComponentContext> const & xContext ) :
     118             :     m_mutex(),
     119             :     m_xContext(xContext),
     120             :     m_xCanvasConfigNameAccess(),
     121             :     m_aAvailableImplementations(),
     122             :     m_aAcceleratedImplementations(),
     123             :     m_aAAImplementations(),
     124             :     m_aCachedImplementations(),
     125             :     m_bCacheHasForcedLastImpl(),
     126             :     m_bCacheHasUseAcceleratedEntry(),
     127           9 :     m_bCacheHasUseAAEntry()
     128             : {
     129             :     try
     130             :     {
     131             :         // read out configuration for preferred services:
     132             :         Reference<lang::XMultiServiceFactory> xConfigProvider(
     133           9 :             configuration::theDefaultProvider::get( m_xContext ) );
     134             : 
     135             :         Any propValue(
     136             :             makeAny( beans::PropertyValue(
     137             :                          OUString("nodepath"), -1,
     138             :                          makeAny( OUString("/org.openoffice.Office.Canvas") ),
     139          18 :                          beans::PropertyState_DIRECT_VALUE ) ) );
     140             : 
     141             :         m_xCanvasConfigNameAccess.set(
     142           9 :             xConfigProvider->createInstanceWithArguments(
     143             :                 OUString("com.sun.star.configuration.ConfigurationAccess"),
     144           9 :                 Sequence<Any>( &propValue, 1 ) ),
     145           9 :             UNO_QUERY_THROW );
     146             : 
     147          18 :         propValue = makeAny(
     148             :             beans::PropertyValue(
     149             :                 OUString("nodepath"), -1,
     150             :                 makeAny( OUString("/org.openoffice.Office.Canvas/CanvasServiceList") ),
     151           9 :                 beans::PropertyState_DIRECT_VALUE ) );
     152             : 
     153             :         Reference<container::XNameAccess> xNameAccess(
     154           9 :             xConfigProvider->createInstanceWithArguments(
     155             :                 OUString("com.sun.star.configuration.ConfigurationAccess"),
     156          18 :                 Sequence<Any>( &propValue, 1 ) ), UNO_QUERY_THROW );
     157             :         Reference<container::XHierarchicalNameAccess> xHierarchicalNameAccess(
     158          18 :             xNameAccess, UNO_QUERY_THROW);
     159             : 
     160          18 :         Sequence<OUString> serviceNames = xNameAccess->getElementNames();
     161           9 :         const OUString* pCurr = serviceNames.getConstArray();
     162           9 :         const OUString* const pEnd = pCurr + serviceNames.getLength();
     163          54 :         while( pCurr != pEnd )
     164             :         {
     165             :             Reference<container::XNameAccess> xEntryNameAccess(
     166          36 :                 xHierarchicalNameAccess->getByHierarchicalName(*pCurr),
     167          36 :                 UNO_QUERY );
     168             : 
     169          36 :             if( xEntryNameAccess.is() )
     170             :             {
     171          36 :                 Sequence<OUString> implementationList;
     172          36 :                 if( (xEntryNameAccess->getByName("PreferredImplementations") >>= implementationList) )
     173             :                 {
     174          36 :                     m_aAvailableImplementations.push_back( std::make_pair(*pCurr,implementationList) );
     175             :                 }
     176          36 :                 if( (xEntryNameAccess->getByName("AcceleratedImplementations") >>= implementationList) )
     177             :                 {
     178          36 :                     m_aAcceleratedImplementations.push_back( std::make_pair(*pCurr,implementationList) );
     179             :                 }
     180          36 :                 if( (xEntryNameAccess->getByName("AntialiasingImplementations") >>= implementationList) )
     181             :                 {
     182          36 :                     m_aAAImplementations.push_back( std::make_pair(*pCurr,implementationList) );
     183          36 :                 }
     184             : 
     185             :             }
     186             : 
     187          36 :             ++pCurr;
     188          45 :         }
     189             :     }
     190           0 :     catch (const RuntimeException &)
     191             :     {
     192           0 :         throw;
     193             :     }
     194           0 :     catch (const Exception&)
     195             :     {
     196             :     }
     197             : 
     198           9 :     if( m_aAvailableImplementations.empty() )
     199             :     {
     200             :         // Ugh. Looks like configuration is borked. Fake minimal
     201             :         // setup.
     202           0 :         Sequence<OUString> aServices(1);
     203           0 :         aServices[0] = "com.sun.star.comp.rendering.Canvas.VCL";
     204             :         m_aAvailableImplementations.push_back( std::make_pair(OUString("com.sun.star.rendering.Canvas"),
     205           0 :                                                               aServices) );
     206             : 
     207           0 :         aServices[0] = "com.sun.star.comp.rendering.SpriteCanvas.VCL";
     208             :         m_aAvailableImplementations.push_back( std::make_pair(OUString("com.sun.star.rendering.SpriteCanvas"),
     209           0 :                                                               aServices) );
     210             :     }
     211           9 : }
     212             : 
     213          18 : CanvasFactory::~CanvasFactory()
     214             : {
     215          18 : }
     216             : 
     217             : 
     218             : // XServiceInfo
     219           1 : OUString CanvasFactory::getImplementationName() throw (RuntimeException, std::exception)
     220             : {
     221           1 :     return OUString("com.sun.star.comp.rendering.CanvasFactory");
     222             : }
     223             : 
     224           0 : sal_Bool CanvasFactory::supportsService( OUString const & serviceName )
     225             :     throw (RuntimeException, std::exception)
     226             : {
     227           0 :     return cppu::supportsService(this, serviceName);
     228             : }
     229             : 
     230           1 : Sequence<OUString> CanvasFactory::getSupportedServiceNames()
     231             :     throw (RuntimeException, std::exception)
     232             : {
     233           1 :     OUString name("com.sun.star.rendering.CanvasFactory");
     234           1 :     return Sequence<OUString>(&name, 1);
     235             : }
     236             : 
     237             : // XMultiComponentFactory
     238           0 : Sequence<OUString> CanvasFactory::getAvailableServiceNames()
     239             :     throw (RuntimeException, std::exception)
     240             : {
     241           0 :     Sequence<OUString> aServiceNames(m_aAvailableImplementations.size());
     242             :     std::transform(m_aAvailableImplementations.begin(),
     243             :                    m_aAvailableImplementations.end(),
     244             :                    aServiceNames.getArray(),
     245           0 :                    o3tl::select1st<AvailPair>());
     246           0 :     return aServiceNames;
     247             : }
     248             : 
     249           0 : Reference<XInterface> CanvasFactory::createInstanceWithContext(
     250             :     OUString const & name, Reference<XComponentContext> const & xContext )
     251             :     throw (Exception, std::exception)
     252             : {
     253             :     return createInstanceWithArgumentsAndContext(
     254           0 :         name, Sequence<Any>(), xContext );
     255             : }
     256             : 
     257             : 
     258         429 : Reference<XInterface> CanvasFactory::use(
     259             :     OUString const & serviceName,
     260             :     Sequence<Any> const & args,
     261             :     Reference<XComponentContext> const & xContext ) const
     262             : {
     263             :     try {
     264         857 :         return m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
     265         429 :             serviceName, args, xContext);
     266             :     }
     267           2 :     catch (css::lang::IllegalArgumentException &)
     268             :     {
     269           1 :         return Reference<XInterface>();
     270             :     }
     271           0 :     catch (const RuntimeException &)
     272             :     {
     273           0 :         throw;
     274             :     }
     275           0 :     catch (const Exception &)
     276             :     {
     277           0 :         return Reference<XInterface>();
     278             :     }
     279             : }
     280             : 
     281             : 
     282         261 : void CanvasFactory::checkConfigFlag( bool& r_bFlag,
     283             :                                      bool& r_CacheFlag,
     284             :                                      const OUString& nodeName ) const
     285             : {
     286         261 :     if( m_xCanvasConfigNameAccess.is() )
     287             :     {
     288         261 :         m_xCanvasConfigNameAccess->getByName( nodeName ) >>= r_bFlag;
     289             : 
     290         261 :         if( r_CacheFlag != r_bFlag )
     291             :         {
     292             :             // cache is invalid, because of different order of
     293             :             // elements
     294          16 :             r_CacheFlag = r_bFlag;
     295          16 :             m_aCachedImplementations.clear();
     296             :         }
     297             :     }
     298         261 : }
     299             : 
     300             : 
     301          87 : Reference<XInterface> CanvasFactory::lookupAndUse(
     302             :     OUString const & serviceName, Sequence<Any> const & args,
     303             :     Reference<XComponentContext> const & xContext ) const
     304             : {
     305          87 :     ::osl::MutexGuard guard(m_mutex);
     306             : 
     307             :     // forcing last entry from impl list, if config flag set
     308          87 :     bool bForceLastEntry(false);
     309             :     checkConfigFlag( bForceLastEntry,
     310             :                      m_bCacheHasForcedLastImpl,
     311          87 :                      OUString("ForceSafeServiceImpl") );
     312             : 
     313             :     // use anti-aliasing canvas, if config flag set (or not existing)
     314          87 :     bool bUseAAEntry(true);
     315             :     checkConfigFlag( bUseAAEntry,
     316             :                      m_bCacheHasUseAAEntry,
     317          87 :                      OUString("UseAntialiasingCanvas") );
     318             : 
     319             :     // use accelerated canvas, if config flag set (or not existing)
     320          87 :     bool bUseAcceleratedEntry(true);
     321             :     checkConfigFlag( bUseAcceleratedEntry,
     322             :                      m_bCacheHasUseAcceleratedEntry,
     323          87 :                      OUString("UseAcceleratedCanvas") );
     324             : 
     325             :     // try to reuse last working implementation for given service name
     326          87 :     const CacheVector::iterator aEnd(m_aCachedImplementations.end());
     327          87 :     CacheVector::iterator aMatch;
     328         174 :     if( (aMatch=std::find_if(m_aCachedImplementations.begin(),
     329             :                              aEnd,
     330             :                              boost::bind(&OUString::equals,
     331             :                                          boost::cref(serviceName),
     332             :                                          boost::bind(
     333             :                                              o3tl::select1st<CachePair>(),
     334         174 :                                              _1)))) != aEnd )
     335             :     {
     336           1 :         Reference<XInterface> xCanvas( use( aMatch->second, args, xContext ) );
     337           1 :         if(xCanvas.is())
     338           1 :             return xCanvas;
     339             :     }
     340             : 
     341             :     // lookup in available service list
     342          86 :     const AvailVector::const_iterator aAvailEnd(m_aAvailableImplementations.end());
     343          86 :     AvailVector::const_iterator aAvailImplsMatch;
     344         172 :     if( (aAvailImplsMatch=std::find_if(m_aAvailableImplementations.begin(),
     345             :                                        aAvailEnd,
     346             :                                        boost::bind(&OUString::equals,
     347             :                                                    boost::cref(serviceName),
     348             :                                                    boost::bind(
     349             :                                                        o3tl::select1st<AvailPair>(),
     350         172 :                                                        _1)))) == aAvailEnd )
     351             :     {
     352           0 :         return Reference<XInterface>();
     353             :     }
     354             : 
     355          86 :     const AvailVector::const_iterator aAAEnd(m_aAAImplementations.end());
     356          86 :     AvailVector::const_iterator aAAImplsMatch;
     357         172 :     if( (aAAImplsMatch=std::find_if(m_aAAImplementations.begin(),
     358             :                                     aAAEnd,
     359             :                                     boost::bind(&OUString::equals,
     360             :                                                 boost::cref(serviceName),
     361             :                                                 boost::bind(
     362             :                                                     o3tl::select1st<AvailPair>(),
     363         172 :                                                     _1)))) == aAAEnd )
     364             :     {
     365           0 :         return Reference<XInterface>();
     366             :     }
     367             : 
     368          86 :     const AvailVector::const_iterator aAccelEnd(m_aAcceleratedImplementations.end());
     369          86 :     AvailVector::const_iterator aAccelImplsMatch;
     370         172 :     if( (aAccelImplsMatch=std::find_if(m_aAcceleratedImplementations.begin(),
     371             :                                        aAccelEnd,
     372             :                                        boost::bind(&OUString::equals,
     373             :                                                    boost::cref(serviceName),
     374             :                                                    boost::bind(
     375             :                                                        o3tl::select1st<AvailPair>(),
     376         172 :                                                        _1)))) == aAccelEnd )
     377             :     {
     378           0 :         return Reference<XInterface>();
     379             :     }
     380             : 
     381         172 :     const Sequence<OUString> aPreferredImpls( aAvailImplsMatch->second );
     382          86 :     const OUString* pCurrImpl = aPreferredImpls.getConstArray();
     383          86 :     const OUString* const pEndImpl = pCurrImpl + aPreferredImpls.getLength();
     384             : 
     385         172 :     const Sequence<OUString> aAAImpls( aAAImplsMatch->second );
     386          86 :     const OUString* const pFirstAAImpl = aAAImpls.getConstArray();
     387          86 :     const OUString* const pEndAAImpl = pFirstAAImpl + aAAImpls.getLength();
     388             : 
     389         172 :     const Sequence<OUString> aAccelImpls( aAccelImplsMatch->second );
     390          86 :     const OUString* const pFirstAccelImpl = aAccelImpls.getConstArray();
     391          86 :     const OUString* const pEndAccelImpl = pFirstAccelImpl + aAccelImpls.getLength();
     392             : 
     393             :     // force last entry from impl list, if config flag set
     394          86 :     if( bForceLastEntry )
     395           0 :         pCurrImpl = pEndImpl-1;
     396             : 
     397         514 :     while( pCurrImpl != pEndImpl )
     398             :     {
     399         343 :         const OUString aCurrName(pCurrImpl->trim());
     400             : 
     401             :         // check whether given canvas service is listed in the
     402             :         // sequence of "accelerated canvas implementations"
     403             :         const bool bIsAcceleratedImpl(
     404             :             std::any_of(pFirstAccelImpl,
     405             :                          pEndAccelImpl,
     406             :                          boost::bind(&OUString::equals,
     407             :                                      boost::cref(aCurrName),
     408             :                                      boost::bind(
     409             :                                          &OUString::trim,
     410         343 :                                          _1))) );
     411             : 
     412             :         // check whether given canvas service is listed in the
     413             :         // sequence of "antialiasing canvas implementations"
     414             :         const bool bIsAAImpl(
     415             :             std::any_of(pFirstAAImpl,
     416             :                          pEndAAImpl,
     417             :                          boost::bind(&OUString::equals,
     418             :                                      boost::cref(aCurrName),
     419             :                                      boost::bind(
     420             :                                          &OUString::trim,
     421         343 :                                          _1))) );
     422             : 
     423             :         // try to instantiate canvas *only* if either accel and AA
     424             :         // property match preference, *or*, if there's a mismatch, only
     425             :         // go for a less capable canvas (that effectively let those
     426             :         // pour canvas impls still work as fallbacks, should an
     427             :         // accelerated/AA one fail). Property implies configuration:
     428             :         // http://en.wikipedia.org/wiki/Truth_table#Logical_implication
     429         343 :         if( (!bIsAAImpl || bUseAAEntry) && (!bIsAcceleratedImpl || bUseAcceleratedEntry) )
     430             :         {
     431             :             Reference<XInterface> xCanvas(
     432         343 :                 use( pCurrImpl->trim(), args, xContext ) );
     433             : 
     434         343 :             if(xCanvas.is())
     435             :             {
     436           1 :                 if( aMatch != aEnd )
     437             :                 {
     438             :                     // cache entry exists, replace dysfunctional
     439             :                     // implementation name
     440           0 :                     aMatch->second = pCurrImpl->trim();
     441             :                 }
     442             :                 else
     443             :                 {
     444             :                     // new service name, add new cache entry
     445             :                     m_aCachedImplementations.push_back(std::make_pair(serviceName,
     446           1 :                                                                       pCurrImpl->trim()));
     447             :                 }
     448             : 
     449           1 :                 return xCanvas;
     450         342 :             }
     451             :         }
     452             : 
     453         342 :         ++pCurrImpl;
     454         342 :     }
     455             : 
     456         172 :     return Reference<XInterface>();
     457             : }
     458             : 
     459             : 
     460          87 : Reference<XInterface> CanvasFactory::createInstanceWithArgumentsAndContext(
     461             :     OUString const & preferredOne, Sequence<Any> const & args,
     462             :     Reference<XComponentContext> const & xContext ) throw (Exception, std::exception)
     463             : {
     464             :     Reference<XInterface> xCanvas(
     465          87 :         lookupAndUse( preferredOne, args, xContext ) );
     466          87 :     if(xCanvas.is())
     467           2 :         return xCanvas;
     468             : 
     469             :     // last resort: try service name directly
     470          85 :     return use( preferredOne, args, xContext );
     471             : }
     472             : 
     473             : // XMultiServiceFactory
     474             : 
     475           0 : Reference<XInterface> CanvasFactory::createInstance( OUString const & name )
     476             :     throw (Exception, std::exception)
     477             : {
     478             :     return createInstanceWithArgumentsAndContext(
     479           0 :         name, Sequence<Any>(), m_xContext );
     480             : }
     481             : 
     482             : 
     483           0 : Reference<XInterface> CanvasFactory::createInstanceWithArguments(
     484             :     OUString const & name, Sequence<Any> const & args ) throw (Exception, std::exception)
     485             : {
     486             :     return createInstanceWithArgumentsAndContext(
     487           0 :         name, args, m_xContext );
     488             : }
     489             : 
     490             : } // anon namespace
     491             : 
     492             : 
     493             : extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
     494           9 : com_sun_star_comp_rendering_CanvasFactory_get_implementation(::com::sun::star::uno::XComponentContext* context,
     495             :                                                              ::com::sun::star::uno::Sequence<css::uno::Any> const &)
     496             : {
     497           9 :     return cppu::acquire(new CanvasFactory(context));
     498          27 : }
     499             : 
     500             : 
     501             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11