1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <dispatch/dispatchprovider.hxx>
21 #include <dispatch/interceptionhelper.hxx>
23 #include <com/sun/star/frame/XInterceptorInfo.hpp>
24 #include <com/sun/star/lang/DisposedException.hpp>
25 #include <osl/diagnose.h>
27 #include <vcl/svapp.hxx>
28 #include <comphelper/diagnose_ex.hxx>
30 using namespace com::sun::star
;
34 InterceptionHelper::InterceptionHelper(const css::uno::Reference
< css::frame::XFrame
>& xOwner
,
35 rtl::Reference
< DispatchProvider
> xSlave
)
36 : m_xOwnerWeak (xOwner
)
37 , m_xSlave (std::move(xSlave
))
41 InterceptionHelper::~InterceptionHelper()
45 css::uno::Reference
< css::frame::XDispatch
> SAL_CALL
InterceptionHelper::queryDispatch(const css::util::URL
& aURL
,
46 const OUString
& sTargetFrameName
,
47 sal_Int32 nSearchFlags
)
49 css::uno::Reference
<css::frame::XDispatchProvider
> xInterceptor
;
52 SolarMutexGuard aReadLock
;
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
;
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())
64 for (auto const& lInterceptionReg
: m_lInterceptionRegs
)
66 if (!lInterceptionReg
.lURLPattern
.hasElements())
68 // no pattern -> need to ask this guy!
69 xInterceptor
= lInterceptionReg
.xInterceptor
;
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
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
;
85 css::uno::Reference
< css::frame::XDispatch
> xReturn
;
86 if (xInterceptor
.is())
87 xReturn
= xInterceptor
->queryDispatch(aURL
, sTargetFrameName
, nSearchFlags
);
91 css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > SAL_CALL
InterceptionHelper::queryDispatches( const css::uno::Sequence
< css::frame::DispatchDescriptor
>& lDescriptor
)
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();
98 for (sal_Int32 i
=0; i
<c
; ++i
)
99 pDispatches
[i
] = queryDispatch(pDescriptor
[i
].FeatureURL
, pDescriptor
[i
].FrameName
, pDescriptor
[i
].SearchFlags
);
104 void SAL_CALL
InterceptionHelper::registerDispatchProviderInterceptor(const css::uno::Reference
< css::frame::XDispatchProviderInterceptor
>& xInterceptor
)
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
);
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
;
116 aInfo
.xInterceptor
= xInterceptor
;
117 css::uno::Reference
< css::frame::XInterceptorInfo
> xInfo(xInterceptor
, css::uno::UNO_QUERY
);
119 aInfo
.lURLPattern
= xInfo
->getInterceptedURLs();
121 aInfo
.lURLPattern
= { "*" };
124 SolarMutexClearableGuard aWriteLock
;
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())
131 xInterceptor
->setMasterDispatchProvider(xThis
);
132 xInterceptor
->setSlaveDispatchProvider (m_xSlave
);
133 m_lInterceptionRegs
.push_back(aInfo
);
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.
140 // insert it before any other existing interceptor - means at the beginning of our list.
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
);
146 xInterceptor
->setMasterDispatchProvider(xThis
);
147 xInterceptor
->setSlaveDispatchProvider (xSlaveD
);
148 xSlaveI
->setMasterDispatchProvider (aInfo
.xInterceptor
);
150 m_lInterceptionRegs
.push_front(aInfo
);
153 css::uno::Reference
< css::frame::XFrame
> xOwner(m_xOwnerWeak
.get(), css::uno::UNO_QUERY
);
158 // Don't forget to send a frame action event "context changed".
159 // Any cached dispatch objects must be validated now!
161 xOwner
->contextChanged();
164 void SAL_CALL
InterceptionHelper::releaseDispatchProviderInterceptor(const css::uno::Reference
< css::frame::XDispatchProviderInterceptor
>& xInterceptor
)
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
);
172 SolarMutexClearableGuard aWriteLock
;
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())
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
);
188 xMasterI
->setSlaveDispatchProvider(xSlaveD
);
194 xSlaveI
->setMasterDispatchProvider(xMasterD
);
196 catch (const lang::DisposedException
&)
198 TOOLS_WARN_EXCEPTION("fwk.dispatch",
199 "InterceptionHelper::releaseDispatchProviderInterceptor: "
200 "xSlaveI is disposed: ");
204 xInterceptor
->setSlaveDispatchProvider (css::uno::Reference
< css::frame::XDispatchProvider
>());
205 xInterceptor
->setMasterDispatchProvider(css::uno::Reference
< css::frame::XDispatchProvider
>());
207 m_lInterceptionRegs
.erase(pIt
);
210 css::uno::Reference
< css::frame::XFrame
> xOwner(m_xOwnerWeak
.get(), css::uno::UNO_QUERY
);
215 // Don't forget to send a frame action event "context changed".
216 // Any cached dispatch objects must be validated now!
218 xOwner
->contextChanged();
221 #define FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
222 void SAL_CALL
InterceptionHelper::disposing(const css::lang::EventObject
& aEvent
)
224 #ifdef FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
226 SolarMutexResettableGuard aReadLock
;
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
)
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
);
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.
240 InterceptionHelper::InterceptorList aCopy
= m_lInterceptionRegs
;
245 for (auto & elem
: aCopy
)
247 if (elem
.xInterceptor
.is())
249 css::uno::Reference
< css::frame::XDispatchProviderInterceptor
> xInterceptor(elem
.xInterceptor
, css::uno::UNO_QUERY_THROW
);
250 releaseDispatchProviderInterceptor(xInterceptor
);
251 elem
.xInterceptor
.clear();
257 #if OSL_DEBUG_LEVEL > 0
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.");
264 #endif // ODL_DEBUG_LEVEL>0
266 #endif // FORCE_DESTRUCTION_OF_INTERCEPTION_CHAIN
269 } // namespace framework
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */