LCOV - code coverage report
Current view: top level - framework/source/dispatch - interceptionhelper.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 79 104 76.0 %
Date: 2014-11-03 Functions: 10 10 100.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 <dispatch/interceptionhelper.hxx>
      21             : 
      22             : #include <com/sun/star/frame/XInterceptorInfo.hpp>
      23             : 
      24             : #include <vcl/svapp.hxx>
      25             : 
      26             : namespace framework{
      27             : 
      28             : bool InterceptionHelper::m_bPreferrFirstInterceptor = true;
      29             : 
      30        5883 : InterceptionHelper::InterceptionHelper(const css::uno::Reference< css::frame::XFrame >&            xOwner,
      31             :                                        const css::uno::Reference< css::frame::XDispatchProvider >& xSlave)
      32             :     : m_xOwnerWeak  (xOwner                       )
      33        5883 :     , m_xSlave      (xSlave                       )
      34             : {
      35        5883 : }
      36             : 
      37       11746 : InterceptionHelper::~InterceptionHelper()
      38             : {
      39       11746 : }
      40             : 
      41      290158 : css::uno::Reference< css::frame::XDispatch > SAL_CALL InterceptionHelper::queryDispatch(const css::util::URL&  aURL            ,
      42             :                                                                                         const OUString& sTargetFrameName,
      43             :                                                                                               sal_Int32        nSearchFlags    )
      44             :     throw(css::uno::RuntimeException, std::exception)
      45             : {
      46             :     // SAFE {
      47      290158 :     SolarMutexClearableGuard aReadLock;
      48             : 
      49             :     // a) first search an interceptor, which match to this URL by its URL pattern registration
      50             :     //    Note: if it return NULL - it does not mean an empty interceptor list automatically!
      51      580316 :     css::uno::Reference< css::frame::XDispatchProvider > xInterceptor;
      52      290158 :     InterceptorList::const_iterator pIt = m_lInterceptionRegs.findByPattern(aURL.Complete);
      53      290158 :     if (pIt != m_lInterceptionRegs.end())
      54      275964 :         xInterceptor = pIt->xInterceptor;
      55             : 
      56             :     // b) No match by registration - but a valid interceptor list.
      57             :     //    Use first interceptor everytimes.
      58             :     //    Note: it doesn't matter, which direction this helper implementation use to ask interceptor objects.
      59             :     //    Using of member m_aInterceptorList will starts at the beginning everytimes.
      60             :     //    It depends from the filling operation, in which direction it works really!
      61      290158 :     if (!xInterceptor.is() && m_lInterceptionRegs.size()>0)
      62             :     {
      63           0 :         pIt          = m_lInterceptionRegs.begin();
      64           0 :         xInterceptor = pIt->xInterceptor;
      65             :     }
      66             : 
      67             :     // c) No registered interceptor => use our direct slave.
      68             :     //    This helper exist by design and must be valid everytimes ...
      69             :     //    But to be more feature proof - we should check that .-)
      70      290158 :     if (!xInterceptor.is() && m_xSlave.is())
      71       14194 :         xInterceptor = m_xSlave;
      72             : 
      73      290158 :     aReadLock.clear();
      74             :     // } SAFE
      75             : 
      76      290158 :     css::uno::Reference< css::frame::XDispatch > xReturn;
      77      290158 :     if (xInterceptor.is())
      78      290158 :         xReturn = xInterceptor->queryDispatch(aURL, sTargetFrameName, nSearchFlags);
      79      580316 :     return xReturn;
      80             : }
      81             : 
      82           2 : css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL InterceptionHelper::queryDispatches( const css::uno::Sequence< css::frame::DispatchDescriptor >& lDescriptor )
      83             :     throw(css::uno::RuntimeException, std::exception)
      84             : {
      85           2 :           sal_Int32                                                          c           = lDescriptor.getLength();
      86           2 :           css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatches (c);
      87           2 :           css::uno::Reference< css::frame::XDispatch >*                      pDispatches = lDispatches.getArray();
      88           2 :     const css::frame::DispatchDescriptor*                                    pDescriptor = lDescriptor.getConstArray();
      89             : 
      90           6 :     for (sal_Int32 i=0; i<c; ++i)
      91           4 :         pDispatches[i] = queryDispatch(pDescriptor[i].FeatureURL, pDescriptor[i].FrameName, pDescriptor[i].SearchFlags);
      92             : 
      93           2 :     return lDispatches;
      94             : }
      95             : 
      96        5256 : void SAL_CALL InterceptionHelper::registerDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor >& xInterceptor)
      97             :     throw(css::uno::RuntimeException, std::exception)
      98             : {
      99             :     // reject wrong calling of this interface method
     100        5256 :     css::uno::Reference< css::frame::XDispatchProvider > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
     101        5256 :     if (!xInterceptor.is())
     102           0 :         throw css::uno::RuntimeException("NULL references not allowed as in parameter", xThis);
     103             : 
     104             :     // Fill a new info structure for new interceptor.
     105             :     // Save his reference and try to get an additional URL/pattern list from him.
     106             :     // If no list exist register these interceptor for all dispatch events with "*"!
     107       10512 :     InterceptorInfo aInfo;
     108             : 
     109        5256 :     aInfo.xInterceptor = css::uno::Reference< css::frame::XDispatchProvider >(xInterceptor, css::uno::UNO_QUERY);
     110       10512 :     css::uno::Reference< css::frame::XInterceptorInfo > xInfo(xInterceptor, css::uno::UNO_QUERY);
     111        5256 :     if (xInfo.is())
     112           0 :         aInfo.lURLPattern = xInfo->getInterceptedURLs();
     113             :     else
     114             :     {
     115        5256 :         aInfo.lURLPattern.realloc(1);
     116        5256 :         aInfo.lURLPattern[0] = "*";
     117             :     }
     118             : 
     119             :     // SAFE {
     120       10512 :     SolarMutexClearableGuard aWriteLock;
     121             : 
     122             :     // a) no interceptor at all - set this instance as master for given interceptor
     123             :     //    and set our slave as it's slave - and put this interceptor to the list.
     124             :     //    It's place there doesn matter. Because this list is currently empty.
     125        5256 :     if (m_lInterceptionRegs.empty())
     126             :     {
     127        5256 :         xInterceptor->setMasterDispatchProvider(xThis   );
     128        5256 :         xInterceptor->setSlaveDispatchProvider (m_xSlave);
     129        5256 :         m_lInterceptionRegs.push_back(aInfo);
     130             :     }
     131             : 
     132             :     // b) OK - there is at least one interceptor already registered.
     133             :     //    It's slave and it's master must be valid references ...
     134             :     //    because we created it. But we have to look for the static bool which
     135             :     //    regulate direction of using of interceptor objects!
     136             : 
     137             :     // b1) If "m_bPreferrFirstInterceptor" is set to true, we have to
     138             :     //     insert it behind any other existing interceptor - means at the end of our list.
     139           0 :     else if (m_bPreferrFirstInterceptor)
     140             :     {
     141           0 :         css::uno::Reference< css::frame::XDispatchProvider >            xMasterD = m_lInterceptionRegs.rbegin()->xInterceptor;
     142           0 :         css::uno::Reference< css::frame::XDispatchProviderInterceptor > xMasterI (xMasterD, css::uno::UNO_QUERY);
     143             : 
     144           0 :         xInterceptor->setMasterDispatchProvider(xMasterD          );
     145           0 :         xInterceptor->setSlaveDispatchProvider (m_xSlave          );
     146           0 :         xMasterI->setSlaveDispatchProvider     (aInfo.xInterceptor);
     147             : 
     148           0 :         m_lInterceptionRegs.push_back(aInfo);
     149             :     }
     150             : 
     151             :     // b2) If "m_bPreferrFirstInterceptor" is set to false, we have to
     152             :     //     insert it before any other existing interceptor - means at the beginning of our list.
     153             :     else
     154             :     {
     155           0 :         css::uno::Reference< css::frame::XDispatchProvider >            xSlaveD = m_lInterceptionRegs.begin()->xInterceptor;
     156           0 :         css::uno::Reference< css::frame::XDispatchProviderInterceptor > xSlaveI (xSlaveD , css::uno::UNO_QUERY);
     157             : 
     158           0 :         xInterceptor->setMasterDispatchProvider(xThis             );
     159           0 :         xInterceptor->setSlaveDispatchProvider (xSlaveD           );
     160           0 :         xSlaveI->setMasterDispatchProvider     (aInfo.xInterceptor);
     161             : 
     162           0 :         m_lInterceptionRegs.push_front(aInfo);
     163             :     }
     164             : 
     165       10512 :     css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY);
     166             : 
     167        5256 :     aWriteLock.clear();
     168             :     // } SAFE
     169             : 
     170             :     // Don't forget to send a frame action event "context changed".
     171             :     // Any cached dispatch objects must be validated now!
     172        5256 :     if (xOwner.is())
     173       10512 :         xOwner->contextChanged();
     174        5256 : }
     175             : 
     176        5254 : void SAL_CALL InterceptionHelper::releaseDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor >& xInterceptor)
     177             :     throw(css::uno::RuntimeException, std::exception)
     178             : {
     179             :     // reject wrong calling of this interface method
     180        5254 :     css::uno::Reference< css::frame::XDispatchProvider > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
     181        5254 :     if (!xInterceptor.is())
     182           0 :         throw css::uno::RuntimeException("NULL references not allowed as in parameter", xThis);
     183             : 
     184             :     // SAFE {
     185       10508 :     SolarMutexClearableGuard aWriteLock;
     186             : 
     187             :     // search this interceptor ...
     188             :     // If it could be located inside cache -
     189             :     // use it's slave/master relations to update the interception list;
     190             :     // set empty references for it as new master and slave;
     191             :     // and relase it from out cache.
     192        5254 :     InterceptorList::iterator pIt = m_lInterceptionRegs.findByReference(xInterceptor);
     193        5254 :     if (pIt != m_lInterceptionRegs.end())
     194             :     {
     195        5254 :         css::uno::Reference< css::frame::XDispatchProvider >            xSlaveD  (xInterceptor->getSlaveDispatchProvider() , css::uno::UNO_QUERY);
     196       10508 :         css::uno::Reference< css::frame::XDispatchProvider >            xMasterD (xInterceptor->getMasterDispatchProvider(), css::uno::UNO_QUERY);
     197       10508 :         css::uno::Reference< css::frame::XDispatchProviderInterceptor > xSlaveI  (xSlaveD                                  , css::uno::UNO_QUERY);
     198       10508 :         css::uno::Reference< css::frame::XDispatchProviderInterceptor > xMasterI (xMasterD                                 , css::uno::UNO_QUERY);
     199             : 
     200        5254 :         if (xMasterI.is())
     201           0 :             xMasterI->setSlaveDispatchProvider(xSlaveD);
     202             : 
     203        5254 :         if (xSlaveI.is())
     204           0 :             xSlaveI->setMasterDispatchProvider(xMasterD);
     205             : 
     206        5254 :         xInterceptor->setSlaveDispatchProvider (css::uno::Reference< css::frame::XDispatchProvider >());
     207        5254 :         xInterceptor->setMasterDispatchProvider(css::uno::Reference< css::frame::XDispatchProvider >());
     208             : 
     209       10508 :         m_lInterceptionRegs.erase(pIt);
     210             :     }
     211             : 
     212       10508 :     css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY);
     213             : 
     214        5254 :     aWriteLock.clear();
     215             :     // } SAFE
     216             : 
     217             :     // Don't forget to send a frame action event "context changed".
     218             :     // Any cached dispatch objects must be validated now!
     219        5254 :     if (xOwner.is())
     220       10508 :         xOwner->contextChanged();
     221        5254 : }
     222             : 
     223             : #define FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
     224        5568 : void SAL_CALL InterceptionHelper::disposing(const css::lang::EventObject& aEvent)
     225             :     throw(css::uno::RuntimeException, std::exception)
     226             : {
     227             :     #ifdef FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
     228             :     // SAFE ->
     229        5568 :     SolarMutexResettableGuard aReadLock;
     230             : 
     231             :     // check calli ... we accept such disposing call's only from our onwer frame.
     232       11136 :     css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY);
     233        5568 :     if (aEvent.Source != xOwner)
     234        5568 :         return;
     235             : 
     236             :     // Because every interceptor hold at least one reference to us ... and we destruct this list
     237             :     // of interception objects ... we should hold ourself alive .-)
     238       11136 :     css::uno::Reference< css::frame::XDispatchProvider > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY_THROW);
     239             : 
     240             :     // We need a full copy of all currently registered interceptor objects.
     241             :     // Otherwise we can't iterate over this vector without the risk, that our iterator will be invalid.
     242             :     // Because this vetor will be influenced by every deregistered interceptor.
     243       11136 :     InterceptionHelper::InterceptorList aCopy = m_lInterceptionRegs;
     244             : 
     245        5568 :     aReadLock.clear();
     246             :     // <- SAFE
     247             : 
     248        5568 :     InterceptionHelper::InterceptorList::iterator pIt;
     249       16704 :     for (  pIt  = aCopy.begin();
     250       11136 :            pIt != aCopy.end();
     251             :          ++pIt                 )
     252             :     {
     253           0 :         InterceptionHelper::InterceptorInfo& rInfo = *pIt;
     254           0 :         if (rInfo.xInterceptor.is())
     255             :         {
     256           0 :             css::uno::Reference< css::frame::XDispatchProviderInterceptor > xInterceptor(rInfo.xInterceptor, css::uno::UNO_QUERY_THROW);
     257           0 :             releaseDispatchProviderInterceptor(xInterceptor);
     258           0 :             rInfo.xInterceptor.clear();
     259             :         }
     260             :     }
     261             : 
     262       11136 :     aCopy.clear();
     263             : 
     264             :     #if OSL_DEBUG_LEVEL > 0
     265             :     // SAFE ->
     266             :     aReadLock.reset();
     267             :     if (!m_lInterceptionRegs.empty() )
     268             :         OSL_FAIL("There are some pending interceptor objects, which seems to be registered during (!) the destruction of a frame.");
     269             :     aReadLock.clear();
     270             :     // <- SAFE
     271             :     #endif // ODL_DEBUG_LEVEL>0
     272             : 
     273             :     #endif // FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
     274             : }
     275             : 
     276         951 : } // namespace framework
     277             : 
     278             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10