1*6d739b60SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*6d739b60SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*6d739b60SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*6d739b60SAndrew Rist * distributed with this work for additional information 6*6d739b60SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*6d739b60SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*6d739b60SAndrew Rist * "License"); you may not use this file except in compliance 9*6d739b60SAndrew Rist * with the License. You may obtain a copy of the License at 10*6d739b60SAndrew Rist * 11*6d739b60SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*6d739b60SAndrew Rist * 13*6d739b60SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*6d739b60SAndrew Rist * software distributed under the License is distributed on an 15*6d739b60SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*6d739b60SAndrew Rist * KIND, either express or implied. See the License for the 17*6d739b60SAndrew Rist * specific language governing permissions and limitations 18*6d739b60SAndrew Rist * under the License. 19*6d739b60SAndrew Rist * 20*6d739b60SAndrew Rist *************************************************************/ 21*6d739b60SAndrew Rist 22*6d739b60SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_framework.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir //_______________________________________________ 28cdf0e10cSrcweir // my own includes 29cdf0e10cSrcweir 30cdf0e10cSrcweir #ifndef __FRAMEWORK_DISPATCH_INTERCEPTIONHELPER_HXX_ 31cdf0e10cSrcweir #include <dispatch/interceptionhelper.hxx> 32cdf0e10cSrcweir #endif 33cdf0e10cSrcweir 34cdf0e10cSrcweir //_______________________________________________ 35cdf0e10cSrcweir // interface includes 36cdf0e10cSrcweir #include <com/sun/star/frame/XInterceptorInfo.hpp> 37cdf0e10cSrcweir 38cdf0e10cSrcweir //_______________________________________________ 39cdf0e10cSrcweir // includes of other projects 40cdf0e10cSrcweir #include <vcl/svapp.hxx> 41cdf0e10cSrcweir 42cdf0e10cSrcweir //_______________________________________________ 43cdf0e10cSrcweir // namespace 44cdf0e10cSrcweir 45cdf0e10cSrcweir namespace framework{ 46cdf0e10cSrcweir 47cdf0e10cSrcweir //_______________________________________________ 48cdf0e10cSrcweir // non exported const 49cdf0e10cSrcweir 50cdf0e10cSrcweir sal_Bool InterceptionHelper::m_bPreferrFirstInterceptor = sal_True; 51cdf0e10cSrcweir 52cdf0e10cSrcweir //_______________________________________________ 53cdf0e10cSrcweir // non exported definitions 54cdf0e10cSrcweir 55cdf0e10cSrcweir //_______________________________________________ 56cdf0e10cSrcweir // declarations 57cdf0e10cSrcweir 58cdf0e10cSrcweir /*----------------------------------------------------------------------------- 59cdf0e10cSrcweir 31.03.2003 09:02 60cdf0e10cSrcweir -----------------------------------------------------------------------------*/ 61cdf0e10cSrcweir DEFINE_XINTERFACE_3(InterceptionHelper , 62cdf0e10cSrcweir OWeakObject , 63cdf0e10cSrcweir DIRECT_INTERFACE(css::frame::XDispatchProvider ), 64cdf0e10cSrcweir DIRECT_INTERFACE(css::frame::XDispatchProviderInterception), 65cdf0e10cSrcweir DIRECT_INTERFACE(css::lang::XEventListener )) 66cdf0e10cSrcweir 67cdf0e10cSrcweir /*----------------------------------------------------------------------------- 68cdf0e10cSrcweir 31.03.2003 09:02 69cdf0e10cSrcweir -----------------------------------------------------------------------------*/ 70cdf0e10cSrcweir InterceptionHelper::InterceptionHelper(const css::uno::Reference< css::frame::XFrame >& xOwner, 71cdf0e10cSrcweir const css::uno::Reference< css::frame::XDispatchProvider >& xSlave) 72cdf0e10cSrcweir // Init baseclasses first 73cdf0e10cSrcweir : ThreadHelpBase(&Application::GetSolarMutex()) 74cdf0e10cSrcweir , OWeakObject ( ) 75cdf0e10cSrcweir // Init member 76cdf0e10cSrcweir , m_xOwnerWeak (xOwner ) 77cdf0e10cSrcweir , m_xSlave (xSlave ) 78cdf0e10cSrcweir { 79cdf0e10cSrcweir } 80cdf0e10cSrcweir 81cdf0e10cSrcweir /*----------------------------------------------------------------------------- 82cdf0e10cSrcweir 31.03.2003 09:02 83cdf0e10cSrcweir -----------------------------------------------------------------------------*/ 84cdf0e10cSrcweir InterceptionHelper::~InterceptionHelper() 85cdf0e10cSrcweir { 86cdf0e10cSrcweir } 87cdf0e10cSrcweir 88cdf0e10cSrcweir /*----------------------------------------------------------------------------- 89cdf0e10cSrcweir 31.03.2003 09:09 90cdf0e10cSrcweir -----------------------------------------------------------------------------*/ 91cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatch > SAL_CALL InterceptionHelper::queryDispatch(const css::util::URL& aURL , 92cdf0e10cSrcweir const ::rtl::OUString& sTargetFrameName, 93cdf0e10cSrcweir sal_Int32 nSearchFlags ) 94cdf0e10cSrcweir throw(css::uno::RuntimeException) 95cdf0e10cSrcweir { 96cdf0e10cSrcweir // SAFE { 97cdf0e10cSrcweir ReadGuard aReadLock(m_aLock); 98cdf0e10cSrcweir 99cdf0e10cSrcweir // a) first search an interceptor, which match to this URL by it's URL pattern registration 100cdf0e10cSrcweir // Note: if it return NULL - it does not mean an empty interceptor list automaticly! 101cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatchProvider > xInterceptor; 102cdf0e10cSrcweir InterceptorList::const_iterator pIt = m_lInterceptionRegs.findByPattern(aURL.Complete); 103cdf0e10cSrcweir if (pIt != m_lInterceptionRegs.end()) 104cdf0e10cSrcweir xInterceptor = pIt->xInterceptor; 105cdf0e10cSrcweir 106cdf0e10cSrcweir // b) No match by registration - but a valid interceptor list. 107cdf0e10cSrcweir // Use first interceptor everytimes. 108cdf0e10cSrcweir // Note: it doesn't matter, which direction this helper implementation use to ask interceptor objects. 109cdf0e10cSrcweir // Using of member m_aInterceptorList will starts at the beginning everytimes. 110cdf0e10cSrcweir // It depends from the filling operation, in which direction it works realy! 111cdf0e10cSrcweir if (!xInterceptor.is() && m_lInterceptionRegs.size()>0) 112cdf0e10cSrcweir { 113cdf0e10cSrcweir pIt = m_lInterceptionRegs.begin(); 114cdf0e10cSrcweir xInterceptor = pIt->xInterceptor; 115cdf0e10cSrcweir } 116cdf0e10cSrcweir 117cdf0e10cSrcweir // c) No registered interceptor => use our direct slave. 118cdf0e10cSrcweir // This helper exist by design and must be valid everytimes ... 119cdf0e10cSrcweir // But to be more feature proof - we should check that .-) 120cdf0e10cSrcweir if (!xInterceptor.is() && m_xSlave.is()) 121cdf0e10cSrcweir xInterceptor = m_xSlave; 122cdf0e10cSrcweir 123cdf0e10cSrcweir aReadLock.unlock(); 124cdf0e10cSrcweir // } SAFE 125cdf0e10cSrcweir 126cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatch > xReturn; 127cdf0e10cSrcweir if (xInterceptor.is()) 128cdf0e10cSrcweir xReturn = xInterceptor->queryDispatch(aURL, sTargetFrameName, nSearchFlags); 129cdf0e10cSrcweir return xReturn; 130cdf0e10cSrcweir } 131cdf0e10cSrcweir 132cdf0e10cSrcweir /*----------------------------------------------------------------------------- 133cdf0e10cSrcweir 31.03.2003 07:58 134cdf0e10cSrcweir -----------------------------------------------------------------------------*/ 135cdf0e10cSrcweir css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL InterceptionHelper::queryDispatches( const css::uno::Sequence< css::frame::DispatchDescriptor >& lDescriptor ) 136cdf0e10cSrcweir throw(css::uno::RuntimeException) 137cdf0e10cSrcweir { 138cdf0e10cSrcweir sal_Int32 c = lDescriptor.getLength(); 139cdf0e10cSrcweir css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatches (c); 140cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatch >* pDispatches = lDispatches.getArray(); 141cdf0e10cSrcweir const css::frame::DispatchDescriptor* pDescriptor = lDescriptor.getConstArray(); 142cdf0e10cSrcweir 143cdf0e10cSrcweir for (sal_Int32 i=0; i<c; ++i) 144cdf0e10cSrcweir pDispatches[i] = queryDispatch(pDescriptor[i].FeatureURL, pDescriptor[i].FrameName, pDescriptor[i].SearchFlags); 145cdf0e10cSrcweir 146cdf0e10cSrcweir return lDispatches; 147cdf0e10cSrcweir } 148cdf0e10cSrcweir 149cdf0e10cSrcweir /*----------------------------------------------------------------------------- 150cdf0e10cSrcweir 31.03.2003 10:20 151cdf0e10cSrcweir -----------------------------------------------------------------------------*/ 152cdf0e10cSrcweir void SAL_CALL InterceptionHelper::registerDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor >& xInterceptor) 153cdf0e10cSrcweir throw(css::uno::RuntimeException) 154cdf0e10cSrcweir { 155cdf0e10cSrcweir // reject wrong calling of this interface method 156cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatchProvider > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY); 157cdf0e10cSrcweir if (!xInterceptor.is()) 158cdf0e10cSrcweir throw css::uno::RuntimeException(DECLARE_ASCII("NULL references not allowed as in parameter"), xThis); 159cdf0e10cSrcweir 160cdf0e10cSrcweir // Fill a new info structure for new interceptor. 161cdf0e10cSrcweir // Save his reference and try to get an additional URL/pattern list from him. 162cdf0e10cSrcweir // If no list exist register these interceptor for all dispatch events with "*"! 163cdf0e10cSrcweir InterceptorInfo aInfo; 164cdf0e10cSrcweir 165cdf0e10cSrcweir aInfo.xInterceptor = css::uno::Reference< css::frame::XDispatchProvider >(xInterceptor, css::uno::UNO_QUERY); 166cdf0e10cSrcweir css::uno::Reference< css::frame::XInterceptorInfo > xInfo(xInterceptor, css::uno::UNO_QUERY); 167cdf0e10cSrcweir if (xInfo.is()) 168cdf0e10cSrcweir aInfo.lURLPattern = xInfo->getInterceptedURLs(); 169cdf0e10cSrcweir else 170cdf0e10cSrcweir { 171cdf0e10cSrcweir aInfo.lURLPattern.realloc(1); 172cdf0e10cSrcweir aInfo.lURLPattern[0] = ::rtl::OUString::createFromAscii("*"); 173cdf0e10cSrcweir } 174cdf0e10cSrcweir 175cdf0e10cSrcweir // SAFE { 176cdf0e10cSrcweir WriteGuard aWriteLock(m_aLock); 177cdf0e10cSrcweir 178cdf0e10cSrcweir // a) no interceptor at all - set this instance as master for given interceptor 179cdf0e10cSrcweir // and set our slave as it's slave - and put this interceptor to the list. 180cdf0e10cSrcweir // It's place there doesn matter. Because this list is currently empty. 181cdf0e10cSrcweir if (m_lInterceptionRegs.empty()) 182cdf0e10cSrcweir { 183cdf0e10cSrcweir xInterceptor->setMasterDispatchProvider(xThis ); 184cdf0e10cSrcweir xInterceptor->setSlaveDispatchProvider (m_xSlave); 185cdf0e10cSrcweir m_lInterceptionRegs.push_back(aInfo); 186cdf0e10cSrcweir } 187cdf0e10cSrcweir 188cdf0e10cSrcweir // b) OK - there is at least one interceptor already registered. 189cdf0e10cSrcweir // It's slave and it's master must be valid references ... 190cdf0e10cSrcweir // because we created it. But we have to look for the static bool which 191cdf0e10cSrcweir // regulate direction of using of interceptor objects! 192cdf0e10cSrcweir 193cdf0e10cSrcweir // b1) If "m_bPreferrFirstInterceptor" is set to true, we have to 194cdf0e10cSrcweir // insert it behind any other existing interceptor - means at the end of our list. 195cdf0e10cSrcweir else if (m_bPreferrFirstInterceptor) 196cdf0e10cSrcweir { 197cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatchProvider > xMasterD = m_lInterceptionRegs.rbegin()->xInterceptor; 198cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatchProviderInterceptor > xMasterI (xMasterD, css::uno::UNO_QUERY); 199cdf0e10cSrcweir 200cdf0e10cSrcweir xInterceptor->setMasterDispatchProvider(xMasterD ); 201cdf0e10cSrcweir xInterceptor->setSlaveDispatchProvider (m_xSlave ); 202cdf0e10cSrcweir xMasterI->setSlaveDispatchProvider (aInfo.xInterceptor); 203cdf0e10cSrcweir 204cdf0e10cSrcweir m_lInterceptionRegs.push_back(aInfo); 205cdf0e10cSrcweir } 206cdf0e10cSrcweir 207cdf0e10cSrcweir // b2) If "m_bPreferrFirstInterceptor" is set to false, we have to 208cdf0e10cSrcweir // insert it before any other existing interceptor - means at the beginning of our list. 209cdf0e10cSrcweir else 210cdf0e10cSrcweir { 211cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatchProvider > xSlaveD = m_lInterceptionRegs.begin()->xInterceptor; 212cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatchProviderInterceptor > xSlaveI (xSlaveD , css::uno::UNO_QUERY); 213cdf0e10cSrcweir 214cdf0e10cSrcweir xInterceptor->setMasterDispatchProvider(xThis ); 215cdf0e10cSrcweir xInterceptor->setSlaveDispatchProvider (xSlaveD ); 216cdf0e10cSrcweir xSlaveI->setMasterDispatchProvider (aInfo.xInterceptor); 217cdf0e10cSrcweir 218cdf0e10cSrcweir m_lInterceptionRegs.push_front(aInfo); 219cdf0e10cSrcweir } 220cdf0e10cSrcweir 221cdf0e10cSrcweir css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY); 222cdf0e10cSrcweir 223cdf0e10cSrcweir aWriteLock.unlock(); 224cdf0e10cSrcweir // } SAFE 225cdf0e10cSrcweir 226cdf0e10cSrcweir // Don't forget to send a frame action event "context changed". 227cdf0e10cSrcweir // Any cached dispatch objects must be validated now! 228cdf0e10cSrcweir if (xOwner.is()) 229cdf0e10cSrcweir xOwner->contextChanged(); 230cdf0e10cSrcweir } 231cdf0e10cSrcweir 232cdf0e10cSrcweir /*----------------------------------------------------------------------------- 233cdf0e10cSrcweir 31.03.2003 10:27 234cdf0e10cSrcweir -----------------------------------------------------------------------------*/ 235cdf0e10cSrcweir void SAL_CALL InterceptionHelper::releaseDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor >& xInterceptor) 236cdf0e10cSrcweir throw(css::uno::RuntimeException) 237cdf0e10cSrcweir { 238cdf0e10cSrcweir // reject wrong calling of this interface method 239cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatchProvider > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY); 240cdf0e10cSrcweir if (!xInterceptor.is()) 241cdf0e10cSrcweir throw css::uno::RuntimeException(DECLARE_ASCII("NULL references not allowed as in parameter"), xThis); 242cdf0e10cSrcweir 243cdf0e10cSrcweir // SAFE { 244cdf0e10cSrcweir WriteGuard aWriteLock(m_aLock); 245cdf0e10cSrcweir 246cdf0e10cSrcweir // search this interceptor ... 247cdf0e10cSrcweir // If it could be located inside cache - 248cdf0e10cSrcweir // use it's slave/master relations to update the interception list; 249cdf0e10cSrcweir // set empty references for it as new master and slave; 250cdf0e10cSrcweir // and relase it from out cache. 251cdf0e10cSrcweir InterceptorList::iterator pIt = m_lInterceptionRegs.findByReference(xInterceptor); 252cdf0e10cSrcweir if (pIt != m_lInterceptionRegs.end()) 253cdf0e10cSrcweir { 254cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatchProvider > xSlaveD (xInterceptor->getSlaveDispatchProvider() , css::uno::UNO_QUERY); 255cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatchProvider > xMasterD (xInterceptor->getMasterDispatchProvider(), css::uno::UNO_QUERY); 256cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatchProviderInterceptor > xSlaveI (xSlaveD , css::uno::UNO_QUERY); 257cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatchProviderInterceptor > xMasterI (xMasterD , css::uno::UNO_QUERY); 258cdf0e10cSrcweir 259cdf0e10cSrcweir if (xMasterI.is()) 260cdf0e10cSrcweir xMasterI->setSlaveDispatchProvider(xSlaveD); 261cdf0e10cSrcweir 262cdf0e10cSrcweir if (xSlaveI.is()) 263cdf0e10cSrcweir xSlaveI->setMasterDispatchProvider(xMasterD); 264cdf0e10cSrcweir 265cdf0e10cSrcweir xInterceptor->setSlaveDispatchProvider (css::uno::Reference< css::frame::XDispatchProvider >()); 266cdf0e10cSrcweir xInterceptor->setMasterDispatchProvider(css::uno::Reference< css::frame::XDispatchProvider >()); 267cdf0e10cSrcweir 268cdf0e10cSrcweir m_lInterceptionRegs.erase(pIt); 269cdf0e10cSrcweir } 270cdf0e10cSrcweir 271cdf0e10cSrcweir css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY); 272cdf0e10cSrcweir 273cdf0e10cSrcweir aWriteLock.unlock(); 274cdf0e10cSrcweir // } SAFE 275cdf0e10cSrcweir 276cdf0e10cSrcweir // Don't forget to send a frame action event "context changed". 277cdf0e10cSrcweir // Any cached dispatch objects must be validated now! 278cdf0e10cSrcweir if (xOwner.is()) 279cdf0e10cSrcweir xOwner->contextChanged(); 280cdf0e10cSrcweir } 281cdf0e10cSrcweir 282cdf0e10cSrcweir /*----------------------------------------------------------------------------- 283cdf0e10cSrcweir 31.03.2003 10:31 284cdf0e10cSrcweir -----------------------------------------------------------------------------*/ 285cdf0e10cSrcweir #define FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN 286cdf0e10cSrcweir void SAL_CALL InterceptionHelper::disposing(const css::lang::EventObject& aEvent) 287cdf0e10cSrcweir throw(css::uno::RuntimeException) 288cdf0e10cSrcweir { 289cdf0e10cSrcweir #ifdef FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN 290cdf0e10cSrcweir // SAFE -> 291cdf0e10cSrcweir ReadGuard aReadLock(m_aLock); 292cdf0e10cSrcweir 293cdf0e10cSrcweir // check calli ... we accept such disposing call's only from our onwer frame. 294cdf0e10cSrcweir css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY); 295cdf0e10cSrcweir if (aEvent.Source != xOwner) 296cdf0e10cSrcweir return; 297cdf0e10cSrcweir 298cdf0e10cSrcweir // Because every interceptor hold at least one reference to us ... and we destruct this list 299cdf0e10cSrcweir // of interception objects ... we should hold ourself alive .-) 300cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatchProvider > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY_THROW); 301cdf0e10cSrcweir 302cdf0e10cSrcweir // We need a full copy of all currently registered interceptor objects. 303cdf0e10cSrcweir // Otherwhise we cant iterate over this vector without the risk, that our iterator will be invalid. 304cdf0e10cSrcweir // Because this vetor will be influenced by every deregistered interceptor. 305cdf0e10cSrcweir InterceptionHelper::InterceptorList aCopy = m_lInterceptionRegs; 306cdf0e10cSrcweir 307cdf0e10cSrcweir aReadLock.unlock(); 308cdf0e10cSrcweir // <- SAFE 309cdf0e10cSrcweir 310cdf0e10cSrcweir InterceptionHelper::InterceptorList::iterator pIt; 311cdf0e10cSrcweir for ( pIt = aCopy.begin(); 312cdf0e10cSrcweir pIt != aCopy.end() ; 313cdf0e10cSrcweir ++pIt ) 314cdf0e10cSrcweir { 315cdf0e10cSrcweir InterceptionHelper::InterceptorInfo& rInfo = *pIt; 316cdf0e10cSrcweir if (rInfo.xInterceptor.is()) 317cdf0e10cSrcweir { 318cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatchProviderInterceptor > xInterceptor(rInfo.xInterceptor, css::uno::UNO_QUERY_THROW); 319cdf0e10cSrcweir releaseDispatchProviderInterceptor(xInterceptor); 320cdf0e10cSrcweir rInfo.xInterceptor.clear(); 321cdf0e10cSrcweir } 322cdf0e10cSrcweir } 323cdf0e10cSrcweir 324cdf0e10cSrcweir aCopy.clear(); 325cdf0e10cSrcweir 326cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0 327cdf0e10cSrcweir // SAFE -> 328cdf0e10cSrcweir aReadLock.lock(); 329cdf0e10cSrcweir if (!m_lInterceptionRegs.empty() ) 330cdf0e10cSrcweir OSL_ENSURE(sal_False, "There are some pending interceptor objects, which seams to be registered during (!) the destruction of a frame."); 331cdf0e10cSrcweir aReadLock.unlock(); 332cdf0e10cSrcweir // <- SAFE 333cdf0e10cSrcweir #endif // ODL_DEBUG_LEVEL>0 334cdf0e10cSrcweir 335cdf0e10cSrcweir #endif // FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN 336cdf0e10cSrcweir } 337cdf0e10cSrcweir 338cdf0e10cSrcweir } // namespace framework 339