From b34e36146fbe1139255f0abf9f5df8e89e2ac024 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Tue, 21 Jul 2020 15:16:21 +0200 Subject: dtrans/generic: create instances with uno constructors See tdf#74608 for motivation. Change-Id: Id5c585f28cb8696f37513bd2a2112af8d278648c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/99161 Tested-by: Jenkins Reviewed-by: Noel Grandin --- dtrans/Library_dtrans.mk | 1 - dtrans/source/generic/clipboardmanager.cxx | 97 +++++++++++++++++------------ dtrans/source/generic/clipboardmanager.hxx | 6 -- dtrans/source/generic/dtrans.component | 8 ++- dtrans/source/generic/dtrans.cxx | 74 ---------------------- dtrans/source/generic/generic_clipboard.cxx | 18 +++--- dtrans/source/generic/generic_clipboard.hxx | 6 -- 7 files changed, 68 insertions(+), 142 deletions(-) delete mode 100644 dtrans/source/generic/dtrans.cxx (limited to 'dtrans') diff --git a/dtrans/Library_dtrans.mk b/dtrans/Library_dtrans.mk index a4f3413fc4f6..e70d7215436b 100644 --- a/dtrans/Library_dtrans.mk +++ b/dtrans/Library_dtrans.mk @@ -31,7 +31,6 @@ $(eval $(call gb_Library_use_libraries,dtrans,\ $(eval $(call gb_Library_add_exception_objects,dtrans,\ dtrans/source/generic/clipboardmanager \ - dtrans/source/generic/dtrans \ dtrans/source/generic/generic_clipboard \ )) diff --git a/dtrans/source/generic/clipboardmanager.cxx b/dtrans/source/generic/clipboardmanager.cxx index 0c5dd396fec9..8cea07171f78 100644 --- a/dtrans/source/generic/clipboardmanager.cxx +++ b/dtrans/source/generic/clipboardmanager.cxx @@ -22,8 +22,10 @@ #include #include #include +#include #include #include +#include using namespace com::sun::star::container; using namespace com::sun::star::datatransfer; @@ -36,6 +38,11 @@ using namespace std; using ::dtrans::ClipboardManager; +static osl::Mutex g_InstanceGuard; +static rtl::Reference g_Instance; +static bool g_Disposed = false; + + ClipboardManager::ClipboardManager(): WeakComponentImplHelper< XClipboardManager, XEventListener, XServiceInfo > (m_aMutex), m_aDefaultName(OUString("default")) @@ -48,7 +55,7 @@ ClipboardManager::~ClipboardManager() OUString SAL_CALL ClipboardManager::getImplementationName( ) { - return CLIPBOARDMANAGER_IMPLEMENTATION_NAME; + return "com.sun.star.comp.datatransfer.ClipboardManager"; } sal_Bool SAL_CALL ClipboardManager::supportsService( const OUString& ServiceName ) @@ -58,7 +65,7 @@ sal_Bool SAL_CALL ClipboardManager::supportsService( const OUString& ServiceName Sequence< OUString > SAL_CALL ClipboardManager::getSupportedServiceNames( ) { - return ClipboardManager_getSupportedServiceNames(); + return { "com.sun.star.datatransfer.clipboard.ClipboardManager" }; } Reference< XClipboard > SAL_CALL ClipboardManager::getClipboard( const OUString& aName ) @@ -139,43 +146,50 @@ Sequence< OUString > SAL_CALL ClipboardManager::listClipboardNames() void SAL_CALL ClipboardManager::dispose() { - ClearableMutexGuard aGuard( rBHelper.rMutex ); - if (!rBHelper.bDisposed && !rBHelper.bInDispose) { - rBHelper.bInDispose = true; - aGuard.clear(); - - // give everyone a chance to save his clipboard instance - EventObject aEvt(static_cast < XClipboardManager * > (this)); - rBHelper.aLC.disposeAndClear( aEvt ); - - // removeClipboard is still allowed here, so make a copy of the - // list (to ensure integrity) and clear the original. - ClearableMutexGuard aGuard2( rBHelper.rMutex ); - ClipboardMap aCopy(m_aClipboardMap); - m_aClipboardMap.clear(); - aGuard2.clear(); - - // dispose all clipboards still in list - for (auto const& elem : aCopy) + osl::MutexGuard aGuard(g_InstanceGuard); + g_Instance.clear(); + g_Disposed = true; + } + { + ClearableMutexGuard aGuard( rBHelper.rMutex ); + if (!rBHelper.bDisposed && !rBHelper.bInDispose) { - Reference< XComponent > xComponent(elem.second, UNO_QUERY); - if (xComponent.is()) + rBHelper.bInDispose = true; + aGuard.clear(); + + // give everyone a chance to save his clipboard instance + EventObject aEvt(static_cast < XClipboardManager * > (this)); + rBHelper.aLC.disposeAndClear( aEvt ); + + // removeClipboard is still allowed here, so make a copy of the + // list (to ensure integrity) and clear the original. + ClearableMutexGuard aGuard2( rBHelper.rMutex ); + ClipboardMap aCopy(m_aClipboardMap); + m_aClipboardMap.clear(); + aGuard2.clear(); + + // dispose all clipboards still in list + for (auto const& elem : aCopy) { - try - { - xComponent->removeEventListener(static_cast < XEventListener * > (this)); - xComponent->dispose(); - } - catch (const Exception&) + Reference< XComponent > xComponent(elem.second, UNO_QUERY); + if (xComponent.is()) { - // exceptions can be safely ignored here. + try + { + xComponent->removeEventListener(static_cast < XEventListener * > (this)); + xComponent->dispose(); + } + catch (const Exception&) + { + // exceptions can be safely ignored here. + } } } - } - rBHelper.bDisposed = true; - rBHelper.bInDispose = false; + rBHelper.bDisposed = true; + rBHelper.bInDispose = false; + } } } @@ -187,16 +201,17 @@ void SAL_CALL ClipboardManager::disposing( const EventObject& event ) removeClipboard(xClipboard->getName()); } -Reference< XInterface > ClipboardManager_createInstance( - const Reference< XMultiServiceFactory > & /*xMultiServiceFactory*/) -{ - return Reference < XInterface >(static_cast(new ClipboardManager())); -} - -Sequence< OUString > ClipboardManager_getSupportedServiceNames() +extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* +dtrans_ClipboardManager_get_implementation( + css::uno::XComponentContext* , css::uno::Sequence const&) { - Sequence < OUString > SupportedServicesNames { "com.sun.star.datatransfer.clipboard.ClipboardManager" }; - return SupportedServicesNames; + osl::MutexGuard aGuard(g_InstanceGuard); + if (g_Disposed) + return nullptr; + if (!g_Instance) + g_Instance.set(new ClipboardManager()); + g_Instance->acquire(); + return static_cast(g_Instance.get()); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/dtrans/source/generic/clipboardmanager.hxx b/dtrans/source/generic/clipboardmanager.hxx index e3217f6bf6bb..2750b61246cb 100644 --- a/dtrans/source/generic/clipboardmanager.hxx +++ b/dtrans/source/generic/clipboardmanager.hxx @@ -28,8 +28,6 @@ #include -#define CLIPBOARDMANAGER_IMPLEMENTATION_NAME "com.sun.star.comp.datatransfer.ClipboardManager" - typedef std::map< OUString, css::uno::Reference< css::datatransfer::clipboard::XClipboard > > ClipboardMap; namespace dtrans @@ -90,10 +88,6 @@ namespace dtrans } -css::uno::Sequence< OUString > ClipboardManager_getSupportedServiceNames(); -css::uno::Reference< css::uno::XInterface > ClipboardManager_createInstance( - const css::uno::Reference< css::lang::XMultiServiceFactory > & xMultiServiceFactory); - #endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/dtrans/source/generic/dtrans.component b/dtrans/source/generic/dtrans.component index 915e3f0bcf8f..1bfb643be4d0 100644 --- a/dtrans/source/generic/dtrans.component +++ b/dtrans/source/generic/dtrans.component @@ -18,11 +18,13 @@ --> - + xmlns="http://openoffice.org/2010/uno-components"> + - + diff --git a/dtrans/source/generic/dtrans.cxx b/dtrans/source/generic/dtrans.cxx deleted file mode 100644 index 0630b9a6f538..000000000000 --- a/dtrans/source/generic/dtrans.cxx +++ /dev/null @@ -1,74 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - -#include -#include -#include "clipboardmanager.hxx" -#include "generic_clipboard.hxx" - -using namespace com::sun::star::lang; -using namespace com::sun::star::registry; -using namespace com::sun::star::uno; -using namespace cppu; - -extern "C" -{ - -SAL_DLLPUBLIC_EXPORT void * dtrans_component_getFactory( - const char * pImplName, - void * pServiceManager, - void * /*pRegistryKey*/ -) -{ - void * pRet = nullptr; - - if (pServiceManager) - { - Reference< XSingleServiceFactory > xFactory; - - if (rtl_str_compare( pImplName, CLIPBOARDMANAGER_IMPLEMENTATION_NAME ) == 0) - { - xFactory = createOneInstanceFactory( - static_cast< XMultiServiceFactory * >( pServiceManager ), - OUString::createFromAscii( pImplName ), - ClipboardManager_createInstance, - ClipboardManager_getSupportedServiceNames() ); - } - else if (rtl_str_compare( pImplName, GENERIC_CLIPBOARD_IMPLEMENTATION_NAME ) == 0) - { - xFactory = createSingleFactory( - static_cast< XMultiServiceFactory * >( pServiceManager ), - OUString::createFromAscii( pImplName ), - GenericClipboard_createInstance, - GenericClipboard_getSupportedServiceNames() ); - } - - if (xFactory.is()) - { - xFactory->acquire(); - pRet = xFactory.get(); - } - } - - return pRet; -} - -} - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/dtrans/source/generic/generic_clipboard.cxx b/dtrans/source/generic/generic_clipboard.cxx index 4f358db37336..0394353601c8 100644 --- a/dtrans/source/generic/generic_clipboard.cxx +++ b/dtrans/source/generic/generic_clipboard.cxx @@ -20,6 +20,7 @@ #include "generic_clipboard.hxx" #include #include +#include #include using namespace com::sun::star::datatransfer; @@ -56,7 +57,7 @@ void SAL_CALL GenericClipboard::initialize( const Sequence< Any >& aArguments ) OUString SAL_CALL GenericClipboard::getImplementationName( ) { - return GENERIC_CLIPBOARD_IMPLEMENTATION_NAME; + return "com.sun.star.comp.datatransfer.clipboard.GenericClipboard"; } sal_Bool SAL_CALL GenericClipboard::supportsService( const OUString& ServiceName ) @@ -66,7 +67,7 @@ sal_Bool SAL_CALL GenericClipboard::supportsService( const OUString& ServiceName Sequence< OUString > SAL_CALL GenericClipboard::getSupportedServiceNames( ) { - return GenericClipboard_getSupportedServiceNames(); + return { "com.sun.star.datatransfer.clipboard.GenericClipboard" }; } Reference< XTransferable > SAL_CALL GenericClipboard::getContents() @@ -137,16 +138,11 @@ void SAL_CALL GenericClipboard::removeClipboardListener( const Reference< XClipb rBHelper.aLC.removeInterface( cppu::UnoType::get(), listener ); } -Sequence< OUString > GenericClipboard_getSupportedServiceNames() +extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface* +dtrans_GenericClipboard_get_implementation( + css::uno::XComponentContext* , css::uno::Sequence const&) { - Sequence< OUString > aRet { "com.sun.star.datatransfer.clipboard.GenericClipboard" }; - return aRet; -} - -Reference< XInterface > GenericClipboard_createInstance( - const Reference< XMultiServiceFactory > & /*xMultiServiceFactory*/) -{ - return Reference < XInterface >(static_cast(new GenericClipboard())); + return cppu::acquire(static_cast(new GenericClipboard())); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/dtrans/source/generic/generic_clipboard.hxx b/dtrans/source/generic/generic_clipboard.hxx index 2b31d063229c..f23a0cc9a120 100644 --- a/dtrans/source/generic/generic_clipboard.hxx +++ b/dtrans/source/generic/generic_clipboard.hxx @@ -29,8 +29,6 @@ #include #include -#define GENERIC_CLIPBOARD_IMPLEMENTATION_NAME "com.sun.star.comp.datatransfer.clipboard.GenericClipboard" - namespace dtrans { @@ -101,10 +99,6 @@ namespace dtrans } -css::uno::Sequence< OUString > GenericClipboard_getSupportedServiceNames(); -css::uno::Reference< css::uno::XInterface > GenericClipboard_createInstance( - const css::uno::Reference< css::lang::XMultiServiceFactory > & xMultiServiceFactory); - #endif /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit