Sync ephy's source

This commit is contained in:
jun7 2018-05-20 17:09:37 +09:00
parent fe25df8e33
commit 74bebfdc68
2 changed files with 127 additions and 150 deletions

View File

@ -24,23 +24,24 @@
* at http://www.twotoasts.de * at http://www.twotoasts.de
*/ */
//#include "config.h" #define LOG(...)
/*
#include "config.h"
*/
#include "ephy-uri-tester.h" #include "ephy-uri-tester.h"
//#include "ephy-debug.h" /*
//#include "ephy-prefs.h" #include "ephy-debug.h"
//#include "ephy-settings.h" #include "ephy-prefs.h"
//#include "ephy-uri-tester-shared.h" #include "ephy-settings.h"
#include "ephy-uri-tester-shared.h"
*/
#include <gio/gio.h> #include <gio/gio.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include <libsoup/soup.h> #include <libsoup/soup.h>
#include <string.h> #include <string.h>
#ifdef HAVE_LIBHTTPSEVERYWHERE
#include <httpseverywhere.h>
#endif
#define SIGNATURE_SIZE 8 #define SIGNATURE_SIZE 8
struct _EphyUriTester { struct _EphyUriTester {
@ -69,11 +70,6 @@ struct _EphyUriTester {
GMainLoop *load_loop; GMainLoop *load_loop;
int adblock_filters_to_load; int adblock_filters_to_load;
gboolean adblock_loaded; gboolean adblock_loaded;
#ifdef HAVE_LIBHTTPSEVERYWHERE
gboolean https_everywhere_loaded;
HTTPSEverywhereContext *https_everywhere_context;
#endif
}; };
enum { enum {
@ -111,10 +107,10 @@ ephy_uri_tester_check_rule (EphyUriTester *tester,
return FALSE; return FALSE;
} }
/* TODO: Domain and document opt check */ /* TODO: Domain and document opt check */
// if (whitelist) if (whitelist)
// LOG ("whitelisted by pattern regexp=%s -- %s", g_regex_get_pattern (regex), req_uri); LOG ("whitelisted by pattern regexp=%s -- %s", g_regex_get_pattern (regex), req_uri);
// else else
// LOG ("blocked by pattern regexp=%s -- %s", g_regex_get_pattern (regex), req_uri); LOG ("blocked by pattern regexp=%s -- %s", g_regex_get_pattern (regex), req_uri);
return TRUE; return TRUE;
} }
@ -216,7 +212,6 @@ static GString *
ephy_uri_tester_fixup_regexp (const char *prefix, char *src) ephy_uri_tester_fixup_regexp (const char *prefix, char *src)
{ {
GString *str; GString *str;
int len = 0;
if (!src) if (!src)
return NULL; return NULL;
@ -228,26 +223,56 @@ ephy_uri_tester_fixup_regexp (const char *prefix, char *src)
(void)*src++; (void)*src++;
} }
/* NOTE: The '$' is used as separator for the rule options, so rule patterns
cannot ever contain them. If a rule needs to match it, it uses "%24".
Splitting the option is done in ephy_uri_tester_add_url_pattern().
The loop below always escapes square brackets. This way there is no chance
that they get interpreted as a character class, and it is NOT needed to
escape '-' because it's only special inside a character class. */
do { do {
switch (*src) { switch (*src) {
case '*': case '*':
g_string_append (str, ".*"); g_string_append (str, ".*");
break; break;
/*case '.': case '^':
g_string_append (str, "\\."); /* Matches a separator character, defined as:
break;*/ * "anything but a letter, a digit, or one of the following: _ - . %" */
g_string_append (str, "([^a-zA-Z\\d]|[_\\-\\.%])");
break;
case '|':
/* If at the end of the pattern, the match is anchored at the end. In
* the middle of a pattern it matches a literal vertical bar and the
* character must be escaped. */
if (src[1] == '\0')
g_string_append (str, "$");
else
g_string_append (str, "\\|");
break;
/* The following characters are escaped as they have a meaning in
* regular expressions:
* - '.' matches any character.
* - '+' matches the preceding pattern one or more times.
* - '?' matches the preceding pattern zero or one times.
* - '[' ']' are used to define a character class.
* - '{' '}' are used to define a min/max quantifier.
* - '(' ')' are used to defin a submatch expression.
* - '\' has several uses in regexps (shortcut character classes.
* matching non-printing characters, using octal/hex, octal
* constants, backreferences... they must to be escaped to
* match a literal backslash and prevent wrecking havoc!). */
case '.':
case '+':
case '?': case '?':
case '[': case '[':
case ']': case ']':
case '{':
case '}':
case '(':
case ')':
case '\\':
g_string_append_printf (str, "\\%c", *src); g_string_append_printf (str, "\\%c", *src);
break; break;
case '|':
/* FIXME: We actually need to match :[0-9]+ or '/'. Sign means
"here could be port number or nothing". So bla.com^ will match
bla.com/ or bla.com:8080/ but not bla.com.au/ */
case '^':
case '+':
break;
default: default:
g_string_append_printf (str, "%c", *src); g_string_append_printf (str, "%c", *src);
break; break;
@ -255,11 +280,6 @@ ephy_uri_tester_fixup_regexp (const char *prefix, char *src)
src++; src++;
} while (*src); } while (*src);
len = str->len;
/* We dont need .* in the end of url. Thats stupid */
if (str->str && str->str[len - 1] == '*' && str->str[len - 2] == '.')
g_string_erase (str, len - 2, 2);
return str; return str;
} }
@ -310,14 +330,14 @@ ephy_uri_tester_compile_regexp (EphyUriTester *tester,
sig = g_strndup (patt + pos, SIGNATURE_SIZE); sig = g_strndup (patt + pos, SIGNATURE_SIZE);
if (!strchr (sig, '*') && if (!strchr (sig, '*') &&
!g_hash_table_lookup (keys, sig)) { !g_hash_table_lookup (keys, sig)) {
// LOG ("sig: %s %s", sig, patt); LOG ("sig: %s %s", sig, patt);
g_hash_table_insert (keys, g_strdup (sig), g_regex_ref (regex)); g_hash_table_insert (keys, g_strdup (sig), g_regex_ref (regex));
g_hash_table_insert (optslist, g_strdup (sig), g_strdup (opts)); g_hash_table_insert (optslist, g_strdup (sig), g_strdup (opts));
signature_count++; signature_count++;
} else { } else {
if (sig[0] == '*' && if (sig[0] == '*' &&
!g_hash_table_lookup (pattern, patt)) { !g_hash_table_lookup (pattern, patt)) {
// LOG ("patt2: %s %s", sig, patt); LOG ("patt2: %s %s", sig, patt);
g_hash_table_insert (pattern, g_strdup (patt), g_regex_ref (regex)); g_hash_table_insert (pattern, g_strdup (patt), g_regex_ref (regex));
g_hash_table_insert (optslist, g_strdup (patt), g_strdup (opts)); g_hash_table_insert (optslist, g_strdup (patt), g_strdup (opts));
} }
@ -329,7 +349,7 @@ ephy_uri_tester_compile_regexp (EphyUriTester *tester,
if (signature_count > 1 && g_hash_table_lookup (pattern, patt)) if (signature_count > 1 && g_hash_table_lookup (pattern, patt))
g_hash_table_remove (pattern, patt); g_hash_table_remove (pattern, patt);
} else { } else {
// LOG ("patt: %s%s", patt, ""); LOG ("patt: %s%s", patt, "");
/* Pattern is a regexp chars */ /* Pattern is a regexp chars */
g_hash_table_insert (pattern, g_strdup (patt), regex); g_hash_table_insert (pattern, g_strdup (patt), regex);
g_hash_table_insert (optslist, g_strdup (patt), g_strdup (opts)); g_hash_table_insert (optslist, g_strdup (patt), g_strdup (opts));
@ -376,10 +396,10 @@ ephy_uri_tester_add_url_pattern (EphyUriTester *tester,
format_patt = ephy_uri_tester_fixup_regexp (prefix, patt); format_patt = ephy_uri_tester_fixup_regexp (prefix, patt);
// if (whitelist) if (whitelist)
// LOG ("whitelist: %s opts %s", format_patt->str, opts); LOG ("whitelist: %s opts %s", format_patt->str, opts);
// else else
// LOG ("blacklist: %s opts %s", format_patt->str, opts); LOG ("blacklist: %s opts %s", format_patt->str, opts);
ephy_uri_tester_compile_regexp (tester, format_patt, opts, whitelist); ephy_uri_tester_compile_regexp (tester, format_patt, opts, whitelist);
@ -395,6 +415,10 @@ ephy_uri_tester_add_url_pattern (EphyUriTester *tester,
static inline void static inline void
ephy_uri_tester_frame_add (EphyUriTester *tester, char *line) ephy_uri_tester_frame_add (EphyUriTester *tester, char *line)
{ {
/*
const char *separator = " , ";
*/
//for wyeb
const char *separator = ",\n"; const char *separator = ",\n";
(void)*line++; (void)*line++;
@ -514,24 +538,9 @@ ephy_uri_tester_adblock_loaded (EphyUriTester *tester)
{ {
if (g_atomic_int_dec_and_test (&tester->adblock_filters_to_load)) { if (g_atomic_int_dec_and_test (&tester->adblock_filters_to_load)) {
tester->adblock_loaded = TRUE; tester->adblock_loaded = TRUE;
#ifdef HAVE_LIBHTTPSEVERYWHERE
if (tester->https_everywhere_loaded)
g_main_loop_quit (tester->load_loop);
#else
g_main_loop_quit (tester->load_loop);
#endif
}
}
#ifdef HAVE_LIBHTTPSEVERYWHERE
static void
ephy_uri_tester_https_everywhere_loaded (EphyUriTester *tester)
{
tester->https_everywhere_loaded = TRUE;
if (tester->adblock_loaded)
g_main_loop_quit (tester->load_loop); g_main_loop_quit (tester->load_loop);
} }
#endif }
static void static void
file_parse_cb (GDataInputStream *stream, GAsyncResult *result, EphyUriTester *tester) file_parse_cb (GDataInputStream *stream, GAsyncResult *result, EphyUriTester *tester)
@ -599,44 +608,18 @@ ephy_uri_tester_block_uri (EphyUriTester *tester,
char * char *
ephy_uri_tester_rewrite_uri (EphyUriTester *tester, ephy_uri_tester_rewrite_uri (EphyUriTester *tester,
const char *request_uri, const char *request_uri,
const char *page_uri, const char *page_uri)
EphyUriTestFlags flags)
{ {
/* Should we block the URL outright? */ /* Should we block the URL outright? */
if ((flags & EPHY_URI_TEST_ADBLOCK) && if (ephy_uri_tester_block_uri (tester, request_uri, page_uri)) {
ephy_uri_tester_block_uri (tester, request_uri, page_uri)) {
g_debug ("Request '%s' blocked (page: '%s')", request_uri, page_uri); g_debug ("Request '%s' blocked (page: '%s')", request_uri, page_uri);
return NULL; return NULL;
} }
#ifdef HAVE_LIBHTTPSEVERYWHERE
if ((flags & EPHY_URI_TEST_HTTPS_EVERYWHERE) && tester->https_everywhere_context != NULL)
return https_everywhere_context_rewrite (tester->https_everywhere_context, request_uri);
#endif
return g_strdup (request_uri); return g_strdup (request_uri);
} }
#ifdef HAVE_LIBHTTPSEVERYWHERE
static void
https_everywhere_context_init_cb (HTTPSEverywhereContext *context,
GAsyncResult *res,
EphyUriTester *tester)
{
GError *error = NULL;
https_everywhere_context_init_finish (context, res, &error);
if (error) {
g_warning ("Failed to initialize HTTPS Everywhere context: %s", error->message);
g_error_free (error);
}
ephy_uri_tester_https_everywhere_loaded (tester);
}
#endif
static void static void
adblock_file_monitor_changed (GFileMonitor *monitor, adblock_file_monitor_changed (GFileMonitor *monitor,
GFile *file, GFile *file,
@ -654,6 +637,7 @@ adblock_file_monitor_changed (GFileMonitor *monitor,
} }
//for wyeb
static GFile *filter_file; static GFile *filter_file;
@ -661,15 +645,18 @@ static void
ephy_uri_tester_begin_loading_adblock_filters (EphyUriTester *tester, ephy_uri_tester_begin_loading_adblock_filters (EphyUriTester *tester,
GList **monitors) GList **monitors)
{ {
// char **filters; //for wyeb
// filters = g_settings_get_strv (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_ADBLOCK_FILTERS);
// tester->adblock_filters_to_load = g_strv_length (filters);
tester->adblock_filters_to_load = 1; tester->adblock_filters_to_load = 1;
// for (guint i = 0; filters[i]; i++) { /*
// GFile *filter_file; char **filters;
// filter_file = ephy_uri_tester_get_adblock_filter_file ( tester->adblock_data_dir, filters[i]); filters = g_settings_get_strv (EPHY_SETTINGS_MAIN, EPHY_PREFS_ADBLOCK_FILTERS);
tester->adblock_filters_to_load = g_strv_length (filters);
for (guint i = 0; filters[i]; i++) {
GFile *filter_file;
filter_file = ephy_uri_tester_get_adblock_filter_file (tester->adblock_data_dir, filters[i]);
*/
if (!g_file_query_exists (filter_file, NULL)) { if (!g_file_query_exists (filter_file, NULL)) {
GFileMonitor *monitor; GFileMonitor *monitor;
GError *error = NULL; GError *error = NULL;
@ -688,9 +675,11 @@ tester->adblock_filters_to_load = 1;
(GAsyncReadyCallback)file_read_cb, (GAsyncReadyCallback)file_read_cb,
tester); tester);
} }
// g_object_unref (filter_file); /*
// } g_object_unref (filter_file);
// g_strfreev (filters); }
g_strfreev (filters);
*/
} }
static void static void
@ -704,16 +693,6 @@ ephy_uri_tester_load_sync (GTask *task,
g_main_context_push_thread_default (context); g_main_context_push_thread_default (context);
tester->load_loop = g_main_loop_new (context, FALSE); tester->load_loop = g_main_loop_new (context, FALSE);
#ifdef HAVE_LIBHTTPSEVERYWHERE
if (!tester->https_everywhere_loaded) {
g_assert (tester->https_everywhere_context == NULL);
tester->https_everywhere_context = https_everywhere_context_new ();
https_everywhere_context_init (tester->https_everywhere_context, NULL,
(GAsyncReadyCallback)https_everywhere_context_init_cb,
tester);
}
#endif
if (!tester->adblock_loaded) if (!tester->adblock_loaded)
ephy_uri_tester_begin_loading_adblock_filters (tester, &monitors); ephy_uri_tester_begin_loading_adblock_filters (tester, &monitors);
@ -730,7 +709,7 @@ ephy_uri_tester_load_sync (GTask *task,
static void static void
ephy_uri_tester_init (EphyUriTester *tester) ephy_uri_tester_init (EphyUriTester *tester)
{ {
// LOG ("EphyUriTester initializing %p", tester); LOG ("EphyUriTester initializing %p", tester);
tester->pattern = g_hash_table_new_full (g_str_hash, g_str_equal, tester->pattern = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify)g_free, (GDestroyNotify)g_free,
@ -797,28 +776,12 @@ ephy_uri_tester_set_property (GObject *object,
} }
} }
static void
ephy_uri_tester_dispose (GObject *object)
{
#ifdef HAVE_LIBHTTPSEVERYWHERE
EphyUriTester *tester = EPHY_URI_TESTER (object);
#endif
// LOG ("EphyUriTester disposing %p", object);
#ifdef HAVE_LIBHTTPSEVERYWHERE
g_clear_object (&tester->https_everywhere_context);
#endif
G_OBJECT_CLASS (ephy_uri_tester_parent_class)->dispose (object);
}
static void static void
ephy_uri_tester_finalize (GObject *object) ephy_uri_tester_finalize (GObject *object)
{ {
EphyUriTester *tester = EPHY_URI_TESTER (object); EphyUriTester *tester = EPHY_URI_TESTER (object);
// LOG ("EphyUriTester finalizing %p", object); LOG ("EphyUriTester finalizing %p", object);
g_free (tester->adblock_data_dir); g_free (tester->adblock_data_dir);
@ -849,7 +812,6 @@ ephy_uri_tester_class_init (EphyUriTesterClass *klass)
GObjectClass *object_class = G_OBJECT_CLASS (klass); GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->set_property = ephy_uri_tester_set_property; object_class->set_property = ephy_uri_tester_set_property;
object_class->dispose = ephy_uri_tester_dispose;
object_class->finalize = ephy_uri_tester_finalize; object_class->finalize = ephy_uri_tester_finalize;
obj_properties[PROP_ADBLOCK_DATA_DIR] = obj_properties[PROP_ADBLOCK_DATA_DIR] =
@ -870,9 +832,7 @@ ephy_uri_tester_new (const char *adblock_data_dir)
/* /*
static void static void
ephy_uri_tester_adblock_filters_changed_cb (GSettings *settings, ephy_uri_tester_reload_adblock_filters (EphyUriTester *tester)
char *key,
EphyUriTester *tester)
{ {
g_hash_table_remove_all (tester->pattern); g_hash_table_remove_all (tester->pattern);
g_hash_table_remove_all (tester->keys); g_hash_table_remove_all (tester->keys);
@ -887,35 +847,61 @@ ephy_uri_tester_adblock_filters_changed_cb (GSettings *settings,
tester->adblock_loaded = FALSE; tester->adblock_loaded = FALSE;
ephy_uri_tester_load (tester); ephy_uri_tester_load (tester);
} }
static void
ephy_uri_tester_adblock_filters_changed_cb (GSettings *settings,
char *key,
EphyUriTester *tester)
{
ephy_uri_tester_reload_adblock_filters (tester);
}
static void
ephy_uri_tester_enable_adblock_changed_cb (GSettings *settings,
char *key,
EphyUriTester *tester)
{
ephy_uri_tester_reload_adblock_filters (tester);
}
*/ */
void void
ephy_uri_tester_load (EphyUriTester *tester) ephy_uri_tester_load (EphyUriTester *tester)
{ {
GTask *task; GTask *task;
// char **trash; /*
char **trash;
g_return_if_fail (EPHY_IS_URI_TESTER (tester)); g_assert (EPHY_IS_URI_TESTER (tester));
if (tester->adblock_loaded if (!g_settings_get_boolean (EPHY_SETTINGS_WEB, EPHY_PREFS_WEB_ENABLE_ADBLOCK))
#ifdef HAVE_LIBHTTPSEVERYWHERE tester->adblock_loaded = TRUE;
&& tester->https_everywhere_loaded */
#endif
) if (tester->adblock_loaded)
return; return;
// g_signal_handlers_disconnect_by_func (EPHY_SETTINGS_WEB, ephy_uri_tester_adblock_filters_changed_cb, tester); /*
g_signal_handlers_disconnect_by_func (EPHY_SETTINGS_WEB, ephy_uri_tester_adblock_filters_changed_cb, tester);
g_signal_handlers_disconnect_by_func (EPHY_SETTINGS_WEB, ephy_uri_tester_enable_adblock_changed_cb, tester);
*/
task = g_task_new (tester, NULL, NULL, NULL); task = g_task_new (tester, NULL, NULL, NULL);
g_task_run_in_thread_sync (task, (GTaskThreadFunc)ephy_uri_tester_load_sync); g_task_run_in_thread_sync (task, (GTaskThreadFunc)ephy_uri_tester_load_sync);
g_object_unref (task); g_object_unref (task);
// g_signal_connect (EPHY_SETTINGS_WEB, "changed::adblock-filters", /*
// G_CALLBACK (ephy_uri_tester_adblock_filters_changed_cb), tester); g_signal_connect (EPHY_SETTINGS_MAIN, "changed::" EPHY_PREFS_ADBLOCK_FILTERS,
G_CALLBACK (ephy_uri_tester_adblock_filters_changed_cb), tester);
g_signal_connect (EPHY_SETTINGS_WEB, "changed::" EPHY_PREFS_WEB_ENABLE_ADBLOCK,
G_CALLBACK (ephy_uri_tester_enable_adblock_changed_cb), tester);
*/
/* GSettings never emits the changed signal until after we read the setting /* GSettings never emits the changed signal until after we read the setting
* the first time after connecting the handler... work around this.*/ * the first time after connecting the handler... work around this.*/
// trash = g_settings_get_strv (EPHY_SETTINGS_WEB, "adblock-filters"); /*
// g_strfreev (trash); trash = g_settings_get_strv (EPHY_SETTINGS_MAIN, EPHY_PREFS_ADBLOCK_FILTERS);
g_strfreev (trash);
*/
} }
@ -1062,8 +1048,8 @@ static char *datafunc(char *req)
//req uri + ' ' + page uri //req uri + ' ' + page uri
char **args = g_strsplit(req, " ", 2); char **args = g_strsplit(req, " ", 2);
char *ret = !tester ? g_strdup(args[0]) : ephy_uri_tester_rewrite_uri(tester, char *ret = !tester ? g_strdup(args[0]) :
args[0], args[1] ?: args[0], EPHY_URI_TEST_ADBLOCK); ephy_uri_tester_rewrite_uri(tester, args[0], args[1] ?: args[0]);
g_strfreev(args); g_strfreev(args);

View File

@ -28,20 +28,11 @@ G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE (EphyUriTester, ephy_uri_tester, EPHY, URI_TESTER, GObject) G_DECLARE_FINAL_TYPE (EphyUriTester, ephy_uri_tester, EPHY, URI_TESTER, GObject)
typedef enum
{
EPHY_URI_TEST_ADBLOCK = 1 << 1,
EPHY_URI_TEST_HTTPS_EVERYWHERE = 1 << 2,
EPHY_URI_TEST_ALL = EPHY_URI_TEST_ADBLOCK | EPHY_URI_TEST_HTTPS_EVERYWHERE
} EphyUriTestFlags;
EphyUriTester *ephy_uri_tester_new (const char *adblock_data_dir); EphyUriTester *ephy_uri_tester_new (const char *adblock_data_dir);
void ephy_uri_tester_load (EphyUriTester *tester); void ephy_uri_tester_load (EphyUriTester *tester);
char *ephy_uri_tester_rewrite_uri (EphyUriTester *tester, char *ephy_uri_tester_rewrite_uri (EphyUriTester *tester,
const char *request_uri, const char *request_uri,
const char *page_uri, const char *page_uri);
EphyUriTestFlags flags);
G_END_DECLS G_END_DECLS