summaryrefslogtreecommitdiff
path: root/src/ext-proxy.c
blob: 51d2eb022a45616d87f3f68155f5d713fcf8c870 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
 * vimb - a webkit based vim like browser.
 *
 * Copyright (C) 2012-2018 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 <gio/gio.h>
#include <glib.h>

#include "ext-proxy.h"
#include "main.h"
#include "webextension/ext-main.h"

static gboolean on_authorize_authenticated_peer(GDBusAuthObserver *observer,
        GIOStream *stream, GCredentials *credentials, gpointer data);
static gboolean on_new_connection(GDBusServer *server,
        GDBusConnection *connection, gpointer data);
static void on_connection_close(GDBusConnection *connection, gboolean
        remote_peer_vanished, GError *error, gpointer data);
static void on_proxy_created (GDBusProxy *proxy, GAsyncResult *result,
        gpointer data);
static void on_vertical_scroll(GDBusConnection *connection,
        const char *sender_name, const char *object_path,
        const char *interface_name, const char *signal_name,
        GVariant *parameters, gpointer data);
static void dbus_call(Client *c, const char *method, GVariant *param,
        GAsyncReadyCallback callback);
static GVariant *dbus_call_sync(Client *c, const char *method, GVariant
        *param);
static void on_web_extension_page_created(GDBusConnection *connection,
        const char *sender_name, const char *object_path,
        const char *interface_name, const char *signal_name,
        GVariant *parameters, gpointer data);

/* TODO we need potentially multiple proxies. Because a single instance of
 * vimb may hold multiple clients which may use more than one webprocess and
 * therefore multiple webextension instances. */
extern struct Vimb vb;
static GDBusServer *dbusserver;


/**
 * Initialize the dbus proxy by watching for appearing dbus name.
 */
const char *ext_proxy_init(void)
{
    char *address, *guid;
    GDBusAuthObserver *observer;
    GError *error = NULL;

    address  = g_strdup_printf("unix:tmpdir=%s", g_get_tmp_dir());
    guid     = g_dbus_generate_guid();
    observer = g_dbus_auth_observer_new();

    g_signal_connect(observer, "authorize-authenticated-peer",
            G_CALLBACK(on_authorize_authenticated_peer), NULL);

    /* Use sync call because server must be starte before the web extension
     * attempt to connect */
    dbusserver = g_dbus_server_new_sync(address, G_DBUS_SERVER_FLAGS_NONE,
            guid, observer, NULL, &error);

    if (error) {
        g_warning("Failed to start web extension server on %s: %s", address, error->message);
        g_error_free(error);
        goto out;
    }

    g_signal_connect(dbusserver, "new-connection", G_CALLBACK(on_new_connection), NULL);
    g_dbus_server_start(dbusserver);

out:
    g_free(address);
    g_free(guid);
    g_object_unref(observer);

    return g_dbus_server_get_client_address(dbusserver);
}

/* TODO move this to a lib or somthing that can be used from ui and web
 * process together */
static gboolean on_authorize_authenticated_peer(GDBusAuthObserver *observer,
        GIOStream *stream, GCredentials *credentials, gpointer data)
{
    gboolean authorized = FALSE;

    if (credentials) {
        GCredentials *own_credentials;

        GError *error   = NULL;
        own_credentials = g_credentials_new();
        if (g_credentials_is_same_user(credentials, own_credentials, &error)) {
            authorized = TRUE;
        } else {
            g_warning("Failed to authorize web extension connection: %s", error->message);
            g_error_free(error);
        }
        g_object_unref(own_credentials);
    } else {
        g_warning ("No credentials received from web extension.\n");
    }

    return authorized;
}

static gboolean on_new_connection(GDBusServer *server,
        GDBusConnection *connection, gpointer data)
{
    /* Create dbus proxy. */
    g_return_val_if_fail(G_IS_DBUS_CONNECTION(connection), FALSE);

    g_signal_connect(connection, "closed", G_CALLBACK(on_connection_close), NULL);

    g_dbus_proxy_new(connection,
            G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES|G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
            NULL,
            NULL,
            VB_WEBEXTENSION_OBJECT_PATH,
            VB_WEBEXTENSION_INTERFACE,
            NULL,
            (GAsyncReadyCallback)on_proxy_created,
            NULL);

    return TRUE;
}

static void on_connection_close(GDBusConnection *connection, gboolean
        remote_peer_vanished, GError *error, gpointer data)
{
    if (error && !remote_peer_vanished) {
        g_warning("Unexpected lost connection to web extension: %s", error->message);
    }
}

static void on_proxy_created(GDBusProxy *new_proxy, GAsyncResult *result,
        gpointer data)
{
    GError *error = NULL;
    GDBusProxy *proxy;
    GDBusConnection *connection;

    proxy = g_dbus_proxy_new_finish(result, &error);
    if (!proxy) {
        if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
            g_warning("Error creating web extension proxy: %s", error->message);
        }
        g_error_free(error);

        /* TODO cancel the dbus connection - use cancelable */
        return;
    }

    connection = g_dbus_proxy_get_connection(proxy);
    g_dbus_connection_signal_subscribe(connection, NULL,
            VB_WEBEXTENSION_INTERFACE, "PageCreated",
            VB_WEBEXTENSION_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
            (GDBusSignalCallback)on_web_extension_page_created, proxy,
            NULL);
}

/**
 * Listen to the VerticalScroll signal of the webextension and set the scroll
 * percent value on the client to update the statusbar.
 */
static void on_vertical_scroll(GDBusConnection *connection,
        const char *sender_name, const char *object_path,
        const char *interface_name, const char *signal_name,
        GVariant *parameters, gpointer data)
{
    glong max, top;
    guint percent;
    guint64 pageid;
    Client *c;

    g_variant_get(parameters, "(ttqt)", &pageid, &max, &percent, &top);
    c = vb_get_client_for_page_id(pageid);
    if (c) {
        c->state.scroll_max     = max;
        c->state.scroll_percent = percent;
        c->state.scroll_top     = top;
    }

    vb_statusbar_update(c);
}

void ext_proxy_eval_script(Client *c, char *js, GAsyncReadyCallback callback)
{
    if (callback) {
        dbus_call(c, "EvalJs", g_variant_new("(ts)", c->page_id, js), callback);
    } else {
        dbus_call(c, "EvalJsNoResult", g_variant_new("(ts)", c->page_id, js), NULL);
    }
}

GVariant *ext_proxy_eval_script_sync(Client *c, char *js)
{
    return dbus_call_sync(c, "EvalJs", g_variant_new("(ts)", c->page_id, js));
}

/**
 * Request the web extension to focus first editable element.
 * Returns whether an focusable element was found or not.
 */
void ext_proxy_focus_input(Client *c)
{
    dbus_call(c, "FocusInput", g_variant_new("(t)", c->page_id), NULL);
}

/**
 * Send the headers string to the webextension.
 */
void ext_proxy_set_header(Client *c, const char *headers)
{
    dbus_call(c, "SetHeaderSetting", g_variant_new("(s)", headers), NULL);
}

void ext_proxy_lock_input(Client *c, const char *element_id)
{
    dbus_call(c, "LockInput", g_variant_new("(ts)", c->page_id, element_id), NULL);
}

void ext_proxy_unlock_input(Client *c, const char *element_id)
{
    dbus_call(c, "UnlockInput", g_variant_new("(ts)", c->page_id, element_id), NULL);
}

/**
 * Call a dbus method.
 */
static void dbus_call(Client *c, const char *method, GVariant *param,
        GAsyncReadyCallback callback)
{
    /* TODO add function to queue calls until the proxy connection is
     * established */
    if (!c->dbusproxy) {
        return;
    }
    g_dbus_proxy_call(c->dbusproxy, method, param, G_DBUS_CALL_FLAGS_NONE, -1, NULL, callback, c);
}

/**
 * Call a dbus method syncron.
 */
static GVariant *dbus_call_sync(Client *c, const char *method, GVariant *param)
{
    GVariant *result = NULL;
    GError *error = NULL;

    if (!c->dbusproxy) {
        return NULL;
    }

    result = g_dbus_proxy_call_sync(c->dbusproxy, method, param,
        G_DBUS_CALL_FLAGS_NONE, 500, NULL, &error);

    if (error) {
        g_warning("Failed dbus method %s: %s", method, error->message);
        g_error_free(error);
    }

    return result;
}

/**
 * Called when the web context created the page.
 *
 * Find the right client to the page id returned from the webextension.
 * Add the proxy connection to the client for later calls.
 */
static void on_web_extension_page_created(GDBusConnection *connection,
        const char *sender_name, const char *object_path,
        const char *interface_name, const char *signal_name,
        GVariant *parameters, gpointer data)
{
    Client *c;
    guint64 pageid;

    g_variant_get(parameters, "(t)", &pageid);

    /* Search for the client with the same page id as returned by the
     * webextension. */
    c = vb_get_client_for_page_id(pageid);
    if (c) {
        /* Set the dbus proxy on the right client based on page id. */
        c->dbusproxy = (GDBusProxy*)data;

        /* Subscribe to dbus signals here. */
        g_dbus_connection_signal_subscribe(connection, NULL,
                VB_WEBEXTENSION_INTERFACE, "VerticalScroll",
                VB_WEBEXTENSION_OBJECT_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE,
                (GDBusSignalCallback)on_vertical_scroll, NULL, NULL);
    }
}