code.pablomurad.com

skull-browser

Keyboard-driven browser for the small web, with native Gopher and Gemini

web_context.c raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * web_context.c - WebKit web context setup and handling
 *
 * Copyright © 2016 Aidan Holm <aidanholm@gmail.com>
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "globalconf.h"
#include "common/log.h"
#include "web_context.h"

#include <webkit2/webkit2.h>

/** WebKit context common to all web views */
static WebKitWebContext *web_context;
/** Whether the web context startup function has been run */
static gboolean web_context_started = FALSE;

/** Defined in widgets/webview/downloads.c */
gboolean download_start_cb(WebKitWebContext *, WebKitDownload *, gpointer);

WebKitWebContext *
web_context_get(void)
{
    g_assert(web_context);
    return web_context;
}

static void
website_data_manager_init(void)
{
    WebKitWebsiteDataManager *data_mgr = webkit_website_data_manager_new(
            "base-cache-directory", globalconf.cache_dir,
            "base-data-directory", globalconf.data_dir,
            NULL);

    web_context = webkit_web_context_new_with_website_data_manager(data_mgr);

    verbose("base_data_directory:                 %s", webkit_website_data_manager_get_base_data_directory(data_mgr));
    verbose("base_cache_directory:                %s", webkit_website_data_manager_get_base_cache_directory(data_mgr));
}

static void
web_context_set_default_spelling_language(void)
{
    /* This seems to autodetect spell checking languages */
    const gchar *null = NULL;
    webkit_web_context_set_spell_checking_languages(web_context, &null);
    gchar **ret = (gchar**)webkit_web_context_get_spell_checking_languages(web_context);
    if (!ret)
        return;
    gchar *langs = g_strjoinv(", ", ret);
    verbose("setting spell check languages: %s", langs);
    g_free(langs);
}

void
web_context_init(void)
{
    website_data_manager_init();
    webkit_web_context_set_favicon_database_directory(web_context, NULL);
    g_signal_connect(G_OBJECT(web_context), "download-started",
            G_CALLBACK(download_start_cb), NULL);

    /* Set default cookie policy: must match default in clib/soup.c */
    WebKitCookieManager *cookie_mgr = webkit_web_context_get_cookie_manager(web_context);
    webkit_cookie_manager_set_accept_policy(cookie_mgr, WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY);

    web_context_set_default_spelling_language();
}

void
web_context_init_finish(void)
{
    if (web_context_started)
        return;

    web_context_started = TRUE;
}

// vim: ft=c:et:sw=4:ts=8:sts=4:tw=80