NetSurf
Data Structures | Typedefs | Functions | Variables
hlcache.c File Reference

High-level resource cache implementation. More...

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "utils/http.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/ring.h"
#include "utils/utils.h"
#include "netsurf/inttypes.h"
#include "netsurf/misc.h"
#include "netsurf/content.h"
#include "desktop/gui_internal.h"
#include "content/mimesniff.h"
#include "content/hlcache.h"
#include "content/content_protected.h"
#include "content/content_factory.h"
Include dependency graph for hlcache.c:

Go to the source code of this file.

Data Structures

struct  hlcache_retrieval_ctx
 High-level cache retrieval context. More...
 
struct  hlcache_handle
 High-level cache handle. More...
 
struct  hlcache_entry
 Entry in high-level cache. More...
 
struct  hlcache_s
 Current state of the cache. More...
 

Typedefs

typedef struct hlcache_entry hlcache_entry
 
typedef struct hlcache_retrieval_ctx hlcache_retrieval_ctx
 

Functions

static void hlcache_clean (void *force_clean_flag)
 Attempt to clean the cache. More...
 
static bool hlcache_type_is_acceptable (lwc_string *mime_type, content_type accepted_types, content_type *computed_type)
 Determine if the specified MIME type is acceptable. More...
 
static void hlcache_content_callback (struct content *c, content_msg msg, const union content_msg_data *data, void *pw)
 Veneer between content callback API and hlcache callback API. More...
 
static nserror hlcache_find_content (hlcache_retrieval_ctx *ctx, lwc_string *effective_type)
 Find a content for the high-level cache handle. More...
 
static nserror hlcache_migrate_ctx (hlcache_retrieval_ctx *ctx, lwc_string *effective_type)
 Migrate a retrieval context into its final destination content. More...
 
static nserror hlcache_llcache_callback (llcache_handle *handle, const llcache_event *event, void *pw)
 Handler for low-level cache events. More...
 
nserror hlcache_initialise (const struct hlcache_parameters *hlcache_parameters)
 Initialise the high-level cache, preparing the llcache also. More...
 
void hlcache_stop (void)
 Stop the high-level cache periodic functionality so that the exit sequence can run. More...
 
void hlcache_finalise (void)
 Finalise the high-level cache, destroying any remaining contents. More...
 
nserror hlcache_handle_retrieve (nsurl *url, uint32_t flags, nsurl *referer, llcache_post_data *post, hlcache_handle_callback cb, void *pw, hlcache_child_context *child, content_type accepted_types, hlcache_handle **result)
 Retrieve a high-level cache handle for an object. More...
 
nserror hlcache_handle_release (hlcache_handle *handle)
 Release a high-level cache handle. More...
 
struct contenthlcache_handle_get_content (const hlcache_handle *handle)
 Retrieve a content object from a cache handle. More...
 
nserror hlcache_handle_abort (hlcache_handle *handle)
 Abort a high-level cache fetch. More...
 
nserror hlcache_handle_replace_callback (hlcache_handle *handle, hlcache_handle_callback cb, void *pw)
 Replace a high-level cache handle's callback. More...
 
nserror hlcache_handle_clone (hlcache_handle *handle, hlcache_handle **result)
 Clone a high level cache handle. More...
 
nsurlhlcache_handle_get_url (const hlcache_handle *handle)
 

Variables

static struct hlcache_shlcache = NULL
 high level cache state More...
 

Detailed Description

High-level resource cache implementation.

Definition in file hlcache.c.

Typedef Documentation

◆ hlcache_entry

typedef struct hlcache_entry hlcache_entry

Definition at line 44 of file hlcache.c.

◆ hlcache_retrieval_ctx

Definition at line 45 of file hlcache.c.

Function Documentation

◆ hlcache_clean()

static void hlcache_clean ( void *  force_clean_flag)
static

Attempt to clean the cache.

Todo:
This is over-zealous: all unused contents will be immediately destroyed. Ideally, we want to purge all unused contents that are using stale source data, and enough fresh contents such that the cache fits in the configured cache size limit.

Definition at line 111 of file hlcache.c.

References hlcache_parameters::bg_clean_time, hlcache_entry::content, content__get_status(), content_abort(), content_count_users(), content_destroy(), hlcache_s::content_list, content_set_error(), CONTENT_STATUS_LOADING, guit, hlcache, hlcache_clean(), llcache_clean(), netsurf_table::misc, hlcache_entry::next, NSLOG, hlcache_s::params, hlcache_entry::prev, and gui_misc_table::schedule.

Referenced by hlcache_clean(), hlcache_finalise(), hlcache_initialise(), and hlcache_stop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hlcache_content_callback()

static void hlcache_content_callback ( struct content c,
content_msg  msg,
const union content_msg_data data,
void *  pw 
)
static

Veneer between content callback API and hlcache callback API.

Parameters
cContent to emit message for
msgMessage to emit
dataData for message
pwPointer to private data (hlcache_handle)

Definition at line 191 of file hlcache.c.

References hlcache_handle::cb, NSERROR_OK, NSLOG, hlcache_handle::pw, and hlcache_event::type.

Referenced by hlcache_find_content(), hlcache_handle_abort(), and hlcache_handle_release().

Here is the caller graph for this function:

◆ hlcache_finalise()

void hlcache_finalise ( void  )

Finalise the high-level cache, destroying any remaining contents.

Definition at line 584 of file hlcache.c.

References hlcache_child_context::charset, hlcache_retrieval_ctx::child, hlcache_entry::content, content_count_users(), hlcache_s::content_list, guit, hlcache_retrieval_ctx::handle, hlcache_s::hit_count, hlcache, hlcache_clean(), hlcache_handle_get_url(), hlcache_retrieval_ctx::llcache, llcache_finalise(), llcache_handle_release(), netsurf_table::misc, hlcache_s::miss_count, hlcache_entry::next, NSLOG, nsurl_access(), hlcache_retrieval_ctx::r_next, hlcache_s::retrieval_ctx_ring, and gui_misc_table::schedule.

Referenced by netsurf_exit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hlcache_find_content()

static nserror hlcache_find_content ( hlcache_retrieval_ctx ctx,
lwc_string *  effective_type 
)
static

Find a content for the high-level cache handle.

Parameters
ctxHigh-level cache retrieval context
effective_typeEffective MIME type of content
Returns
NSERROR_OK on success, NSERROR_NEED_DATA on success where data is needed, appropriate error otherwise
Precondition
handle::state == HLCACHE_HANDLE_NEW
Headers must have been received for associated low-level handle
Postcondition
Low-level handle is either released, or associated with new content
High-level handle is registered with content

Definition at line 225 of file hlcache.c.

References hlcache_handle::cb, hlcache_child_context::charset, hlcache_retrieval_ctx::child, hlcache_entry::content, content_add_user(), content_factory_create_content(), content_get_llcache_handle(), content_get_status(), content_is_shareable(), hlcache_s::content_list, content_matches_quirks(), CONTENT_MSG_DONE, CONTENT_MSG_LOADING, CONTENT_MSG_READY, CONTENT_STATUS_DONE, CONTENT_STATUS_ERROR, CONTENT_STATUS_LOADING, CONTENT_STATUS_READY, hlcache_handle::entry, hlcache_retrieval_ctx::handle, hlcache_s::hit_count, hlcache, hlcache_content_callback(), hlcache_retrieval_ctx::llcache, llcache_handle_references_same_object(), llcache_handle_release(), hlcache_s::miss_count, hlcache_entry::next, NSERROR_NEED_DATA, NSERROR_NOMEM, NSERROR_OK, hlcache_entry::prev, hlcache_handle::pw, and hlcache_child_context::quirks.

Referenced by hlcache_migrate_ctx().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hlcache_handle_abort()

nserror hlcache_handle_abort ( hlcache_handle handle)

Abort a high-level cache fetch.

Parameters
handleHandle to abort
Returns
NSERROR_OK on success, appropriate error otherwise

Definition at line 786 of file hlcache.c.

References hlcache_entry::content, content_abort(), content_add_user(), content_clone(), content_count_users(), content_destroy(), hlcache_s::content_list, content_remove_user(), hlcache_handle::entry, hlcache, hlcache_content_callback(), llcache_handle_abort(), llcache_handle_release(), hlcache_entry::next, NSERROR_NOMEM, NSERROR_OK, hlcache_entry::prev, hlcache_s::retrieval_ctx_ring, RING_ITERATE_END, RING_ITERATE_START, RING_ITERATE_STOP, and RING_REMOVE.

Referenced by browser_window_destroy_internal(), browser_window_stop(), html_css_process_modified_style(), and html_object_abort_objects().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hlcache_handle_clone()

nserror hlcache_handle_clone ( hlcache_handle handle,
hlcache_handle **  result 
)

Clone a high level cache handle.

Parameters
handleThe handle to clone.
resultThe cloned handle.
Returns
NSERROR_OK on success, appropriate error otherwise

Definition at line 869 of file hlcache.c.

References NSERROR_CLONE_FAILED, and result.

Referenced by print_init().

Here is the caller graph for this function:

◆ hlcache_handle_get_content()

struct content * hlcache_handle_get_content ( const hlcache_handle handle)

Retrieve a content object from a cache handle.

Parameters
handleCache handle to dereference
Returns
Pointer to content object, or NULL if there is none
Todo:
This may not be correct. Ideally, the client should never need to directly access a content object. It may, therefore, be better to provide a bunch of veneers here that take a hlcache_handle and invoke the corresponding content_ API. If there's no content object associated with the hlcache_handle (e.g. because the source data is still being fetched, so it doesn't exist yet), then these veneers would behave as a NOP. The important thing being that the client need not care about this possibility and can just call the functions with impugnity.

Definition at line 776 of file hlcache.c.

References hlcache_entry::content, and hlcache_handle::entry.

Referenced by browser_window_callback(), content_can_reformat(), content_clear_selection(), content_close(), content_debug(), content_debug_dump(), content_drop_file_at_point(), content_exec(), content_find_rfc5988_link(), content_get_available_width(), content_get_bitmap(), content_get_contextual_content(), content_get_encoding(), content_get_height(), content_get_mime_type(), content_get_opaque(), content_get_quirks(), content_get_refresh_url(), content_get_selection(), content_get_source_data(), content_get_status(), content_get_status_message(), content_get_title(), content_get_type(), content_get_width(), content_invalidate_reuse_data(), content_is_locked(), content_keypress(), content_mouse_action(), content_mouse_track(), content_open(), content_redraw(), content_reformat(), content_request_redraw(), content_saw_insecure_objects(), content_scaled_redraw(), content_scroll_at_point(), content_textsearch(), content_textsearch_clear(), html_get_base_target(), html_get_base_url(), html_get_box_tree(), html_get_document(), html_get_frameset(), html_get_iframe(), html_get_objects(), html_get_stylesheets(), html_set_file_gadget_filename(), nscss_content_done(), nscss_get_imports(), nscss_get_stylesheet(), nscss_register_import(), and save_complete_ctx_has_content().

Here is the caller graph for this function:

◆ hlcache_handle_get_url()

nsurl * hlcache_handle_get_url ( const hlcache_handle handle)

Definition at line 876 of file hlcache.c.

References hlcache_entry::content, content_get_url(), hlcache_handle::entry, hlcache, llcache_handle_get_url(), result, hlcache_s::retrieval_ctx_ring, RING_ITERATE_END, RING_ITERATE_START, and RING_ITERATE_STOP.

Referenced by hlcache_finalise().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hlcache_handle_release()

nserror hlcache_handle_release ( hlcache_handle handle)

Release a high-level cache handle.

Parameters
handleHandle to release
Returns
NSERROR_OK on success, appropriate error otherwise

Definition at line 740 of file hlcache.c.

References hlcache_handle::cb, hlcache_entry::content, content_remove_user(), hlcache_handle::entry, hlcache, hlcache_content_callback(), llcache_handle_abort(), llcache_handle_release(), NSERROR_OK, hlcache_handle::pw, hlcache_s::retrieval_ctx_ring, RING_ITERATE_END, RING_ITERATE_START, RING_ITERATE_STOP, and RING_REMOVE.

Referenced by browser_window__handle_error(), browser_window_content_ready(), browser_window_convert_to_download(), browser_window_destroy_internal(), browser_window_favicon_callback(), browser_window_stop(), convert_script_async_cb(), convert_script_defer_cb(), convert_script_sync_cb(), default_ico_callback(), html_convert_css_callback(), html_css_free_stylesheets(), html_css_process_modified_style(), html_object_abort_objects(), html_object_callback(), html_object_free_objects(), html_object_nobox_callback(), html_replace_object(), html_script_free(), nscss_destroy_css_data(), nscss_import(), print_cleanup(), ro_gui_url_bar_fini(), search_web_finalise(), search_web_ico_callback(), theme_install_close(), and treeview_fini().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hlcache_handle_replace_callback()

nserror hlcache_handle_replace_callback ( hlcache_handle handle,
hlcache_handle_callback  cb,
void *  pw 
)

Replace a high-level cache handle's callback.

Parameters
handleHandle to replace callback of
cbNew callback routine
pwPrivate data for callback
Returns
NSERROR_OK on success, appropriate error otherwise

Definition at line 860 of file hlcache.c.

References hlcache_handle::cb, NSERROR_OK, and hlcache_handle::pw.

Referenced by theme_install_start().

Here is the caller graph for this function:

◆ hlcache_handle_retrieve()

nserror hlcache_handle_retrieve ( nsurl url,
uint32_t  flags,
nsurl referer,
llcache_post_data post,
hlcache_handle_callback  cb,
void *  pw,
hlcache_child_context child,
content_type  accepted_types,
hlcache_handle **  result 
)

Retrieve a high-level cache handle for an object.

Parameters
urlURL of the object to retrieve handle for
flagsObject retrieval flags
refererReferring URL, or NULL if none
postPOST data, or NULL for a GET request
cbCallback to handle object events
pwPointer to client-specific data for callback
childChild retrieval context, or NULL for top-level content
accepted_typesBitmap of acceptable content types
resultPointer to location to recieve cache handle
Returns
NSERROR_OK on success, appropriate error otherwise

Child contents are keyed on the tuple < URL, quirks >. The quirks field is ignored for child contents whose behaviour is not affected by quirks mode.

Todo:
The above rules should be encoded in the handler_map.
Todo:
Is there any way to sensibly reduce the number of parameters here?

Definition at line 679 of file hlcache.c.

References hlcache_retrieval_ctx::accepted_types, hlcache_handle::cb, hlcache_child_context::charset, hlcache_retrieval_ctx::child, hlcache_retrieval_ctx::flags, hlcache_retrieval_ctx::handle, hlcache, hlcache_llcache_callback(), hlcache_retrieval_ctx::llcache, llcache_handle_retrieve(), NSERROR_NOMEM, NSERROR_OK, hlcache_handle::pw, hlcache_child_context::quirks, result, hlcache_s::retrieval_ctx_ring, and RING_INSERT.

Referenced by browser_window_favicon_callback(), browser_window_update_favicon(), exec_src_script(), html_css_new_stylesheets(), html_css_process_link(), html_css_quirks_stylesheets(), html_fetch_object(), html_replace_object(), html_stylesheet_from_domnode(), navigate_internal_real(), nscss_handle_import(), ro_gui_url_bar_init(), search_web_init(), search_web_select_provider(), and treeview_init_resources().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hlcache_initialise()

nserror hlcache_initialise ( const struct hlcache_parameters hlcache_parameters)

Initialise the high-level cache, preparing the llcache also.

Parameters
hlcache_parametersSettings to initialise cache with
Returns
NSERROR_OK on success, appropriate error otherwise.

Definition at line 552 of file hlcache.c.

References hlcache_parameters::bg_clean_time, guit, hlcache, hlcache_clean(), hlcache_parameters::llcache, llcache_initialise(), netsurf_table::misc, NSERROR_NOMEM, NSERROR_OK, hlcache_s::params, and gui_misc_table::schedule.

Referenced by netsurf_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hlcache_llcache_callback()

static nserror hlcache_llcache_callback ( llcache_handle handle,
const llcache_event event,
void *  pw 
)
static

Handler for low-level cache events.

Parameters
handleHandle for which event is issued
eventEvent data
pwPointer to client-specific data
Returns
NSERROR_OK on success, appropriate error otherwise

Definition at line 429 of file hlcache.c.

References hlcache_retrieval_ctx::accepted_types, llcache_event::buf, hlcache_handle::cb, content_msg_data::chain, CONTENT_IMAGE, CONTENT_MSG_ERROR, CONTENT_MSG_REDIRECT, CONTENT_MSG_SSL_CERTS, hlcache_event::data, llcache_event::data, content_msg_data::errorcode, content_msg_data::errordata, content_msg_data::errormsg, hlcache_retrieval_ctx::flags, content_msg_data::from, hlcache_retrieval_ctx::handle, hlcache_migrate_ctx(), HLCACHE_RETRIEVE_SNIFF_TYPE, llcache_event::len, hlcache_retrieval_ctx::llcache, LLCACHE_EVENT_DONE, LLCACHE_EVENT_ERROR, LLCACHE_EVENT_GOT_CERTS, LLCACHE_EVENT_HAD_DATA, LLCACHE_EVENT_HAD_HEADERS, LLCACHE_EVENT_PROGRESS, LLCACHE_EVENT_REDIRECT, llcache_handle_get_header(), mimesniff_compute_effective_type(), NSERROR_NEED_DATA, NSERROR_NOT_FOUND, NSERROR_OK, hlcache_handle::pw, content_msg_data::redirect, content_msg_data::to, hlcache_event::type, and llcache_event::type.

Referenced by hlcache_handle_retrieve().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hlcache_migrate_ctx()

static nserror hlcache_migrate_ctx ( hlcache_retrieval_ctx ctx,
lwc_string *  effective_type 
)
static

Migrate a retrieval context into its final destination content.

Parameters
ctxContext to migrate
effective_typeThe effective MIME type of the content, or NULL
Returns
NSERROR_OK on success, NSERROR_NEED_DATA on success where data is needed, appropriate error otherwise

Definition at line 348 of file hlcache.c.

References hlcache_retrieval_ctx::accepted_types, hlcache_handle::cb, hlcache_child_context::charset, hlcache_retrieval_ctx::child, CONTENT_MSG_DOWNLOAD, CONTENT_MSG_ERROR, CONTENT_NONE, hlcache_event::data, content_msg_data::download, content_msg_data::errorcode, content_msg_data::errordata, content_msg_data::errormsg, hlcache_retrieval_ctx::flags, hlcache_retrieval_ctx::handle, hlcache, hlcache_find_content(), HLCACHE_RETRIEVE_MAY_DOWNLOAD, hlcache_type_is_acceptable(), hlcache_retrieval_ctx::llcache, llcache_handle_abort(), llcache_handle_force_stream(), llcache_handle_release(), messages_get(), hlcache_retrieval_ctx::migrate_target, NSERROR_NEED_DATA, NSERROR_OK, NSERROR_UNKNOWN, hlcache_handle::pw, hlcache_s::retrieval_ctx_ring, RING_REMOVE, hlcache_event::type, and type.

Referenced by hlcache_llcache_callback().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hlcache_stop()

void hlcache_stop ( void  )

Stop the high-level cache periodic functionality so that the exit sequence can run.

Definition at line 577 of file hlcache.c.

References guit, hlcache_clean(), netsurf_table::misc, and gui_misc_table::schedule.

Referenced by netsurf_exit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hlcache_type_is_acceptable()

static bool hlcache_type_is_acceptable ( lwc_string *  mime_type,
content_type  accepted_types,
content_type computed_type 
)
static

Determine if the specified MIME type is acceptable.

Parameters
mime_typeMIME type to consider
accepted_typesArray of acceptable types, or NULL for any
computed_typePointer to location to receive computed type of object
Returns
True if the type is acceptable, false otherwise

Definition at line 171 of file hlcache.c.

References content_factory_type_from_mime_type(), and type.

Referenced by hlcache_migrate_ctx().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ hlcache

struct hlcache_s* hlcache = NULL
static