Reload all rulesets instead of re-downloading one

This commit is contained in:
Alexander Butenko 2011-12-18 10:52:31 -04:00 committed by Christian Dywan
parent b850dd3da2
commit fe6acc509a

View file

@ -35,12 +35,12 @@
#define adblock_debug(dmsg, darg1, darg2) /* nothing */ #define adblock_debug(dmsg, darg1, darg2) /* nothing */
#endif #endif
static GHashTable* pattern; static GHashTable* pattern = NULL;
static GHashTable* keys; static GHashTable* keys = NULL;
static GHashTable* optslist; static GHashTable* optslist = NULL;
static GHashTable* urlcache; static GHashTable* urlcache = NULL;
static GHashTable* blockcssprivate; static GHashTable* blockcssprivate = NULL;
static GString* blockcss; static GString* blockcss = NULL;
#ifdef G_ENABLE_DEBUG #ifdef G_ENABLE_DEBUG
static guint debug; static guint debug;
#endif #endif
@ -93,10 +93,8 @@ adblock_build_js (const gchar* uri)
while (cnt >= 0) while (cnt >= 0)
{ {
g_string_prepend (subdomain, subdomains[cnt]); g_string_prepend (subdomain, subdomains[cnt]);
/* g_debug ("checking %s", subdomain->str); */
if ((block = g_hash_table_lookup (blockcssprivate, subdomain->str))) if ((block = g_hash_table_lookup (blockcssprivate, subdomain->str)))
{ {
/* g_debug ("found"); */
g_string_append (code, block); g_string_append (code, block);
g_string_append_c (code, ','); g_string_append_c (code, ',');
blockscnt++; blockscnt++;
@ -130,6 +128,23 @@ static GString*
adblock_fixup_regexp (const gchar* prefix, adblock_fixup_regexp (const gchar* prefix,
gchar* src); gchar* src);
static void
adblock_destroy_db ()
{
if (blockcss)
g_string_free (blockcss, TRUE);
blockcss = NULL;
g_hash_table_destroy (pattern);
pattern = NULL;
g_hash_table_destroy (optslist);
optslist = NULL;
g_hash_table_destroy (urlcache);
urlcache = NULL;
g_hash_table_destroy (blockcssprivate);
blockcssprivate = NULL;
}
static void static void
adblock_init_db () adblock_init_db ()
{ {
@ -159,22 +174,9 @@ adblock_download_notify_status_cb (WebKitDownload* download,
GParamSpec* pspec, GParamSpec* pspec,
MidoriExtension* extension) MidoriExtension* extension)
{ {
gchar* path;
MidoriApp* app;
MidoriWebSettings* settings;
if (webkit_download_get_status (download) != WEBKIT_DOWNLOAD_STATUS_FINISHED) if (webkit_download_get_status (download) != WEBKIT_DOWNLOAD_STATUS_FINISHED)
return; return;
adblock_reload_rules (extension, FALSE);
path = g_filename_from_uri (webkit_download_get_destination_uri (download), NULL, NULL);
adblock_parse_file (path);
g_free (path);
app = midori_extension_get_app (extension);
settings = katze_object_get_object (app, "settings");
g_string_append (blockcss, " {display: none !important}\n");
midori_web_settings_add_style (settings, "adblock-blockcss", blockcss->str);
g_object_unref (settings);
} }
static gchar* static gchar*
@ -213,9 +215,9 @@ adblock_reload_rules (MidoriExtension* extension,
MidoriApp* app = midori_extension_get_app (extension); MidoriApp* app = midori_extension_get_app (extension);
MidoriWebSettings* settings = katze_object_get_object (app, "settings"); MidoriWebSettings* settings = katze_object_get_object (app, "settings");
/* g_test_timer_start (); */ if (pattern)
adblock_destroy_db ();
adblock_init_db (); adblock_init_db ();
/* g_debug ("match: %f%s", g_test_timer_elapsed (), "seconds"); */
custom_list = g_build_filename (midori_extension_get_config_dir (extension), custom_list = g_build_filename (midori_extension_get_config_dir (extension),
CUSTOM_LIST_NAME, NULL); CUSTOM_LIST_NAME, NULL);
@ -1381,16 +1383,13 @@ adblock_parse_file (gchar* path)
FILE* file; FILE* file;
gchar line[2000]; gchar line[2000];
/* G_ENABLE_DEBUG g_test_timer_start (); */
if ((file = g_fopen (path, "r"))) if ((file = g_fopen (path, "r")))
{ {
while (fgets (line, 2000, file)) while (fgets (line, 2000, file))
adblock_parse_line (line); adblock_parse_line (line);
fclose (file); fclose (file);
/* g_debug ("match: %f%s %s", g_test_timer_elapsed (), "seconds", path); */
return TRUE; return TRUE;
} }
/* g_debug ("match: %f%s %s", g_test_timer_elapsed (), "seconds", path); */
return FALSE; return FALSE;
} }
@ -1431,15 +1430,8 @@ adblock_deactivate_cb (MidoriExtension* extension,
browser, adblock_add_tab_cb, extension); browser, adblock_add_tab_cb, extension);
midori_browser_foreach (browser, (GtkCallback)adblock_deactivate_tabs, browser); midori_browser_foreach (browser, (GtkCallback)adblock_deactivate_tabs, browser);
if (blockcss) adblock_destroy_db ();
g_string_free (blockcss, TRUE);
midori_web_settings_remove_style (settings, "adblock-blockcss"); midori_web_settings_remove_style (settings, "adblock-blockcss");
blockcss = NULL;
g_hash_table_destroy (pattern);
g_hash_table_destroy (optslist);
g_hash_table_destroy (urlcache);
g_hash_table_destroy (blockcssprivate);
g_object_unref (settings); g_object_unref (settings);
} }