client: add nm_client_get_object_by_path() and nm_object_get_client() API

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.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/384
This commit is contained in:
Thomas Haller 2020-01-04 07:58:58 +01:00
parent 5080dfa46f
commit 900af25263
5 changed files with 57 additions and 2 deletions

View file

@ -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;

View file

@ -3913,6 +3913,26 @@ nm_client_get_nm_running (NMClient *client)
return NM_CLIENT_GET_PRIVATE (client)->nm_running;
}
/**
* 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

View file

@ -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);

View file

@ -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);
}
/*****************************************************************************/

View file

@ -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