From b30f5dbd66fe534a977725fd6f0360f772f8642a Mon Sep 17 00:00:00 2001 From: Kevron Rees Date: Wed, 8 Jan 2014 15:37:58 -0800 Subject: [PATCH] make FindObject() create the dbus object on the fly --- plugins/dbus/abstractdbusinterface.cpp | 5 ++- plugins/dbus/abstractdbusinterface.h | 2 ++ plugins/dbus/automotivemanager.cpp | 64 ++++++++++++++++++++++++++++++++-- plugins/dbus/automotivemanager.h | 7 ++++ plugins/dbus/dbusplugin.cpp | 6 ---- 5 files changed, 75 insertions(+), 9 deletions(-) diff --git a/plugins/dbus/abstractdbusinterface.cpp b/plugins/dbus/abstractdbusinterface.cpp index e2ddd87..440de77 100644 --- a/plugins/dbus/abstractdbusinterface.cpp +++ b/plugins/dbus/abstractdbusinterface.cpp @@ -318,6 +318,8 @@ void AbstractDBusInterface::unregisterObject() { if(regId) g_dbus_connection_unregister_object(mConnection, regId); + + regId=0; } void AbstractDBusInterface::updateValue(AbstractProperty *property) @@ -327,7 +329,8 @@ void AbstractDBusInterface::updateValue(AbstractProperty *property) return; } - signaller->fireSignal(mConnection, mObjectPath, "org.freedesktop.DBus.Properties", "PropertiesChanged", property); + if(isRegistered()) + signaller->fireSignal(mConnection, mObjectPath, "org.freedesktop.DBus.Properties", "PropertiesChanged", property); } std::list AbstractDBusInterface::getObjectsForProperty(string object) diff --git a/plugins/dbus/abstractdbusinterface.h b/plugins/dbus/abstractdbusinterface.h index 5ac3bf6..4678cee 100644 --- a/plugins/dbus/abstractdbusinterface.h +++ b/plugins/dbus/abstractdbusinterface.h @@ -95,6 +95,8 @@ public: std::unordered_map getProperties() { return properties; } + bool isRegistered() { return regId > 0; } + protected: void startRegistration(); diff --git a/plugins/dbus/automotivemanager.cpp b/plugins/dbus/automotivemanager.cpp index 17b513f..abb2756 100644 --- a/plugins/dbus/automotivemanager.cpp +++ b/plugins/dbus/automotivemanager.cpp @@ -1,5 +1,6 @@ #include "automotivemanager.h" #include "abstractdbusinterface.h" +#include "listplusplus.h" static const gchar introspection_xml[] = "" @@ -51,9 +52,11 @@ static void handleMethodCall(GDBusConnection *connection, std::string method = method_name; + uint pid = getPid(sender); + if(DebugOut::getDebugThreshhold() >= 6) { - DebugOut(6)<<"DBus method call from: "<isSupported()) continue; + + if(!t->isRegistered()) + t->registerObject(); + + std::list processes = manager->subscribedProcesses[t]; + + if(!contains(processes,sender)) + { + DebugOut()<<"Referencing "<objectPath()<<" with sender: "<subscribedProcesses[t].push_back(sender); + } + GVariant *newvar = g_variant_new("o",t->objectPath().c_str()); g_variant_builder_add_value(¶ms, newvar); - } //GVariant* var = g_variant_builder_end(¶ms); @@ -311,6 +325,48 @@ static void handleMethodCall(GDBusConnection *connection, } } +static void signalCallback( GDBusConnection *connection, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + AutomotiveManager* manager = static_cast(user_data); + + gchar* name=nullptr; + gchar* newOwner=nullptr; + gchar* oldOwner = nullptr; + g_variant_get(parameters,"(sss)",&name, &oldOwner, &newOwner); + + if(std::string(newOwner) == "") + { + for(auto i : manager->subscribedProcesses) + { + AbstractDBusInterface* iface = i.first; + for(auto n : i.second) + { + if(n == name) + { + DebugOut()<<"unreferencing "<objectPath()<subscribedProcesses[iface].remove(n); + } + } + + if(manager->subscribedProcesses[iface].empty()) + { + DebugOut()<<"No more subscribers. Unregistering."<unregisterObject(); + } + } + } + + g_free(name); + g_free(newOwner); + g_free(oldOwner); +} + static GVariant* getProperty(GDBusConnection* connection, const gchar* sender, const gchar* objectPath, const gchar* interfaceName, const gchar* propertyName, GError** error, gpointer userData) { return NULL; @@ -347,6 +403,10 @@ AutomotiveManager::AutomotiveManager(GDBusConnection *connection) } g_assert(regId > 0); + + g_dbus_connection_signal_subscribe(g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL,NULL), "org.freedesktop.DBus", "org.freedesktop.DBus", + "NameOwnerChanged", "/org/freedesktop/DBus", NULL, G_DBUS_SIGNAL_FLAGS_NONE, + signalCallback, this, NULL); } AutomotiveManager::~AutomotiveManager() diff --git a/plugins/dbus/automotivemanager.h b/plugins/dbus/automotivemanager.h index 0db9f1b..e8d1c11 100644 --- a/plugins/dbus/automotivemanager.h +++ b/plugins/dbus/automotivemanager.h @@ -2,6 +2,10 @@ #define AUTOMOTIVEMANAGER_H_ #include +#include +#include + +class AbstractDBusInterface; class AutomotiveManager { @@ -10,10 +14,13 @@ public: AutomotiveManager(GDBusConnection* connection); ~AutomotiveManager(); + std::map> subscribedProcesses; + private: GDBusConnection* mConnection; guint regId; + }; diff --git a/plugins/dbus/dbusplugin.cpp b/plugins/dbus/dbusplugin.cpp index 14670cc..9547072 100644 --- a/plugins/dbus/dbusplugin.cpp +++ b/plugins/dbus/dbusplugin.cpp @@ -65,12 +65,6 @@ void DBusSink::supportedChanged(PropertyList supportedProperties) supported = true; } } - - - if(supported) - registerObject(); - else - unregisterObject(); } void DBusSink::propertyChanged(AbstractPropertyType *value) -- 2.7.4