LCOV - code coverage report
Current view: top level - ucbhelper/source/client - contentbroker.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 76 95 80.0 %
Date: 2012-08-25 Functions: 18 18 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 55 144 38.2 %

           Branch data     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                 :            : /**************************************************************************
      22                 :            :                                 TODO
      23                 :            :  **************************************************************************
      24                 :            : 
      25                 :            :  *************************************************************************/
      26                 :            : #include <osl/diagnose.h>
      27                 :            : #include <osl/mutex.hxx>
      28                 :            : #include <rtl/instance.hxx>
      29                 :            : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
      30                 :            : #include <com/sun/star/lang/XComponent.hpp>
      31                 :            : #include <com/sun/star/ucb/XContentIdentifierFactory.hpp>
      32                 :            : #include <com/sun/star/ucb/XContentProvider.hpp>
      33                 :            : #include <com/sun/star/ucb/XContentProviderManager.hpp>
      34                 :            : #include <com/sun/star/ucb/XCommandProcessor.hpp>
      35                 :            : #include <ucbhelper/contentbroker.hxx>
      36                 :            : 
      37                 :            : #ifdef ANDROID
      38                 :            : #include <cppuhelper/bootstrap.hxx>
      39                 :            : #include <com/sun/star/uno/XComponentContext.hpp>
      40                 :            : #include <ucbhelper/configurationkeys.hxx>
      41                 :            : #endif
      42                 :            : 
      43                 :            : using namespace com::sun::star::lang;
      44                 :            : using namespace com::sun::star::ucb;
      45                 :            : using namespace com::sun::star::uno;
      46                 :            : 
      47                 :            : using ::rtl::OUString;
      48                 :            : 
      49                 :            : namespace
      50                 :            : {
      51                 :            :     struct theContentBrokerMutex : public rtl::Static< osl::Mutex, theContentBrokerMutex > {};
      52                 :            : } // namespace
      53                 :            : 
      54                 :            : namespace ucbhelper
      55                 :            : {
      56                 :            : 
      57                 :            : //=========================================================================
      58                 :            : //=========================================================================
      59                 :            : //
      60                 :            : // class ContentBroker_Impl.
      61                 :            : //
      62                 :            : //=========================================================================
      63                 :            : //=========================================================================
      64                 :            : 
      65                 :            : class ContentBroker_Impl
      66                 :            : {
      67                 :            :     Reference< XMultiServiceFactory >       m_xSMgr;
      68                 :            :     Reference< XContentIdentifierFactory >  m_xIdFac;
      69                 :            :     Reference< XContentProvider >           m_xProvider;
      70                 :            :     Reference< XContentProviderManager >    m_xProviderMgr;
      71                 :            :     Reference< XCommandProcessor >          m_xCommandProc;
      72                 :            :     osl::Mutex                              m_aMutex;
      73                 :            :     Sequence< Any >                         m_aArguments;
      74                 :            :     ContentProviderDataList                 m_aProvData;
      75                 :            :     bool                                    m_bInitDone;
      76                 :            : 
      77                 :            : public:
      78                 :       1062 :     ContentBroker_Impl( const Reference< XMultiServiceFactory >& rSMgr,
      79                 :            :                         const Sequence< Any >& rArguments )
      80 [ +  - ][ +  - ]:       1062 :     : m_xSMgr( rSMgr ), m_aArguments( rArguments ), m_bInitDone( sal_False )
                 [ +  - ]
      81                 :       1062 :     {}
      82                 :            : 
      83                 :            :     ContentBroker_Impl( const Reference< XMultiServiceFactory >& rSMgr,
      84                 :            :                         const ContentProviderDataList & rData )
      85                 :            :     : m_xSMgr( rSMgr ), m_aProvData( rData ), m_bInitDone( sal_False )
      86                 :            :     {}
      87                 :            : 
      88                 :            :     ~ContentBroker_Impl();
      89                 :            : 
      90                 :            :     bool initialize();
      91                 :            : 
      92                 :     129284 :     const Reference< XMultiServiceFactory >& getServiceManager() const
      93                 :     129284 :     { return m_xSMgr; }
      94                 :            : 
      95                 :     124598 :     const Reference< XContentIdentifierFactory >& getIdFactory() const
      96                 :     124598 :     { return m_xIdFac; }
      97                 :            : 
      98                 :     122372 :     const Reference< XContentProvider >& getProvider() const
      99                 :     122372 :     { return m_xProvider; }
     100                 :            : 
     101                 :      34110 :     const Reference< XContentProviderManager >& getProviderManager() const
     102                 :      34110 :     { return m_xProviderMgr; }
     103                 :            : 
     104                 :       1244 :     const Reference< XCommandProcessor >& getCommandProcessor() const
     105                 :       1244 :     { return m_xCommandProc; }
     106                 :            : };
     107                 :            : 
     108                 :            : //=========================================================================
     109                 :            : //=========================================================================
     110                 :            : //
     111                 :            : // ContentBroker Implementation.
     112                 :            : //
     113                 :            : //=========================================================================
     114                 :            : //=========================================================================
     115                 :            : 
     116                 :            : // static member!
     117                 :            : ContentBroker* ContentBroker::m_pTheBroker = 0;
     118                 :            : 
     119                 :            : //=========================================================================
     120                 :       1062 : ContentBroker::ContentBroker( const Reference< XMultiServiceFactory >& rSMgr,
     121                 :       1062 :                               const Sequence< Any >& rArguments )
     122                 :            : {
     123 [ +  - ][ +  - ]:       1062 :     m_pImpl = new ContentBroker_Impl( rSMgr, rArguments );
     124                 :       1062 : }
     125                 :            : 
     126                 :            : //=========================================================================
     127                 :       1062 : ContentBroker::~ContentBroker()
     128                 :            : {
     129 [ +  - ][ +  - ]:       1062 :     delete m_pImpl;
     130                 :       1062 : }
     131                 :            : 
     132                 :            : //=========================================================================
     133                 :     129284 : Reference< XMultiServiceFactory > ContentBroker::getServiceManager() const
     134                 :            : {
     135                 :     129284 :     return m_pImpl->getServiceManager();
     136                 :            : }
     137                 :            : 
     138                 :            : //=========================================================================
     139                 :            : Reference< XContentIdentifierFactory >
     140                 :     124598 :                 ContentBroker::getContentIdentifierFactoryInterface() const
     141                 :            : {
     142                 :     124598 :     return m_pImpl->getIdFactory();
     143                 :            : }
     144                 :            : 
     145                 :            : //=========================================================================
     146                 :            : Reference< XContentProvider >
     147                 :     122372 :                 ContentBroker::getContentProviderInterface() const
     148                 :            : {
     149                 :     122372 :     return m_pImpl->getProvider();
     150                 :            : }
     151                 :            : 
     152                 :            : //=========================================================================
     153                 :            : Reference< XContentProviderManager >
     154                 :      34110 :                 ContentBroker::getContentProviderManagerInterface() const
     155                 :            : {
     156                 :      34110 :     return m_pImpl->getProviderManager();
     157                 :            : }
     158                 :            : 
     159                 :            : //=========================================================================
     160                 :            : Reference< XCommandProcessor >
     161                 :       1244 :                 ContentBroker::getCommandProcessorInterface() const
     162                 :            : {
     163                 :       1244 :     return m_pImpl->getCommandProcessor();
     164                 :            : }
     165                 :            : 
     166                 :            : //=========================================================================
     167                 :            : // static
     168                 :       1062 : sal_Bool ContentBroker::initialize(
     169                 :            :                         const Reference< XMultiServiceFactory >& rSMgr,
     170                 :            :                         const Sequence< Any >& rArguments )
     171                 :            : {
     172                 :            :     OSL_ENSURE( !m_pTheBroker,
     173                 :            :                 "ContentBroker::initialize - already initialized!" );
     174                 :            : 
     175         [ +  - ]:       1062 :     if ( !m_pTheBroker )
     176                 :            :     {
     177 [ +  - ][ +  - ]:       1062 :         osl::Guard< osl::Mutex > aGuard( theContentBrokerMutex::get() );
     178                 :            : 
     179         [ +  - ]:       1062 :         if ( !m_pTheBroker )
     180                 :            :         {
     181 [ +  - ][ +  - ]:       1062 :             ContentBroker * pBroker = new ContentBroker( rSMgr, rArguments );
     182                 :            : 
     183                 :            :             // Force init to be able to detect UCB init trouble immediately.
     184 [ +  - ][ +  - ]:       1062 :             if ( pBroker->m_pImpl->initialize() )
     185                 :       1062 :                 m_pTheBroker = pBroker;
     186                 :            :             else
     187 [ #  # ][ #  # ]:          0 :                 delete pBroker;
     188         [ +  - ]:       1062 :         }
     189                 :            :     }
     190                 :            : 
     191                 :       1062 :     return m_pTheBroker != 0;
     192                 :            : }
     193                 :            : 
     194                 :            : #ifdef ANDROID
     195                 :            : 
     196                 :            : extern "C" __attribute__ ((visibility("default"))) void
     197                 :            : InitUCBHelper()
     198                 :            : {
     199                 :            :     Reference< XMultiServiceFactory > xFactory;
     200                 :            :     try
     201                 :            :     {
     202                 :            :         Reference< XComponentContext > xCtx = ::cppu::defaultBootstrap_InitialComponentContext();
     203                 :            :         xFactory = Reference< XMultiServiceFactory >(  xCtx->getServiceManager(),
     204                 :            :                                                        UNO_QUERY );
     205                 :            :     }
     206                 :            :     catch( Exception& )
     207                 :            :     {
     208                 :            :     }
     209                 :            : 
     210                 :            :     if( !xFactory.is() )
     211                 :            :     {
     212                 :            :         fprintf( stderr,
     213                 :            :                  "Could not bootstrap UNO, installation must be in disorder. Exiting.\n" );
     214                 :            :         exit( 1 );
     215                 :            :     }
     216                 :            : 
     217                 :            :     // Create UCB.
     218                 :            :     Sequence< Any > aArgs( 2 );
     219                 :            :     aArgs[ 0 ] <<= rtl::OUString( UCB_CONFIGURATION_KEY1_LOCAL );
     220                 :            :     aArgs[ 1 ] <<= rtl::OUString( UCB_CONFIGURATION_KEY2_OFFICE );
     221                 :            :     ::ucbhelper::ContentBroker::initialize( xFactory, aArgs );
     222                 :            : }
     223                 :            : 
     224                 :            : #endif
     225                 :            : 
     226                 :            : //=========================================================================
     227                 :            : // static
     228                 :       1077 : void ContentBroker::deinitialize()
     229                 :            : {
     230 [ +  - ][ +  - ]:       1077 :     osl::MutexGuard aGuard( theContentBrokerMutex::get() );
     231                 :            : 
     232 [ +  + ][ +  - ]:       1077 :     delete m_pTheBroker;
     233         [ +  - ]:       1077 :     m_pTheBroker = 0;
     234                 :       1077 : }
     235                 :            : 
     236                 :            : //=========================================================================
     237                 :            : // static
     238                 :     179244 : ContentBroker* ContentBroker::get()
     239                 :            : {
     240                 :     179244 :     return m_pTheBroker;
     241                 :            : }
     242                 :            : 
     243                 :            : //=========================================================================
     244                 :            : //=========================================================================
     245                 :            : //
     246                 :            : // ContentBroker_Impl Implementation.
     247                 :            : //
     248                 :            : //=========================================================================
     249                 :            : //=========================================================================
     250                 :            : 
     251 [ +  - ][ +  - ]:       1062 : ContentBroker_Impl::~ContentBroker_Impl()
     252                 :            : {
     253         [ +  - ]:       1062 :     Reference< XComponent > xComponent( m_xProvider, UNO_QUERY );
     254         [ +  - ]:       1062 :     if ( xComponent.is() )
     255                 :            :     {
     256         [ +  - ]:       1062 :         m_xIdFac       = 0;
     257         [ +  - ]:       1062 :         m_xProvider    = 0;
     258         [ +  - ]:       1062 :         m_xProviderMgr = 0;
     259                 :            : 
     260 [ +  - ][ +  - ]:       1062 :         xComponent->dispose();
     261                 :       1062 :     }
     262                 :       1062 : }
     263                 :            : 
     264                 :            : //=========================================================================
     265                 :       1062 : bool ContentBroker_Impl::initialize()
     266                 :            : {
     267         [ +  - ]:       1062 :     if ( !m_bInitDone )
     268                 :            :     {
     269         [ +  - ]:       1062 :         osl::MutexGuard aGuard( m_aMutex );
     270                 :            : 
     271         [ +  - ]:       1062 :         if ( !m_bInitDone )
     272                 :            :         {
     273                 :       1062 :             Reference< XInterface > xIfc;
     274                 :            : 
     275         [ -  + ]:       1062 :             if ( m_aProvData.size() > 0 )
     276                 :            :             {
     277                 :            :                 try
     278                 :            :                 {
     279         [ #  # ]:          0 :                     xIfc = m_xSMgr->createInstance(
     280                 :            :                             OUString(
     281 [ #  # ][ #  # ]:          0 :                                 "com.sun.star.ucb.UniversalContentBroker" ) );
                 [ #  # ]
     282                 :            :                 }
     283         [ #  # ]:          0 :                 catch ( Exception const & )
     284                 :            :                 {
     285                 :            :                 }
     286                 :            : 
     287         [ #  # ]:          0 :                 if ( xIfc.is() )
     288                 :            :                 {
     289                 :            :                     m_xProviderMgr
     290 [ #  # ][ #  # ]:          0 :                         = Reference< XContentProviderManager >( xIfc, UNO_QUERY );
     291                 :            : 
     292         [ #  # ]:          0 :                     if ( m_xProviderMgr.is() )
     293                 :            :                     {
     294         [ #  # ]:          0 :                         ContentProviderDataList::const_iterator aEnd(m_aProvData.end());
     295 [ #  # ][ #  # ]:          0 :                         for (ContentProviderDataList::const_iterator aIt(m_aProvData.begin());
                 [ #  # ]
     296                 :            :                             aIt != aEnd; ++aIt)
     297                 :            :                         {
     298                 :            :                             registerAtUcb(m_xProviderMgr,
     299                 :            :                                       m_xSMgr,
     300                 :          0 :                                       aIt->ServiceName,
     301                 :          0 :                                       aIt->Arguments,
     302                 :          0 :                                       aIt->URLTemplate,
     303         [ #  # ]:          0 :                                       0);
     304                 :            :                         }
     305                 :            : 
     306                 :            :                     }
     307                 :            :                 }
     308                 :            :             }
     309                 :            :             else
     310                 :            :             {
     311                 :            :                 try
     312                 :            :                 {
     313         [ +  - ]:       1062 :                     xIfc = m_xSMgr->createInstanceWithArguments(
     314                 :            :                             OUString(
     315                 :            :                                 "com.sun.star.ucb.UniversalContentBroker" ),
     316 [ +  - ][ +  - ]:       1062 :                             m_aArguments );
                 [ #  # ]
     317                 :            :                 }
     318         [ #  # ]:          0 :                 catch ( Exception const & )
     319                 :            :                 {
     320                 :            :                 }
     321                 :            :             }
     322                 :            : 
     323                 :            :             OSL_ENSURE( xIfc.is(), "Error creating UCB service 'com.sun.star.ucb.UniversalContentBroker'" );
     324                 :            : 
     325         [ -  + ]:       1062 :             if ( !xIfc.is() )
     326                 :          0 :                 return false;
     327                 :            : 
     328                 :            : 
     329                 :            :             m_xIdFac
     330 [ +  - ][ +  - ]:       1062 :                 = Reference< XContentIdentifierFactory >( xIfc, UNO_QUERY );
     331                 :            : 
     332                 :            :             OSL_ENSURE( m_xIdFac.is(),
     333                 :            :                 "UCB without required interface XContentIdentifierFactory!" );
     334                 :            : 
     335         [ -  + ]:       1062 :             if ( !m_xIdFac.is() )
     336                 :          0 :                 return false;
     337                 :            : 
     338 [ +  - ][ +  - ]:       1062 :             m_xProvider = Reference< XContentProvider >( xIfc, UNO_QUERY );
     339                 :            : 
     340                 :            :             OSL_ENSURE( m_xProvider.is(),
     341                 :            :                 "UCB without required interface XContentProvider!" );
     342                 :            : 
     343         [ -  + ]:       1062 :             if ( !m_xProvider.is() )
     344                 :          0 :                 return false;
     345                 :            : 
     346         [ +  - ]:       1062 :             if ( !m_xProviderMgr.is() )
     347                 :            :                 m_xProviderMgr
     348 [ +  - ][ +  - ]:       1062 :                     = Reference< XContentProviderManager >( xIfc, UNO_QUERY );
     349                 :            : 
     350                 :            :             OSL_ENSURE( m_xProviderMgr.is(),
     351                 :            :                 "UCB without required interface XContentProviderManager!" );
     352                 :            : 
     353         [ -  + ]:       1062 :             if ( !m_xProviderMgr.is() )
     354                 :          0 :                 return false;
     355                 :            : 
     356 [ +  - ][ +  - ]:       1062 :             m_xCommandProc = Reference< XCommandProcessor >( xIfc, UNO_QUERY );
     357                 :            : 
     358                 :            :             OSL_ENSURE( m_xCommandProc.is(),
     359                 :            :                 "UCB without required interface XCommandProcessor!" );
     360                 :            : 
     361         [ -  + ]:       1062 :             if ( !m_xCommandProc.is() )
     362                 :          0 :                 return false;
     363                 :            : 
     364                 :            :             // Everything okay.
     365         [ +  - ]:       1062 :             m_bInitDone = sal_True;
     366 [ +  - ][ +  - ]:       1062 :         }
     367                 :            :     }
     368                 :            : 
     369                 :       1062 :     return true;
     370                 :            : }
     371                 :            : 
     372                 :            : } /* namespace ucbhelper */
     373                 :            : 
     374                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10