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     throw(css::uno::RuntimeException)
95 {
96     // SAFE {
97     ReadGuard aReadLock(m_aLock);
98 
99     // a) first search an interceptor, which match to this URL by it's URL pattern registration
100     //    Note: if it return NULL - it does not mean an empty interceptor list automaticly!
101     css::uno::Reference< css::frame::XDispatchProvider > xInterceptor;
102     InterceptorList::const_iterator pIt = m_lInterceptionRegs.findByPattern(aURL.Complete);
103     if (pIt != m_lInterceptionRegs.end())
104         xInterceptor = pIt->xInterceptor;
105 
106     // b) No match by registration - but a valid interceptor list.
107     //    Use first interceptor everytimes.
108     //    Note: it doesn't matter, which direction this helper implementation use to ask interceptor objects.
109     //    Using of member m_aInterceptorList will starts at the beginning everytimes.
110     //    It depends from the filling operation, in which direction it works realy!
111     if (!xInterceptor.is() && m_lInterceptionRegs.size()>0)
112     {
113         pIt          = m_lInterceptionRegs.begin();
114         xInterceptor = pIt->xInterceptor;
115     }
116 
117     // c) No registered interceptor => use our direct slave.
118     //    This helper exist by design and must be valid everytimes ...
119     //    But to be more feature proof - we should check that .-)
120     if (!xInterceptor.is() && m_xSlave.is())
121         xInterceptor = m_xSlave;
122 
123     aReadLock.unlock();
124     // } SAFE
125 
126     css::uno::Reference< css::frame::XDispatch > xReturn;
127     if (xInterceptor.is())
128         xReturn = xInterceptor->queryDispatch(aURL, sTargetFrameName, nSearchFlags);
129 	return xReturn;
130 }
131 
132 /*-----------------------------------------------------------------------------
133     31.03.2003 07:58
134 -----------------------------------------------------------------------------*/
queryDispatches(const css::uno::Sequence<css::frame::DispatchDescriptor> & lDescriptor)135 css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL InterceptionHelper::queryDispatches( const css::uno::Sequence< css::frame::DispatchDescriptor >& lDescriptor )
136     throw(css::uno::RuntimeException)
137 {
138           sal_Int32                                                          c           = lDescriptor.getLength();
139           css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatches (c);
140           css::uno::Reference< css::frame::XDispatch >*                      pDispatches = lDispatches.getArray();
141     const css::frame::DispatchDescriptor*                                    pDescriptor = lDescriptor.getConstArray();
142 
143     for (sal_Int32 i=0; i<c; ++i)
144         pDispatches[i] = queryDispatch(pDescriptor[i].FeatureURL, pDescriptor[i].FrameName, pDescriptor[i].SearchFlags);
145 
146     return lDispatches;
147 }
148 
149 /*-----------------------------------------------------------------------------
150     31.03.2003 10:20
151 -----------------------------------------------------------------------------*/
registerDispatchProviderInterceptor(const css::uno::Reference<css::frame::XDispatchProviderInterceptor> & xInterceptor)152 void SAL_CALL InterceptionHelper::registerDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor >& xInterceptor)
153     throw(css::uno::RuntimeException)
154 {
155     // reject wrong calling of this interface method
156     css::uno::Reference< css::frame::XDispatchProvider > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
157     if (!xInterceptor.is())
158         throw css::uno::RuntimeException(DECLARE_ASCII("NULL references not allowed as in parameter"), xThis);
159 
160 	// Fill a new info structure for new interceptor.
161 	// Save his reference and try to get an additional URL/pattern list from him.
162 	// If no list exist register these interceptor for all dispatch events with "*"!
163     InterceptorInfo aInfo;
164 
165     aInfo.xInterceptor = css::uno::Reference< css::frame::XDispatchProvider >(xInterceptor, css::uno::UNO_QUERY);
166     css::uno::Reference< css::frame::XInterceptorInfo > xInfo(xInterceptor, css::uno::UNO_QUERY);
167     if (xInfo.is())
168         aInfo.lURLPattern = xInfo->getInterceptedURLs();
169 	else
170 	{
171         aInfo.lURLPattern.realloc(1);
172         aInfo.lURLPattern[0] = ::rtl::OUString::createFromAscii("*");
173 	}
174 
175     // SAFE {
176     WriteGuard aWriteLock(m_aLock);
177 
178     // a) no interceptor at all - set this instance as master for given interceptor
179     //    and set our slave as it's slave - and put this interceptor to the list.
180     //    It's place there doesn matter. Because this list is currently empty.
181     if (m_lInterceptionRegs.empty())
182     {
183         xInterceptor->setMasterDispatchProvider(xThis   );
184         xInterceptor->setSlaveDispatchProvider (m_xSlave);
185         m_lInterceptionRegs.push_back(aInfo);
186     }
187 
188     // b) OK - there is at least one interceptor already registered.
189     //    It's slave and it's master must be valid references ...
190     //    because we created it. But we have to look for the static bool which
191     //    regulate direction of using of interceptor objects!
192 
193     // b1) If "m_bPreferrFirstInterceptor" is set to true, we have to
194     //     insert it behind any other existing interceptor - means at the end of our list.
195     else if (m_bPreferrFirstInterceptor)
196 	{
197         css::uno::Reference< css::frame::XDispatchProvider >            xMasterD = m_lInterceptionRegs.rbegin()->xInterceptor;
198         css::uno::Reference< css::frame::XDispatchProviderInterceptor > xMasterI (xMasterD, css::uno::UNO_QUERY);
199 
200         xInterceptor->setMasterDispatchProvider(xMasterD          );
201         xInterceptor->setSlaveDispatchProvider (m_xSlave          );
202         xMasterI->setSlaveDispatchProvider     (aInfo.xInterceptor);
203 
204         m_lInterceptionRegs.push_back(aInfo);
205 	}
206 
207     // b2) If "m_bPreferrFirstInterceptor" is set to false, we have to
208     //     insert it before any other existing interceptor - means at the beginning of our list.
209     else
210     {
211         css::uno::Reference< css::frame::XDispatchProvider >            xSlaveD = m_lInterceptionRegs.begin()->xInterceptor;
212         css::uno::Reference< css::frame::XDispatchProviderInterceptor > xSlaveI (xSlaveD , css::uno::UNO_QUERY);
213 
214         xInterceptor->setMasterDispatchProvider(xThis             );
215         xInterceptor->setSlaveDispatchProvider (xSlaveD           );
216         xSlaveI->setMasterDispatchProvider     (aInfo.xInterceptor);
217 
218         m_lInterceptionRegs.push_front(aInfo);
219     }
220 
221     css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY);
222 
223     aWriteLock.unlock();
224     // } SAFE
225 
226     // Don't forget to send a frame action event "context changed".
227     // Any cached dispatch objects must be validated now!
228     if (xOwner.is())
229 		xOwner->contextChanged();
230 }
231 
232 /*-----------------------------------------------------------------------------
233     31.03.2003 10:27
234 -----------------------------------------------------------------------------*/
releaseDispatchProviderInterceptor(const css::uno::Reference<css::frame::XDispatchProviderInterceptor> & xInterceptor)235 void SAL_CALL InterceptionHelper::releaseDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor >& xInterceptor)
236     throw(css::uno::RuntimeException)
237 {
238     // reject wrong calling of this interface method
239     css::uno::Reference< css::frame::XDispatchProvider > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
240     if (!xInterceptor.is())
241         throw css::uno::RuntimeException(DECLARE_ASCII("NULL references not allowed as in parameter"), xThis);
242 
243     // SAFE {
244     WriteGuard aWriteLock(m_aLock);
245 
246     // search this interceptor ...
247     // If it could be located inside cache -
248     // use it's slave/master relations to update the interception list;
249     // set empty references for it as new master and slave;
250     // and relase it from out cache.
251     InterceptorList::iterator pIt = m_lInterceptionRegs.findByReference(xInterceptor);
252     if (pIt != m_lInterceptionRegs.end())
253     {
254         css::uno::Reference< css::frame::XDispatchProvider >            xSlaveD  (xInterceptor->getSlaveDispatchProvider() , css::uno::UNO_QUERY);
255         css::uno::Reference< css::frame::XDispatchProvider >            xMasterD (xInterceptor->getMasterDispatchProvider(), css::uno::UNO_QUERY);
256         css::uno::Reference< css::frame::XDispatchProviderInterceptor > xSlaveI  (xSlaveD                                  , css::uno::UNO_QUERY);
257         css::uno::Reference< css::frame::XDispatchProviderInterceptor > xMasterI (xMasterD                                 , css::uno::UNO_QUERY);
258 
259         if (xMasterI.is())
260             xMasterI->setSlaveDispatchProvider(xSlaveD);
261 
262         if (xSlaveI.is())
263             xSlaveI->setMasterDispatchProvider(xMasterD);
264 
265         xInterceptor->setSlaveDispatchProvider (css::uno::Reference< css::frame::XDispatchProvider >());
266         xInterceptor->setMasterDispatchProvider(css::uno::Reference< css::frame::XDispatchProvider >());
267 
268         m_lInterceptionRegs.erase(pIt);
269     }
270 
271     css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY);
272 
273     aWriteLock.unlock();
274     // } SAFE
275 
276     // Don't forget to send a frame action event "context changed".
277     // Any cached dispatch objects must be validated now!
278     if (xOwner.is())
279 		xOwner->contextChanged();
280 }
281 
282 /*-----------------------------------------------------------------------------
283     31.03.2003 10:31
284 -----------------------------------------------------------------------------*/
285 #define FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
disposing(const css::lang::EventObject & aEvent)286 void SAL_CALL InterceptionHelper::disposing(const css::lang::EventObject& aEvent)
287     throw(css::uno::RuntimeException)
288 {
289     #ifdef FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
290     // SAFE ->
291     ReadGuard aReadLock(m_aLock);
292 
293     // check calli ... we accept such disposing call's only from our onwer frame.
294     css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY);
295     if (aEvent.Source != xOwner)
296         return;
297 
298     // Because every interceptor hold at least one reference to us ... and we destruct this list
299     // of interception objects ... we should hold ourself alive .-)
300     css::uno::Reference< css::frame::XDispatchProvider > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY_THROW);
301 
302     // We need a full copy of all currently registered interceptor objects.
303     // Otherwhise we cant iterate over this vector without the risk, that our iterator will be invalid.
304     // Because this vetor will be influenced by every deregistered interceptor.
305     InterceptionHelper::InterceptorList aCopy = m_lInterceptionRegs;
306 
307     aReadLock.unlock();
308     // <- SAFE
309 
310     InterceptionHelper::InterceptorList::iterator pIt;
311     for (  pIt  = aCopy.begin();
312            pIt != aCopy.end()  ;
313          ++pIt                 )
314     {
315         InterceptionHelper::InterceptorInfo& rInfo = *pIt;
316         if (rInfo.xInterceptor.is())
317         {
318             css::uno::Reference< css::frame::XDispatchProviderInterceptor > xInterceptor(rInfo.xInterceptor, css::uno::UNO_QUERY_THROW);
319             releaseDispatchProviderInterceptor(xInterceptor);
320             rInfo.xInterceptor.clear();
321         }
322     }
323 
324     aCopy.clear();
325 
326     #if OSL_DEBUG_LEVEL > 0
327     // SAFE ->
328     aReadLock.lock();
329     if (!m_lInterceptionRegs.empty() )
330         OSL_ENSURE(sal_False, "There are some pending interceptor objects, which seams to be registered during (!) the destruction of a frame.");
331     aReadLock.unlock();
332     // <- SAFE
333     #endif // ODL_DEBUG_LEVEL>0
334 
335     #endif // FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
336 }
337 
338 } // namespace framework
339