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