LCOV - code coverage report
Current view: top level - filter/source/config/cache - lateinitlistener.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 21 28 75.0 %
Date: 2014-04-11 Functions: 4 5 80.0 %
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             : #include "sal/config.h"
      21             : 
      22             : #include "rtl/ref.hxx"
      23             : #include "rtl/ustring.hxx"
      24             : 
      25             : #include "lateinitlistener.hxx"
      26             : #include "lateinitthread.hxx"
      27             : 
      28             : #include <com/sun/star/frame/theGlobalEventBroadcaster.hpp>
      29             : 
      30             : 
      31             : namespace filter{
      32             :     namespace config{
      33             : 
      34          75 : LateInitListener::LateInitListener(const css::uno::Reference< css::uno::XComponentContext >& rxContext)
      35          75 :     : BaseLock(     )
      36             : {
      37             :     // important to do so ...
      38             :     // Otherwise the temp. reference to ourselves
      39             :     // will kill us at releasing time!
      40          75 :     osl_atomic_increment( &m_refCount );
      41             : 
      42         150 :     m_xBroadcaster = css::uno::Reference< css::document::XEventBroadcaster >(
      43             :         css::frame::theGlobalEventBroadcaster::get(rxContext),
      44          75 :         css::uno::UNO_QUERY_THROW);
      45             : 
      46          75 :     m_xBroadcaster->addEventListener(static_cast< css::document::XEventListener* >(this));
      47             : 
      48          75 :     osl_atomic_decrement( &m_refCount );
      49          75 : }
      50             : 
      51             : 
      52             : 
      53          76 : LateInitListener::~LateInitListener()
      54             : {
      55          76 : }
      56             : 
      57             : 
      58             : 
      59       18373 : void SAL_CALL LateInitListener::notifyEvent(const css::document::EventObject& aEvent)
      60             :     throw(css::uno::RuntimeException, std::exception)
      61             : {
      62             :     // wait for events which either
      63             :     // a) indicate completed open of the first document in which case launch thread
      64             :     // b) indicate close of application without any documents opened, in which case skip launching thread but drop references break cyclic dependencies in
      65             :     // case of e.g. cancel from open/new database wizard or impress wizard
      66       18373 :     if ( aEvent.EventName == "OnNew" || aEvent.EventName == "OnLoad" || aEvent.EventName == "OnCloseApp" )
      67             :     {
      68             :         // this thread must be started one times only ...
      69             :         // cancel listener connection before!
      70             : 
      71             :         // SAFE ->
      72          38 :         ::osl::ResettableMutexGuard aLock(m_aLock);
      73             : 
      74          38 :         if ( !m_xBroadcaster.is() )
      75             :             // the beauty of multi-threading ... OnLoad can be notified synchronously or asynchronously. In particular,
      76             :             // SFX-based documents notify it synchronously, database documents do it asynchronously.
      77             :             // Now if multiple documents are opened "at the same time", it is well possible that we get two events from
      78             :             // different threads, where upon the first event, we already remove ourself from m_xBroadcaster, and start
      79             :             // the thread, nonetheless there's also a second notification "in the queue", which will arrive short
      80             :             // thereafter.
      81             :             // In such a case, simply ignore this second event.
      82       18373 :             return;
      83             : 
      84          38 :         m_xBroadcaster->removeEventListener(static_cast< css::document::XEventListener* >(this));
      85          38 :         m_xBroadcaster.clear();
      86             : 
      87          38 :         aLock.clear();
      88             :         // <- SAFE
      89             : 
      90          38 :         if ( aEvent.EventName != "OnCloseApp" )
      91             :         {
      92          33 :             rtl::Reference< LateInitThread >(new LateInitThread())->launch();
      93             :                 //TODO: a protocol is missing how to join with the launched
      94             :                 // thread before exit(3), to ensure the thread is no longer
      95             :                 // relying on any infrastructure while that infrastructure is
      96             :                 // being shut down in atexit handlers
      97          38 :         }
      98             :     }
      99             : }
     100             : 
     101             : 
     102             : 
     103           0 : void SAL_CALL LateInitListener::disposing(const css::lang::EventObject& /* aEvent */ )
     104             :     throw(css::uno::RuntimeException, std::exception)
     105             : {
     106             :     // ???
     107             :     // Normaly it should never be called. Because we cancel our listener connection
     108             :     // if we got the event about finished open of the first office document.
     109             :     // But if this method was reached, it indicates an office, which was started
     110             :     // (might as remote script container for an external API client) but not really used.
     111             : 
     112             :     // SAFE ->
     113           0 :     ::osl::ResettableMutexGuard aLock(m_aLock);
     114           0 :     if ( !m_xBroadcaster.is() )
     115           0 :         return;
     116             : 
     117           0 :     m_xBroadcaster->removeEventListener(static_cast< css::document::XEventListener* >(this));
     118           0 :     m_xBroadcaster.clear();
     119           0 :     aLock.clear();
     120             :     // <- SAFE
     121             : }
     122             : 
     123             :     } // namespace config
     124             : } // namespace filter
     125             : 
     126             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10