1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * This file is part of libsignon-glib
6 * Copyright (C) 2012 Canonical Ltd.
8 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
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.
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.
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
26 #include "signon-errors.h"
27 #include "signon-internals.h"
28 #include "sso-auth-service.h"
30 static GHashTable *thread_objects = NULL;
31 static GMutex map_mutex;
33 static SsoAuthService *
36 SsoAuthService *object = NULL;
38 g_mutex_lock (&map_mutex);
40 if (thread_objects != NULL)
43 ref = g_hash_table_lookup (thread_objects, g_thread_self ());
46 object = g_weak_ref_get (ref);
50 g_mutex_unlock (&map_mutex);
55 set_singleton (SsoAuthService *object)
57 g_return_if_fail (IS_SSO_AUTH_SERVICE (object));
59 g_mutex_lock (&map_mutex);
61 if (thread_objects == NULL)
63 thread_objects = g_hash_table_new (g_direct_hash, g_direct_equal);
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);
73 g_mutex_unlock (&map_mutex);
77 sso_auth_service_get_instance ()
79 SsoAuthService *sso_auth_service = NULL;
80 GDBusConnection *connection = NULL;
83 sso_auth_service = get_singleton ();
84 if (sso_auth_service != NULL) return sso_auth_service;
87 connection = g_dbus_connection_new_for_address_sync (SIGNOND_BUS_ADDRESS,
88 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
93 connection = g_bus_get_sync (SIGNOND_BUS_TYPE, NULL, &error);
95 /* Create the object */
97 sso_auth_service_proxy_new_sync (connection,
98 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
104 SIGNOND_DAEMON_OBJECTPATH,
107 if (G_LIKELY (error == NULL)) {
108 set_singleton (sso_auth_service);
112 g_warning ("Couldn't activate signond: %s", error->message);
113 g_clear_error (&error);
116 /* While at it, register the error mapping with GDBus */
117 signon_error_quark ();
119 return sso_auth_service;