1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * Copyright (C) 2013 Intel Corporation.
6 * Contact: Amarnath Valluri <amarnath.valluri@linux.intel.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include "dbus-service.h"
25 #include "common/dbus-service-glue.h"
26 #include "common/dbus-error.h"
27 #include "common/log.h"
31 G_DEFINE_TYPE (MsgPortDbusService, msgport_dbus_service, G_TYPE_OBJECT)
33 #define MSGPORT_DBUS_SERVICE_GET_PRIV(obj) \
34 G_TYPE_INSTANCE_GET_PRIVATE ((obj), MSGPORT_TYPE_DBUS_SERVICE, MsgPortDbusServicePrivate)
36 struct _MsgPortDbusServicePrivate {
38 MsgPortDbusGlueService *dbus_skeleton;
39 MsgPortDbusManager *owner;
46 _dbus_service_finalize (GObject *self)
48 MsgPortDbusService *dbus_service = MSGPORT_DBUS_SERVICE (self);
50 G_OBJECT_CLASS (msgport_dbus_service_parent_class)->finalize (self);
54 _dbus_service_dispose (GObject *self)
56 MsgPortDbusService *dbus_service = MSGPORT_DBUS_SERVICE (self);
57 DBG ("Unregistering service '%s'", dbus_service->priv->port_name);
58 if (dbus_service->priv->dbus_skeleton) {
59 g_dbus_interface_skeleton_unexport (
60 G_DBUS_INTERFACE_SKELETON (dbus_service->priv->dbus_skeleton));
61 g_clear_object (&dbus_service->priv->dbus_skeleton);
64 G_OBJECT_CLASS (msgport_dbus_service_parent_class)->dispose (self);
69 _dbus_service_handle_send_message (
70 MsgPortDbusService *dbus_service,
71 GDBusMethodInvocation *invocation,
72 guint remote_service_id,
76 MsgPortDbusService *peer_dbus_service = NULL;
77 MsgPortManager *manager = NULL;
80 msgport_return_val_if_fail_with_error (dbus_service && MSGPORT_IS_DBUS_SERVICE (dbus_service), FALSE, &error);
82 DBG ("Send Message rquest on service %p to remote service id : %d", dbus_service, remote_service_id);
83 manager = msgport_dbus_manager_get_manager (dbus_service->priv->owner);
84 peer_dbus_service = msgport_manager_get_service_by_id (manager, remote_service_id, &error);
86 if (peer_dbus_service) {
87 if (msgport_dbus_service_send_message (peer_dbus_service, data,
88 msgport_dbus_service_get_app_id (dbus_service),
89 dbus_service->priv->port_name,
90 dbus_service->priv->is_trusted, &error)) {
91 msgport_dbus_glue_service_complete_send_message (
92 dbus_service->priv->dbus_skeleton, invocation);
98 if (!error) error = msgport_error_unknown_new ();
99 g_dbus_method_invocation_take_error (invocation, error);
105 _dbus_service_handle_unregister (
106 MsgPortDbusService *dbus_service,
107 GDBusMethodInvocation *invocation,
110 GError *error = NULL;
111 msgport_return_val_if_fail_with_error (dbus_service && MSGPORT_IS_DBUS_SERVICE (dbus_service), FALSE, &error);
113 /* FIXME unregister */
118 msgport_dbus_service_init (MsgPortDbusService *self)
120 MsgPortDbusServicePrivate *priv = MSGPORT_DBUS_SERVICE_GET_PRIV (self);
122 priv->dbus_skeleton = msgport_dbus_glue_service_skeleton_new ();
125 priv->port_name = NULL;
127 g_signal_connect_swapped (priv->dbus_skeleton, "handle-send-message",
128 G_CALLBACK (_dbus_service_handle_send_message), (gpointer)self);
129 g_signal_connect_swapped (priv->dbus_skeleton, "handle-unregister",
130 G_CALLBACK (_dbus_service_handle_unregister), (gpointer)self);
136 msgport_dbus_service_class_init (MsgPortDbusServiceClass *klass)
138 GObjectClass *gklass = G_OBJECT_CLASS(klass);
140 g_type_class_add_private (klass, sizeof(MsgPortDbusServicePrivate));
142 gklass->finalize = _dbus_service_finalize;
143 gklass->dispose = _dbus_service_dispose;
147 msgport_dbus_service_new (MsgPortDbusManager *owner, const gchar *name, gboolean is_trusted, GError **error)
149 static guint object_conter = 0;
151 MsgPortDbusService *dbus_service = NULL;
152 GDBusConnection *connection = NULL;
153 gchar *object_path = 0;
155 msgport_return_val_if_fail_with_error (owner && MSGPORT_IS_DBUS_MANAGER (owner), NULL, error);
156 msgport_return_val_if_fail_with_error (name && name[0], NULL, error);
158 connection = msgport_dbus_manager_get_connection (owner),
159 dbus_service = MSGPORT_DBUS_SERVICE (g_object_new (MSGPORT_TYPE_DBUS_SERVICE, NULL));
161 if (error) *error = msgport_error_no_memory_new ();
164 dbus_service->priv->owner = owner;
165 dbus_service->priv->id = object_conter + 1;
166 dbus_service->priv->port_name = g_strdup (name);
167 dbus_service->priv->is_trusted = is_trusted;
169 /* FIXME: better way of path creation */
170 object_path = g_strdup_printf ("/%u", dbus_service->priv->id);
171 if (!g_dbus_interface_skeleton_export (
172 G_DBUS_INTERFACE_SKELETON (dbus_service->priv->dbus_skeleton),
176 g_print ("Failed to export dbus object on connection %p : %s",
177 connection, error ? (*error)->message : "");
178 g_object_unref (dbus_service);
179 g_free (object_path);
184 g_free (object_path);
186 /* set dbus-properties */
187 g_object_set (G_OBJECT (dbus_service->priv->dbus_skeleton),
188 "id", dbus_service->priv->id,
189 "port-name", dbus_service->priv->port_name,
190 "is-trusted", dbus_service->priv->is_trusted, NULL);
198 msgport_dbus_service_get_id (MsgPortDbusService *dbus_service)
200 g_return_val_if_fail (dbus_service && MSGPORT_IS_DBUS_SERVICE (dbus_service), 0);
202 return dbus_service->priv->id;
206 msgport_dbus_service_get_object_path (MsgPortDbusService *dbus_service)
208 g_return_val_if_fail (dbus_service && MSGPORT_IS_DBUS_SERVICE (dbus_service), NULL);
210 return g_dbus_interface_skeleton_get_object_path (
211 G_DBUS_INTERFACE_SKELETON (dbus_service->priv->dbus_skeleton)) ;
215 msgport_dbus_service_get_connection (MsgPortDbusService *dbus_service)
217 g_return_val_if_fail (dbus_service && MSGPORT_IS_DBUS_SERVICE (dbus_service), NULL);
219 return msgport_dbus_manager_get_connection (dbus_service->priv->owner);
223 msgport_dbus_service_get_owner (MsgPortDbusService *dbus_service)
225 g_return_val_if_fail (dbus_service && MSGPORT_IS_DBUS_SERVICE (dbus_service), NULL);
227 return dbus_service->priv->owner;
231 msgport_dbus_service_get_port_name (MsgPortDbusService *dbus_service)
233 g_return_val_if_fail (dbus_service && MSGPORT_IS_DBUS_SERVICE (dbus_service), NULL);
235 return (const gchar *)dbus_service->priv->port_name;
239 msgport_dbus_service_get_app_id (MsgPortDbusService *dbus_service)
241 g_return_val_if_fail (dbus_service && MSGPORT_IS_DBUS_SERVICE (dbus_service), NULL);
243 return msgport_dbus_manager_get_app_id (dbus_service->priv->owner);
247 msgport_dbus_service_get_is_trusted (MsgPortDbusService *dbus_service)
249 g_return_val_if_fail (dbus_service && MSGPORT_IS_DBUS_SERVICE (dbus_service), FALSE);
251 return dbus_service->priv->is_trusted;
255 msgport_dbus_service_send_message (
256 MsgPortDbusService *dbus_service,
258 const gchar *r_app_id,
260 gboolean r_is_trusted,
263 msgport_return_val_if_fail_with_error (dbus_service && MSGPORT_IS_DBUS_SERVICE (dbus_service), FALSE, error);
265 if (dbus_service->priv->is_trusted &&
266 !msgport_dbus_manager_validate_peer_certificate (dbus_service->priv->owner, r_app_id)) {
267 if (error) *error = msgport_error_certificate_mismatch_new ();
271 DBG ("Sending message to %p from '%s:%s'", dbus_service, r_app_id, r_port);
272 msgport_dbus_glue_service_emit_on_message (dbus_service->priv->dbus_skeleton, data, r_app_id, r_port, r_is_trusted);