summaryrefslogtreecommitdiff
path: root/gck/gck-object-cache.c
blob: 215486722a4ba45d5f59ff1cb65270d9b880220a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* gck-object-cache.c - the GObject PKCS#11 wrapper library

   Copyright (C) 2011 Collabora Ltd.

   The Gnome Keyring 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.

   The Gnome Keyring 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 the Gnome Library; see the file COPYING.LIB.  If not,
   see <http://www.gnu.org/licenses/>.

   Author: Stef Walter <stefw@collabora.co.uk>
*/

#include "config.h"

#include "gck.h"
#include "gck-private.h"

#include <string.h>

/**
 * GckObjectCache:
 *
 * An interface implemented by derived classes of [class@Object] to indicate
 * which attributes they'd like an enumerator to retrieve.
 *
 * These attributes are then cached on the object and can be retrieved through
 * the [property@ObjectCache:attributes] property.
 */

/**
 * GckObjectCacheInterface:
 * @interface: parent interface
 * @default_types: (array length=n_default_types): attribute types that an
 *                   enumerator should retrieve
 * @n_default_types: number of attribute types to be retrieved
 * @fill: virtual method to add attributes to the cache
 *
 * Interface for [iface@ObjectCache]. If the @default_types field is
 * implemented by a implementing class, then that will be used by a
 * [class@Enumerator] which has been setup using
 * [method@Enumerator.set_object_type]
 *
 * The implementation for @populate should add the attributes to the
 * cache. It must be thread safe.
 */

G_DEFINE_INTERFACE (GckObjectCache, gck_object_cache, GCK_TYPE_OBJECT);

static void
gck_object_cache_default_init (GckObjectCacheInterface *iface)
{
	/**
	 * GckObjectCache:attributes:
	 *
	 * The attributes cached on this object.
	 */
	g_object_interface_install_property (iface,
	         g_param_spec_boxed ("attributes", "Attributes", "PKCS#11 Attributes",
	                             GCK_TYPE_ATTRIBUTES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}

/**
 * gck_object_cache_get_attributes: (skip):
 * @object: an object with an attribute cache
 *
 * Gets the attributes cached on this object.
 *
 * Returns: (transfer full) (nullable): the attributes
 */
GckAttributes *
gck_object_cache_get_attributes (GckObjectCache *object)
{
	GckAttributes *attributes = NULL;
	g_return_val_if_fail (GCK_IS_OBJECT_CACHE (object), NULL);
	g_object_get (object, "attributes", &attributes, NULL);
	return attributes;
}

/**
 * gck_object_cache_set_attributes:
 * @object: an object with an attribute cache
 * @attrs: (nullable): the attributes to set
 *
 * Sets the attributes cached on this object.
 */
void
gck_object_cache_set_attributes (GckObjectCache *object,
                                 GckAttributes *attrs)
{
	g_return_if_fail (GCK_IS_OBJECT_CACHE (object));

	g_object_set (object, "attributes", attrs, NULL);
}

/**
 * gck_object_cache_fill:
 * @object: an object with the cache
 * @attrs: the attributes to cache
 *
 * Adds the attributes to the set cached on this object. If an attribute is
 * already present in the cache it will be overridden by this value.
 *
 * This will be done in a thread-safe manner.
 */
void
gck_object_cache_fill (GckObjectCache *object,
                       GckAttributes *attrs)
{
	GckObjectCacheInterface *iface;

	g_return_if_fail (GCK_IS_OBJECT_CACHE (object));
	g_return_if_fail (attrs != NULL);

	iface = GCK_OBJECT_CACHE_GET_IFACE (object);
	g_return_if_fail (iface->fill != NULL);

	(iface->fill) (object, attrs);
}

/**
 * gck_object_cache_update:
 * @object: the object with the cache
 * @attr_types: (array length=n_attr_types): the types of attributes to update
 * @n_attr_types: the number of attribute types
 * @cancellable: optional cancellation object
 * @error: location to place an error
 *
 * Update the object cache with given attributes. If an attribute already
 * exists in the cache, it will be updated, and if it doesn't it will be added.
 *
 * This may block, use the asynchronous version when this is not desirable
 *
 * Returns: whether the cache update was successful
 */
gboolean
gck_object_cache_update (GckObjectCache *object,
                         const gulong *attr_types,
                         gint n_attr_types,
                         GCancellable *cancellable,
                         GError **error)
{
	GckObjectCacheInterface *iface;
	GckAttributes *attrs;

	g_return_val_if_fail (GCK_IS_OBJECT_CACHE (object), FALSE);
	g_return_val_if_fail (attr_types != NULL || n_attr_types == 0, FALSE);
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	iface = GCK_OBJECT_CACHE_GET_IFACE (object);

	if (attr_types == NULL) {
		attr_types = iface->default_types;
		n_attr_types = iface->n_default_types;

		if (attr_types == NULL || n_attr_types == 0) {
			g_warning ("no attribute types passed to gck_object_cache_update() "
			           "and no default types on object.");
			return FALSE;
		}
	}

	attrs = gck_object_get_full (GCK_OBJECT (object),
	                             attr_types, n_attr_types,
	                             cancellable, error);

	if (attrs != NULL) {
		gck_object_cache_fill (object, attrs);
		gck_attributes_unref (attrs);
	}

	return attrs != NULL;
}

static void
on_cache_object_get (GObject *source,
                     GAsyncResult *result,
                     gpointer user_data)
{
	GTask *task = G_TASK (user_data);
	GckAttributes *attrs;
	GError *error = NULL;

	attrs = gck_object_get_finish (GCK_OBJECT (source), result, &error);
	if (error == NULL) {
		gck_object_cache_fill (GCK_OBJECT_CACHE (source), attrs);
		gck_attributes_unref (attrs);
		g_task_return_boolean (task, TRUE);
	} else {
		g_task_return_error (task, g_steal_pointer (&error));
	}

	g_clear_object (&task);
}

/**
 * gck_object_cache_update_async:
 * @object: the object with the cache
 * @attr_types: (array length=n_attr_types): the types of attributes to update
 * @n_attr_types: the number of attribute types
 * @cancellable: optional cancellation object
 * @callback: called when the operation completes
 * @user_data: data to be passed to the callback
 *
 * Update the object cache with given attributes. If an attribute already
 * exists in the cache, it will be updated, and if it doesn't it will be added.
 *
 * This call will return immediately and complete asynchronously.
 */
void
gck_object_cache_update_async (GckObjectCache *object,
                               const gulong *attr_types,
                               gint n_attr_types,
                               GCancellable *cancellable,
                               GAsyncReadyCallback callback,
                               gpointer user_data)
{
	GckObjectCacheInterface *iface;
	GTask *task;

	g_return_if_fail (GCK_IS_OBJECT_CACHE (object));
	g_return_if_fail (attr_types != NULL || n_attr_types == 0);
	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

	iface = GCK_OBJECT_CACHE_GET_IFACE (object);

	if (attr_types == NULL) {
		attr_types = iface->default_types;
		n_attr_types = iface->n_default_types;

		if (attr_types == NULL || n_attr_types == 0) {
			g_warning ("no attribute types passed to gck_object_cache_update_async() "
			           "and no default types on object.");
			return;
		}
	}

	task = g_task_new (object, cancellable, callback, user_data);
	g_task_set_source_tag (task, gck_object_cache_update_async);

	gck_object_get_async (GCK_OBJECT (object), attr_types, n_attr_types,
	                      cancellable, on_cache_object_get, g_steal_pointer (&task));

	g_clear_object (&task);
}

/**
 * gck_object_cache_update_finish:
 * @object: the object with the cache
 * @result: the asynchronous result passed to the callback
 * @error: location to place an error
 *
 * Complete an asynchronous operation to update the object cache with given
 * attributes.
 *
 * Returns: whether the cache update was successful
 */
gboolean
gck_object_cache_update_finish (GckObjectCache *object,
                                GAsyncResult *result,
                                GError **error)
{
	g_return_val_if_fail (GCK_IS_OBJECT_CACHE (object), FALSE);
	g_return_val_if_fail (g_task_is_valid (result, object), FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	return g_task_propagate_boolean (G_TASK (result), error);
}

static gboolean
check_have_attributes (GckAttributes *attrs,
                       const gulong *attr_types,
                       gint n_attr_types)
{
	gint i;

	if (attrs == NULL)
		return FALSE;

	for (i = 0; i < n_attr_types; i++) {
		if (!gck_attributes_find (attrs, attr_types[i]))
			return FALSE;
	}

	return TRUE;
}

/**
 * gck_object_cache_lookup:
 * @object: the object
 * @attr_types: (array length=n_attr_types): the types of attributes to update
 * @n_attr_types: the number of attribute types
 * @cancellable: optional cancellation object
 * @error: location to place an error
 *
 * Lookup attributes in the cache, or retrieve them from the object if necessary.
 *
 * If @object is a #GckObjectCache then this will lookup the attributes there
 * first if available, otherwise will read them from the object and update
 * the cache.
 *
 * If @object is not a #GckObjectCache, then the attributes will simply be
 * read from the object.
 *
 * This may block, use the asynchronous version when this is not desirable
 *
 * Returns: (transfer full): the attributes retrieved or %NULL on failure
 */
GckAttributes *
gck_object_cache_lookup (GckObject *object,
                         const gulong *attr_types,
                         gint n_attr_types,
                         GCancellable *cancellable,
                         GError **error)
{
	GckAttributes *attrs;
	GckObjectCache *cache;

	g_return_val_if_fail (GCK_IS_OBJECT (object), NULL);
	g_return_val_if_fail (attr_types != NULL || n_attr_types == 0, NULL);
	g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
	g_return_val_if_fail (error == NULL || *error == NULL, NULL);

	if (GCK_IS_OBJECT_CACHE (object)) {
		cache = GCK_OBJECT_CACHE (object);
		attrs = gck_object_cache_get_attributes (cache);
		if (check_have_attributes (attrs, attr_types, n_attr_types))
			return attrs;
		gck_attributes_unref (attrs);

		if (!gck_object_cache_update (cache, attr_types, n_attr_types,
		                              cancellable, error))
			return NULL;

		return gck_object_cache_get_attributes (cache);

	} else {
		return gck_object_get_full (object, attr_types, n_attr_types,
		                            cancellable, error);
	}
}

/**
 * gck_object_cache_lookup_async:
 * @object: the object
 * @attr_types: (array length=n_attr_types): the types of attributes to update
 * @n_attr_types: the number of attribute types
 * @cancellable: optional cancellation object
 * @callback: called when the operation completes
 * @user_data: data to pass to the callback
 *
 * Lookup attributes in the cache, or retrieve them from the object if necessary.
 *
 * If @object is a #GckObjectCache then this will lookup the attributes there
 * first if available, otherwise will read them from the object and update
 * the cache.
 *
 * If @object is not a #GckObjectCache, then the attributes will simply be
 * read from the object.
 *
 * This will return immediately and complete asynchronously
 */
void
gck_object_cache_lookup_async (GckObject *object,
                               const gulong *attr_types,
                               gint n_attr_types,
                               GCancellable *cancellable,
                               GAsyncReadyCallback callback,
                               gpointer user_data)
{
	g_return_if_fail (GCK_IS_OBJECT (object));
	g_return_if_fail (attr_types != NULL || n_attr_types == 0);
	g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));

	if (GCK_IS_OBJECT_CACHE (object)) {
		GckObjectCache *cache;
		GckAttributes *attrs;
		gboolean have;

		cache = GCK_OBJECT_CACHE (object);
		attrs = gck_object_cache_get_attributes (cache);
		have = check_have_attributes (attrs, attr_types, n_attr_types);
		gck_attributes_unref (attrs);

		if (have) {
			GTask *task;

			task = g_task_new (cache, cancellable, callback, user_data);
			g_task_set_source_tag (task, gck_object_cache_lookup_async);
			g_task_return_boolean (task, TRUE);
			g_clear_object (&task);
		} else {
			gck_object_cache_update_async (cache, attr_types, n_attr_types,
			                               cancellable, callback, user_data);
		}
	} else {
		gck_object_get_async (object, attr_types, n_attr_types, cancellable,
		                      callback, user_data);
	}
}

/**
 * gck_object_cache_lookup_finish:
 * @object: the object
 * @result: the asynchrounous result passed to the callback
 * @error: location to place an error
 *
 * Complete an operation to lookup attributes in the cache or retrieve them
 * from the object if necessary.
 *
 * Returns: (transfer full): the attributes retrieved or %NULL on failure
 */
GckAttributes *
gck_object_cache_lookup_finish (GckObject *object,
                                GAsyncResult *result,
                                GError **error)
{
	GckObjectCache *cache;

	g_return_val_if_fail (GCK_IS_OBJECT (object), NULL);
	g_return_val_if_fail (error == NULL || *error == NULL, NULL);

	if (GCK_IS_OBJECT_CACHE (object)) {
		cache = GCK_OBJECT_CACHE (object);
		if (!g_task_is_valid (result, object))
			if (!gck_object_cache_update_finish (cache, result, error))
				return NULL;
		return gck_object_cache_get_attributes (cache);
	}

	return gck_object_get_finish (object, result, error);
}