summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-01-04 07:58:58 +0100
committerThomas Haller <thaller@redhat.com>2020-01-08 13:44:05 +0100
commit85f03e393d15c3c500e50fc7e5fc8a8749235060 (patch)
tree5a41f57cfb6ace82da3fa467dc39ed767f4c71bd
parent5080dfa46fc6496be110d743ce233d2ca6b9977f (diff)
downloadNetworkManager-th/nm-client-get-object-by-path.tar.gz
client: add nm_client_get_object_by_path() and nm_object_get_client() APIth/nm-client-get-object-by-path
When iterating the GMainContext of the NMClient instance, D-Bus events get processed. That means, every time you iterate the context (or "return to the main loop"), the content of the cache might change completely. It makes sense to keep a reference to an NMObject instance, do something, and afterwards check whether the instance can still be found in the cache. Add an API for that. nm_object_get_client() allows to know whether the object is still cached. Likewise, while NMClient abstracts D-Bus, it should still provide a way to look up an NMObject by D-Bus path. Add nm_client_get_object_by_path() for that.
-rw-r--r--libnm/libnm.ver2
-rw-r--r--libnm/nm-client.c20
-rw-r--r--libnm/nm-client.h3
-rw-r--r--libnm/nm-object.c29
-rw-r--r--libnm/nm-object.h5
5 files changed, 57 insertions, 2 deletions
diff --git a/libnm/libnm.ver b/libnm/libnm.ver
index 1a25f046dd..d8e069f8ae 100644
--- a/libnm/libnm.ver
+++ b/libnm/libnm.ver
@@ -1661,6 +1661,8 @@ global:
libnm_1_24_0 {
global:
nm_client_get_instance_flags;
+ nm_client_get_object_by_path;
nm_client_get_permissions_state;
nm_client_instance_flags_get_type;
+ nm_object_get_client;
} libnm_1_22_0;
diff --git a/libnm/nm-client.c b/libnm/nm-client.c
index 02a5b002f5..7e2cacc673 100644
--- a/libnm/nm-client.c
+++ b/libnm/nm-client.c
@@ -3914,6 +3914,26 @@ nm_client_get_nm_running (NMClient *client)
}
/**
+ * nm_client_get_object_by_path:
+ * @client: the #NMClient instance
+ * @dbus_path: the D-Bus path of the object to look up
+ *
+ * Returns: (transfer none): the #NMObject instance that is
+ * cached under @dbus_path, or %NULL if no such object exists.
+ *
+ * Since: 1.24
+ */
+NMObject *
+nm_client_get_object_by_path (NMClient *client,
+ const char *dbus_path)
+{
+ g_return_val_if_fail (NM_IS_CLIENT (client), NULL);
+ g_return_val_if_fail (dbus_path, NULL);
+
+ return _dbobjs_get_nmobj_unpack_visible (client, dbus_path, G_TYPE_NONE);
+}
+
+/**
* nm_client_get_metered:
* @client: a #NMClient
*
diff --git a/libnm/nm-client.h b/libnm/nm-client.h
index e409fbc05b..9c2fdff9f1 100644
--- a/libnm/nm-client.h
+++ b/libnm/nm-client.h
@@ -174,6 +174,9 @@ NMState nm_client_get_state (NMClient *client);
gboolean nm_client_get_startup (NMClient *client);
gboolean nm_client_get_nm_running (NMClient *client);
+NMObject *nm_client_get_object_by_path (NMClient *client,
+ const char *dbus_path);
+
NM_AVAILABLE_IN_1_22
NMMetered nm_client_get_metered (NMClient *client);
diff --git a/libnm/nm-object.c b/libnm/nm-object.c
index 89dc8bb3c8..d8c4fb41d2 100644
--- a/libnm/nm-object.c
+++ b/libnm/nm-object.c
@@ -78,13 +78,40 @@ _nm_object_get_client (gpointer self)
*
* Returns: the object's path. This is the internal string used by the
* object, and must not be modified.
+ *
+ * Note that the D-Bus path of an NMObject never changes, even
+ * if the instance gets removed from the cache. To find out
+ * whether the object is still alive/cached, check nm_object_get_client().
**/
const char *
nm_object_get_path (NMObject *object)
{
g_return_val_if_fail (NM_IS_OBJECT (object), NULL);
- return NM_OBJECT_GET_PRIVATE (object)->dbobj->dbus_path->str;
+ return _nm_object_get_path (object);
+}
+
+/**
+ * nm_object_get_client:
+ * @object: a #NMObject
+ *
+ * Returns the #NMClient instance in which object is cached.
+ * Also, if the object got removed from the client cached,
+ * this returns %NULL. So it can be used to check whether the
+ * object is still alive.
+ *
+ * Returns: (transfer none): the #NMClient cache in which the
+ * object can be found, or %NULL if the object is no longer
+ * cached.
+ *
+ * Since: 1.24
+ **/
+NMClient *
+nm_object_get_client (NMObject *object)
+{
+ g_return_val_if_fail (NM_IS_OBJECT (object), NULL);
+
+ return _nm_object_get_client (object);
}
/*****************************************************************************/
diff --git a/libnm/nm-object.h b/libnm/nm-object.h
index 88c34a9eb1..18b44d8aa5 100644
--- a/libnm/nm-object.h
+++ b/libnm/nm-object.h
@@ -31,7 +31,10 @@ typedef struct _NMObjectClass NMObjectClass;
GType nm_object_get_type (void);
-const char *nm_object_get_path (NMObject *object);
+const char *nm_object_get_path (NMObject *object);
+
+NM_AVAILABLE_IN_1_24
+NMClient *nm_object_get_client (NMObject *object);
G_END_DECLS