Simplify meta block parsing and support Glib < 2.14
This commit is contained in:
parent
630e88d9af
commit
bc85edacdf
1 changed files with 90 additions and 63 deletions
|
@ -165,21 +165,15 @@ midori_addons_init (MidoriAddons* addons)
|
||||||
gtk_box_pack_start (GTK_BOX (addons), addons->treeview, TRUE, TRUE, 0);
|
gtk_box_pack_start (GTK_BOX (addons), addons->treeview, TRUE, TRUE, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define HAVE_GREGEX GLIB_CHECK_VERSION (2, 14, 0)
|
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
_include_exclude_from_file (const gchar* filename,
|
_include_exclude_from_file (const gchar* filename,
|
||||||
GSList** includes,
|
GSList** includes,
|
||||||
GSList** excludes)
|
GSList** excludes)
|
||||||
{
|
{
|
||||||
GIOChannel* channel;
|
GIOChannel* channel;
|
||||||
gchar* line;
|
|
||||||
gboolean found_meta;
|
gboolean found_meta;
|
||||||
#if HAVE_GREGEX
|
gchar* line;
|
||||||
GRegex* meta_re;
|
gchar* rest_of_line;
|
||||||
GMatchInfo* match_info;
|
|
||||||
#endif
|
|
||||||
gchar* meta_name;
|
|
||||||
|
|
||||||
if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
|
if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@ -189,79 +183,50 @@ _include_exclude_from_file (const gchar* filename,
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
found_meta = FALSE;
|
found_meta = FALSE;
|
||||||
/* FIXME: Implement this without GRegex for Glib < 2.14 */
|
|
||||||
#if HAVE_GREGEX
|
|
||||||
meta_re = g_regex_new (
|
|
||||||
"//[[:space:]]+@([^[:space:]]+)[[:space:]]+([^[:space:]]+)[[:space:]]*",
|
|
||||||
G_REGEX_OPTIMIZE, 0, NULL);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
while (g_io_channel_read_line (channel, &line, NULL, NULL, NULL)
|
while (g_io_channel_read_line (channel, &line, NULL, NULL, NULL)
|
||||||
== G_IO_STATUS_NORMAL)
|
== G_IO_STATUS_NORMAL)
|
||||||
{
|
{
|
||||||
if (g_str_has_prefix (line, "// ==UserScript=="))
|
if (g_str_has_prefix (line, "// ==UserScript=="))
|
||||||
{
|
|
||||||
found_meta = TRUE;
|
found_meta = TRUE;
|
||||||
g_free (line);
|
else if (found_meta)
|
||||||
continue;
|
|
||||||
}
|
|
||||||
#if HAVE_GREGEX
|
|
||||||
match_info = NULL;
|
|
||||||
#endif
|
|
||||||
meta_name = NULL;
|
|
||||||
if (found_meta)
|
|
||||||
{
|
{
|
||||||
if (g_str_has_prefix (line, "// ==/UserScript=="))
|
if (g_str_has_prefix (line, "// ==/UserScript=="))
|
||||||
{
|
|
||||||
found_meta = FALSE;
|
found_meta = FALSE;
|
||||||
g_free (line);
|
else if (g_str_has_prefix (line, "// @require") ||
|
||||||
continue;
|
g_str_has_prefix (line, "// @resource"))
|
||||||
}
|
|
||||||
#if HAVE_GREGEX
|
|
||||||
if (g_regex_match (meta_re, line, 0, &match_info))
|
|
||||||
{
|
{
|
||||||
meta_name = g_match_info_fetch (match_info, 1);
|
|
||||||
if (!strcmp (meta_name, "require") ||
|
|
||||||
!strcmp (meta_name, "resource"))
|
|
||||||
{
|
|
||||||
/* We don't support these, so abort here */
|
/* We don't support these, so abort here */
|
||||||
g_match_info_free (match_info);
|
|
||||||
g_free (meta_name);
|
|
||||||
g_free (line);
|
g_free (line);
|
||||||
g_io_channel_shutdown (channel, false, 0);
|
g_io_channel_shutdown (channel, false, 0);
|
||||||
g_regex_unref (meta_re);
|
|
||||||
g_slist_free (*includes);
|
g_slist_free (*includes);
|
||||||
g_slist_free (*excludes);
|
g_slist_free (*excludes);
|
||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
else if (!strcmp (meta_name, "include"))
|
else if (g_str_has_prefix (line, "// @include"))
|
||||||
{
|
{
|
||||||
*includes = g_slist_prepend (*includes,
|
rest_of_line = g_strdup (line + strlen ("// @include"));
|
||||||
g_match_info_fetch (match_info, 2));
|
rest_of_line = g_strstrip (rest_of_line);
|
||||||
}
|
*includes = g_slist_prepend (*includes, rest_of_line);
|
||||||
else if (!strcmp (meta_name, "exclude"))
|
}
|
||||||
{
|
else if (g_str_has_prefix (line, "// @exclude"))
|
||||||
*excludes = g_slist_prepend (*excludes,
|
{
|
||||||
g_match_info_fetch (match_info, 2));
|
rest_of_line = g_strdup (line + strlen ("// @exclude"));
|
||||||
}
|
rest_of_line = g_strstrip (rest_of_line);
|
||||||
}
|
*excludes = g_slist_prepend (*excludes, rest_of_line);
|
||||||
#endif
|
}
|
||||||
}
|
}
|
||||||
#if HAVE_GREGEX
|
|
||||||
g_match_info_free (match_info);
|
|
||||||
#endif
|
|
||||||
g_free (meta_name);
|
|
||||||
g_free (line);
|
g_free (line);
|
||||||
}
|
}
|
||||||
g_io_channel_shutdown (channel, false, 0);
|
g_io_channel_shutdown (channel, false, 0);
|
||||||
g_io_channel_unref (channel);
|
g_io_channel_unref (channel);
|
||||||
#if HAVE_GREGEX
|
|
||||||
g_regex_unref (meta_re);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define HAVE_GREGEX GLIB_CHECK_VERSION (2, 14, 0)
|
||||||
|
|
||||||
|
#if HAVE_GREGEX
|
||||||
static gchar*
|
static gchar*
|
||||||
_convert_to_simple_regexp (const gchar* pattern)
|
_convert_to_simple_regexp (const gchar* pattern)
|
||||||
{
|
{
|
||||||
|
@ -313,6 +278,35 @@ _convert_to_simple_regexp (const gchar* pattern)
|
||||||
}
|
}
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
static bool
|
||||||
|
_match_with_wildcard (const gchar* str,
|
||||||
|
const gchar* pattern)
|
||||||
|
{
|
||||||
|
gchar** parts;
|
||||||
|
gchar** parts_ref;
|
||||||
|
const gchar* subpart;
|
||||||
|
gchar* newsubpart;
|
||||||
|
|
||||||
|
parts = g_strsplit (pattern, "*", 0);
|
||||||
|
parts_ref = parts;
|
||||||
|
subpart = str;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
newsubpart = g_strstr_len (subpart, strlen (subpart), *parts);
|
||||||
|
if (!newsubpart)
|
||||||
|
{
|
||||||
|
g_strfreev (parts_ref);
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
subpart = newsubpart + strlen (*parts);
|
||||||
|
parts++;
|
||||||
|
}
|
||||||
|
while (*parts);
|
||||||
|
g_strfreev (parts_ref);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
_may_load_script (const gchar* uri,
|
_may_load_script (const gchar* uri,
|
||||||
|
@ -321,7 +315,12 @@ _may_load_script (const gchar* uri,
|
||||||
{
|
{
|
||||||
gboolean match;
|
gboolean match;
|
||||||
GSList* list;
|
GSList* list;
|
||||||
|
#if HAVE_GREGEX
|
||||||
gchar* re;
|
gchar* re;
|
||||||
|
#else
|
||||||
|
guint uri_len;
|
||||||
|
guint pattern_len;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (*includes)
|
if (*includes)
|
||||||
match = FALSE;
|
match = FALSE;
|
||||||
|
@ -329,17 +328,32 @@ _may_load_script (const gchar* uri,
|
||||||
match = TRUE;
|
match = TRUE;
|
||||||
|
|
||||||
list = *includes;
|
list = *includes;
|
||||||
|
#if !HAVE_GREGEX
|
||||||
|
uri_len = strlen (uri);
|
||||||
|
#endif
|
||||||
while (list)
|
while (list)
|
||||||
{
|
{
|
||||||
re = _convert_to_simple_regexp (list->data);
|
|
||||||
#if HAVE_GREGEX
|
#if HAVE_GREGEX
|
||||||
|
re = _convert_to_simple_regexp (list->data);
|
||||||
if (g_regex_match_simple (re, uri, 0, 0))
|
if (g_regex_match_simple (re, uri, 0, 0))
|
||||||
{
|
{
|
||||||
match = TRUE;
|
match = TRUE;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
g_free (re);
|
g_free (re);
|
||||||
|
#else
|
||||||
|
pattern_len = strlen (list->data);
|
||||||
|
if (!g_ascii_strncasecmp (uri, list->data, MAX (uri_len, pattern_len)))
|
||||||
|
{
|
||||||
|
match = TRUE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (_match_with_wildcard (uri, list->data))
|
||||||
|
{
|
||||||
|
match = TRUE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
list = list->next;
|
list = list->next;
|
||||||
}
|
}
|
||||||
if (!match)
|
if (!match)
|
||||||
|
@ -349,15 +363,27 @@ _may_load_script (const gchar* uri,
|
||||||
list = *excludes;
|
list = *excludes;
|
||||||
while (list)
|
while (list)
|
||||||
{
|
{
|
||||||
re = _convert_to_simple_regexp (list->data);
|
|
||||||
#if HAVE_GREGEX
|
#if HAVE_GREGEX
|
||||||
|
re = _convert_to_simple_regexp (list->data);
|
||||||
if (g_regex_match_simple (re, uri, 0, 0))
|
if (g_regex_match_simple (re, uri, 0, 0))
|
||||||
{
|
{
|
||||||
match = FALSE;
|
match = FALSE;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
g_free (re);
|
g_free (re);
|
||||||
|
#else
|
||||||
|
pattern_len = strlen (list->data);
|
||||||
|
if (!g_ascii_strncasecmp (uri, list->data, MAX (uri_len, pattern_len)))
|
||||||
|
{
|
||||||
|
match = FALSE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (_match_with_wildcard (uri, list->data))
|
||||||
|
{
|
||||||
|
match = FALSE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
list = list->next;
|
list = list->next;
|
||||||
}
|
}
|
||||||
return match;
|
return match;
|
||||||
|
@ -409,6 +435,10 @@ midori_web_widget_window_object_cleared_cb (GtkWidget* web_widget,
|
||||||
gchar* exception;
|
gchar* exception;
|
||||||
gchar* message;
|
gchar* message;
|
||||||
|
|
||||||
|
uri = webkit_web_frame_get_uri (web_frame);
|
||||||
|
if (!uri)
|
||||||
|
return;
|
||||||
|
|
||||||
/* FIXME: We want to honor system installed addons as well */
|
/* FIXME: We want to honor system installed addons as well */
|
||||||
addon_path = g_build_filename (g_get_user_data_dir (), PACKAGE_NAME,
|
addon_path = g_build_filename (g_get_user_data_dir (), PACKAGE_NAME,
|
||||||
_folder_for_kind (addons->kind), NULL);
|
_folder_for_kind (addons->kind), NULL);
|
||||||
|
@ -425,14 +455,11 @@ midori_web_widget_window_object_cleared_cb (GtkWidget* web_widget,
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (includes || excludes)
|
if (includes || excludes)
|
||||||
{
|
|
||||||
uri = webkit_web_frame_get_uri (web_frame);
|
|
||||||
if (!_may_load_script (uri, &includes, &excludes))
|
if (!_may_load_script (uri, &includes, &excludes))
|
||||||
{
|
{
|
||||||
g_free (fullname);
|
g_free (fullname);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
g_slist_free (includes);
|
g_slist_free (includes);
|
||||||
g_slist_free (excludes);
|
g_slist_free (excludes);
|
||||||
exception = NULL;
|
exception = NULL;
|
||||||
|
|
Loading…
Reference in a new issue