LCOV - code coverage report
Current view: top level - ucb/source/ucp/package - pkgprovider.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 69 78 88.5 %
Date: 2014-11-03 Functions: 24 28 85.7 %
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             : /**************************************************************************
      22             :                                 TODO
      23             :  **************************************************************************
      24             : 
      25             :  *************************************************************************/
      26             : 
      27             : #include <boost/unordered_map.hpp>
      28             : #include <osl/diagnose.h>
      29             : #include <comphelper/processfactory.hxx>
      30             : #include <cppuhelper/weak.hxx>
      31             : #include <ucbhelper/contentidentifier.hxx>
      32             : #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
      33             : #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
      34             : #include "pkgprovider.hxx"
      35             : #include "pkgcontent.hxx"
      36             : #include "pkguri.hxx"
      37             : 
      38             : using namespace com::sun::star;
      39             : 
      40             : namespace package_ucp
      41             : {
      42             : 
      43             : 
      44             : 
      45             : // class Package.
      46             : 
      47             : 
      48             : 
      49             : class Package : public cppu::OWeakObject,
      50             :                 public container::XHierarchicalNameAccess
      51             : {
      52             :     friend class ContentProvider;
      53             : 
      54             :     OUString                                        m_aName;
      55             :     uno::Reference< container::XHierarchicalNameAccess > m_xNA;
      56             :     ContentProvider*                                     m_pOwner;
      57             : 
      58             : public:
      59          34 :     Package( const OUString& rName,
      60             :              const uno::Reference< container::XHierarchicalNameAccess > & xNA,
      61             :              ContentProvider* pOwner )
      62          34 :     : m_aName( rName ), m_xNA( xNA ), m_pOwner( pOwner ) {}
      63          68 :     virtual ~Package() { m_pOwner->removePackage( m_aName ); }
      64             : 
      65             :     // XInterface
      66             :     virtual uno::Any SAL_CALL
      67          32 :     queryInterface( const uno::Type& aType )
      68             :         throw( uno::RuntimeException, std::exception ) SAL_OVERRIDE
      69          32 :     { return m_xNA->queryInterface( aType ); }
      70             :     virtual void SAL_CALL
      71         168 :     acquire() throw() SAL_OVERRIDE
      72         168 :     { OWeakObject::acquire(); }
      73             :     virtual void SAL_CALL
      74         168 :     release() throw() SAL_OVERRIDE
      75         168 :     { OWeakObject::release(); }
      76             : 
      77             :     // XHierarchicalNameAccess
      78             :     virtual uno::Any SAL_CALL
      79          66 :     getByHierarchicalName( const OUString& aName )
      80             :         throw( container::NoSuchElementException, uno::RuntimeException, std::exception ) SAL_OVERRIDE
      81          66 :     { return m_xNA->getByHierarchicalName( aName ); }
      82             :     virtual sal_Bool SAL_CALL
      83          66 :     hasByHierarchicalName( const OUString& aName )
      84             :         throw( uno::RuntimeException, std::exception ) SAL_OVERRIDE
      85          66 :     { return m_xNA->hasByHierarchicalName( aName ); }
      86             : };
      87             : 
      88             : 
      89             : 
      90             : // Packages.
      91             : 
      92             : 
      93             : 
      94             : typedef boost::unordered_map
      95             : <
      96             :     OUString,
      97             :     Package*,
      98             :     OUStringHash
      99             : >
     100             : PackageMap;
     101             : 
     102          28 : class Packages : public PackageMap {};
     103             : 
     104             : }
     105             : 
     106             : using namespace package_ucp;
     107             : 
     108             : 
     109             : 
     110             : 
     111             : // ContentProvider Implementation.
     112             : 
     113             : 
     114             : 
     115             : 
     116          14 : ContentProvider::ContentProvider(
     117             :             const uno::Reference< uno::XComponentContext >& rxContext )
     118             : : ::ucbhelper::ContentProviderImplHelper( rxContext ),
     119          14 :   m_pPackages( 0 )
     120             : {
     121          14 : }
     122             : 
     123             : 
     124             : // virtual
     125          42 : ContentProvider::~ContentProvider()
     126             : {
     127          14 :     delete m_pPackages;
     128          28 : }
     129             : 
     130             : // XInterface methods.
     131        1324 : void SAL_CALL ContentProvider::acquire()
     132             :     throw()
     133             : {
     134        1324 :     OWeakObject::acquire();
     135        1324 : }
     136             : 
     137        1324 : void SAL_CALL ContentProvider::release()
     138             :     throw()
     139             : {
     140        1324 :     OWeakObject::release();
     141        1324 : }
     142             : 
     143         112 : css::uno::Any SAL_CALL ContentProvider::queryInterface( const css::uno::Type & rType )
     144             :     throw( css::uno::RuntimeException, std::exception )
     145             : {
     146             :     css::uno::Any aRet = cppu::queryInterface( rType,
     147             :                                                (static_cast< lang::XTypeProvider* >(this)),
     148             :                                                (static_cast< lang::XServiceInfo* >(this)),
     149             :                                                (static_cast< ucb::XContentProvider* >(this))
     150         112 :                                                );
     151         112 :     return aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType );
     152             : }
     153             : 
     154             : // XTypeProvider methods.
     155             : 
     156             : 
     157             : 
     158           0 : XTYPEPROVIDER_IMPL_3( ContentProvider,
     159             :                       lang::XTypeProvider,
     160             :                       lang::XServiceInfo,
     161             :                       ucb::XContentProvider );
     162             : 
     163             : 
     164             : 
     165             : // XServiceInfo methods.
     166             : 
     167             : 
     168             : 
     169          60 : XSERVICEINFO_IMPL_1_CTX( ContentProvider,
     170             :                      OUString( "com.sun.star.comp.ucb.PackageContentProvider" ),
     171             :                      OUString( PACKAGE_CONTENT_PROVIDER_SERVICE_NAME ) );
     172             : 
     173             : 
     174             : 
     175             : // Service factory implementation.
     176             : 
     177             : 
     178             : 
     179          14 : ONE_INSTANCE_SERVICE_FACTORY_IMPL( ContentProvider );
     180             : 
     181             : 
     182             : 
     183             : // XContentProvider methods.
     184             : 
     185             : 
     186             : 
     187             : // virtual
     188          54 : uno::Reference< ucb::XContent > SAL_CALL ContentProvider::queryContent(
     189             :             const uno::Reference< ucb::XContentIdentifier >& Identifier )
     190             :     throw( ucb::IllegalIdentifierException, uno::RuntimeException, std::exception )
     191             : {
     192          54 :     if ( !Identifier.is() )
     193           0 :         return uno::Reference< ucb::XContent >();
     194             : 
     195          54 :     PackageUri aUri( Identifier->getContentIdentifier() );
     196          54 :     if ( !aUri.isValid() )
     197           0 :         throw ucb::IllegalIdentifierException();
     198             : 
     199             :     // Create a new identifier for the mormalized URL returned by
     200             :     // PackageUri::getUri().
     201         108 :     uno::Reference< ucb::XContentIdentifier > xId = new ::ucbhelper::ContentIdentifier( aUri.getUri() );
     202             : 
     203         108 :     osl::MutexGuard aGuard( m_aMutex );
     204             : 
     205             :     // Check, if a content with given id already exists...
     206             :     uno::Reference< ucb::XContent > xContent
     207         108 :         = queryExistingContent( xId ).get();
     208          54 :     if ( xContent.is() )
     209           4 :         return xContent;
     210             : 
     211             :     // Create a new content.
     212             : 
     213          50 :     xContent = Content::create( m_xContext, this, Identifier ); // not xId!!!
     214          50 :     registerNewContent( xContent );
     215             : 
     216          50 :     if ( xContent.is() && !xContent->getIdentifier().is() )
     217           0 :         throw ucb::IllegalIdentifierException();
     218             : 
     219         104 :     return xContent;
     220             : }
     221             : 
     222             : 
     223             : 
     224             : // Other methods.
     225             : 
     226             : 
     227             : 
     228             : uno::Reference< container::XHierarchicalNameAccess >
     229          66 : ContentProvider::createPackage( const PackageUri & rURI )
     230             : {
     231          66 :     osl::MutexGuard aGuard( m_aMutex );
     232             : 
     233         132 :     OUString rURL = rURI.getPackage() + rURI.getParam();
     234             : 
     235          66 :     if ( m_pPackages )
     236             :     {
     237          52 :         Packages::const_iterator it = m_pPackages->find( rURL );
     238          52 :         if ( it != m_pPackages->end() )
     239             :         {
     240             :             // Already instantiated. Return package.
     241          32 :             return (*it).second->m_xNA;
     242             :         }
     243             :     }
     244             :     else
     245          14 :         m_pPackages = new Packages;
     246             : 
     247             :     // Create new package...
     248          68 :     uno::Sequence< uno::Any > aArguments( 1 );
     249          34 :     aArguments[ 0 ] <<= rURL;
     250          68 :     uno::Reference< container::XHierarchicalNameAccess > xNameAccess;
     251             :     try
     252             :     {
     253          68 :         xNameAccess = uno::Reference< container::XHierarchicalNameAccess >(
     254          68 :             m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
     255             :                 "com.sun.star.packages.comp.ZipPackage",
     256          34 :                 aArguments, m_xContext ),
     257          34 :             css::uno::UNO_QUERY_THROW );
     258             :     }
     259           0 :     catch ( uno::RuntimeException const & )
     260             :     {
     261           0 :         throw;
     262             :     }
     263           0 :     catch ( uno::Exception const & e )
     264             :     {
     265             :         throw css::lang::WrappedTargetRuntimeException(
     266           0 :             e.Message, e.Context, css::uno::makeAny(e));
     267             :     }
     268             : 
     269          68 :     rtl::Reference< Package> xPackage = new Package( rURL, xNameAccess, this );
     270          34 :     (*m_pPackages)[ rURL ] = xPackage.get();
     271         100 :     return xPackage.get();
     272             : }
     273             : 
     274             : 
     275          34 : bool ContentProvider::removePackage( const OUString & rName )
     276             : {
     277          34 :     osl::MutexGuard aGuard( m_aMutex );
     278             : 
     279          34 :     if ( m_pPackages )
     280             :     {
     281          34 :         Packages::iterator it = m_pPackages->find( rName );
     282          34 :         if ( it != m_pPackages->end() )
     283             :         {
     284          34 :             m_pPackages->erase( it );
     285          34 :             return true;
     286             :         }
     287             :     }
     288           0 :     return false;
     289             : }
     290             : 
     291             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10