From 2bcc913a7f302cc404efcba951565bdeab9e1de4 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 29 Dec 2014 23:37:11 -0200 Subject: Use QDBusServiceWatcher in QDBusAbstractInterface to track the owner Instead of connecting to the "NameOwnerChanged" signal. This has better performance, is simpler to implement and is future-proof: when we switch to kdbus, there won't be a "NameOwnerChanged" signal to connect to. On the drawback side, this will use slightly more memory. Change-Id: I5801b8027949f041309567a493000fe7de9bf227 Reviewed-by: Alex Blasche Reviewed-by: Albert Astals Cid --- src/dbus/qdbusabstractinterface.cpp | 45 ++++++++++++++----------------------- src/dbus/qdbusabstractinterface_p.h | 2 ++ 2 files changed, 19 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/dbus/qdbusabstractinterface.cpp b/src/dbus/qdbusabstractinterface.cpp index a7c70bce70..5ccd8c8d83 100644 --- a/src/dbus/qdbusabstractinterface.cpp +++ b/src/dbus/qdbusabstractinterface.cpp @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2015 The Qt Company Ltd. +** Copyright (C) 2015 Intel Corporation. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtDBus module of the Qt Toolkit. @@ -41,6 +42,7 @@ #include "qdbusmessage_p.h" #include "qdbusmetaobject_p.h" #include "qdbusmetatype_p.h" +#include "qdbusservicewatcher.h" #include "qdbusutil_p.h" #include @@ -97,6 +99,17 @@ QDBusAbstractInterfacePrivate::QDBusAbstractInterfacePrivate(const QString &serv } } +void QDBusAbstractInterfacePrivate::initOwnerTracking() +{ + if (!isValid || !connection.isConnected() || connectionPrivate()->mode == QDBusConnectionPrivate::PeerMode) + return; + if (service.isEmpty() || service.startsWith(QLatin1Char(':'))) + return; + QObject::connect(new QDBusServiceWatcher(service, connection, QDBusServiceWatcher::WatchForOwnerChange, q_func()), + SIGNAL(serviceOwnerChanged(QString,QString,QString)), + q_func(), SLOT(_q_serviceOwnerChanged(QString,QString,QString))); +} + bool QDBusAbstractInterfacePrivate::canMakeCalls() const { // recheck only if we have a wildcard (i.e. empty) service or path @@ -218,9 +231,8 @@ void QDBusAbstractInterfacePrivate::_q_serviceOwnerChanged(const QString &name, Q_UNUSED(oldOwner); Q_UNUSED(name); //qDebug() << "QDBusAbstractInterfacePrivate serviceOwnerChanged" << name << oldOwner << newOwner; - if (name == service) { - currentOwner = newOwner; - } + Q_ASSERT(name == service); + currentOwner = newOwner; } QDBusAbstractInterfaceBase::QDBusAbstractInterfaceBase(QDBusAbstractInterfacePrivate &d, QObject *parent) @@ -285,19 +297,7 @@ int QDBusAbstractInterfaceBase::qt_metacall(QMetaObject::Call _c, int _id, void QDBusAbstractInterface::QDBusAbstractInterface(QDBusAbstractInterfacePrivate &d, QObject *parent) : QDBusAbstractInterfaceBase(d, parent) { - // keep track of the service owner - if (d.isValid && - d.connection.isConnected() - && !d.service.isEmpty() - && !d.service.startsWith(QLatin1Char(':')) - && d.connectionPrivate()->mode != QDBusConnectionPrivate::PeerMode) - d_func()->connection.connect(QDBusUtil::dbusService(), // service - QString(), // path - QDBusUtil::dbusInterface(), // interface - QDBusUtil::nameOwnerChanged(), - QStringList() << d.service, - QString(), // signature - this, SLOT(_q_serviceOwnerChanged(QString,QString,QString))); + d.initOwnerTracking(); } /*! @@ -312,18 +312,7 @@ QDBusAbstractInterface::QDBusAbstractInterface(const QString &service, const QSt con, false), parent) { // keep track of the service owner - if (d_func()->isValid && - d_func()->connection.isConnected() - && !service.isEmpty() - && !service.startsWith(QLatin1Char(':')) - && d_func()->connectionPrivate()->mode != QDBusConnectionPrivate::PeerMode) - d_func()->connection.connect(QDBusUtil::dbusService(), // service - QString(), // path - QDBusUtil::dbusInterface(), // interface - QDBusUtil::nameOwnerChanged(), - QStringList() << service, - QString(), //signature - this, SLOT(_q_serviceOwnerChanged(QString,QString,QString))); + d_func()->initOwnerTracking(); } /*! diff --git a/src/dbus/qdbusabstractinterface_p.h b/src/dbus/qdbusabstractinterface_p.h index ba2bcadfb0..1ce457d94b 100644 --- a/src/dbus/qdbusabstractinterface_p.h +++ b/src/dbus/qdbusabstractinterface_p.h @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2015 The Qt Company Ltd. +** Copyright (C) 2015 Intel Corporation. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the QtDBus module of the Qt Toolkit. @@ -78,6 +79,7 @@ public: QDBusAbstractInterfacePrivate(const QString &serv, const QString &p, const QString &iface, const QDBusConnection& con, bool dynamic); virtual ~QDBusAbstractInterfacePrivate() { } + void initOwnerTracking(); bool canMakeCalls() const; // these functions do not check if the property is valid -- cgit v1.2.3