summaryrefslogtreecommitdiff
path: root/dbus-1/dconf-dbus-1.c
blob: 64e02c4d2dc48804f85ddb726d57e5e5d89ff235 (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
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/**
 * Copyright © 2010 Canonical Limited
 *
 * 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 licence, 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/>.
 *
 * Author: Ryan Lortie <desrt@desrt.ca>
 **/

#include "dconf-dbus-1.h"

#include <dconf-engine.h>

#include <string.h>

typedef struct _Outstanding Outstanding;

struct _DConfDBusClient
{
  DBusConnection *session_bus;
  DBusConnection *system_bus;
  GSList *watches;
  gint ref_count;

  Outstanding *outstanding;
  gchar *anti_expose_tag;

  DConfEngine *engine;
};

static void
dconf_dbus_client_add_value_to_iter (DBusMessageIter *iter,
                                     GVariant        *value)
{
  GVariantClass class;

  class = g_variant_classify (value);

  switch (class)
    {
    case G_VARIANT_CLASS_BOOLEAN:
      {
        dbus_bool_t boolean;

        boolean = g_variant_get_boolean (value);
        dbus_message_iter_append_basic (iter, 'b', &boolean);
      }
      break;

    case G_VARIANT_CLASS_BYTE:
    case G_VARIANT_CLASS_INT16:
    case G_VARIANT_CLASS_UINT16:
    case G_VARIANT_CLASS_INT32:
    case G_VARIANT_CLASS_UINT32:
    case G_VARIANT_CLASS_INT64:
    case G_VARIANT_CLASS_UINT64:
    case G_VARIANT_CLASS_DOUBLE:
      dbus_message_iter_append_basic (iter, class,
                                      g_variant_get_data (value));
      break;

    case G_VARIANT_CLASS_STRING:
    case G_VARIANT_CLASS_OBJECT_PATH:
    case G_VARIANT_CLASS_SIGNATURE:
      {
        const gchar *str;

        str = g_variant_get_string (value, NULL);
        dbus_message_iter_append_basic (iter, class, &str);
      }
      break;

    case G_VARIANT_CLASS_ARRAY:
      {
        const gchar *contained;
        DBusMessageIter sub;
        gint i, n;

        contained = g_variant_get_type_string (value) + 1;
        n = g_variant_n_children (value);
        dbus_message_iter_open_container (iter, 'a', contained, &sub);
        for (i = 0; i < n; i++)
          {
            GVariant *child;

            child = g_variant_get_child_value (value, i);
            dconf_dbus_client_add_value_to_iter (&sub, child);
            g_variant_unref (child);
          }
          
        dbus_message_iter_close_container (iter, &sub);
      }
      break;

    case G_VARIANT_CLASS_TUPLE:
      {
        DBusMessageIter sub;
        gint i, n;

        n = g_variant_n_children (value);
        dbus_message_iter_open_container (iter, 'r', NULL, &sub);
        for (i = 0; i < n; i++)
          {
            GVariant *child;

            child = g_variant_get_child_value (value, i);
            dconf_dbus_client_add_value_to_iter (&sub, child);
            g_variant_unref (child);
          }
          
        dbus_message_iter_close_container (iter, &sub);
      }
      break;

    case G_VARIANT_CLASS_DICT_ENTRY:
      {
        DBusMessageIter sub;
        gint i;

        dbus_message_iter_open_container (iter, 'e', NULL, &sub);
        for (i = 0; i < 2; i++)
          {
            GVariant *child;

            child = g_variant_get_child_value (value, i);
            dconf_dbus_client_add_value_to_iter (&sub, child);
            g_variant_unref (child);
          }
          
        dbus_message_iter_close_container (iter, &sub);
      }
      break;

    case G_VARIANT_CLASS_VARIANT:
      {
        DBusMessageIter sub;
        GVariant *child;

        child = g_variant_get_variant (value);
        dbus_message_iter_open_container (iter, 'v',
                                          g_variant_get_type_string (child),
                                          &sub);
        dconf_dbus_client_add_value_to_iter (&sub, child);
        dbus_message_iter_close_container (iter, &sub);
        g_variant_unref (child);
      }
      break;

    default:
      g_assert_not_reached ();
    }
}

static void
dconf_dbus_client_send (DConfDBusClient               *dcdbc,
                        DConfEngineMessage            *dcem,
                        DBusPendingCallNotifyFunction  callback,
                        gpointer                       user_data)
{
  DBusConnection *connection;
  gint i;

  for (i = 0; i < dcem->n_messages; i++)
    {
      switch (dcem->bus_types[i])
        {
        case 'e':
          connection = dcdbc->session_bus;
          break;

        case 'y':
          connection = dcdbc->system_bus;
          break;

        default:
          g_assert_not_reached ();
        }

      if (connection == NULL && callback != NULL)
        callback (NULL, user_data);

      if (connection != NULL)
        {
          DBusPendingCall *pending;
          DBusMessageIter diter;
          DBusMessage *message;
          GVariantIter giter;
          GVariant *child;

          message = dbus_message_new_method_call (dcem->bus_name,
                                                  dcem->object_path,
                                                  dcem->interface_name,
                                                  dcem->method_name);

          dbus_message_iter_init_append (message, &diter);
          g_variant_iter_init (&giter, dcem->parameters[i]);

          while ((child = g_variant_iter_next_value (&giter)))
            {
              dconf_dbus_client_add_value_to_iter (&diter, child);
              g_variant_unref (child);
            }

          dbus_connection_send_with_reply (connection, message,
                                           &pending, 120000);
          dbus_pending_call_set_notify (pending, callback, user_data, NULL);
          dbus_message_unref (message);
        }
    }
}

static GVariant *
dconf_dbus_client_send_finish (DBusPendingCall *pending)
{
  DBusMessage *message;
  GVariant *result;

  if (pending == NULL)
    return NULL;

  message = dbus_pending_call_steal_reply (pending);
  dbus_pending_call_unref (pending);

  /* We only have to deal with two types of replies: () and (s) */
  if (dbus_message_has_signature (message, "s"))
    {
      dbus_message_get_args (message, NULL,
                             DBUS_TYPE_STRING, &result,
                             DBUS_TYPE_INVALID);
      result = g_variant_new ("(s)", result);
    }
  else
    result = g_variant_new ("()");

  dbus_message_unref (message);

  return result;
}

struct _Outstanding
{
  Outstanding *next;

  DConfDBusClient *dcdbc;
  DConfEngineMessage    dcem;

  gchar    *set_key;
  GVariant *set_value;
  GTree    *tree;
};

static void
dconf_dbus_client_outstanding_returned (DBusPendingCall *pending,
                                        gpointer         user_data)
{
  Outstanding *outstanding = user_data;
  DConfDBusClient *dcdbc;
  GVariant *reply;

  dcdbc = outstanding->dcdbc;

  /* One way or another we no longer need this hooked into the list.
   */
  {
    Outstanding **tmp;

    for (tmp = &dcdbc->outstanding; tmp; tmp = &(*tmp)->next)
      if (*tmp == outstanding)
        {
          *tmp = outstanding->next;
          break;
        }
  }

  reply = dconf_dbus_client_send_finish (pending);

  if (reply)
    {
      /* Success.
       *
       * We want to ensure that we don't emit an extra change
       * notification signal when we see the signal that the service is
       * about to send, so store the tag so we know to ignore it when
       * the signal comes.
       *
       * No thread safety issue here since this variable is only
       * accessed from the worker thread.
       */
      g_free (dcdbc->anti_expose_tag);

      if (g_variant_is_of_type (reply, G_VARIANT_TYPE ("(s)")))
        g_variant_get_child (reply, 0, "s", dcdbc->anti_expose_tag);

      g_variant_unref (reply);
    }
  else
    {
      /* An error of some kind.
       *
       * We already removed the outstanding entry from the list, so the
       * unmodified database is now visible to the client.  Change
       * notify so that they see it.
       */
      if (outstanding->set_key)
          /* XXX emit */;
      else
          /* XXX emit */;
    }

  dconf_engine_message_destroy (&outstanding->dcem);
  dconf_dbus_client_unref (outstanding->dcdbc);
  g_free (outstanding->set_key);

  if (outstanding->set_value)
    g_variant_unref (outstanding->set_value);

  if (outstanding->tree)
    g_tree_unref (outstanding->tree);

  g_slice_free (Outstanding, outstanding);
}

static void
dconf_dbus_client_queue (DConfDBusClient *dcdbc,
                         DConfEngineMessage   *dcem,
                         const gchar          *set_key,
                         GVariant             *set_value,
                         GTree                *tree)
{
  Outstanding *outstanding;

  outstanding = g_slice_new (Outstanding);
  outstanding->dcdbc = dconf_dbus_client_ref (dcdbc);
  outstanding->dcem = *dcem;

  outstanding->set_key = g_strdup (set_key);
  outstanding->set_value = set_value ? g_variant_ref_sink (set_value) : NULL;
  outstanding->tree = tree ? g_tree_ref (tree) : NULL;

  outstanding->next = dcdbc->outstanding;
  dcdbc->outstanding = outstanding;

  dconf_dbus_client_send (outstanding->dcdbc,
                               &outstanding->dcem,
                               dconf_dbus_client_outstanding_returned,
                               outstanding);
}

static gboolean
dconf_dbus_client_scan_outstanding_tree (GTree       *tree,
                                         const gchar *key,
                                         gsize        key_length,
                                         gpointer    *value)
{
  gchar *mykey;

  mykey = g_alloca (key_length + 1);
  memcpy (mykey, key, key_length + 1);

  while (!g_tree_lookup_extended (tree, mykey, NULL, value) &&
         --key_length)
    {
      while (mykey[key_length - 1] != '/')
        key_length--;

      mykey[key_length] = '\0';
    }

  return key_length != 0;
}

static gboolean
dconf_dbus_client_scan_outstanding (DConfDBusClient  *dcdbc,
                                    const gchar      *key,
                                    GVariant        **value)
{
  gboolean found = FALSE;
  Outstanding *node;
  gsize length;

  length = strlen (key);

  if G_LIKELY (dcdbc->outstanding == NULL)
    return FALSE;

  for (node = dcdbc->outstanding; node; node = node->next)
    {
      if (node->set_key)
        {
          if (strcmp (key, node->set_key) == 0)
            {
              if (node->set_value != NULL)
                *value = g_variant_ref (node->set_value);
              else
                *value = NULL;

              found = TRUE;
              break;
            }
        }

      else
        {
          gpointer result;

          if (dconf_dbus_client_scan_outstanding_tree (node->tree, key,
                                                            length, &result))
            {
              if (result)
                *value = g_variant_ref (result);
              else
                *value = NULL;

              found = TRUE;
              break;
            }
        }
    }

  return found;
}

/* Watches are reference counted because they can be held both by the
 * list of watches and by the pending watch registration.  In the normal
 * case, the registration completes before the watch is unsubscribed
 * from but it might be the case that the watch is unsubscribed from
 * before the AddMatch completes.  For that reason, either thing could
 * be responsible for freeing the watch structure; we solve that
 * ambiguity using a reference count.
 *
 * We just initially set it to 2, since these are the only two users.
 * That way we can skip having the ref() function.
 *
 * We also track the number of outstanding messages that we are waiting
 * to see before assuming that the watch has been established.  This is
 * set to the number of messages sent and decremented for each incoming
 * reply.  When it hits zero, we do one single unref.
 *
 * We need to keep this number separate from the refcount due to the
 * fact that we need to do some additional checking when the last reply
 * arrives (see code below).
 */
typedef struct
{
  DConfDBusClient *dcdbc;
  gchar           *name;
  DConfDBusNotify  notify;
  gpointer         user_data;
  guint64          initial_state;
  gint             ref_count;
  gint             n_messages;
} Watch;



static void
dconf_dbus_emit_change (DConfDBusClient *dcdbc,
                        const gchar     *key)
{
  GSList *iter;

  for (iter = dcdbc->watches; iter; iter = iter->next)
    {
      Watch *watch = iter->data;

      if (g_str_has_prefix (key, watch->name))
        watch->notify (dcdbc, key, watch->user_data);
    }
}

GVariant *
dconf_dbus_client_read (DConfDBusClient *dcdbc,
                        const gchar     *key)
{
  GVariant *value;

  if (dconf_dbus_client_scan_outstanding (dcdbc, key, &value))
    return value;

  return dconf_engine_read (dcdbc->engine, key);
}

gboolean
dconf_dbus_client_write (DConfDBusClient *dcdbc,
                         const gchar     *key,
                         GVariant        *value)
{
  DConfEngineMessage dcem;

  if (!dconf_engine_write (dcdbc->engine, key, value, &dcem, NULL))
    return FALSE;

  dconf_dbus_client_queue (dcdbc, &dcem, key, value, NULL);
  dconf_dbus_emit_change (dcdbc, key);

  return TRUE;
}

static Watch *
watch_new (DConfDBusClient *dcdbc,
           const gchar     *name,
           DConfDBusNotify  notify,
           gpointer         user_data)
{
  Watch *watch;

  watch = g_slice_new (Watch);
  watch->dcdbc = dconf_dbus_client_ref (dcdbc);
  watch->user_data = user_data;
  watch->name = g_strdup (name);
  watch->notify = notify;

  watch->initial_state = dconf_engine_get_state (dcdbc->engine);
  watch->ref_count = 2;

  dcdbc->watches = g_slist_prepend (dcdbc->watches, watch);

  return watch;
}

static void
watch_unref (Watch *watch)
{
  if (--watch->ref_count == 0)
    {
      dconf_dbus_client_unref (watch->dcdbc);
      g_free (watch->name);
      g_slice_free (Watch, watch);
    }
}

static void
add_match_done (DBusPendingCall *pending,
                gpointer         user_data)
{
  Watch *watch = user_data;
  GVariant *reply;

  reply = dconf_dbus_client_send_finish (pending);

  if (reply == NULL)
    {
      /* This is extremely unlikely to happen and it happens
       * asynchronous to the user's call.  Since the user doesn't know
       * that it happened, we pretend that it didn't (ie: we leave the
       * watch structure in the list).
       */

      g_critical ("DBus AddMatch for dconf path '%s'", watch->name);
      watch_unref (watch);

      return;
    }

  else
    g_variant_unref (reply); /* it is just an empty tuple */

  /* There may be multiple messages to receive replies for.  Make sure
   * we have them all before proceeding.
   */
  if (--watch->n_messages != 0)
    return;

  /* In the normal case we're done.
   *
   * There is a fleeting chance, however, that the database has changed
   * in the meantime.  In that case we can only assume that the subject
   * of this watch changed in that time period and emit a signal to that
   * effect.
   */
  if (dconf_engine_get_state (watch->dcdbc->engine) != watch->initial_state)
    watch->notify (watch->dcdbc, watch->name, watch->user_data);

  watch_unref (watch);
}

void
dconf_dbus_client_subscribe (DConfDBusClient *dcdbc,
                             const gchar     *name,
                             DConfDBusNotify  notify,
                             gpointer         user_data)
{
  DConfEngineMessage dcem;
  Watch *watch;

  watch = watch_new (dcdbc, name, notify, user_data);
  dconf_engine_watch (dcdbc->engine, name, &dcem);
  watch->n_messages = dcem.n_messages;
  dconf_dbus_client_send (dcdbc, &dcem, add_match_done, watch);
  dconf_engine_message_destroy (&dcem);
}

void
dconf_dbus_client_unsubscribe (DConfDBusClient *dcdbc,
                               DConfDBusNotify  notify,
                               gpointer         user_data)
{
  DConfEngineMessage dcem;
  GSList **ptr;

  for (ptr = &dcdbc->watches; *ptr; ptr = &(*ptr)->next)
    {
      Watch *watch = (*ptr)->data;

      if (watch->notify == notify && watch->user_data == user_data)
        {
          *ptr = g_slist_remove_link (*ptr, *ptr);

          dconf_engine_unwatch (dcdbc->engine, watch->name, &dcem);
          dconf_dbus_client_send (dcdbc, &dcem, NULL, NULL);
          dconf_engine_message_destroy (&dcem);
          watch_unref (watch);

          return;
        }
    }

  g_warning ("No matching watch found to unsubscribe");
}

gboolean
dconf_dbus_client_has_pending (DConfDBusClient *dcdbc)
{
  return dcdbc->outstanding != NULL;
}

static DBusHandlerResult
dconf_dbus_client_filter (DBusConnection *connection,
                          DBusMessage    *message,
                          void           *user_data)
{
  DConfDBusClient *dcdbc = user_data;

  if (dbus_message_is_signal (message, "ca.desrt.dconf.Writer", "Notify") &&
      dbus_message_has_signature (message, "sass"))
    {
      DBusMessageIter iter, sub;
      const gchar *path, *tag;

      dbus_message_iter_init (message, &iter);
      dbus_message_iter_get_basic (&iter, &path);
      dbus_message_iter_next (&iter);
      dbus_message_iter_recurse (&iter, &sub);
      dbus_message_iter_next (&iter);
      dbus_message_iter_get_basic (&iter, &tag);
      dbus_message_iter_next (&iter);

      /* Only emit the event if it hasn't been anti-exposed */
      if (dcdbc->anti_expose_tag == NULL ||
          strcmp (tag, dcdbc->anti_expose_tag) != 0)
        {
          /* Empty list means that only one key changed */
          if (!dbus_message_iter_get_arg_type (&sub))
            dconf_dbus_emit_change (dcdbc, path);

          while (dbus_message_iter_get_arg_type (&sub) == 's')
            {
              const gchar *item;
              gchar *full;

              dbus_message_iter_get_basic (&sub, &item);
              full = g_strconcat (path, item, NULL);
              dconf_dbus_emit_change (dcdbc, full);
              g_free (full);

              dbus_message_iter_next (&sub);
            }
        }
    }

  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

DConfDBusClient *
dconf_dbus_client_new (const gchar    *profile,
                       DBusConnection *session,
                       DBusConnection *system)
{
  DConfDBusClient *dcdbc;

  if (session == NULL)
    session = dbus_bus_get (DBUS_BUS_SESSION, NULL);

  if (system == NULL)
    system = dbus_bus_get (DBUS_BUS_SYSTEM, NULL);

  dcdbc = g_slice_new (DConfDBusClient);
  dcdbc->engine = dconf_engine_new (profile);
  dcdbc->system_bus = dbus_connection_ref (system);
  dcdbc->session_bus = dbus_connection_ref (session);
  dcdbc->anti_expose_tag = NULL;
  dcdbc->outstanding = NULL;
  dcdbc->watches = NULL;
  dcdbc->ref_count = 1;

  dbus_connection_add_filter (system, dconf_dbus_client_filter, dcdbc, NULL);
  dbus_connection_add_filter (session, dconf_dbus_client_filter, dcdbc, NULL);

  return dcdbc;
}

void
dconf_dbus_client_unref (DConfDBusClient *dcdbc)
{
  if (--dcdbc->ref_count == 0)
    {
      dbus_connection_remove_filter (dcdbc->session_bus,
                                     dconf_dbus_client_filter, dcdbc);
      dbus_connection_remove_filter (dcdbc->system_bus,
                                     dconf_dbus_client_filter, dcdbc);
      dbus_connection_unref (dcdbc->session_bus);
      dbus_connection_unref (dcdbc->system_bus);

      g_slice_free (DConfDBusClient, dcdbc);
    }
}

DConfDBusClient *
dconf_dbus_client_ref (DConfDBusClient *dcdbc)
{
  dcdbc->ref_count++;

  return dcdbc;
}