LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/comphelper/source/misc - instancelocker.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 8 203 3.9 %
Date: 2013-07-09 Functions: 3 24 12.5 %
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 "comphelper_module.hxx"
      22             : 
      23             : #include <com/sun/star/util/XCloseBroadcaster.hpp>
      24             : #include <com/sun/star/util/XCloseable.hpp>
      25             : #include <com/sun/star/lang/DisposedException.hpp>
      26             : #include <com/sun/star/lang/IllegalArgumentException.hpp>
      27             : #include <com/sun/star/frame/XDesktop.hpp>
      28             : #include <com/sun/star/frame/DoubleInitializationException.hpp>
      29             : #include <com/sun/star/beans/XPropertySet.hpp>
      30             : 
      31             : #include "instancelocker.hxx"
      32             : 
      33             : using namespace ::com::sun::star;
      34             : 
      35             : 
      36             : // ====================================================================
      37             : // OInstanceLocker
      38             : // ====================================================================
      39             : 
      40             : // --------------------------------------------------------
      41           0 : OInstanceLocker::OInstanceLocker( const uno::Reference< uno::XComponentContext >& xContext )
      42             : : m_xContext( xContext )
      43             : , m_pLockListener( NULL )
      44             : , m_pListenersContainer( NULL )
      45             : , m_bDisposed( sal_False )
      46           0 : , m_bInitialized( sal_False )
      47             : {
      48           0 : }
      49             : 
      50             : // --------------------------------------------------------
      51           0 : OInstanceLocker::~OInstanceLocker()
      52             : {
      53           0 :     if ( !m_bDisposed )
      54             :     {
      55           0 :         m_refCount++; // to call dispose
      56             :         try {
      57           0 :             dispose();
      58             :         }
      59           0 :         catch ( uno::RuntimeException& )
      60             :         {}
      61             :     }
      62             : 
      63           0 :     if ( m_pListenersContainer )
      64             :     {
      65           0 :         delete m_pListenersContainer;
      66           0 :         m_pListenersContainer = NULL;
      67             :     }
      68           0 : }
      69             : 
      70             : // XComponent
      71             : // --------------------------------------------------------
      72           0 : void SAL_CALL OInstanceLocker::dispose()
      73             :     throw (uno::RuntimeException)
      74             : {
      75           0 :     ::osl::MutexGuard aGuard( m_aMutex );
      76             : 
      77           0 :     if ( m_bDisposed )
      78           0 :         throw lang::DisposedException();
      79             : 
      80           0 :        lang::EventObject aSource( static_cast< ::cppu::OWeakObject* >(this) );
      81           0 :     if ( m_pListenersContainer )
      82           0 :         m_pListenersContainer->disposeAndClear( aSource );
      83             : 
      84           0 :     if ( m_xLockListener.is() )
      85             :     {
      86           0 :         if ( m_pLockListener )
      87             :         {
      88           0 :             m_pLockListener->Dispose();
      89           0 :             m_pLockListener = NULL;
      90             :         }
      91           0 :         m_xLockListener = uno::Reference< uno::XInterface >();
      92             :     }
      93             : 
      94           0 :     m_bDisposed = sal_True;
      95           0 : }
      96             : 
      97             : // --------------------------------------------------------
      98           0 : void SAL_CALL OInstanceLocker::addEventListener( const uno::Reference< lang::XEventListener >& xListener )
      99             :     throw (uno::RuntimeException)
     100             : {
     101           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     102           0 :     if ( m_bDisposed )
     103           0 :         throw lang::DisposedException(); // TODO
     104             : 
     105           0 :     if ( !m_pListenersContainer )
     106           0 :         m_pListenersContainer = new ::cppu::OInterfaceContainerHelper( m_aMutex );
     107             : 
     108           0 :     m_pListenersContainer->addInterface( xListener );
     109           0 : }
     110             : 
     111             : // --------------------------------------------------------
     112           0 : void SAL_CALL OInstanceLocker::removeEventListener( const uno::Reference< lang::XEventListener >& xListener )
     113             :     throw (uno::RuntimeException)
     114             : {
     115           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     116           0 :     if ( m_pListenersContainer )
     117           0 :         m_pListenersContainer->removeInterface( xListener );
     118           0 : }
     119             : 
     120             : // XInitialization
     121             : // --------------------------------------------------------
     122           0 : void SAL_CALL OInstanceLocker::initialize( const uno::Sequence< uno::Any >& aArguments )
     123             :     throw (uno::Exception, uno::RuntimeException)
     124             : {
     125           0 :     ::osl::MutexGuard aGuard( m_aMutex );
     126           0 :     if ( m_bInitialized )
     127           0 :         throw frame::DoubleInitializationException();
     128             : 
     129           0 :     if ( m_bDisposed )
     130           0 :         throw lang::DisposedException(); // TODO
     131             : 
     132           0 :     if ( !m_refCount )
     133           0 :         throw uno::RuntimeException(); // the object must be refcounted already!
     134             : 
     135           0 :     uno::Reference< uno::XInterface > xInstance;
     136           0 :     uno::Reference< embed::XActionsApproval > xApproval;
     137           0 :     sal_Int32 nModes = 0;
     138             : 
     139             :     try
     140             :     {
     141           0 :         sal_Int32 nLen = aArguments.getLength();
     142           0 :         if ( nLen < 2 || nLen > 3 )
     143             :             throw lang::IllegalArgumentException(
     144             :                             OUString( "Wrong count of parameters!" ),
     145             :                             uno::Reference< uno::XInterface >(),
     146           0 :                             0 );
     147             : 
     148           0 :         if ( !( aArguments[0] >>= xInstance ) || !xInstance.is() )
     149             :             throw lang::IllegalArgumentException(
     150             :                     OUString( "Nonempty reference is expected as the first argument!" ),
     151             :                     uno::Reference< uno::XInterface >(),
     152           0 :                     0 );
     153             : 
     154           0 :         if (
     155           0 :             !( aArguments[1] >>= nModes ) ||
     156             :             (
     157           0 :               !( nModes & embed::Actions::PREVENT_CLOSE ) &&
     158           0 :               !( nModes & embed::Actions::PREVENT_TERMINATION )
     159             :             )
     160             :            )
     161             :         {
     162             :             throw lang::IllegalArgumentException(
     163             :                     OUString("The correct modes set is expected as the second argument!"),
     164             :                     uno::Reference< uno::XInterface >(),
     165           0 :                     0 );
     166             :         }
     167             : 
     168           0 :         if ( nLen == 3 && !( aArguments[2] >>= xApproval ) )
     169             :             throw lang::IllegalArgumentException(
     170             :                     OUString( "If the third argument is provided, it must be XActionsApproval implementation!" ),
     171             :                     uno::Reference< uno::XInterface >(),
     172           0 :                     0 );
     173             : 
     174             :         m_pLockListener = new OLockListener( uno::Reference< lang::XComponent > ( static_cast< lang::XComponent* >( this ) ),
     175             :                                             xInstance,
     176             :                                             nModes,
     177           0 :                                             xApproval );
     178           0 :         m_xLockListener = uno::Reference< uno::XInterface >( static_cast< OWeakObject* >( m_pLockListener ) );
     179           0 :         m_pLockListener->Init();
     180             :     }
     181           0 :     catch( uno::Exception& )
     182             :     {
     183           0 :         dispose();
     184           0 :         throw;
     185             :     }
     186             : 
     187           0 :     m_bInitialized = sal_True;
     188           0 : }
     189             : 
     190             : 
     191             : // XServiceInfo
     192             : // --------------------------------------------------------
     193           0 : OUString SAL_CALL OInstanceLocker::getImplementationName(  )
     194             :     throw (uno::RuntimeException)
     195             : {
     196           0 :     return getImplementationName_static();
     197             : }
     198             : 
     199             : // --------------------------------------------------------
     200           0 : ::sal_Bool SAL_CALL OInstanceLocker::supportsService( const OUString& ServiceName )
     201             :     throw (uno::RuntimeException)
     202             : {
     203           0 :     uno::Sequence< OUString > aSeq = getSupportedServiceNames();
     204             : 
     205           0 :     for ( sal_Int32 nInd = 0; nInd < aSeq.getLength(); nInd++ )
     206           0 :         if ( ServiceName == aSeq[nInd] )
     207           0 :             return sal_True;
     208             : 
     209           0 :     return sal_False;
     210             : }
     211             : 
     212             : // --------------------------------------------------------
     213           0 : uno::Sequence< OUString > SAL_CALL OInstanceLocker::getSupportedServiceNames()
     214             :     throw (uno::RuntimeException)
     215             : {
     216           0 :     return getSupportedServiceNames_static();
     217             : }
     218             : 
     219             : // Static methods
     220             : // --------------------------------------------------------
     221         119 : uno::Sequence< OUString > SAL_CALL OInstanceLocker::getSupportedServiceNames_static()
     222             : {
     223         119 :     const OUString aServiceName( "com.sun.star.embed.InstanceLocker" );
     224         119 :     return uno::Sequence< OUString >( &aServiceName, 1 );
     225             : }
     226             : 
     227             : // --------------------------------------------------------
     228         119 : OUString SAL_CALL OInstanceLocker::getImplementationName_static()
     229             : {
     230         119 :     return OUString( "com.sun.star.comp.embed.InstanceLocker" );
     231             : }
     232             : 
     233             : // --------------------------------------------------------
     234           0 : uno::Reference< uno::XInterface > SAL_CALL OInstanceLocker::Create(
     235             :                                 const uno::Reference< uno::XComponentContext >& rxContext )
     236             : {
     237           0 :     return static_cast< cppu::OWeakObject * >( new OInstanceLocker( rxContext ) );
     238             : }
     239             : 
     240             : 
     241             : 
     242             : // ====================================================================
     243             : // OLockListener
     244             : // ====================================================================
     245             : 
     246             : // --------------------------------------------------------
     247           0 : OLockListener::OLockListener( const uno::WeakReference< lang::XComponent >& xWrapper,
     248             :                     const uno::Reference< uno::XInterface >& xInstance,
     249             :                     sal_Int32 nMode,
     250             :                     const uno::Reference< embed::XActionsApproval > xApproval )
     251             : : m_xInstance( xInstance )
     252             : , m_xApproval( xApproval )
     253             : , m_xWrapper( xWrapper )
     254             : , m_bDisposed( sal_False )
     255             : , m_bInitialized( sal_False )
     256           0 : , m_nMode( nMode )
     257             : {
     258           0 : }
     259             : 
     260             : // --------------------------------------------------------
     261           0 : OLockListener::~OLockListener()
     262             : {
     263           0 : }
     264             : 
     265             : // --------------------------------------------------------
     266           0 : void OLockListener::Dispose()
     267             : {
     268           0 :     ::osl::ResettableMutexGuard aGuard( m_aMutex );
     269             : 
     270           0 :     if ( m_bDisposed )
     271           0 :         return;
     272             : 
     273           0 :     if ( m_nMode & embed::Actions::PREVENT_CLOSE )
     274             :     {
     275             :         try
     276             :         {
     277           0 :             uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( m_xInstance, uno::UNO_QUERY );
     278           0 :             if ( xCloseBroadcaster.is() )
     279           0 :                 xCloseBroadcaster->removeCloseListener( static_cast< util::XCloseListener* >( this ) );
     280             : 
     281           0 :             uno::Reference< util::XCloseable > xCloseable( m_xInstance, uno::UNO_QUERY );
     282           0 :             if ( xCloseable.is() )
     283           0 :                 xCloseable->close( sal_True );
     284             :         }
     285           0 :         catch( uno::Exception& )
     286             :         {}
     287             :     }
     288             : 
     289           0 :     if ( m_nMode & embed::Actions::PREVENT_TERMINATION )
     290             :     {
     291             :         try
     292             :         {
     293           0 :             uno::Reference< frame::XDesktop > xDesktop( m_xInstance, uno::UNO_QUERY_THROW );
     294           0 :             xDesktop->removeTerminateListener( static_cast< frame::XTerminateListener* >( this ) );
     295             :         }
     296           0 :         catch( uno::Exception& )
     297             :         {}
     298             :     }
     299             : 
     300           0 :     m_xInstance = uno::Reference< uno::XInterface >();
     301           0 :     m_bDisposed = sal_True;
     302             : }
     303             : 
     304             : // XEventListener
     305             : // --------------------------------------------------------
     306           0 : void SAL_CALL OLockListener::disposing( const lang::EventObject& aEvent )
     307             :     throw (uno::RuntimeException)
     308             : {
     309           0 :     ::osl::ResettableMutexGuard aGuard( m_aMutex );
     310             : 
     311             :     // object is disposed
     312           0 :     if ( aEvent.Source == m_xInstance )
     313             :     {
     314             :         // the object does not listen for anything any more
     315           0 :         m_nMode = 0;
     316             : 
     317             :         // dispose the wrapper;
     318           0 :         uno::Reference< lang::XComponent > xComponent( m_xWrapper.get(), uno::UNO_QUERY );
     319           0 :         aGuard.clear();
     320           0 :         if ( xComponent.is() )
     321             :         {
     322           0 :             try { xComponent->dispose(); }
     323           0 :             catch( uno::Exception& ){}
     324           0 :         }
     325           0 :     }
     326           0 : }
     327             : 
     328             : 
     329             : // XCloseListener
     330             : // --------------------------------------------------------
     331           0 : void SAL_CALL OLockListener::queryClosing( const lang::EventObject& aEvent, sal_Bool )
     332             :     throw (util::CloseVetoException, uno::RuntimeException)
     333             : {
     334             :     // GetsOwnership parameter is always ignored, the user of the service must close the object always
     335           0 :     ::osl::ResettableMutexGuard aGuard( m_aMutex );
     336           0 :     if ( !m_bDisposed && aEvent.Source == m_xInstance && ( m_nMode & embed::Actions::PREVENT_CLOSE ) )
     337             :     {
     338             :         try
     339             :         {
     340           0 :             uno::Reference< embed::XActionsApproval > xApprove = m_xApproval;
     341             : 
     342             :             // unlock the mutex here
     343           0 :             aGuard.clear();
     344             : 
     345           0 :             if ( xApprove.is() && xApprove->approveAction( embed::Actions::PREVENT_CLOSE ) )
     346           0 :                 throw util::CloseVetoException();
     347             :         }
     348           0 :         catch( util::CloseVetoException& )
     349             :         {
     350             :             // rethrow this exception
     351           0 :             throw;
     352             :         }
     353           0 :         catch( uno::Exception& )
     354             :         {
     355             :             // no action should be done
     356             :         }
     357           0 :     }
     358           0 : }
     359             : 
     360             : // --------------------------------------------------------
     361           0 : void SAL_CALL OLockListener::notifyClosing( const lang::EventObject& aEvent )
     362             :     throw (uno::RuntimeException)
     363             : {
     364           0 :     ::osl::ResettableMutexGuard aGuard( m_aMutex );
     365             : 
     366             :     // object is closed, no reason to listen
     367           0 :     if ( aEvent.Source == m_xInstance )
     368             :     {
     369           0 :         uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( aEvent.Source, uno::UNO_QUERY );
     370           0 :         if ( xCloseBroadcaster.is() )
     371             :         {
     372           0 :             xCloseBroadcaster->removeCloseListener( static_cast< util::XCloseListener* >( this ) );
     373           0 :             m_nMode &= ~embed::Actions::PREVENT_CLOSE;
     374           0 :             if ( !m_nMode )
     375             :             {
     376             :                 // dispose the wrapper;
     377           0 :                 uno::Reference< lang::XComponent > xComponent( m_xWrapper.get(), uno::UNO_QUERY );
     378           0 :                 aGuard.clear();
     379           0 :                 if ( xComponent.is() )
     380             :                 {
     381           0 :                     try { xComponent->dispose(); }
     382           0 :                     catch( uno::Exception& ){}
     383           0 :                 }
     384             :             }
     385           0 :         }
     386           0 :     }
     387           0 : }
     388             : 
     389             : 
     390             : // XTerminateListener
     391             : // --------------------------------------------------------
     392           0 : void SAL_CALL OLockListener::queryTermination( const lang::EventObject& aEvent )
     393             :     throw (frame::TerminationVetoException, uno::RuntimeException)
     394             : {
     395           0 :     ::osl::ResettableMutexGuard aGuard( m_aMutex );
     396           0 :     if ( aEvent.Source == m_xInstance && ( m_nMode & embed::Actions::PREVENT_TERMINATION ) )
     397             :     {
     398             :         try
     399             :         {
     400           0 :             uno::Reference< embed::XActionsApproval > xApprove = m_xApproval;
     401             : 
     402             :             // unlock the mutex here
     403           0 :             aGuard.clear();
     404             : 
     405           0 :             if ( xApprove.is() && xApprove->approveAction( embed::Actions::PREVENT_TERMINATION ) )
     406           0 :                 throw frame::TerminationVetoException();
     407             :         }
     408           0 :         catch( frame::TerminationVetoException& )
     409             :         {
     410             :             // rethrow this exception
     411           0 :             throw;
     412             :         }
     413           0 :         catch( uno::Exception& )
     414             :         {
     415             :             // no action should be done
     416             :         }
     417           0 :     }
     418           0 : }
     419             : 
     420             : // --------------------------------------------------------
     421           0 : void SAL_CALL OLockListener::notifyTermination( const lang::EventObject& aEvent )
     422             :     throw (uno::RuntimeException)
     423             : {
     424           0 :     ::osl::ResettableMutexGuard aGuard( m_aMutex );
     425             : 
     426             :     // object is terminated, no reason to listen
     427           0 :     if ( aEvent.Source == m_xInstance )
     428             :     {
     429           0 :         uno::Reference< frame::XDesktop > xDesktop( aEvent.Source, uno::UNO_QUERY );
     430           0 :         if ( xDesktop.is() )
     431             :         {
     432             :             try
     433             :             {
     434           0 :                 xDesktop->removeTerminateListener( static_cast< frame::XTerminateListener* >( this ) );
     435           0 :                 m_nMode &= ~embed::Actions::PREVENT_TERMINATION;
     436           0 :                 if ( !m_nMode )
     437             :                 {
     438             :                     // dispose the wrapper;
     439           0 :                     uno::Reference< lang::XComponent > xComponent( m_xWrapper.get(), uno::UNO_QUERY );
     440           0 :                     aGuard.clear();
     441           0 :                     if ( xComponent.is() )
     442             :                     {
     443           0 :                         try { xComponent->dispose(); }
     444           0 :                         catch( uno::Exception& ){}
     445           0 :                     }
     446             :                 }
     447             :             }
     448           0 :             catch( uno::Exception& )
     449             :             {}
     450           0 :         }
     451           0 :     }
     452           0 : }
     453             : 
     454             : 
     455             : // XInitialization
     456             : // --------------------------------------------------------
     457           0 : sal_Bool OLockListener::Init()
     458             : {
     459           0 :     ::osl::ResettableMutexGuard aGuard( m_aMutex );
     460             : 
     461           0 :     if ( m_bDisposed || m_bInitialized )
     462           0 :         return sal_False;
     463             : 
     464             :     try
     465             :     {
     466           0 :         if ( m_nMode & embed::Actions::PREVENT_CLOSE )
     467             :         {
     468           0 :             uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( m_xInstance, uno::UNO_QUERY_THROW );
     469           0 :             xCloseBroadcaster->addCloseListener( static_cast< util::XCloseListener* >( this ) );
     470             :         }
     471             : 
     472           0 :         if ( m_nMode & embed::Actions::PREVENT_TERMINATION )
     473             :         {
     474           0 :             uno::Reference< frame::XDesktop > xDesktop( m_xInstance, uno::UNO_QUERY_THROW );
     475           0 :             xDesktop->addTerminateListener( static_cast< frame::XTerminateListener* >( this ) );
     476             :         }
     477             :     }
     478           0 :     catch( uno::Exception& )
     479             :     {
     480             :         // dispose the wrapper;
     481           0 :         uno::Reference< lang::XComponent > xComponent( m_xWrapper.get(), uno::UNO_QUERY );
     482           0 :         aGuard.clear();
     483           0 :         if ( xComponent.is() )
     484             :         {
     485           0 :             try { xComponent->dispose(); }
     486           0 :             catch( uno::Exception& ){}
     487             :         }
     488             : 
     489           0 :         throw;
     490             :     }
     491             : 
     492           0 :     m_bInitialized = sal_True;
     493             : 
     494           0 :     return sal_True;
     495             : }
     496             : 
     497         119 : void createRegistryInfo_OInstanceLocker()
     498             : {
     499         119 :     static ::comphelper::module::OAutoRegistration< OInstanceLocker > aAutoRegistration;
     500         119 : }
     501             : 
     502             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10