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