summaryrefslogtreecommitdiff
path: root/dbus-1/dconf-libdbus-1.c
blob: aedc2cfea983901ed36cd030e196fc109f3622fb (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
/**
 * 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-libdbus-1.h"

#include "../engine/dconf-engine.h"

#include <string.h>

static DBusConnection *dconf_libdbus_1_buses[5];

struct _DConfDBusClient
{
  DConfEngine *engine;
  GSList *watches;
  gint ref_count;
};

#define DCONF_LIBDBUS_1_ERROR (g_quark_from_static_string("DCONF_LIBDBUS_1_ERROR"))
#define DCONF_LIBDBUS_1_ERROR_FAILED 0

static DBusMessage *
dconf_libdbus_1_new_method_call (const gchar *bus_name,
                                 const gchar *object_path,
                                 const gchar *interface_name,
                                 const gchar *method_name,
                                 GVariant    *parameters)
{
  DBusMessageIter dbus_iter;
  DBusMessage *message;
  GVariantIter iter;
  GVariant *child;

  g_variant_ref_sink (parameters);

  message = dbus_message_new_method_call (bus_name, object_path, interface_name, method_name);
  dbus_message_iter_init_append (message, &dbus_iter);
  g_variant_iter_init (&iter, parameters);

  while ((child = g_variant_iter_next_value (&iter)))
    {
      if (g_variant_is_of_type (child, G_VARIANT_TYPE_STRING))
        {
          const gchar *str;

          str = g_variant_get_string (child, NULL);
          dbus_message_iter_append_basic (&dbus_iter, DBUS_TYPE_STRING, &str);
        }

      else
        {
          const guint8 *bytes;
          gsize n_elements;

          g_assert (g_variant_is_of_type (child, G_VARIANT_TYPE_BYTESTRING));

          bytes = g_variant_get_fixed_array (child, &n_elements, sizeof (guint8));
          dbus_message_iter_append_fixed_array (&dbus_iter, DBUS_TYPE_BYTE, &bytes, n_elements);
        }

      g_variant_unref (child);
    }

  g_variant_unref (parameters);

  return message;
}

static GVariant *
dconf_libdbus_1_get_message_body (DBusMessage  *message,
                                  GError      **error)
{
  GVariantBuilder builder;
  const gchar *signature;
  DBusMessageIter iter;

  /* We support two types: strings and arrays of strings.
   *
   * It's very simple to detect if the message contains only these
   * types: check that the signature contains only the letters "a" and
   * "s" and that it does not contain "aa".
   */
  signature = dbus_message_get_signature (message);
  if (signature[strspn(signature, "as")] != '\0' || strstr (signature, "aa"))
    {
      g_set_error (error, DCONF_LIBDBUS_1_ERROR, DCONF_LIBDBUS_1_ERROR_FAILED,
                   "unable to handle message type '(%s)'", signature);
      return NULL;
    }

  g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
  dbus_message_iter_init (message, &iter);
  while (dbus_message_iter_get_arg_type (&iter))
    {
      const gchar *string;

      if (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_STRING)
        {
          dbus_message_iter_get_basic (&iter, &string);
          g_variant_builder_add (&builder, "s", string);
        }
      else
        {
          DBusMessageIter sub;

          g_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY &&
                    dbus_message_iter_get_element_type (&iter) == DBUS_TYPE_STRING);

          g_variant_builder_open (&builder, G_VARIANT_TYPE_STRING_ARRAY);
          dbus_message_iter_recurse (&iter, &sub);

          while (dbus_message_iter_get_arg_type (&sub))
            {
              const gchar *string;
              dbus_message_iter_get_basic (&sub, &string);
              g_variant_builder_add (&builder, "s", string);
              dbus_message_iter_next (&sub);
            }

          g_variant_builder_close (&builder);
        }
      dbus_message_iter_next (&iter);
    }

  return g_variant_ref_sink (g_variant_builder_end (&builder));
}

static GVariant *
dconf_libdbus_1_interpret_result (DBusMessage         *result,
                                  const GVariantType  *expected_type,
                                  GError             **error)
{
  GVariant *reply;

  if (dbus_message_get_type (result) == DBUS_MESSAGE_TYPE_ERROR)
    {
      const gchar *errstr = "(no message)";

      dbus_message_get_args (result, NULL, DBUS_TYPE_STRING, &errstr, DBUS_TYPE_INVALID);
      g_set_error (error, DCONF_LIBDBUS_1_ERROR, DCONF_LIBDBUS_1_ERROR_FAILED,
                   "%s: %s", dbus_message_get_error_name (result), errstr);
      return NULL;
    }

  reply = dconf_libdbus_1_get_message_body (result, error);

  if (reply && expected_type && !g_variant_is_of_type (reply, expected_type))
    {
      gchar *expected_string;

      expected_string = g_variant_type_dup_string (expected_type);
      g_set_error (error, DCONF_LIBDBUS_1_ERROR, DCONF_LIBDBUS_1_ERROR_FAILED,
                   "received reply '%s' is not of the expected type %s",
                   g_variant_get_type_string (reply), expected_string);
      g_free (expected_string);

      g_variant_unref (reply);
      reply = NULL;
    }

  return reply;
}

static void
dconf_libdbus_1_method_call_done (DBusPendingCall *pending,
                                  gpointer         user_data)
{
  DConfEngineCallHandle *handle = user_data;
  const GVariantType *expected_type;
  DBusMessage *message;
  GError *error = NULL;
  GVariant *reply;

  if (pending == NULL)
    return;

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

  expected_type = dconf_engine_call_handle_get_expected_type (handle);
  reply = dconf_libdbus_1_interpret_result (message, expected_type, &error);
  dbus_message_unref (message);

  dconf_engine_call_handle_reply (handle, reply, error);

  if (reply)
    g_variant_unref (reply);
  if (error)
    g_error_free (error);
}

gboolean
dconf_engine_dbus_call_async_func (GBusType                bus_type,
                                   const gchar            *bus_name,
                                   const gchar            *object_path,
                                   const gchar            *interface_name,
                                   const gchar            *method_name,
                                   GVariant               *parameters,
                                   DConfEngineCallHandle  *handle,
                                   GError                **error)
{
  DBusConnection *connection;
  DBusPendingCall *pending;
  DBusMessage *message;

  g_assert_cmpint (bus_type, <, G_N_ELEMENTS (dconf_libdbus_1_buses));
  connection = dconf_libdbus_1_buses[bus_type];
  g_assert (connection != NULL);

  message = dconf_libdbus_1_new_method_call (bus_name, object_path, interface_name, method_name, parameters);
  dbus_connection_send_with_reply (connection, message, &pending, -1);
  dbus_pending_call_set_notify (pending, dconf_libdbus_1_method_call_done, handle, NULL);
  dbus_message_unref (message);

  return TRUE;
}

static void
dconf_libdbus_1_convert_error (DBusError  *dbus_error,
                               GError    **error)
{
  g_set_error (error, DCONF_LIBDBUS_1_ERROR, DCONF_LIBDBUS_1_ERROR_FAILED,
               "%s: %s", dbus_error->name, dbus_error->message);
}

GVariant *
dconf_engine_dbus_call_sync_func (GBusType             bus_type,
                                  const gchar         *bus_name,
                                  const gchar         *object_path,
                                  const gchar         *interface_name,
                                  const gchar         *method_name,
                                  GVariant            *parameters,
                                  const GVariantType  *expected_type,
                                  GError             **error)
{
  DBusConnection *connection;
  DBusMessage *message;
  DBusError dbus_error;
  DBusMessage *result;
  GVariant *reply;

  g_assert_cmpint (bus_type, <, G_N_ELEMENTS (dconf_libdbus_1_buses));
  connection = dconf_libdbus_1_buses[bus_type];
  g_assert (connection != NULL);

  dbus_error_init (&dbus_error);
  message = dconf_libdbus_1_new_method_call (bus_name, object_path, interface_name, method_name, parameters);
  result = dbus_connection_send_with_reply_and_block (connection, message, -1, &dbus_error);
  dbus_message_unref (message);

  if (result == NULL)
    {
      dconf_libdbus_1_convert_error (&dbus_error, error);
      dbus_error_free (&dbus_error);
      return NULL;
    }

  reply = dconf_libdbus_1_interpret_result (result, expected_type, error);
  dbus_message_unref (result);

  return reply;
}

static DBusHandlerResult
dconf_libdbus_1_filter (DBusConnection *connection,
                        DBusMessage    *message,
                        gpointer        user_data)
{
  GBusType bus_type = GPOINTER_TO_INT (user_data);

  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_SIGNAL)
    {
      const gchar *interface;

      interface = dbus_message_get_interface (message);

      if (interface && g_str_equal (interface, "ca.desrt.dconf.Writer"))
        {
          GVariant *parameters;

          parameters = dconf_libdbus_1_get_message_body (message, NULL);

          if (parameters != NULL)
            {
              dconf_engine_handle_dbus_signal (bus_type,
                                               dbus_message_get_sender (message),
                                               dbus_message_get_path (message),
                                               dbus_message_get_member (message),
                                               parameters);
              g_variant_unref (parameters);
            }
        }
    }

  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

void
dconf_libdbus_1_provide_bus (GBusType        bus_type,
                             DBusConnection *connection)
{
  g_assert_cmpint (bus_type, <, G_N_ELEMENTS (dconf_libdbus_1_buses));

  if (!dconf_libdbus_1_buses[bus_type])
    {
      dconf_libdbus_1_buses[bus_type] = dbus_connection_ref (connection);
      dbus_connection_add_filter (connection, dconf_libdbus_1_filter, GINT_TO_POINTER (bus_type), NULL);
    }
}

#ifndef PIC
static gboolean
dconf_libdbus_1_check_connection (gpointer user_data)
{
  DBusConnection *connection = user_data;

  dbus_connection_read_write (connection, 0);
  dbus_connection_dispatch (connection);

  return G_SOURCE_CONTINUE;
}

void
dconf_engine_dbus_init_for_testing (void)
{
  DBusConnection *session;
  DBusConnection *system;

  dconf_libdbus_1_provide_bus (G_BUS_TYPE_SESSION, session = dbus_bus_get (DBUS_BUS_SESSION, NULL));
  dconf_libdbus_1_provide_bus (G_BUS_TYPE_SYSTEM, system = dbus_bus_get (DBUS_BUS_SYSTEM, NULL));

  /* "mainloop integration" */
  g_timeout_add (1, dconf_libdbus_1_check_connection, session);
  g_timeout_add (1, dconf_libdbus_1_check_connection, system);
}
#endif