on xenial help sign in
DashCommunicator: replace QDBusInterface with a non-blocking simplification which does not introspect the service on creation Fixes: #1403508

Approved by: Michael Zanetti

commit107822f9fb2c0e81e886eb0d87720ad2510fa346 authorGerry Boland <gerry.boland@canonical.com> dateTue 13 Jan 2015, 20:56 (12 years ago) parentd8635d9f9587d3
4 files changed+64−14
plugins/Unity/DashCommunicator/dashconnection.cpp+30 plugins/Unity/DashCommunicator/dashconnection.h+4−1 src/libunity8-private/abstractdbusservicemonitor.cpp+21−8 src/libunity8-private/abstractdbusservicemonitor.h+9−5
plugins/Unity/DashCommunicator/dashconnection.cpp+30
@@ -19,12 +19,42 @@
1919#include <QDBusInterface>
2020#include <QDBusPendingCall>
2121
22+/* The default implementation of AbstractDBusServiceMonitor creates a QDBusInterface when the service
23+ * appears on the bus.
24+ *
25+ * On construction QDBusInterface synchronously introspects the service, which will block the GUI
26+ * thread of this process if the service is busy. QDBusAbstractInterface does not perform this
27+ * introspection, so let's subclass that and avoid the blocking scenario.
28+ *
29+ * However we lose Qt's wrapping of the DBus service with a MetaObject, with the result that we
30+ * cannot easily connect to DBus signals with the usual connect() calls. So this approach only
31+ * suited to push communication with the DBus service.
32+ */
33+class AsyncDBusInterface : public QDBusAbstractInterface
34+{
35+public:
36+ AsyncDBusInterface(const QString &service, const QString &path,
37+ const QString &interface, const QDBusConnection &connection,
38+ QObject *parent = 0)
39+ : QDBusAbstractInterface(service, path, interface.toLatin1().data(), connection, parent)
40+ {}
41+ ~AsyncDBusInterface() = default;
42+};
43+
44+
2245DashConnection::DashConnection(const QString &service, const QString &path, const QString &interface, QObject *parent):
2346 AbstractDBusServiceMonitor(service, path, interface, SessionBus, parent)
2447{
2548
2649}
2750
51+/* Override the default implementation to create a non-blocking DBus interface (see note above). */
52+QDBusAbstractInterface* DashConnection::createInterface(const QString &service, const QString &path,
53+ const QString &interface, const QDBusConnection &connection)
54+{
55+ return new AsyncDBusInterface(service, path, interface, connection);
56+}
57+
2858void DashConnection::setCurrentScope(int index, bool animate, bool isSwipe)
2959{
3060 if (dbusInterface()) {
plugins/Unity/DashCommunicator/dashconnection.h+4−1
@@ -20,7 +20,6 @@
2020// local
2121#include "abstractdbusservicemonitor.h"
2222
23−
2423class DashConnection: public AbstractDBusServiceMonitor
2524{
2625 Q_OBJECT
@@ -29,6 +28,10 @@ public:
2928
3029public Q_SLOTS:
3130 void setCurrentScope(int index, bool animate, bool isSwipe);
31+
32+private:
33+ QDBusAbstractInterface* createInterface(const QString &service, const QString &path,
34+ const QString &interface, const QDBusConnection &connection) override;
3235};
3336
3437#endif
src/libunity8-private/abstractdbusservicemonitor.cpp+21−8
@@ -32,19 +32,20 @@ AbstractDBusServiceMonitor::AbstractDBusServiceMonitor(const QString &service, c
3232 , m_service(service)
3333 , m_path(path)
3434 , m_interface(interface)
35+ , m_bus(bus)
3536 , m_watcher(new QDBusServiceWatcher(service,
3637 (bus == SystemBus) ? QDBusConnection::systemBus()
3738 : QDBusConnection::sessionBus()))
3839 , m_dbusInterface(nullptr)
3940{
40− connect(m_watcher, &QDBusServiceWatcher::serviceRegistered, this, &AbstractDBusServiceMonitor::createInterface);
41− connect(m_watcher, &QDBusServiceWatcher::serviceUnregistered, this, &AbstractDBusServiceMonitor::destroyInterface);
41+ connect(m_watcher, &QDBusServiceWatcher::serviceRegistered, this, &AbstractDBusServiceMonitor::onServiceRegistered);
42+ connect(m_watcher, &QDBusServiceWatcher::serviceUnregistered, this, &AbstractDBusServiceMonitor::onServiceUnregistered);
4243
4344 // Connect to the service if it's up already
4445 QDBusConnectionInterface* sessionBus = QDBusConnection::sessionBus().interface();
4546 QDBusReply<bool> reply = sessionBus->isServiceRegistered(m_service);
4647 if (reply.isValid() && reply.value()) {
47− createInterface(m_service);
48+ onServiceRegistered(m_service);
4849 }
4950}
5051
@@ -54,19 +55,31 @@ AbstractDBusServiceMonitor::~AbstractDBusServiceMonitor()
5455 delete m_dbusInterface;
5556}
5657
57−void AbstractDBusServiceMonitor::createInterface(const QString &)
58+void AbstractDBusServiceMonitor::onServiceRegistered(const QString &)
5859{
5960 if (m_dbusInterface != nullptr) {
6061 delete m_dbusInterface;
6162 m_dbusInterface = nullptr;
6263 }
6364
64− m_dbusInterface = new QDBusInterface(m_service, m_path, m_interface,
65− QDBusConnection::sessionBus());
65+ m_dbusInterface = createInterface(m_service, m_path, m_interface,
66+ (m_bus == SystemBus) ? QDBusConnection::systemBus()
67+ : QDBusConnection::sessionBus());
6668 Q_EMIT serviceAvailableChanged(true);
6769}
6870
69−void AbstractDBusServiceMonitor::destroyInterface(const QString &)
71+/*
72+ * Default implementation creates a QDBusInterface. This performs blocking introspection of the
73+ * service at initialization, which may be undesirable if the service is slow/blocked.
74+ */
75+QDBusAbstractInterface*
76+AbstractDBusServiceMonitor::createInterface(const QString &service, const QString &path,
77+ const QString &interface, const QDBusConnection &connection)
78+{
79+ return new QDBusInterface(service, path, interface, connection);
80+}
81+
82+void AbstractDBusServiceMonitor::onServiceUnregistered(const QString &)
7083{
7184 if (m_dbusInterface != nullptr) {
7285 delete m_dbusInterface;
@@ -76,7 +89,7 @@ void AbstractDBusServiceMonitor::destroyInterface(const QString &)
7689 Q_EMIT serviceAvailableChanged(false);
7790}
7891
79−QDBusInterface* AbstractDBusServiceMonitor::dbusInterface() const
92+QDBusAbstractInterface* AbstractDBusServiceMonitor::dbusInterface() const
8093{
8194 return m_dbusInterface;
8295}
src/libunity8-private/abstractdbusservicemonitor.h+9−5
@@ -24,7 +24,7 @@
2424#include <QString>
2525#include <QDBusConnection>
2626
27−class QDBusInterface;
27+class QDBusAbstractInterface;
2828class QDBusServiceWatcher;
2929
3030class Q_DECL_EXPORT AbstractDBusServiceMonitor : public QObject
@@ -44,7 +44,7 @@ public:
4444 QObject *parent = 0);
4545 ~AbstractDBusServiceMonitor();
4646
47− QDBusInterface* dbusInterface() const;
47+ QDBusAbstractInterface* dbusInterface() const;
4848
4949 bool serviceAvailable() const;
5050
@@ -52,15 +52,19 @@ Q_SIGNALS:
5252 void serviceAvailableChanged(bool available);
5353
5454private Q_SLOTS:
55− void createInterface(const QString &service);
56− void destroyInterface(const QString &service);
55+ void onServiceRegistered(const QString &service);
56+ void onServiceUnregistered(const QString &service);
5757
5858protected:
59+ virtual QDBusAbstractInterface* createInterface(const QString &service, const QString &path,
60+ const QString &interface, const QDBusConnection &connection);
61+
5962 const QString m_service;
6063 const QString m_path;
6164 const QString m_interface;
65+ const Bus m_bus;
6266 QDBusServiceWatcher* m_watcher;
63− QDBusInterface* m_dbusInterface;
67+ QDBusAbstractInterface* m_dbusInterface;
6468};
6569
6670#endif // ABSTRACTDBUSSERVICEMONITOR_H