LCOV - code coverage report
Current view: top level - filter/source/config/cache - lateinitlistener.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 21 28 75.0 %
Date: 2012-08-25 Functions: 4 5 80.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 36 78 46.2 %

           Branch data     Line data    Source code
       1                 :            : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 :            : /*************************************************************************
       3                 :            :  *
       4                 :            :  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       5                 :            :  *
       6                 :            :  * Copyright 2000, 2010 Oracle and/or its affiliates.
       7                 :            :  *
       8                 :            :  * OpenOffice.org - a multi-platform office productivity suite
       9                 :            :  *
      10                 :            :  * This file is part of OpenOffice.org.
      11                 :            :  *
      12                 :            :  * OpenOffice.org is free software: you can redistribute it and/or modify
      13                 :            :  * it under the terms of the GNU Lesser General Public License version 3
      14                 :            :  * only, as published by the Free Software Foundation.
      15                 :            :  *
      16                 :            :  * OpenOffice.org is distributed in the hope that it will be useful,
      17                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      18                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19                 :            :  * GNU Lesser General Public License version 3 for more details
      20                 :            :  * (a copy is included in the LICENSE file that accompanied this code).
      21                 :            :  *
      22                 :            :  * You should have received a copy of the GNU Lesser General Public License
      23                 :            :  * version 3 along with OpenOffice.org.  If not, see
      24                 :            :  * <http://www.openoffice.org/license.html>
      25                 :            :  * for a copy of the LGPLv3 License.
      26                 :            :  *
      27                 :            :  ************************************************************************/
      28                 :            : 
      29                 :            : #include "sal/config.h"
      30                 :            : 
      31                 :            : #include "rtl/ref.hxx"
      32                 :            : #include "rtl/ustring.hxx"
      33                 :            : 
      34                 :            : #include "lateinitlistener.hxx"
      35                 :            : #include "lateinitthread.hxx"
      36                 :            : 
      37                 :            : 
      38                 :            : namespace filter{
      39                 :            :     namespace config{
      40                 :            : 
      41                 :            : namespace css = ::com::sun::star;
      42                 :            : 
      43                 :            : 
      44                 :            : 
      45                 :            : 
      46                 :        127 : LateInitListener::LateInitListener(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
      47                 :            :     : BaseLock(     )
      48         [ +  - ]:        127 :     , m_xSMGR (xSMGR)
      49                 :            : {
      50                 :            :     // important to do so ...
      51                 :            :     // Otherwise the temp. reference to ourselves
      52                 :            :     // will kill us at releasing time!
      53         [ +  - ]:        127 :     osl_incrementInterlockedCount( &m_refCount );
      54                 :            : 
      55                 :            :     m_xBroadcaster = css::uno::Reference< css::document::XEventBroadcaster >(
      56         [ +  - ]:        127 :         m_xSMGR->createInstance(::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.GlobalEventBroadcaster" ))),
      57 [ +  - ][ +  - ]:        127 :         css::uno::UNO_QUERY_THROW);
         [ +  - ][ +  - ]
      58                 :            : 
      59 [ +  - ][ +  - ]:        127 :     m_xBroadcaster->addEventListener(static_cast< css::document::XEventListener* >(this));
                 [ +  - ]
      60                 :            : 
      61         [ +  - ]:        127 :     osl_decrementInterlockedCount( &m_refCount );
      62                 :        127 : }
      63                 :            : 
      64                 :            : 
      65                 :            : 
      66         [ +  - ]:         74 : LateInitListener::~LateInitListener()
      67                 :            : {
      68         [ -  + ]:        148 : }
      69                 :            : 
      70                 :            : 
      71                 :            : 
      72                 :       9195 : void SAL_CALL LateInitListener::notifyEvent(const css::document::EventObject& aEvent)
      73                 :            :     throw(css::uno::RuntimeException)
      74                 :            : {
      75                 :            :     // wait for events which either
      76                 :            :     // a) indicate completed open of the first document in which case launch thread
      77                 :            :     // b) indicate close of application without any documents opened, in which case skip launching thread but drop references break cyclic dependencies in
      78                 :            :     // case of e.g. cancel from open/new database wizard or impress wizard
      79 [ +  + ][ +  + ]:       9195 :     if ( aEvent.EventName == "OnNew" || aEvent.EventName == "OnLoad" || aEvent.EventName == "OnCloseApp" )
         [ +  + ][ +  + ]
      80                 :            :     {
      81                 :            :         // this thread must be started one times only ...
      82                 :            :         // cancel listener connection before!
      83                 :            : 
      84                 :            :         // SAFE ->
      85         [ +  - ]:         74 :         ::osl::ResettableMutexGuard aLock(m_aLock);
      86                 :            : 
      87         [ -  + ]:         74 :         if ( !m_xBroadcaster.is() )
      88                 :            :             // the beauty of multi-threading ... OnLoad can be notified synchronously or asynchronously. In particular,
      89                 :            :             // SFX-based documents notify it synchronously, database documents do it asynchronously.
      90                 :            :             // Now if multiple documents are opened "at the same time", it is well possible that we get two events from
      91                 :            :             // different threads, where upon the first event, we already remove ourself from m_xBroadcaster, and start
      92                 :            :             // the thread, nonetheless there's also a second notification "in the queue", which will arrive short
      93                 :            :             // thereafter.
      94                 :            :             // In such a case, simply ignore this second event.
      95                 :       9195 :             return;
      96                 :            : 
      97 [ +  - ][ +  - ]:         74 :         m_xBroadcaster->removeEventListener(static_cast< css::document::XEventListener* >(this));
                 [ +  - ]
      98                 :         74 :         m_xBroadcaster.clear();
      99                 :            : 
     100         [ +  - ]:         74 :         aLock.clear();
     101                 :            :         // <- SAFE
     102                 :            : 
     103         [ +  + ]:         74 :         if ( aEvent.EventName != "OnCloseApp" )
     104                 :            :         {
     105 [ +  - ][ +  - ]:         74 :             rtl::Reference< LateInitThread >(new LateInitThread())->launch();
         [ +  - ][ +  - ]
                 [ +  - ]
     106                 :            :                 //TODO: a protocol is missing how to join with the launched
     107                 :            :                 // thread before exit(3), to ensure the thread is no longer
     108                 :            :                 // relying on any infrastructure while that infrastructure is
     109                 :            :                 // being shut down in atexit handlers
     110 [ +  - ][ +  - ]:         74 :         }
     111                 :            :     }
     112                 :            : }
     113                 :            : 
     114                 :            : 
     115                 :            : 
     116                 :          0 : void SAL_CALL LateInitListener::disposing(const css::lang::EventObject& /* aEvent */ )
     117                 :            :     throw(css::uno::RuntimeException)
     118                 :            : {
     119                 :            :     // ???
     120                 :            :     // Normaly it should never be called. Because we cancel our listener connection
     121                 :            :     // if we got the event about finished open of the first office document.
     122                 :            :     // But if this method was reached, it indicates an office, which was started
     123                 :            :     // (might as remote script container for an external API client) but not realy used.
     124                 :            : 
     125                 :            :     // SAFE ->
     126         [ #  # ]:          0 :     ::osl::ResettableMutexGuard aLock(m_aLock);
     127         [ #  # ]:          0 :     if ( !m_xBroadcaster.is() )
     128                 :          0 :         return;
     129                 :            : 
     130 [ #  # ][ #  # ]:          0 :     m_xBroadcaster->removeEventListener(static_cast< css::document::XEventListener* >(this));
                 [ #  # ]
     131                 :          0 :     m_xBroadcaster.clear();
     132 [ #  # ][ #  # ]:          0 :     aLock.clear();
                 [ #  # ]
     133                 :            :     // <- SAFE
     134                 :            : }
     135                 :            : 
     136                 :            :     } // namespace config
     137                 :            : } // namespace filter
     138                 :            : 
     139                 :            : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10