~midori/midori/webKitTwoOnly

« back to all changes in this revision

Viewing changes to midori/midori-websettings.c

  • Committer: gue5t
  • Date: 2016-02-10 18:15:47 UTC
  • Revision ID: gue5t@midori.launchpad-20160210181547-fqdxt4f10yaxusm1
support user-stylesheet-uri with wk2 by adding a WebKit.UserContentManager to Midori.Settings

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
    gchar* site_data_rules;
63
63
    gboolean enforce_font_family;
64
64
    gchar* user_stylesheet_uri;
65
 
    gchar* user_stylesheet_uri_cached;
66
 
    GHashTable* user_stylesheets;
67
65
    gboolean print_without_dialog;
68
66
};
69
67
 
562
560
static void
563
561
midori_web_settings_init (MidoriWebSettings* web_settings)
564
562
{
565
 
    web_settings->user_stylesheet_uri = web_settings->user_stylesheet_uri_cached = NULL;
566
 
    web_settings->user_stylesheets = NULL;
567
 
 
568
 
    #if defined (_WIN32) && !GTK_CHECK_VERSION (3, 0, 0)
569
 
    /* Try to work-around black borders on native widgets and GTK+2 on Win32 */
570
 
    midori_web_settings_add_style (web_settings, "black-widgets-workaround",
571
 
    "input[type='checkbox'] { -webkit-appearance: checkbox !important }"
572
 
    " input[type='radio'] { -webkit-appearance: radio !important }"
573
 
    " * { -webkit-appearance: none !important }");
574
 
    #endif
 
563
    web_settings->user_stylesheet_uri = NULL;
575
564
 
576
565
    g_signal_connect (web_settings, "notify::default-charset",
577
566
                      G_CALLBACK (notify_default_encoding_cb), NULL);
590
579
    katze_assign (web_settings->accept, NULL);
591
580
    katze_assign (web_settings->ident_string, NULL);
592
581
    katze_assign (web_settings->user_stylesheet_uri, NULL);
593
 
    katze_assign (web_settings->user_stylesheet_uri_cached, NULL);
594
 
    if (web_settings->user_stylesheets != NULL)
595
 
        g_hash_table_destroy (web_settings->user_stylesheets);
596
582
 
597
583
    G_OBJECT_CLASS (midori_web_settings_parent_class)->finalize (object);
598
584
}
850
836
}
851
837
 
852
838
static void
853
 
midori_web_settings_process_stylesheets (MidoriWebSettings* settings,
854
 
                                         gint               delta_len);
855
 
 
856
 
static void
857
 
base64_space_pad (gchar* base64,
858
 
                  guint  len);
859
 
 
860
 
static void
861
839
midori_web_settings_set_property (GObject*      object,
862
840
                                  guint         prop_id,
863
841
                                  const GValue* value,
966
944
                "code, code *, pre, pre *, blockquote, blockquote *, "
967
945
                "input, textarea { font-family: %s !important; }",
968
946
                                          font_family, monospace);
969
 
            midori_web_settings_add_style (web_settings, "enforce-font-family", css);
 
947
            midori_settings_add_style (MIDORI_SETTINGS (web_settings), "enforce-font-family", css);
970
948
            g_free (font_family);
971
949
            g_free (monospace);
972
950
            g_free (css);
973
951
        }
974
952
        else
975
 
            midori_web_settings_remove_style (web_settings, "enforce-font-family");
 
953
            midori_settings_remove_style (MIDORI_SETTINGS (web_settings), "enforce-font-family");
976
954
        break;
977
955
    case PROP_ENABLE_FULLSCREEN:
978
956
        g_object_set (web_settings, WEB_SETTINGS_STRING ("enable-fullscreen"),
979
957
                      g_value_get_boolean (value), NULL);
980
958
        break;
981
959
    case PROP_USER_STYLESHEET_URI:
 
960
        if ((web_settings->user_stylesheet_uri = g_value_dup_string (value)))
982
961
        {
983
 
            gint old_len = web_settings->user_stylesheet_uri_cached
984
 
                ? strlen (web_settings->user_stylesheet_uri_cached) : 0;
985
 
            gint new_len = 0;
986
 
            if ((web_settings->user_stylesheet_uri = g_value_dup_string (value)))
987
 
            {
988
 
                gchar* import = g_strdup_printf ("@import url(\"%s\");",
989
 
                    web_settings->user_stylesheet_uri);
990
 
                gchar* encoded = g_base64_encode ((const guchar*)import, strlen (import));
991
 
                new_len = strlen (encoded);
992
 
                base64_space_pad (encoded, new_len);
993
 
                g_free (import);
994
 
                katze_assign (web_settings->user_stylesheet_uri_cached, encoded);
995
 
            }
996
 
            /* Make original user-stylesheet-uri available to main.c */
997
 
            g_object_set_data (G_OBJECT (web_settings), "user-stylesheet-uri",
998
 
                web_settings->user_stylesheet_uri);
999
 
            midori_web_settings_process_stylesheets (web_settings, new_len - old_len);
 
962
            GError* error = NULL;
 
963
            gchar* contents;
 
964
            gchar* filename = g_filename_from_uri (web_settings->user_stylesheet_uri, NULL, &error);
 
965
            if (filename)
 
966
            {
 
967
                if (g_file_get_contents (filename, &contents, NULL, &error))
 
968
                {
 
969
                    midori_settings_add_style (MIDORI_SETTINGS (web_settings), "user-stylesheet-uri", contents);
 
970
                    g_free (contents);
 
971
                }
 
972
                else
 
973
                {
 
974
                    printf (_("Could not read file specified by user-stylesheet-uri: %s\n"), error->message);
 
975
                    g_error_free (error);
 
976
                }
 
977
            }
 
978
            else
 
979
            {
 
980
                printf (_("Could not convert user-stylesheet-uri to local file path: %s\n"), error->message);
 
981
                g_error_free (error);
 
982
            }
1000
983
        }
 
984
        else
 
985
            midori_settings_remove_style (MIDORI_SETTINGS (web_settings), "user-stylesheet-uri");
 
986
 
 
987
        /* Make original user-stylesheet-uri available to main.c */
 
988
        g_object_set_data (G_OBJECT (web_settings), "user-stylesheet-uri",
 
989
            web_settings->user_stylesheet_uri);
1001
990
        break;
1002
991
    case PROP_PRINT_WITHOUT_DIALOG:
1003
992
        web_settings->print_without_dialog = g_value_get_boolean(value);
1141
1130
    return web_settings;
1142
1131
}
1143
1132
 
1144
 
static void
1145
 
midori_web_settings_process_stylesheets (MidoriWebSettings* settings,
1146
 
                                         gint               delta_len)
1147
 
{
1148
 
    GHashTableIter it;
1149
 
    GString* css;
1150
 
    gchar* encoded;
1151
 
    gpointer value;
1152
 
    static guint length = 0;
1153
 
 
1154
 
    g_return_if_fail ((gint)length >= -delta_len);
1155
 
 
1156
 
    length += delta_len;
1157
 
 
1158
 
    /* Precalculate size to avoid re-allocations */
1159
 
    css = g_string_sized_new (length);
1160
 
 
1161
 
    if (settings->user_stylesheet_uri_cached != NULL)
1162
 
        g_string_append (css, settings->user_stylesheet_uri_cached);
1163
 
 
1164
 
    if (settings->user_stylesheets != NULL)
1165
 
    {
1166
 
        g_hash_table_iter_init (&it, settings->user_stylesheets);
1167
 
        while (g_hash_table_iter_next (&it, NULL, &value))
1168
 
            g_string_append (css, (gchar*)value);
1169
 
    }
1170
 
 
1171
 
    /* data: uri prefix from Source/WebCore/page/Page.cpp:700 in WebKit */
1172
 
    encoded = g_strconcat ("data:text/css;charset=utf-8;base64,", css->str, NULL);
1173
 
    /* TODO: webkit_web_view_group_add_user_style_sheet */
1174
 
    g_free (encoded);
1175
 
    g_string_free (css, TRUE);
1176
 
}
1177
 
 
1178
 
static void
1179
 
base64_space_pad (gchar* base64,
1180
 
                  guint  len)
1181
 
{
1182
 
    /* Replace '=' padding at the end with encoded spaces
1183
 
       so WebKit will accept concatenations to this string */
1184
 
    if (len > 2 && base64[len - 2] == '=')
1185
 
    {
1186
 
        base64[len - 3] += 2;
1187
 
        base64[len - 2] = 'A';
1188
 
    }
1189
 
    if (len > 1 && base64[len - 1] == '=')
1190
 
        base64[len - 1] = 'g';
1191
 
}
1192
 
 
1193
 
/**
1194
 
 * midori_web_settings_add_style:
1195
 
 * @settings: the MidoriWebSettings instance to modify
1196
 
 * @rule_id: a static string identifier
1197
 
 * @style: a CSS stylesheet
1198
 
 *
1199
 
 * Adds or replaces a custom stylesheet.
1200
 
 *
1201
 
 * Since: 0.4.2
1202
 
 **/
1203
 
void
1204
 
midori_web_settings_add_style (MidoriWebSettings* settings,
1205
 
                               const gchar*       rule_id,
1206
 
                               const gchar*       style)
1207
 
{
1208
 
    gchar* base64;
1209
 
    guint len;
1210
 
 
1211
 
    g_return_if_fail (MIDORI_IS_WEB_SETTINGS (settings));
1212
 
    g_return_if_fail (rule_id != NULL);
1213
 
    g_return_if_fail (style != NULL);
1214
 
 
1215
 
    len = strlen (style);
1216
 
    base64 = g_base64_encode ((const guchar*)style, len);
1217
 
    len = ((len + 2) / 3) * 4;
1218
 
    base64_space_pad (base64, len);
1219
 
 
1220
 
    if (settings->user_stylesheets == NULL)
1221
 
        settings->user_stylesheets = g_hash_table_new_full (g_str_hash, NULL,
1222
 
                                                            NULL, g_free);
1223
 
 
1224
 
    g_hash_table_insert (settings->user_stylesheets, (gchar*)rule_id, base64);
1225
 
    midori_web_settings_process_stylesheets (settings, len);
1226
 
}
1227
 
 
1228
 
/**
1229
 
 * midori_web_settings_remove_style:
1230
 
 * @settings: the MidoriWebSettings instance to modify
1231
 
 * @rule_id: the string identifier used previously
1232
 
 *
1233
 
 * Removes a stylesheet from midori settings.
1234
 
 *
1235
 
 * Since: 0.4.2
1236
 
 **/
1237
 
void
1238
 
midori_web_settings_remove_style (MidoriWebSettings* settings,
1239
 
                                  const gchar*       rule_id)
1240
 
{
1241
 
    gchar* str;
1242
 
 
1243
 
    g_return_if_fail (MIDORI_IS_WEB_SETTINGS (settings));
1244
 
    g_return_if_fail (rule_id != NULL);
1245
 
 
1246
 
    if (settings->user_stylesheets != NULL)
1247
 
    {
1248
 
        if ((str = g_hash_table_lookup (settings->user_stylesheets, rule_id)))
1249
 
        {
1250
 
            guint len = strlen (str);
1251
 
            g_hash_table_remove (settings->user_stylesheets, rule_id);
1252
 
            midori_web_settings_process_stylesheets (settings, -len);
1253
 
        }
1254
 
    }
1255
 
}
1256
 
 
1257
1133
/**
1258
1134
 * midori_settings_new_full:
1259
1135
 * @extensions: (out) (allow-none): a pointer into which