summaryrefslogtreecommitdiff
path: root/src/session.c
blob: 632ced16d81fad046c1ac26a41d91bcc7c3eb702 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
 * vimb - a webkit based vim like browser.
 *
 * Copyright (C) 2012-2013 Daniel Carl
 *
 * 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 <sys/file.h>
#include "main.h"
#include "session.h"

#ifdef FEATURE_COOKIE

#define COOKIEJAR_TYPE (cookiejar_get_type())
#define COOKIEJAR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), COOKIEJAR_TYPE, CookieJar))

typedef struct {
    SoupCookieJarText parent_instance;
    int lock;
} CookieJar;

typedef struct {
    SoupCookieJarTextClass parent_class;
} CookieJarClass;

static GType cookiejar_get_type(void);

G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)

static SoupCookieJar *cookiejar_new(const char *file, gboolean ro);
static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old, SoupCookie *new);
static void cookiejar_class_init(CookieJarClass *class);
static void cookiejar_finalize(GObject *self);
static void cookiejar_init(CookieJar *self);
static void cookiejar_set_property(GObject *self, guint prop_id,
    const GValue *value, GParamSpec *pspec);
#endif

extern VbCore vb;
extern const unsigned int SETTING_MAX_CONNS;
extern const unsigned int SETTING_MAX_CONNS_PER_HOST;


void session_init(void)
{
    /* init soup session */
    vb.session = webkit_get_default_session();
    g_object_set(vb.session, "max-conns", SETTING_MAX_CONNS , NULL);
    g_object_set(vb.session, "max-conns-per-host", SETTING_MAX_CONNS_PER_HOST, NULL);
    g_object_set(vb.session, "accept-language-auto", true, NULL);

#ifdef FEATURE_COOKIE
    soup_session_add_feature(
        vb.session,
        SOUP_SESSION_FEATURE(cookiejar_new(vb.files[FILES_COOKIE], false))
    );
#endif
}

#ifdef FEATURE_COOKIE
static SoupCookieJar *cookiejar_new(const char *file, gboolean ro)
{
    return g_object_new(
        COOKIEJAR_TYPE,
        SOUP_COOKIE_JAR_TEXT_FILENAME, file,
        SOUP_COOKIE_JAR_READ_ONLY, ro,
        SOUP_COOKIE_JAR_ACCEPT_POLICY, SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY,
        NULL
    );
}

static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupCookie *new_cookie)
{
    flock(COOKIEJAR(self)->lock, LOCK_EX);
    if (new_cookie && !new_cookie->expires) {
        soup_cookie_set_expires(new_cookie, soup_date_new_from_now(vb.config.cookie_timeout));
    }
    SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self, old_cookie, new_cookie);
    flock(COOKIEJAR(self)->lock, LOCK_UN);
}

static void cookiejar_class_init(CookieJarClass *class)
{
    SOUP_COOKIE_JAR_CLASS(class)->changed = cookiejar_changed;
    G_OBJECT_CLASS(class)->get_property   = G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
    G_OBJECT_CLASS(class)->set_property   = cookiejar_set_property;
    G_OBJECT_CLASS(class)->finalize       = cookiejar_finalize;
    g_object_class_override_property(G_OBJECT_CLASS(class), 1, "filename");
}

static void cookiejar_finalize(GObject *self)
{
    close(COOKIEJAR(self)->lock);
    G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
}

static void cookiejar_init(CookieJar *self)
{
    self->lock = open(vb.files[FILES_COOKIE], 0);
}

static void cookiejar_set_property(GObject *self, guint prop_id, const
    GValue *value, GParamSpec *pspec)
{
    flock(COOKIEJAR(self)->lock, LOCK_SH);
    G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id, value, pspec);
    flock(COOKIEJAR(self)->lock, LOCK_UN);
}
#endif