peer to peer dbus support added
[platform/upstream/libgsignon-glib.git] / libsignon-glib / sso-auth-service.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 libsignon-glib
5  *
6  * Copyright (C) 2012 Canonical Ltd.
7  *
8  * Contact: Alberto Mardegan <alberto.mardegan@canonical.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 License
12  * version 2.1 as published by the Free Software Foundation.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23  */
24
25 #include <config.h>
26 #include "signon-errors.h"
27 #include "signon-internals.h"
28 #include "sso-auth-service.h"
29
30 static GHashTable *thread_objects = NULL;
31 static GMutex map_mutex;
32
33 static SsoAuthService *
34 get_singleton ()
35 {
36     SsoAuthService *object = NULL;
37
38     g_mutex_lock (&map_mutex);
39
40     if (thread_objects != NULL)
41     {
42         GWeakRef *ref;
43         ref = g_hash_table_lookup (thread_objects, g_thread_self ());
44         if (ref != NULL)
45         {
46             object = g_weak_ref_get (ref);
47         }
48     }
49
50     g_mutex_unlock (&map_mutex);
51     return object;
52 }
53
54 static void
55 set_singleton (SsoAuthService *object)
56 {
57     g_return_if_fail (IS_SSO_AUTH_SERVICE (object));
58
59     g_mutex_lock (&map_mutex);
60
61     if (thread_objects == NULL)
62     {
63         thread_objects = g_hash_table_new (g_direct_hash, g_direct_equal);
64     }
65
66     if (object != NULL)
67     {
68         GWeakRef *ref = g_slice_new (GWeakRef);
69         g_weak_ref_init (ref, object);
70         g_hash_table_insert (thread_objects, g_thread_self (), ref);
71     }
72
73     g_mutex_unlock (&map_mutex);
74 }
75
76 SsoAuthService *
77 sso_auth_service_get_instance ()
78 {
79     SsoAuthService *sso_auth_service = NULL;
80     GDBusConnection *connection = NULL;
81     GError *error = NULL;
82
83     sso_auth_service = get_singleton ();
84     if (sso_auth_service != NULL) return sso_auth_service;
85
86 #ifdef USE_P2P
87     connection = g_dbus_connection_new_for_address_sync (SIGNOND_BUS_ADDRESS,
88                                                          G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
89                                                          NULL,
90                                                          NULL,
91                                                          &error);
92 #else
93     connection = g_bus_get_sync (SIGNOND_BUS_TYPE, NULL, &error);
94 #endif
95     /* Create the object */
96     sso_auth_service =
97         sso_auth_service_proxy_new_sync (connection,
98                                          G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
99 #ifdef USE_P2P
100                                          NULL,
101 #else
102                                          SIGNOND_SERVICE,
103 #endif
104                                          SIGNOND_DAEMON_OBJECTPATH,
105                                          NULL,
106                                          &error);
107     if (G_LIKELY (error == NULL)) {
108         set_singleton (sso_auth_service);
109     }
110     else
111     {
112         g_warning ("Couldn't activate signond: %s", error->message);
113         g_clear_error (&error);
114     }
115
116     /* While at it, register the error mapping with GDBus */
117     signon_error_quark ();
118
119     return sso_auth_service;
120 }