Update git submodules
[LibreOffice.git] / framework / source / dispatch / dispatchprovider.cxx
bloba1125016ce30ce4bb38ac7844834eb18876711e7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <loadenv/loadenv.hxx>
22 #include <dispatch/loaddispatcher.hxx>
23 #include <dispatch/closedispatcher.hxx>
24 #include <dispatch/startmoduledispatcher.hxx>
26 #include <pattern/window.hxx>
27 #include <targets.h>
28 #include "isstartmoduledispatch.hxx"
30 #include <com/sun/star/frame/XDesktop.hpp>
31 #include <com/sun/star/frame/FrameSearchFlag.hpp>
32 #include <com/sun/star/uno/Exception.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/lang/XInitialization.hpp>
35 #include <com/sun/star/util/XCacheInfo.hpp>
37 #include <rtl/ustring.hxx>
38 #include <utility>
39 #include <vcl/svapp.hxx>
40 #include <sal/log.hxx>
41 #include <framework/dispatchhelper.hxx>
43 namespace framework{
45 /**
46 @short standard ctor/dtor
47 @descr These initialize a new instance of this class with needed information for work.
48 We hold a weakreference to our owner frame which start dispatches at us.
49 We can't use a normal reference because he hold a reference of us too ...
50 nobody can die so ...!
52 @seealso using at owner
54 @param rxContext
55 reference to servicemanager to create new services.
56 @param xFrame
57 reference to our owner frame.
59 DispatchProvider::DispatchProvider( css::uno::Reference< css::uno::XComponentContext > xContext ,
60 const css::uno::Reference< css::frame::XFrame >& xFrame )
61 : m_xContext (std::move( xContext ))
62 , m_xFrame ( xFrame )
66 /**
67 @short protected(!) dtor for deinitializing
68 @descr We made it protected to prevent using of us as base class instead as a member.
70 DispatchProvider::~DispatchProvider()
74 /**
75 @interface XDispatchProvider
76 @short search a dispatcher for given URL
77 @descr If no interceptor is set on owner, we search for right frame and dispatch URL to it.
78 If no frame was found, we do nothing.
79 But we don't do it directly here. We detect the type of our owner frame and calls
80 specialized queryDispatch() helper dependen from that. Because a Desktop handle some
81 requests in another way then a normal frame.
83 @param aURL
84 URL to dispatch.
85 @param sTargetFrameName
86 name of searched frame.
87 @param nSearchFlags
88 flags for searching.
89 @return A reference to a dispatch object for this URL (if someone was found!).
91 @threadsafe yes
93 css::uno::Reference< css::frame::XDispatch > SAL_CALL DispatchProvider::queryDispatch( const css::util::URL& aURL ,
94 const OUString& sTargetFrameName ,
95 sal_Int32 nSearchFlags )
97 css::uno::Reference< css::frame::XDispatch > xDispatcher;
99 css::uno::Reference< css::frame::XFrame > xOwner(m_xFrame);
101 css::uno::Reference< css::frame::XDesktop > xDesktopCheck( xOwner, css::uno::UNO_QUERY );
103 if (xDesktopCheck.is())
104 xDispatcher = implts_queryDesktopDispatch(xOwner, aURL, sTargetFrameName, nSearchFlags);
105 else
106 xDispatcher = implts_queryFrameDispatch(xOwner, aURL, sTargetFrameName, nSearchFlags);
108 return xDispatcher;
112 @interface XDispatchProvider
113 @short do the same like queryDispatch() ... but handle multiple dispatches at the same time
114 @descr It's an optimism. User give us a list of queries ... and we return a list of dispatcher.
115 If one of given queries couldn't be solved to a real existing dispatcher ...
116 we return a list with empty references in it! Order of both lists will be retained!
118 @seealso method queryDispatch()
120 @param lDescriptions
121 a list of all dispatch parameters for multiple requests
122 @return A reference a list of dispatch objects for these URLs - may with some <NULL/> values inside.
124 @threadsafe yes
126 css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL DispatchProvider::queryDispatches( const css::uno::Sequence< css::frame::DispatchDescriptor >& lDescriptions )
128 // Create return list - which must have same size then the given descriptor
129 // It's not allowed to pack it!
130 sal_Int32 nCount = lDescriptions.getLength();
131 css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatcher( nCount );
132 auto lDispatcherRange = asNonConstRange(lDispatcher);
133 // Step over all descriptors and try to get any dispatcher for it.
134 for( sal_Int32 i=0; i<nCount; ++i )
136 lDispatcherRange[i] = queryDispatch( lDescriptions[i].FeatureURL ,
137 lDescriptions[i].FrameName ,
138 lDescriptions[i].SearchFlags );
141 return lDispatcher;
145 @short helper for queryDispatch()
146 @descr Every member of the frame tree (frame, desktop) must handle such request
147 in another way. So we implement different specialized methods for everyone.
149 @threadsafe yes
151 css::uno::Reference< css::frame::XDispatch > DispatchProvider::implts_queryDesktopDispatch( const css::uno::Reference< css::frame::XFrame >& xDesktop ,
152 const css::util::URL& aURL ,
153 const OUString& sTargetFrameName ,
154 sal_Int32 nSearchFlags )
156 css::uno::Reference< css::frame::XDispatch > xDispatcher;
158 // ignore wrong requests which are not supported
159 if (
160 (sTargetFrameName==SPECIALTARGET_PARENT ) || // we have no parent by definition
161 (sTargetFrameName==SPECIALTARGET_BEAMER ) // beamer frames are allowed as child of tasks only -
162 // and they exist more than ones. We have no idea which our sub tasks is the right one
165 return nullptr;
168 // I) handle special cases which not right for using findFrame() first
170 // I.I) "_blank"
171 // It's not the right place to create a new task here - because we are queried for a dispatch object
172 // only, which can handle such request. Such dispatcher should create the required task on demand.
173 // Normally the functionality for "_blank" is provided by findFrame() - but that would create it directly
174 // here. that's why we must "intercept" here.
176 if (sTargetFrameName==SPECIALTARGET_BLANK)
178 if (implts_isLoadableContent(aURL))
179 xDispatcher = implts_getOrCreateDispatchHelper( E_BLANKDISPATCHER, xDesktop );
182 // I.II) "_default"
183 // This is a combination of search an empty task for recycling - or create a new one.
185 else if (sTargetFrameName==SPECIALTARGET_DEFAULT)
187 if (implts_isLoadableContent(aURL))
188 xDispatcher = implts_getOrCreateDispatchHelper( E_DEFAULTDISPATCHER, xDesktop );
190 if (isStartModuleDispatch(aURL))
191 xDispatcher = implts_getOrCreateDispatchHelper( E_STARTMODULEDISPATCHER, xDesktop );
194 // I.III) "_self", "", "_top"
195 // The desktop can't load any document - but he can handle some special protocols like "uno", "slot" ...
196 // Why is "top" here handled too? Because the desktop is the topest frame. Normally it's superfluous
197 // to use this target - but we can handle it in the same manner then "_self".
199 else if (
200 (sTargetFrameName==SPECIALTARGET_SELF) ||
201 (sTargetFrameName==SPECIALTARGET_TOP ) ||
202 (sTargetFrameName.isEmpty())
205 xDispatcher = implts_searchProtocolHandler(aURL);
208 // I.IV) no further special targets exist
209 // Now we have to search for the right target frame by calling findFrame() - but should provide our code
210 // against creation of a new task if no frame could be found.
211 // I said it before - it's allowed for dispatch() only.
213 else
215 sal_Int32 nRightFlags = nSearchFlags & ~css::frame::FrameSearchFlag::CREATE;
217 // try to find any existing target and ask him for his dispatcher
218 css::uno::Reference< css::frame::XFrame > xFoundFrame = xDesktop->findFrame(sTargetFrameName, nRightFlags);
219 if (xFoundFrame.is())
221 css::uno::Reference< css::frame::XDispatchProvider > xProvider( xFoundFrame, css::uno::UNO_QUERY );
222 xDispatcher = xProvider->queryDispatch(aURL,SPECIALTARGET_SELF,0);
224 // if it couldn't be found - but creation was allowed
225 // use special dispatcher for creation or forwarding to the browser
226 else if (nSearchFlags & css::frame::FrameSearchFlag::CREATE)
227 xDispatcher = implts_getOrCreateDispatchHelper( E_CREATEDISPATCHER, xDesktop, sTargetFrameName, nSearchFlags );
230 return xDispatcher;
233 css::uno::Reference< css::frame::XDispatch > DispatchProvider::implts_queryFrameDispatch( const css::uno::Reference< css::frame::XFrame >& xFrame ,
234 const css::util::URL& aURL ,
235 const OUString& sTargetFrameName ,
236 sal_Int32 nSearchFlags )
238 css::uno::Reference< css::frame::XDispatch > xDispatcher;
240 // 0) Some URLs are dispatched in a generic way (e.g. by the menu) using the default target "".
241 // But they are specified to use her own fix target. Detect such URLs here and use the correct target.
243 // I) handle special cases which not right for using findFrame() first
245 // I.I) "_blank", "_default"
246 // It's not the right place to create a new task here. Only the desktop can do that.
247 // Normally the functionality for "_blank" is provided by findFrame() - but that would create it directly
248 // here. that's why we must "intercept" here.
250 if (
251 (sTargetFrameName==SPECIALTARGET_BLANK ) ||
252 (sTargetFrameName==SPECIALTARGET_DEFAULT)
255 css::uno::Reference< css::frame::XDispatchProvider > xParent( xFrame->getCreator(), css::uno::UNO_QUERY );
256 if (xParent.is())
257 xDispatcher = xParent->queryDispatch(aURL, sTargetFrameName, 0); // it's a special target - ignore search flags
260 // I.II) "_beamer"
261 // Special sub frame of a top frame only. Search or create it. ... OK it's currently a little bit HACKI.
262 // Only the sfx (means the controller) can create it.
264 else if (sTargetFrameName==SPECIALTARGET_BEAMER)
266 css::uno::Reference< css::frame::XDispatchProvider > xBeamer( xFrame->findFrame( SPECIALTARGET_BEAMER, css::frame::FrameSearchFlag::CHILDREN | css::frame::FrameSearchFlag::SELF ), css::uno::UNO_QUERY );
267 if (xBeamer.is())
269 xDispatcher = xBeamer->queryDispatch(aURL, SPECIALTARGET_SELF, 0);
271 else
273 css::uno::Reference< css::frame::XDispatchProvider > xController( xFrame->getController(), css::uno::UNO_QUERY );
274 if (xController.is())
275 // force using of special target - but use original search flags
276 // May the caller used the CREATE flag or not!
277 xDispatcher = xController->queryDispatch(aURL, SPECIALTARGET_BEAMER, nSearchFlags);
281 // I.IV) "_parent"
282 // Our parent frame (if it exist) should handle this URL.
284 else if (sTargetFrameName==SPECIALTARGET_PARENT)
286 css::uno::Reference< css::frame::XDispatchProvider > xParent( xFrame->getCreator(), css::uno::UNO_QUERY );
287 if (xParent.is())
288 // SELF => we must address the parent directly... and not his parent or any other parent!
289 xDispatcher = xParent->queryDispatch(aURL, SPECIALTARGET_SELF, 0);
292 // I.V) "_top"
293 // This request must be forwarded to any parent frame, till we reach a top frame.
294 // If no parent exist, we can handle itself.
296 else if (sTargetFrameName==SPECIALTARGET_TOP)
298 if (xFrame->isTop())
300 // If we are this top frame itself (means our owner frame)
301 // we should call ourself recursiv with a better target "_self".
302 // So we can share the same code! (see reaction for "_self" inside this method too.)
303 xDispatcher = queryDispatch(aURL,SPECIALTARGET_SELF,0);
305 else
307 css::uno::Reference< css::frame::XDispatchProvider > xParent( xFrame->getCreator(), css::uno::UNO_QUERY );
308 // Normally if isTop() returned sal_False ... the parent frame MUST(!) exist ...
309 // But it seems to be better to check that here to prevent us against an access violation.
310 if (xParent.is())
311 xDispatcher = xParent->queryDispatch(aURL, SPECIALTARGET_TOP, 0);
315 // I.VI) "_self", ""
316 // Our owner frame should handle this URL. But we can't do it for all of them.
317 // So we ask the internal set controller first. If he disagree we try to find a registered
318 // protocol handler. If this failed too - we check for a loadable content and in case of true
319 // we load it into the frame by returning specialized dispatch object.
321 else if (
322 (sTargetFrameName==SPECIALTARGET_SELF) ||
323 (sTargetFrameName.isEmpty())
326 // There exist a hard coded interception for special URLs.
327 if ( aURL.Complete == ".uno:CloseDoc" || aURL.Complete == ".uno:CloseWin" )
329 css::uno::Reference< css::frame::XDispatchProvider > xParent( xFrame->getCreator(), css::uno::UNO_QUERY );
330 // In case the frame is not a top one, is not based on system window and has a parent,
331 // the parent frame should be queried for the correct dispatcher.
332 // See i93473
333 if (
334 !WindowHelper::isTopWindow(xFrame->getContainerWindow()) &&
335 !VCLUnoHelper::GetWindow(xFrame->getContainerWindow())->IsSystemWindow() &&
336 xParent.is()
338 xDispatcher = xParent->queryDispatch(aURL, SPECIALTARGET_SELF, 0);
339 else
340 xDispatcher = implts_getOrCreateDispatchHelper( E_CLOSEDISPATCHER, xFrame );
342 else if ( aURL.Complete == ".uno:CloseFrame" )
343 xDispatcher = implts_getOrCreateDispatchHelper( E_CLOSEDISPATCHER, xFrame );
345 if ( ! xDispatcher.is())
347 // Ask our controller for his agreement for these dispatched URL ...
348 // because some URLs are internal and can be handled faster by SFX - which most is the current controller!
349 // But in case of e.g. the bibliography not all queries will be handled successfully here.
350 css::uno::Reference< css::frame::XDispatchProvider > xController( xFrame->getController(), css::uno::UNO_QUERY );
351 if (xController.is())
352 xDispatcher = xController->queryDispatch(aURL, SPECIALTARGET_SELF, 0);
355 // If controller has no fun to dispatch these URL - we must search another right dispatcher.
356 // Search for any registered protocol handler first.
357 if (!xDispatcher.is())
358 xDispatcher = implts_searchProtocolHandler(aURL);
360 // Not for controller - not for protocol handler
361 // It should be a loadable content - may be a file. Check it ...
362 // This check is necessary to found out, that
363 // support for some protocols isn't installed by user. May be
364 // "ftp" isn't available. So we suppress creation of our self dispatcher.
365 // The result will be clear. He can't handle it - but he would try it.
366 if (
367 ( ! xDispatcher.is() ) &&
368 ( implts_isLoadableContent(aURL) )
371 xDispatcher = implts_getOrCreateDispatchHelper( E_SELFDISPATCHER, xFrame );
375 // I.VII) no further special handlings exist
376 // Now we have to search for the right target frame by calling findFrame() - but should provide our code
377 // against creation of a new task if no frame could be found.
378 // I said it before - it's allowed for dispatch() only.
380 else
382 sal_Int32 nRightFlags = nSearchFlags & ~css::frame::FrameSearchFlag::CREATE;
384 // try to find any existing target and ask him for his dispatcher
385 css::uno::Reference< css::frame::XFrame > xFoundFrame = xFrame->findFrame(sTargetFrameName, nRightFlags);
386 if (xFoundFrame.is())
388 // Attention: Found target is our own owner frame!
389 // Don't ask him for his dispatcher. We know it already - it's our self dispatch helper.
390 // Otherwise we can start a never ending recursiv call. Why?
391 // Somewhere called our owner frame - he called some interceptor objects - and may by this dispatch provider
392 // is called. If wa use queryDispatch() on our owner frame again - we start this call stack again ... and again.
393 if (xFoundFrame==xFrame)
394 xDispatcher = implts_getOrCreateDispatchHelper( E_SELFDISPATCHER, xFrame );
395 else
397 css::uno::Reference< css::frame::XDispatchProvider > xProvider( xFoundFrame, css::uno::UNO_QUERY );
398 xDispatcher = xProvider->queryDispatch(aURL,SPECIALTARGET_SELF,0);
401 else
402 // if it couldn't be found - but creation was allowed
403 // forward request to the desktop.
404 // Note: The given target name must be used to set the name on new created task!
405 // Don't forward request by changing it to a special one e.g _blank.
406 // Use the CREATE flag only to prevent call against further searches.
407 // We already know it - the target must be created new.
408 if (nSearchFlags & css::frame::FrameSearchFlag::CREATE)
410 css::uno::Reference< css::frame::XDispatchProvider > xParent( xFrame->getCreator(), css::uno::UNO_QUERY );
411 if (xParent.is())
412 xDispatcher = xParent->queryDispatch(aURL, sTargetFrameName, css::frame::FrameSearchFlag::CREATE);
416 return xDispatcher;
420 @short search for a registered protocol handler and ask him for a dispatch object
421 @descr We search a suitable handler inside our cfg package org.openoffice.Office.ProtocolHandler.
422 If we found anyone, we create and initialize it. Initialize means: we set our owner frame on it
423 as context information. He can use it or leave it. Of course - we are aware of handler implementations,
424 which doesn't support initialization. It's an optional feature.
426 @param aURL
427 the dispatch URL for which may a handler is registered
429 @return A dispatch object if a handler was found and agree with the given URL or <NULL/> otherwise.
431 @threadsafe yes
433 css::uno::Reference< css::frame::XDispatch > DispatchProvider::implts_searchProtocolHandler( const css::util::URL& aURL )
435 css::uno::Reference< css::frame::XDispatch > xDispatcher;
436 ProtocolHandler aHandler;
438 // This member is threadsafe by himself and lives if we live - we don't need any mutex here.
439 if (framework::HandlerCache::search(aURL,&aHandler))
441 css::uno::Reference< css::frame::XDispatchProvider > xHandler;
443 SolarMutexGuard g;
445 // create it
446 bool bInitialize = true;
449 // Only create the protocol handler instance once, the creation is expensive.
450 auto it = m_aProtocolHandlers.find(aHandler.m_sUNOName);
451 if (it == m_aProtocolHandlers.end())
453 xHandler.set(
454 css::uno::Reference<css::lang::XMultiServiceFactory>(m_xContext->getServiceManager(), css::uno::UNO_QUERY_THROW)
455 ->createInstance(aHandler.m_sUNOName),
456 css::uno::UNO_QUERY);
458 // Check if the handler explicitly requested to avoid caching.
459 css::uno::Reference<css::util::XCacheInfo> xCacheInfo(xHandler, css::uno::UNO_QUERY);
460 if (!xCacheInfo.is() || xCacheInfo->isCachingAllowed())
462 m_aProtocolHandlers.emplace(aHandler.m_sUNOName, xHandler);
465 else
467 xHandler = it->second;
468 bInitialize = false;
471 catch(const css::uno::Exception&) {}
473 // look if initialization is necessary
474 css::uno::Reference< css::lang::XInitialization > xInit( xHandler, css::uno::UNO_QUERY );
475 if (xInit.is() && bInitialize)
477 css::uno::Reference< css::frame::XFrame > xOwner( m_xFrame.get(), css::uno::UNO_QUERY );
478 SAL_WARN_IF(!xOwner.is(), "fwk", "DispatchProvider::implts_searchProtocolHandler(): Couldn't get reference to my owner frame. So I can't set may needed context information for this protocol handler.");
479 if (xOwner.is())
483 // but do it only, if all context information is OK
484 css::uno::Sequence< css::uno::Any > lContext{ css::uno::Any(xOwner) };
485 xInit->initialize(lContext);
487 catch(const css::uno::Exception&) {}
492 // ask for his (sub)dispatcher for the given URL
493 if (xHandler.is())
494 xDispatcher = xHandler->queryDispatch(aURL,SPECIALTARGET_SELF,0);
497 return xDispatcher;
501 @short get or create new dispatch helper
502 @descr Sometimes we need some helper implementations to support dispatching of special URLs or commands.
503 But it's not a good idea to hold these services for the whole life time of this provider instance.
504 We should create it on demand...
505 That's why we implement this method. It return an already existing helper or create a new one otherwise.
507 @attention The parameter sTarget and nSearchFlags are defaulted to "" and 0!
508 Mostly it depends from the parameter eHelper is they are required or not.
510 @param eHelper
511 specify the requested dispatch helper
512 @param xOwner
513 the target of possible dispatch() call on created dispatch helper
514 @param sTarget
515 the target parameter of the original queryDispatch() request
516 @param nSearchFlags
517 the flags parameter of the original queryDispatch() request
518 @return A reference to a dispatch helper.
520 @threadsafe yes
522 css::uno::Reference< css::frame::XDispatch > DispatchProvider::implts_getOrCreateDispatchHelper( EDispatchHelper eHelper ,
523 const css::uno::Reference< css::frame::XFrame >& xOwner ,
524 const OUString& sTarget ,
525 sal_Int32 nSearchFlags)
527 css::uno::Reference< css::frame::XDispatch > xDispatchHelper;
529 switch (eHelper)
531 case E_CREATEDISPATCHER :
532 xDispatchHelper = new LoadDispatcher(m_xContext, xOwner, sTarget, nSearchFlags);
533 break;
535 case E_BLANKDISPATCHER :
537 if (xOwner.is())
538 xDispatchHelper = new LoadDispatcher(m_xContext, xOwner, SPECIALTARGET_BLANK, 0);
540 break;
542 case E_DEFAULTDISPATCHER :
544 if (xOwner.is())
545 xDispatchHelper = new LoadDispatcher(m_xContext, xOwner, SPECIALTARGET_DEFAULT, 0);
547 break;
549 case E_SELFDISPATCHER :
550 xDispatchHelper = new LoadDispatcher(m_xContext, xOwner, SPECIALTARGET_SELF, 0);
551 break;
553 case E_CLOSEDISPATCHER :
554 xDispatchHelper = new CloseDispatcher( m_xContext, xOwner, sTarget );
555 break;
557 case E_STARTMODULEDISPATCHER :
558 xDispatchHelper = new StartModuleDispatcher( m_xContext );
559 break;
562 return xDispatchHelper;
566 @short check URL for support by our used loader or handler
567 @descr If we must return our own dispatch helper implementations (self, blank, create dispatcher!)
568 we should be sure, that URL describe any loadable content. Otherwise slot/uno URLs
569 will be detected... but there exist nothing for real loading into a target frame!
571 @param aURL
572 URL which should be "detected"
573 @return <TRUE/> if somewhere could handle that - <FALSE/> otherwise.
575 @threadsafe yes
577 // static
578 bool DispatchProvider::implts_isLoadableContent( const css::util::URL& aURL )
580 LoadEnv::EContentType eType = LoadEnv::classifyContent(aURL.Complete, css::uno::Sequence< css::beans::PropertyValue >());
581 return ( eType == LoadEnv::E_CAN_BE_LOADED );
584 } // namespace framework
586 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */