aboutsummaryrefslogtreecommitdiffstats
path: root/src/ephy-dbus.c
blob: ea33a076bf576b304c7dc9c5be3ea9e25b0a94c2 (plain) (blame)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
 *  Copyright (C) 2004, 2005 Jean-François Rameau
 *
 *  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 2, 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *  $Id$
 */

#include "config.h"

#include "ephy-dbus.h"
#include "ephy-type-builtins.h"
#include "ephy-marshal.h"
#include "ephy-debug.h"
#include "ephy-activation.h"
#include "ephy-dbus-server-bindings.h"

#include <string.h>
#include <dbus/dbus-glib-bindings.h>

/* dbus 0.6 API change */
#ifndef DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT
#define DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT 0
#endif

#define RECONNECT_DELAY 3000

#define EPHY_DBUS_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EPHY_TYPE_DBUS, EphyDbusPrivate))

struct _EphyDbusPrivate
{
    DBusGConnection *session_bus;
    DBusGConnection *system_bus;
    guint session_reconnect_timeout_id;
    guint system_reconnect_timeout_id;
    guint is_session_service_owner : 1;
};

enum
{
    CONNECTED,
    DISCONNECTED,
    LAST_SIGNAL
};

static EphyDbus *ephy_dbus_instance;

static guint signals[LAST_SIGNAL];
static GObjectClass *parent_class;
GQuark ephy_dbus_error_quark;

/* Filter signals form session bus */
static DBusHandlerResult session_filter_func (DBusConnection *connection,
                              DBusMessage *message,
                              void *user_data);
/* Filter signals from system bus */
static DBusHandlerResult system_filter_func (DBusConnection *connection,
                             DBusMessage *message,
                             void *user_data);

/* Handler for NetworkManager's "DevicesChanged" signals */
static void ephy_dbus_nm_devices_changed_cb (DBusGProxy *proxy,
                         const char *string,
                         EphyDbus *ephy_dbus);

/* Both  connect to their respective bus */
static gboolean ephy_dbus_connect_to_session_bus (EphyDbus*, GError**);
static gboolean ephy_dbus_connect_to_system_bus  (EphyDbus*, GError**);

/* implementation of the DBUS helpers */

static gboolean
ephy_dbus_connect_to_session_bus_cb (gpointer user_data)
{
    EphyDbus *dbus = EPHY_DBUS (user_data);
    GError *error = NULL;

    if (!ephy_dbus_connect_to_session_bus (dbus, &error))
    {
        g_error_free (error);

        /* try again */
        return TRUE;
    }

    dbus->priv->session_reconnect_timeout_id = 0;

    /* we're done */
    return FALSE;
}

static gboolean
ephy_dbus_connect_to_system_bus_cb (gpointer user_data)
{
    EphyDbus *dbus = EPHY_DBUS (user_data);
    GError *error = NULL;

    if (!ephy_dbus_connect_to_system_bus (dbus, &error))
    {
        g_error_free (error);

        /* try again */
        return TRUE;
    }

    dbus->priv->system_reconnect_timeout_id = 0;

    /* we're done */
    return FALSE;
}

static DBusHandlerResult
session_filter_func (DBusConnection *connection,
                 DBusMessage *message,
                 void *user_data)
{
    EphyDbus *ephy_dbus = EPHY_DBUS (user_data);

    if (dbus_message_is_signal (message,
                    DBUS_INTERFACE_LOCAL,
                    "Disconnected"))
    {
        LOG ("EphyDbus disconnected from session bus");

        dbus_g_connection_unref (ephy_dbus->priv->session_bus);
        ephy_dbus->priv->session_bus = NULL;

        g_signal_emit (ephy_dbus, signals[DISCONNECTED], 0, EPHY_DBUS_SESSION);

        /* try to reconnect later ... */
        ephy_dbus->priv->session_reconnect_timeout_id =
            g_timeout_add (RECONNECT_DELAY,
                       (GSourceFunc) ephy_dbus_connect_to_session_bus_cb,
                       ephy_dbus);

        return DBUS_HANDLER_RESULT_HANDLED;
    }

    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static DBusHandlerResult
system_filter_func (DBusConnection *connection,
                DBusMessage *message,
                void *user_data)
{
    EphyDbus *ephy_dbus = EPHY_DBUS (user_data);

    LOG ("EphyDbus filtering message from system bus");

    if (dbus_message_is_signal (message,
                    DBUS_INTERFACE_LOCAL,
                    "Disconnected"))
    {
        LOG ("EphyDbus disconnected from system bus");

        dbus_g_connection_unref (ephy_dbus->priv->system_bus);
        ephy_dbus->priv->system_bus = NULL;

        g_signal_emit (ephy_dbus, signals[DISCONNECTED], 0, EPHY_DBUS_SYSTEM);

        /* try to reconnect later ... */
        ephy_dbus->priv->system_reconnect_timeout_id =
            g_timeout_add (RECONNECT_DELAY,
                       (GSourceFunc) ephy_dbus_connect_to_system_bus_cb,
                       ephy_dbus);

        return DBUS_HANDLER_RESULT_HANDLED;
    }

    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static gboolean
ephy_dbus_connect_to_system_bus (EphyDbus *ephy_dbus,
                 GError **error)
{
    DBusGProxy *proxy;

    LOG ("EphyDbus connecting to system DBUS");

    ephy_dbus->priv->system_bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, error);
    if (ephy_dbus->priv->system_bus == NULL)
    {
        g_warning ("Unable to connect to system bus: %s", error ? (*error)->message : "");
        return FALSE;
    }

    if (dbus_g_connection_get_connection (ephy_dbus->priv->system_bus) == NULL)
    {
        g_warning ("DBus connection is null");
        g_set_error (error,
                 EPHY_DBUS_ERROR_QUARK,
                 0,
                 "DBus connection is NULL");
        return FALSE;
    }

    dbus_connection_set_exit_on_disconnect 
        (dbus_g_connection_get_connection (ephy_dbus->priv->system_bus),
         FALSE);

    dbus_connection_add_filter
        (dbus_g_connection_get_connection (ephy_dbus->priv->system_bus),
         system_filter_func, ephy_dbus, NULL);

    proxy = dbus_g_proxy_new_for_name (ephy_dbus->priv->system_bus,
                       DBUS_NETWORK_MANAGER_SERVICE,
                       DBUS_NETWORK_MANAGER_PATH,
                       DBUS_NETWORK_MANAGER_INTERFACE);

    dbus_g_proxy_add_signal (proxy, "DevicesChanged", G_TYPE_STRING,
                 G_TYPE_INVALID);
    dbus_g_proxy_connect_signal (proxy, "DevicesChanged",
                     G_CALLBACK (ephy_dbus_nm_devices_changed_cb),
                     ephy_dbus, NULL);

    g_object_unref (proxy);

    g_signal_emit (ephy_dbus, signals[CONNECTED], 0, EPHY_DBUS_SYSTEM);

    return TRUE;
}

static gboolean
ephy_dbus_connect_to_session_bus (EphyDbus *ephy_dbus,
                  GError **error)
{
    DBusGProxy *proxy;
    guint request_ret;
    
    LOG ("EphyDbus connecting to session DBUS");

    /* Init the DBus connection */
    ephy_dbus->priv->session_bus = dbus_g_bus_get (DBUS_BUS_SESSION, error);
    if (ephy_dbus->priv->session_bus == NULL)
    {
        g_warning("Unable to connect to session bus: %s", error ? (*error)->message : "");
        return FALSE;
    }

    dbus_connection_set_exit_on_disconnect 
        (dbus_g_connection_get_connection (ephy_dbus->priv->session_bus),
         FALSE);

    dbus_connection_add_filter
        (dbus_g_connection_get_connection (ephy_dbus->priv->session_bus),
         session_filter_func, ephy_dbus, NULL);
    
    dbus_g_object_type_install_info (EPHY_TYPE_DBUS,
                     &dbus_glib_ephy_activation_object_info);

    /* Register DBUS path */
    dbus_g_connection_register_g_object (ephy_dbus->priv->session_bus,
                         DBUS_EPHY_PATH,
                         G_OBJECT (ephy_dbus));

    /* Register the service name, the constant here are defined in dbus-glib-bindings.h */
    proxy = dbus_g_proxy_new_for_name (ephy_dbus->priv->session_bus,
                       DBUS_SERVICE_DBUS,
                       DBUS_PATH_DBUS,
                       DBUS_INTERFACE_DBUS);

    if (!org_freedesktop_DBus_request_name (proxy,
                        DBUS_EPHY_SERVICE,
                        DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT,
                        &request_ret, error))
    {
        /* We have a BIG problem! */
        g_warning ("RequestName failed: %s\n", error ? (*error)->message : "");
        return FALSE;
    }

    if (request_ret == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
    {
        ephy_dbus->priv->is_session_service_owner = TRUE;
    }
    else if (request_ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
    {
        ephy_dbus->priv->is_session_service_owner = FALSE;
    }
    else
    {
        /* FIXME can this happen? */
        g_assert_not_reached ();
    }

    LOG ("Instance is %ssession bus owner.", ephy_dbus->priv->is_session_service_owner ? "" : "NOT ");

    g_object_unref (proxy);

    return TRUE;
}

static void
ephy_dbus_disconnect_bus (DBusGConnection *bus)
{
    if (bus != NULL) {
        dbus_connection_close
            (dbus_g_connection_get_connection (bus));
        dbus_g_connection_unref (bus);
    }
}

static void
ephy_dbus_nm_devices_changed_cb (DBusGProxy *proxy,
                 const char *device,
                 EphyDbus *ephy_dbus)
{
    GError *error = NULL;
    char *status;

    /* query status from network manager */
    dbus_g_proxy_call (proxy, "status", &error, G_TYPE_INVALID,
               G_TYPE_STRING, &status,
               G_TYPE_INVALID);
    if (status != NULL)
    {
        g_warning ("NetworkManager's DBus \"status()\" call returned \"null\": %s",
               error->message);
        g_error_free (error);
        return;
    }

    if (strcmp ("connected", status) == 0)
    {
        /* change ephy's status to online */
    }
    else if (strcmp ("disconnected", status) == 0)
    {
        /* change ephy's status to offline */
    }
}

/* Public methods */

static void
ephy_dbus_shutdown (EphyDbus *dbus)
{
    LOG ("EphyDbus shutdown");

    if (dbus->priv->session_reconnect_timeout_id != 0)
    {
        g_source_remove (dbus->priv->session_reconnect_timeout_id);
        dbus->priv->session_reconnect_timeout_id = 0;
    }

    if (dbus->priv->system_reconnect_timeout_id != 0)
    {
        g_source_remove (dbus->priv->system_reconnect_timeout_id);
        dbus->priv->system_reconnect_timeout_id = 0;
    }

    if (dbus->priv->session_bus)
    {
        dbus_connection_remove_filter
            (dbus_g_connection_get_connection (dbus->priv->session_bus),
             session_filter_func, dbus);
        ephy_dbus_disconnect_bus (dbus->priv->session_bus);
        dbus->priv->session_bus = NULL;
    }

        if (dbus->priv->system_bus)
    {
        dbus_connection_remove_filter
            (dbus_g_connection_get_connection (dbus->priv->system_bus),
             system_filter_func, dbus);
        ephy_dbus_disconnect_bus (dbus->priv->system_bus);
        dbus->priv->system_bus = NULL;
    }
}

/* Class implementation */

static void
ephy_dbus_init (EphyDbus *dbus)
{
    dbus->priv = EPHY_DBUS_GET_PRIVATE (dbus);

    LOG ("EphyDbus initialising");
}

static void
ephy_dbus_finalize (GObject *object)
{
    EphyDbus *dbus = EPHY_DBUS (object);

    /* Have to do this after the object's weak ref notifiers have
     * been called, see https://bugs.freedesktop.org/show_bug.cgi?id=5688
     */
    ephy_dbus_shutdown (dbus);

    LOG ("EphyDbus finalised");

    parent_class->finalize (object);
}

static void
ephy_dbus_class_init (EphyDbusClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    parent_class = g_type_class_peek_parent (klass);

    object_class->finalize = ephy_dbus_finalize;

    signals[CONNECTED] =
        g_signal_new ("connected",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (EphyDbusClass, connected),
                  NULL, NULL,
                  ephy_marshal_VOID__ENUM,
                  G_TYPE_NONE,
                  1,
                  EPHY_TYPE_DBUS_BUS);

    signals[DISCONNECTED] =
        g_signal_new ("disconnected",
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (EphyDbusClass, disconnected),
                  NULL, NULL,
                  ephy_marshal_VOID__ENUM,
                  G_TYPE_NONE,
                  1,
                  EPHY_TYPE_DBUS_BUS);

    g_type_class_add_private (object_class, sizeof(EphyDbusPrivate));
}

GType
ephy_dbus_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0))
    {
        static const GTypeInfo our_info =
        {
            sizeof (EphyDbusClass),
            NULL, /* base_init */
            NULL, /* base_finalize */
            (GClassInitFunc) ephy_dbus_class_init,
            NULL,
            NULL, /* class_data */
            sizeof (EphyDbus),
            0, /* n_preallocs */
            (GInstanceInitFunc) ephy_dbus_init
        };

        type = g_type_register_static (G_TYPE_OBJECT,
                           "EphyDbus",
                           &our_info, 0);
    }

    return type;
}

EphyDbus *
ephy_dbus_get_default (void)
{
    g_assert (ephy_dbus_instance != NULL);

    return ephy_dbus_instance;
}

DBusGConnection *
ephy_dbus_get_bus (EphyDbus *dbus,
           EphyDbusBus kind)
{
    DBusGConnection *bus = NULL;

    g_return_val_if_fail (EPHY_IS_DBUS (dbus), NULL);

    if (kind == EPHY_DBUS_SYSTEM)
    {
        /* We connect lazily to the system bus */
        if (dbus->priv->system_bus == NULL)
        {
            ephy_dbus_connect_to_system_bus (dbus, NULL);
        }
        bus = dbus->priv->system_bus;
    }
    else if (kind == EPHY_DBUS_SESSION)
    {
        bus = dbus->priv->session_bus;
    }
    else
    {
        g_assert_not_reached ();
    }

    g_assert (bus != NULL);

    return bus;
}

DBusGProxy *
ephy_dbus_get_proxy (EphyDbus *dbus,
             EphyDbusBus kind)
{
    DBusGConnection *bus = NULL;

    g_return_val_if_fail (EPHY_IS_DBUS (dbus), NULL);
    
    bus = ephy_dbus_get_bus (dbus, kind);

    if (bus == NULL)
    {
        g_warning ("Unable to get proxy for the %s bus.\n",
               kind == EPHY_DBUS_SESSION ? "session" : "system");
        return NULL;
    }

    return dbus_g_proxy_new_for_name (bus,
                      DBUS_EPHY_SERVICE,
                      DBUS_EPHY_PATH,
                      DBUS_EPHY_INTERFACE);
}

/* private API */

gboolean
_ephy_dbus_startup (GError **error)
{
    g_assert (ephy_dbus_instance == NULL);

    ephy_dbus_error_quark = g_quark_from_static_string ("ephy-dbus-error");
        
    ephy_dbus_instance = g_object_new (EPHY_TYPE_DBUS, NULL);

    /* We only connect to the session bus on startup*/
    return ephy_dbus_connect_to_session_bus (ephy_dbus_instance, error);
}

void
_ephy_dbus_release (void)
{
    g_assert (ephy_dbus_instance != NULL);

    g_object_unref (ephy_dbus_instance);
    ephy_dbus_instance = NULL;
}

gboolean
_ephy_dbus_is_name_owner (void)
{
    g_assert (ephy_dbus_instance != NULL);

    return ephy_dbus_instance->priv->is_session_service_owner;
}