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

Generated by: LCOV version 1.11