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-manager.h"
25 #include "common/dbus-manager-glue.h"
26 #include "common/dbus-service-glue.h"
27 #include "common/log.h"
28 #include "dbus-service.h"
29 #include "dbus-server.h"
32 G_DEFINE_TYPE (MsgPortDbusManager, msgport_dbus_manager, G_TYPE_OBJECT)
34 #define MSGPORT_DBUS_MANAGER_GET_PRIV(obj) \
35 G_TYPE_INSTANCE_GET_PRIVATE ((obj), MSGPORT_TYPE_DBUS_MANAGER, MsgPortDbusManagerPrivate)
37 struct _MsgPortDbusManagerPrivate {
38 MsgPortDbusGlueManager *dbus_skeleton;
39 GDBusConnection *connection;
40 MsgPortManager *manager;
41 MsgPortDbusServer *server;
47 _dbus_manager_finalize (GObject *self)
49 //MsgPortDbusManager *manager = MSGPORT_DBUS_MANAGER (self);
51 G_OBJECT_CLASS (msgport_dbus_manager_parent_class)->finalize (self);
55 _dbus_manager_dispose (GObject *self)
57 MsgPortDbusManager *dbus_mgr = MSGPORT_DBUS_MANAGER (self);
59 DBG ("Unexporting dbus manager %p on connection %p", dbus_mgr, dbus_mgr->priv->connection);
60 if (dbus_mgr->priv->dbus_skeleton) {
61 g_dbus_interface_skeleton_unexport (
62 G_DBUS_INTERFACE_SKELETON (dbus_mgr->priv->dbus_skeleton));
63 g_clear_object (&dbus_mgr->priv->dbus_skeleton);
66 g_clear_object (&dbus_mgr->priv->connection);
68 /* unregister all services owned by this connection */
69 msgport_manager_unregister_services (dbus_mgr->priv->manager, dbus_mgr);
71 g_clear_object (&dbus_mgr->priv->manager);
73 G_OBJECT_CLASS (msgport_dbus_manager_parent_class)->dispose (self);
78 _dbus_manager_handle_register_service (
79 MsgPortDbusManager *dbus_mgr,
80 GDBusMethodInvocation *invocation,
81 const gchar *port_name,
86 MsgPortDbusService *dbus_service = NULL;
87 g_return_val_if_fail (dbus_mgr && MSGPORT_IS_DBUS_MANAGER (dbus_mgr), FALSE);
89 DBG ("register service request from %p for port %s", dbus_mgr, port_name);
91 dbus_service = msgport_manager_register_service (
92 dbus_mgr->priv->manager, dbus_mgr,
93 port_name, is_trusted, &error);
96 g_dbus_method_invocation_take_error (invocation, error);
99 msgport_dbus_glue_manager_complete_register_service (
100 dbus_mgr->priv->dbus_skeleton, invocation,
101 msgport_dbus_service_get_object_path(dbus_service));
108 _dbus_manager_handle_check_for_remote_service (
109 MsgPortDbusManager *dbus_mgr,
110 GDBusMethodInvocation *invocation,
111 const gchar *remote_app_id,
112 const gchar *remote_port_name,
116 GError *error = NULL;
117 MsgPortDbusService *dbus_service = NULL;
118 MsgPortDbusManager *remote_dbus_manager = NULL;
120 g_return_val_if_fail (dbus_mgr && MSGPORT_IS_DBUS_MANAGER (dbus_mgr), FALSE);
122 DBG ("check remote service request from %p for %s %s", dbus_mgr, remote_app_id, remote_port_name);
124 remote_dbus_manager = msgport_dbus_server_get_dbus_manager_by_app_id (
125 dbus_mgr->priv->server, remote_app_id);
127 dbus_service = msgport_manager_get_service (dbus_mgr->priv->manager,
128 remote_dbus_manager, remote_port_name, is_trusted);
131 g_dbus_method_invocation_take_error (invocation, error);
134 DBG ("Found service id : %d", msgport_dbus_service_get_id (dbus_service));
135 msgport_dbus_glue_manager_complete_check_for_remote_service (
136 dbus_mgr->priv->dbus_skeleton, invocation,
137 msgport_dbus_service_get_id (dbus_service));
144 _dbus_manager_handle_send_message (
145 MsgPortDbusManager *dbus_mgr,
146 GDBusMethodInvocation *invocation,
151 GError *error = NULL;
152 MsgPortDbusService *dbus_service = 0;
154 g_return_val_if_fail (dbus_mgr && MSGPORT_IS_DBUS_MANAGER (dbus_mgr), FALSE);
156 DBG ("send_message from %p : %d ", dbus_mgr, service_id);
158 dbus_service = msgport_manager_get_service_by_id (
159 dbus_mgr->priv->manager, service_id);
162 msgport_dbus_service_send_message (dbus_service, data, "", "", FALSE);
163 msgport_dbus_glue_manager_complete_send_message (
164 dbus_mgr->priv->dbus_skeleton, invocation);
167 /* FIXME : fill error */
168 g_dbus_method_invocation_take_error (invocation, error);
175 msgport_dbus_manager_init (MsgPortDbusManager *self)
177 MsgPortDbusManagerPrivate *priv = MSGPORT_DBUS_MANAGER_GET_PRIV (self);
179 priv->dbus_skeleton = msgport_dbus_glue_manager_skeleton_new ();
180 priv->manager = msgport_manager_new ();
182 g_signal_connect_swapped (priv->dbus_skeleton, "handle-register-service",
183 G_CALLBACK (_dbus_manager_handle_register_service), (gpointer)self);
184 g_signal_connect_swapped (priv->dbus_skeleton, "handle-check-for-remote-service",
185 G_CALLBACK (_dbus_manager_handle_check_for_remote_service), (gpointer)self);
186 g_signal_connect_swapped (priv->dbus_skeleton, "handle-send-message",
187 G_CALLBACK (_dbus_manager_handle_send_message), (gpointer)self);
193 msgport_dbus_manager_class_init (MsgPortDbusManagerClass *klass)
195 GObjectClass *gklass = G_OBJECT_CLASS(klass);
197 g_type_class_add_private (klass, sizeof(MsgPortDbusManagerPrivate));
199 gklass->finalize = _dbus_manager_finalize;
200 gklass->dispose = _dbus_manager_dispose;
204 _get_app_id_from_connection (GDBusConnection *connection)
206 gchar *str_cred = NULL;
207 GCredentials *cred = g_dbus_connection_get_peer_credentials (connection);
209 g_return_val_if_fail (cred != NULL, NULL);
211 str_cred = g_credentials_to_string (cred);
212 DBG ("Client Credentials : %s", str_cred);
215 return g_strdup_printf ("%d", g_credentials_get_unix_pid(cred, NULL));
219 msgport_dbus_manager_new (
220 GDBusConnection *connection,
221 MsgPortDbusServer *server,
222 MsgPortManager *manager)
224 MsgPortDbusManager *dbus_mgr = NULL;
225 GError *error = NULL;
227 dbus_mgr = MSGPORT_DBUS_MANAGER (g_object_new (MSGPORT_TYPE_DBUS_MANAGER, NULL));
228 if (!dbus_mgr) return NULL;
230 if (!g_dbus_interface_skeleton_export (
231 G_DBUS_INTERFACE_SKELETON (dbus_mgr->priv->dbus_skeleton),
235 g_print ("Failed to export dbus object on connection %p : %s",
236 connection, error->message);
237 g_error_free (error);
238 g_object_unref (dbus_mgr);
241 dbus_mgr->priv->connection = g_object_ref (connection);
242 dbus_mgr->priv->manager = g_object_ref (manager);
243 dbus_mgr->priv->server = server;
244 dbus_mgr->priv->app_id = _get_app_id_from_connection (connection);
250 msgport_dbus_manager_get_manager (MsgPortDbusManager *dbus_manager)
252 g_return_val_if_fail (dbus_manager && MSGPORT_IS_DBUS_MANAGER (dbus_manager), NULL);
254 return dbus_manager->priv->manager;
258 msgport_dbus_manager_get_connection (MsgPortDbusManager *dbus_manager)
260 g_return_val_if_fail (dbus_manager && MSGPORT_IS_DBUS_MANAGER (dbus_manager), NULL);
262 return dbus_manager->priv->connection;
266 msgport_dbus_manager_get_app_id (MsgPortDbusManager *dbus_manager)
268 g_return_val_if_fail (dbus_manager && MSGPORT_IS_DBUS_MANAGER (dbus_manager), NULL);
270 return (const gchar *)dbus_manager->priv->app_id;