641eecae2c253fa8fffa2a04ec3508f18bc05acf
[profile/ivi/gsignond.git] / src / daemon / plugins / plugind / gsignond-plugin-daemon.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of gsignond
5  *
6  * Copyright (C) 2013 Intel Corporation.
7  *
8  * Contact: Imran Zaman <imran.zaman@intel.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25
26 #include "gsignond/gsignond-plugin-interface.h"
27 #include "gsignond/gsignond-log.h"
28 #include "gsignond/gsignond-error.h"
29 #include "common/gsignond-plugin-loader.h"
30 #include "common/gsignond-pipe-stream.h"
31 #include "daemon/dbus/gsignond-dbus-remote-plugin-gen.h"
32 #include "daemon/dbus/gsignond-dbus.h"
33 #include "gsignond-plugin-daemon.h"
34
35 struct _GSignondPluginDaemonPrivate
36 {
37     GDBusConnection   *connection;
38     GSignondDbusRemotePlugin *dbus_remote_plugin;
39     GSignondPlugin *plugin;
40     gchar *plugin_type;
41 };
42
43 G_DEFINE_TYPE (GSignondPluginDaemon, gsignond_plugin_daemon, G_TYPE_OBJECT)
44
45
46 #define GSIGNOND_PLUGIN_DAEMON_GET_PRIV(obj) \
47     G_TYPE_INSTANCE_GET_PRIVATE ((obj), GSIGNOND_TYPE_PLUGIN_DAEMON,\
48             GSignondPluginDaemonPrivate)
49
50 static void
51 _dispose (GObject *object)
52 {
53     GSignondPluginDaemon *self = GSIGNOND_PLUGIN_DAEMON (object);
54
55     if (self->priv->dbus_remote_plugin) {
56         g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (
57                 self->priv->dbus_remote_plugin));
58         g_object_unref (self->priv->dbus_remote_plugin);
59         self->priv->dbus_remote_plugin = NULL;
60     }
61
62     if (self->priv->connection) {
63         g_object_unref (self->priv->connection);
64         self->priv->connection = NULL;
65     }
66
67     if (self->priv->plugin) {
68         g_object_unref (self->priv->plugin);
69         self->priv->plugin = NULL;
70     }
71
72     G_OBJECT_CLASS (gsignond_plugin_daemon_parent_class)->dispose (object);
73 }
74
75 static void
76 _finalize (GObject *object)
77 {
78     GSignondPluginDaemon *self = GSIGNOND_PLUGIN_DAEMON (object);
79
80     if (self->priv->plugin_type) {
81         g_free (self->priv->plugin_type);
82         self->priv->plugin_type = NULL;
83     }
84
85     G_OBJECT_CLASS (gsignond_plugin_daemon_parent_class)->finalize (object);
86 }
87
88 static void
89 gsignond_plugin_daemon_class_init (
90         GSignondPluginDaemonClass *klass)
91 {
92     GObjectClass* object_class = G_OBJECT_CLASS (klass);
93
94     g_type_class_add_private (object_class, sizeof (
95             GSignondPluginDaemonPrivate));
96
97     object_class->dispose = _dispose;
98     object_class->finalize = _finalize;
99
100 }
101
102 static void
103 gsignond_plugin_daemon_init (
104         GSignondPluginDaemon *self)
105 {
106     self->priv = GSIGNOND_PLUGIN_DAEMON_GET_PRIV(self);
107     self->priv->connection = NULL;
108     self->priv->dbus_remote_plugin = NULL;
109     self->priv->plugin_type = NULL;
110     self->priv->plugin = NULL;
111 }
112
113 static void
114 _on_connection_closed (
115         GDBusConnection *connection,
116         gboolean         remote_peer_vanished,
117         GError          *error,
118         gpointer         user_data)
119 {
120     GSignondPluginDaemon *daemon = GSIGNOND_PLUGIN_DAEMON (user_data);
121
122     g_signal_handlers_disconnect_by_func (connection, _on_connection_closed,
123             user_data);
124     DBG("dbus connection(%p) closed (peer vanished : %d)", connection,
125             remote_peer_vanished);
126     if (error) {
127        DBG("...reason : %s", error->message);
128     }
129     g_object_unref (daemon);
130 }
131
132 static gboolean
133 _handle_cancel_from_dbus (
134         GSignondPluginDaemon *self,
135         GDBusMethodInvocation *invocation,
136         gpointer user_data)
137 {
138     DBG ("");
139     g_return_val_if_fail (self && GSIGNOND_IS_PLUGIN_DAEMON (self), FALSE);
140     gsignond_dbus_remote_plugin_complete_cancel (self->priv->dbus_remote_plugin,
141             invocation);
142
143     gsignond_plugin_cancel (self->priv->plugin);
144     return TRUE;
145 }
146
147 static gboolean
148 _handle_request_from_dbus (
149         GSignondPluginDaemon *self,
150         GDBusMethodInvocation *invocation,
151         const GVariant *session_data,
152         gpointer user_data)
153 {
154     DBG ("");
155     g_return_val_if_fail (self && GSIGNOND_IS_PLUGIN_DAEMON (self), FALSE);
156
157     gsignond_dbus_remote_plugin_complete_request (
158             self->priv->dbus_remote_plugin, invocation);
159
160     GSignondSessionData *data = (GSignondSessionData *)
161             gsignond_dictionary_new_from_variant ((GVariant *)session_data);
162     gsignond_plugin_request (self->priv->plugin, data);
163     gsignond_dictionary_unref (data);
164     return TRUE;
165 }
166
167 static void
168 _handle_response_final_from_plugin (
169         GSignondPluginDaemon *self,
170         GSignondSessionData *session_data,
171         gpointer user_data);
172
173 static gboolean
174 _handle_request_initial_from_dbus (
175         GSignondPluginDaemon *self,
176         GDBusMethodInvocation *invocation,
177         const GVariant *session_data,
178         const gchar *mechanism,
179         gpointer user_data)
180 {
181     DBG ("");
182     g_return_val_if_fail (self && GSIGNOND_IS_PLUGIN_DAEMON (self), FALSE);
183
184     gsignond_dbus_remote_plugin_complete_request_initial (
185             self->priv->dbus_remote_plugin, invocation);
186
187     GSignondSessionData *data = (GSignondSessionData *)
188             gsignond_dictionary_new_from_variant ((GVariant *)session_data);
189     gsignond_plugin_request_initial (self->priv->plugin, data, mechanism);
190     gsignond_dictionary_unref (data);
191
192     return TRUE;
193 }
194
195 static gboolean
196 _handle_user_action_finished_from_dbus (
197         GSignondPluginDaemon *self,
198         GDBusMethodInvocation *invocation,
199         const GVariant *ui_data,
200         gpointer user_data)
201 {
202     DBG ("");
203     g_return_val_if_fail (self && GSIGNOND_IS_PLUGIN_DAEMON (self), FALSE);
204
205     gsignond_dbus_remote_plugin_complete_user_action_finished (
206             self->priv->dbus_remote_plugin, invocation);
207
208     GSignondSignonuiData *data = (GSignondSignonuiData *)
209             gsignond_signonui_data_new_from_variant ((GVariant *)ui_data);
210     gsignond_plugin_user_action_finished (self->priv->plugin, data);
211     gsignond_signonui_data_unref (data);
212     return TRUE;
213 }
214
215 static gboolean
216 _handle_refresh_from_dbus (
217         GSignondPluginDaemon *self,
218         GDBusMethodInvocation *invocation,
219         const GVariant *ui_data,
220         gpointer user_data)
221 {
222     DBG ("");
223     g_return_val_if_fail (self && GSIGNOND_IS_PLUGIN_DAEMON (self), FALSE);
224
225     gsignond_dbus_remote_plugin_complete_refresh (
226             self->priv->dbus_remote_plugin, invocation);
227
228     GSignondSignonuiData *data = (GSignondSignonuiData *)
229             gsignond_signonui_data_new_from_variant ((GVariant *)ui_data);
230     gsignond_plugin_refresh (self->priv->plugin, data);
231     gsignond_signonui_data_unref (data);
232     return TRUE;
233 }
234
235 static gboolean
236 _handle_get_info_from_dbus (
237         GSignondPluginDaemon *self,
238         GDBusMethodInvocation *invocation,
239         gpointer user_data)
240 {
241     DBG ("");
242     g_return_val_if_fail (self && GSIGNOND_IS_PLUGIN_DAEMON (self), FALSE);
243     gchar *type = NULL;
244     gchar **mechanisms = NULL;
245
246     g_object_get (self->priv->plugin, "type", &type, "mechanisms", &mechanisms,
247             NULL);
248     gsignond_dbus_remote_plugin_complete_get_info (
249             self->priv->dbus_remote_plugin, invocation, (const gchar*)type,
250             (const gchar *const *)mechanisms);
251     g_free (type);
252     g_strfreev (mechanisms);
253     return TRUE;
254 }
255
256 static void
257 _handle_response_from_plugin (
258         GSignondPluginDaemon *self,
259         GSignondSessionData *session_data,
260         gpointer user_data)
261 {
262     DBG ("");
263     g_return_if_fail (self && GSIGNOND_IS_PLUGIN_DAEMON (self));
264
265     GVariant *data = gsignond_dictionary_to_variant (
266             (GSignondDictionary *)session_data);
267     gsignond_dbus_remote_plugin_emit_response (self->priv->dbus_remote_plugin,
268             data);
269 }
270
271 static void
272 _handle_response_final_from_plugin (
273         GSignondPluginDaemon *self,
274         GSignondSessionData *session_data,
275         gpointer user_data)
276 {
277     DBG ("");
278     g_return_if_fail (self && GSIGNOND_IS_PLUGIN_DAEMON (self));
279
280     GVariant *data = gsignond_dictionary_to_variant (
281             (GSignondDictionary *)session_data);
282     gsignond_dbus_remote_plugin_emit_response_final (
283             self->priv->dbus_remote_plugin, data);
284 }
285
286 static void
287 _handle_store_from_plugin (
288         GSignondPluginDaemon *self,
289         GSignondSessionData *session_data,
290         gpointer user_data)
291 {
292     DBG ("");
293     g_return_if_fail (self && GSIGNOND_IS_PLUGIN_DAEMON (self));
294
295     GVariant *data = gsignond_dictionary_to_variant (
296             (GSignondDictionary *)session_data);
297     gsignond_dbus_remote_plugin_emit_store (self->priv->dbus_remote_plugin,
298             data);
299 }
300
301 static void
302 _handle_error_from_plugin (
303         GSignondPluginDaemon *self,
304         GError *gerror,
305         gpointer user_data)
306 {
307     DBG ("");
308     g_return_if_fail (self && GSIGNOND_IS_PLUGIN_DAEMON (self));
309
310     GVariant *error = gsignond_error_to_variant (gerror);
311     gsignond_dbus_remote_plugin_emit_error (self->priv->dbus_remote_plugin,
312             error);
313 }
314
315 static void
316 _handle_user_action_required_from_plugin (
317         GSignondPluginDaemon *self,
318         GSignondSignonuiData *ui_data,
319         gpointer user_data)
320 {
321     DBG ("");
322     g_return_if_fail (self && GSIGNOND_IS_PLUGIN_DAEMON (self));
323
324     GVariant *data = gsignond_signonui_data_to_variant (ui_data);
325     gsignond_dbus_remote_plugin_emit_user_action_required (
326             self->priv->dbus_remote_plugin, data);
327 }
328
329 static void
330 _handle_refreshed_from_plugin(
331         GSignondPluginDaemon *self,
332         GSignondSignonuiData *ui_data,
333         gpointer user_data)
334 {
335     DBG ("");
336     g_return_if_fail (self && GSIGNOND_IS_PLUGIN_DAEMON (self));
337
338     GVariant *data = gsignond_signonui_data_to_variant (ui_data);
339     gsignond_dbus_remote_plugin_emit_refreshed (self->priv->dbus_remote_plugin,
340             data);
341 }
342
343 static void
344 _handle_status_changed_from_plugin (
345         GSignondPluginDaemon *self,
346         GSignondPluginState status,
347         gchar *message,
348         gpointer user_data)
349 {
350     DBG ("");
351     g_return_if_fail (self && GSIGNOND_IS_PLUGIN_DAEMON (self));
352
353     gsignond_dbus_remote_plugin_emit_status_changed (
354             self->priv->dbus_remote_plugin, (gint)status,
355             (const gchar *)message);
356 }
357
358 GSignondPluginDaemon *
359 gsignond_plugin_daemon_new (
360         const gchar* filename,
361         const gchar* plugin_type,
362         gint in_fd,
363         gint out_fd)
364 {
365     GError *error = NULL;
366     GSignondPipeStream *stream = NULL;
367
368     g_return_val_if_fail (filename != NULL && plugin_type != NULL, NULL);
369
370     GSignondPluginDaemon *daemon = GSIGNOND_PLUGIN_DAEMON (g_object_new (
371             GSIGNOND_TYPE_PLUGIN_DAEMON, NULL));
372
373     /* Load plugin */
374     daemon->priv->plugin = gsignond_load_plugin_with_filename (
375             (gchar *)plugin_type, (gchar *)filename);
376     if (!daemon->priv->plugin) {
377         DBG ("failed to load plugin");
378         g_object_unref (daemon);
379         return NULL;
380     }
381
382     daemon->priv->plugin_type = g_strdup (plugin_type);
383
384     /* Create dbus connection */
385     stream = gsignond_pipe_stream_new (in_fd, out_fd, TRUE);
386     daemon->priv->connection = g_dbus_connection_new_sync (G_IO_STREAM (stream),
387             NULL, G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING, NULL, NULL,
388             NULL);
389     g_object_unref (stream);
390
391     /* Create dbus object */
392     daemon->priv->dbus_remote_plugin =
393             gsignond_dbus_remote_plugin_skeleton_new ();
394
395     g_dbus_interface_skeleton_export (
396                 G_DBUS_INTERFACE_SKELETON(daemon->priv->dbus_remote_plugin),
397                 daemon->priv->connection, GSIGNOND_PLUGIN_OBJECTPATH, &error);
398     if (error) {
399         DBG ("failed to register object: %s", error->message);
400         g_error_free (error);
401         g_object_unref (daemon);
402         return NULL;
403     }
404     DBG("Started plugin daemon '%p' at path '%s' on conneciton '%p'",
405             daemon, GSIGNOND_PLUGIN_OBJECTPATH, daemon->priv->connection);
406
407     /* Connect dbus remote plugin signals to handlers */
408     g_signal_connect_swapped (daemon->priv->dbus_remote_plugin,
409             "handle-cancel", G_CALLBACK (_handle_cancel_from_dbus), daemon);
410     g_signal_connect_swapped (daemon->priv->dbus_remote_plugin,
411             "handle-request", G_CALLBACK(_handle_request_from_dbus), daemon);
412     g_signal_connect_swapped (daemon->priv->dbus_remote_plugin,
413             "handle-request-initial",
414             G_CALLBACK(_handle_request_initial_from_dbus), daemon);
415     g_signal_connect_swapped (daemon->priv->dbus_remote_plugin,
416             "handle-user-action-finished",
417             G_CALLBACK(_handle_user_action_finished_from_dbus), daemon);
418     g_signal_connect_swapped (daemon->priv->dbus_remote_plugin,
419             "handle-refresh", G_CALLBACK(_handle_refresh_from_dbus), daemon);
420     g_signal_connect_swapped (daemon->priv->dbus_remote_plugin,
421             "handle-get-info", G_CALLBACK(_handle_get_info_from_dbus), daemon);
422
423     /* Connect plugin signals to handlers */
424     g_signal_connect_swapped (daemon->priv->plugin, "response",
425             G_CALLBACK (_handle_response_from_plugin), daemon);
426     g_signal_connect_swapped (daemon->priv->plugin, "response-final",
427             G_CALLBACK(_handle_response_final_from_plugin), daemon);
428     g_signal_connect_swapped (daemon->priv->plugin, "store",
429             G_CALLBACK(_handle_store_from_plugin), daemon);
430     g_signal_connect_swapped (daemon->priv->plugin, "error",
431             G_CALLBACK(_handle_error_from_plugin), daemon);
432     g_signal_connect_swapped (daemon->priv->plugin, "user-action-required",
433             G_CALLBACK(_handle_user_action_required_from_plugin), daemon);
434     g_signal_connect_swapped (daemon->priv->plugin, "refreshed",
435             G_CALLBACK(_handle_refreshed_from_plugin), daemon);
436     g_signal_connect_swapped (daemon->priv->plugin, "status-changed",
437             G_CALLBACK(_handle_status_changed_from_plugin), daemon);
438
439     g_signal_connect (daemon->priv->connection, "closed",
440             G_CALLBACK(_on_connection_closed), daemon);
441
442     g_dbus_connection_start_message_processing (daemon->priv->connection);
443
444     return daemon;
445 }
446