LibreOffice Module framework (master) 1
interceptionhelper.cxx
Go to the documentation of this file.
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
22
23#include <com/sun/star/frame/XInterceptorInfo.hpp>
24#include <com/sun/star/lang/DisposedException.hpp>
25#include <osl/diagnose.h>
26#include <utility>
27#include <vcl/svapp.hxx>
29
30using namespace com::sun::star;
31
32namespace framework{
33
34InterceptionHelper::InterceptionHelper(const css::uno::Reference< css::frame::XFrame >& xOwner,
36 : m_xOwnerWeak (xOwner )
37 , m_xSlave (std::move(xSlave ))
38{
39}
40
42{
43}
44
45css::uno::Reference< css::frame::XDispatch > SAL_CALL InterceptionHelper::queryDispatch(const css::util::URL& aURL ,
46 const OUString& sTargetFrameName,
47 sal_Int32 nSearchFlags )
48{
49 css::uno::Reference<css::frame::XDispatchProvider> xInterceptor;
50 // SAFE {
51 {
52 SolarMutexGuard aReadLock;
53
54 // a) first search an interceptor, which match to this URL by its URL pattern registration
55 // Note: if it return NULL - it does not mean an empty interceptor list automatically!
56 InterceptorList::const_iterator pIt = m_lInterceptionRegs.findByPattern(aURL.Complete);
57 if (pIt != m_lInterceptionRegs.end())
58 xInterceptor = pIt->xInterceptor;
59
60 // b) No match by registration - but a valid interceptor list.
61 // Find first interceptor w/o pattern, so we need to query it
62 if (!xInterceptor.is())
63 {
64 for (auto const& lInterceptionReg : m_lInterceptionRegs)
65 {
66 if (!lInterceptionReg.lURLPattern.hasElements())
67 {
68 // no pattern -> need to ask this guy!
69 xInterceptor = lInterceptionReg.xInterceptor;
70 break;
71 }
72 }
73 // if we didn't find any non-pattern interceptor, there's no-one
74 // registered for this command url (we already searched for matching
75 // patterns above)
76 }
77 // c) No registered interceptor => use our direct slave.
78 // This helper exist by design and must be valid everytimes ...
79 // But to be more feature proof - we should check that .-)
80 if (!xInterceptor.is() && m_xSlave.is())
81 xInterceptor = m_xSlave;
82 }
83 // } SAFE
84
85 css::uno::Reference< css::frame::XDispatch > xReturn;
86 if (xInterceptor.is())
87 xReturn = xInterceptor->queryDispatch(aURL, sTargetFrameName, nSearchFlags);
88 return xReturn;
89}
90
91css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL InterceptionHelper::queryDispatches( const css::uno::Sequence< css::frame::DispatchDescriptor >& lDescriptor )
92{
93 sal_Int32 c = lDescriptor.getLength();
94 css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatches (c);
95 css::uno::Reference< css::frame::XDispatch >* pDispatches = lDispatches.getArray();
96 const css::frame::DispatchDescriptor* pDescriptor = lDescriptor.getConstArray();
97
98 for (sal_Int32 i=0; i<c; ++i)
99 pDispatches[i] = queryDispatch(pDescriptor[i].FeatureURL, pDescriptor[i].FrameName, pDescriptor[i].SearchFlags);
100
101 return lDispatches;
102}
103
104void SAL_CALL InterceptionHelper::registerDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor >& xInterceptor)
105{
106 // reject incorrect calls of this interface method
107 css::uno::Reference< css::frame::XDispatchProvider > xThis(this);
108 if (!xInterceptor.is())
109 throw css::uno::RuntimeException("NULL references not allowed as in parameter", xThis);
110
111 // Fill a new info structure for new interceptor.
112 // Save his reference and try to get an additional URL/pattern list from him.
113 // If no list exist register these interceptor for all dispatch events with "*"!
114 InterceptorInfo aInfo;
115
116 aInfo.xInterceptor = xInterceptor;
117 css::uno::Reference< css::frame::XInterceptorInfo > xInfo(xInterceptor, css::uno::UNO_QUERY);
118 if (xInfo.is())
119 aInfo.lURLPattern = xInfo->getInterceptedURLs();
120 else
121 aInfo.lURLPattern = { "*" };
122
123 // SAFE {
124 SolarMutexClearableGuard aWriteLock;
125
126 // a) no interceptor at all - set this instance as master for given interceptor
127 // and set our slave as its slave - and put this interceptor to the list.
128 // Its place there doesn't matter. Because this list is currently empty.
129 if (m_lInterceptionRegs.empty())
130 {
131 xInterceptor->setMasterDispatchProvider(xThis );
132 xInterceptor->setSlaveDispatchProvider (m_xSlave);
133 m_lInterceptionRegs.push_back(aInfo);
134 }
135
136 // b) OK - there is at least one interceptor already registered.
137 // It's slave and it's master must be valid references ...
138 // because we created it.
139
140 // insert it before any other existing interceptor - means at the beginning of our list.
141 else
142 {
143 css::uno::Reference< css::frame::XDispatchProvider > xSlaveD = m_lInterceptionRegs.begin()->xInterceptor;
144 css::uno::Reference< css::frame::XDispatchProviderInterceptor > xSlaveI (xSlaveD , css::uno::UNO_QUERY);
145
146 xInterceptor->setMasterDispatchProvider(xThis );
147 xInterceptor->setSlaveDispatchProvider (xSlaveD );
148 xSlaveI->setMasterDispatchProvider (aInfo.xInterceptor);
149
150 m_lInterceptionRegs.push_front(aInfo);
151 }
152
153 css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY);
154
155 aWriteLock.clear();
156 // } SAFE
157
158 // Don't forget to send a frame action event "context changed".
159 // Any cached dispatch objects must be validated now!
160 if (xOwner.is())
161 xOwner->contextChanged();
162}
163
164void SAL_CALL InterceptionHelper::releaseDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor >& xInterceptor)
165{
166 // reject wrong calling of this interface method
167 css::uno::Reference< css::frame::XDispatchProvider > xThis(this);
168 if (!xInterceptor.is())
169 throw css::uno::RuntimeException("NULL references not allowed as in parameter", xThis);
170
171 // SAFE {
172 SolarMutexClearableGuard aWriteLock;
173
174 // search this interceptor ...
175 // If it could be located inside cache -
176 // use its slave/master relations to update the interception list;
177 // set empty references for it as new master and slave;
178 // and release it from out cache.
179 InterceptorList::iterator pIt = m_lInterceptionRegs.findByReference(xInterceptor);
180 if (pIt != m_lInterceptionRegs.end())
181 {
182 css::uno::Reference< css::frame::XDispatchProvider > xSlaveD = xInterceptor->getSlaveDispatchProvider();
183 css::uno::Reference< css::frame::XDispatchProvider > xMasterD = xInterceptor->getMasterDispatchProvider();
184 css::uno::Reference< css::frame::XDispatchProviderInterceptor > xSlaveI (xSlaveD , css::uno::UNO_QUERY);
185 css::uno::Reference< css::frame::XDispatchProviderInterceptor > xMasterI (xMasterD , css::uno::UNO_QUERY);
186
187 if (xMasterI.is())
188 xMasterI->setSlaveDispatchProvider(xSlaveD);
189
190 if (xSlaveI.is())
191 {
192 try
193 {
194 xSlaveI->setMasterDispatchProvider(xMasterD);
195 }
196 catch (const lang::DisposedException&)
197 {
198 TOOLS_WARN_EXCEPTION("fwk.dispatch",
199 "InterceptionHelper::releaseDispatchProviderInterceptor: "
200 "xSlaveI is disposed: ");
201 }
202 }
203
204 xInterceptor->setSlaveDispatchProvider (css::uno::Reference< css::frame::XDispatchProvider >());
205 xInterceptor->setMasterDispatchProvider(css::uno::Reference< css::frame::XDispatchProvider >());
206
207 m_lInterceptionRegs.erase(pIt);
208 }
209
210 css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY);
211
212 aWriteLock.clear();
213 // } SAFE
214
215 // Don't forget to send a frame action event "context changed".
216 // Any cached dispatch objects must be validated now!
217 if (xOwner.is())
218 xOwner->contextChanged();
219}
220
221#define FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
222void SAL_CALL InterceptionHelper::disposing(const css::lang::EventObject& aEvent)
223{
224 #ifdef FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
225 // SAFE ->
227
228 // check call... we accept such disposing calls only from our owner frame.
229 css::uno::Reference< css::frame::XFrame > xOwner(m_xOwnerWeak.get(), css::uno::UNO_QUERY);
230 if (aEvent.Source != xOwner)
231 return;
232
233 // Because every interceptor hold at least one reference to us ... and we destruct this list
234 // of interception objects ... we should hold ourself alive .-)
235 css::uno::Reference< css::frame::XDispatchProvider > xThis(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY_THROW);
236
237 // We need a full copy of all currently registered interceptor objects.
238 // Otherwise we can't iterate over this vector without the risk, that our iterator will be invalid.
239 // Because this vector will be influenced by every deregistered interceptor.
241
242 aReadLock.clear();
243 // <- SAFE
244
245 for (auto & elem : aCopy)
246 {
247 if (elem.xInterceptor.is())
248 {
249 css::uno::Reference< css::frame::XDispatchProviderInterceptor > xInterceptor(elem.xInterceptor, css::uno::UNO_QUERY_THROW);
251 elem.xInterceptor.clear();
252 }
253 }
254
255 aCopy.clear();
256
257 #if OSL_DEBUG_LEVEL > 0
258 // SAFE ->
259 aReadLock.reset();
260 if (!m_lInterceptionRegs.empty() )
261 OSL_FAIL("There are some pending interceptor objects, which seems to be registered during (!) the destruction of a frame.");
262 aReadLock.clear();
263 // <- SAFE
264 #endif // ODL_DEBUG_LEVEL>0
265
266 #endif // FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
267}
268
269} // namespace framework
270
271/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
AnyEventRef aEvent
implements a list of items of type InterceptorInfo, and provides some special functions on it.
iterator findByReference(const css::uno::Reference< css::frame::XDispatchProviderInterceptor > &xInterceptor)
search for an interceptor inside this list using it's reference.
iterator findByPattern(std::u16string_view sURL)
search for an interceptor inside this list using it's reference.
virtual void SAL_CALL registerDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor > &xInterceptor) override
register an interceptor.
virtual void SAL_CALL releaseDispatchProviderInterceptor(const css::uno::Reference< css::frame::XDispatchProviderInterceptor > &xInterceptor) override
release an interceptor.
virtual css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL queryDispatches(const css::uno::Sequence< css::frame::DispatchDescriptor > &lDescriptor) override
implements an optimized queryDispatch() for remote.
InterceptionHelper(const css::uno::Reference< css::frame::XFrame > &xOwner, rtl::Reference< DispatchProvider > xSlave)
creates a new interception helper instance.
css::uno::WeakReference< css::frame::XFrame > m_xOwnerWeak
reference to the frame, which uses this instance to implement its own interception.
InterceptorList m_lInterceptionRegs
contains all registered interceptor objects.
virtual css::uno::Reference< css::frame::XDispatch > SAL_CALL queryDispatch(const css::util::URL &aURL, const OUString &sTargetFrameName, sal_Int32 nSearchFlags) override
query for a dispatch, which implements the requested feature.
virtual ~InterceptionHelper() override
standard destructor.
rtl::Reference< DispatchProvider > m_xSlave
this interception helper implements the top level master of an interceptor list .....
virtual void SAL_CALL disposing(const css::lang::EventObject &aEvent) override
Is called from our owner frame, in case he will be disposed.
#define TOOLS_WARN_EXCEPTION(area, stream)
URL aURL
int i
bind an interceptor component to its URL pattern registration.
css::uno::Reference< css::frame::XDispatchProvider > xInterceptor
reference to the interceptor component.
css::uno::Sequence< OUString > lURLPattern
it's registration for URL patterns.