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 3494 : InterceptionHelper::InterceptionHelper(const css::uno::Reference< css::frame::XFrame >& xOwner,
31 : const css::uno::Reference< css::frame::XDispatchProvider >& xSlave)
32 : : m_xOwnerWeak (xOwner )
33 3494 : , m_xSlave (xSlave )
34 : {
35 3494 : }
36 :
37 6966 : InterceptionHelper::~InterceptionHelper()
38 : {
39 6966 : }
40 :
41 198970 : 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 198970 : 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 397940 : css::uno::Reference< css::frame::XDispatchProvider > xInterceptor;
52 198970 : InterceptorList::const_iterator pIt = m_lInterceptionRegs.findByPattern(aURL.Complete);
53 198970 : if (pIt != m_lInterceptionRegs.end())
54 184932 : 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 198970 : 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 198970 : if (!xInterceptor.is() && m_xSlave.is())
71 14038 : xInterceptor = m_xSlave;
72 :
73 198970 : aReadLock.clear();
74 : // } SAFE
75 :
76 198970 : css::uno::Reference< css::frame::XDispatch > xReturn;
77 198970 : if (xInterceptor.is())
78 198970 : xReturn = xInterceptor->queryDispatch(aURL, sTargetFrameName, nSearchFlags);
79 397940 : return xReturn;
80 : }
81 :
82 1 : 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 1 : sal_Int32 c = lDescriptor.getLength();
86 1 : css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatches (c);
87 1 : css::uno::Reference< css::frame::XDispatch >* pDispatches = lDispatches.getArray();
88 1 : const css::frame::DispatchDescriptor* pDescriptor = lDescriptor.getConstArray();
89 :
90 3 : for (sal_Int32 i=0; i<c; ++i)
91 2 : pDispatches[i] = queryDispatch(pDescriptor[i].FeatureURL, pDescriptor[i].FrameName, pDescriptor[i].SearchFlags);
92 :
93 1 : return lDispatches;
94 : }
95 :
96 3110 : 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 3110 : css::uno::Reference< css::frame::XDispatchProvider > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
101 3110 : 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 6220 : InterceptorInfo aInfo;
108 :
109 3110 : aInfo.xInterceptor = css::uno::Reference< css::frame::XDispatchProvider >(xInterceptor, css::uno::UNO_QUERY);
110 6220 : css::uno::Reference< css::frame::XInterceptorInfo > xInfo(xInterceptor, css::uno::UNO_QUERY);
111 3110 : if (xInfo.is())
112 0 : aInfo.lURLPattern = xInfo->getInterceptedURLs();
113 : else
114 : {
115 3110 : aInfo.lURLPattern.realloc(1);
116 3110 : aInfo.lURLPattern[0] = "*";
117 : }
118 :
119 : // SAFE {
120 6220 : 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 3110 : if (m_lInterceptionRegs.empty())
126 : {
127 3110 : xInterceptor->setMasterDispatchProvider(xThis );
128 3110 : xInterceptor->setSlaveDispatchProvider (m_xSlave);
129 3110 : 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 6220 : css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY);
166 :
167 3110 : 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 3110 : if (xOwner.is())
173 6220 : xOwner->contextChanged();
174 3110 : }
175 :
176 3103 : 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 3103 : css::uno::Reference< css::frame::XDispatchProvider > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY);
181 3103 : if (!xInterceptor.is())
182 0 : throw css::uno::RuntimeException("NULL references not allowed as in parameter", xThis);
183 :
184 : // SAFE {
185 6206 : 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 3103 : InterceptorList::iterator pIt = m_lInterceptionRegs.findByReference(xInterceptor);
193 3103 : if (pIt != m_lInterceptionRegs.end())
194 : {
195 3103 : css::uno::Reference< css::frame::XDispatchProvider > xSlaveD (xInterceptor->getSlaveDispatchProvider() , css::uno::UNO_QUERY);
196 6206 : css::uno::Reference< css::frame::XDispatchProvider > xMasterD (xInterceptor->getMasterDispatchProvider(), css::uno::UNO_QUERY);
197 6206 : css::uno::Reference< css::frame::XDispatchProviderInterceptor > xSlaveI (xSlaveD , css::uno::UNO_QUERY);
198 6206 : css::uno::Reference< css::frame::XDispatchProviderInterceptor > xMasterI (xMasterD , css::uno::UNO_QUERY);
199 :
200 3103 : if (xMasterI.is())
201 0 : xMasterI->setSlaveDispatchProvider(xSlaveD);
202 :
203 3103 : if (xSlaveI.is())
204 0 : xSlaveI->setMasterDispatchProvider(xMasterD);
205 :
206 3103 : xInterceptor->setSlaveDispatchProvider (css::uno::Reference< css::frame::XDispatchProvider >());
207 3103 : xInterceptor->setMasterDispatchProvider(css::uno::Reference< css::frame::XDispatchProvider >());
208 :
209 6206 : m_lInterceptionRegs.erase(pIt);
210 : }
211 :
212 6206 : css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY);
213 :
214 3103 : 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 3103 : if (xOwner.is())
220 6206 : xOwner->contextChanged();
221 3103 : }
222 :
223 : #define FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
224 3274 : 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 3274 : SolarMutexResettableGuard aReadLock;
230 :
231 : // check calli ... we accept such disposing call's only from our onwer frame.
232 6548 : css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY);
233 3274 : if (aEvent.Source != xOwner)
234 3274 : 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 6548 : 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 6548 : InterceptionHelper::InterceptorList aCopy = m_lInterceptionRegs;
244 :
245 3274 : aReadLock.clear();
246 : // <- SAFE
247 :
248 3274 : InterceptionHelper::InterceptorList::iterator pIt;
249 9822 : for ( pIt = aCopy.begin();
250 6548 : 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 6548 : 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 : } // namespace framework
277 :
278 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|