gstreamer/gst/rtsp-server/rtsp-permissions.c
Wim Taymans facc91a942 permissions: simplify API a little
Avoid passing GstStructure in the add_role method, use varargs instead
to construct the structure behind the scenes. We can then also use the
structure name as the role and simplify some more logic.
2013-07-12 16:17:15 +02:00

252 lines
6.9 KiB
C

/* GStreamer
* Copyright (C) 2013 Wim Taymans <wim.taymans at gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:rtsp-permissions
* @short_description: Roles and associated permissions
* @see_also: #GstRTSPToken, #GstRTSPAuth
*
* Last reviewed on 2013-07-11 (1.0.0)
*/
#include <string.h>
#include "rtsp-permissions.h"
typedef struct _GstRTSPPermissionsImpl
{
GstRTSPPermissions permissions;
/* Roles, array of GstStructure */
GPtrArray *roles;
} GstRTSPPermissionsImpl;
static void
free_structure (GstStructure * structure)
{
gst_structure_set_parent_refcount (structure, NULL);
gst_structure_free (structure);
}
//GST_DEBUG_CATEGORY_STATIC (rtsp_permissions_debug);
//#define GST_CAT_DEFAULT rtsp_permissions_debug
GST_DEFINE_MINI_OBJECT_TYPE (GstRTSPPermissions, gst_rtsp_permissions);
static void gst_rtsp_permissions_init (GstRTSPPermissionsImpl * permissions,
GstStructure * structure);
static void
_gst_rtsp_permissions_free (GstRTSPPermissions * permissions)
{
GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
g_ptr_array_free (impl->roles, TRUE);
g_slice_free1 (sizeof (GstRTSPPermissionsImpl), permissions);
}
static GstRTSPPermissions *
_gst_rtsp_permissions_copy (GstRTSPPermissionsImpl * permissions)
{
GstRTSPPermissionsImpl *copy;
GstStructure *structure;
copy = g_slice_new0 (GstRTSPPermissionsImpl);
gst_rtsp_permissions_init (copy, structure);
return GST_RTSP_PERMISSIONS (copy);
}
static void
gst_rtsp_permissions_init (GstRTSPPermissionsImpl * permissions,
GstStructure * structure)
{
gst_mini_object_init (GST_MINI_OBJECT_CAST (permissions), 0,
GST_TYPE_RTSP_PERMISSIONS,
(GstMiniObjectCopyFunction) _gst_rtsp_permissions_copy, NULL,
(GstMiniObjectFreeFunction) _gst_rtsp_permissions_free);
permissions->roles =
g_ptr_array_new_with_free_func ((GDestroyNotify) free_structure);
}
/**
* gst_rtsp_permissions_new:
*
* Create a new empty Authorization permissions.
*
* Returns: (transfer full): a new empty authorization permissions.
*/
GstRTSPPermissions *
gst_rtsp_permissions_new (void)
{
GstRTSPPermissionsImpl *permissions;
permissions = g_slice_new0 (GstRTSPPermissionsImpl);
gst_rtsp_permissions_init (permissions,
gst_structure_new_empty ("GstRTSPPermissions"));
return GST_RTSP_PERMISSIONS (permissions);
}
/**
* gst_rtsp_permissions_add_role:
* @permissions: a #GstRTSPPermissions
* @role: a role
* @fielname: the first field name
* @...: additional arguments
*
* Add a new @role to @permissions with the given variables. The fields
* are the same layout as gst_structure_new().
*/
void
gst_rtsp_permissions_add_role (GstRTSPPermissions * permissions,
const gchar * role, const gchar * fieldname, ...)
{
va_list var_args;
va_start (var_args, fieldname);
gst_rtsp_permissions_add_role_valist (permissions, role, fieldname, var_args);
va_end (var_args);
}
/**
* gst_rtsp_permissions_add_role_valist:
* @permissions: a #GstRTSPPermissions
* @role: a role
* @fielname: the first field name
* @var_args: additional fields to add
*
* Add a new @role to @permissions with the given variables. Structure fields
* are set according to the varargs in a manner similar to gst_structure_new().
*/
void
gst_rtsp_permissions_add_role_valist (GstRTSPPermissions * permissions,
const gchar * role, const gchar * fieldname, va_list var_args)
{
GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
GstStructure *structure;
gint i, len;
gboolean found;
g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
g_return_if_fail (gst_mini_object_is_writable (&permissions->mini_object));
g_return_if_fail (role != NULL);
g_return_if_fail (fieldname != NULL);
structure = gst_structure_new_valist (role, fieldname, var_args);
g_return_if_fail (structure != NULL);
len = impl->roles->len;
found = FALSE;
for (i = 0; i < len; i++) {
GstStructure *entry = g_ptr_array_index (impl->roles, i);
if (gst_structure_has_name (entry, role)) {
gst_structure_free (entry);
found = TRUE;
break;
}
}
gst_structure_set_parent_refcount (structure,
&impl->permissions.mini_object.refcount);
if (!found)
g_ptr_array_add (impl->roles, structure);
else
g_ptr_array_index (impl->roles, i) = structure;
}
/**
* gst_rtsp_permissions_remove_role:
* @permissions: a #GstRTSPPermissions
* @role: a role
*
* Remove all permissions for @role in @permissions.
*/
void
gst_rtsp_permissions_remove_role (GstRTSPPermissions * permissions,
const gchar * role)
{
g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
g_return_if_fail (gst_mini_object_is_writable (&permissions->mini_object));
g_return_if_fail (role != NULL);
}
/**
* gst_rtsp_permissions_get_role:
* @permissions: a #GstRTSPPermissions
* @role: a role
*
* Get all permissions for @role in @permissions.
*
* Returns: the structure with permissions for @role.
*/
const GstStructure *
gst_rtsp_permissions_get_role (GstRTSPPermissions * permissions,
const gchar * role)
{
GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
gint i, len;
g_return_val_if_fail (GST_IS_RTSP_PERMISSIONS (permissions), NULL);
g_return_val_if_fail (role != NULL, NULL);
len = impl->roles->len;
for (i = 0; i < len; i++) {
GstStructure *entry = g_ptr_array_index (impl->roles, i);
if (gst_structure_has_name (entry, role))
return entry;
}
return NULL;
}
/**
* gst_rtsp_permissions_is_allowed:
* @permissions: a #GstRTSPPermissions
* @role: a role
* @permission: a permission
*
* Check if @role in @permissions is given permission for @permission.
*
* Returns: %TRUE if @role is allowed @permission.
*/
gboolean
gst_rtsp_permissions_is_allowed (GstRTSPPermissions * permissions,
const gchar * role, const gchar * permission)
{
const GstStructure *str;
gboolean result;
g_return_val_if_fail (GST_IS_RTSP_PERMISSIONS (permissions), FALSE);
g_return_val_if_fail (role != NULL, FALSE);
g_return_val_if_fail (permission != NULL, FALSE);
str = gst_rtsp_permissions_get_role (permissions, role);
if (str == NULL)
return FALSE;
if (!gst_structure_get_boolean (str, permission, &result))
result = FALSE;
return result;
}