From cc8aa69e2347e27ea3beb90be2f81b12c5a4c907 Mon Sep 17 00:00:00 2001 From: Radoslaw Buczkowski Date: Fri, 28 Aug 2020 13:20:04 +0200 Subject: [PATCH] Reduce scope of impact of InterfaceManager singleton --- .../templates/IPCAdapter.template.cpp | 4 +- .../facelift/templates/IPCProxy.template.cpp | 3 +- src/ipc/ipc-common/CMakeLists.txt | 1 + src/ipc/ipc-common/IPCProxy.h | 5 +- src/ipc/ipc-common/IPCServiceAdapter.h | 4 +- src/ipc/ipc-common/InterfaceManager.h | 22 +++---- .../ipc-common/InterfaceManagerInterface.h | 57 +++++++++++++++++++ src/ipc/ipc-common/LocalProviderBinder.h | 8 ++- .../ipc-common/LocalProviderBinderBase.cpp | 4 +- src/ipc/ipc-common/LocalProviderBinderBase.h | 6 +- .../ipc-common/NewIPCServiceAdapterBase.cpp | 9 +-- src/ipc/ipc-common/NewIPCServiceAdapterBase.h | 4 +- 12 files changed, 100 insertions(+), 27 deletions(-) create mode 100644 src/ipc/ipc-common/InterfaceManagerInterface.h diff --git a/codegen/facelift/templates/IPCAdapter.template.cpp b/codegen/facelift/templates/IPCAdapter.template.cpp index 25cd3e7e..331dacf7 100644 --- a/codegen/facelift/templates/IPCAdapter.template.cpp +++ b/codegen/facelift/templates/IPCAdapter.template.cpp @@ -36,6 +36,7 @@ #include "{{module.fullyQualifiedPath}}/{{interfaceName}}IPCAdapter.h" #include "IPCServiceAdapterBase.h" #include +#include "InterfaceManager.h" #ifdef DBUS_IPC_ENABLED #include "{{module.fullyQualifiedPath}}/{{interfaceName}}IPCDBusAdapter.h" @@ -76,7 +77,8 @@ struct {{interfaceName}}IPCAdapter::Impl { }; -{{interfaceName}}IPCAdapter::{{interfaceName}}IPCAdapter(QObject* parent) : BaseType(parent) +{{interfaceName}}IPCAdapter::{{interfaceName}}IPCAdapter(QObject* parent) : + BaseType(facelift::InterfaceManager::instance(), parent) { } diff --git a/codegen/facelift/templates/IPCProxy.template.cpp b/codegen/facelift/templates/IPCProxy.template.cpp index 8b65aa4e..5d100338 100644 --- a/codegen/facelift/templates/IPCProxy.template.cpp +++ b/codegen/facelift/templates/IPCProxy.template.cpp @@ -35,6 +35,7 @@ #include "{{interfaceName}}IPCProxy.h" #include "{{interfaceName}}NotAvailableImpl.h" +#include "InterfaceManager.h" #ifdef DBUS_IPC_ENABLED #include "{{module.fullyQualifiedPath}}/{{interfaceName}}IPCDBusProxy.h" @@ -77,7 +78,7 @@ struct {{className}}::Impl { > m_proxies = {}; }; -{{className}}::{{className}}(QObject *parent) : BaseType(parent), +{{className}}::{{className}}(QObject *parent) : BaseType(facelift::InterfaceManager::instance(), parent), m_impl(std::make_unique()) { ipc()->setObjectPath(SINGLETON_OBJECT_PATH); diff --git a/src/ipc/ipc-common/CMakeLists.txt b/src/ipc/ipc-common/CMakeLists.txt index 1528847b..e85b58a6 100644 --- a/src/ipc/ipc-common/CMakeLists.txt +++ b/src/ipc/ipc-common/CMakeLists.txt @@ -29,6 +29,7 @@ facelift_add_library(FaceliftIPCCommonLib IPCProxy.h IPCTypeHandler.h InterfaceManager.h + InterfaceManagerInterface.h IPCProxyModelProperty.h IPCProxyBinderBase.h IPCAdapterModelPropertyHandler.h diff --git a/src/ipc/ipc-common/IPCProxy.h b/src/ipc/ipc-common/IPCProxy.h index e051cc09..37b8836b 100644 --- a/src/ipc/ipc-common/IPCProxy.h +++ b/src/ipc/ipc-common/IPCProxy.h @@ -34,6 +34,7 @@ #include "StaticArrayReference.h" #include "IPCProxyBinderBase.h" #include "LocalProviderBinder.h" +#include "InterfaceManagerInterface.h" #if defined(FaceliftIPCCommonLib_LIBRARY) # define FaceliftIPCCommonLib_EXPORT Q_DECL_EXPORT @@ -55,9 +56,9 @@ class IPCProxy : public WrapperType, public IPCProxyNewBase InterfaceType* proxy = nullptr; }; - IPCProxy(QObject *parent) : WrapperType(parent) + IPCProxy(InterfaceManagerInterface& interfaceManager, QObject *parent) : WrapperType(parent) , IPCProxyNewBase(*static_cast(this)) - , m_localProviderBinder(*this) + , m_localProviderBinder(interfaceManager, *this) { QObject::connect(ipc(), &IPCProxyBinderBase::complete, this, [this] () { m_localProviderBinder.init(); diff --git a/src/ipc/ipc-common/IPCServiceAdapter.h b/src/ipc/ipc-common/IPCServiceAdapter.h index 80868ff0..178fad10 100644 --- a/src/ipc/ipc-common/IPCServiceAdapter.h +++ b/src/ipc/ipc-common/IPCServiceAdapter.h @@ -41,6 +41,7 @@ namespace facelift { class IPCAdapterFactoryManager; +class InterfaceManagerInterface; template class IPCServiceAdapter : public NewIPCServiceAdapterBase @@ -49,7 +50,8 @@ class IPCServiceAdapter : public NewIPCServiceAdapterBase using TheServiceType = InterfaceType; using NewIPCServiceAdapterBase::registerService; - IPCServiceAdapter(QObject *parent) : NewIPCServiceAdapterBase(parent) + IPCServiceAdapter(InterfaceManagerInterface& interfaceManager, QObject *parent) : + NewIPCServiceAdapterBase(interfaceManager, parent) { setObjectPath(InterfaceType::SINGLETON_OBJECT_PATH); } diff --git a/src/ipc/ipc-common/InterfaceManager.h b/src/ipc/ipc-common/InterfaceManager.h index 28f3829e..c09cdaf6 100644 --- a/src/ipc/ipc-common/InterfaceManager.h +++ b/src/ipc/ipc-common/InterfaceManager.h @@ -32,6 +32,7 @@ #include #include #include "Registry.h" +#include "InterfaceManagerInterface.h" #if defined(FaceliftIPCCommonLib_LIBRARY) # define FaceliftIPCCommonLib_EXPORT Q_DECL_EXPORT @@ -41,33 +42,32 @@ namespace facelift { -class NewIPCServiceAdapterBase; class InterfaceBase; /** * This class maintains a registry of IPC services registered locally, which enables local proxies to get a direct reference to them */ -class FaceliftIPCCommonLib_EXPORT InterfaceManager : public QObject +class FaceliftIPCCommonLib_EXPORT InterfaceManager : public InterfaceManagerInterface { Q_OBJECT public: + InterfaceManager(const InterfaceManager&) = delete; + InterfaceManager(const InterfaceManager&&) = delete; + InterfaceManager& operator=(const InterfaceManager&) = delete; + InterfaceManager& operator=(const InterfaceManager&&) = delete; - InterfaceManager(); - - void registerAdapter(const QString &objectPath, NewIPCServiceAdapterBase *adapter); + void registerAdapter(const QString &objectPath, NewIPCServiceAdapterBase *adapter) override; - void unregisterAdapter(NewIPCServiceAdapterBase *adapter); + void unregisterAdapter(NewIPCServiceAdapterBase *adapter) override; - NewIPCServiceAdapterBase *getAdapter(const QString &objectPath); - - Q_SIGNAL void adapterUnavailable(QString objectPath, NewIPCServiceAdapterBase *adapter); + NewIPCServiceAdapterBase *getAdapter(const QString &objectPath) override; static InterfaceManager &instance(); static InterfaceBase * serviceMatches(const QString& objectPath, NewIPCServiceAdapterBase *adapter); - Registry>& content() + Registry>& content() override { return m_registry; } @@ -75,6 +75,8 @@ class FaceliftIPCCommonLib_EXPORT InterfaceManager : public QObject private: Registry> m_registry; + // singleton + InterfaceManager(); }; } diff --git a/src/ipc/ipc-common/InterfaceManagerInterface.h b/src/ipc/ipc-common/InterfaceManagerInterface.h new file mode 100644 index 00000000..ac4e88e7 --- /dev/null +++ b/src/ipc/ipc-common/InterfaceManagerInterface.h @@ -0,0 +1,57 @@ +/********************************************************************** +** +** Copyright (C) 2020 Luxoft Sweden AB +** +** This file is part of the FaceLift project +** +** Permission is hereby granted, free of charge, to any person +** obtaining a copy of this software and associated documentation files +** (the "Software"), to deal in the Software without restriction, +** including without limitation the rights to use, copy, modify, merge, +** publish, distribute, sublicense, and/or sell copies of the Software, +** and to permit persons to whom the Software is furnished to do so, +** subject to the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +** BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +** ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +** SOFTWARE. +** +** SPDX-License-Identifier: MIT +** +**********************************************************************/ +#pragma once + +#include +#include +#include "Registry.h" + +#if defined(FaceliftIPCCommonLib_LIBRARY) +# define FaceliftIPCCommonLib_EXPORT Q_DECL_EXPORT +#else +# define FaceliftIPCCommonLib_EXPORT Q_DECL_IMPORT +#endif + +namespace facelift { + +class NewIPCServiceAdapterBase; + +class FaceliftIPCCommonLib_EXPORT InterfaceManagerInterface : public QObject +{ + Q_OBJECT +public: + virtual void registerAdapter(const QString &objectPath, NewIPCServiceAdapterBase *adapter) = 0; + virtual void unregisterAdapter(NewIPCServiceAdapterBase *adapter) = 0; + virtual NewIPCServiceAdapterBase *getAdapter(const QString &objectPath) = 0; + virtual Registry>& content() = 0; + Q_SIGNAL void adapterUnavailable(QString objectPath, NewIPCServiceAdapterBase *adapter); +}; + +} // end namespace facelift diff --git a/src/ipc/ipc-common/LocalProviderBinder.h b/src/ipc/ipc-common/LocalProviderBinder.h index 0b32b570..c2824750 100644 --- a/src/ipc/ipc-common/LocalProviderBinder.h +++ b/src/ipc/ipc-common/LocalProviderBinder.h @@ -31,8 +31,9 @@ #include #include "LocalProviderBinderBase.h" -#include "InterfaceManager.h" #include "IPCProxyNewBase.h" +#include "InterfaceManagerInterface.h" +#include "NewIPCServiceAdapterBase.h" #if defined(FaceliftIPCCommonLib_LIBRARY) # define FaceliftIPCCommonLib_EXPORT Q_DECL_EXPORT @@ -47,7 +48,8 @@ class LocalProviderBinder : public LocalProviderBinderBase { public: - LocalProviderBinder(IPCProxyNewBase &proxy) : LocalProviderBinderBase(proxy) + LocalProviderBinder(InterfaceManagerInterface& interfaceManager, IPCProxyNewBase &proxy) : + LocalProviderBinderBase(interfaceManager, proxy) { } @@ -56,7 +58,7 @@ class LocalProviderBinder : public LocalProviderBinderBase auto adapter = m_interfaceManager.getAdapter(m_proxy.objectPath()); if (adapter) { - auto* service = m_interfaceManager.serviceMatches(m_proxy.objectPath(), adapter); + auto* service = (m_proxy.objectPath() == adapter->objectPath() ? adapter->service() : nullptr); if (service) { auto provider = qobject_cast(service); if (provider != m_provider) { diff --git a/src/ipc/ipc-common/LocalProviderBinderBase.cpp b/src/ipc/ipc-common/LocalProviderBinderBase.cpp index 8e89f600..3114d144 100644 --- a/src/ipc/ipc-common/LocalProviderBinderBase.cpp +++ b/src/ipc/ipc-common/LocalProviderBinderBase.cpp @@ -39,7 +39,9 @@ namespace facelift { -LocalProviderBinderBase::LocalProviderBinderBase(IPCProxyNewBase &proxy) : m_proxy(proxy) +LocalProviderBinderBase::LocalProviderBinderBase(InterfaceManagerInterface& interfaceManager, IPCProxyNewBase &proxy) : + m_proxy(proxy), + m_interfaceManager(interfaceManager) { } diff --git a/src/ipc/ipc-common/LocalProviderBinderBase.h b/src/ipc/ipc-common/LocalProviderBinderBase.h index 236714b3..20065fb9 100644 --- a/src/ipc/ipc-common/LocalProviderBinderBase.h +++ b/src/ipc/ipc-common/LocalProviderBinderBase.h @@ -30,7 +30,7 @@ #pragma once #include -#include "InterfaceManager.h" +#include "InterfaceManagerInterface.h" #include "IPCProxyNewBase.h" #if defined(FaceliftIPCCommonLib_LIBRARY) @@ -45,7 +45,7 @@ class LocalProviderBinderBase : public QObject { public: - LocalProviderBinderBase(IPCProxyNewBase &proxy); + LocalProviderBinderBase(InterfaceManagerInterface& interfaceManager, IPCProxyNewBase &proxy); virtual void checkLocalAdapterAvailability() = 0; @@ -53,7 +53,7 @@ class LocalProviderBinderBase : public QObject { protected: IPCProxyNewBase &m_proxy; - InterfaceManager &m_interfaceManager = InterfaceManager::instance(); + InterfaceManagerInterface &m_interfaceManager; }; } diff --git a/src/ipc/ipc-common/NewIPCServiceAdapterBase.cpp b/src/ipc/ipc-common/NewIPCServiceAdapterBase.cpp index 027c231c..d35c1697 100644 --- a/src/ipc/ipc-common/NewIPCServiceAdapterBase.cpp +++ b/src/ipc/ipc-common/NewIPCServiceAdapterBase.cpp @@ -30,11 +30,12 @@ #include "NewIPCServiceAdapterBase.h" #include "IPCServiceAdapterBase.h" -#include "InterfaceManager.h" namespace facelift { -NewIPCServiceAdapterBase::NewIPCServiceAdapterBase(QObject *parent) : QObject(parent) +NewIPCServiceAdapterBase::NewIPCServiceAdapterBase(InterfaceManagerInterface& interfaceManager, QObject *parent) : + QObject(parent), + m_interfaceManager(interfaceManager) { } @@ -45,12 +46,12 @@ NewIPCServiceAdapterBase::~NewIPCServiceAdapterBase() { void NewIPCServiceAdapterBase::registerLocalService() { - InterfaceManager::instance().registerAdapter(objectPath(), this); + m_interfaceManager.registerAdapter(objectPath(), this); } void NewIPCServiceAdapterBase::unregisterLocalService() { - InterfaceManager::instance().unregisterAdapter(this); + m_interfaceManager.unregisterAdapter(this); } void NewIPCServiceAdapterBase::registerService() diff --git a/src/ipc/ipc-common/NewIPCServiceAdapterBase.h b/src/ipc/ipc-common/NewIPCServiceAdapterBase.h index 50c26602..00bd55a4 100644 --- a/src/ipc/ipc-common/NewIPCServiceAdapterBase.h +++ b/src/ipc/ipc-common/NewIPCServiceAdapterBase.h @@ -34,6 +34,7 @@ #include "QMLAdapter.h" #include "StringConversionHandler.h" #include "span.h" +#include "InterfaceManagerInterface.h" #if defined(FaceliftIPCCommonLib_LIBRARY) # define FaceliftIPCCommonLib_EXPORT Q_DECL_EXPORT @@ -53,7 +54,7 @@ class FaceliftIPCCommonLib_EXPORT NewIPCServiceAdapterBase : public QObject Q_PROPERTY(QString objectPath READ objectPath WRITE setObjectPath) Q_PROPERTY(bool enabled READ enabled WRITE setEnabled) - NewIPCServiceAdapterBase(QObject *parent); + NewIPCServiceAdapterBase(InterfaceManagerInterface& interfaceManager, QObject *parent); ~NewIPCServiceAdapterBase(); @@ -137,6 +138,7 @@ class FaceliftIPCCommonLib_EXPORT NewIPCServiceAdapterBase : public QObject bool m_enabled = true; bool m_providerReady = false; bool m_registered = false; + InterfaceManagerInterface& m_interfaceManager; }; }