summaryrefslogtreecommitdiff
path: root/content/fetchcache.c
diff options
context:
space:
mode:
authorJames Bursa <james@netsurf-browser.org>2003-06-17 19:24:21 +0000
committerJames Bursa <james@netsurf-browser.org>2003-06-17 19:24:21 +0000
commit0c0ff3c59631d0968c888279195ea40d4a7fd824 (patch)
treef94b64c20bc361664de61ef8993be7693ad2a08d /content/fetchcache.c
parentce6dbbb5db1d4ffe77ca99411ddb2d19694eff9c (diff)
downloadnetsurf-0c0ff3c59631d0968c888279195ea40d4a7fd824.tar.gz
netsurf-0c0ff3c59631d0968c888279195ea40d4a7fd824.tar.bz2
[project @ 2003-06-17 19:24:20 by bursa]
Change fetchcache system to store loading contents in cache. svn path=/import/netsurf/; revision=180
Diffstat (limited to 'content/fetchcache.c')
-rw-r--r--content/fetchcache.c198
1 files changed, 34 insertions, 164 deletions
diff --git a/content/fetchcache.c b/content/fetchcache.c
index 600fd13fd..0755b37c5 100644
--- a/content/fetchcache.c
+++ b/content/fetchcache.c
@@ -1,117 +1,52 @@
/**
- * $Id: fetchcache.c,v 1.9 2003/04/25 08:03:15 bursa Exp $
+ * $Id: fetchcache.c,v 1.10 2003/06/17 19:24:20 bursa Exp $
*/
#include <assert.h>
#include <string.h>
#include "netsurf/content/cache.h"
+#include "netsurf/content/content.h"
#include "netsurf/content/fetchcache.h"
#include "netsurf/content/fetch.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/utils.h"
-struct fetchcache {
- char *url;
- void (*callback)(fetchcache_msg msg, struct content *c, void *p, const char *error);
- void *p;
- struct fetch *f;
- struct content *c;
- unsigned long width, height;
- unsigned long size;
- content_type allowed;
- struct fetchcache *next;
- struct fetchcache *prev;
- struct fetchcache *next_request;
- int active;
-};
-
-static struct fetchcache *fetchcache_list = 0;
-
-static void fetchcache_free(struct fetchcache *fc);
-static void fetchcache_callback(fetchcache_msg msg, void *p, char *data, unsigned long size);
-static void status_callback(void *p, const char *status);
+static void fetchcache_callback(fetch_msg msg, void *p, char *data, unsigned long size);
-void fetchcache(const char *url, char *referer,
- void (*callback)(fetchcache_msg msg, struct content *c, void *p, const char *error),
- void *p, unsigned long width, unsigned long height, content_type allowed)
+struct content * fetchcache(const char *url, char *referer,
+ void (*callback)(content_msg msg, struct content *c, void *p1,
+ void *p2, const char *error),
+ void *p1, void *p2, unsigned long width, unsigned long height)
{
struct content *c;
- struct fetchcache *fc, *fc_url;
+
+ LOG(("url %s", url));
c = cache_get(url);
if (c != 0) {
- /* check type is allowed */
- if ((1 << c->type) & allowed) {
- callback(FETCHCACHE_STATUS, c, p, "Found in cache");
- content_revive(c, width, height);
- callback(FETCHCACHE_OK, c, p, 0);
- } else {
- callback(FETCHCACHE_BADTYPE, 0, p, "");
- cache_free(c);
- }
- return;
- }
-
- callback(FETCHCACHE_STATUS, c, p, "Starting fetch");
- fc = xcalloc(1, sizeof(struct fetchcache));
- fc->url = xstrdup(url);
- fc->callback = callback;
- fc->p = p;
- fc->c = 0;
- fc->width = width;
- fc->height = height;
- fc->size = 0;
- fc->allowed = allowed;
- fc->next = 0;
- fc->prev = 0;
- fc->next_request = 0;
- fc->active = 1;
-
- /* check if we're already fetching this url */
- for (fc_url = fetchcache_list;
- fc_url != 0 && strcmp(fc_url->url, url) != 0;
- fc_url = fc_url->next)
- ;
- if (fc_url != 0) {
- /* already fetching: add ourselves to list of requestors */
- LOG(("already fetching"));
- fc->next_request = fc_url->next_request;
- fc_url->next_request = fc;
-
- } else {
- /* not fetching yet */
- if (fetchcache_list != 0)
- fetchcache_list->prev = fc;
- fc->next = fetchcache_list;
- fetchcache_list = fc;
- fc->f = fetch_start(fc->url, referer, fetchcache_callback, fc);
+ content_add_user(c, callback, p1, p2);
+ return c;
}
-}
-
-void fetchcache_free(struct fetchcache *fc)
-{
- free(fc->url);
- free(fc);
- if (fc->prev == 0)
- fetchcache_list = fc->next;
- else
- fc->prev->next = fc->next;
- if (fc->next != 0)
- fc->next->prev = fc->prev;
+ c = content_create(url);
+ content_add_user(c, callback, p1, p2);
+ cache_put(c);
+ c->fetch_size = 0;
+ c->width = width;
+ c->height = height;
+ c->fetch = fetch_start(url, referer, fetchcache_callback, c);
+ return c;
}
void fetchcache_callback(fetch_msg msg, void *p, char *data, unsigned long size)
{
- struct fetchcache *fc = p, *fc_url;
+ struct content *c = p;
content_type type;
char *mime_type;
char *semic;
- char status[40];
- int active = 0;
switch (msg) {
case FETCH_TYPE:
@@ -119,89 +54,33 @@ void fetchcache_callback(fetch_msg msg, void *p, char *data, unsigned long size)
if ((semic = strchr(mime_type, ';')) != 0)
*semic = 0; /* remove "; charset=..." */
type = content_lookup(mime_type);
- LOG(("FETCH_TYPE, type %u", type));
-
- /* check if each request allows this type */
- for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request) {
- if (!fc_url->active)
- continue;
- if ((1 << type) & fc_url->allowed) {
- active++;
- } else {
- fc_url->active = 0;
- fc_url->callback(FETCHCACHE_BADTYPE, 0,
- fc_url->p, mime_type);
- }
- }
- if (active != 0) {
- /* someone is still interested */
- fc->c = content_create(type, fc->url);
- fc->c->status_callback = status_callback;
- fc->c->status_p = fc;
- } else {
- /* no request allows the type */
- fetch_abort(fc->f);
- for (; fc != 0; fc = fc_url) {
- fc_url = fc->next_request;
- fetchcache_free(fc);
- }
- }
-
free(mime_type);
+ LOG(("FETCH_TYPE, type %u", type));
+ content_set_type(c, type);
break;
case FETCH_DATA:
LOG(("FETCH_DATA"));
- assert(fc->c != 0);
- fc->size += size;
- sprintf(status, "Received %lu bytes", fc->size);
- for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request)
- if (fc_url->active)
- fc_url->callback(FETCHCACHE_STATUS, fc->c,
- fc_url->p, status);
- content_process_data(fc->c, data, size);
+ c->fetch_size += size;
+ sprintf(c->status_message, "Received %lu bytes", c->fetch_size);
+ content_broadcast(c, CONTENT_MSG_STATUS, 0);
+ content_process_data(c, data, size);
break;
case FETCH_FINISHED:
LOG(("FETCH_FINISHED"));
- assert(fc->c != 0);
- sprintf(status, "Converting %lu bytes", fc->size);
- for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request)
- if (fc_url->active)
- fc_url->callback(FETCHCACHE_STATUS, fc->c,
- fc_url->p, status);
-
- if (content_convert(fc->c, fc->width, fc->height) == 0) {
- cache_put(fc->c);
- for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request)
- if (fc_url->active)
- fc_url->callback(FETCHCACHE_OK, cache_get(fc->url),
- fc_url->p, 0);
- cache_free(fc->c);
- } else {
- content_destroy(fc->c);
- for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request)
- if (fc_url->active)
- fc_url->callback(FETCHCACHE_ERROR, 0,
- fc_url->p, "Conversion failed");
- }
- for (; fc != 0; fc = fc_url) {
- fc_url = fc->next_request;
- fetchcache_free(fc);
- }
+ sprintf(c->status_message, "Converting %lu bytes", c->fetch_size);
+ c->fetch = 0;
+ content_broadcast(c, CONTENT_MSG_STATUS, 0);
+ content_convert(c, c->width, c->height);
break;
case FETCH_ERROR:
LOG(("FETCH_ERROR, '%s'", data));
- if (fc->c != 0)
- content_destroy(fc->c);
- for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request)
- if (fc_url->active)
- fc->callback(FETCHCACHE_ERROR, 0, fc_url->p, data);
- for (; fc != 0; fc = fc_url) {
- fc_url = fc->next_request;
- fetchcache_free(fc);
- }
+ c->fetch = 0;
+ content_broadcast(c, CONTENT_MSG_ERROR, data);
+ cache_destroy(c);
+ content_destroy(c);
break;
default:
@@ -210,15 +89,6 @@ void fetchcache_callback(fetch_msg msg, void *p, char *data, unsigned long size)
}
-void status_callback(void *p, const char *status)
-{
- struct fetchcache *fc = p, *fc_url;
- for (fc_url = fc; fc_url != 0; fc_url = fc_url->next_request)
- if (fc_url->active)
- fc_url->callback(FETCHCACHE_STATUS, fc->c, fc_url->p, status);
-}
-
-
#ifdef TEST
#include <unistd.h>