Branch data Line data Source code
1 : : /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2 : : *
3 : : * Copyright © 2018 Endless Mobile, Inc.
4 : : *
5 : : * This library is free software; you can redistribute it and/or
6 : : * modify it under the terms of the GNU Lesser General Public
7 : : * License as published by the Free Software Foundation; either
8 : : * version 2.1 of the License, or (at your option) any later version.
9 : : *
10 : : * This library is distributed in the hope that it will be useful,
11 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : : * Lesser General Public License for more details.
14 : : *
15 : : * You should have received a copy of the GNU Lesser General Public
16 : : * License along with this library; if not, write to the Free Software
17 : : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 : : *
19 : : * Authors:
20 : : * - Philip Withnall <withnall@endlessm.com>
21 : : */
22 : :
23 : : #include "config.h"
24 : :
25 : : #include <glib.h>
26 : : #include <glib-object.h>
27 : : #include <glib/gi18n-lib.h>
28 : : #include <gio/gio.h>
29 : : #include <libmogwai-schedule/peer-manager.h>
30 : :
31 : :
32 : : /**
33 : : * MwsPeerManager:
34 : : *
35 : : * A peer manager is an abstraction over the management of peers on a D-Bus
36 : : * connection, monitoring when they disappear, and allowing querying and caching
37 : : * of their credentials.
38 : : *
39 : : * Currently, the only credential stored is the path to the peer’s executable,
40 : : * which can be used to identify that peer. In future, other credentials may be
41 : : * added (and the API will change accordingly).
42 : : *
43 : : * The default implementation for production use is #MwsPeerManagerDBus,
44 : : * which uses the D-Bus daemon to get credentials. Unit tests will use a
45 : : * different implementation, allowing them to provide fake data to the scheduler
46 : : * easily.
47 : : *
48 : : * Since: 0.1.0
49 : : */
50 : :
51 [ + + + - : 438 : G_DEFINE_INTERFACE (MwsPeerManager, mws_peer_manager, G_TYPE_OBJECT)
+ + ]
52 : :
53 : : static void
54 : 2 : mws_peer_manager_default_init (MwsPeerManagerInterface *iface)
55 : : {
56 : : /**
57 : : * MwsPeerManager::peer-vanished:
58 : : * @self: a #MwsPeerManager
59 : : * @name: unique name of the D-Bus peer which vanished
60 : : *
61 : : * Emitted when a peer disappears off the bus. The peer’s unique name will be
62 : : * given as @name.
63 : : *
64 : : * Since: 0.1.0
65 : : */
66 : 2 : g_signal_new ("peer-vanished", G_TYPE_FROM_INTERFACE (iface),
67 : : G_SIGNAL_RUN_LAST,
68 : : 0, NULL, NULL, NULL,
69 : : G_TYPE_NONE, 1,
70 : : G_TYPE_STRING);
71 : 2 : }
72 : :
73 : : /**
74 : : * mws_peer_manager_ensure_peer_credentials_async:
75 : : * @self: a #MwsPeerManager
76 : : * @sender: D-Bus unique name for the peer
77 : : *
78 : : * Ensure the credentials for a peer are in the peer manager, querying them from
79 : : * the D-Bus daemon if needed. Also start watching the @sender, so that if it
80 : : * disappears from the bus, a #MwsPeerManager::peer-vanished signal will be
81 : : * emitted.
82 : : *
83 : : * Since: 0.1.0
84 : : */
85 : : void
86 : 9 : mws_peer_manager_ensure_peer_credentials_async (MwsPeerManager *self,
87 : : const gchar *sender,
88 : : GCancellable *cancellable,
89 : : GAsyncReadyCallback callback,
90 : : gpointer user_data)
91 : : {
92 [ - + ]: 9 : g_return_if_fail (MWS_IS_PEER_MANAGER (self));
93 [ - + ]: 9 : g_return_if_fail (g_dbus_is_unique_name (sender));
94 [ + - - + : 9 : g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
+ - + - -
+ ]
95 : :
96 : 9 : MwsPeerManagerInterface *iface = MWS_PEER_MANAGER_GET_IFACE (self);
97 [ - + ]: 9 : g_assert (iface->ensure_peer_credentials_async != NULL);
98 : 9 : iface->ensure_peer_credentials_async (self, sender, cancellable, callback, user_data);
99 : : }
100 : :
101 : : /**
102 : : * mws_peer_manager_ensure_peer_credentials_finish:
103 : : * @self: a #MwsPeerManager
104 : : * @result: asynchronous operation result
105 : : * @error: return location for a #GError
106 : : *
107 : : * Finish ensuring the credentials for a peer are in the peer manager. See
108 : : * mws_peer_manager_ensure_peer_credentials_async().
109 : : *
110 : : * Returns: (transfer full): path to the executable for the peer,
111 : : * or %NULL on error
112 : : * Since: 0.1.0
113 : : */
114 : : gchar *
115 : 9 : mws_peer_manager_ensure_peer_credentials_finish (MwsPeerManager *self,
116 : : GAsyncResult *result,
117 : : GError **error)
118 : : {
119 [ - + ]: 9 : g_return_val_if_fail (MWS_IS_PEER_MANAGER (self), NULL);
120 [ - + + - : 9 : g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
- + - + ]
121 [ + - - + ]: 9 : g_return_val_if_fail (error == NULL || *error == NULL, NULL);
122 : :
123 : 9 : MwsPeerManagerInterface *iface = MWS_PEER_MANAGER_GET_IFACE (self);
124 [ - + ]: 9 : g_assert (iface->ensure_peer_credentials_finish != NULL);
125 : :
126 : 9 : g_autoptr(GError) local_error = NULL;
127 : 18 : g_autofree gchar *sender_path = iface->ensure_peer_credentials_finish (self, result,
128 : : &local_error);
129 [ - + ]: 9 : g_return_val_if_fail ((sender_path == NULL) == (local_error != NULL), NULL);
130 : :
131 [ + + ]: 9 : if (local_error != NULL)
132 : 2 : g_propagate_error (error, g_steal_pointer (&local_error));
133 : 9 : return g_steal_pointer (&sender_path);
134 : : }
135 : :
136 : : /**
137 : : * mws_peer_manager_get_peer_credentials:
138 : : * @self: a #MwsPeerManager
139 : : * @sender: D-Bus unique name for the peer
140 : : *
141 : : * Get the credentials for the given peer. If no credentials are in the cache
142 : : * for @sender, %NULL will be returned.
143 : : *
144 : : * Returns: (nullable): path to the executable for the peer, or %NULL if it’s
145 : : * unknown
146 : : * Since: 0.1.0
147 : : */
148 : : const gchar *
149 : 146 : mws_peer_manager_get_peer_credentials (MwsPeerManager *self,
150 : : const gchar *sender)
151 : : {
152 [ - + ]: 146 : g_return_val_if_fail (MWS_IS_PEER_MANAGER (self), NULL);
153 [ - + ]: 146 : g_return_val_if_fail (g_dbus_is_unique_name (sender), NULL);
154 : :
155 : 146 : MwsPeerManagerInterface *iface = MWS_PEER_MANAGER_GET_IFACE (self);
156 [ - + ]: 146 : g_assert (iface->get_peer_credentials != NULL);
157 : :
158 : 146 : return iface->get_peer_credentials (self, sender);
159 : : }
|