| 1 | /* |
| 2 | * Copyright (C) 2014 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of lomiri-online-accounts-ui |
| 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/ipc.h> |
| 23 | #include "request.h" |
| 24 | #include "signonui-request.h" |
| 25 | #include "ui-server.h" |
| 26 | |
| 27 | #include <LomiriOnlineAccountsPlugin/request-handler.h> |
| 28 | #include <QByteArray> |
| 29 | #include <QDataStream> |
| 30 | #include <QLocalSocket> |
| 31 | #include <QtQml> |
| 32 | #include <SignOn/uisessiondata_priv.h> |
| 33 | |
| 34 | using namespace OnlineAccountsUi; |
| 35 | |
| 36 | static UiServer *m_instance = 0; |
| 37 | |
| 38 | namespace OnlineAccountsUi { |
| 39 | |
| 40 | class UiServerPrivate: public QObject |
| 41 | { |
| 42 | Q_OBJECT |
| 43 | Q_DECLARE_PUBLIC(UiServer) |
| 44 | |
| 45 | public: |
| 46 | inline UiServerPrivate(const QString &address, UiServer *pluginServer); |
| 47 | inline ~UiServerPrivate(); |
| 48 | |
| 49 | bool setupSocket(); |
| 50 | bool init(); |
| 51 | void sendOperation(const QVariantMap &data); |
| 52 | |
| 53 | private Q_SLOTS: |
| 54 | void onDataReady(QByteArray &data); |
| 55 | void onRequestCompleted(); |
| 56 | void registerHandler(SignOnUi::RequestHandler *handler); |
| 57 | |
| 58 | private: |
| 59 | QLocalSocket m_socket; |
| 60 | OnlineAccountsUi::Ipc m_ipc; |
| 61 | SignOnUi::RequestHandlerWatcher m_handlerWatcher; |
| 62 | mutable UiServer *q_ptr; |
| 63 | }; |
| 64 | |
| 65 | } // namespace |
| 66 | |
| 67 | UiServerPrivate::UiServerPrivate(const QString &address, |
| 68 | UiServer *pluginServer): |
| 69 | QObject(pluginServer), |
| 70 | q_ptr(pluginServer) |
| 71 | { |
| 72 | QObject::connect(&m_ipc, SIGNAL(dataReady(QByteArray &)), |
| 73 | this, SLOT(onDataReady(QByteArray &))); |
| 74 | QObject::connect(&m_socket, SIGNAL(disconnected()), |
| 75 | q_ptr, SIGNAL(finished())); |
| 76 | m_socket.connectToServer(address); |
| 77 | |
| 78 | QObject::connect(&m_handlerWatcher, |
| 79 | SIGNAL(newHandler(SignOnUi::RequestHandler *)), |
| 80 | this, |
| 81 | SLOT(registerHandler(SignOnUi::RequestHandler *))); |
| 82 | } |
| 83 | |
| 84 | UiServerPrivate::~UiServerPrivate() |
| 85 | { |
| 86 | DEBUG(); |
| 87 | } |
| 88 | |
| 89 | void UiServerPrivate::sendOperation(const QVariantMap &data) |
| 90 | { |
| 91 | QByteArray ba; |
| 92 | QDataStream stream(&ba, QIODevice::WriteOnly); |
| 93 | stream << data; |
| 94 | m_ipc.write(ba); |
| 95 | } |
| 96 | |
| 97 | void UiServerPrivate::onDataReady(QByteArray &data) |
| 98 | { |
| 99 | QVariantMap map; |
| 100 | QDataStream stream(&data, QIODevice::ReadOnly); |
| 101 | stream >> map; |
| 102 | |
| 103 | DEBUG() << map; |
| 104 | |
| 105 | QString code = map.value(OAU_OPERATION_CODE).toString(); |
| 106 | if (code == OAU_OPERATION_CODE_PROCESS) { |
| 107 | QVariantMap parameters = map[OAU_OPERATION_DATA].toMap(); |
| 108 | Request *request = |
| 109 | Request::newRequest(map[OAU_OPERATION_INTERFACE].toString(), |
| 110 | map[OAU_OPERATION_ID].toInt(), |
| 111 | map[OAU_OPERATION_CLIENT_PROFILE].toString(), |
| 112 | parameters, |
| 113 | this); |
| 114 | QObject::connect(request, SIGNAL(completed()), |
| 115 | this, SLOT(onRequestCompleted())); |
| 116 | |
| 117 | /* Check if a RequestHandler has been setup to handle this request. If |
| 118 | * so, bing the request object to the handler and start the request |
| 119 | * immediately. */ |
| 120 | SignOnUi::Request *signonRequest = |
| 121 | qobject_cast<SignOnUi::Request*>(request); |
| 122 | if (signonRequest) { |
| 123 | SignOnUi::RequestHandler *handler = |
| 124 | m_handlerWatcher.findMatching(parameters); |
| 125 | if (handler) { |
| 126 | signonRequest->setHandler(handler); |
| 127 | } |
| 128 | } |
| 129 | request->start(); |
| 130 | } else { |
| 131 | qWarning() << "Invalid operation code: " << code; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void UiServerPrivate::onRequestCompleted() |
| 136 | { |
| 137 | Request *request = qobject_cast<Request*>(sender()); |
| 138 | request->disconnect(this); |
| 139 | request->deleteLater(); |
| 140 | |
| 141 | if (request->errorName().isEmpty()) { |
| 142 | QVariantMap operation; |
| 143 | operation.insert(OAU_OPERATION_CODE, |
| 144 | OAU_OPERATION_CODE_REQUEST_FINISHED); |
| 145 | operation.insert(OAU_OPERATION_ID, request->id()); |
| 146 | operation.insert(OAU_OPERATION_DATA, request->result()); |
| 147 | operation.insert(OAU_OPERATION_DELAY, request->delay()); |
| 148 | operation.insert(OAU_OPERATION_INTERFACE, request->interface()); |
| 149 | sendOperation(operation); |
| 150 | } else { |
| 151 | QVariantMap operation; |
| 152 | operation.insert(OAU_OPERATION_CODE, |
| 153 | OAU_OPERATION_CODE_REQUEST_FAILED); |
| 154 | operation.insert(OAU_OPERATION_ID, request->id()); |
| 155 | operation.insert(OAU_OPERATION_INTERFACE, request->interface()); |
| 156 | operation.insert(OAU_OPERATION_ERROR_NAME, request->errorName()); |
| 157 | operation.insert(OAU_OPERATION_ERROR_MESSAGE, request->errorMessage()); |
| 158 | sendOperation(operation); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | bool UiServerPrivate::init() |
| 163 | { |
| 164 | if (Q_UNLIKELY(!m_socket.waitForConnected())) return false; |
| 165 | |
| 166 | m_ipc.setChannels(&m_socket, &m_socket); |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | void UiServerPrivate::registerHandler(SignOnUi::RequestHandler *handler) |
| 171 | { |
| 172 | QVariantMap operation; |
| 173 | operation.insert(OAU_OPERATION_CODE, |
| 174 | OAU_OPERATION_CODE_REGISTER_HANDLER); |
| 175 | operation.insert(OAU_OPERATION_HANDLER_ID, handler->matchId()); |
| 176 | sendOperation(operation); |
| 177 | } |
| 178 | |
| 179 | UiServer::UiServer(const QString &address, QObject *parent): |
| 180 | QObject(parent), |
| 181 | d_ptr(new UiServerPrivate(address, this)) |
| 182 | { |
| 183 | m_instance = this; |
| 184 | } |
| 185 | |
| 186 | UiServer::~UiServer() |
| 187 | { |
| 188 | m_instance = 0; |
| 189 | } |
| 190 | |
| 191 | UiServer *UiServer::instance() |
| 192 | { |
| 193 | return m_instance; |
| 194 | } |
| 195 | |
| 196 | bool UiServer::init() |
| 197 | { |
| 198 | Q_D(UiServer); |
| 199 | return d->init(); |
| 200 | } |
| 201 | |
| 202 | #include "ui-server.moc" |