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