LCOV - code coverage report
Current view: top level - unotools/source/config - extendedsecurityoptions.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 81 0.0 %
Date: 2012-08-25 Functions: 0 16 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 158 0.0 %

           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                 :            : #include <unotools/extendedsecurityoptions.hxx>
      21                 :            : #include <unotools/configmgr.hxx>
      22                 :            : #include <unotools/configitem.hxx>
      23                 :            : #include <tools/debug.hxx>
      24                 :            : #include <com/sun/star/uno/Any.hxx>
      25                 :            : #include <com/sun/star/uno/Sequence.hxx>
      26                 :            : #include <rtl/ustrbuf.hxx>
      27                 :            : 
      28                 :            : #include <unotools/pathoptions.hxx>
      29                 :            : 
      30                 :            : #include <boost/unordered_map.hpp>
      31                 :            : 
      32                 :            : #include <rtl/logfile.hxx>
      33                 :            : #include "itemholder1.hxx"
      34                 :            : 
      35                 :            : using namespace ::utl                   ;
      36                 :            : using namespace ::rtl                   ;
      37                 :            : using namespace ::osl                   ;
      38                 :            : using namespace ::com::sun::star::uno   ;
      39                 :            : 
      40                 :            : #define ROOTNODE_SECURITY               OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Security"))
      41                 :            : 
      42                 :            : #define SECURE_EXTENSIONS_SET           OUString(RTL_CONSTASCII_USTRINGPARAM("SecureExtensions"))
      43                 :            : #define EXTENSION_PROPNAME              OUString(RTL_CONSTASCII_USTRINGPARAM("/Extension"))
      44                 :            : 
      45                 :            : #define PROPERTYNAME_HYPERLINKS_OPEN    OUString(RTL_CONSTASCII_USTRINGPARAM("Hyperlinks/Open"))
      46                 :            : 
      47                 :            : #define PROPERTYHANDLE_HYPERLINKS_OPEN  0
      48                 :            : 
      49                 :            : #define PROPERTYCOUNT                   1
      50                 :            : 
      51                 :            : struct OUStringHashCode
      52                 :            : {
      53                 :          0 :     size_t operator()( const ::rtl::OUString& sString ) const
      54                 :            :     {
      55                 :          0 :         return sString.hashCode();
      56                 :            :     }
      57                 :            : };
      58                 :            : 
      59         [ #  # ]:          0 : class ExtensionHashMap : public ::boost::unordered_map< ::rtl::OUString,
      60                 :            :                                                  sal_Int32,
      61                 :            :                                                  OUStringHashCode,
      62                 :            :                                                  ::std::equal_to< ::rtl::OUString > >
      63                 :            : {
      64                 :            :     public:
      65                 :            :         inline void free()
      66                 :            :         {
      67                 :            :             ExtensionHashMap().swap( *this );
      68                 :            :         }
      69                 :            : };
      70                 :            : 
      71                 :            : class SvtExtendedSecurityOptions_Impl : public ConfigItem
      72                 :            : {
      73                 :            :     public:
      74                 :            :          SvtExtendedSecurityOptions_Impl();
      75                 :            :         ~SvtExtendedSecurityOptions_Impl();
      76                 :            : 
      77                 :            :         /*-****************************************************************************************************//**
      78                 :            :             @short      called for notify of configmanager
      79                 :            :             @descr      These method is called from the ConfigManager before application ends or from the
      80                 :            :                          PropertyChangeListener if the sub tree broadcasts changes. You must update your
      81                 :            :                         internal values.
      82                 :            : 
      83                 :            :             @seealso    baseclass ConfigItem
      84                 :            : 
      85                 :            :             @param      "seqPropertyNames" is the list of properties which should be updated.
      86                 :            :             @return     -
      87                 :            : 
      88                 :            :             @onerror    -
      89                 :            :         *//*-*****************************************************************************************************/
      90                 :            : 
      91                 :            :         virtual void Notify( const Sequence< OUString >& seqPropertyNames );
      92                 :            : 
      93                 :            :         /*-****************************************************************************************************//**
      94                 :            :             @short      write changes to configuration
      95                 :            :             @descr      These method writes the changed values into the sub tree
      96                 :            :                         and should always called in our destructor to guarantee consistency of config data.
      97                 :            : 
      98                 :            :             @seealso    baseclass ConfigItem
      99                 :            : 
     100                 :            :             @param      -
     101                 :            :             @return     -
     102                 :            : 
     103                 :            :             @onerror    -
     104                 :            :         *//*-*****************************************************************************************************/
     105                 :            : 
     106                 :            :         virtual void Commit();
     107                 :            : 
     108                 :            :         SvtExtendedSecurityOptions::OpenHyperlinkMode   GetOpenHyperlinkMode();
     109                 :            :     private:
     110                 :            : 
     111                 :            :         /*-****************************************************************************************************//**
     112                 :            :             @short      return list of key names of ouer configuration management which represent oue module tree
     113                 :            :             @descr      These methods return a static const list of key names. We need it to get needed values from our
     114                 :            :                         configuration management.
     115                 :            : 
     116                 :            :             @seealso    -
     117                 :            : 
     118                 :            :             @param      -
     119                 :            :             @return     A list of needed configuration keys is returned.
     120                 :            : 
     121                 :            :             @onerror    -
     122                 :            :         *//*-*****************************************************************************************************/
     123                 :            : 
     124                 :            :         static Sequence< OUString > GetPropertyNames();
     125                 :            : 
     126                 :            :         /*-****************************************************************************************************//**
     127                 :            :             @short      Fills the hash map with all extensions known to be secure
     128                 :            :             @descr      These methods fills the given hash map object with all extensions known to be secure.
     129                 :            : 
     130                 :            :             @seealso    -
     131                 :            : 
     132                 :            :             @param      aHashMap
     133                 :            :                         A hash map to be filled with secure extension strings.
     134                 :            :             @return     -
     135                 :            : 
     136                 :            :             @onerror    -
     137                 :            :         *//*-*****************************************************************************************************/
     138                 :            :         void FillExtensionHashMap( ExtensionHashMap& aHashMap );
     139                 :            : 
     140                 :            :         OUString                                        m_aSecureExtensionsSetName;
     141                 :            :         OUString                                        m_aExtensionPropName;
     142                 :            : 
     143                 :            :         SvtExtendedSecurityOptions::OpenHyperlinkMode   m_eOpenHyperlinkMode;
     144                 :            :         sal_Bool                                        m_bROOpenHyperlinkMode;
     145                 :            :         ExtensionHashMap                                m_aExtensionHashMap;
     146                 :            : };
     147                 :            : 
     148                 :            : //*****************************************************************************************************************
     149                 :            : //  constructor
     150                 :            : //*****************************************************************************************************************
     151                 :          0 : SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()
     152                 :            :     // Init baseclasses first
     153                 :            :     :   ConfigItem          ( ROOTNODE_SECURITY         ),
     154                 :            :     m_aSecureExtensionsSetName( SECURE_EXTENSIONS_SET ),
     155                 :            :     m_aExtensionPropName( EXTENSION_PROPNAME ),
     156 [ #  # ][ #  # ]:          0 :     m_bROOpenHyperlinkMode(sal_False)
         [ #  # ][ #  # ]
     157                 :            :     // Init member then.
     158                 :            : {
     159                 :            :     // Fill the extension hash map with all secure extension strings
     160         [ #  # ]:          0 :     FillExtensionHashMap( m_aExtensionHashMap );
     161                 :            : 
     162         [ #  # ]:          0 :     Sequence< OUString >    seqNames    = GetPropertyNames();
     163         [ #  # ]:          0 :     Sequence< Any >         seqValues   = GetProperties( seqNames );
     164         [ #  # ]:          0 :     Sequence< sal_Bool >    seqRO       = GetReadOnlyStates ( seqNames  );
     165                 :            : 
     166                 :          0 :     sal_Int32 nPropertyCount = seqValues.getLength();
     167         [ #  # ]:          0 :     for( sal_Int32 nProperty=0; nProperty<nPropertyCount; ++nProperty )
     168                 :            :     {
     169                 :            :         // Safe impossible cases.
     170                 :            :         // Check any for valid value.
     171                 :            :         DBG_ASSERT( !(seqValues[nProperty].hasValue()==sal_False), "SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()\nInvalid property value detected!\n" );
     172         [ #  # ]:          0 :         switch( nProperty )
     173                 :            :         {
     174                 :            :             case PROPERTYHANDLE_HYPERLINKS_OPEN:
     175                 :            :             {
     176                 :            :                 DBG_ASSERT( ( seqValues[nProperty].getValueTypeClass() == TypeClass_LONG ), "SvtExtendedSecurityOptions_Impl::SvtExtendedSecurityOptions_Impl()\nWho has changed the value type of 'Hyperlink/Open'?" );
     177                 :            : 
     178                 :          0 :                 sal_Int32 nMode = SvtExtendedSecurityOptions::OPEN_WITHSECURITYCHECK;
     179 [ #  # ][ #  # ]:          0 :                 if ( seqValues[nProperty] >>= nMode )
     180                 :          0 :                     m_eOpenHyperlinkMode = (SvtExtendedSecurityOptions::OpenHyperlinkMode)nMode;
     181                 :            :                 else {
     182                 :            :                     OSL_FAIL("Wrong type for Open mode!");
     183                 :            :                 }
     184         [ #  # ]:          0 :                 m_bROOpenHyperlinkMode = seqRO[nProperty];
     185                 :            :             }
     186                 :          0 :             break;
     187                 :            :         }
     188                 :            :     }
     189                 :            : 
     190                 :            :     // Enable notification mechanism of our baseclass.
     191                 :            :     // We need it to get information about changes outside these class on ouer used configuration keys!
     192         [ #  # ]:          0 :     Sequence< OUString > seqNotifyNames( 1 );
     193         [ #  # ]:          0 :     seqNotifyNames[0] = m_aSecureExtensionsSetName;
     194 [ #  # ][ #  # ]:          0 :     EnableNotification( seqNotifyNames );
         [ #  # ][ #  # ]
                 [ #  # ]
     195                 :          0 : }
     196                 :            : 
     197                 :            : //*****************************************************************************************************************
     198                 :            : //  destructor
     199                 :            : //*****************************************************************************************************************
     200         [ #  # ]:          0 : SvtExtendedSecurityOptions_Impl::~SvtExtendedSecurityOptions_Impl()
     201                 :            : {
     202                 :            :     // We must save our current values .. if user forget it!
     203 [ #  # ][ #  # ]:          0 :     if( IsModified() == sal_True )
     204                 :            :     {
     205         [ #  # ]:          0 :         Commit();
     206                 :            :     }
     207         [ #  # ]:          0 : }
     208                 :            : 
     209                 :            : //*****************************************************************************************************************
     210                 :            : //  public method
     211                 :            : //*****************************************************************************************************************
     212                 :          0 : void SvtExtendedSecurityOptions_Impl::Notify( const Sequence< OUString >& )
     213                 :            : {
     214                 :            :     // Not implemented
     215                 :          0 : }
     216                 :            : 
     217                 :            : //*****************************************************************************************************************
     218                 :            : //  public method
     219                 :            : //*****************************************************************************************************************
     220                 :          0 : void SvtExtendedSecurityOptions_Impl::Commit()
     221                 :            : {
     222                 :            :     // Get names of supported properties, create a list for values and copy current values to it.
     223         [ #  # ]:          0 :     Sequence< OUString >    seqNames    = GetPropertyNames  ();
     224                 :          0 :     sal_Int32               nCount      = seqNames.getLength();
     225         [ #  # ]:          0 :     Sequence< Any >         seqValues   ( nCount );
     226         [ #  # ]:          0 :     for( sal_Int32 nProperty=0; nProperty<nCount; ++nProperty )
     227                 :            :     {
     228         [ #  # ]:          0 :         switch( nProperty )
     229                 :            :         {
     230                 :            :             case PROPERTYHANDLE_HYPERLINKS_OPEN:    {
     231 [ #  # ][ #  # ]:          0 :                                                         seqValues[nProperty] <<= (sal_Int32)m_eOpenHyperlinkMode;
     232                 :            :                                                     }
     233                 :          0 :                                                     break;
     234                 :            :         }
     235                 :            :     }
     236                 :            : 
     237                 :            :     // Set properties in configuration.
     238 [ #  # ][ #  # ]:          0 :     PutProperties( seqNames, seqValues );
                 [ #  # ]
     239                 :          0 : }
     240                 :            : 
     241                 :            : //*****************************************************************************************************************
     242                 :            : //  public method
     243                 :            : //*****************************************************************************************************************
     244                 :          0 : SvtExtendedSecurityOptions::OpenHyperlinkMode SvtExtendedSecurityOptions_Impl::GetOpenHyperlinkMode()
     245                 :            : {
     246                 :          0 :     return m_eOpenHyperlinkMode;
     247                 :            : }
     248                 :            : 
     249                 :            : //*****************************************************************************************************************
     250                 :            : //  private method
     251                 :            : //*****************************************************************************************************************
     252                 :          0 : void SvtExtendedSecurityOptions_Impl::FillExtensionHashMap( ExtensionHashMap& aHashMap )
     253                 :            : {
     254                 :            :     // Get sequence with secure extensions from configuration
     255         [ #  # ]:          0 :     Sequence< OUString >    seqNodes = GetNodeNames( m_aSecureExtensionsSetName );
     256                 :            : 
     257                 :          0 :     OUString                aValue;
     258         [ #  # ]:          0 :     Sequence< Any >         aValues;
     259         [ #  # ]:          0 :     Sequence< OUString >    aPropSeq( 1 );
     260         [ #  # ]:          0 :     for ( int i = 0; i < seqNodes.getLength(); i++ )
     261                 :            :     {
     262                 :            :         // Create access name for property
     263         [ #  # ]:          0 :         OUStringBuffer aExtEntryProp( m_aSecureExtensionsSetName );
     264         [ #  # ]:          0 :         aExtEntryProp.appendAscii( "/" );
     265 [ #  # ][ #  # ]:          0 :         aExtEntryProp.append( seqNodes[i] );
     266         [ #  # ]:          0 :         aExtEntryProp.append( m_aExtensionPropName );
     267                 :            : 
     268 [ #  # ][ #  # ]:          0 :         aPropSeq[0] = aExtEntryProp.makeStringAndClear();
     269 [ #  # ][ #  # ]:          0 :         aValues = GetProperties( aPropSeq );
                 [ #  # ]
     270         [ #  # ]:          0 :         if ( aValues.getLength() == 1 )
     271                 :            :         {
     272                 :            :             // Don't use value if sequence has not the correct length
     273 [ #  # ][ #  # ]:          0 :             if ( aValues[0] >>= aValue )
     274                 :            :                 // Add extension into secure extensions hash map
     275         [ #  # ]:          0 :                 aHashMap.insert( ExtensionHashMap::value_type( aValue.toAsciiLowerCase(), 1 ) );
     276                 :            :             else
     277                 :            :             {
     278                 :            :                 SAL_WARN( "unotools.config", "SvtExtendedSecurityOptions_Impl::FillExtensionHashMap(): not string value?" );
     279                 :            :             }
     280                 :            :         }
     281 [ #  # ][ #  # ]:          0 :     }
                 [ #  # ]
     282                 :          0 : }
     283                 :            : 
     284                 :            : //*****************************************************************************************************************
     285                 :            : //  private method (currently not used)
     286                 :            : //*****************************************************************************************************************
     287                 :          0 : Sequence< OUString > SvtExtendedSecurityOptions_Impl::GetPropertyNames()
     288                 :            : {
     289                 :            :     // Build list of configuration key names.
     290                 :            :     const OUString pProperties[] =
     291                 :            :     {
     292                 :            :         PROPERTYNAME_HYPERLINKS_OPEN
     293         [ #  # ]:          0 :     };
           [ #  #  #  # ]
     294                 :            :     // Initialize return sequence with these list ...
     295         [ #  # ]:          0 :     const Sequence< OUString > seqPropertyNames( pProperties, PROPERTYCOUNT );
     296                 :            :     // ... and return it.
     297 [ #  # ][ #  # ]:          0 :     return seqPropertyNames;
     298                 :            : }
     299                 :            : 
     300                 :            : //*****************************************************************************************************************
     301                 :            : //  initialize static member
     302                 :            : //  DON'T DO IT IN YOUR HEADER!
     303                 :            : //  see definition for further informations
     304                 :            : //*****************************************************************************************************************
     305                 :            : SvtExtendedSecurityOptions_Impl*    SvtExtendedSecurityOptions::m_pDataContainer    = NULL  ;
     306                 :            : sal_Int32                           SvtExtendedSecurityOptions::m_nRefCount         = 0     ;
     307                 :            : 
     308                 :            : //*****************************************************************************************************************
     309                 :            : //  constructor
     310                 :            : //*****************************************************************************************************************
     311                 :          0 : SvtExtendedSecurityOptions::SvtExtendedSecurityOptions()
     312                 :            : {
     313                 :            :     // Global access, must be guarded (multithreading!).
     314 [ #  # ][ #  # ]:          0 :     MutexGuard aGuard( GetInitMutex() );
     315                 :            :     // Increase ouer refcount ...
     316                 :          0 :     ++m_nRefCount;
     317                 :            :     // ... and initialize ouer data container only if it not already exist!
     318         [ #  # ]:          0 :     if( m_pDataContainer == NULL )
     319                 :            :     {
     320                 :            :        RTL_LOGFILE_CONTEXT(aLog, "unotools ( ??? ) ::SvtExtendedSecurityOptions_Impl::ctor()");
     321 [ #  # ][ #  # ]:          0 :        m_pDataContainer = new SvtExtendedSecurityOptions_Impl;
     322                 :            : 
     323         [ #  # ]:          0 :         ItemHolder1::holdConfigItem(E_EXTENDEDSECURITYOPTIONS);
     324         [ #  # ]:          0 :     }
     325                 :          0 : }
     326                 :            : 
     327                 :            : //*****************************************************************************************************************
     328                 :            : //  destructor
     329                 :            : //*****************************************************************************************************************
     330                 :          0 : SvtExtendedSecurityOptions::~SvtExtendedSecurityOptions()
     331                 :            : {
     332                 :            :     // Global access, must be guarded (multithreading!)
     333 [ #  # ][ #  # ]:          0 :     MutexGuard aGuard( GetInitMutex() );
     334                 :            :     // Decrease ouer refcount.
     335                 :          0 :     --m_nRefCount;
     336                 :            :     // If last instance was deleted ...
     337                 :            :     // we must destroy ouer static data container!
     338         [ #  # ]:          0 :     if( m_nRefCount <= 0 )
     339                 :            :     {
     340 [ #  # ][ #  # ]:          0 :         delete m_pDataContainer;
     341                 :          0 :         m_pDataContainer = NULL;
     342         [ #  # ]:          0 :     }
     343         [ #  # ]:          0 : }
     344                 :            : 
     345                 :            : //*****************************************************************************************************************
     346                 :            : //  public method
     347                 :            : //*****************************************************************************************************************
     348                 :          0 : SvtExtendedSecurityOptions::OpenHyperlinkMode SvtExtendedSecurityOptions::GetOpenHyperlinkMode()
     349                 :            : {
     350 [ #  # ][ #  # ]:          0 :     MutexGuard aGuard( GetInitMutex() );
     351         [ #  # ]:          0 :     return m_pDataContainer->GetOpenHyperlinkMode();
     352                 :            : }
     353                 :            : 
     354                 :            : namespace
     355                 :            : {
     356                 :            :     class theExtendedSecurityOptionsMutex : public rtl::Static<osl::Mutex, theExtendedSecurityOptionsMutex>{};
     357                 :            : }
     358                 :            : 
     359                 :            : //*****************************************************************************************************************
     360                 :            : //  private method
     361                 :            : //*****************************************************************************************************************
     362                 :          0 : Mutex& SvtExtendedSecurityOptions::GetInitMutex()
     363                 :            : {
     364                 :          0 :     return theExtendedSecurityOptionsMutex::get();
     365                 :            : }
     366                 :            : 
     367                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10