Debian Codegraph @

online-accounts-plugins/lib/LomiriOnlineAccountsPlugin/request-handler.cpp

raw symbols SCIP
1 /*
2 * This file is part of liblomiri-online-accounts-plugin
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 "request-handler.h"
22
23 #include <SignOn/uisessiondata_priv.h>
24 #include <QDebug>
25 #include <QList>
26 #include <QPointer>
27 #include <unistd.h>
28
29 using namespace SignOnUi;
30
31 namespace SignOnUi {
32
33 static QList<RequestHandler *> allRequestHandlers;
34 static int counter = 1;
35
36 class RequestHandlerPrivate
37 {
38 Q_DECLARE_PUBLIC(RequestHandler)
39
40 public:
41 RequestHandlerPrivate(RequestHandler *request);
42 ~RequestHandlerPrivate();
43
44 private:
45 QString m_matchId;
46 QPointer<QObject> m_request;
47 mutable RequestHandler *q_ptr;
48 };
49
50 class RequestHandlerWatcherPrivate
51 {
52 Q_DECLARE_PUBLIC(RequestHandlerWatcher)
53
54 public:
55 RequestHandlerWatcherPrivate(RequestHandlerWatcher *watcher);
56 ~RequestHandlerWatcherPrivate();
57
58 static RequestHandlerWatcherPrivate *instance();
59 void registerHandler(RequestHandler *handler);
60
61 private:
62 mutable RequestHandlerWatcher *q_ptr;
63 static RequestHandlerWatcherPrivate *m_instance;
64 };
65
66 RequestHandlerWatcherPrivate *RequestHandlerWatcherPrivate::m_instance = 0;
67
68 } // namespace
69
70 RequestHandlerPrivate::RequestHandlerPrivate(RequestHandler *request):
71 q_ptr(request)
72 {
73 m_matchId = QString("%1-%2").arg(getpid()).arg(counter++);
74 }
75
76 RequestHandlerPrivate::~RequestHandlerPrivate()
77 {
78 }
79
80 RequestHandler::RequestHandler(QObject *parent):
81 QObject(parent),
82 d_ptr(new RequestHandlerPrivate(this))
83 {
84 allRequestHandlers.append(this);
85
86 RequestHandlerWatcherPrivate *watcher =
87 RequestHandlerWatcherPrivate::instance();
88 if (Q_LIKELY(watcher)) {
89 watcher->registerHandler(this);
90 }
91 }
92
93 RequestHandler::~RequestHandler()
94 {
95 allRequestHandlers.removeOne(this);
96 delete d_ptr;
97 }
98
99 void RequestHandler::setRequest(QObject *request)
100 {
101 Q_D(RequestHandler);
102
103 if (request == d->m_request) return;
104
105 if (d->m_request != 0 && request != 0) {
106 qCritical() << "RequestHandler is already assigned a request";
107 return;
108 }
109 d->m_request = request;
110 Q_EMIT requestChanged();
111 }
112
113 QObject *RequestHandler::request() const
114 {
115 Q_D(const RequestHandler);
116 return d->m_request;
117 }
118
119 QString RequestHandler::matchId() const
120 {
121 Q_D(const RequestHandler);
122 return d->m_matchId;
123 }
124
125 RequestHandlerWatcherPrivate::RequestHandlerWatcherPrivate(RequestHandlerWatcher *watcher):
126 q_ptr(watcher)
127 {
128 if (Q_UNLIKELY(m_instance)) {
129 qWarning() << "RequestHandlerWatcher should be instantiated once!";
130 }
131
132 m_instance = this;
133 }
134
135 RequestHandlerWatcherPrivate::~RequestHandlerWatcherPrivate()
136 {
137 m_instance = 0;
138 }
139
140 RequestHandlerWatcherPrivate *RequestHandlerWatcherPrivate::instance()
141 {
142 return m_instance;
143 }
144
145 void RequestHandlerWatcherPrivate::registerHandler(RequestHandler *handler)
146 {
147 Q_Q(RequestHandlerWatcher);
148
149 Q_EMIT q->newHandler(handler);
150 }
151
152 RequestHandlerWatcher::RequestHandlerWatcher(QObject *parent):
153 QObject(parent),
154 d_ptr(new RequestHandlerWatcherPrivate(this))
155 {
156 }
157
158 RequestHandlerWatcher::~RequestHandlerWatcher()
159 {
160 delete d_ptr;
161 }
162
163 RequestHandler *RequestHandlerWatcher::findMatching(const QVariantMap &parameters)
164 {
165 /* Find if there's any RequestHandler expecting to handle the SignOnUi
166 * request having "parameters" as parameters.
167 * This is simply done by matching on the X-RequestHandler key (aka
168 * matchKey()), if present. We expect that account plugins add that field
169 * to their AuthSession requests which they want to handle themselves.
170 */
171 qDebug() << parameters;
172 if (!parameters.contains(SSOUI_KEY_CLIENT_DATA)) return 0;
173 QVariantMap clientData = parameters[SSOUI_KEY_CLIENT_DATA].toMap();
174 qDebug() << "client data:" << clientData;
175 QString matchId = clientData.value(RequestHandler::matchKey()).toString();
176 if (matchId.isEmpty()) return 0;
177
178 Q_FOREACH(RequestHandler *handler, allRequestHandlers) {
179 if (handler->matchId() == matchId) return handler;
180 }
181 return 0;
182 }