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 Sequence< Any > inArgs( 0 ); 158 159 if ( m_bInitialised ) 160 { 161 ::rtl::OUString aReferer; 162 if ( lArgs.getLength() > 0 ) 163 { 164 int argCount = 0; 165 for ( int index = 0; index < lArgs.getLength(); index++ ) 166 { 167 // The propertyval named "Referer" 168 // is not an argument to be passed to script 169 if ( lArgs[ index ].Name.compareToAscii("Referer") == 0 ) { 170 lArgs [ index ].Value >>= aReferer; 171 } else { 172 inArgs.realloc( ++argCount ); 173 inArgs[ argCount - 1 ] = lArgs[ index ].Value; 174 } 175 } 176 } 177 try 178 { 179 ::rtl::OUString xStringUri = ::rtl::Uri::decode( aURL.Complete, 180 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8 ); 181 bool bIsDocumentScript = ( xStringUri.indexOfAsciiL( RTL_CONSTASCII_STRINGPARAM( "document" ) ) !=-1 ); 182 bool bRefererIsTrusted = ( aReferer.compareToAscii("private:", 8) == 0 ); 183 184 // obtain the component for our security check. We could check bIsDocumentScript but the "location" could be forged 185 if ( getScriptInvocation() ) { 186 Reference< XEmbeddedScripts > xDocumentScripts; 187 xDocumentScripts.set( m_xScriptInvocation->getScriptContainer(), UNO_SET_THROW ); 188 189 OSL_ENSURE( xDocumentScripts.is(), "ScriptProtocolHandler::dispatchWithNotification: can't do the security check!" ); 190 if ( !xDocumentScripts.is() || 191 ( !bRefererIsTrusted && !xDocumentScripts->getAllowMacroExecution() ) ) 192 { 193 if ( xListener.is() ) 194 { 195 ::com::sun::star::frame::DispatchResultEvent aEvent( 196 static_cast< ::cppu::OWeakObject* >( this ), 197 ::com::sun::star::frame::DispatchResultState::FAILURE, 198 invokeResult ); 199 try 200 { 201 xListener->dispatchFinished( aEvent ) ; 202 } 203 catch(RuntimeException & e) 204 { 205 OSL_TRACE( 206 "ScriptProtocolHandler::dispatchWithNotification: caught RuntimeException" 207 "while dispatchFinished with failture of the execution %s", 208 ::rtl::OUStringToOString( e.Message, 209 RTL_TEXTENCODING_ASCII_US ).pData->buffer ); 210 } 211 } 212 return; 213 } 214 } 215 216 // Creates a ScriptProvider ( if one is not created allready ) 217 createScriptProvider(); 218 219 Reference< provider::XScript > xFunc = 220 m_xScriptProvider->getScript( aURL.Complete ); 221 ENSURE_OR_THROW( xFunc.is(), 222 "ScriptProtocolHandler::dispatchWithNotification: validate xFunc - unable to obtain XScript interface" ); 223 224 225 Sequence< Any > outArgs( 0 ); 226 Sequence< sal_Int16 > outIndex; 227 228 // attempt to protect the document against the script tampering with its Undo Context 229 ::std::auto_ptr< ::framework::DocumentUndoGuard > pUndoGuard; 230 if ( bIsDocumentScript ) 231 pUndoGuard.reset( new ::framework::DocumentUndoGuard( m_xScriptInvocation ) ); 232 233 bSuccess = sal_False; 234 while ( !bSuccess ) 235 { 236 Any aFirstCaughtException; 237 try 238 { 239 invokeResult = xFunc->invoke( inArgs, outIndex, outArgs ); 240 bSuccess = sal_True; 241 } 242 catch( const provider::ScriptFrameworkErrorException& se ) 243 { 244 if ( !aFirstCaughtException.hasValue() ) 245 aFirstCaughtException = ::cppu::getCaughtException(); 246 247 if ( se.errorType != provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT ) 248 // the only condition which allows us to retry is if there is no method with the 249 // given name/signature 250 ::cppu::throwException( aFirstCaughtException ); 251 252 if ( inArgs.getLength() == 0 ) 253 // no chance to retry if we can't strip more in-args 254 ::cppu::throwException( aFirstCaughtException ); 255 256 // strip one argument, then retry 257 inArgs.realloc( inArgs.getLength() - 1 ); 258 } 259 } 260 } 261 // Office doesn't handle exceptions rethrown here very well, it cores, 262 // all we can is log them and then set fail for the dispatch event! 263 // (if there is a listener of course) 264 catch ( const Exception & e ) 265 { 266 aException = ::cppu::getCaughtException(); 267 268 ::rtl::OUString reason = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ScriptProtocolHandler::dispatch: caught " ) ); 269 270 invokeResult <<= reason.concat( aException.getValueTypeName() ).concat( e.Message ); 271 272 bCaughtException = sal_True; 273 } 274 } 275 else 276 { 277 ::rtl::OUString reason = ::rtl::OUString::createFromAscii( 278 "ScriptProtocolHandler::dispatchWithNotification failed, ScriptProtocolHandler not initialised" 279 ); 280 invokeResult <<= reason; 281 } 282 283 if ( bCaughtException ) 284 { 285 SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create(); 286 287 if ( pFact != NULL ) 288 { 289 VclAbstractDialog* pDlg = 290 pFact->CreateScriptErrorDialog( NULL, aException ); 291 292 if ( pDlg != NULL ) 293 { 294 pDlg->Execute(); 295 delete pDlg; 296 } 297 } 298 } 299 300 if ( xListener.is() ) 301 { 302 // always call dispatchFinished(), because we didn't load a document but 303 // executed a macro instead! 304 ::com::sun::star::frame::DispatchResultEvent aEvent; 305 306 aEvent.Source = static_cast< ::cppu::OWeakObject* >( this ); 307 aEvent.Result = invokeResult; 308 if ( bSuccess ) 309 { 310 aEvent.State = ::com::sun::star::frame::DispatchResultState::SUCCESS; 311 } 312 else 313 { 314 aEvent.State = ::com::sun::star::frame::DispatchResultState::FAILURE; 315 } 316 317 try 318 { 319 xListener->dispatchFinished( aEvent ) ; 320 } 321 catch(RuntimeException & e) 322 { 323 OSL_TRACE( 324 "ScriptProtocolHandler::dispatchWithNotification: caught RuntimeException" 325 "while dispatchFinished %s", 326 ::rtl::OUStringToOString( e.Message, 327 RTL_TEXTENCODING_ASCII_US ).pData->buffer ); 328 } 329 } 330 } 331 332 void SAL_CALL ScriptProtocolHandler::dispatch( 333 const URL& aURL, const Sequence< PropertyValue >& lArgs ) 334 throw ( RuntimeException ) 335 { 336 dispatchWithNotification( aURL, lArgs, Reference< XDispatchResultListener >() ); 337 } 338 339 void SAL_CALL ScriptProtocolHandler::addStatusListener( 340 const Reference< XStatusListener >& xControl, const URL& aURL ) 341 throw ( RuntimeException ) 342 { 343 (void)xControl; 344 (void)aURL; 345 346 // implement if status is supported 347 } 348 349 void SAL_CALL ScriptProtocolHandler::removeStatusListener( 350 const Reference< XStatusListener >& xControl, const URL& aURL ) 351 throw ( RuntimeException ) 352 { 353 (void)xControl; 354 (void)aURL; 355 } 356 357 bool 358 ScriptProtocolHandler::getScriptInvocation() 359 { 360 if ( !m_xScriptInvocation.is() && m_xFrame.is() ) 361 { 362 Reference< XController > xController = m_xFrame->getController(); 363 if ( xController .is() ) 364 { 365 // try to obtain an XScriptInvocationContext interface, preferred from the 366 // mode, then from the controller 367 if ( !m_xScriptInvocation.set( xController->getModel(), UNO_QUERY ) ) 368 m_xScriptInvocation.set( xController, UNO_QUERY ); 369 } 370 else 371 { 372 Reference< XFrame > xFrame( m_xFrame.get(), UNO_QUERY ); 373 if ( xFrame.is() ) 374 { 375 SfxFrame* pFrame = NULL; 376 for ( pFrame = SfxFrame::GetFirst(); pFrame; pFrame = SfxFrame::GetNext( *pFrame ) ) 377 { 378 if ( pFrame->GetFrameInterface() == xFrame ) 379 break; 380 } 381 SfxObjectShell* pDocShell = pFrame ? pFrame->GetCurrentDocument() : SfxObjectShell::Current(); 382 if ( pDocShell ) 383 { 384 Reference< XModel > xModel( pDocShell->GetModel() ); 385 m_xScriptInvocation.set( xModel, UNO_QUERY ); 386 } 387 } 388 } 389 } 390 return m_xScriptInvocation.is(); 391 } 392 393 void ScriptProtocolHandler::createScriptProvider() 394 { 395 if ( m_xScriptProvider.is() ) 396 return; 397 398 try 399 { 400 // first, ask the component supporting the XScriptInvocationContext interface 401 // (if there is one) for a script provider 402 if ( getScriptInvocation() ) 403 { 404 Reference< XScriptProviderSupplier > xSPS( m_xScriptInvocation, UNO_QUERY ); 405 if ( xSPS.is() ) 406 m_xScriptProvider = xSPS->getScriptProvider(); 407 } 408 409 // second, ask the model in our frame 410 if ( !m_xScriptProvider.is() && m_xFrame.is() ) 411 { 412 Reference< XController > xController = m_xFrame->getController(); 413 if ( xController .is() ) 414 { 415 Reference< XScriptProviderSupplier > xSPS( xController->getModel(), UNO_QUERY ); 416 if ( xSPS.is() ) 417 m_xScriptProvider = xSPS->getScriptProvider(); 418 } 419 } 420 421 422 // as a fallback, ask the controller 423 if ( !m_xScriptProvider.is() && m_xFrame.is() ) 424 { 425 Reference< XScriptProviderSupplier > xSPS( m_xFrame->getController(), UNO_QUERY ); 426 if ( xSPS.is() ) 427 m_xScriptProvider = xSPS->getScriptProvider(); 428 } 429 430 // if nothing of this is successful, use the master script provider 431 if ( !m_xScriptProvider.is() ) 432 { 433 Reference< XPropertySet > xProps( m_xFactory, UNO_QUERY_THROW ); 434 435 ::rtl::OUString dc( 436 RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ); 437 438 Reference< XComponentContext > xCtx( 439 xProps->getPropertyValue( dc ), UNO_QUERY_THROW ); 440 441 ::rtl::OUString tmspf = ::rtl::OUString::createFromAscii( 442 "/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory"); 443 444 Reference< provider::XScriptProviderFactory > xFac( 445 xCtx->getValueByName( tmspf ), UNO_QUERY_THROW ); 446 447 Any aContext; 448 if ( getScriptInvocation() ) 449 aContext = makeAny( m_xScriptInvocation ); 450 m_xScriptProvider = Reference< provider::XScriptProvider > ( 451 xFac->createScriptProvider( aContext ), UNO_QUERY_THROW ); 452 } 453 } 454 catch ( RuntimeException & e ) 455 { 456 ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::createScriptProvider(), " ); 457 throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() ); 458 } 459 catch ( Exception & e ) 460 { 461 ::rtl::OUString temp = OUSTR( "ScriptProtocolHandler::createScriptProvider: " ); 462 throw RuntimeException( temp.concat( e.Message ), Reference< XInterface >() ); 463 } 464 } 465 466 ScriptProtocolHandler::ScriptProtocolHandler( 467 Reference< css::lang::XMultiServiceFactory > const& rFact ) : 468 m_bInitialised( false ), m_xFactory( rFact ) 469 { 470 } 471 472 ScriptProtocolHandler::~ScriptProtocolHandler() 473 { 474 } 475 476 /* XServiceInfo */ 477 ::rtl::OUString SAL_CALL ScriptProtocolHandler::getImplementationName( ) 478 throw( RuntimeException ) 479 { 480 return impl_getStaticImplementationName(); 481 } 482 483 /* XServiceInfo */ 484 sal_Bool SAL_CALL ScriptProtocolHandler::supportsService( 485 const ::rtl::OUString& sServiceName ) 486 throw( RuntimeException ) 487 { 488 Sequence< ::rtl::OUString > seqServiceNames = getSupportedServiceNames(); 489 const ::rtl::OUString* pArray = seqServiceNames.getConstArray(); 490 for ( sal_Int32 nCounter = 0; nCounter < seqServiceNames.getLength(); nCounter++ ) 491 { 492 if ( pArray[ nCounter ] == sServiceName ) 493 { 494 return sal_True ; 495 } 496 } 497 498 return sal_False ; 499 } 500 501 /* XServiceInfo */ 502 Sequence< ::rtl::OUString > SAL_CALL ScriptProtocolHandler::getSupportedServiceNames() 503 throw( RuntimeException ) 504 { 505 return impl_getStaticSupportedServiceNames(); 506 } 507 508 /* Helper for XServiceInfo */ 509 Sequence< ::rtl::OUString > ScriptProtocolHandler::impl_getStaticSupportedServiceNames() 510 { 511 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ); 512 Sequence< ::rtl::OUString > seqServiceNames( 1 ); 513 seqServiceNames.getArray() [ 0 ] = 514 ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYSERVICENAME ); 515 return seqServiceNames ; 516 } 517 518 /* Helper for XServiceInfo */ 519 ::rtl::OUString ScriptProtocolHandler::impl_getStaticImplementationName() 520 { 521 return ::rtl::OUString::createFromAscii( ::scripting_protocolhandler::MYIMPLNAME ); 522 } 523 524 /* Helper for registry */ 525 Reference< XInterface > SAL_CALL ScriptProtocolHandler::impl_createInstance( 526 const Reference< css::lang::XMultiServiceFactory >& xServiceManager ) 527 throw( RuntimeException ) 528 { 529 return Reference< XInterface > ( *new ScriptProtocolHandler( xServiceManager ) ); 530 } 531 532 /* Factory for registration */ 533 Reference< XSingleServiceFactory > ScriptProtocolHandler::impl_createFactory( 534 const Reference< XMultiServiceFactory >& xServiceManager ) 535 { 536 Reference< XSingleServiceFactory > xReturn ( 537 cppu::createSingleFactory( xServiceManager, 538 ScriptProtocolHandler::impl_getStaticImplementationName(), 539 ScriptProtocolHandler::impl_createInstance, 540 ScriptProtocolHandler::impl_getStaticSupportedServiceNames() ) 541 ); 542 return xReturn; 543 } 544 545 } // namespace scripting_protocolhandler 546 547 /* exported functions for registration */ 548 extern "C" 549 { 550 551 #undef css 552 #define css ::com::sun::star 553 554 void SAL_CALL component_getImplementationEnvironment( 555 const sal_Char** ppEnvironmentTypeName, uno_Environment** ppEnvironment ) 556 { 557 (void)ppEnvironment; 558 559 *ppEnvironmentTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME ; 560 } 561 562 void* SAL_CALL component_getFactory( const sal_Char * pImplementationName , 563 void * pServiceManager , 564 void * pRegistryKey ) 565 { 566 (void)pRegistryKey; 567 568 // Set default return value for this operation - if it failed. 569 void * pReturn = NULL ; 570 571 if ( 572 ( pImplementationName != NULL ) && 573 ( pServiceManager != NULL ) 574 ) 575 { 576 // Define variables which are used in following macros. 577 ::com::sun::star::uno::Reference< 578 ::com::sun::star::lang::XSingleServiceFactory > xFactory ; 579 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > 580 xServiceManager( reinterpret_cast< 581 ::com::sun::star::lang::XMultiServiceFactory* >( pServiceManager ) ) ; 582 583 if ( ::scripting_protocolhandler::ScriptProtocolHandler::impl_getStaticImplementationName().equals( 584 ::rtl::OUString::createFromAscii( pImplementationName ) ) ) 585 { 586 xFactory = ::scripting_protocolhandler::ScriptProtocolHandler::impl_createFactory( xServiceManager ); 587 } 588 589 // Factory is valid - service was found. 590 if ( xFactory.is() ) 591 { 592 xFactory->acquire(); 593 pReturn = xFactory.get(); 594 } 595 } 596 597 // Return with result of this operation. 598 return pReturn ; 599 } 600 } // extern "C" 601 602 603