1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_scripting.hxx" 26 #include "scripthandler.hxx" 27 28 #include <osl/mutex.hxx> 29 30 #include <com/sun/star/frame/DispatchResultEvent.hpp> 31 #include <com/sun/star/frame/DispatchResultState.hpp> 32 #include <com/sun/star/frame/XController.hpp> 33 #include <com/sun/star/frame/XModel.hpp> 34 35 #include <com/sun/star/document/XEmbeddedScripts.hpp> 36 #include <com/sun/star/document/XScriptInvocationContext.hpp> 37 38 #include <com/sun/star/lang/XSingleServiceFactory.hpp> 39 40 #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp> 41 #include <com/sun/star/script/provider/XScriptProviderFactory.hpp> 42 #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp> 43 44 #include <rtl/uri.hxx> 45 #include <sfx2/objsh.hxx> 46 #include <sfx2/frame.hxx> 47 #include <sfx2/sfxdlg.hxx> 48 #include <vcl/abstdlg.hxx> 49 #include <tools/diagnose_ex.h> 50 51 #include <cppuhelper/factory.hxx> 52 #include <cppuhelper/exc_hlp.hxx> 53 #include <util/util.hxx> 54 #include <framework/documentundoguard.hxx> 55 56 #include "com/sun/star/uno/XComponentContext.hpp" 57 #include "com/sun/star/uri/XUriReference.hpp" 58 #include "com/sun/star/uri/XUriReferenceFactory.hpp" 59 #include "com/sun/star/uri/XVndSunStarScriptUrl.hpp" 60 #include "com/sun/star/beans/XPropertySet.hpp" 61 62 using namespace ::com::sun::star; 63 using namespace ::com::sun::star::uno; 64 using namespace ::com::sun::star::frame; 65 using namespace ::com::sun::star::util; 66 using namespace ::com::sun::star::beans; 67 using namespace ::com::sun::star::lang; 68 using namespace ::com::sun::star::script; 69 using namespace ::com::sun::star::script::provider; 70 using namespace ::com::sun::star::document; 71 72 namespace scripting_protocolhandler 73 { 74 75 const sal_Char * const MYSERVICENAME = "com.sun.star.frame.ProtocolHandler"; 76 const sal_Char * const MYIMPLNAME = "com.sun.star.comp.ScriptProtocolHandler"; 77 const sal_Char * MYSCHEME = "vnd.sun.star.script"; 78 const sal_Int32 MYSCHEME_LEN = 20; 79 80 void SAL_CALL ScriptProtocolHandler::initialize( 81 const css::uno::Sequence < css::uno::Any >& aArguments ) 82 throw ( css::uno::Exception ) 83 { 84 if ( m_bInitialised ) 85 { 86 return ; 87 } 88 89 // first argument contains a reference to the frame (may be empty or the desktop, 90 // but usually it's a "real" frame) 91 if ( aArguments.getLength() && 92 sal_False == ( aArguments[ 0 ] >>= m_xFrame ) ) 93 { 94 ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::initialize: could not extract reference to the frame" ); 95 throw RuntimeException( temp, Reference< XInterface >() ); 96 } 97 98 ENSURE_OR_THROW( m_xFactory.is(), "ScriptProtocolHandler::initialize: No Service Manager available" ); 99 m_bInitialised = true; 100 } 101 102 Reference< XDispatch > SAL_CALL ScriptProtocolHandler::queryDispatch( 103 const URL& aURL, const ::rtl::OUString& sTargetFrameName, sal_Int32 nSearchFlags ) 104 throw( ::com::sun::star::uno::RuntimeException ) 105 { 106 (void)sTargetFrameName; 107 (void)nSearchFlags; 108 109 Reference< XDispatch > xDispatcher; 110 // get scheme of url 111 112 Reference< uri::XUriReferenceFactory > xFac ( 113 m_xFactory->createInstance( rtl::OUString::createFromAscii( 114 "com.sun.star.uri.UriReferenceFactory") ) , UNO_QUERY ); 115 if ( xFac.is() ) 116 { 117 Reference< uri::XUriReference > uriRef( 118 xFac->parse( aURL.Complete ), UNO_QUERY ); 119 if ( uriRef.is() ) 120 { 121 if ( uriRef->getScheme().equals( ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYSCHEME ) ) ) 122 { 123 xDispatcher = this; 124 } 125 } 126 } 127 128 return xDispatcher; 129 } 130 131 Sequence< Reference< XDispatch > > SAL_CALL 132 ScriptProtocolHandler::queryDispatches( 133 const Sequence < DispatchDescriptor >& seqDescriptor ) 134 throw( RuntimeException ) 135 { 136 sal_Int32 nCount = seqDescriptor.getLength(); 137 Sequence< Reference< XDispatch > > lDispatcher( nCount ); 138 for ( sal_Int32 i = 0; i < nCount; ++i ) 139 { 140 lDispatcher[ i ] = this->queryDispatch( seqDescriptor[ i ].FeatureURL, 141 seqDescriptor[ i ].FrameName, 142 seqDescriptor[ i ].SearchFlags ); 143 } 144 return lDispatcher; 145 } 146 147 void SAL_CALL ScriptProtocolHandler::dispatchWithNotification( 148 const URL& aURL, const Sequence < PropertyValue >& lArgs, 149 const Reference< XDispatchResultListener >& xListener ) 150 throw ( RuntimeException ) 151 { 152 153 sal_Bool bSuccess = sal_False; 154 Any invokeResult; 155 bool bCaughtException = sal_False; 156 Any aException; 157 158 if ( m_bInitialised ) 159 { 160 try 161 { 162 ::rtl::OUString xStringUri = ::rtl::Uri::decode( aURL.Complete, 163 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 ); 164 bool bIsDocumentScript = ( xStringUri.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "document" ) ) !=-1 ); 165 166 if ( bIsDocumentScript ) 167 { 168 // obtain the component for our security check 169 Reference< XEmbeddedScripts > xDocumentScripts; 170 if ( getScriptInvocation() ) 171 xDocumentScripts.set( m_xScriptInvocation->getScriptContainer(), UNO_SET_THROW ); 172 173 OSL_ENSURE( xDocumentScripts.is(), "ScriptProtocolHandler::dispatchWithNotification: can't do the security check!" ); 174 if ( !xDocumentScripts.is() || !xDocumentScripts->getAllowMacroExecution() ) 175 { 176 if ( xListener.is() ) 177 { 178 ::com::sun::star::frame::DispatchResultEvent aEvent( 179 static_cast< ::cppu::OWeakObject* >( this ), 180 ::com::sun::star::frame::DispatchResultState::FAILURE, 181 invokeResult ); 182 try 183 { 184 xListener->dispatchFinished( aEvent ) ; 185 } 186 catch(RuntimeException & e) 187 { 188 OSL_TRACE( 189 "ScriptProtocolHandler::dispatchWithNotification: caught RuntimeException" 190 "while dispatchFinished with failture of the execution %s", 191 ::rtl::OUStringToOString( e.Message, 192 RTL_TEXTENCODING_ASCII_US ).pData->buffer ); 193 } 194 } 195 return; 196 } 197 } 198 199 // Creates a ScriptProvider ( if one is not created already ) 200 createScriptProvider(); 201 202 Reference< provider::XScript > xFunc = 203 m_xScriptProvider->getScript( aURL.Complete ); 204 ENSURE_OR_THROW( xFunc.is(), 205 "ScriptProtocolHandler::dispatchWithNotification: validate xFunc - unable to obtain XScript interface" ); 206 207 208 Sequence< Any > inArgs( 0 ); 209 Sequence< Any > outArgs( 0 ); 210 Sequence< sal_Int16 > outIndex; 211 212 if ( lArgs.getLength() > 0 ) 213 { 214 int argCount = 0; 215 for ( int index = 0; index < lArgs.getLength(); index++ ) 216 { 217 // Sometimes we get a propertyval with name = "Referer" 218 // this is not an argument to be passed to script, so 219 // ignore. 220 if ( lArgs[ index ].Name.compareToAscii("Referer") != 0 || 221 lArgs[ index ].Name.getLength() == 0 ) 222 { 223 inArgs.realloc( ++argCount ); 224 inArgs[ argCount - 1 ] = lArgs[ index ].Value; 225 } 226 } 227 } 228 229 // attempt to protect the document against the script tampering with its Undo Context 230 ::std::auto_ptr< ::framework::DocumentUndoGuard > pUndoGuard; 231 if ( bIsDocumentScript ) 232 pUndoGuard.reset( new ::framework::DocumentUndoGuard( m_xScriptInvocation ) ); 233 234 bSuccess = sal_False; 235 while ( !bSuccess ) 236 { 237 Any aFirstCaughtException; 238 try 239 { 240 invokeResult = xFunc->invoke( inArgs, outIndex, outArgs ); 241 bSuccess = sal_True; 242 } 243 catch( const provider::ScriptFrameworkErrorException& se ) 244 { 245 if ( !aFirstCaughtException.hasValue() ) 246 aFirstCaughtException = ::cppu::getCaughtException(); 247 248 if ( se.errorType != provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT ) 249 // the only condition which allows us to retry is if there is no method with the 250 // given name/signature 251 ::cppu::throwException( aFirstCaughtException ); 252 253 if ( inArgs.getLength() == 0 ) 254 // no chance to retry if we can't strip more in-args 255 ::cppu::throwException( aFirstCaughtException ); 256 257 // strip one argument, then retry 258 inArgs.realloc( inArgs.getLength() - 1 ); 259 } 260 } 261 } 262 // Office doesn't handle exceptions rethrown here very well, it cores, 263 // all we can is log them and then set fail for the dispatch event! 264 // (if there is a listener of course) 265 catch ( const Exception & e ) 266 { 267 aException = ::cppu::getCaughtException(); 268 269 ::rtl::OUString reason = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ScriptProtocolHandler::dispatch: caught " ) ); 270 271 invokeResult <<= reason.concat( aException.getValueTypeName() ).concat( e.Message ); 272 273 bCaughtException = sal_True; 274 } 275 } 276 else 277 { 278 ::rtl::OUString reason = ::rtl::OUString::createFromAscii( 279 "ScriptProtocolHandler::dispatchWithNotification failed, ScriptProtocolHandler not initialised" 280 ); 281 invokeResult <<= reason; 282 } 283 284 if ( bCaughtException ) 285 { 286 SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create(); 287 288 if ( pFact != NULL ) 289 { 290 VclAbstractDialog* pDlg = 291 pFact->CreateScriptErrorDialog( NULL, aException ); 292 293 if ( pDlg != NULL ) 294 { 295 pDlg->Execute(); 296 delete pDlg; 297 } 298 } 299 } 300 301 if ( xListener.is() ) 302 { 303 // always call dispatchFinished(), because we didn't load a document but 304 // executed a macro instead! 305 ::com::sun::star::frame::DispatchResultEvent aEvent; 306 307 aEvent.Source = static_cast< ::cppu::OWeakObject* >( this ); 308 aEvent.Result = invokeResult; 309 if ( bSuccess ) 310 { 311 aEvent.State = ::com::sun::star::frame::DispatchResultState::SUCCESS; 312 } 313 else 314 { 315 aEvent.State = ::com::sun::star::frame::DispatchResultState::FAILURE; 316 } 317 318 try 319 { 320 xListener->dispatchFinished( aEvent ) ; 321 } 322 catch(RuntimeException & e) 323 { 324 OSL_TRACE( 325 "ScriptProtocolHandler::dispatchWithNotification: caught RuntimeException" 326 "while dispatchFinished %s", 327 ::rtl::OUStringToOString( e.Message, 328 RTL_TEXTENCODING_ASCII_US ).pData->buffer ); 329 } 330 } 331 } 332 333 void SAL_CALL ScriptProtocolHandler::dispatch( 334 const URL& aURL, const Sequence< PropertyValue >& lArgs ) 335 throw ( RuntimeException ) 336 { 337 dispatchWithNotification( aURL, lArgs, Reference< XDispatchResultListener >() ); 338 } 339 340 void SAL_CALL ScriptProtocolHandler::addStatusListener( 341 const Reference< XStatusListener >& xControl, const URL& aURL ) 342 throw ( RuntimeException ) 343 { 344 (void)xControl; 345 (void)aURL; 346 347 // implement if status is supported 348 } 349 350 void SAL_CALL ScriptProtocolHandler::removeStatusListener( 351 const Reference< XStatusListener >& xControl, const URL& aURL ) 352 throw ( RuntimeException ) 353 { 354 (void)xControl; 355 (void)aURL; 356 } 357 358 bool 359 ScriptProtocolHandler::getScriptInvocation() 360 { 361 if ( !m_xScriptInvocation.is() && m_xFrame.is() ) 362 { 363 Reference< XController > xController = m_xFrame->getController(); 364 if ( xController .is() ) 365 { 366 // try to obtain an XScriptInvocationContext interface, preferred from the 367 // mode, then from the controller 368 if ( !m_xScriptInvocation.set( xController->getModel(), UNO_QUERY ) ) 369 m_xScriptInvocation.set( xController, UNO_QUERY ); 370 } 371 else 372 { 373 Reference< XFrame > xFrame( m_xFrame.get(), UNO_QUERY ); 374 if ( xFrame.is() ) 375 { 376 SfxFrame* pFrame = NULL; 377 for ( pFrame = SfxFrame::GetFirst(); pFrame; pFrame = SfxFrame::GetNext( *pFrame ) ) 378 { 379 if ( pFrame->GetFrameInterface() == xFrame ) 380 break; 381 } 382 SfxObjectShell* pDocShell = pFrame ? pFrame->GetCurrentDocument() : SfxObjectShell::Current(); 383 if ( pDocShell ) 384 { 385 Reference< XModel > xModel( pDocShell->GetModel() ); 386 m_xScriptInvocation.set( xModel, UNO_QUERY ); 387 } 388 } 389 } 390 } 391 return m_xScriptInvocation.is(); 392 } 393 394 void ScriptProtocolHandler::createScriptProvider() 395 { 396 if ( m_xScriptProvider.is() ) 397 return; 398 399 try 400 { 401 // first, ask the component supporting the XScriptInvocationContext interface 402 // (if there is one) for a script provider 403 if ( getScriptInvocation() ) 404 { 405 Reference< XScriptProviderSupplier > xSPS( m_xScriptInvocation, UNO_QUERY ); 406 if ( xSPS.is() ) 407 m_xScriptProvider = xSPS->getScriptProvider(); 408 } 409 410 // second, ask the model in our frame 411 if ( !m_xScriptProvider.is() && m_xFrame.is() ) 412 { 413 Reference< XController > xController = m_xFrame->getController(); 414 if ( xController .is() ) 415 { 416 Reference< XScriptProviderSupplier > xSPS( xController->getModel(), UNO_QUERY ); 417 if ( xSPS.is() ) 418 m_xScriptProvider = xSPS->getScriptProvider(); 419 } 420 } 421 422 423 // as a fallback, ask the controller 424 if ( !m_xScriptProvider.is() && m_xFrame.is() ) 425 { 426 Reference< XScriptProviderSupplier > xSPS( m_xFrame->getController(), UNO_QUERY ); 427 if ( xSPS.is() ) 428 m_xScriptProvider = xSPS->getScriptProvider(); 429 } 430 431 // if nothing of this is successful, use the master script provider 432 if ( !m_xScriptProvider.is() ) 433 { 434 Reference< XPropertySet > xProps( m_xFactory, UNO_QUERY_THROW ); 435 436 ::rtl::OUString dc( 437 RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ); 438 439 Reference< XComponentContext > xCtx( 440 xProps->getPropertyValue( dc ), UNO_QUERY_THROW ); 441 442 ::rtl::OUString tmspf = ::rtl::OUString::createFromAscii( 443 "/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory"); 444 445 Reference< provider::XScriptProviderFactory > xFac( 446 xCtx->getValueByName( tmspf ), UNO_QUERY_THROW ); 447 448 Any aContext; 449 if ( getScriptInvocation() ) 450 aContext = makeAny( m_xScriptInvocation ); 451 m_xScriptProvider = Reference< provider::XScriptProvider > ( 452 xFac->createScriptProvider( aContext ), UNO_QUERY_THROW ); 453 } 454 } 455 catch ( RuntimeException & e ) 456 { 457 ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::createScriptProvider(), " ); 458 throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() ); 459 } 460 catch ( Exception & e ) 461 { 462 ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::createScriptProvider: " ); 463 throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() ); 464 } 465 } 466 467 ScriptProtocolHandler::ScriptProtocolHandler( 468 Reference< css::lang::XMultiServiceFactory > const& rFact ) : 469 m_bInitialised( false ), m_xFactory( rFact ) 470 { 471 } 472 473 ScriptProtocolHandler::~ScriptProtocolHandler() 474 { 475 } 476 477 /* XServiceInfo */ 478 ::rtl::OUString SAL_CALL ScriptProtocolHandler::getImplementationName( ) 479 throw( RuntimeException ) 480 { 481 return impl_getStaticImplementationName(); 482 } 483 484 /* XServiceInfo */ 485 sal_Bool SAL_CALL ScriptProtocolHandler::supportsService( 486 const ::rtl::OUString& sServiceName ) 487 throw( RuntimeException ) 488 { 489 Sequence< ::rtl::OUString > seqServiceNames = getSupportedServiceNames(); 490 const ::rtl::OUString* pArray = seqServiceNames.getConstArray(); 491 for ( sal_Int32 nCounter = 0; nCounter < seqServiceNames.getLength(); nCounter++ ) 492 { 493 if ( pArray[ nCounter ] == sServiceName ) 494 { 495 return sal_True ; 496 } 497 } 498 499 return sal_False ; 500 } 501 502 /* XServiceInfo */ 503 Sequence< ::rtl::OUString > SAL_CALL ScriptProtocolHandler::getSupportedServiceNames() 504 throw( RuntimeException ) 505 { 506 return impl_getStaticSupportedServiceNames(); 507 } 508 509 /* Helper for XServiceInfo */ 510 Sequence< ::rtl::OUString > ScriptProtocolHandler::impl_getStaticSupportedServiceNames() 511 { 512 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ); 513 Sequence< ::rtl::OUString > seqServiceNames( 1 ); 514 seqServiceNames.getArray() [ 0 ] = 515 ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYSERVICENAME ); 516 return seqServiceNames ; 517 } 518 519 /* Helper for XServiceInfo */ 520 ::rtl::OUString ScriptProtocolHandler::impl_getStaticImplementationName() 521 { 522 return ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYIMPLNAME ); 523 } 524 525 /* Helper for registry */ 526 Reference< XInterface > SAL_CALL ScriptProtocolHandler::impl_createInstance( 527 const Reference< css::lang::XMultiServiceFactory >& xServiceManager ) 528 throw( RuntimeException ) 529 { 530 return Reference< XInterface > ( *new ScriptProtocolHandler( xServiceManager ) ); 531 } 532 533 /* Factory for registration */ 534 Reference< XSingleServiceFactory > ScriptProtocolHandler::impl_createFactory( 535 const Reference< XMultiServiceFactory >& xServiceManager ) 536 { 537 Reference< XSingleServiceFactory > xReturn ( 538 cppu::createSingleFactory( xServiceManager, 539 ScriptProtocolHandler::impl_getStaticImplementationName(), 540 ScriptProtocolHandler::impl_createInstance, 541 ScriptProtocolHandler::impl_getStaticSupportedServiceNames() ) 542 ); 543 return xReturn; 544 } 545 546 } // namespace scripting_protocolhandler 547 548 /* exported functions for registration */ 549 extern "C" 550 { 551 552 #undef css 553 #define css ::com::sun::star 554 555 void SAL_CALL component_getImplementationEnvironment( 556 const sal_Char** ppEnvironmentTypeName, uno_Environment** ppEnvironment ) 557 { 558 (void)ppEnvironment; 559 560 *ppEnvironmentTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME ; 561 } 562 563 void* SAL_CALL component_getFactory( const sal_Char * pImplementationName , 564 void * pServiceManager , 565 void * pRegistryKey ) 566 { 567 (void)pRegistryKey; 568 569 // Set default return value for this operation - if it failed. 570 void * pReturn = NULL ; 571 572 if ( 573 ( pImplementationName != NULL ) && 574 ( pServiceManager != NULL ) 575 ) 576 { 577 // Define variables which are used in following macros. 578 ::com::sun::star::uno::Reference< 579 ::com::sun::star::lang::XSingleServiceFactory > xFactory ; 580 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > 581 xServiceManager( reinterpret_cast< 582 ::com::sun::star::lang::XMultiServiceFactory* >( pServiceManager ) ) ; 583 584 if ( ::scripting_protocolhandler::ScriptProtocolHandler::impl_getStaticImplementationName().equals( 585 ::rtl::OUString::createFromAscii( pImplementationName ) ) ) 586 { 587 xFactory = ::scripting_protocolhandler::ScriptProtocolHandler::impl_createFactory( xServiceManager ); 588 } 589 590 // Factory is valid - service was found. 591 if ( xFactory.is() ) 592 { 593 xFactory->acquire(); 594 pReturn = xFactory.get(); 595 } 596 } 597 598 // Return with result of this operation. 599 return pReturn ; 600 } 601 } // extern "C" 602 603 604