Skip to content

Commit

Permalink
Reduce scope of impact of InterfaceManager singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
Radoslaw Buczkowski authored and bitmouse committed Sep 1, 2020
1 parent 7dff83f commit cc8aa69
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 27 deletions.
4 changes: 3 additions & 1 deletion codegen/facelift/templates/IPCAdapter.template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "{{module.fullyQualifiedPath}}/{{interfaceName}}IPCAdapter.h"
#include "IPCServiceAdapterBase.h"
#include <array>
#include "InterfaceManager.h"

#ifdef DBUS_IPC_ENABLED
#include "{{module.fullyQualifiedPath}}/{{interfaceName}}IPCDBusAdapter.h"
Expand Down Expand Up @@ -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)
{
}

Expand Down
3 changes: 2 additions & 1 deletion codegen/facelift/templates/IPCProxy.template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<Impl>())
{
ipc()->setObjectPath(SINGLETON_OBJECT_PATH);
Expand Down
1 change: 1 addition & 0 deletions src/ipc/ipc-common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ facelift_add_library(FaceliftIPCCommonLib
IPCProxy.h
IPCTypeHandler.h
InterfaceManager.h
InterfaceManagerInterface.h
IPCProxyModelProperty.h
IPCProxyBinderBase.h
IPCAdapterModelPropertyHandler.h
Expand Down
5 changes: 3 additions & 2 deletions src/ipc/ipc-common/IPCProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<InterfaceBase *>(this))
, m_localProviderBinder(*this)
, m_localProviderBinder(interfaceManager, *this)
{
QObject::connect(ipc(), &IPCProxyBinderBase::complete, this, [this] () {
m_localProviderBinder.init();
Expand Down
4 changes: 3 additions & 1 deletion src/ipc/ipc-common/IPCServiceAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
namespace facelift {

class IPCAdapterFactoryManager;
class InterfaceManagerInterface;

template<typename InterfaceType>
class IPCServiceAdapter : public NewIPCServiceAdapterBase
Expand All @@ -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);
}
Expand Down
22 changes: 12 additions & 10 deletions src/ipc/ipc-common/InterfaceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <QObject>
#include <QPointer>
#include "Registry.h"
#include "InterfaceManagerInterface.h"

#if defined(FaceliftIPCCommonLib_LIBRARY)
# define FaceliftIPCCommonLib_EXPORT Q_DECL_EXPORT
Expand All @@ -41,40 +42,41 @@

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<QPointer<NewIPCServiceAdapterBase>>& content()
Registry<QPointer<NewIPCServiceAdapterBase>>& content() override
{
return m_registry;
}

private:
Registry<QPointer<NewIPCServiceAdapterBase>> m_registry;

// singleton
InterfaceManager();
};

}
57 changes: 57 additions & 0 deletions src/ipc/ipc-common/InterfaceManagerInterface.h
Original file line number Diff line number Diff line change
@@ -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 <QObject>
#include <QString>
#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<QPointer<NewIPCServiceAdapterBase>>& content() = 0;
Q_SIGNAL void adapterUnavailable(QString objectPath, NewIPCServiceAdapterBase *adapter);
};

} // end namespace facelift
8 changes: 5 additions & 3 deletions src/ipc/ipc-common/LocalProviderBinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@

#include <QObject>
#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
Expand All @@ -47,7 +48,8 @@ class LocalProviderBinder : public LocalProviderBinderBase
{

public:
LocalProviderBinder(IPCProxyNewBase &proxy) : LocalProviderBinderBase(proxy)
LocalProviderBinder(InterfaceManagerInterface& interfaceManager, IPCProxyNewBase &proxy) :
LocalProviderBinderBase(interfaceManager, proxy)
{
}

Expand All @@ -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<InterfaceType *>(service);
if (provider != m_provider) {
Expand Down
4 changes: 3 additions & 1 deletion src/ipc/ipc-common/LocalProviderBinderBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/ipc/ipc-common/LocalProviderBinderBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#pragma once

#include <QObject>
#include "InterfaceManager.h"
#include "InterfaceManagerInterface.h"
#include "IPCProxyNewBase.h"

#if defined(FaceliftIPCCommonLib_LIBRARY)
Expand All @@ -45,15 +45,15 @@ class LocalProviderBinderBase : public QObject {

public:

LocalProviderBinderBase(IPCProxyNewBase &proxy);
LocalProviderBinderBase(InterfaceManagerInterface& interfaceManager, IPCProxyNewBase &proxy);

virtual void checkLocalAdapterAvailability() = 0;

void init();

protected:
IPCProxyNewBase &m_proxy;
InterfaceManager &m_interfaceManager = InterfaceManager::instance();
InterfaceManagerInterface &m_interfaceManager;
};

}
9 changes: 5 additions & 4 deletions src/ipc/ipc-common/NewIPCServiceAdapterBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand All @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion src/ipc/ipc-common/NewIPCServiceAdapterBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();

Expand Down Expand Up @@ -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;
};

}

0 comments on commit cc8aa69

Please sign in to comment.