| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the QtSystems module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include <qserviceframeworkglobal.h> |
| 41 | #include <QTimer> |
| 42 | #include "instancemanager_p.h" |
| 43 | #include "qremoteserviceregisterentry_p.h" |
| 44 | |
| 45 | QT_BEGIN_NAMESPACE |
| 46 | |
| 47 | Q_GLOBAL_STATIC(InstanceManager, typeRegister); |
| 48 | |
| 49 | /*! |
| 50 | \internal |
| 51 | |
| 52 | Returns the instance manager for the service process |
| 53 | */ |
| 54 | InstanceManager* InstanceManager::instance() |
| 55 | { |
| 56 | return typeRegister(); |
| 57 | } |
| 58 | |
| 59 | InstanceManager::InstanceManager(QObject *parent) |
| 60 | : QObject(parent) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | InstanceManager::~InstanceManager() |
| 65 | { |
| 66 | QList<QRemoteServiceRegister::Entry> allEntries = metaMap.keys(); |
| 67 | while (!allEntries.isEmpty()) { |
| 68 | ServiceIdentDescriptor descr = metaMap.take(allEntries.takeFirst()); |
| 69 | if (descr.entryData->instanceType == QRemoteServiceRegister::GlobalInstance) { |
| 70 | if (descr.globalInstance) |
| 71 | descr.globalInstance->deleteLater(); |
| 72 | descr.globalInstance = 0; |
| 73 | } else { |
| 74 | QList<QUuid> allUuids = descr.individualInstances.keys(); |
| 75 | while (!allUuids.isEmpty()) { |
| 76 | descr.individualInstances.take(allUuids.takeFirst())->deleteLater(); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | } |
| 82 | |
| 83 | /*! |
| 84 | \internal |
| 85 | |
| 86 | Adds an entry to the map of service identifiers |
| 87 | */ |
| 88 | bool InstanceManager::addType(const QRemoteServiceRegister::Entry& e) |
| 89 | { |
| 90 | QMutexLocker ml(&lock); |
| 91 | |
| 92 | if (metaMap.contains(e)) { |
| 93 | qWarning() << "Service" << e.serviceName() << "(" << e.interfaceName() |
| 94 | << ", " << e.version() << ")" << "already registered"; |
| 95 | } else { |
| 96 | ServiceIdentDescriptor d; |
| 97 | d.entryData = e.d; |
| 98 | metaMap.insert(e, d); |
| 99 | return true; |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | /*! |
| 105 | \internal |
| 106 | |
| 107 | Returns the metaobject of a registered service object identified by its \a entry |
| 108 | */ |
| 109 | const QMetaObject* InstanceManager::metaObject(const QRemoteServiceRegister::Entry& entry) const |
| 110 | { |
| 111 | QMutexLocker ml(&lock); |
| 112 | if (metaMap.contains(entry)) { |
| 113 | return metaMap[entry].entryData->meta; |
| 114 | } else { |
| 115 | return 0; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /*! |
| 120 | \internal |
| 121 | |
| 122 | Returns a list of all the registered entries |
| 123 | */ |
| 124 | QList<QRemoteServiceRegister::Entry> InstanceManager::allEntries() const |
| 125 | { |
| 126 | QMutexLocker ml(&lock); |
| 127 | return metaMap.keys(); |
| 128 | } |
| 129 | |
| 130 | /*! |
| 131 | \internal |
| 132 | |
| 133 | Instance manager takes ownership of service instance. Returns a null pointer |
| 134 | if \a entry cannot be mapped to a known meta object. The \a instanceId will |
| 135 | contain the unique ID for the new service instance. |
| 136 | */ |
| 137 | QObject* InstanceManager::createObjectInstance(const QRemoteServiceRegister::Entry& entry, QUuid& instanceId, QServiceClientCredentials& creds) |
| 138 | { |
| 139 | instanceId = QUuid(); |
| 140 | QMutexLocker ml(&lock); |
| 141 | if (!metaMap.contains(entry)) |
| 142 | return 0; |
| 143 | |
| 144 | QObject* service = 0; |
| 145 | ServiceIdentDescriptor& descr = metaMap[entry]; |
| 146 | |
| 147 | if (descr.entryData->instanceType == QRemoteServiceRegister::GlobalInstance) { |
| 148 | if (descr.globalInstance) { |
| 149 | service = descr.globalInstance; |
| 150 | instanceId = descr.globalId; |
| 151 | descr.globalRefCount++; |
| 152 | if (!QMetaObject::invokeMethod(service, "verifyNewServiceClientCredentials", Q_ARG(QServiceClientCredentials*, &creds))){ |
| 153 | qWarning() << "Unable to authenticate new client connection on shared object" << descr.entryData->meta->className(); |
| 154 | } |
| 155 | } else { |
| 156 | bool hasSecureConstructor = false; |
| 157 | const QMetaObject* metaObject = descr.entryData->meta; |
| 158 | for (int i = 0; i < metaObject->constructorCount(); ++i) { |
| 159 | const QMetaMethod method = metaObject->constructor(i); |
| 160 | const QList<QByteArray> params = method.parameterTypes(); |
| 161 | if (params.at(0) == "QServiceClientCredentials*") { |
| 162 | hasSecureConstructor = true; |
| 163 | service = metaObject->newInstance(Q_ARG(QServiceClientCredentials*, &creds)); |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | if (!hasSecureConstructor) { |
| 168 | qWarning() << "caution SFW using constructor without security credentials" << descr.entryData->meta->className(); |
| 169 | service = (*descr.entryData->cptr)(); |
| 170 | } |
| 171 | if (!service) |
| 172 | return 0; |
| 173 | |
| 174 | descr.globalInstance = service; |
| 175 | descr.globalId = instanceId = QUuid::createUuid(); |
| 176 | descr.globalRefCount = 1; |
| 177 | } |
| 178 | } else { |
| 179 | bool hasSecureConstructor = false; |
| 180 | const QMetaObject* metaObject = descr.entryData->meta; |
| 181 | for (int i = 0; i < metaObject->constructorCount(); ++i) { |
| 182 | const QMetaMethod method = metaObject->constructor(i); |
| 183 | const QList<QByteArray> params = method.parameterTypes(); |
| 184 | if (params.at(0) == "QServiceClientCredentials*") { |
| 185 | hasSecureConstructor = true; |
| 186 | service = metaObject->newInstance(Q_ARG(QServiceClientCredentials*, &creds)); |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | if (!hasSecureConstructor) { |
| 191 | qWarning() << "caution SFW using constructor without security credentials" << descr.entryData->meta->className(); |
| 192 | service = (*descr.entryData->cptr)(); |
| 193 | } |
| 194 | if (!service) |
| 195 | return 0; |
| 196 | instanceId = QUuid::createUuid(); |
| 197 | descr.individualInstances.insert(instanceId, service); |
| 198 | } |
| 199 | |
| 200 | return service; |
| 201 | } |
| 202 | |
| 203 | /*! |
| 204 | \internal |
| 205 | |
| 206 | The associated service object instance will be deleted in the service process. |
| 207 | Removes an instance with \a instanceId from a map of remote service descriptors |
| 208 | using the \a entry as the key. |
| 209 | |
| 210 | Emits instanceClosed() and allInstancesClosed() if no more instances are open |
| 211 | */ |
| 212 | void InstanceManager::removeObjectInstance(const QRemoteServiceRegister::Entry& entry, const QUuid& instanceId) |
| 213 | { |
| 214 | QMutexLocker ml(&lock); |
| 215 | if (!metaMap.contains(entry)) |
| 216 | return; |
| 217 | |
| 218 | ServiceIdentDescriptor& descr = metaMap[entry]; |
| 219 | if (descr.entryData->instanceType == QRemoteServiceRegister::GlobalInstance) { |
| 220 | if (descr.globalRefCount < 1) |
| 221 | return; |
| 222 | |
| 223 | if (descr.globalRefCount == 1) { |
| 224 | if (descr.globalInstance) |
| 225 | QTimer::singleShot(0, descr.globalInstance, SLOT(deleteLater())); |
| 226 | descr.globalInstance = 0; |
| 227 | descr.globalId = QUuid(); |
| 228 | descr.globalRefCount = 0; |
| 229 | emit instanceClosed(entry); |
| 230 | emit instanceClosed(entry, instanceId); //internal use |
| 231 | } else { |
| 232 | descr.globalRefCount--; |
| 233 | } |
| 234 | } else { |
| 235 | QObject* service = descr.individualInstances.take(instanceId); |
| 236 | if (service) { |
| 237 | service->deleteLater(); |
| 238 | emit instanceClosed(entry); |
| 239 | emit instanceClosed(entry, instanceId); //internal use |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // Check that no instances are open |
| 244 | if (totalInstances() < 1) |
| 245 | emit allInstancesClosed(); |
| 246 | } |
| 247 | |
| 248 | /*! |
| 249 | \internal |
| 250 | |
| 251 | Provides a count of how many global and private instances are currently open |
| 252 | */ |
| 253 | int InstanceManager::totalInstances() const |
| 254 | { |
| 255 | int total = 0; |
| 256 | |
| 257 | QList<QRemoteServiceRegister::Entry> allEntries = metaMap.keys(); |
| 258 | foreach (const QRemoteServiceRegister::Entry& entry, allEntries) { |
| 259 | ServiceIdentDescriptor descr = metaMap[entry]; |
| 260 | total += descr.globalRefCount; |
| 261 | total += descr.individualInstances.size(); |
| 262 | } |
| 263 | |
| 264 | return total; |
| 265 | } |
| 266 | |
| 267 | #include "moc_instancemanager_p.cpp" |
| 268 | |
| 269 | QT_END_NAMESPACE |