Debian Codegraph @

online-accounts-service/src/indicator-service.cpp

raw symbols SCIP
1 /*
2 * This file is part of lomiri-online-accounts-service
3 *
4 * Copyright (C) 2014 Canonical Ltd.
5 *
6 * Contact: Alberto Mardegan <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 3, as published
10 * by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranties of
14 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "indicator-service.h"
22
23 #include <online-accounts-common/debug.h>
24 #include <online-accounts-common/i18n.h>
25 #include "notification.h"
26 #include "reauthenticator.h"
27 #include "webcredentialsadaptor.h"
28
29 #include <QByteArray>
30 #include <QDBusContext>
31
32 using namespace OnlineAccountsUi;
33 using namespace SignOnUi;
34
35 QDBusArgument &operator<<(QDBusArgument &argument, const QSet<uint> &set)
36 {
37 argument.beginArray(qMetaTypeId<uint>());
38 Q_FOREACH(uint id, set) {
39 argument << id;
40 }
41 argument.endArray();
42 return argument;
43 }
44
45 const QDBusArgument &operator>>(const QDBusArgument &argument,
46 QSet<uint> &set)
47 {
48 argument.beginArray();
49 while (!argument.atEnd()) {
50 uint id;
51 argument >> id;
52 set.insert(id);
53 }
54 argument.endArray();
55 return argument;
56 }
57
58 namespace SignOnUi {
59
60 static IndicatorService *m_instance = 0;
61
62 class IndicatorServicePrivate: public QObject, QDBusContext
63 {
64 Q_OBJECT
65 Q_DECLARE_PUBLIC(IndicatorService)
66
67 public:
68 Q_PROPERTY(QSet<uint> Failures READ failures)
69 Q_PROPERTY(bool ErrorStatus READ errorStatus)
70
71 IndicatorServicePrivate(IndicatorService *service);
72 ~IndicatorServicePrivate() {};
73
74 QSet<uint> failures() const { return m_failures; }
75 bool errorStatus() const { return m_errorStatus; }
76
77 public Q_SLOTS:
78 void ClearErrorStatus();
79 void RemoveFailures(const QSet<uint> &accountIds);
80 void ReportFailure(uint accountId, const QVariantMap &notification);
81 bool ReauthenticateAccount(uint accountId,
82 const QVariantMap &extraParameters);
83
84 private:
85 void showNotification(const QVariantMap &parameters);
86 void notifyPropertyChanged(const char *propertyName);
87
88 private Q_SLOTS:
89 void onReauthenticatorFinished(bool success);
90
91 private:
92 mutable IndicatorService *q_ptr;
93 WebcredentialsAdaptor *m_adaptor;
94 QSet<uint> m_failures;
95 QMap<uint, QList<AuthData> > m_failureClientData;
96 QMap<uint, Reauthenticator*> m_reauthenticators;
97 QDBusMessage m_clientMessage;
98 bool m_errorStatus;
99 };
100
101 } // namespace
102
103 IndicatorServicePrivate::IndicatorServicePrivate(IndicatorService *service):
104 QObject(service),
105 q_ptr(service),
106 m_adaptor(new WebcredentialsAdaptor(service)),
107 m_errorStatus(false)
108 {
109 qDBusRegisterMetaType< QSet<uint> >();
110 }
111
112 void IndicatorServicePrivate::ClearErrorStatus()
113 {
114 if (m_errorStatus) {
115 m_errorStatus = false;
116 notifyPropertyChanged("ErrorStatus");
117 }
118 }
119
120 void IndicatorServicePrivate::RemoveFailures(const QSet<uint> &accountIds)
121 {
122 Q_Q(IndicatorService);
123 m_failures.subtract(accountIds);
124 notifyPropertyChanged("Failures");
125 if (q->isIdle()) {
126 Q_EMIT q->isIdleChanged();
127 }
128 }
129
130 void IndicatorServicePrivate::ReportFailure(uint accountId,
131 const QVariantMap &notification)
132 {
133 Q_Q(IndicatorService);
134 bool wasIdle = q->isIdle();
135 m_failures.insert(accountId);
136 if (wasIdle) {
137 Q_EMIT q->isIdleChanged();
138 }
139
140 /* If the original client data is provided, we remember it: it can
141 * be used to replay the authentication later.
142 */
143 if (notification.contains("ClientData")) {
144 /* If the key is not found, the QMap's [] operator inserts an empty
145 * element in the map and return a reference to it. So the following
146 * line of code returns a valid QList even if the account never failed
147 * before.
148 */
149 QList<AuthData> &failedAuthentications =
150 m_failureClientData[accountId];
151
152 AuthData authData;
153 authData.sessionData = notification["ClientData"].toMap();
154 authData.identity = quint32(notification["Identity"].toUInt());
155 authData.method = notification["Method"].toString();
156 authData.mechanism = notification["Mechanism"].toString();
157 failedAuthentications.append(authData);
158 }
159
160 notifyPropertyChanged("Failures");
161
162 showNotification(notification);
163 }
164
165 bool IndicatorServicePrivate::ReauthenticateAccount(uint accountId,
166 const QVariantMap &extraParameters)
167 {
168 if (!m_failureClientData.contains(accountId)) {
169 /* Nothing we can do about this account */
170 DEBUG() << "No reauthentication data for account" << accountId;
171 return false;
172 }
173
174 if (m_reauthenticators.contains(accountId)) {
175 /* A reauthenticator for this account is already at work. This
176 * shouldn't happen in a real world scenario. */
177 qWarning() << "Reauthenticator already active on" << accountId;
178 return false;
179 }
180
181 DEBUG() << "Reauthenticating account" << accountId;
182
183 /* If we need to reauthenticate, we are delivering the result
184 * after iterating the event loop, so we must inform QtDBus that
185 * it shouldn't use this method's return value as a result.
186 */
187 setDelayedReply(true);
188 m_clientMessage = message();
189 QList<AuthData> &failedAuthentications = m_failureClientData[accountId];
190
191 Reauthenticator *reauthenticator =
192 new Reauthenticator(failedAuthentications, extraParameters, this);
193 m_reauthenticators[accountId] = reauthenticator;
194
195 QObject::connect(reauthenticator, SIGNAL(finished(bool)),
196 this, SLOT(onReauthenticatorFinished(bool)),
197 Qt::QueuedConnection);
198 reauthenticator->start();
199
200 return true; // ignored, see setDelayedReply() above.
201 }
202
203 void IndicatorServicePrivate::showNotification(const QVariantMap &parameters)
204 {
205 /* Don't show more than one notification, until the error status is
206 * cleared */
207 if (m_errorStatus) return;
208
209 m_errorStatus = true;
210 notifyPropertyChanged("ErrorStatus");
211
212 QString applicationName = parameters.value("DisplayName").toString();
213
214 QString summary;
215 if (applicationName.isEmpty()) {
216 summary = _("Applications can no longer access "
217 "some of your Online Accounts",
218 SIGNONUI_I18N_DOMAIN);
219 } else {
220 summary = _("Applications can no longer access "
221 "your %1 Online Account",
222 SIGNONUI_I18N_DOMAIN).arg(applicationName);
223 }
224
225 QString message = _("Choose <b>Online Accounts</b> from the user "
226 "menu to reinstate access to this account.",
227 SIGNONUI_I18N_DOMAIN);
228
229 Notification notification(summary, message);
230 notification.show();
231 }
232
233 void IndicatorServicePrivate::notifyPropertyChanged(const char *propertyName)
234 {
235 QDBusMessage signal =
236 QDBusMessage::createSignal(WEBCREDENTIALS_OBJECT_PATH,
237 "org.freedesktop.DBus.Properties",
238 "PropertiesChanged");
239 signal << WEBCREDENTIALS_INTERFACE;
240 QVariantMap changedProps;
241 changedProps.insert(QString::fromLatin1(propertyName),
242 property(propertyName));
243 signal << changedProps;
244 signal << QStringList();
245 QDBusConnection::sessionBus().send(signal);
246 }
247
248 void IndicatorServicePrivate::onReauthenticatorFinished(bool success)
249 {
250 Q_Q(IndicatorService);
251
252 Reauthenticator *reauthenticator =
253 qobject_cast<Reauthenticator*>(sender());
254
255 /* Find the account; searching a map by value is inefficient, but
256 * in this case it's extremely likely that the map contains just
257 * one element. :-) */
258 uint accountId = 0;
259 QMap<uint,Reauthenticator*>::const_iterator i;
260 for (i = m_reauthenticators.constBegin();
261 i != m_reauthenticators.constEnd();
262 i++) {
263 if (i.value() == reauthenticator) {
264 accountId = i.key();
265 break;
266 }
267 }
268 Q_ASSERT (accountId != 0);
269
270 QDBusMessage reply = m_clientMessage.createReply(success);
271 QDBusConnection::sessionBus().send(reply);
272
273 if (success) {
274 m_failureClientData.remove(accountId);
275 m_failures.remove(accountId);
276 notifyPropertyChanged("Failures");
277
278 if (m_failures.isEmpty()) {
279 ClearErrorStatus();
280 Q_EMIT q->isIdleChanged();
281 }
282 }
283
284 m_reauthenticators.remove(accountId);
285 reauthenticator->deleteLater();
286 }
287
288 IndicatorService::IndicatorService(QObject *parent):
289 QObject(parent),
290 d_ptr(new IndicatorServicePrivate(this))
291 {
292 if (m_instance == 0) {
293 m_instance = this;
294 } else {
295 qWarning() << "Instantiating a second IndicatorService!";
296 }
297 }
298
299 IndicatorService::~IndicatorService()
300 {
301 m_instance = 0;
302 delete d_ptr;
303 }
304
305 IndicatorService *IndicatorService::instance()
306 {
307 return m_instance;
308 }
309
310 QObject *IndicatorService::serviceObject() const
311 {
312 return d_ptr;
313 }
314
315 void IndicatorService::ClearErrorStatus()
316 {
317 Q_D(IndicatorService);
318 d->ClearErrorStatus();
319 }
320
321 bool IndicatorService::ReauthenticateAccount(uint accountId,
322 const QVariantMap &extraParameters)
323 {
324 Q_D(IndicatorService);
325 return d->ReauthenticateAccount(accountId, extraParameters);
326 }
327
328 void IndicatorService::RemoveFailures(const QSet<uint> &accountIds)
329 {
330 Q_D(IndicatorService);
331 d->RemoveFailures(accountIds);
332 }
333
334 void IndicatorService::ReportFailure(uint accountId,
335 const QVariantMap &notification)
336 {
337 Q_D(IndicatorService);
338 d->ReportFailure(accountId, notification);
339 }
340
341 QSet<uint> IndicatorService::failures() const
342 {
343 Q_D(const IndicatorService);
344 return d->m_failures;
345 }
346
347 bool IndicatorService::errorStatus() const
348 {
349 Q_D(const IndicatorService);
350 return d->m_errorStatus;
351 }
352
353 bool IndicatorService::isIdle() const
354 {
355 Q_D(const IndicatorService);
356 return d->m_failures.isEmpty();
357 }
358
359 #include "indicator-service.moc"