From 3d71da088b83c2cc5741dfdc8f619fbf2bba55a2 Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Thu, 15 Sep 2011 22:31:16 +0000 Subject: [PATCH] Clean up content_factory_register_handler API -- content handlers no longer use the mime type, so don't require it to be interned up front svn path=/trunk/netsurf/; revision=12796 --- amiga/dt_anim.c | 3 ++- amiga/dt_picture.c | 3 ++- amiga/dt_sound.c | 3 ++- amiga/icon.c | 43 ++---------------------------- amiga/plugin_hack.c | 3 ++- cocoa/apple_image.m | 8 +----- content/content_factory.c | 14 +++++++--- content/content_factory.h | 26 +++--------------- css/css.c | 21 ++++++--------- render/html.c | 24 ++++------------- render/textplain.c | 23 +++++++--------- riscos/content-handlers/artworks.c | 42 ++--------------------------- riscos/content-handlers/draw.c | 41 +--------------------------- riscos/content-handlers/sprite.c | 41 +--------------------------- 14 files changed, 53 insertions(+), 242 deletions(-) diff --git a/amiga/dt_anim.c b/amiga/dt_anim.c index aa2f4b2d2..478aa149b 100644 --- a/amiga/dt_anim.c +++ b/amiga/dt_anim.c @@ -116,7 +116,8 @@ nserror amiga_dt_anim_init(void) if(node) { - error = content_factory_register_handler(type, + error = content_factory_register_handler( + lwc_string_data(type), &amiga_dt_anim_content_handler); if (error != NSERROR_OK) diff --git a/amiga/dt_picture.c b/amiga/dt_picture.c index f373fcdbf..304506b06 100644 --- a/amiga/dt_picture.c +++ b/amiga/dt_picture.c @@ -103,7 +103,8 @@ nserror amiga_dt_picture_init(void) if(node) { - error = content_factory_register_handler(type, + error = content_factory_register_handler( + lwc_string_data(type), &amiga_dt_picture_content_handler); if (error != NSERROR_OK) diff --git a/amiga/dt_sound.c b/amiga/dt_sound.c index 2f661a688..c4c8e99a1 100644 --- a/amiga/dt_sound.c +++ b/amiga/dt_sound.c @@ -100,7 +100,8 @@ nserror amiga_dt_sound_init(void) if(node) { - error = content_factory_register_handler(type, + error = content_factory_register_handler( + lwc_string_data(type), &amiga_dt_sound_content_handler); if (error != NSERROR_OK) diff --git a/amiga/icon.c b/amiga/icon.c index c05950e92..4fd1b2fc8 100644 --- a/amiga/icon.c +++ b/amiga/icon.c @@ -97,47 +97,8 @@ static const char *amiga_icon_types[] = { "image/x-amiga-icon" }; -static lwc_string *amiga_icon_mime_types[NOF_ELEMENTS(amiga_icon_types)]; - -nserror amiga_icon_init(void) -{ - uint32_t i; - lwc_error lerror; - nserror error; - - for (i = 0; i < NOF_ELEMENTS(amiga_icon_mime_types); i++) { - lerror = lwc_intern_string(amiga_icon_types[i], - strlen(amiga_icon_types[i]), - &amiga_icon_mime_types[i]); - if (lerror != lwc_error_ok) { - error = NSERROR_NOMEM; - goto error; - } - - error = content_factory_register_handler( - amiga_icon_mime_types[i], - &amiga_icon_content_handler); - if (error != NSERROR_OK) - goto error; - } - - return NSERROR_OK; - -error: - amiga_icon_fini(); - - return error; -} - -void amiga_icon_fini(void) -{ - uint32_t i; - - for (i = 0; i < NOF_ELEMENTS(amiga_icon_mime_types); i++) { - if (amiga_icon_mime_types[i] != NULL) - lwc_string_unref(amiga_icon_mime_types[i]); - } -} +CONTENT_FACTORY_REGISTER_TYPES(amiga_icon, amiga_icon_types, + amiga_icon_content_handler) nserror amiga_icon_create(const content_handler *handler, lwc_string *imime_type, const http_parameter *params, diff --git a/amiga/plugin_hack.c b/amiga/plugin_hack.c index 7e6cbcb4e..9159cddb0 100644 --- a/amiga/plugin_hack.c +++ b/amiga/plugin_hack.c @@ -81,7 +81,8 @@ nserror amiga_plugin_hack_init(void) { LOG(("plugin_hack registered %s\n",lwc_string_data(type))); - error = content_factory_register_handler(type, + error = content_factory_register_handler( + lwc_string_data(type), &amiga_plugin_hack_content_handler); if (error != NSERROR_OK) diff --git a/cocoa/apple_image.m b/cocoa/apple_image.m index f88e6cc7e..f38970234 100644 --- a/cocoa/apple_image.m +++ b/cocoa/apple_image.m @@ -77,13 +77,7 @@ static nserror register_for_type( NSString *mime ) return NSERROR_OK; #endif - lwc_string *string = NULL; - lwc_error lerror = lwc_intern_string( type, strlen( type ), &string ); - if (lerror != lwc_error_ok) return NSERROR_NOMEM; - - nserror error = content_factory_register_handler( string, &apple_image_content_handler ); - lwc_string_unref( string ); - + nserror error = content_factory_register_handler( type, &apple_image_content_handler ); if (error != NSERROR_OK) return error; return NSERROR_OK; diff --git a/content/content_factory.c b/content/content_factory.c index 8baa20f7a..3f06dc432 100644 --- a/content/content_factory.c +++ b/content/content_factory.c @@ -71,14 +71,20 @@ void content_factory_fini(void) * * \note Latest registration for a MIME type wins */ -nserror content_factory_register_handler(lwc_string *mime_type, +nserror content_factory_register_handler(const char *mime_type, const content_handler *handler) { + lwc_string *imime_type; + lwc_error lerror; content_handler_entry *entry; bool match; + lerror = lwc_intern_string(mime_type, strlen(mime_type), &imime_type); + if (lerror != lwc_error_ok) + return NSERROR_NOMEM; + for (entry = content_handlers; entry != NULL; entry = entry->next) { - if (lwc_string_caseless_isequal(mime_type, entry->mime_type, + if (lwc_string_caseless_isequal(imime_type, entry->mime_type, &match) == lwc_error_ok && match) break; } @@ -91,7 +97,9 @@ nserror content_factory_register_handler(lwc_string *mime_type, entry->next = content_handlers; content_handlers = entry; - entry->mime_type = lwc_string_ref(mime_type); + entry->mime_type = imime_type; + } else { + lwc_string_unref(imime_type); } entry->handler = handler; diff --git a/content/content_factory.h b/content/content_factory.h index b383f461b..26c587575 100644 --- a/content/content_factory.h +++ b/content/content_factory.h @@ -28,25 +28,14 @@ #define CONTENT_FACTORY_REGISTER_TYPES(HNAME, HTYPELIST, HHANDLER) \ \ -static lwc_string *HNAME##_mime_types[NOF_ELEMENTS(HTYPELIST)]; \ - \ nserror HNAME##_init(void) \ { \ uint32_t i; \ - lwc_error lerror; \ nserror error; \ \ - for (i = 0; i < NOF_ELEMENTS(HNAME##_mime_types); i++) { \ - lerror = lwc_intern_string(HTYPELIST[i], \ - strlen(HTYPELIST[i]), \ - &HNAME##_mime_types[i]); \ - if (lerror != lwc_error_ok) { \ - error = NSERROR_NOMEM; \ - goto error; \ - } \ - \ + for (i = 0; i < NOF_ELEMENTS(HTYPELIST); i++) { \ error = content_factory_register_handler( \ - HNAME##_mime_types[i], \ + HTYPELIST[i], \ &HHANDLER); \ if (error != NSERROR_OK) \ goto error; \ @@ -59,16 +48,9 @@ error: \ \ return error; \ } \ - \ +/* Pointless */ \ void HNAME##_fini(void) \ { \ - uint32_t i; \ - \ - for (i = 0; i < NOF_ELEMENTS(HNAME##_mime_types); i++) { \ - if (HNAME##_mime_types[i] != NULL) { \ - lwc_string_unref(HNAME##_mime_types[i]); \ - } \ - } \ } struct content; @@ -78,7 +60,7 @@ typedef struct content_handler content_handler; void content_factory_fini(void); -nserror content_factory_register_handler(lwc_string *mime_type, +nserror content_factory_register_handler(const char *mime_type, const content_handler *handler); struct content *content_factory_create_content(struct llcache_handle *llcache, diff --git a/css/css.c b/css/css.c index 61f0b2e7e..06dba6133 100644 --- a/css/css.c +++ b/css/css.c @@ -89,7 +89,6 @@ static const content_handler css_content_handler = { .no_share = false, }; -static lwc_string *css_mime_type; static lwc_string *css_charset; static css_stylesheet *blank_import; @@ -101,22 +100,15 @@ nserror css_init(void) lwc_error lerror; nserror error; - lerror = lwc_intern_string("text/css", SLEN("text/css"), - &css_mime_type); - if (lerror != lwc_error_ok) - return NSERROR_NOMEM; - lerror = lwc_intern_string("charset", SLEN("charset"), &css_charset); if (lerror != lwc_error_ok) { - lwc_string_unref(css_mime_type); return NSERROR_NOMEM; } - error = content_factory_register_handler(css_mime_type, + error = content_factory_register_handler("text/css", &css_content_handler); if (error != NSERROR_OK) { lwc_string_unref(css_charset); - lwc_string_unref(css_mime_type); } return error; @@ -127,12 +119,15 @@ nserror css_init(void) */ void css_fini(void) { - lwc_string_unref(css_charset); + if (css_charset != NULL) { + lwc_string_unref(css_charset); + css_charset = NULL; + } - lwc_string_unref(css_mime_type); - - if (blank_import != NULL) + if (blank_import != NULL) { css_stylesheet_destroy(blank_import); + blank_import = NULL; + } } /** diff --git a/render/html.c b/render/html.c index 13f8d84b5..5378480c5 100644 --- a/render/html.c +++ b/render/html.c @@ -145,7 +145,6 @@ static const char *html_types[] = { "text/html" }; -static lwc_string *html_mime_types[NOF_ELEMENTS(html_types)]; static lwc_string *html_charset; nserror html_init(void) @@ -160,16 +159,8 @@ nserror html_init(void) goto error; } - for (i = 0; i < NOF_ELEMENTS(html_mime_types); i++) { - lerror = lwc_intern_string(html_types[i], - strlen(html_types[i]), - &html_mime_types[i]); - if (lerror != lwc_error_ok) { - error = NSERROR_NOMEM; - goto error; - } - - error = content_factory_register_handler(html_mime_types[i], + for (i = 0; i < NOF_ELEMENTS(html_types); i++) { + error = content_factory_register_handler(html_types[i], &html_content_handler); if (error != NSERROR_OK) goto error; @@ -185,15 +176,10 @@ error: void html_fini(void) { - uint32_t i; - - for (i = 0; i < NOF_ELEMENTS(html_mime_types); i++) { - if (html_mime_types[i] != NULL) - lwc_string_unref(html_mime_types[i]); - } - - if (html_charset != NULL) + if (html_charset != NULL) { lwc_string_unref(html_charset); + html_charset = NULL; + } } /** diff --git a/render/textplain.c b/render/textplain.c index 1b1876786..786fe21e0 100644 --- a/render/textplain.c +++ b/render/textplain.c @@ -148,7 +148,6 @@ static const content_handler textplain_content_handler = { .no_share = true, }; -static lwc_string *textplain_mime_type; static lwc_string *textplain_charset; static lwc_string *textplain_default_charset; @@ -160,15 +159,9 @@ nserror textplain_init(void) lwc_error lerror; nserror error; - lerror = lwc_intern_string("text/plain", SLEN("text/plain"), - &textplain_mime_type); - if (lerror != lwc_error_ok) - return NSERROR_NOMEM; - lerror = lwc_intern_string("charset", SLEN("charset"), &textplain_charset); if (lerror != lwc_error_ok) { - lwc_string_unref(textplain_mime_type); return NSERROR_NOMEM; } @@ -176,16 +169,14 @@ nserror textplain_init(void) &textplain_default_charset); if (lerror != lwc_error_ok) { lwc_string_unref(textplain_charset); - lwc_string_unref(textplain_mime_type); return NSERROR_NOMEM; } - error = content_factory_register_handler(textplain_mime_type, + error = content_factory_register_handler("text/plain", &textplain_content_handler); if (error != NSERROR_OK) { lwc_string_unref(textplain_default_charset); lwc_string_unref(textplain_charset); - lwc_string_unref(textplain_mime_type); } return error; @@ -196,9 +187,15 @@ nserror textplain_init(void) */ void textplain_fini(void) { - lwc_string_unref(textplain_default_charset); - lwc_string_unref(textplain_charset); - lwc_string_unref(textplain_mime_type); + if (textplain_default_charset != NULL) { + lwc_string_unref(textplain_default_charset); + textplain_default_charset = NULL; + } + + if (textplain_charset != NULL) { + lwc_string_unref(textplain_charset); + textplain_charset = NULL; + } } /** diff --git a/riscos/content-handlers/artworks.c b/riscos/content-handlers/artworks.c index a669dce14..b9c5f3378 100644 --- a/riscos/content-handlers/artworks.c +++ b/riscos/content-handlers/artworks.c @@ -129,46 +129,8 @@ static const char *artworks_types[] = { "image/x-artworks" }; -static lwc_string *artworks_mime_types[NOF_ELEMENTS(artworks_types)]; - -nserror artworks_init(void) -{ - uint32_t i; - lwc_error lerror; - nserror error; - - for (i = 0; i < NOF_ELEMENTS(artworks_mime_types); i++) { - lerror = lwc_intern_string(artworks_types[i], - strlen(artworks_types[i]), - &artworks_mime_types[i]); - if (lerror != lwc_error_ok) { - error = NSERROR_NOMEM; - goto error; - } - - error = content_factory_register_handler(artworks_mime_types[i], - &artworks_content_handler); - if (error != NSERROR_OK) - goto error; - } - - return NSERROR_OK; - -error: - artworks_fini(); - - return error; -} - -void artworks_fini(void) -{ - uint32_t i; - - for (i = 0; i < NOF_ELEMENTS(artworks_mime_types); i++) { - if (artworks_mime_types[i] != NULL) - lwc_string_unref(artworks_mime_types[i]); - } -} +CONTENT_FACTORY_REGISTER_TYPES(artworks, artworks_types, + artworks_content_handler) nserror artworks_create(const content_handler *handler, lwc_string *imime_type, const http_parameter *params, diff --git a/riscos/content-handlers/draw.c b/riscos/content-handlers/draw.c index 536ee0516..0d8c86914 100644 --- a/riscos/content-handlers/draw.c +++ b/riscos/content-handlers/draw.c @@ -72,46 +72,7 @@ static const char *draw_types[] = { "image/x-drawfile" }; -static lwc_string *draw_mime_types[NOF_ELEMENTS(draw_types)]; - -nserror draw_init(void) -{ - uint32_t i; - lwc_error lerror; - nserror error; - - for (i = 0; i < NOF_ELEMENTS(draw_mime_types); i++) { - lerror = lwc_intern_string(draw_types[i], - strlen(draw_types[i]), - &draw_mime_types[i]); - if (lerror != lwc_error_ok) { - error = NSERROR_NOMEM; - goto error; - } - - error = content_factory_register_handler(draw_mime_types[i], - &draw_content_handler); - if (error != NSERROR_OK) - goto error; - } - - return NSERROR_OK; - -error: - draw_fini(); - - return error; -} - -void draw_fini(void) -{ - uint32_t i; - - for (i = 0; i < NOF_ELEMENTS(draw_mime_types); i++) { - if (draw_mime_types[i] != NULL) - lwc_string_unref(draw_mime_types[i]); - } -} +CONTENT_FACTORY_REGISTER_TYPES(draw, draw_types, draw_content_handler) nserror draw_create(const content_handler *handler, lwc_string *imime_type, const http_parameter *params, diff --git a/riscos/content-handlers/sprite.c b/riscos/content-handlers/sprite.c index f93deaa60..7b6045cb9 100644 --- a/riscos/content-handlers/sprite.c +++ b/riscos/content-handlers/sprite.c @@ -71,46 +71,7 @@ static const char *sprite_types[] = { "image/x-riscos-sprite" }; -static lwc_string *sprite_mime_types[NOF_ELEMENTS(sprite_types)]; - -nserror sprite_init(void) -{ - uint32_t i; - lwc_error lerror; - nserror error; - - for (i = 0; i < NOF_ELEMENTS(sprite_mime_types); i++) { - lerror = lwc_intern_string(sprite_types[i], - strlen(sprite_types[i]), - &sprite_mime_types[i]); - if (lerror != lwc_error_ok) { - error = NSERROR_NOMEM; - goto error; - } - - error = content_factory_register_handler(sprite_mime_types[i], - &sprite_content_handler); - if (error != NSERROR_OK) - goto error; - } - - return NSERROR_OK; - -error: - sprite_fini(); - - return error; -} - -void sprite_fini(void) -{ - uint32_t i; - - for (i = 0; i < NOF_ELEMENTS(sprite_mime_types); i++) { - if (sprite_mime_types[i] != NULL) - lwc_string_unref(sprite_mime_types[i]); - } -} +CONTENT_FACTORY_REGISTER_TYPES(sprite, sprite_types, sprite_content_handler) nserror sprite_create(const content_handler *handler, lwc_string *imime_type, const http_parameter *params,