2003-06-25 03:22:00 +04:00
|
|
|
/*
|
|
|
|
* This file is part of NetSurf, http://netsurf.sourceforge.net/
|
|
|
|
* Licensed under the GNU General Public License,
|
|
|
|
* http://www.opensource.org/licenses/gpl-license
|
|
|
|
* Copyright 2003 James Bursa <bursa@users.sourceforge.net>
|
2003-02-09 15:58:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "netsurf/content/content.h"
|
2003-06-17 23:24:21 +04:00
|
|
|
#include "netsurf/content/other.h"
|
2003-04-04 19:19:32 +04:00
|
|
|
#include "netsurf/css/css.h"
|
2003-02-09 15:58:15 +03:00
|
|
|
#include "netsurf/render/html.h"
|
|
|
|
#include "netsurf/render/textplain.h"
|
2003-07-15 02:57:45 +04:00
|
|
|
#ifdef riscos
|
2003-02-26 00:00:27 +03:00
|
|
|
#include "netsurf/riscos/jpeg.h"
|
2003-05-10 15:15:49 +04:00
|
|
|
#include "netsurf/riscos/png.h"
|
2003-06-05 17:17:55 +04:00
|
|
|
#include "netsurf/riscos/gif.h"
|
2003-07-08 02:10:51 +04:00
|
|
|
#include "netsurf/riscos/plugin.h"
|
2003-07-15 02:57:45 +04:00
|
|
|
#endif
|
2003-06-17 23:24:21 +04:00
|
|
|
#include "netsurf/utils/log.h"
|
2003-02-09 15:58:15 +03:00
|
|
|
#include "netsurf/utils/utils.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* mime_map must be in sorted order by mime_type */
|
|
|
|
struct mime_entry {
|
2003-07-08 02:10:51 +04:00
|
|
|
char mime_type[40];
|
2003-02-09 15:58:15 +03:00
|
|
|
content_type type;
|
|
|
|
};
|
|
|
|
static const struct mime_entry mime_map[] = {
|
2003-06-17 23:24:21 +04:00
|
|
|
#ifdef riscos
|
2003-06-05 17:17:55 +04:00
|
|
|
{"image/gif", CONTENT_GIF},
|
2003-02-26 00:00:27 +03:00
|
|
|
{"image/jpeg", CONTENT_JPEG},
|
2003-05-10 15:15:49 +04:00
|
|
|
{"image/png", CONTENT_PNG},
|
2003-06-17 23:24:21 +04:00
|
|
|
#endif
|
2003-04-04 19:19:32 +04:00
|
|
|
{"text/css", CONTENT_CSS},
|
2003-02-09 15:58:15 +03:00
|
|
|
{"text/html", CONTENT_HTML},
|
|
|
|
{"text/plain", CONTENT_TEXTPLAIN},
|
|
|
|
};
|
|
|
|
#define MIME_MAP_COUNT (sizeof(mime_map) / sizeof(mime_map[0]))
|
|
|
|
|
|
|
|
/* handler_map must be ordered as enum content_type */
|
|
|
|
struct handler_entry {
|
|
|
|
void (*create)(struct content *c);
|
|
|
|
void (*process_data)(struct content *c, char *data, unsigned long size);
|
|
|
|
int (*convert)(struct content *c, unsigned int width, unsigned int height);
|
|
|
|
void (*revive)(struct content *c, unsigned int width, unsigned int height);
|
|
|
|
void (*reformat)(struct content *c, unsigned int width, unsigned int height);
|
|
|
|
void (*destroy)(struct content *c);
|
2003-05-10 15:15:49 +04:00
|
|
|
void (*redraw)(struct content *c, long x, long y,
|
|
|
|
unsigned long width, unsigned long height);
|
2003-07-15 02:57:45 +04:00
|
|
|
void (*add_instance)(struct content *c, struct browser_window *bw,
|
|
|
|
struct content *page, struct box *box,
|
|
|
|
struct object_params *params, void **state);
|
|
|
|
void (*remove_instance)(struct content *c, struct browser_window *bw,
|
|
|
|
struct content *page, struct box *box,
|
|
|
|
struct object_params *params, void **state);
|
|
|
|
void (*reshape_instance)(struct content *c, struct browser_window *bw,
|
|
|
|
struct content *page, struct box *box,
|
|
|
|
struct object_params *params, void **state);
|
2003-02-09 15:58:15 +03:00
|
|
|
};
|
|
|
|
static const struct handler_entry handler_map[] = {
|
2003-05-10 15:15:49 +04:00
|
|
|
{html_create, html_process_data, html_convert, html_revive,
|
2003-07-15 02:57:45 +04:00
|
|
|
html_reformat, html_destroy, 0,
|
|
|
|
html_add_instance, html_remove_instance, 0},
|
2003-02-09 15:58:15 +03:00
|
|
|
{textplain_create, textplain_process_data, textplain_convert,
|
2003-07-15 02:57:45 +04:00
|
|
|
textplain_revive, textplain_reformat, textplain_destroy, 0, 0, 0, 0},
|
2003-06-17 23:24:21 +04:00
|
|
|
#ifdef riscos
|
2003-05-10 15:15:49 +04:00
|
|
|
{jpeg_create, jpeg_process_data, jpeg_convert, jpeg_revive,
|
2003-07-15 02:57:45 +04:00
|
|
|
jpeg_reformat, jpeg_destroy, jpeg_redraw, 0, 0, 0},
|
2003-06-17 23:24:21 +04:00
|
|
|
#endif
|
2003-07-10 01:33:01 +04:00
|
|
|
{css_create, css_process_data, css_convert, css_revive,
|
2003-07-15 02:57:45 +04:00
|
|
|
css_reformat, css_destroy, 0, 0, 0, 0},
|
2003-06-17 23:24:21 +04:00
|
|
|
#ifdef riscos
|
2003-05-10 15:15:49 +04:00
|
|
|
{nspng_create, nspng_process_data, nspng_convert, nspng_revive,
|
2003-07-15 02:57:45 +04:00
|
|
|
nspng_reformat, nspng_destroy, nspng_redraw, 0, 0, 0},
|
2003-06-05 17:17:55 +04:00
|
|
|
{nsgif_create, nsgif_process_data, nsgif_convert, nsgif_revive,
|
2003-07-15 02:57:45 +04:00
|
|
|
nsgif_reformat, nsgif_destroy, nsgif_redraw, 0, 0, 0},
|
2003-07-08 02:10:51 +04:00
|
|
|
{plugin_create, plugin_process_data, plugin_convert, plugin_revive,
|
2003-07-10 01:33:01 +04:00
|
|
|
plugin_reformat, plugin_destroy, plugin_redraw,
|
2003-07-15 02:57:45 +04:00
|
|
|
plugin_add_instance, plugin_remove_instance,
|
|
|
|
plugin_reshape_instance},
|
2003-06-17 23:24:21 +04:00
|
|
|
#endif
|
|
|
|
{other_create, other_process_data, other_convert, other_revive,
|
2003-07-15 02:57:45 +04:00
|
|
|
other_reformat, other_destroy, 0, 0, 0, 0}
|
2003-02-09 15:58:15 +03:00
|
|
|
};
|
2003-06-17 23:24:21 +04:00
|
|
|
#define HANDLER_MAP_COUNT (sizeof(handler_map) / sizeof(handler_map[0]))
|
2003-02-09 15:58:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* content_lookup -- look up mime type
|
|
|
|
*/
|
|
|
|
|
|
|
|
content_type content_lookup(const char *mime_type)
|
|
|
|
{
|
|
|
|
struct mime_entry *m;
|
|
|
|
m = bsearch(mime_type, mime_map, MIME_MAP_COUNT, sizeof(mime_map[0]),
|
|
|
|
(int (*)(const void *, const void *)) strcmp);
|
2003-07-10 01:33:01 +04:00
|
|
|
if (m == 0) {
|
2003-07-15 02:57:45 +04:00
|
|
|
#ifdef riscos
|
2003-07-10 01:33:01 +04:00
|
|
|
if (plugin_handleable(mime_type))
|
|
|
|
return CONTENT_PLUGIN;
|
2003-07-15 02:57:45 +04:00
|
|
|
#endif
|
2003-02-09 15:58:15 +03:00
|
|
|
return CONTENT_OTHER;
|
2003-07-10 01:33:01 +04:00
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
return m->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2003-06-17 23:24:21 +04:00
|
|
|
* content_create -- create a content structure
|
2003-02-09 15:58:15 +03:00
|
|
|
*/
|
|
|
|
|
2003-06-17 23:24:21 +04:00
|
|
|
struct content * content_create(char *url)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
|
|
|
struct content *c;
|
2003-06-17 23:24:21 +04:00
|
|
|
struct content_user *user_sentinel;
|
|
|
|
LOG(("url %s", url));
|
2003-02-09 15:58:15 +03:00
|
|
|
c = xcalloc(1, sizeof(struct content));
|
|
|
|
c->url = xstrdup(url);
|
2003-06-17 23:24:21 +04:00
|
|
|
c->type = CONTENT_UNKNOWN;
|
|
|
|
c->status = CONTENT_STATUS_TYPE_UNKNOWN;
|
2003-02-09 15:58:15 +03:00
|
|
|
c->size = sizeof(struct content);
|
2003-06-17 23:24:21 +04:00
|
|
|
c->fetch = 0;
|
2003-04-15 21:53:00 +04:00
|
|
|
strcpy(c->status_message, "Loading");
|
2003-06-17 23:24:21 +04:00
|
|
|
user_sentinel = xcalloc(1, sizeof(*user_sentinel));
|
|
|
|
user_sentinel->callback = 0;
|
|
|
|
user_sentinel->p1 = user_sentinel->p2 = 0;
|
|
|
|
user_sentinel->next = 0;
|
|
|
|
c->user_list = user_sentinel;
|
2003-02-09 15:58:15 +03:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-17 23:24:21 +04:00
|
|
|
/**
|
|
|
|
* content_set_type -- initialise the content for the specified mime type
|
|
|
|
*/
|
|
|
|
|
2003-07-08 02:10:51 +04:00
|
|
|
void content_set_type(struct content *c, content_type type, char* mime_type)
|
2003-06-17 23:24:21 +04:00
|
|
|
{
|
2003-07-15 02:57:45 +04:00
|
|
|
assert(c != 0);
|
2003-06-17 23:24:21 +04:00
|
|
|
assert(c->status == CONTENT_STATUS_TYPE_UNKNOWN);
|
|
|
|
assert(type < CONTENT_UNKNOWN);
|
|
|
|
LOG(("content %s, type %i", c->url, type));
|
|
|
|
c->type = type;
|
2003-07-08 02:10:51 +04:00
|
|
|
c->mime_type = mime_type;
|
2003-06-17 23:24:21 +04:00
|
|
|
c->status = CONTENT_STATUS_LOADING;
|
|
|
|
content_broadcast(c, CONTENT_MSG_LOADING, 0);
|
|
|
|
handler_map[type].create(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-09 15:58:15 +03:00
|
|
|
/**
|
|
|
|
* content_process_data -- process a block source data
|
|
|
|
*/
|
|
|
|
|
|
|
|
void content_process_data(struct content *c, char *data, unsigned long size)
|
|
|
|
{
|
|
|
|
assert(c != 0);
|
2003-06-17 23:24:21 +04:00
|
|
|
assert(c->status == CONTENT_STATUS_LOADING);
|
|
|
|
LOG(("content %s, size %lu", c->url, size));
|
2003-02-09 15:58:15 +03:00
|
|
|
handler_map[c->type].process_data(c, data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* content_convert -- all data has arrived, complete the conversion
|
|
|
|
*/
|
|
|
|
|
2003-06-17 23:24:21 +04:00
|
|
|
void content_convert(struct content *c, unsigned long width, unsigned long height)
|
2003-02-09 15:58:15 +03:00
|
|
|
{
|
|
|
|
assert(c != 0);
|
2003-06-17 23:24:21 +04:00
|
|
|
assert(c->type < HANDLER_MAP_COUNT);
|
|
|
|
assert(c->status == CONTENT_STATUS_LOADING);
|
|
|
|
LOG(("content %s", c->url));
|
2003-04-15 21:53:00 +04:00
|
|
|
c->available_width = width;
|
2003-06-17 23:24:21 +04:00
|
|
|
if (handler_map[c->type].convert(c, width, height)) {
|
|
|
|
/* convert failed, destroy content */
|
|
|
|
content_broadcast(c, CONTENT_MSG_ERROR, "Conversion failed");
|
|
|
|
content_destroy(c);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(c->status == CONTENT_STATUS_READY ||
|
|
|
|
c->status == CONTENT_STATUS_DONE);
|
|
|
|
if (c->status == CONTENT_STATUS_READY)
|
|
|
|
content_broadcast(c, CONTENT_MSG_READY, 0);
|
|
|
|
else
|
|
|
|
content_broadcast(c, CONTENT_MSG_DONE, 0);
|
2003-02-09 15:58:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* content_revive -- fix content that has been loaded from the cache
|
|
|
|
* eg. load dependencies, reformat to current width
|
|
|
|
*/
|
|
|
|
|
|
|
|
void content_revive(struct content *c, unsigned long width, unsigned long height)
|
|
|
|
{
|
|
|
|
assert(c != 0);
|
2003-06-17 23:24:21 +04:00
|
|
|
if (c->status != CONTENT_STATUS_DONE)
|
2003-04-18 01:35:02 +04:00
|
|
|
return;
|
2003-04-15 21:53:00 +04:00
|
|
|
c->available_width = width;
|
2003-02-09 15:58:15 +03:00
|
|
|
handler_map[c->type].revive(c, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* content_reformat -- reformat to new size
|
|
|
|
*/
|
|
|
|
|
|
|
|
void content_reformat(struct content *c, unsigned long width, unsigned long height)
|
|
|
|
{
|
|
|
|
assert(c != 0);
|
2003-06-17 23:24:21 +04:00
|
|
|
assert(c->status == CONTENT_STATUS_READY ||
|
|
|
|
c->status == CONTENT_STATUS_DONE);
|
2003-04-15 21:53:00 +04:00
|
|
|
c->available_width = width;
|
2003-02-09 15:58:15 +03:00
|
|
|
handler_map[c->type].reformat(c, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* content_destroy -- free content
|
|
|
|
*/
|
|
|
|
|
|
|
|
void content_destroy(struct content *c)
|
|
|
|
{
|
2003-06-17 23:24:21 +04:00
|
|
|
struct content_user *user, *next;
|
2003-02-09 15:58:15 +03:00
|
|
|
assert(c != 0);
|
2003-06-17 23:24:21 +04:00
|
|
|
LOG(("content %p %s", c, c->url));
|
|
|
|
if (c->type < HANDLER_MAP_COUNT)
|
|
|
|
handler_map[c->type].destroy(c);
|
|
|
|
for (user = c->user_list; user != 0; user = next) {
|
|
|
|
next = user->next;
|
|
|
|
xfree(user);
|
|
|
|
}
|
2003-02-09 15:58:15 +03:00
|
|
|
xfree(c);
|
|
|
|
}
|
|
|
|
|
2003-05-10 15:15:49 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* content_redraw -- display content on screen
|
|
|
|
*/
|
|
|
|
|
|
|
|
void content_redraw(struct content *c, long x, long y,
|
|
|
|
unsigned long width, unsigned long height)
|
|
|
|
{
|
|
|
|
assert(c != 0);
|
|
|
|
if (handler_map[c->type].redraw != 0)
|
|
|
|
handler_map[c->type].redraw(c, x, y, width, height);
|
|
|
|
}
|
|
|
|
|
2003-06-17 23:24:21 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* content_add_user -- register a user for callbacks
|
|
|
|
*/
|
|
|
|
|
|
|
|
void content_add_user(struct content *c,
|
|
|
|
void (*callback)(content_msg msg, struct content *c, void *p1,
|
|
|
|
void *p2, const char *error),
|
2003-07-15 02:57:45 +04:00
|
|
|
void *p1, void *p2)
|
2003-06-17 23:24:21 +04:00
|
|
|
{
|
|
|
|
struct content_user *user;
|
|
|
|
LOG(("content %s, user %p %p %p", c->url, callback, p1, p2));
|
|
|
|
user = xcalloc(1, sizeof(*user));
|
|
|
|
user->callback = callback;
|
|
|
|
user->p1 = p1;
|
|
|
|
user->p2 = p2;
|
|
|
|
user->next = c->user_list->next;
|
|
|
|
c->user_list->next = user;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* content_remove_user -- remove a callback user
|
|
|
|
*/
|
|
|
|
|
|
|
|
void content_remove_user(struct content *c,
|
|
|
|
void (*callback)(content_msg msg, struct content *c, void *p1,
|
|
|
|
void *p2, const char *error),
|
2003-07-15 02:57:45 +04:00
|
|
|
void *p1, void *p2)
|
2003-06-17 23:24:21 +04:00
|
|
|
{
|
|
|
|
struct content_user *user, *next;
|
|
|
|
LOG(("content %s, user %p %p %p", c->url, callback, p1, p2));
|
|
|
|
|
|
|
|
/* user_list starts with a sentinel */
|
|
|
|
for (user = c->user_list; user->next != 0 &&
|
|
|
|
!(user->next->callback == callback &&
|
|
|
|
user->next->p1 == p1 &&
|
|
|
|
user->next->p2 == p2); user = user->next)
|
|
|
|
;
|
|
|
|
if (user->next == 0) {
|
|
|
|
LOG(("user not found in list"));
|
|
|
|
assert(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
next = user->next;
|
|
|
|
user->next = next->next;
|
|
|
|
xfree(next);
|
|
|
|
|
|
|
|
/* if there are now no users, stop any loading in progress
|
|
|
|
* and destroy content structure if not in state READY or DONE */
|
|
|
|
if (c->user_list->next == 0) {
|
|
|
|
LOG(("no users for %p %s", c, c->url));
|
|
|
|
if (c->fetch != 0)
|
|
|
|
fetch_abort(c->fetch);
|
|
|
|
if (c->status < CONTENT_STATUS_READY) {
|
|
|
|
cache_destroy(c);
|
|
|
|
content_destroy(c);
|
|
|
|
} else
|
|
|
|
cache_freeable(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* content_broadcast -- send a message to all users
|
|
|
|
*/
|
|
|
|
|
|
|
|
void content_broadcast(struct content *c, content_msg msg, char *error)
|
|
|
|
{
|
|
|
|
struct content_user *user, *next;
|
|
|
|
LOG(("content %s, message %i", c->url, msg));
|
|
|
|
for (user = c->user_list->next; user != 0; user = next) {
|
|
|
|
next = user->next; /* user may be destroyed during callback */
|
|
|
|
if (user->callback != 0)
|
|
|
|
user->callback(msg, c, user->p1, user->p2, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-15 02:57:45 +04:00
|
|
|
|
|
|
|
void content_add_instance(struct content *c, struct browser_window *bw,
|
|
|
|
struct content *page, struct box *box,
|
|
|
|
struct object_params *params, void **state)
|
|
|
|
{
|
|
|
|
assert(c != 0);
|
|
|
|
assert(c->type < CONTENT_UNKNOWN);
|
|
|
|
LOG(("content %s", c->url));
|
|
|
|
if (handler_map[c->type].add_instance != 0)
|
|
|
|
handler_map[c->type].add_instance(c, bw, page, box, params, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void content_remove_instance(struct content *c, struct browser_window *bw,
|
|
|
|
struct content *page, struct box *box,
|
|
|
|
struct object_params *params, void **state)
|
|
|
|
{
|
|
|
|
assert(c != 0);
|
|
|
|
assert(c->type < CONTENT_UNKNOWN);
|
|
|
|
LOG(("content %s", c->url));
|
|
|
|
if (handler_map[c->type].remove_instance != 0)
|
|
|
|
handler_map[c->type].remove_instance(c, bw, page, box, params, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void content_reshape_instance(struct content *c, struct browser_window *bw,
|
|
|
|
struct content *page, struct box *box,
|
|
|
|
struct object_params *params, void **state)
|
|
|
|
{
|
|
|
|
assert(c != 0);
|
|
|
|
assert(c->type < CONTENT_UNKNOWN);
|
|
|
|
LOG(("content %s", c->url));
|
|
|
|
if (handler_map[c->type].reshape_instance != 0)
|
|
|
|
handler_map[c->type].reshape_instance(c, bw, page, box, params, state);
|
|
|
|
}
|
|
|
|
|