| 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 "request-manager.h" |
| 25 | #include "ui-proxy.h" |
| 26 | |
| 27 | #include <QQueue> |
| 28 | |
| 29 | using namespace OnlineAccountsUi; |
| 30 | |
| 31 | namespace OnlineAccountsUi { |
| 32 | |
| 33 | static RequestManager *m_instance = 0; |
| 34 | |
| 35 | typedef QQueue<Request*> RequestQueue; |
| 36 | |
| 37 | class RequestManagerPrivate: public QObject |
| 38 | { |
| 39 | Q_OBJECT |
| 40 | Q_DECLARE_PUBLIC(RequestManager) |
| 41 | |
| 42 | public: |
| 43 | RequestManagerPrivate(RequestManager *service); |
| 44 | ~RequestManagerPrivate(); |
| 45 | |
| 46 | RequestQueue &queueForWindowId(quint64 windowId); |
| 47 | void enqueue(Request *request); |
| 48 | void runQueue(RequestQueue &queue); |
| 49 | |
| 50 | private Q_SLOTS: |
| 51 | void onRequestCompleted(); |
| 52 | void onProxyFinished(); |
| 53 | |
| 54 | private: |
| 55 | mutable RequestManager *q_ptr; |
| 56 | /* each window Id has a different queue */ |
| 57 | QMap<quint64,RequestQueue> m_requests; |
| 58 | QList<UiProxy*> m_proxies; |
| 59 | }; |
| 60 | |
| 61 | } // namespace |
| 62 | |
| 63 | RequestManagerPrivate::RequestManagerPrivate(RequestManager *service): |
| 64 | QObject(service), |
| 65 | q_ptr(service) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | RequestManagerPrivate::~RequestManagerPrivate() |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | RequestQueue &RequestManagerPrivate::queueForWindowId(quint64 windowId) |
| 74 | { |
| 75 | if (!m_requests.contains(windowId)) { |
| 76 | RequestQueue queue; |
| 77 | m_requests.insert(windowId, queue); |
| 78 | } |
| 79 | return m_requests[windowId]; |
| 80 | } |
| 81 | |
| 82 | void RequestManagerPrivate::enqueue(Request *request) |
| 83 | { |
| 84 | Q_Q(RequestManager); |
| 85 | |
| 86 | /* First, see if any of the existing proxies can handle this request */ |
| 87 | Q_FOREACH(UiProxy *proxy, m_proxies) { |
| 88 | if (proxy->hasHandlerFor(request->parameters())) { |
| 89 | QObject::connect(request, SIGNAL(completed()), |
| 90 | request, SLOT(deleteLater())); |
| 91 | proxy->handleRequest(request); |
| 92 | return; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | bool wasIdle = q->isIdle(); |
| 97 | |
| 98 | quint64 windowId = request->windowId(); |
| 99 | |
| 100 | RequestQueue &queue = queueForWindowId(windowId); |
| 101 | queue.enqueue(request); |
| 102 | |
| 103 | if (wasIdle) { |
| 104 | Q_EMIT q->isIdleChanged(); |
| 105 | } |
| 106 | |
| 107 | runQueue(queue); |
| 108 | } |
| 109 | |
| 110 | void RequestManagerPrivate::runQueue(RequestQueue &queue) |
| 111 | { |
| 112 | Request *request = queue.head(); |
| 113 | DEBUG() << "Head:" << request; |
| 114 | |
| 115 | if (request->isInProgress()) { |
| 116 | DEBUG() << "Already in progress"; |
| 117 | return; // Nothing to do |
| 118 | } |
| 119 | |
| 120 | QObject::connect(request, SIGNAL(completed()), |
| 121 | this, SLOT(onRequestCompleted())); |
| 122 | |
| 123 | UiProxy *proxy = new UiProxy(request->clientPid(), this); |
| 124 | if (Q_UNLIKELY(!proxy->init())) { |
| 125 | qWarning() << "UiProxy initialization failed!"; |
| 126 | request->fail(OAU_ERROR_PROMPT_SESSION, |
| 127 | "Could not create a prompt session"); |
| 128 | return; |
| 129 | } |
| 130 | QObject::connect(proxy, SIGNAL(finished()), |
| 131 | this, SLOT(onProxyFinished())); |
| 132 | m_proxies.append(proxy); |
| 133 | proxy->handleRequest(request); |
| 134 | } |
| 135 | |
| 136 | void RequestManagerPrivate::onRequestCompleted() |
| 137 | { |
| 138 | Q_Q(RequestManager); |
| 139 | |
| 140 | Request *request = qobject_cast<Request*>(sender()); |
| 141 | quint64 windowId = request->windowId(); |
| 142 | |
| 143 | RequestQueue &queue = queueForWindowId(windowId); |
| 144 | if (request != queue.head()) { |
| 145 | qCritical("Completed request is not first in queue!"); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | queue.dequeue(); |
| 150 | request->deleteLater(); |
| 151 | |
| 152 | if (queue.isEmpty()) { |
| 153 | m_requests.remove(windowId); |
| 154 | } else { |
| 155 | /* start the next request */ |
| 156 | runQueue(queue); |
| 157 | } |
| 158 | |
| 159 | if (q->isIdle()) { |
| 160 | Q_EMIT q->isIdleChanged(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void RequestManagerPrivate::onProxyFinished() |
| 165 | { |
| 166 | UiProxy *proxy = qobject_cast<UiProxy*>(sender()); |
| 167 | m_proxies.removeOne(proxy); |
| 168 | |
| 169 | proxy->deleteLater(); |
| 170 | } |
| 171 | |
| 172 | RequestManager::RequestManager(QObject *parent): |
| 173 | QObject(parent), |
| 174 | d_ptr(new RequestManagerPrivate(this)) |
| 175 | { |
| 176 | if (m_instance == 0) { |
| 177 | m_instance = this; |
| 178 | } else { |
| 179 | qWarning() << "Instantiating a second RequestManager!"; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | RequestManager::~RequestManager() |
| 184 | { |
| 185 | } |
| 186 | |
| 187 | RequestManager *RequestManager::instance() |
| 188 | { |
| 189 | return m_instance; |
| 190 | } |
| 191 | |
| 192 | void RequestManager::enqueue(Request *request) |
| 193 | { |
| 194 | Q_D(RequestManager); |
| 195 | d->enqueue(request); |
| 196 | } |
| 197 | |
| 198 | bool RequestManager::isIdle() const |
| 199 | { |
| 200 | Q_D(const RequestManager); |
| 201 | return d->m_requests.isEmpty(); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | #include "request-manager.moc" |