From 79ce683b4e6d34fe327b00f1e427e476016cfab0 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Sun, 4 Apr 2010 12:41:19 +0000 Subject: Most of a stop implementation. Remaining work: 1) Clone content_html_data 2) Cloning content_css_data requires the charset of the old content 3) Calling hlcache_handle_abort() before a content has been created must clean up the retrieval context. svn path=/trunk/netsurf/; revision=10236 --- content/content.c | 208 ++++++++++++++++++++++++++++++++++++++++-------------- content/content.h | 10 +-- content/hlcache.c | 62 +++++++++++++++- content/hlcache.h | 8 +++ content/llcache.c | 179 +++++++++++++++++++++++++++++++++++++--------- content/llcache.h | 18 +++++ 6 files changed, 391 insertions(+), 94 deletions(-) (limited to 'content') diff --git a/content/content.c b/content/content.c index 9a33d964b..a247dda2b 100644 --- a/content/content.c +++ b/content/content.c @@ -267,6 +267,7 @@ struct handler_entry { struct box *box, struct object_params *params); void (*close)(struct content *c); + bool (*clone)(const struct content *old, struct content *new_content); /** There must be one content per user for this type. */ bool no_share; }; @@ -275,83 +276,87 @@ struct handler_entry { static const struct handler_entry handler_map[] = { {html_create, html_process_data, html_convert, html_reformat, html_destroy, html_stop, html_redraw, 0, - html_open, html_close, + html_open, html_close, html_clone, true}, {textplain_create, textplain_process_data, textplain_convert, textplain_reformat, textplain_destroy, 0, textplain_redraw, 0, - 0, 0, true}, + 0, 0, textplain_clone, true}, {nscss_create, nscss_process_data, nscss_convert, 0, nscss_destroy, - 0, 0, 0, 0, 0, true}, + 0, 0, 0, 0, 0, nscss_clone, true}, #ifdef WITH_JPEG {0, 0, nsjpeg_convert, 0, nsjpeg_destroy, 0, - nsjpeg_redraw, nsjpeg_redraw_tiled, 0, 0, false}, + nsjpeg_redraw, nsjpeg_redraw_tiled, 0, 0, nsjpeg_clone, false}, #endif #ifdef WITH_GIF {nsgif_create, 0, nsgif_convert, 0, nsgif_destroy, 0, - nsgif_redraw, nsgif_redraw_tiled, 0, 0, false}, + nsgif_redraw, nsgif_redraw_tiled, 0, 0, nsgif_clone, false}, #endif #ifdef WITH_BMP {nsbmp_create, 0, nsbmp_convert, 0, nsbmp_destroy, 0, - nsbmp_redraw, nsbmp_redraw_tiled, 0, 0, false}, + nsbmp_redraw, nsbmp_redraw_tiled, 0, 0, nsbmp_clone, false}, {nsico_create, 0, nsico_convert, 0, nsico_destroy, 0, - nsico_redraw, nsico_redraw_tiled, 0, 0, false}, + nsico_redraw, nsico_redraw_tiled, 0, 0, nsico_clone, false}, #endif #ifdef WITH_PNG {nspng_create, nspng_process_data, nspng_convert, 0, nspng_destroy, 0, nspng_redraw, nspng_redraw_tiled, - 0, 0, false}, + 0, 0, nspng_clone, false}, #else #ifdef WITH_MNG {nsmng_create, nsmng_process_data, nsmng_convert, 0, nsmng_destroy, 0, nsmng_redraw, nsmng_redraw_tiled, - 0, 0, false}, + 0, 0, nsmng_clone, false}, #endif #endif #ifdef WITH_MNG {nsmng_create, nsmng_process_data, nsmng_convert, 0, nsmng_destroy, 0, nsmng_redraw, nsmng_redraw_tiled, - 0, 0, false}, + 0, 0, nsmng_clone, false}, {nsmng_create, nsmng_process_data, nsmng_convert, 0, nsmng_destroy, 0, nsmng_redraw, nsmng_redraw_tiled, - 0, 0, false}, + 0, 0, nsmng_clone, false}, #endif #ifdef WITH_SPRITE {0, 0, sprite_convert, - 0, sprite_destroy, 0, sprite_redraw, 0, 0, 0, false}, + 0, sprite_destroy, 0, sprite_redraw, 0, + 0, 0, sprite_clone, false}, #endif #ifdef WITH_NSSPRITE {0, 0, nssprite_convert, - 0, nssprite_destroy, 0, nssprite_redraw, 0, 0, 0, false}, + 0, nssprite_destroy, 0, nssprite_redraw, 0, + 0, 0, nssprite_clone, false}, #endif #ifdef WITH_DRAW {0, 0, draw_convert, - 0, draw_destroy, 0, draw_redraw, 0, 0, 0, false}, + 0, draw_destroy, 0, draw_redraw, 0, 0, 0, draw_clone, false}, #endif #ifdef WITH_PLUGIN {plugin_create, 0, plugin_convert, plugin_reformat, plugin_destroy, 0, plugin_redraw, 0, - plugin_open, plugin_close, + plugin_open, plugin_close, plugin_clone, true}, #endif {directory_create, 0, directory_convert, - 0, directory_destroy, 0, 0, 0, 0, 0, true}, + 0, directory_destroy, 0, 0, 0, 0, 0, directory_clone, true}, #ifdef WITH_THEME_INSTALL - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, false}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, false}, #endif #ifdef WITH_ARTWORKS {0, 0, artworks_convert, - 0, artworks_destroy, 0, artworks_redraw, 0, 0, 0, false}, + 0, artworks_destroy, 0, artworks_redraw, 0, + 0, 0, artworks_clone, false}, #endif #ifdef WITH_NS_SVG {svg_create, 0, svg_convert, - svg_reformat, svg_destroy, 0, svg_redraw, 0, 0, 0, true}, + svg_reformat, svg_destroy, 0, svg_redraw, 0, + 0, 0, svg_clone, true}, #endif #ifdef WITH_RSVG {rsvg_create, rsvg_process_data, rsvg_convert, - 0, rsvg_destroy, 0, rsvg_redraw, 0, 0, 0, false}, + 0, rsvg_destroy, 0, rsvg_redraw, 0, 0, 0, rsvg_clone, false}, #endif - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, false} + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, false} }; #define HANDLER_MAP_COUNT (sizeof(handler_map) / sizeof(handler_map[0])) @@ -938,6 +943,22 @@ void content_remove_user(struct content *c, talloc_free(next); } +/** + * Count users for the content. + */ + +uint32_t content_count_users(struct content *c) +{ + struct content_user *user; + uint32_t counter = 0; + + assert(c != NULL); + + for (user = c->user_list; user != NULL; user = user->next) + counter += 1; + + return counter - 1; /* Subtract 1 for the sentinel */ +} /** * Send a message to all users. @@ -957,39 +978,6 @@ void content_broadcast(struct content *c, content_msg msg, } -/** - * Stop a content loading. - * - * May only be called in CONTENT_STATUS_READY only. If all users have requested - * stop, the loading is stopped and the content placed in CONTENT_STATUS_DONE. - */ - -void content_stop(hlcache_handle *h, - void (*callback)(struct content *c, content_msg msg, - union content_msg_data data, void *pw), - void *pw) -{ -//newcache -#if 0 - struct content_user *user; - - assert(c->status == CONTENT_STATUS_READY); - - user = content_find_user(c, callback, p1, p2); - if (!user) { - LOG(("user not found in list")); - assert(0); - return; - } - - LOG(("%p %s: stop user %p 0x%" PRIxPTR " 0x%" PRIxPTR, - c, llcache_handle_get_url(c->llcache), - callback, p1, p2)); - user->stop = true; -#endif -} - - /** * A window containing the content has been opened. * @@ -1308,6 +1296,118 @@ const llcache_handle *content_get_llcache_handle(struct content *c) return c->llcache; } +/** + * Clone a content object in its current state. + * + * \param c Content to clone + * \return Clone of \a c + */ +struct content *content_clone(struct content *c) +{ + struct content *nc = talloc_zero(0, struct content); + + if (nc == NULL) { + return NULL; + } + + if (llcache_handle_clone(c->llcache, &(nc->llcache)) != NSERROR_OK) { + content_destroy(nc); + return NULL; + } + + llcache_handle_change_callback(nc->llcache, + content_llcache_callback, nc); + + nc->type = c->type; + + if (c->mime_type != NULL) { + nc->mime_type = talloc_strdup(nc, c->mime_type); + if (nc->mime_type == NULL) { + content_destroy(nc); + return NULL; + } + } + + nc->status = c->status; + + nc->width = c->width; + nc->height = c->height; + nc->available_width = c->available_width; + nc->quirks = c->quirks; + + if (c->fallback_charset != NULL) { + nc->fallback_charset = talloc_strdup(nc, c->fallback_charset); + if (nc->fallback_charset == NULL) { + content_destroy(nc); + return NULL; + } + } + + if (c->refresh != NULL) { + nc->refresh = talloc_strdup(nc, c->refresh); + if (nc->refresh == NULL) { + content_destroy(nc); + return NULL; + } + } + + nc->fresh = c->fresh; + nc->time = c->time; + nc->reformat_time = c->reformat_time; + nc->size = c->size; + nc->talloc_size = c->talloc_size; + + if (c->title != NULL) { + nc->title = talloc_strdup(nc, c->title); + if (nc->title == NULL) { + content_destroy(nc); + return NULL; + } + } + + nc->active = c->active; + + memcpy(&(nc->status_message), &(c->status_message), 120); + memcpy(&(nc->sub_status), &(c->sub_status), 80); + + nc->locked = c->locked; + nc->total_size = c->total_size; + nc->http_code = c->http_code; + + /* Duplicate the data member (and bitmap, if appropriate) */ + if (handler_map[nc->type].clone != NULL) { + if (handler_map[nc->type].clone(c, nc) == false) { + content_destroy(nc); + return NULL; + } + } + + return nc; +} + +/** + * Abort a content object + * + * \param c The content object to abort + * \return NSERROR_OK on success, otherwise appropriate error + */ +nserror content_abort(struct content *c) +{ + LOG(("Aborting %p", c)); + + if (c->status == CONTENT_STATUS_READY) { + switch (c->type) { + case CONTENT_HTML: + html_stop(c); + break; + default: + LOG(("Unable to abort sub-parts for type %d", c->type)); + } + } + + /* And for now, abort our llcache object */ + return llcache_handle_abort(c->llcache); +} /** * Convert a content into a download diff --git a/content/content.h b/content/content.h index 1dd7f83cd..61fc6346f 100644 --- a/content/content.h +++ b/content/content.h @@ -29,6 +29,7 @@ #include #include "utils/config.h" +#include "utils/errors.h" #include "content/content_type.h" #include "desktop/plot_style.h" @@ -112,8 +113,13 @@ void content_remove_user(struct content *c, union content_msg_data data, void *pw), void *pw); +uint32_t content_count_users(struct content *c); + const struct llcache_handle *content_get_llcache_handle(struct content *c); +struct content *content_clone(struct content *c); + +nserror content_abort(struct content *c); /* Client functions */ bool content_can_reformat(struct hlcache_handle *h); @@ -129,10 +135,6 @@ bool content_redraw_tiled(struct hlcache_handle *h, int x, int y, int clip_x0, int clip_y0, int clip_x1, int clip_y1, float scale, colour background_colour, bool repeat_x, bool repeat_y); -void content_stop(struct hlcache_handle *h, - void (*callback)(struct content *c, content_msg msg, - union content_msg_data data, void *pw), - void *pw); void content_open(struct hlcache_handle *h, struct browser_window *bw, struct content *page, unsigned int index, struct box *box, struct object_params *params); diff --git a/content/hlcache.c b/content/hlcache.c index 3a55dbeb4..05a416b0b 100644 --- a/content/hlcache.c +++ b/content/hlcache.c @@ -141,6 +141,61 @@ struct content *hlcache_handle_get_content(const hlcache_handle *handle) return NULL; } +/* See hlcache.h for documentation */ +nserror hlcache_handle_abort(hlcache_handle *handle) +{ + struct hlcache_entry *entry = handle->entry; + struct content *c; + + if (entry == NULL) { + /* This handle is not yet associated with a cache entry. + * The implication is that the fetch for the handle has + * not progressed to the point where the entry can be + * created. */ + /** \todo Find retrieval context and abort llcache handle */ + + return NSERROR_OK; + } + + c = entry->content; + + if (content_count_users(c) > 1) { + /* We are not the only user of 'c' so clone it. */ + struct content *clone = content_clone(c); + + if (clone == NULL) + return NSERROR_NOMEM; + + entry = calloc(sizeof(struct hlcache_entry), 1); + + if (entry == NULL) { + content_destroy(clone); + return NSERROR_NOMEM; + } + + if (content_add_user(clone, + hlcache_content_callback, handle) == false) { + content_destroy(clone); + free(entry); + return NSERROR_NOMEM; + } + + content_remove_user(c, hlcache_content_callback, handle); + + entry->content = clone; + handle->entry = entry; + entry->prev = NULL; + entry->next = hlcache_content_list; + if (hlcache_content_list != NULL) + hlcache_content_list->prev = entry; + hlcache_content_list = entry; + + c = clone; + } + + return content_abort(c); +} + /****************************************************************************** * High-level cache internals * ******************************************************************************/ @@ -309,13 +364,14 @@ void hlcache_content_callback(struct content *c, content_msg msg, { hlcache_handle *handle = pw; hlcache_event event; - nserror error; + nserror error = NSERROR_OK; event.type = msg; event.data = data; - - error = handle->cb(handle, &event, handle->pw); + if (handle->cb != NULL) + error = handle->cb(handle, &event, handle->pw); + if (error != NSERROR_OK) LOG(("Error in callback: %d", error)); } diff --git a/content/hlcache.h b/content/hlcache.h index 7c4f05290..5ef42a301 100644 --- a/content/hlcache.h +++ b/content/hlcache.h @@ -87,6 +87,14 @@ nserror hlcache_handle_retrieve(const char *url, uint32_t flags, */ nserror hlcache_handle_release(hlcache_handle *handle); +/** + * Abort a high-level cache fetch + * + * \param handle Handle to abort + * \return NSERROR_OK on success, appropriate error otherwise + */ +nserror hlcache_handle_abort(hlcache_handle *handle); + /** * Retrieve a content object from a cache handle * diff --git a/content/llcache.c b/content/llcache.c index 49556b1fe..94df0f6e5 100644 --- a/content/llcache.c +++ b/content/llcache.c @@ -172,6 +172,9 @@ static nserror llcache_object_remove_from_list(llcache_object *object, static nserror llcache_object_notify_users(llcache_object *object); +static nserror llcache_object_snapshot(llcache_object *object, + llcache_object **snapshot); + static nserror llcache_clean(void); static nserror llcache_post_data_clone(const llcache_post_data *orig, @@ -301,6 +304,66 @@ nserror llcache_handle_release(llcache_handle *handle) return error; } +/* See llcache.h for documentation */ +nserror llcache_handle_clone(llcache_handle *handle, llcache_handle **result) +{ + nserror error; + llcache_object_user *newuser; + + error = llcache_object_user_new(handle->cb, handle->pw, &newuser); + if (error == NSERROR_OK) { + llcache_object_add_user(handle->object, newuser); + newuser->handle.state = handle->state; + *result = &newuser->handle; + } + + return error; +} + +/* See llcache.h for documentation */ +nserror llcache_handle_abort(llcache_handle *handle) +{ + llcache_object_user *user = (llcache_object_user *) handle; + llcache_object *object = handle->object, *newobject; + nserror error = NSERROR_OK; + bool all_alone = true; + + /* Determine if we are the only user */ + if (user->prev != NULL) + all_alone = false; + if (user->next != NULL) + all_alone = false; + + if (all_alone == false) { + /* We must snapshot this object */ + error = llcache_object_snapshot(object, &newobject); + if (error != NSERROR_OK) + return error; + /* Move across to the new object */ + llcache_object_remove_user(object, user); + llcache_object_add_user(newobject, user); + + /* Add new object to uncached list */ + llcache_object_add_to_list(object, &llcache_uncached_objects); + + /* And use it from now on. */ + object = newobject; + } else { + /* We're the only user, so abort any fetch in progress */ + if (object->fetch.fetch != NULL) { + fetch_abort(object->fetch.fetch); + object->fetch.fetch = NULL; + } + + object->fetch.state = LLCACHE_FETCH_COMPLETE; + + /* Invalidate cache control data */ + memset(&(object->cache), 0, sizeof(llcache_cache_control)); + } + + return error; +} + /* See llcache.h for documentation */ const char *llcache_handle_get_url(const llcache_handle *handle) { @@ -1033,8 +1096,7 @@ nserror llcache_object_notify_users(llcache_object *object) for (user = object->users; user != NULL; user = next_user) { /* Emit necessary events to bring the user up-to-date */ llcache_handle *handle = &user->handle; - llcache_fetch_state hstate = handle->state; - llcache_fetch_state objstate = object->fetch.state; + const llcache_fetch_state objstate = object->fetch.state; /* Save identity of next user in case client destroys * the user underneath us */ @@ -1042,20 +1104,22 @@ nserror llcache_object_notify_users(llcache_object *object) next_user = user->next; #ifdef LLCACHE_TRACE - if (hstate != objstate) + if (handle->state != objstate) LOG(("User %p state: %d Object state: %d", - user, hstate, objstate)); + user, handle->state, objstate)); #endif /* User: INIT, Obj: HEADERS, DATA, COMPLETE => User->HEADERS */ - if (hstate == LLCACHE_FETCH_INIT && + if (handle->state == LLCACHE_FETCH_INIT && objstate > LLCACHE_FETCH_INIT) { - hstate = LLCACHE_FETCH_HEADERS; + handle->state = LLCACHE_FETCH_HEADERS; } /* User: HEADERS, Obj: DATA, COMPLETE => User->DATA */ - if (hstate == LLCACHE_FETCH_HEADERS && + if (handle->state == LLCACHE_FETCH_HEADERS && objstate > LLCACHE_FETCH_HEADERS) { + handle->state = LLCACHE_FETCH_DATA; + /* Emit HAD_HEADERS event */ event.type = LLCACHE_EVENT_HAD_HEADERS; @@ -1069,20 +1133,21 @@ nserror llcache_object_notify_users(llcache_object *object) llcache_object_user_destroy(user); continue; } - - hstate = LLCACHE_FETCH_DATA; } /* User: DATA, Obj: DATA, COMPLETE, more source available */ - if (hstate == LLCACHE_FETCH_DATA && + if (handle->state == LLCACHE_FETCH_DATA && objstate >= LLCACHE_FETCH_DATA && object->source_len > handle->bytes) { + size_t oldbytes = handle->bytes; + + /* Update record of last byte emitted */ + handle->bytes = object->source_len; + /* Emit HAD_DATA event */ event.type = LLCACHE_EVENT_HAD_DATA; - event.data.data.buf = - object->source_data + handle->bytes; - event.data.data.len = - object->source_len - handle->bytes; + event.data.data.buf = object->source_data + oldbytes; + event.data.data.len = object->source_len - oldbytes; error = handle->cb(handle, &event, handle->pw); if (error != NSERROR_OK) { @@ -1094,14 +1159,13 @@ nserror llcache_object_notify_users(llcache_object *object) llcache_object_user_destroy(user); continue; } - - /* Update record of last byte emitted */ - handle->bytes = object->source_len; } /* User: DATA, Obj: COMPLETE => User->COMPLETE */ - if (hstate == LLCACHE_FETCH_DATA && + if (handle->state == LLCACHE_FETCH_DATA && objstate > LLCACHE_FETCH_DATA) { + handle->state = LLCACHE_FETCH_COMPLETE; + /* Emit DONE event */ event.type = LLCACHE_EVENT_DONE; @@ -1115,20 +1179,79 @@ nserror llcache_object_notify_users(llcache_object *object) llcache_object_user_destroy(user); continue; } - - hstate = LLCACHE_FETCH_COMPLETE; } /* No longer the target of an iterator */ user->iterator_target = false; - - /* Sync handle's state with reality */ - handle->state = hstate; } return NSERROR_OK; } +/** + * Make a snapshot of the current state of an llcache_object. + * + * This has the side-effect of the new object being non-cacheable, + * also not-fetching and not a candidate for any other object. + * + * Also note that this new object has no users and at least one + * should be assigned to it before llcache_clean is entered or it + * will be immediately cleaned up. + * + * \param object The object to take a snapshot of + * \param snapshot Pointer to receive snapshot of \a object + * \return NSERROR_OK on success, appropriate error otherwise + */ +nserror llcache_object_snapshot(llcache_object *object, + llcache_object **snapshot) +{ + llcache_object *newobj; + nserror error; + + error = llcache_object_new(object->url, &newobj); + + if (error != NSERROR_OK) + return error; + + newobj->has_query = object->has_query; + + newobj->source_alloc = newobj->source_len = object->source_len; + + if (object->source_len > 0) { + newobj->source_data = malloc(newobj->source_alloc); + if (newobj->source_data == NULL) { + llcache_object_destroy(newobj); + return NSERROR_NOMEM; + } + memcpy(newobj->source_data, object->source_data, newobj->source_len); + } + + if (object->num_headers > 0) { + newobj->headers = calloc(sizeof(llcache_header), object->num_headers); + if (newobj->headers == NULL) { + llcache_object_destroy(newobj); + return NSERROR_NOMEM; + } + while (newobj->num_headers < object->num_headers) { + llcache_header *nh = &(newobj->headers[newobj->num_headers]); + llcache_header *oh = &(object->headers[newobj->num_headers]); + newobj->num_headers += 1; + nh->name = strdup(oh->name); + nh->value = strdup(oh->value); + if (nh->name == NULL || nh->value == NULL) { + llcache_object_destroy(newobj); + return NSERROR_NOMEM; + } + } + } + + newobj->fetch.state = LLCACHE_FETCH_COMPLETE; + + *snapshot = newobj; + + return NSERROR_OK; +} + /** * Attempt to clean the cache * @@ -1376,16 +1499,6 @@ void llcache_fetch_callback(fetch_msg msg, void *p, const void *data, } return; } - - /* Keep users in sync with reality */ - error = llcache_object_notify_users(object); - if (error != NSERROR_OK) { - /** \todo Error handling */ - if (object->fetch.fetch != NULL) { - fetch_abort(object->fetch.fetch); - object->fetch.fetch = NULL; - } - } } /** diff --git a/content/llcache.h b/content/llcache.h index 7c3fad6de..434d0b641 100644 --- a/content/llcache.h +++ b/content/llcache.h @@ -200,6 +200,24 @@ nserror llcache_handle_change_callback(llcache_handle *handle, */ nserror llcache_handle_release(llcache_handle *handle); +/** + * Clone a low-level cache handle, producing a new handle to + * the same fetch/content. + * + * \param handle Handle to clone + * \param result Pointer to location to receive cloned handle + * \return NSERROR_OK on success, appropriate error otherwise + */ +nserror llcache_handle_clone(llcache_handle *handle, llcache_handle **result); + +/** + * Abort a low-level fetch, informing all users of this action. + * + * \param handle Handle to abort + * \return NSERROR_OK on success, appropriate error otherwise + */ +nserror llcache_handle_abort(llcache_handle *handle); + /** * Retrieve the post-redirect URL of a low-level cache object * -- cgit v1.2.3