Subversion Repositories Kolibri OS

Rev

Rev 3584 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3584 sourcerer 1
/*
2
 * Copyright 2009 John-Mark Bell 
3
 *
4
 * This file is part of NetSurf, http://www.netsurf-browser.org/
5
 *
6
 * NetSurf is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; version 2 of the License.
9
 *
10
 * NetSurf is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see .
17
 */
18
 
19
/** \file
20
 * High-level resource cache (implementation)
21
 */
22
 
23
#include 
24
#include 
25
#include 
26
 
27
#include "content/content.h"
28
#include "content/hlcache.h"
29
#include "content/mimesniff.h"
30
#include "utils/http.h"
31
#include "utils/log.h"
32
#include "utils/messages.h"
33
#include "utils/ring.h"
34
#include "utils/schedule.h"
35
#include "utils/url.h"
36
#include "utils/utils.h"
37
 
38
typedef struct hlcache_entry hlcache_entry;
39
typedef struct hlcache_retrieval_ctx hlcache_retrieval_ctx;
40
 
41
/** High-level cache retrieval context */
42
struct hlcache_retrieval_ctx {
43
	struct hlcache_retrieval_ctx *r_prev; /**< Previous retrieval context in the ring */
44
	struct hlcache_retrieval_ctx *r_next; /**< Next retrieval context in the ring */
45
 
46
	llcache_handle *llcache;	/**< Low-level cache handle */
47
 
48
	hlcache_handle *handle;		/**< High-level handle for object */
49
 
50
	uint32_t flags;			/**< Retrieval flags */
51
 
52
	content_type accepted_types;	/**< Accepted types */
53
 
54
	hlcache_child_context child;	/**< Child context */
55
 
56
	bool migrate_target;		/**< Whether this context is the migration target */
57
};
58
 
59
/** High-level cache handle */
60
struct hlcache_handle {
61
	hlcache_entry *entry;		/**< Pointer to cache entry */
62
 
63
	hlcache_handle_callback cb;	/**< Client callback */
64
	void *pw;			/**< Client data */
65
};
66
 
67
/** Entry in high-level cache */
68
struct hlcache_entry {
69
	struct content *content;	/**< Pointer to associated content */
70
 
71
	hlcache_entry *next;		/**< Next sibling */
72
	hlcache_entry *prev;		/**< Previous sibling */
73
};
74
 
75
/** Current state of the cache.
76
 *
77
 * Global state of the cache.
78
 */
79
struct hlcache_s {
80
	struct hlcache_parameters params;
81
 
82
	/** List of cached content objects */
83
	hlcache_entry *content_list;
84
 
85
	/** Ring of retrieval contexts */
86
	hlcache_retrieval_ctx *retrieval_ctx_ring;
87
 
88
	/* statsistics */
89
	unsigned int hit_count;
90
	unsigned int miss_count;
91
};
92
 
93
/** high level cache state */
94
static struct hlcache_s *hlcache = NULL;
95
 
96
 
97
static void hlcache_clean(void *ignored);
98
 
99
static nserror hlcache_llcache_callback(llcache_handle *handle,
100
		const llcache_event *event, void *pw);
101
static nserror hlcache_migrate_ctx(hlcache_retrieval_ctx *ctx,
102
		lwc_string *effective_type);
103
static bool hlcache_type_is_acceptable(lwc_string *mime_type,
104
		content_type accepted_types, content_type *computed_type);
105
static nserror hlcache_find_content(hlcache_retrieval_ctx *ctx,
106
		lwc_string *effective_type);
107
static void hlcache_content_callback(struct content *c,
108
		content_msg msg, union content_msg_data data, void *pw);
109
 
110
/******************************************************************************
111
 * Public API								      *
112
 ******************************************************************************/
113
 
114
nserror
115
hlcache_initialise(const struct hlcache_parameters *hlcache_parameters)
116
{
117
	nserror ret;
118
 
119
	hlcache = calloc(1, sizeof(struct hlcache_s));
120
	if (hlcache == NULL) {
121
		return NSERROR_NOMEM;
122
	}
123
 
124
	ret = llcache_initialise(hlcache_parameters->cb,
125
				 hlcache_parameters->cb_ctx,
126
				 hlcache_parameters->limit);
127
	if (ret != NSERROR_OK) {
128
		free(hlcache);
129
		hlcache = NULL;
130
		return ret;
131
	}
132
 
133
	hlcache->params = *hlcache_parameters;
134
 
135
	/* Schedule the cache cleanup */
136
	schedule(hlcache->params.bg_clean_time / 10, hlcache_clean, NULL);
137
 
138
	return NSERROR_OK;
139
}
140
 
141
/* See hlcache.h for documentation */
142
void hlcache_stop(void)
143
{
144
	/* Remove the hlcache_clean schedule */
145
	schedule_remove(hlcache_clean, NULL);
146
}
147
 
148
/* See hlcache.h for documentation */
149
void hlcache_finalise(void)
150
{
151
	uint32_t num_contents, prev_contents;
152
	hlcache_entry *entry;
153
	hlcache_retrieval_ctx *ctx, *next;
154
 
155
	/* Obtain initial count of contents remaining */
156
	for (num_contents = 0, entry = hlcache->content_list;
157
			entry != NULL; entry = entry->next) {
158
		num_contents++;
159
	}
160
 
161
	LOG(("%d contents remain before cache drain", num_contents));
162
 
163
	/* Drain cache */
164
	do {
165
		prev_contents = num_contents;
166
 
167
		hlcache_clean(NULL);
168
 
169
		for (num_contents = 0, entry = hlcache->content_list;
170
				entry != NULL; entry = entry->next) {
171
			num_contents++;
172
		}
173
	} while (num_contents > 0 && num_contents != prev_contents);
174
 
175
	LOG(("%d contents remaining:", num_contents));
176
	for (entry = hlcache->content_list; entry != NULL; entry = entry->next) {
177
		hlcache_handle entry_handle = { entry, NULL, NULL };
178
 
179
		if (entry->content != NULL) {
180
			LOG(("	%p : %s (%d users)", entry,
181
			     nsurl_access(hlcache_handle_get_url(&entry_handle)), content_count_users(entry->content)));
182
		} else {
183
			LOG(("	%p", entry));
184
		}
185
	}
186
 
187
	/* Clean up retrieval contexts */
188
	if (hlcache->retrieval_ctx_ring != NULL) {
189
		ctx = hlcache->retrieval_ctx_ring;
190
 
191
		do {
192
			next = ctx->r_next;
193
 
194
			if (ctx->llcache != NULL)
195
				llcache_handle_release(ctx->llcache);
196
 
197
			if (ctx->handle != NULL)
198
				free(ctx->handle);
199
 
200
			if (ctx->child.charset != NULL)
201
				free((char *) ctx->child.charset);
202
 
203
			free(ctx);
204
 
205
			ctx = next;
206
		} while (ctx != hlcache->retrieval_ctx_ring);
207
 
208
		hlcache->retrieval_ctx_ring = NULL;
209
	}
210
 
211
	LOG(("hit/miss %d/%d", hlcache->hit_count, hlcache->miss_count));
212
 
213
	free(hlcache);
214
	hlcache = NULL;
215
 
216
	LOG(("Finalising low-level cache"));
217
	llcache_finalise();
218
}
219
 
220
/* See hlcache.h for documentation */
221
nserror hlcache_poll(void)
222
{
223
 
224
	llcache_poll();
225
 
226
	return NSERROR_OK;
227
}
228
 
229
/* See hlcache.h for documentation */
230
nserror hlcache_handle_retrieve(nsurl *url, uint32_t flags,
231
		nsurl *referer, llcache_post_data *post,
232
		hlcache_handle_callback cb, void *pw,
233
		hlcache_child_context *child,
234
		content_type accepted_types, hlcache_handle **result)
235
{
236
	hlcache_retrieval_ctx *ctx;
237
	nserror error;
238
 
239
	assert(cb != NULL);
240
 
241
	ctx = calloc(1, sizeof(hlcache_retrieval_ctx));
242
	if (ctx == NULL)
243
		return NSERROR_NOMEM;
244
 
245
	ctx->handle = calloc(1, sizeof(hlcache_handle));
246
	if (ctx->handle == NULL) {
247
		free(ctx);
248
		return NSERROR_NOMEM;
249
	}
250
 
251
	if (child != NULL) {
252
		if (child->charset != NULL) {
253
			ctx->child.charset = strdup(child->charset);
254
			if (ctx->child.charset == NULL) {
255
				free(ctx->handle);
256
				free(ctx);
257
				return NSERROR_NOMEM;
258
			}
259
		}
260
		ctx->child.quirks = child->quirks;
261
	}
262
 
263
	ctx->flags = flags;
264
	ctx->accepted_types = accepted_types;
265
 
266
	ctx->handle->cb = cb;
267
	ctx->handle->pw = pw;
268
 
269
	error = llcache_handle_retrieve(url, flags, referer, post,
270
			hlcache_llcache_callback, ctx,
271
			&ctx->llcache);
272
	if (error != NSERROR_OK) {
273
		free((char *) ctx->child.charset);
274
		free(ctx->handle);
275
		free(ctx);
276
		return error;
277
	}
278
 
279
	RING_INSERT(hlcache->retrieval_ctx_ring, ctx);
280
 
281
	*result = ctx->handle;
282
 
283
	return NSERROR_OK;
284
}
285
 
286
/* See hlcache.h for documentation */
287
nserror hlcache_handle_release(hlcache_handle *handle)
288
{
289
	if (handle->entry != NULL) {
290
		content_remove_user(handle->entry->content,
291
				hlcache_content_callback, handle);
292
	} else {
293
		RING_ITERATE_START(struct hlcache_retrieval_ctx,
294
				   hlcache->retrieval_ctx_ring,
295
				   ictx) {
296
			if (ictx->handle == handle &&
297
					ictx->migrate_target == false) {
298
				/* This is the nascent context for us,
299
				 * so abort the fetch */
300
				llcache_handle_abort(ictx->llcache);
301
				llcache_handle_release(ictx->llcache);
302
				/* Remove us from the ring */
303
				RING_REMOVE(hlcache->retrieval_ctx_ring, ictx);
304
				/* Throw us away */
305
				free((char *) ictx->child.charset);
306
				free(ictx);
307
				/* And stop */
308
				RING_ITERATE_STOP(hlcache->retrieval_ctx_ring,
309
						ictx);
310
			}
311
		} RING_ITERATE_END(hlcache->retrieval_ctx_ring, ictx);
312
	}
313
 
314
	handle->cb = NULL;
315
	handle->pw = NULL;
316
 
317
	free(handle);
318
 
319
	return NSERROR_OK;
320
}
321
 
322
/* See hlcache.h for documentation */
323
struct content *hlcache_handle_get_content(const hlcache_handle *handle)
324
{
325
	assert(handle != NULL);
326
 
327
	if (handle->entry != NULL)
328
		return handle->entry->content;
329
 
330
	return NULL;
331
}
332
 
333
/* See hlcache.h for documentation */
334
nserror hlcache_handle_abort(hlcache_handle *handle)
335
{
336
	struct hlcache_entry *entry = handle->entry;
337
	struct content *c;
338
 
339
	if (entry == NULL) {
340
		/* This handle is not yet associated with a cache entry.
341
		 * The implication is that the fetch for the handle has
342
		 * not progressed to the point where the entry can be
343
		 * created. */
344
 
345
		RING_ITERATE_START(struct hlcache_retrieval_ctx,
346
				   hlcache->retrieval_ctx_ring,
347
				   ictx) {
348
			if (ictx->handle == handle &&
349
					ictx->migrate_target == false) {
350
				/* This is the nascent context for us,
351
				 * so abort the fetch */
352
				llcache_handle_abort(ictx->llcache);
353
				llcache_handle_release(ictx->llcache);
354
				/* Remove us from the ring */
355
				RING_REMOVE(hlcache->retrieval_ctx_ring, ictx);
356
				/* Throw us away */
357
				free((char *) ictx->child.charset);
358
				free(ictx);
359
				/* And stop */
360
				RING_ITERATE_STOP(hlcache->retrieval_ctx_ring,
361
						ictx);
362
			}
363
		} RING_ITERATE_END(hlcache->retrieval_ctx_ring, ictx);
364
 
365
		return NSERROR_OK;
366
	}
367
 
368
	c = entry->content;
369
 
370
	if (content_count_users(c) > 1) {
371
		/* We are not the only user of 'c' so clone it. */
372
		struct content *clone = content_clone(c);
373
 
374
		if (clone == NULL)
375
			return NSERROR_NOMEM;
376
 
377
		entry = calloc(sizeof(struct hlcache_entry), 1);
378
 
379
		if (entry == NULL) {
380
			content_destroy(clone);
381
			return NSERROR_NOMEM;
382
		}
383
 
384
		if (content_add_user(clone,
385
				hlcache_content_callback, handle) == false) {
386
			content_destroy(clone);
387
			free(entry);
388
			return NSERROR_NOMEM;
389
		}
390
 
391
		content_remove_user(c, hlcache_content_callback, handle);
392
 
393
		entry->content = clone;
394
		handle->entry = entry;
395
		entry->prev = NULL;
396
		entry->next = hlcache->content_list;
397
		if (hlcache->content_list != NULL)
398
			hlcache->content_list->prev = entry;
399
		hlcache->content_list = entry;
400
 
401
		c = clone;
402
	}
403
 
404
	return content_abort(c);
405
}
406
 
407
/* See hlcache.h for documentation */
408
nserror hlcache_handle_replace_callback(hlcache_handle *handle,
409
		hlcache_handle_callback cb, void *pw)
410
{
411
	handle->cb = cb;
412
	handle->pw = pw;
413
 
414
	return NSERROR_OK;
415
}
416
 
417
nserror hlcache_handle_clone(hlcache_handle *handle, hlcache_handle **result)
418
{
419
	*result = NULL;
420
	return NSERROR_CLONE_FAILED;
421
}
422
 
423
/* See hlcache.h for documentation */
424
nsurl *hlcache_handle_get_url(const hlcache_handle *handle)
425
{
426
	nsurl *result = NULL;
427
 
428
	assert(handle != NULL);
429
 
430
	if (handle->entry != NULL) {
431
		result = content_get_url(handle->entry->content);
432
	} else {
433
		RING_ITERATE_START(struct hlcache_retrieval_ctx,
434
				   hlcache->retrieval_ctx_ring,
435
				   ictx) {
436
			if (ictx->handle == handle) {
437
				/* This is the nascent context for us */
438
				result = llcache_handle_get_url(ictx->llcache);
439
 
440
				/* And stop */
441
				RING_ITERATE_STOP(hlcache->retrieval_ctx_ring,
442
						ictx);
443
			}
444
		} RING_ITERATE_END(hlcache->retrieval_ctx_ring, ictx);
445
	}
446
 
447
	return result;
448
}
449
 
450
/******************************************************************************
451
 * High-level cache internals						      *
452
 ******************************************************************************/
453
 
454
/**
455
 * Attempt to clean the cache
456
 */
457
void hlcache_clean(void *ignored)
458
{
459
	hlcache_entry *entry, *next;
460
 
461
	for (entry = hlcache->content_list; entry != NULL; entry = next) {
462
		next = entry->next;
463
 
464
		if (entry->content == NULL)
465
			continue;
466
 
467
		if (content__get_status(entry->content) ==
468
				CONTENT_STATUS_LOADING)
469
			continue;
470
 
471
		if (content_count_users(entry->content) != 0)
472
			continue;
473
 
474
		/** \todo This is over-zealous: all unused contents
475
		 * will be immediately destroyed. Ideally, we want to
476
		 * purge all unused contents that are using stale
477
		 * source data, and enough fresh contents such that
478
		 * the cache fits in the configured cache size limit.
479
		 */
480
 
481
		/* Remove entry from cache */
482
		if (entry->prev == NULL)
483
			hlcache->content_list = entry->next;
484
		else
485
			entry->prev->next = entry->next;
486
 
487
		if (entry->next != NULL)
488
			entry->next->prev = entry->prev;
489
 
490
		/* Destroy content */
491
		content_destroy(entry->content);
492
 
493
		/* Destroy entry */
494
		free(entry);
495
	}
496
 
497
	/* Attempt to clean the llcache */
498
	llcache_clean();
499
 
500
	/* Re-schedule ourselves */
501
	schedule(hlcache->params.bg_clean_time / 10, hlcache_clean, NULL);
502
}
503
 
504
/**
505
 * Handler for low-level cache events
506
 *
507
 * \param handle  Handle for which event is issued
508
 * \param event	  Event data
509
 * \param pw	  Pointer to client-specific data
510
 * \return NSERROR_OK on success, appropriate error otherwise
511
 */
512
nserror hlcache_llcache_callback(llcache_handle *handle,
513
		const llcache_event *event, void *pw)
514
{
515
	hlcache_retrieval_ctx *ctx = pw;
516
	lwc_string *effective_type = NULL;
517
	nserror error;
518
 
519
	assert(ctx->llcache == handle);
520
 
521
	switch (event->type) {
522
	case LLCACHE_EVENT_HAD_HEADERS:
523
		error = mimesniff_compute_effective_type(handle, NULL, 0,
524
				ctx->flags & HLCACHE_RETRIEVE_SNIFF_TYPE,
525
				ctx->accepted_types == CONTENT_IMAGE,
526
				&effective_type);
527
		if (error == NSERROR_OK || error == NSERROR_NOT_FOUND) {
528
			/* If the sniffer was successful or failed to find
529
			 * a Content-Type header when sniffing was
530
			 * prohibited, we must migrate the retrieval context. */
531
			error = hlcache_migrate_ctx(ctx, effective_type);
532
 
533
			if (effective_type != NULL)
534
				lwc_string_unref(effective_type);
535
		}
536
 
537
		/* No need to report that we need data:
538
		 * we'll get some anyway if there is any */
539
		if (error == NSERROR_NEED_DATA)
540
			error = NSERROR_OK;
541
 
542
		return error;
543
 
544
		break;
545
	case LLCACHE_EVENT_HAD_DATA:
546
		error = mimesniff_compute_effective_type(handle,
547
				event->data.data.buf, event->data.data.len,
548
				ctx->flags & HLCACHE_RETRIEVE_SNIFF_TYPE,
549
				ctx->accepted_types == CONTENT_IMAGE,
550
				&effective_type);
551
		if (error != NSERROR_OK) {
552
			assert(0 && "MIME sniff failed with data");
553
		}
554
 
555
		error = hlcache_migrate_ctx(ctx, effective_type);
556
 
557
		lwc_string_unref(effective_type);
558
 
559
		return error;
560
 
561
		break;
562
	case LLCACHE_EVENT_DONE:
563
		/* DONE event before we could determine the effective MIME type.
564
		 */
565
		error = mimesniff_compute_effective_type(handle,
566
				NULL, 0, false, false, &effective_type);
567
		if (error == NSERROR_OK) {
568
			error = hlcache_migrate_ctx(ctx, effective_type);
569
 
570
			lwc_string_unref(effective_type);
571
 
572
			return error;
573
		}
574
 
575
		if (ctx->handle->cb != NULL) {
576
			hlcache_event hlevent;
577
 
578
			hlevent.type = CONTENT_MSG_ERROR;
579
			hlevent.data.error = messages_get("BadType");
580
 
581
			ctx->handle->cb(ctx->handle, &hlevent, ctx->handle->pw);
582
		}
583
		break;
584
	case LLCACHE_EVENT_ERROR:
585
		if (ctx->handle->cb != NULL) {
586
			hlcache_event hlevent;
587
 
588
			hlevent.type = CONTENT_MSG_ERROR;
589
			hlevent.data.error = event->data.msg;
590
 
591
			ctx->handle->cb(ctx->handle, &hlevent, ctx->handle->pw);
592
		}
593
		break;
594
	case LLCACHE_EVENT_PROGRESS:
595
		break;
596
	}
597
 
598
	return NSERROR_OK;
599
}
600
 
601
/**
602
 * Migrate a retrieval context into its final destination content
603
 *
604
 * \param ctx             Context to migrate
605
 * \param effective_type  The effective MIME type of the content, or NULL
606
 * \return NSERROR_OK on success,
607
 *         NSERROR_NEED_DATA on success where data is needed,
608
 *         appropriate error otherwise
609
 */
610
nserror hlcache_migrate_ctx(hlcache_retrieval_ctx *ctx,
611
		lwc_string *effective_type)
612
{
613
	content_type type = CONTENT_NONE;
614
	nserror error = NSERROR_OK;
615
 
616
	ctx->migrate_target = true;
617
 
618
	if (effective_type != NULL &&
619
			hlcache_type_is_acceptable(effective_type,
620
			ctx->accepted_types, &type)) {
621
		error = hlcache_find_content(ctx, effective_type);
622
		if (error != NSERROR_OK && error != NSERROR_NEED_DATA) {
623
			if (ctx->handle->cb != NULL) {
624
				hlcache_event hlevent;
625
 
626
				hlevent.type = CONTENT_MSG_ERROR;
627
				hlevent.data.error = messages_get("MiscError");
628
 
629
				ctx->handle->cb(ctx->handle, &hlevent,
630
						ctx->handle->pw);
631
			}
632
 
633
			llcache_handle_abort(ctx->llcache);
634
			llcache_handle_release(ctx->llcache);
635
		}
636
	} else if (type == CONTENT_NONE &&
637
			(ctx->flags & HLCACHE_RETRIEVE_MAY_DOWNLOAD)) {
638
		/* Unknown type, and we can download, so convert */
639
		llcache_handle_force_stream(ctx->llcache);
640
 
641
		if (ctx->handle->cb != NULL) {
642
			hlcache_event hlevent;
643
 
644
			hlevent.type = CONTENT_MSG_DOWNLOAD;
645
			hlevent.data.download = ctx->llcache;
646
 
647
			ctx->handle->cb(ctx->handle, &hlevent,
648
					ctx->handle->pw);
649
		}
650
 
651
		/* Ensure caller knows we need data */
652
		error = NSERROR_NEED_DATA;
653
	} else {
654
		/* Unacceptable type: report error */
655
		if (ctx->handle->cb != NULL) {
656
			hlcache_event hlevent;
657
 
658
			hlevent.type = CONTENT_MSG_ERROR;
659
			hlevent.data.error = messages_get("UnacceptableType");
660
 
661
			ctx->handle->cb(ctx->handle, &hlevent,
662
					ctx->handle->pw);
663
		}
664
 
665
		llcache_handle_abort(ctx->llcache);
666
		llcache_handle_release(ctx->llcache);
667
	}
668
 
669
	ctx->migrate_target = false;
670
 
671
	/* No longer require retrieval context */
672
	RING_REMOVE(hlcache->retrieval_ctx_ring, ctx);
673
	free((char *) ctx->child.charset);
674
	free(ctx);
675
 
676
	return error;
677
}
678
 
679
/**
680
 * Determine if the specified MIME type is acceptable
681
 *
682
 * \param mime_type       MIME type to consider
683
 * \param accepted_types  Array of acceptable types, or NULL for any
684
 * \param computed_type	  Pointer to location to receive computed type of object
685
 * \return True if the type is acceptable, false otherwise
686
 */
687
bool hlcache_type_is_acceptable(lwc_string *mime_type,
688
		content_type accepted_types, content_type *computed_type)
689
{
690
	content_type type;
691
 
692
	type = content_factory_type_from_mime_type(mime_type);
693
 
694
	*computed_type = type;
695
 
696
	return ((accepted_types & type) != 0);
697
}
698
 
699
/**
700
 * Find a content for the high-level cache handle
701
 *
702
 * \param ctx             High-level cache retrieval context
703
 * \param effective_type  Effective MIME type of content
704
 * \return NSERROR_OK on success,
705
 *         NSERROR_NEED_DATA on success where data is needed,
706
 *         appropriate error otherwise
707
 *
708
 * \pre handle::state == HLCACHE_HANDLE_NEW
709
 * \pre Headers must have been received for associated low-level handle
710
 * \post Low-level handle is either released, or associated with new content
711
 * \post High-level handle is registered with content
712
 */
713
nserror hlcache_find_content(hlcache_retrieval_ctx *ctx,
714
		lwc_string *effective_type)
715
{
716
	hlcache_entry *entry;
717
	hlcache_event event;
718
	nserror error = NSERROR_OK;
719
 
720
	/* Search list of cached contents for a suitable one */
721
	for (entry = hlcache->content_list; entry != NULL; entry = entry->next) {
722
		hlcache_handle entry_handle = { entry, NULL, NULL };
723
		const llcache_handle *entry_llcache;
724
 
725
		if (entry->content == NULL)
726
			continue;
727
 
728
		/* Ignore contents in the error state */
729
		if (content_get_status(&entry_handle) == CONTENT_STATUS_ERROR)
730
			continue;
731
 
732
		/* Ensure that content is shareable */
733
		if (content_is_shareable(entry->content) == false)
734
			continue;
735
 
736
		/* Ensure that quirks mode is acceptable */
737
		if (content_matches_quirks(entry->content,
738
				ctx->child.quirks) == false)
739
			continue;
740
 
741
		/* Ensure that content uses same low-level object as
742
		 * low-level handle */
743
		entry_llcache = content_get_llcache_handle(entry->content);
744
 
745
		if (llcache_handle_references_same_object(entry_llcache,
746
				ctx->llcache))
747
			break;
748
	}
749
 
750
	if (entry == NULL) {
751
		/* No existing entry, so need to create one */
752
		entry = malloc(sizeof(hlcache_entry));
753
		if (entry == NULL)
754
			return NSERROR_NOMEM;
755
 
756
		/* Create content using llhandle */
757
		entry->content = content_factory_create_content(ctx->llcache,
758
				ctx->child.charset, ctx->child.quirks,
759
				effective_type);
760
		if (entry->content == NULL) {
761
			free(entry);
762
			return NSERROR_NOMEM;
763
		}
764
 
765
		/* Insert into cache */
766
		entry->prev = NULL;
767
		entry->next = hlcache->content_list;
768
		if (hlcache->content_list != NULL)
769
			hlcache->content_list->prev = entry;
770
		hlcache->content_list = entry;
771
 
772
		/* Signal to caller that we created a content */
773
		error = NSERROR_NEED_DATA;
774
 
775
		hlcache->miss_count++;
776
	} else {
777
		/* Found a suitable content: no longer need low-level handle */
778
		llcache_handle_release(ctx->llcache);
779
		hlcache->hit_count++;
780
	}
781
 
782
	/* Associate handle with content */
783
	if (content_add_user(entry->content,
784
			hlcache_content_callback, ctx->handle) == false)
785
		return NSERROR_NOMEM;
786
 
787
	/* Associate cache entry with handle */
788
	ctx->handle->entry = entry;
789
 
790
	/* Catch handle up with state of content */
791
	if (ctx->handle->cb != NULL) {
792
		content_status status = content_get_status(ctx->handle);
793
 
794
		if (status == CONTENT_STATUS_LOADING) {
795
			event.type = CONTENT_MSG_LOADING;
796
			ctx->handle->cb(ctx->handle, &event, ctx->handle->pw);
797
		} else if (status == CONTENT_STATUS_READY) {
798
			event.type = CONTENT_MSG_LOADING;
799
			ctx->handle->cb(ctx->handle, &event, ctx->handle->pw);
800
 
801
			if (ctx->handle->cb != NULL) {
802
				event.type = CONTENT_MSG_READY;
803
				ctx->handle->cb(ctx->handle, &event,
804
						ctx->handle->pw);
805
			}
806
		} else if (status == CONTENT_STATUS_DONE) {
807
			event.type = CONTENT_MSG_LOADING;
808
			ctx->handle->cb(ctx->handle, &event, ctx->handle->pw);
809
 
810
			if (ctx->handle->cb != NULL) {
811
				event.type = CONTENT_MSG_READY;
812
				ctx->handle->cb(ctx->handle, &event,
813
						ctx->handle->pw);
814
			}
815
 
816
			if (ctx->handle->cb != NULL) {
817
				event.type = CONTENT_MSG_DONE;
818
				ctx->handle->cb(ctx->handle, &event,
819
						ctx->handle->pw);
820
			}
821
		}
822
	}
823
 
824
	return error;
825
}
826
 
827
/**
828
 * Veneer between content callback API and hlcache callback API
829
 *
830
 * \param c	Content to emit message for
831
 * \param msg	Message to emit
832
 * \param data	Data for message
833
 * \param pw	Pointer to private data (hlcache_handle)
834
 */
835
void hlcache_content_callback(struct content *c, content_msg msg,
836
		union content_msg_data data, void *pw)
837
{
838
	hlcache_handle *handle = pw;
839
	hlcache_event event;
840
	nserror error = NSERROR_OK;
841
 
842
	event.type = msg;
843
	event.data = data;
844
 
845
	if (handle->cb != NULL)
846
		error = handle->cb(handle, &event, handle->pw);
847
 
848
	if (error != NSERROR_OK)
849
		LOG(("Error in callback: %d", error));
850
}