/*
* Mapping from MqConfig to WebKitSettings and WebKitWebContext
*
* Copyright (C) 2017 Patrick McDermott
*
* This file is part of Marquee.
*
* Marquee is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Marquee is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Marquee. If not, see .
*/
#include
#include "web-settings.h"
#include "config.h"
enum mapping_type {
MAP_BOOL,
MAP_BOOL_N,
MAP_INT,
/* MAP_DBL, */
MAP_STR,
/* MAP_STRL, */
};
struct mapping {
enum mapping_type type;
const gchar *mq_name;
const gchar *wk_name;
};
static struct mapping mappings[] = {
/* General -> Navigation and Accessibility */
{MAP_BOOL, "navigation.smooth-scrolling",
"enable-smooth-scrolling"},
{MAP_BOOL, "navigation.tabbing",
"enable-tabs-to-links"},
{MAP_BOOL, "navigation.caret",
"enable-caret-browsing"},
{MAP_BOOL, "navigation.spatial",
"enable-spatial-navigation"},
/* General -> Miscellaneous */
{MAP_BOOL, "display.textarea.resize.enable",
"enable-resizable-text-areas"},
{MAP_BOOL, "devtools.enable",
"enable-developer-extras"},
/* General -> Compatibility */
// compatibility.user-agent
{MAP_BOOL, "compatibility.quirks",
"enable-site-specific-quirks"},
/* Display -> Fonts */
{MAP_STR, "font.family.default",
"default-font-family"},
{MAP_STR, "font.family.monospace",
"monospace-font-family"},
{MAP_STR, "font.family.serif",
"serif-font-family"},
{MAP_STR, "font.family.sans-serif",
"sans-serif-font-family"},
{MAP_STR, "font.family.cursive",
"cursive-font-family"},
{MAP_STR, "font.family.fantasy",
"fantasy-font-family"},
{MAP_STR, "font.family.pictograph",
"pictograph-font-family"},
{MAP_INT, "font.size.default",
"default-font-size"},
{MAP_INT, "font.size.monospace-default",
"default-monospace-font-size"},
{MAP_INT, "font.size.minimum",
"minimum-font-size"},
/* Display -> Languages */
{MAP_STR, "intl.default-charset",
"default-charset"},
/* Display -> Zoom */
{MAP_BOOL, "zoom.text-only",
"zoom-text-only"},
/* Permissions -> General */
{MAP_BOOL, "permissions.images.auto-load",
"auto-load-images"},
{MAP_BOOL, "permissions.images.favicons.override",
"load-icons-ignoring-image-load-setting"},
{MAP_BOOL, "permissions.java.enable",
"enable-java"},
{MAP_BOOL, "permissions.javascript.enable",
"enable-javascript"},
{MAP_BOOL, "permissions.plugins.enable",
"enable-plugins"},
/* Permissions -> JavaScript */
{MAP_BOOL, "permissions.javascript.open-windows",
"javascript-can-open-windows-automatically"},
{MAP_BOOL, "permissions.javascript.fullscreen",
"enable-fullscreen"},
{MAP_BOOL, "permissions.javascript.modal-dialogs",
"allow-modal-dialogs"},
{MAP_BOOL, "permissions.javascript.clipboard",
"javascript-can-access-clipboard"},
/* Permissions -> Data Storage */
{MAP_BOOL, "permissions.javascript.database",
"enable-html5-database"},
{MAP_BOOL, "permissions.javascript.storage",
"enable-html5-local-storage"},
{MAP_BOOL, "permissions.appcache",
"enable-offline-web-application-cache"},
/* Permissions -> Graphics and Multimedia */
{MAP_BOOL, "canvas.acceleration.enable",
"enable-accelerated-2d-canvas"},
{MAP_BOOL, "permissions.javascript.webgl",
"enable-webgl"},
{MAP_BOOL, "permissions.javascript.audio",
"enable-webaudio"},
{MAP_BOOL_N, "media.autoplay",
"media-playback-requires-user-gesture"},
{MAP_BOOL_N, "media.force-fullscreen",
"media-playback-allows-inline"},
{MAP_BOOL, "permissions.javascript.mediastream.enable",
"enable-media-stream"},
{MAP_BOOL, "permissions.javascript.mediasource.enable",
"enable-mediasource"},
/* Security and Privacy -> History */
{MAP_BOOL, "privacy.private-browsing.enabled",
"enable-private-browsing"},
/* Security and Privacy -> Security */
{MAP_BOOL, "security.xss-auditor.enable",
"enable-xss-auditor"},
/* Security and Privacy -> Network */
{MAP_BOOL, "dns.prefetch.enable",
"enable-dns-prefetching"},
};
static GHashTable *mappings_table = NULL;
static void
wks_bool_cb(MqConfig G_GNUC_UNUSED *config, const gchar *name,
const gboolean value, WebKitSettings *wk_settings)
{
GValue g_value = G_VALUE_INIT;
g_value_init(&g_value, G_TYPE_BOOLEAN);
g_value_set_boolean(&g_value, value);
g_object_set_property(G_OBJECT(wk_settings),
g_hash_table_lookup(mappings_table, name), &g_value);
}
static void
wks_bool_n_cb(MqConfig G_GNUC_UNUSED *config, const gchar *name,
const gboolean value, WebKitSettings *wk_settings)
{
GValue g_value = G_VALUE_INIT;
g_value_init(&g_value, G_TYPE_BOOLEAN);
g_value_set_boolean(&g_value, value);
g_object_set_property(G_OBJECT(wk_settings),
g_hash_table_lookup(mappings_table, name), &g_value);
}
static void
wks_int_cb(MqConfig G_GNUC_UNUSED *config, const gchar *name,
const gint value, WebKitSettings *wk_settings)
{
GValue g_value = G_VALUE_INIT;
g_value_init(&g_value, G_TYPE_INT);
g_value_set_int(&g_value, value);
g_object_set_property(G_OBJECT(wk_settings),
g_hash_table_lookup(mappings_table, name), &g_value);
}
static void
wks_str_cb(MqConfig G_GNUC_UNUSED *config, const gchar *name,
const gchar *value, WebKitSettings *wk_settings)
{
GValue g_value = G_VALUE_INIT;
g_value_init(&g_value, G_TYPE_STRING);
g_value_set_static_string(&g_value, value);
g_object_set_property(G_OBJECT(wk_settings),
g_hash_table_lookup(mappings_table, name), &g_value);
}
static void
wkwc_spell_checking_enabled_cb(MqConfig G_GNUC_UNUSED *config,
const gchar G_GNUC_UNUSED *name, const gboolean value,
WebKitWebContext *wk_web_context)
{
webkit_web_context_set_spell_checking_enabled(wk_web_context, value);
}
static void
wkwc_spell_checking_languages_cb(MqConfig *config, const gchar *name,
const gchar G_GNUC_UNUSED *value, WebKitWebContext *wk_web_context)
{
webkit_web_context_set_spell_checking_languages(wk_web_context,
(const gchar * const *) mq_config_get_string_list(config,
name));
}
static void
wkwc_preferred_languages_cb(MqConfig *config, const gchar *name,
const gchar G_GNUC_UNUSED *value, WebKitWebContext *wk_web_context)
{
webkit_web_context_set_preferred_languages(wk_web_context,
(const gchar * const *) mq_config_get_string_list(config,
name));
}
static void
wkcm_accept_policy_cb(MqConfig G_GNUC_UNUSED *config,
const gchar G_GNUC_UNUSED *name, const gchar *value,
WebKitCookieManager *wk_cookie_manager)
{
if (g_strcmp0(value, "always") == 0) {
webkit_cookie_manager_set_accept_policy(wk_cookie_manager,
WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS);
} else if (g_strcmp0(value, "never") == 0) {
webkit_cookie_manager_set_accept_policy(wk_cookie_manager,
WEBKIT_COOKIE_POLICY_ACCEPT_NEVER);
} else if (g_strcmp0(value, "never") == 0) {
webkit_cookie_manager_set_accept_policy(wk_cookie_manager,
WEBKIT_COOKIE_POLICY_ACCEPT_NEVER);
} else if (g_strcmp0(value, "no-third-party") == 0) {
webkit_cookie_manager_set_accept_policy(wk_cookie_manager,
WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY);
} else {
g_assert_not_reached();
}
}
static void
init_mappings_table(void)
{
gsize i;
if (mappings_table) {
return;
}
mappings_table = g_hash_table_new(g_str_hash, g_str_equal);
for (i = 0; i < sizeof(mappings) / sizeof(mappings[0]); ++i) {
g_hash_table_insert(mappings_table,
g_strdup(mappings[i].mq_name),
g_strdup(mappings[i].wk_name));
}
}
void
mq_web_settings_connect(MqConfig *config, WebKitSettings *wk_settings,
WebKitWebContext *wk_web_context)
{
gsize i;
WebKitCookieManager *wk_cookie_manager;
wk_cookie_manager = webkit_web_context_get_cookie_manager(
wk_web_context);
init_mappings_table();
for (i = 0; i < sizeof(mappings) / sizeof(mappings[0]); ++i) {
switch (mappings[i].type) {
case MAP_BOOL:
mq_config_notify_boolean(config,
mappings[i].mq_name,
(MqConfigBooleanCallback) wks_bool_cb,
wk_settings);
wks_bool_cb(config, mappings[i].mq_name,
mq_config_get_boolean(config,
mappings[i].mq_name),
wk_settings);
break;
case MAP_BOOL_N:
mq_config_notify_boolean(config,
mappings[i].mq_name,
(MqConfigBooleanCallback) wks_bool_n_cb,
wk_settings);
wks_bool_n_cb(config, mappings[i].mq_name,
mq_config_get_boolean(config,
mappings[i].mq_name),
wk_settings);
break;
case MAP_INT:
mq_config_notify_integer(config,
mappings[i].mq_name,
(MqConfigIntegerCallback) wks_int_cb,
wk_settings);
wks_int_cb(config, mappings[i].mq_name,
mq_config_get_integer(config,
mappings[i].mq_name),
wk_settings);
break;
case MAP_STR:
mq_config_notify_string(config,
mappings[i].mq_name,
(MqConfigStringCallback) wks_str_cb,
wk_settings);
wks_str_cb(config, mappings[i].mq_name,
mq_config_get_string(config,
mappings[i].mq_name),
wk_settings);
break;
}
}
/* General -> Spell Checking */
mq_config_notify_boolean(config, "spell.enable",
(MqConfigBooleanCallback) wkwc_spell_checking_enabled_cb,
wk_web_context);
wkwc_spell_checking_enabled_cb(config, "spell.enable",
mq_config_get_boolean(config, "spell.enable"), wk_web_context);
mq_config_notify_string(config, "spell.langs",
(MqConfigStringCallback) wkwc_spell_checking_languages_cb,
wk_web_context);
wkwc_spell_checking_languages_cb(config, "spell.langs", NULL,
wk_web_context);
/* Display -> Languages */
mq_config_notify_string(config, "intl.accept-languages",
(MqConfigStringCallback) wkwc_preferred_languages_cb,
wk_web_context);
wkwc_preferred_languages_cb(config, "intl.accept-languages", NULL,
wk_web_context);
/* Security and Privacy -> Cookies */
mq_config_notify_string(config, "cookies.accept",
(MqConfigStringCallback) wkcm_accept_policy_cb,
wk_cookie_manager);
wkcm_accept_policy_cb(config, "cookies.accept",
mq_config_get_string(config, "cookies.accept"),
wk_cookie_manager);
}