Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / scripting / source / protocolhandler / scripthandler.cxx
blobc167fb3c42d820cdb10b77e0c94d0af91e33db77
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 "scripthandler.hxx"
22 #include <com/sun/star/frame/DispatchResultEvent.hpp>
23 #include <com/sun/star/frame/DispatchResultState.hpp>
24 #include <com/sun/star/frame/XController.hpp>
25 #include <com/sun/star/frame/XModel.hpp>
27 #include <com/sun/star/document/XEmbeddedScripts.hpp>
28 #include <com/sun/star/document/XScriptInvocationContext.hpp>
30 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
31 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
32 #include <com/sun/star/script/provider/ScriptFrameworkErrorException.hpp>
33 #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
34 #include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp>
35 #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
37 #include <sfx2/objsh.hxx>
38 #include <sfx2/frame.hxx>
39 #include <sfx2/sfxdlg.hxx>
40 #include <vcl/abstdlg.hxx>
41 #include <tools/diagnose_ex.h>
42 #include <sal/log.hxx>
44 #include <comphelper/processfactory.hxx>
45 #include <cppuhelper/factory.hxx>
46 #include <cppuhelper/exc_hlp.hxx>
47 #include <cppuhelper/supportsservice.hxx>
48 #include <framework/documentundoguard.hxx>
49 #include <officecfg/Office/Common.hxx>
51 #include <com/sun/star/uno/XComponentContext.hpp>
52 #include <com/sun/star/uri/XUriReference.hpp>
53 #include <com/sun/star/uri/XVndSunStarScriptUrlReference.hpp>
54 #include <com/sun/star/uri/UriReferenceFactory.hpp>
56 #include <memory>
58 using namespace ::com::sun::star;
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::frame;
61 using namespace ::com::sun::star::util;
62 using namespace ::com::sun::star::beans;
63 using namespace ::com::sun::star::lang;
64 using namespace ::com::sun::star::script;
65 using namespace ::com::sun::star::script::provider;
66 using namespace ::com::sun::star::document;
68 namespace scripting_protocolhandler
71 void SAL_CALL ScriptProtocolHandler::initialize(
72 const css::uno::Sequence < css::uno::Any >& aArguments )
74 if ( m_bInitialised )
76 return ;
79 // first argument contains a reference to the frame (may be empty or the desktop,
80 // but usually it's a "real" frame)
81 if ( aArguments.hasElements() && !( aArguments[ 0 ] >>= m_xFrame ) )
83 OUString temp = "ScriptProtocolHandler::initialize: could not extract reference to the frame";
84 throw RuntimeException( temp );
87 ENSURE_OR_THROW( m_xContext.is(), "ScriptProtocolHandler::initialize: No Service Manager available" );
88 m_bInitialised = true;
91 Reference< XDispatch > SAL_CALL ScriptProtocolHandler::queryDispatch(
92 const URL& aURL, const OUString&, sal_Int32 )
94 Reference< XDispatch > xDispatcher;
95 // get scheme of url
97 Reference< uri::XUriReferenceFactory > xFac = uri::UriReferenceFactory::create( m_xContext );
98 Reference< uri::XUriReference > uriRef = xFac->parse( aURL.Complete );
99 if ( uriRef.is() )
101 if ( uriRef->getScheme() == "vnd.sun.star.script" )
103 xDispatcher = this;
107 return xDispatcher;
110 Sequence< Reference< XDispatch > > SAL_CALL
111 ScriptProtocolHandler::queryDispatches(
112 const Sequence < DispatchDescriptor >& seqDescriptor )
114 sal_Int32 nCount = seqDescriptor.getLength();
115 Sequence< Reference< XDispatch > > lDispatcher( nCount );
116 std::transform(seqDescriptor.begin(), seqDescriptor.end(), lDispatcher.begin(),
117 [this](const DispatchDescriptor& rDescr) -> Reference<XDispatch> {
118 return queryDispatch(rDescr.FeatureURL, rDescr.FrameName, rDescr.SearchFlags); });
119 return lDispatcher;
122 void SAL_CALL ScriptProtocolHandler::dispatchWithNotification(
123 const URL& aURL, const Sequence < PropertyValue >& lArgs,
124 const Reference< XDispatchResultListener >& xListener )
126 if (officecfg::Office::Common::Security::Scripting::DisableMacrosExecution::get())
127 return;
129 bool bSuccess = false;
130 Any invokeResult;
131 bool bCaughtException = false;
132 Any aException;
134 if ( m_bInitialised )
138 css::uno::Reference<css::uri::XUriReferenceFactory> urifac(
139 css::uri::UriReferenceFactory::create(m_xContext));
140 css::uno::Reference<css::uri::XVndSunStarScriptUrlReference> uri(
141 urifac->parse(aURL.Complete), css::uno::UNO_QUERY_THROW);
142 auto const loc = uri->getParameter("location");
143 bool bIsDocumentScript = loc == "document";
145 if ( bIsDocumentScript )
147 // obtain the component for our security check
148 Reference< XEmbeddedScripts > xDocumentScripts;
149 if ( getScriptInvocation() )
150 xDocumentScripts.set( m_xScriptInvocation->getScriptContainer(), UNO_SET_THROW );
152 OSL_ENSURE( xDocumentScripts.is(), "ScriptProtocolHandler::dispatchWithNotification: can't do the security check!" );
153 if ( !xDocumentScripts.is() || !xDocumentScripts->getAllowMacroExecution() )
155 if ( xListener.is() )
157 css::frame::DispatchResultEvent aEvent(
158 static_cast< ::cppu::OWeakObject* >( this ),
159 css::frame::DispatchResultState::FAILURE,
160 invokeResult );
163 xListener->dispatchFinished( aEvent ) ;
165 catch(const RuntimeException &)
167 TOOLS_WARN_EXCEPTION("scripting",
168 "ScriptProtocolHandler::dispatchWithNotification: caught RuntimeException"
169 "while dispatchFinished with failure of the execution");
172 return;
176 // Creates a ScriptProvider ( if one is not created already )
177 createScriptProvider();
179 Reference< provider::XScript > xFunc =
180 m_xScriptProvider->getScript( aURL.Complete );
181 ENSURE_OR_THROW( xFunc.is(),
182 "ScriptProtocolHandler::dispatchWithNotification: validate xFunc - unable to obtain XScript interface" );
185 Sequence< Any > inArgs( 0 );
186 Sequence< Any > outArgs( 0 );
187 Sequence< sal_Int16 > outIndex;
189 if ( lArgs.hasElements() )
191 int argCount = 0;
192 for ( const auto& rArg : lArgs )
194 // Sometimes we get a propertyval with name = "Referer" or "SynchronMode". These
195 // are not actual arguments to be passed to script, but flags describing the
196 // call, so ignore. Who thought that passing such "meta-arguments" mixed in with
197 // real arguments was a good idea?
198 if ( (rArg.Name != "Referer" &&
199 rArg.Name != "SynchronMode") ||
200 rArg.Name.isEmpty() ) //TODO:???
202 inArgs.realloc( ++argCount );
203 inArgs[ argCount - 1 ] = rArg.Value;
208 // attempt to protect the document against the script tampering with its Undo Context
209 std::unique_ptr< ::framework::DocumentUndoGuard > pUndoGuard;
210 if ( bIsDocumentScript )
211 pUndoGuard.reset( new ::framework::DocumentUndoGuard( m_xScriptInvocation ) );
213 bSuccess = false;
214 while ( !bSuccess )
216 std::exception_ptr aFirstCaughtException;
219 invokeResult = xFunc->invoke( inArgs, outIndex, outArgs );
220 bSuccess = true;
222 catch( const provider::ScriptFrameworkErrorException& se )
224 if (!aFirstCaughtException)
225 aFirstCaughtException = std::current_exception();
227 if ( se.errorType != provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT )
228 // the only condition which allows us to retry is if there is no method with the
229 // given name/signature
230 std::rethrow_exception(aFirstCaughtException);
232 if ( !inArgs.hasElements() )
233 // no chance to retry if we can't strip more in-args
234 std::rethrow_exception(aFirstCaughtException);
236 // strip one argument, then retry
237 inArgs.realloc( inArgs.getLength() - 1 );
241 // Office doesn't handle exceptions rethrown here very well, it cores,
242 // all we can is log them and then set fail for the dispatch event!
243 // (if there is a listener of course)
244 catch ( const Exception & e )
246 aException = ::cppu::getCaughtException();
248 OUString reason = "ScriptProtocolHandler::dispatch: caught ";
250 invokeResult <<= reason.concat( aException.getValueTypeName() ).concat( ": " ).concat( e.Message );
252 bCaughtException = true;
255 else
257 invokeResult <<= OUString(
258 "ScriptProtocolHandler::dispatchWithNotification failed, ScriptProtocolHandler not initialised"
262 if ( bCaughtException )
264 SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
265 ScopedVclPtr<VclAbstractDialog> pDlg(
266 pFact->CreateScriptErrorDialog( aException ));
267 pDlg->Execute();
270 if ( xListener.is() )
272 // always call dispatchFinished(), because we didn't load a document but
273 // executed a macro instead!
274 css::frame::DispatchResultEvent aEvent;
276 aEvent.Source = static_cast< ::cppu::OWeakObject* >( this );
277 aEvent.Result = invokeResult;
278 if ( bSuccess )
280 aEvent.State = css::frame::DispatchResultState::SUCCESS;
282 else
284 aEvent.State = css::frame::DispatchResultState::FAILURE;
289 xListener->dispatchFinished( aEvent ) ;
291 catch(const RuntimeException &)
293 TOOLS_WARN_EXCEPTION("scripting",
294 "ScriptProtocolHandler::dispatchWithNotification: caught RuntimeException"
295 "while dispatchFinished" );
300 void SAL_CALL ScriptProtocolHandler::dispatch(
301 const URL& aURL, const Sequence< PropertyValue >& lArgs )
303 dispatchWithNotification( aURL, lArgs, Reference< XDispatchResultListener >() );
306 void SAL_CALL ScriptProtocolHandler::addStatusListener(
307 const Reference< XStatusListener >&, const URL& )
309 // implement if status is supported
312 void SAL_CALL ScriptProtocolHandler::removeStatusListener(
313 const Reference< XStatusListener >&, const URL& )
316 bool
317 ScriptProtocolHandler::getScriptInvocation()
319 if ( !m_xScriptInvocation.is() && m_xFrame.is() )
321 Reference< XController > xController = m_xFrame->getController();
322 if ( xController .is() )
324 // try to obtain an XScriptInvocationContext interface, preferred from the
325 // mode, then from the controller
326 if ( !m_xScriptInvocation.set( xController->getModel(), UNO_QUERY ) )
327 m_xScriptInvocation.set( xController, UNO_QUERY );
329 else
331 if ( m_xFrame.is() )
333 SfxFrame* pFrame = nullptr;
334 for ( pFrame = SfxFrame::GetFirst(); pFrame; pFrame = SfxFrame::GetNext( *pFrame ) )
336 if ( pFrame->GetFrameInterface() == m_xFrame )
337 break;
339 SfxObjectShell* pDocShell = pFrame ? pFrame->GetCurrentDocument() : SfxObjectShell::Current();
340 if ( pDocShell )
342 Reference< XModel > xModel( pDocShell->GetModel() );
343 m_xScriptInvocation.set( xModel, UNO_QUERY );
348 return m_xScriptInvocation.is();
351 void ScriptProtocolHandler::createScriptProvider()
353 if ( m_xScriptProvider.is() )
354 return;
358 // first, ask the component supporting the XScriptInvocationContext interface
359 // (if there is one) for a script provider
360 if ( getScriptInvocation() )
362 Reference< XScriptProviderSupplier > xSPS( m_xScriptInvocation, UNO_QUERY );
363 if ( xSPS.is() )
364 m_xScriptProvider = xSPS->getScriptProvider();
367 // second, ask the model in our frame
368 if ( !m_xScriptProvider.is() && m_xFrame.is() )
370 Reference< XController > xController = m_xFrame->getController();
371 if ( xController .is() )
373 Reference< XScriptProviderSupplier > xSPS( xController->getModel(), UNO_QUERY );
374 if ( xSPS.is() )
375 m_xScriptProvider = xSPS->getScriptProvider();
380 // as a fallback, ask the controller
381 if ( !m_xScriptProvider.is() && m_xFrame.is() )
383 Reference< XScriptProviderSupplier > xSPS( m_xFrame->getController(), UNO_QUERY );
384 if ( xSPS.is() )
385 m_xScriptProvider = xSPS->getScriptProvider();
388 // if nothing of this is successful, use the master script provider
389 if ( !m_xScriptProvider.is() )
391 Reference< provider::XScriptProviderFactory > xFac =
392 provider::theMasterScriptProviderFactory::get( m_xContext );
394 Any aContext;
395 if ( getScriptInvocation() )
396 aContext <<= m_xScriptInvocation;
397 m_xScriptProvider.set( xFac->createScriptProvider( aContext ), UNO_SET_THROW );
400 catch ( const Exception & e )
402 css::uno::Any anyEx = cppu::getCaughtException();
403 throw css::lang::WrappedTargetRuntimeException(
404 "ScriptProtocolHandler::createScriptProvider: " + e.Message,
405 nullptr, anyEx );
409 ScriptProtocolHandler::ScriptProtocolHandler( const Reference< css::uno::XComponentContext > & xContext )
410 : m_bInitialised( false ), m_xContext( xContext )
414 ScriptProtocolHandler::~ScriptProtocolHandler()
418 /* XServiceInfo */
419 OUString SAL_CALL ScriptProtocolHandler::getImplementationName( )
421 return impl_getStaticImplementationName();
424 /* XServiceInfo */
425 sal_Bool SAL_CALL ScriptProtocolHandler::supportsService(const OUString& sServiceName )
427 return cppu::supportsService(this, sServiceName);
430 /* XServiceInfo */
431 Sequence< OUString > SAL_CALL ScriptProtocolHandler::getSupportedServiceNames()
433 return impl_getStaticSupportedServiceNames();
436 /* Helper for XServiceInfo */
437 Sequence< OUString > ScriptProtocolHandler::impl_getStaticSupportedServiceNames()
439 return {"com.sun.star.frame.ProtocolHandler"};
442 /* Helper for XServiceInfo */
443 OUString ScriptProtocolHandler::impl_getStaticImplementationName()
445 return "com.sun.star.comp.ScriptProtocolHandler";
448 /* Helper for registry */
449 Reference< XInterface > SAL_CALL ScriptProtocolHandler::impl_createInstance(
450 const Reference< css::lang::XMultiServiceFactory >& xServiceManager )
452 return Reference< XInterface > ( *new ScriptProtocolHandler( comphelper::getComponentContext(xServiceManager) ) );
455 /* Factory for registration */
456 Reference< XSingleServiceFactory > ScriptProtocolHandler::impl_createFactory(
457 const Reference< XMultiServiceFactory >& xServiceManager )
459 Reference< XSingleServiceFactory > xReturn (
460 cppu::createSingleFactory( xServiceManager,
461 ScriptProtocolHandler::impl_getStaticImplementationName(),
462 ScriptProtocolHandler::impl_createInstance,
463 ScriptProtocolHandler::impl_getStaticSupportedServiceNames() )
465 return xReturn;
468 } // namespace scripting_protocolhandler
470 extern "C"
472 SAL_DLLPUBLIC_EXPORT void* protocolhandler_component_getFactory( const sal_Char * pImplementationName ,
473 void * pServiceManager ,
474 void * )
476 // Set default return value for this operation - if it failed.
477 void * pReturn = nullptr ;
479 if (
480 ( pImplementationName != nullptr ) &&
481 ( pServiceManager != nullptr )
484 // Define variables which are used in following macros.
485 css::uno::Reference< css::lang::XSingleServiceFactory > xFactory;
486 css::uno::Reference< css::lang::XMultiServiceFactory > xServiceManager(
487 static_cast< css::lang::XMultiServiceFactory* >( pServiceManager ) ) ;
489 if ( ::scripting_protocolhandler::ScriptProtocolHandler::impl_getStaticImplementationName().equalsAscii(
490 pImplementationName ) )
492 xFactory = ::scripting_protocolhandler::ScriptProtocolHandler::impl_createFactory( xServiceManager );
495 // Factory is valid - service was found.
496 if ( xFactory.is() )
498 xFactory->acquire();
499 pReturn = xFactory.get();
503 // Return with result of this operation.
504 return pReturn ;
506 } // extern "C"
509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */