| 1 | /* |
| 2 | * Copyright (C) 2013 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of lomiri-online-accounts-service |
| 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 <online-accounts-common/debug.h> |
| 22 | #include <online-accounts-common/globals.h> |
| 23 | #include "request.h" |
| 24 | #include "utils.h" |
| 25 | |
| 26 | #include <Accounts/Manager> |
| 27 | #include <Accounts/Service> |
| 28 | #include <SignOn/uisessiondata_priv.h> |
| 29 | |
| 30 | using namespace OnlineAccountsUi; |
| 31 | |
| 32 | static bool mapIsSuperset(const QVariantMap &test, const QVariantMap &set) |
| 33 | { |
| 34 | QMapIterator<QString, QVariant> it(set); |
| 35 | while (it.hasNext()) { |
| 36 | it.next(); |
| 37 | if (test.value(it.key()) != it.value()) return false; |
| 38 | } |
| 39 | |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | namespace OnlineAccountsUi { |
| 44 | |
| 45 | static QList<Request *> allRequests; |
| 46 | |
| 47 | class RequestPrivate: public QObject |
| 48 | { |
| 49 | Q_OBJECT |
| 50 | Q_DECLARE_PUBLIC(Request) |
| 51 | |
| 52 | public: |
| 53 | RequestPrivate(const QDBusConnection &connection, |
| 54 | const QDBusMessage &message, |
| 55 | const QVariantMap ¶meters, |
| 56 | Request *request); |
| 57 | ~RequestPrivate(); |
| 58 | |
| 59 | quint64 windowId() const { |
| 60 | return m_parameters[OAU_KEY_WINDOW_ID].toUInt(); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | mutable Request *q_ptr; |
| 65 | QDBusConnection m_connection; |
| 66 | QDBusMessage m_message; |
| 67 | QVariantMap m_parameters; |
| 68 | QString m_clientApparmorProfile; |
| 69 | bool m_inProgress; |
| 70 | int m_delay; |
| 71 | }; |
| 72 | |
| 73 | } // namespace |
| 74 | |
| 75 | RequestPrivate::RequestPrivate(const QDBusConnection &connection, |
| 76 | const QDBusMessage &message, |
| 77 | const QVariantMap ¶meters, |
| 78 | Request *request): |
| 79 | QObject(request), |
| 80 | q_ptr(request), |
| 81 | m_connection(connection), |
| 82 | m_message(message), |
| 83 | m_parameters(parameters), |
| 84 | m_inProgress(false), |
| 85 | m_delay(0) |
| 86 | { |
| 87 | m_clientApparmorProfile = apparmorProfileOfPeer(message); |
| 88 | } |
| 89 | |
| 90 | RequestPrivate::~RequestPrivate() |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | Request::Request(const QDBusConnection &connection, |
| 95 | const QDBusMessage &message, |
| 96 | const QVariantMap ¶meters, |
| 97 | QObject *parent): |
| 98 | QObject(parent), |
| 99 | d_ptr(new RequestPrivate(connection, message, parameters, this)) |
| 100 | { |
| 101 | allRequests.append(this); |
| 102 | } |
| 103 | |
| 104 | Request::~Request() |
| 105 | { |
| 106 | allRequests.removeOne(this); |
| 107 | } |
| 108 | |
| 109 | Request *Request::find(const QVariantMap &match) |
| 110 | { |
| 111 | Q_FOREACH(Request *r, allRequests) { |
| 112 | if (mapIsSuperset(r->parameters(), match)) { |
| 113 | return r; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | quint64 Request::windowId() const |
| 121 | { |
| 122 | Q_D(const Request); |
| 123 | return d->windowId(); |
| 124 | } |
| 125 | |
| 126 | pid_t Request::clientPid() const |
| 127 | { |
| 128 | Q_D(const Request); |
| 129 | if (interface() == OAU_INTERFACE) { |
| 130 | return d->m_parameters.value(OAU_KEY_PID, 0).toUInt(); |
| 131 | } else if (interface() == SIGNONUI_INTERFACE) { |
| 132 | if (d->m_clientApparmorProfile == "unconfined") { |
| 133 | QVariantMap clientData = |
| 134 | d->m_parameters.value(SSOUI_KEY_CLIENT_DATA).toMap(); |
| 135 | if (clientData.contains("requestorPid")) { |
| 136 | return clientData.value("requestorPid").toUInt(); |
| 137 | } |
| 138 | } |
| 139 | return d->m_parameters.value(SSOUI_KEY_PID, 0).toUInt(); |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | void Request::setInProgress(bool inProgress) |
| 146 | { |
| 147 | Q_D(Request); |
| 148 | d->m_inProgress = inProgress; |
| 149 | } |
| 150 | |
| 151 | bool Request::isInProgress() const |
| 152 | { |
| 153 | Q_D(const Request); |
| 154 | return d->m_inProgress; |
| 155 | } |
| 156 | |
| 157 | const QVariantMap &Request::parameters() const |
| 158 | { |
| 159 | Q_D(const Request); |
| 160 | return d->m_parameters; |
| 161 | } |
| 162 | |
| 163 | QString Request::clientApparmorProfile() const |
| 164 | { |
| 165 | Q_D(const Request); |
| 166 | return d->m_clientApparmorProfile; |
| 167 | } |
| 168 | |
| 169 | QString Request::interface() const { |
| 170 | Q_D(const Request); |
| 171 | return d->m_message.interface(); |
| 172 | } |
| 173 | |
| 174 | QString Request::providerId() const |
| 175 | { |
| 176 | Q_D(const Request); |
| 177 | if (interface() == OAU_INTERFACE) { |
| 178 | QString providerId = |
| 179 | d->m_parameters.value(OAU_KEY_PROVIDER).toString(); |
| 180 | if (providerId.isEmpty() && |
| 181 | d->m_parameters.contains(OAU_KEY_SERVICE_ID)) { |
| 182 | Accounts::Manager manager; |
| 183 | QString serviceId = d->m_parameters[OAU_KEY_SERVICE_ID].toString(); |
| 184 | Accounts::Service service = manager.service(serviceId); |
| 185 | if (service.isValid()) { |
| 186 | providerId = service.provider(); |
| 187 | } |
| 188 | } |
| 189 | return providerId; |
| 190 | } else { |
| 191 | return QString(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void Request::setDelay(int delay) |
| 196 | { |
| 197 | Q_D(Request); |
| 198 | d->m_delay = delay; |
| 199 | } |
| 200 | |
| 201 | int Request::delay() const |
| 202 | { |
| 203 | Q_D(const Request); |
| 204 | return d->m_delay; |
| 205 | } |
| 206 | |
| 207 | void Request::cancel() |
| 208 | { |
| 209 | setCanceled(); |
| 210 | } |
| 211 | |
| 212 | void Request::fail(const QString &name, const QString &message) |
| 213 | { |
| 214 | Q_D(Request); |
| 215 | QDBusMessage reply = d->m_message.createErrorReply(name, message); |
| 216 | d->m_connection.send(reply); |
| 217 | |
| 218 | Q_EMIT completed(); |
| 219 | } |
| 220 | |
| 221 | void Request::setCanceled() |
| 222 | { |
| 223 | Q_D(Request); |
| 224 | if (d->m_inProgress) { |
| 225 | fail(OAU_ERROR_USER_CANCELED, QStringLiteral("Canceled")); |
| 226 | d->m_inProgress = false; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void Request::setResult(const QVariantMap &result) |
| 231 | { |
| 232 | Q_D(Request); |
| 233 | if (d->m_inProgress) { |
| 234 | QDBusMessage reply = d->m_message.createReply(result); |
| 235 | d->m_connection.send(reply); |
| 236 | |
| 237 | Q_EMIT completed(); |
| 238 | d->m_inProgress = false; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | #include "request.moc" |