diff options
Diffstat (limited to 'cppuhelper/source/factory.cxx')
-rw-r--r-- | cppuhelper/source/factory.cxx | 369 |
1 files changed, 132 insertions, 237 deletions
diff --git a/cppuhelper/source/factory.cxx b/cppuhelper/source/factory.cxx index 8768d9817ab7..d039e43824a8 100644 --- a/cppuhelper/source/factory.cxx +++ b/cppuhelper/source/factory.cxx @@ -20,8 +20,9 @@ #include <sal/log.hxx> #include <osl/diagnose.h> #include <osl/mutex.hxx> +#include <cppuhelper/basemutex.hxx> #include <cppuhelper/weak.hxx> -#include <cppuhelper/component.hxx> +#include <cppuhelper/compbase.hxx> #include <cppuhelper/factory.hxx> #include <cppuhelper/implbase.hxx> #include <cppuhelper/queryinterface.hxx> @@ -29,6 +30,7 @@ #include <rtl/unload.h> #include <cppuhelper/propshlp.hxx> +#include <o3tl/string_view.hxx> #include <com/sun/star/lang/XServiceInfo.hpp> #include <com/sun/star/lang/XSingleServiceFactory.hpp> @@ -42,6 +44,7 @@ #include <com/sun/star/beans/PropertyAttribute.hpp> #include <memory> +#include <utility> using namespace osl; @@ -56,36 +59,36 @@ namespace cppu namespace { -class OSingleFactoryHelper - : public XServiceInfo - , public XSingleServiceFactory - , public lang::XSingleComponentFactory - , public XUnloadingPreference +class OFactoryComponentHelper + : public cppu::BaseMutex + , public WeakComponentImplHelper< + XServiceInfo, + XSingleServiceFactory, + lang::XSingleComponentFactory, + XUnloadingPreference> { public: - OSingleFactoryHelper( + OFactoryComponentHelper( const Reference<XMultiServiceFactory > & rServiceManager, - const OUString & rImplementationName_, + OUString aImplementationName_, ComponentInstantiation pCreateFunction_, ComponentFactoryFunc fptr, - const Sequence< OUString > * pServiceNames_ ) - : xSMgr( rServiceManager ) + const Sequence< OUString > * pServiceNames_, + bool bOneInstance_ ) + : WeakComponentImplHelper( m_aMutex ) + , bOneInstance( bOneInstance_ ) + , xSMgr( rServiceManager ) , pCreateFunction( pCreateFunction_ ) , m_fptr( fptr ) - , aImplementationName( rImplementationName_ ) + , aImplementationName(std::move( aImplementationName_ )) { if( pServiceNames_ ) aServiceNames = *pServiceNames_; } - virtual ~OSingleFactoryHelper(); - - // XInterface - Any SAL_CALL queryInterface( const Type & rType ) override; - // XSingleServiceFactory Reference<XInterface > SAL_CALL createInstance() override; - virtual Reference<XInterface > SAL_CALL createInstanceWithArguments(const Sequence<Any>& Arguments) override; + Reference<XInterface > SAL_CALL createInstanceWithArguments( const Sequence<Any>& Arguments ) override; // XSingleComponentFactory virtual Reference< XInterface > SAL_CALL createInstanceWithContext( Reference< XComponentContext > const & xContext ) override; @@ -98,7 +101,27 @@ public: sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override; Sequence< OUString > SAL_CALL getSupportedServiceNames() override; + // XTypeProvider + virtual Sequence< Type > SAL_CALL getTypes() override; + + // XUnloadingPreference + virtual sal_Bool SAL_CALL releaseOnNotification() override; + + // WeakComponentImplHelper + void SAL_CALL disposing() override; + +private: + css::uno::Reference<css::uno::XInterface> createInstanceWithArgumentsEveryTime( + css::uno::Sequence<css::uno::Any> const & rArguments, + css::uno::Reference<css::uno::XComponentContext> const & xContext); + + Reference<XInterface > xTheInstance; + bool bOneInstance; protected: + // needed for implementing XUnloadingPreference in inheriting classes + bool isOneInstance() const {return bOneInstance;} + bool isInstance() const {return xTheInstance.is();} + /** * Create an instance specified by the factory. The one instance logic is implemented * in the createInstance and createInstanceWithArguments methods. @@ -118,23 +141,22 @@ protected: } -OSingleFactoryHelper::~OSingleFactoryHelper() +// XTypeProvider +Sequence< Type > OFactoryComponentHelper::getTypes() { -} + Type ar[ 4 ]; + ar[ 0 ] = cppu::UnoType<XSingleServiceFactory>::get(); + ar[ 1 ] = cppu::UnoType<XServiceInfo>::get(); + ar[ 2 ] = cppu::UnoType<XUnloadingPreference>::get(); + if (m_fptr) + ar[ 3 ] = cppu::UnoType<XSingleComponentFactory>::get(); -Any OSingleFactoryHelper::queryInterface( const Type & rType ) -{ - return ::cppu::queryInterface( - rType, - static_cast< XSingleComponentFactory * >( this ), - static_cast< XSingleServiceFactory * >( this ), - static_cast< XServiceInfo * >( this ) , - static_cast< XUnloadingPreference * >( this )); + return Sequence< Type >( ar, m_fptr ? 4 : 3 ); } -// OSingleFactoryHelper -Reference<XInterface > OSingleFactoryHelper::createInstanceEveryTime( +// OFactoryComponentHelper +Reference<XInterface > OFactoryComponentHelper::createInstanceEveryTime( Reference< XComponentContext > const & xContext ) { if (m_fptr) @@ -156,195 +178,19 @@ Reference<XInterface > OSingleFactoryHelper::createInstanceEveryTime( } // XSingleServiceFactory -Reference<XInterface > OSingleFactoryHelper::createInstance() -{ - return createInstanceWithContext( Reference< XComponentContext >() ); -} - -// XSingleServiceFactory -Reference<XInterface > OSingleFactoryHelper::createInstanceWithArguments( - const Sequence<Any>& Arguments ) -{ - return createInstanceWithArgumentsAndContext( - Arguments, Reference< XComponentContext >() ); -} - -// XSingleComponentFactory - -Reference< XInterface > OSingleFactoryHelper::createInstanceWithContext( - Reference< XComponentContext > const & xContext ) -{ - return createInstanceEveryTime( xContext ); -} - -Reference< XInterface > OSingleFactoryHelper::createInstanceWithArgumentsAndContext( - Sequence< Any > const & rArguments, - Reference< XComponentContext > const & xContext ) -{ - Reference< XInterface > xRet( createInstanceWithContext( xContext ) ); - - Reference< lang::XInitialization > xInit( xRet, UNO_QUERY ); - // always call initialize, even if there are no arguments. #i63511# - if (xInit.is()) - { - xInit->initialize( rArguments ); - } - else - { - if ( rArguments.hasElements() ) - { - // dispose the here created UNO object before throwing out exception - // to avoid risk of memory leaks #i113722# - Reference<XComponent> xComp( xRet, UNO_QUERY ); - if (xComp.is()) - xComp->dispose(); - - throw lang::IllegalArgumentException( - "cannot pass arguments to component => no XInitialization implemented!", - Reference< XInterface >(), 0 ); - } - } - - return xRet; -} - -// XServiceInfo -OUString OSingleFactoryHelper::getImplementationName() -{ - return aImplementationName; -} - -// XServiceInfo -sal_Bool OSingleFactoryHelper::supportsService( - const OUString& ServiceName ) -{ - return cppu::supportsService(this, ServiceName); -} - -// XServiceInfo -Sequence< OUString > OSingleFactoryHelper::getSupportedServiceNames() -{ - return aServiceNames; -} - -namespace { - -struct OFactoryComponentHelper_Mutex -{ - Mutex aMutex; -}; - -class OFactoryComponentHelper - : public OFactoryComponentHelper_Mutex - , public OComponentHelper - , public OSingleFactoryHelper -{ -public: - OFactoryComponentHelper( - const Reference<XMultiServiceFactory > & rServiceManager, - const OUString & rImplementationName_, - ComponentInstantiation pCreateFunction_, - ComponentFactoryFunc fptr, - const Sequence< OUString > * pServiceNames_, - bool bOneInstance_ ) - : OComponentHelper( aMutex ) - , OSingleFactoryHelper( rServiceManager, rImplementationName_, pCreateFunction_, fptr, pServiceNames_ ) - , bOneInstance( bOneInstance_ ) - { - } - - // XInterface - Any SAL_CALL queryInterface( const Type & rType ) override; - void SAL_CALL acquire() noexcept override - { OComponentHelper::acquire(); } - void SAL_CALL release() noexcept override - { OComponentHelper::release(); } - - // XSingleServiceFactory - Reference<XInterface > SAL_CALL createInstance() override; - Reference<XInterface > SAL_CALL createInstanceWithArguments( const Sequence<Any>& Arguments ) override; - // XSingleComponentFactory - virtual Reference< XInterface > SAL_CALL createInstanceWithContext( - Reference< XComponentContext > const & xContext ) override; - virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext( - Sequence< Any > const & rArguments, - Reference< XComponentContext > const & xContext ) override; - - // XTypeProvider - virtual Sequence< Type > SAL_CALL getTypes() override; - virtual Sequence< sal_Int8 > SAL_CALL getImplementationId() override; - - // XAggregation - Any SAL_CALL queryAggregation( const Type & rType ) override; - - // XUnloadingPreference - virtual sal_Bool SAL_CALL releaseOnNotification() override; - - // OComponentHelper - void SAL_CALL dispose() override; - -private: - Reference<XInterface > xTheInstance; - bool bOneInstance; -protected: - // needed for implementing XUnloadingPreference in inheriting classes - bool isOneInstance() const {return bOneInstance;} - bool isInstance() const {return xTheInstance.is();} -}; - -} - -Any SAL_CALL OFactoryComponentHelper::queryInterface( const Type & rType ) -{ - if( rType == cppu::UnoType<XUnloadingPreference>::get() ) - { - return Any( - Reference< XUnloadingPreference >( - static_cast< XUnloadingPreference * >(this) ) ); - } - return OComponentHelper::queryInterface( rType ); -} - -// XAggregation -Any OFactoryComponentHelper::queryAggregation( const Type & rType ) -{ - Any aRet( OComponentHelper::queryAggregation( rType ) ); - return (aRet.hasValue() ? aRet : OSingleFactoryHelper::queryInterface( rType )); -} - -// XTypeProvider -Sequence< Type > OFactoryComponentHelper::getTypes() -{ - Type ar[ 4 ]; - ar[ 0 ] = cppu::UnoType<XSingleServiceFactory>::get(); - ar[ 1 ] = cppu::UnoType<XServiceInfo>::get(); - ar[ 2 ] = cppu::UnoType<XUnloadingPreference>::get(); - - if (m_fptr) - ar[ 3 ] = cppu::UnoType<XSingleComponentFactory>::get(); - - return Sequence< Type >( ar, m_fptr ? 4 : 3 ); -} - -Sequence< sal_Int8 > OFactoryComponentHelper::getImplementationId() -{ - return css::uno::Sequence<sal_Int8>(); -} - -// XSingleServiceFactory Reference<XInterface > OFactoryComponentHelper::createInstance() { if( bOneInstance ) { if( !xTheInstance.is() ) { - MutexGuard aGuard( aMutex ); + MutexGuard aGuard( m_aMutex ); if( !xTheInstance.is() ) - xTheInstance = OSingleFactoryHelper::createInstance(); + xTheInstance = createInstanceEveryTime( Reference< XComponentContext >() ); } return xTheInstance; } - return OSingleFactoryHelper::createInstance(); + return createInstanceEveryTime( Reference< XComponentContext >() ); } Reference<XInterface > OFactoryComponentHelper::createInstanceWithArguments( @@ -354,14 +200,15 @@ Reference<XInterface > OFactoryComponentHelper::createInstanceWithArguments( { if( !xTheInstance.is() ) { - MutexGuard aGuard( aMutex ); + MutexGuard aGuard( m_aMutex ); // OSL_ENSURE( !xTheInstance.is(), "### arguments will be ignored!" ); if( !xTheInstance.is() ) - xTheInstance = OSingleFactoryHelper::createInstanceWithArguments( Arguments ); + xTheInstance = createInstanceWithArgumentsEveryTime( + Arguments, Reference< XComponentContext >() ); } return xTheInstance; } - return OSingleFactoryHelper::createInstanceWithArguments( Arguments ); + return createInstanceWithArgumentsEveryTime( Arguments, Reference< XComponentContext >() ); } // XSingleComponentFactory @@ -373,14 +220,14 @@ Reference< XInterface > OFactoryComponentHelper::createInstanceWithContext( { if( !xTheInstance.is() ) { - MutexGuard aGuard( aMutex ); + MutexGuard aGuard( m_aMutex ); // OSL_ENSURE( !xTheInstance.is(), "### context will be ignored!" ); if( !xTheInstance.is() ) - xTheInstance = OSingleFactoryHelper::createInstanceWithContext( xContext ); + xTheInstance = createInstanceEveryTime( xContext ); } return xTheInstance; } - return OSingleFactoryHelper::createInstanceWithContext( xContext ); + return createInstanceEveryTime( xContext ); } Reference< XInterface > OFactoryComponentHelper::createInstanceWithArgumentsAndContext( @@ -391,26 +238,56 @@ Reference< XInterface > OFactoryComponentHelper::createInstanceWithArgumentsAndC { if( !xTheInstance.is() ) { - MutexGuard aGuard( aMutex ); + MutexGuard aGuard( m_aMutex ); // OSL_ENSURE( !xTheInstance.is(), "### context and arguments will be ignored!" ); if( !xTheInstance.is() ) - xTheInstance = OSingleFactoryHelper::createInstanceWithArgumentsAndContext( rArguments, xContext ); + xTheInstance = createInstanceWithArgumentsEveryTime( rArguments, xContext ); } return xTheInstance; } - return OSingleFactoryHelper::createInstanceWithArgumentsAndContext( rArguments, xContext ); + return createInstanceWithArgumentsEveryTime( rArguments, xContext ); } - -// OComponentHelper -void OFactoryComponentHelper::dispose() +css::uno::Reference<css::uno::XInterface> +OFactoryComponentHelper::createInstanceWithArgumentsEveryTime( + css::uno::Sequence<css::uno::Any> const & rArguments, + css::uno::Reference<css::uno::XComponentContext> const & xContext) { - OComponentHelper::dispose(); + Reference< XInterface > xRet( createInstanceEveryTime( xContext ) ); + + Reference< lang::XInitialization > xInit( xRet, UNO_QUERY ); + // always call initialize, even if there are no arguments. #i63511# + if (xInit.is()) + { + xInit->initialize( rArguments ); + } + else + { + if ( rArguments.hasElements() ) + { + // dispose the here created UNO object before throwing out exception + // to avoid risk of memory leaks #i113722# + Reference<XComponent> xComp( xRet, UNO_QUERY ); + if (xComp.is()) + xComp->dispose(); + throw lang::IllegalArgumentException( + "cannot pass arguments to component => no XInitialization implemented!", + Reference< XInterface >(), 0 ); + } + } + + return xRet; +} + + +// WeakComponentImplHelper +void OFactoryComponentHelper::disposing() +{ Reference<XInterface > x; { // do not delete in the guard section - MutexGuard aGuard( aMutex ); + MutexGuard aGuard( m_aMutex ); x = xTheInstance; xTheInstance.clear(); } @@ -420,6 +297,25 @@ void OFactoryComponentHelper::dispose() xComp->dispose(); } +// XServiceInfo +OUString OFactoryComponentHelper::getImplementationName() +{ + return aImplementationName; +} + +// XServiceInfo +sal_Bool OFactoryComponentHelper::supportsService( + const OUString& ServiceName ) +{ + return cppu::supportsService(this, ServiceName); +} + +// XServiceInfo +Sequence< OUString > OFactoryComponentHelper::getSupportedServiceNames() +{ + return aServiceNames; +} + // XUnloadingPreference // This class is used for single factories, component factories and // one-instance factories. Depending on the usage this function has @@ -448,7 +344,7 @@ public: bool bOneInstance_ ) : OFactoryComponentHelper( rServiceManager, rImplementationName_, nullptr, nullptr, nullptr, bOneInstance_ ), - OPropertySetHelper( OComponentHelper::rBHelper ), + OPropertySetHelper( WeakComponentImplHelper::rBHelper ), xImplementationKey( xImplementationKey_ ) {} @@ -472,7 +368,7 @@ public: virtual void SAL_CALL getFastPropertyValue( Any & rValue, sal_Int32 nHandle ) const override; - // OSingleFactoryHelper + // OFactoryComponentHelper Reference<XInterface > createInstanceEveryTime( Reference< XComponentContext > const & xContext ) override; @@ -549,7 +445,7 @@ Sequence< Type > ORegistryFactoryHelper::getTypes() Reference< beans::XPropertySetInfo > ORegistryFactoryHelper::getPropertySetInfo() { - ::osl::MutexGuard guard( aMutex ); + ::osl::MutexGuard guard( m_aMutex ); if (! m_xInfo.is()) m_xInfo = createPropertySetInfo( getInfoHelper() ); return m_xInfo; @@ -559,7 +455,7 @@ ORegistryFactoryHelper::getPropertySetInfo() IPropertyArrayHelper & ORegistryFactoryHelper::getInfoHelper() { - ::osl::MutexGuard guard( aMutex ); + ::osl::MutexGuard guard( m_aMutex ); if (m_property_array_helper == nullptr) { beans::Property prop( @@ -616,7 +512,7 @@ Reference<XInterface > ORegistryFactoryHelper::createInstanceEveryTime( Reference< XInterface > x( createModuleFactory() ); if (x.is()) { - MutexGuard aGuard( aMutex ); + MutexGuard aGuard( m_aMutex ); if( !xModuleFactory.is() && !xModuleFactoryDepr.is() ) { xModuleFactory.set( x, UNO_QUERY ); @@ -644,7 +540,7 @@ Reference<XInterface > SAL_CALL ORegistryFactoryHelper::createInstanceWithArgume Reference< XInterface > x( createModuleFactory() ); if (x.is()) { - MutexGuard aGuard( aMutex ); + MutexGuard aGuard( m_aMutex ); if( !xModuleFactory.is() && !xModuleFactoryDepr.is() ) { xModuleFactory.set( x, UNO_QUERY ); @@ -674,7 +570,7 @@ Reference< XInterface > ORegistryFactoryHelper::createInstanceWithArgumentsAndCo Reference< XInterface > x( createModuleFactory() ); if (x.is()) { - MutexGuard aGuard( aMutex ); + MutexGuard aGuard( m_aMutex ); if( !xModuleFactory.is() && !xModuleFactoryDepr.is() ) { xModuleFactory.set( x, UNO_QUERY ); @@ -696,7 +592,6 @@ Reference< XInterface > ORegistryFactoryHelper::createInstanceWithArgumentsAndCo } -// OSingleFactoryHelper Reference< XInterface > ORegistryFactoryHelper::createModuleFactory() { OUString aActivatorUrl; @@ -709,7 +604,7 @@ Reference< XInterface > ORegistryFactoryHelper::createModuleFactory() { aActivatorUrl = xActivatorKey->getAsciiValue(); - aActivatorName = aActivatorUrl.getToken(0, ':'); + aActivatorName = o3tl::getToken(aActivatorUrl, 0, ':'); Reference<XRegistryKey > xLocationKey = xImplementationKey->openKey( "/UNO/LOCATION" ); @@ -732,11 +627,11 @@ Reference< XInterface > ORegistryFactoryHelper::createModuleFactory() sal_Int32 nPos = aLocation.indexOf("://"); if( nPos != -1 ) { - aActivatorName = aLocation.copy( 0, nPos ); - if( aActivatorName == "java" ) - aActivatorName = "com.sun.star.loader.Java"; - else if( aActivatorName == "module" ) - aActivatorName = "com.sun.star.loader.SharedLibrary"; + aActivatorName = aLocation.subView( 0, nPos ); + if( aActivatorName == u"java" ) + aActivatorName = u"com.sun.star.loader.Java"_ustr; + else if( aActivatorName == u"module" ) + aActivatorName = u"com.sun.star.loader.SharedLibrary"_ustr; aLocation = aLocation.copy( nPos + 3 ); } } @@ -758,7 +653,7 @@ Reference< XInterface > ORegistryFactoryHelper::createModuleFactory() // XServiceInfo Sequence< OUString > ORegistryFactoryHelper::getSupportedServiceNames() { - MutexGuard aGuard( aMutex ); + MutexGuard aGuard( m_aMutex ); if( !aServiceNames.hasElements() ) { // not yet loaded |