docs: update docs
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-permissions.c
1 /* GStreamer
2  * Copyright (C) 2013 Wim Taymans <wim.taymans at gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include <string.h>
21
22 #include "rtsp-permissions.h"
23
24 typedef struct _GstRTSPPermissionsImpl
25 {
26   GstRTSPPermissions permissions;
27
28   /* Roles, array of RoleEntry */
29   GArray *roles;
30 } GstRTSPPermissionsImpl;
31
32 typedef struct
33 {
34   gchar *role;
35   GstStructure *structure;
36 } RoleEntry;
37
38 static void
39 clear_role_entry (RoleEntry * role)
40 {
41   g_free (role->role);
42   gst_structure_set_parent_refcount (role->structure, NULL);
43   gst_structure_free (role->structure);
44 }
45
46 //GST_DEBUG_CATEGORY_STATIC (rtsp_permissions_debug);
47 //#define GST_CAT_DEFAULT rtsp_permissions_debug
48
49 GST_DEFINE_MINI_OBJECT_TYPE (GstRTSPPermissions, gst_rtsp_permissions);
50
51 static void gst_rtsp_permissions_init (GstRTSPPermissionsImpl * permissions,
52     GstStructure * structure);
53
54 static void
55 _gst_rtsp_permissions_free (GstRTSPPermissions * permissions)
56 {
57   GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
58
59   g_array_free (impl->roles, TRUE);
60
61   g_slice_free1 (sizeof (GstRTSPPermissionsImpl), permissions);
62 }
63
64 static GstRTSPPermissions *
65 _gst_rtsp_permissions_copy (GstRTSPPermissionsImpl * permissions)
66 {
67   GstRTSPPermissionsImpl *copy;
68   GstStructure *structure;
69
70   copy = g_slice_new0 (GstRTSPPermissionsImpl);
71   gst_rtsp_permissions_init (copy, structure);
72
73   return GST_RTSP_PERMISSIONS (copy);
74 }
75
76 static void
77 gst_rtsp_permissions_init (GstRTSPPermissionsImpl * permissions,
78     GstStructure * structure)
79 {
80   gst_mini_object_init (GST_MINI_OBJECT_CAST (permissions), 0,
81       GST_TYPE_RTSP_PERMISSIONS,
82       (GstMiniObjectCopyFunction) _gst_rtsp_permissions_copy, NULL,
83       (GstMiniObjectFreeFunction) _gst_rtsp_permissions_free);
84
85   permissions->roles = g_array_new (FALSE, TRUE, sizeof (RoleEntry));
86   g_array_set_clear_func (permissions->roles,
87       (GDestroyNotify) clear_role_entry);
88 }
89
90 /**
91  * gst_rtsp_permissions_new:
92  *
93  * Create a new empty Authorization permissions.
94  *
95  * Returns: (transfer full): a new empty authorization permissions.
96  */
97 GstRTSPPermissions *
98 gst_rtsp_permissions_new (void)
99 {
100   GstRTSPPermissionsImpl *permissions;
101
102   permissions = g_slice_new0 (GstRTSPPermissionsImpl);
103
104   gst_rtsp_permissions_init (permissions,
105       gst_structure_new_empty ("GstRTSPPermissions"));
106
107   return GST_RTSP_PERMISSIONS (permissions);
108 }
109
110 /**
111  * gst_rtsp_permissions_add_role:
112  * @permissions: a #GstRTSPPermissions
113  * @role: a role
114  * @structure: the permissions structure
115  *
116  * Add the configuration in @structure to @permissions for @role.
117  */
118 void
119 gst_rtsp_permissions_add_role (GstRTSPPermissions * permissions,
120     const gchar * role, GstStructure * structure)
121 {
122   GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
123   gint i, len;
124   RoleEntry item;
125   gboolean found;
126
127   g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
128   g_return_if_fail (gst_mini_object_is_writable (&permissions->mini_object));
129   g_return_if_fail (role != NULL);
130   g_return_if_fail (structure != NULL);
131
132   len = impl->roles->len;
133   found = FALSE;
134   for (i = 0; i < len; i++) {
135     RoleEntry *entry = &g_array_index (impl->roles, RoleEntry, i);
136
137     if (g_str_equal (entry->role, role)) {
138       gst_structure_free (entry->structure);
139       entry->structure = structure;
140       found = TRUE;
141       break;
142     }
143   }
144   if (!found) {
145     item.role = g_strdup (role);
146     item.structure = structure;
147     gst_structure_set_parent_refcount (structure,
148         &impl->permissions.mini_object.refcount);
149     g_array_append_val (impl->roles, item);
150   }
151 }
152
153 /**
154  * gst_rtsp_permissions_remove_role:
155  * @permissions: a #GstRTSPPermissions
156  * @role: a role
157  *
158  * Remove all permissions for @role in @permissions.
159  */
160 void
161 gst_rtsp_permissions_remove_role (GstRTSPPermissions * permissions,
162     const gchar * role)
163 {
164   g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
165   g_return_if_fail (gst_mini_object_is_writable (&permissions->mini_object));
166   g_return_if_fail (role != NULL);
167 }
168
169 /**
170  * gst_rtsp_permissions_get_role:
171  * @permissions: a #GstRTSPPermissions
172  * @role: a role
173  *
174  * Get all permissions for @role in @permissions.
175  *
176  * Returns: the structure with permissions for @role.
177  */
178 const GstStructure *
179 gst_rtsp_permissions_get_role (GstRTSPPermissions * permissions,
180     const gchar * role)
181 {
182   GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
183   gint i, len;
184
185   g_return_val_if_fail (GST_IS_RTSP_PERMISSIONS (permissions), NULL);
186   g_return_val_if_fail (role != NULL, NULL);
187
188   len = impl->roles->len;
189   for (i = 0; i < len; i++) {
190     RoleEntry *entry = &g_array_index (impl->roles, RoleEntry, i);
191
192     if (g_str_equal (entry->role, role))
193       return entry->structure;
194   }
195   return NULL;
196 }
197
198 /**
199  * gst_rtsp_permissions_is_allowed:
200  * @permissions: a #GstRTSPPermissions
201  * @role: a role
202  * @permission: a permission
203  *
204  * Check if @role in @permissions is given permission for @permission.
205  *
206  * Returns: %TRUE if @role is allowed @permission.
207  */
208 gboolean
209 gst_rtsp_permissions_is_allowed (GstRTSPPermissions * permissions,
210     const gchar * role, const gchar * permission)
211 {
212   const GstStructure *str;
213   gboolean result;
214
215   g_return_val_if_fail (GST_IS_RTSP_PERMISSIONS (permissions), FALSE);
216   g_return_val_if_fail (role != NULL, FALSE);
217   g_return_val_if_fail (permission != NULL, FALSE);
218
219   str = gst_rtsp_permissions_get_role (permissions, role);
220   if (str == NULL)
221     return FALSE;
222
223   if (!gst_structure_get_boolean (str, permission, &result))
224     result = FALSE;
225
226   return result;
227 }