| 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 "objectendpoint_dbus_p.h" |
| 41 | #include "instancemanager_p.h" |
| 42 | #include "proxyobject_p.h" |
| 43 | #include "qsignalintercepter_p.h" |
| 44 | #include <private/qmetaobjectbuilder_p.h> |
| 45 | #include <QTimer> |
| 46 | #include <QEventLoop> |
| 47 | #include <QVarLengthArray> |
| 48 | |
| 49 | QT_BEGIN_NAMESPACE |
| 50 | |
| 51 | class Response |
| 52 | { |
| 53 | public: |
| 54 | Response() : isFinished(false), result(0) |
| 55 | { } |
| 56 | |
| 57 | bool isFinished; |
| 58 | void* result; |
| 59 | }; |
| 60 | |
| 61 | typedef QHash<QUuid, Response*> Replies; |
| 62 | Q_GLOBAL_STATIC(Replies, openRequests); |
| 63 | |
| 64 | class ServiceSignalIntercepter : public QSignalIntercepter |
| 65 | { |
| 66 | //Do not put Q_OBJECT here |
| 67 | public: |
| 68 | ServiceSignalIntercepter(QObject* sender, const QByteArray& signal, |
| 69 | ObjectEndPoint* parent) |
| 70 | : QSignalIntercepter(sender, signal, parent), endPoint(parent) |
| 71 | { |
| 72 | |
| 73 | } |
| 74 | |
| 75 | void setMetaIndex(int index) |
| 76 | { |
| 77 | metaIndex = index; |
| 78 | } |
| 79 | |
| 80 | protected: |
| 81 | void activated( const QList<QVariant>& args ) |
| 82 | { |
| 83 | endPoint->invokeRemote(metaIndex, args, QMetaType::Void); |
| 84 | } |
| 85 | private: |
| 86 | ObjectEndPoint* endPoint; |
| 87 | int metaIndex; |
| 88 | }; |
| 89 | |
| 90 | struct ClientInstance { |
| 91 | QString clientId; |
| 92 | QRemoteServiceRegister::Entry entry; |
| 93 | QUuid instanceId; |
| 94 | int ref; |
| 95 | }; |
| 96 | |
| 97 | class ObjectEndPointPrivate |
| 98 | { |
| 99 | public: |
| 100 | ObjectEndPointPrivate() |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | ~ObjectEndPointPrivate() |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | // Used on client and service side |
| 109 | ObjectEndPoint::Type endPointType; |
| 110 | ObjectEndPoint* parent; |
| 111 | |
| 112 | // Used to calculate the registered paths on DBus |
| 113 | QRemoteServiceRegister::Entry entry; |
| 114 | QUuid serviceInstanceId; |
| 115 | |
| 116 | // Service side local client ownership list |
| 117 | QList<ClientInstance> clientList; |
| 118 | }; |
| 119 | |
| 120 | /*! |
| 121 | Client to service communication only used for establishing an object request since the returned |
| 122 | proxy is an interface to that object registered on QtDBus. Client communicates directly to QtDBus |
| 123 | for method and property access. Signals are automatically relayed from QtDBus to the proxy object. |
| 124 | */ |
| 125 | ObjectEndPoint::ObjectEndPoint(Type type, QServiceIpcEndPoint* comm, QObject* parent) |
| 126 | : QObject(parent), dispatch(comm), service(0), iface(0) |
| 127 | { |
| 128 | Q_ASSERT(dispatch); |
| 129 | d = new ObjectEndPointPrivate; |
| 130 | d->parent = this; |
| 131 | d->endPointType = type; |
| 132 | |
| 133 | dispatch->setParent(this); |
| 134 | connect(dispatch, SIGNAL(readyRead()), this, SLOT(newPackageReady())); |
| 135 | if (type == Client) { |
| 136 | // client waiting for construct proxy and registers DBus custom type |
| 137 | qDBusRegisterMetaType<QServiceUserTypeDBus>(); |
| 138 | qRegisterMetaType<QServiceUserTypeDBus>(); |
| 139 | return; |
| 140 | } else { |
| 141 | connect(InstanceManager::instance(), |
| 142 | SIGNAL(instanceClosed(QRemoteServiceRegister::Entry,QUuid)), |
| 143 | this, SLOT(unregisterObjectDBus(QRemoteServiceRegister::Entry,QUuid))); |
| 144 | |
| 145 | if (dispatch->packageAvailable()) |
| 146 | QTimer::singleShot(0, this, SLOT(newPackageReady())); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | ObjectEndPoint::~ObjectEndPoint() |
| 151 | { |
| 152 | if (iface) |
| 153 | delete iface; |
| 154 | delete d; |
| 155 | } |
| 156 | |
| 157 | /*! |
| 158 | Removes all instances of the client from the instance manager |
| 159 | */ |
| 160 | void ObjectEndPoint::disconnected(const QString& clientId, const QString& instanceId) |
| 161 | { |
| 162 | // Service Side |
| 163 | Q_ASSERT(d->endPointType != ObjectEndPoint::Client); |
| 164 | |
| 165 | for (int i=d->clientList.size()-1; i>=0; i--) { |
| 166 | // Find right client process |
| 167 | if (d->clientList[i].clientId == clientId) { |
| 168 | if (d->clientList[i].ref-- == 1) { |
| 169 | QRemoteServiceRegister::Entry entry = d->clientList[i].entry; |
| 170 | QUuid instance = d->clientList[i].instanceId; |
| 171 | |
| 172 | if (instance.toString() == instanceId) { |
| 173 | // Remove an instance from the InstanceManager and local list |
| 174 | InstanceManager::instance()->removeObjectInstance(entry, instance); |
| 175 | d->clientList.removeAt(i); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /*! |
| 183 | Unregisters the DBus object |
| 184 | */ |
| 185 | void ObjectEndPoint::unregisterObjectDBus(const QRemoteServiceRegister::Entry& entry, const QUuid& id) |
| 186 | { |
| 187 | uint hash = qHash(id.toString()); |
| 188 | QString objPath = QLatin1Char('/') + entry.interfaceName() + QLatin1Char('/') + entry.version() + |
| 189 | QLatin1Char('/') + QString::number(hash); |
| 190 | objPath.replace(QLatin1Char('.'), QLatin1Char('/')); |
| 191 | QDBusConnection::sessionBus().unregisterObject(objPath, QDBusConnection::UnregisterTree); |
| 192 | } |
| 193 | |
| 194 | /*! |
| 195 | Client requests proxy object. The proxy is owned by calling |
| 196 | code and this object must clean itself up upon destruction of |
| 197 | proxy. |
| 198 | */ |
| 199 | QObject* ObjectEndPoint::constructProxy(const QRemoteServiceRegister::Entry& entry) |
| 200 | { |
| 201 | // Client side |
| 202 | Q_ASSERT(d->endPointType == ObjectEndPoint::Client); |
| 203 | |
| 204 | // Request a serialized meta object |
| 205 | QServicePackage p; |
| 206 | p.d = new QServicePackagePrivate(); |
| 207 | p.d->messageId = QUuid::createUuid(); |
| 208 | p.d->entry = entry; |
| 209 | |
| 210 | Response* response = new Response(); |
| 211 | openRequests()->insert(p.d->messageId, response); |
| 212 | |
| 213 | dispatch->writePackage(p); |
| 214 | waitForResponse(p.d->messageId); |
| 215 | |
| 216 | // Get the proxy based on the meta object |
| 217 | if (response->isFinished) { |
| 218 | if (response->result == 0) |
| 219 | qWarning() << "Request for remote service failed"; |
| 220 | else |
| 221 | service = reinterpret_cast<QServiceProxy* >(response->result); |
| 222 | } else { |
| 223 | qDebug() << "response passed but not finished"; |
| 224 | } |
| 225 | |
| 226 | openRequests()->take(p.d->messageId); |
| 227 | delete response; |
| 228 | |
| 229 | if (!service) |
| 230 | return 0; |
| 231 | |
| 232 | // Connect all DBus interface signals to the proxy slots |
| 233 | const QMetaObject *mo = service->metaObject(); |
| 234 | while (mo && strcmp(mo->className(), "QObject")) { |
| 235 | for (int i = mo->methodOffset(); i < mo->methodCount(); i++) { |
| 236 | const QMetaMethod mm = mo->method(i); |
| 237 | if (mm.methodType() == QMetaMethod::Signal) { |
| 238 | QByteArray sig(mm.methodSignature()); |
| 239 | |
| 240 | bool customType = false; |
| 241 | |
| 242 | QList<QByteArray> params = mm.parameterTypes(); |
| 243 | for (int arg = 0; arg < params.size(); arg++) { |
| 244 | const QByteArray& type = params[arg]; |
| 245 | int variantType = QMetaType::type(type); |
| 246 | if (variantType >= QMetaType::User || variantType == QMetaType::QVariant) { |
| 247 | sig.replace(QByteArray(type), QByteArray("QDBusVariant")); |
| 248 | customType = true; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | int serviceIndex = iface->metaObject()->indexOfSignal(sig); |
| 253 | QByteArray signal = QByteArray("2").append(sig); |
| 254 | |
| 255 | if (serviceIndex > 0) { |
| 256 | if (customType) { |
| 257 | QObject::connect(iface, signal.constData(), signalsObject, signal.constData()); |
| 258 | |
| 259 | ServiceSignalIntercepter *intercept = |
| 260 | new ServiceSignalIntercepter((QObject*)signalsObject, signal, this); |
| 261 | intercept->setMetaIndex(localToRemote[i]); |
| 262 | } else { |
| 263 | QObject::connect(iface, signal.constData(), service, signal.constData()); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | mo = mo->superClass(); |
| 269 | } |
| 270 | |
| 271 | return service; |
| 272 | } |
| 273 | |
| 274 | /*! |
| 275 | Received a new package from the DBus client-server controller. |
| 276 | Once an object request is handled there is only direct communication to the DBus object so |
| 277 | no other package types should be received on this layer. |
| 278 | */ |
| 279 | void ObjectEndPoint::newPackageReady() |
| 280 | { |
| 281 | // Client and service side |
| 282 | while (dispatch->packageAvailable()) |
| 283 | { |
| 284 | // must call get getSecurityCredentials everytime you call nextPackage |
| 285 | QServiceClientCredentials creds; |
| 286 | dispatch->getSecurityCredentials(creds); |
| 287 | QServicePackage p = dispatch->nextPackage(); |
| 288 | if (!p.isValid()) |
| 289 | continue; |
| 290 | |
| 291 | if (p.d->packageType == QServicePackage::ObjectCreation) { |
| 292 | objectRequest(p, creds); |
| 293 | } else { |
| 294 | qWarning() << "Unknown package type received."; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void ObjectEndPoint::setLookupTable(int *local, int *remote) |
| 300 | { |
| 301 | localToRemote = local; |
| 302 | remoteToLocal = remote; |
| 303 | } |
| 304 | |
| 305 | /*! |
| 306 | Service finds existing objects or spawns new object instances and registers them on DBus using a |
| 307 | hash of the unique instance ID. This registered object has a special metaobject representation |
| 308 | of the service that is compatible with the QDBus type system. |
| 309 | |
| 310 | Client receives a package containing the information to connect an interface to the registered |
| 311 | DBus object. |
| 312 | */ |
| 313 | void ObjectEndPoint::objectRequest(const QServicePackage& p, QServiceClientCredentials& creds) |
| 314 | { |
| 315 | if (p.d->responseType != QServicePackage::NotAResponse ) { |
| 316 | // Client side |
| 317 | Q_ASSERT(d->endPointType == ObjectEndPoint::Client); |
| 318 | |
| 319 | d->serviceInstanceId = p.d->instanceId; |
| 320 | d->entry = p.d->entry; |
| 321 | |
| 322 | Response* response = openRequests()->value(p.d->messageId); |
| 323 | if (p.d->responseType == QServicePackage::Failed) { |
| 324 | response->result = 0; |
| 325 | response->isFinished = true; |
| 326 | QTimer::singleShot(0, this, SIGNAL(pendingRequestFinished())); |
| 327 | qWarning() << "Service instantiation failed"; |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | // Deserialize meta object and create proxy object |
| 332 | QServiceProxy* proxy = new QServiceProxy(p.d->payload.toByteArray(), this); |
| 333 | response->result = reinterpret_cast<void *>(proxy); |
| 334 | response->isFinished = true; |
| 335 | |
| 336 | // Create DBUS interface by using a hash of the service instance ID |
| 337 | QString serviceName = QStringLiteral("com.nokia.qtmobility.sfw.") + p.d->entry.serviceName(); |
| 338 | uint hash = qHash(d->serviceInstanceId.toString()); |
| 339 | QString objPath = QLatin1Char('/') + p.d->entry.interfaceName() + QLatin1Char('/') + p.d->entry.version() + QLatin1Char('/') + QString::number(hash); |
| 340 | objPath.replace(QLatin1Char('.'), QLatin1Char('/')); |
| 341 | |
| 342 | #ifdef DEBUG |
| 343 | qDebug() << "Client Interface ObjectPath:" << objPath; |
| 344 | #endif |
| 345 | // Instantiate our DBus interface and its corresponding signals object |
| 346 | if (!iface) |
| 347 | iface = new QDBusInterface(serviceName, objPath, QString(), QDBusConnection::sessionBus(), this); |
| 348 | signalsObject = new QServiceMetaObjectDBus(iface, true); |
| 349 | |
| 350 | // Wake up waiting proxy construction code |
| 351 | QTimer::singleShot(0, this, SIGNAL(pendingRequestFinished())); |
| 352 | |
| 353 | } else { |
| 354 | // Service side |
| 355 | Q_ASSERT(d->endPointType == ObjectEndPoint::Service); |
| 356 | |
| 357 | QServicePackage response = p.createResponse(); |
| 358 | InstanceManager* iManager = InstanceManager::instance(); |
| 359 | |
| 360 | if (!creds.isValid()) { |
| 361 | qWarning() << "SFW Unable to get socket credentials client asking for" << p.d->entry.interfaceName() << p.d->entry.serviceName() << "this may fail in the future"; |
| 362 | } |
| 363 | |
| 364 | // Instantiate service object from type register |
| 365 | service = iManager->createObjectInstance(p.d->entry, d->serviceInstanceId, creds); |
| 366 | if (!service) { |
| 367 | qWarning() << "Cannot instantiate service object"; |
| 368 | dispatch->writePackage(response); |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | if (!creds.isClientAccepted()) { |
| 373 | qWarning() << "SFW Security failure by" << |
| 374 | creds.getProcessIdentifier() << |
| 375 | creds.getUserIdentifier() << |
| 376 | creds.getGroupIdentifier() << |
| 377 | "requesting" << p.d->entry.interfaceName() << p.d->entry.serviceName(); |
| 378 | iManager->removeObjectInstance(p.d->entry, d->serviceInstanceId); |
| 379 | dispatch->writePackage(response); |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | // Start DBus connection and register proxy service |
| 384 | if (!QDBusConnection::sessionBus().isConnected()) { |
| 385 | qWarning() << "Cannot connect to DBus"; |
| 386 | } |
| 387 | |
| 388 | // DBus registration path uses a hash of the service instance ID |
| 389 | QString serviceName = QStringLiteral("com.nokia.qtmobility.sfw.") + p.d->entry.serviceName(); |
| 390 | uint hash = qHash(d->serviceInstanceId.toString()); |
| 391 | QString objPath = QLatin1Char('/') + p.d->entry.interfaceName() + QLatin1Char('/') + p.d->entry.version() + QLatin1Char('/') + QString::number(hash); |
| 392 | objPath.replace(QLatin1Char('.'), QLatin1Char('/')); |
| 393 | |
| 394 | QServiceMetaObjectDBus *serviceDBus = new QServiceMetaObjectDBus(service); |
| 395 | QDBusConnection::sessionBus().registerObject(objPath, serviceDBus, QDBusConnection::ExportAllContents); |
| 396 | |
| 397 | QString clientId = p.d->payload.toString(); |
| 398 | |
| 399 | int exists = 0; |
| 400 | for (int i=d->clientList.size()-1; i>=0; i--) { |
| 401 | // Find right client process |
| 402 | if (d->clientList[i].clientId == clientId) { |
| 403 | d->clientList[i].ref++; |
| 404 | exists = 1; |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if (!exists) { |
| 410 | // Add new instance to client ownership list |
| 411 | ClientInstance c; |
| 412 | c.clientId = clientId; |
| 413 | c.entry = p.d->entry; |
| 414 | c.instanceId = d->serviceInstanceId; |
| 415 | c.ref = 1; |
| 416 | d->clientList << c; |
| 417 | } |
| 418 | |
| 419 | |
| 420 | |
| 421 | #ifdef DEBUG |
| 422 | qDebug() << "Service Interface ObjectPath:" << objPath; |
| 423 | |
| 424 | const QMetaObject *s_meta = service->metaObject(); |
| 425 | qDebug() << "+++++++++++++++++++++SERVICE+++++++++++++++++++++++"; |
| 426 | qDebug() << s_meta->className(); |
| 427 | qDebug() << "METHOD COUNT: " << s_meta->methodCount(); |
| 428 | for (int i=0; i<s_meta->methodCount(); i++) { |
| 429 | QMetaMethod mm = s_meta->method(i); |
| 430 | |
| 431 | QString type; |
| 432 | switch (mm.methodType()) { |
| 433 | case QMetaMethod::Method: |
| 434 | type = "Q_INVOKABLE"; |
| 435 | break; |
| 436 | case QMetaMethod::Signal: |
| 437 | type = "SIGNAL"; |
| 438 | break; |
| 439 | case QMetaMethod::Slot: |
| 440 | type = "SLOT"; |
| 441 | break; |
| 442 | default: |
| 443 | break; |
| 444 | } |
| 445 | |
| 446 | QString returnType = mm.typeName(); |
| 447 | if (returnType == "") returnType = "void"; |
| 448 | |
| 449 | qDebug() << "METHOD" << type << ":" << returnType << mm.methodSignature(); |
| 450 | } |
| 451 | qDebug() << "++++++++++++++++++++++++++++++++++++++++++++++++++++"; |
| 452 | if (!iface) |
| 453 | iface = new QDBusInterface(serviceName, objPath, "", QDBusConnection::sessionBus(), this); |
| 454 | const QMetaObject *i_meta = iface->metaObject(); |
| 455 | qDebug() << "++++++++++++++++++++DBUS SERVICE++++++++++++++++++++"; |
| 456 | qDebug() << i_meta->className(); |
| 457 | qDebug() << "METHOD COUNT: " << i_meta->methodCount(); |
| 458 | for (int i=0; i<i_meta->methodCount(); i++) { |
| 459 | QMetaMethod mm = i_meta->method(i); |
| 460 | |
| 461 | QString type; |
| 462 | switch (mm.methodType()) { |
| 463 | case QMetaMethod::Method: |
| 464 | type = "Q_INVOKABLE"; |
| 465 | break; |
| 466 | case QMetaMethod::Signal: |
| 467 | type = "SIGNAL"; |
| 468 | break; |
| 469 | case QMetaMethod::Slot: |
| 470 | type = "SLOT"; |
| 471 | break; |
| 472 | default: |
| 473 | break; |
| 474 | } |
| 475 | |
| 476 | QString returnType = mm.typeName(); |
| 477 | if (returnType == "") returnType = "void"; |
| 478 | |
| 479 | qDebug() << "METHOD" << type << ":" << returnType << mm.methodSignature(); |
| 480 | } |
| 481 | qDebug() << "+++++++++++++++++++++++++++++++++++++++++++++++++++"; |
| 482 | #endif |
| 483 | |
| 484 | // Get meta object from type register |
| 485 | const QMetaObject* meta = iManager->metaObject(p.d->entry); |
| 486 | if (!meta) { |
| 487 | qDebug() << "Unknown type" << p.d->entry; |
| 488 | dispatch->writePackage(response); |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | // Serialize meta object |
| 493 | QByteArray data; |
| 494 | QDataStream stream( &data, QIODevice::WriteOnly | QIODevice::Append ); |
| 495 | QMetaObjectBuilder builder(meta); |
| 496 | builder.serialize(stream); |
| 497 | |
| 498 | // Send meta object and instance ID to the client for processing |
| 499 | d->entry = p.d->entry; |
| 500 | response.d->instanceId = d->serviceInstanceId; |
| 501 | response.d->entry = p.d->entry; |
| 502 | response.d->responseType = QServicePackage::Success; |
| 503 | response.d->payload = QVariant(data); |
| 504 | dispatch->writePackage(response); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /*! |
| 509 | Returns the created service instance Id |
| 510 | */ |
| 511 | QString ObjectEndPoint::getInstanceId() const |
| 512 | { |
| 513 | Q_ASSERT(d->endPointType == ObjectEndPoint::Client); |
| 514 | |
| 515 | return d->serviceInstanceId.toString(); |
| 516 | } |
| 517 | |
| 518 | /*! |
| 519 | Client side property call that directly accesses properties through the DBus interface. |
| 520 | Read and reset have special hardcoded DBus methods due to the nature of QtDBus properties |
| 521 | without an adaptor class incorrectly forwarding the metacall type |
| 522 | */ |
| 523 | QVariant ObjectEndPoint::invokeRemoteProperty(int metaIndex, const QVariant& arg, int /*returnType*/, QMetaObject::Call c ) |
| 524 | { |
| 525 | Q_ASSERT(d->endPointType == ObjectEndPoint::Client); |
| 526 | |
| 527 | const QMetaObject *imeta = service->metaObject(); |
| 528 | QMetaProperty property = imeta->property(metaIndex); |
| 529 | |
| 530 | if (c == QMetaObject::WriteProperty) { |
| 531 | // Writing property, direct property DBus call |
| 532 | if (!iface->setProperty(property.name(), arg)) { |
| 533 | qWarning() << "Service property write call failed"; |
| 534 | } |
| 535 | |
| 536 | } else if (c == QMetaObject::ResetProperty) { |
| 537 | // Resetting property, direct special method DBus call |
| 538 | QVariantList args; |
| 539 | args << QVariant(QLatin1String(property.name())); |
| 540 | QDBusMessage msg = iface->callWithArgumentList(QDBus::Block, QLatin1String("propertyReset"), args); |
| 541 | if (msg.type() == QDBusMessage::InvalidMessage) { |
| 542 | qWarning() << "Service property reset call failed"; |
| 543 | } |
| 544 | |
| 545 | } else if (c == QMetaObject::ReadProperty) { |
| 546 | // Reading property, direct special method DBus call |
| 547 | QVariantList args; |
| 548 | args << QVariant(QLatin1String(property.name())); |
| 549 | QDBusMessage msg = iface->callWithArgumentList(QDBus::Block, QLatin1String("propertyRead"), args); |
| 550 | if (msg.type() == QDBusMessage::ReplyMessage) { |
| 551 | QVariantList retList = msg.arguments(); |
| 552 | return retList[0]; |
| 553 | } else { |
| 554 | qWarning() << "Service property read call failed" << msg.errorMessage(); |
| 555 | } |
| 556 | } else { |
| 557 | qWarning() << "Invalid property call"; |
| 558 | } |
| 559 | |
| 560 | return QVariant(); |
| 561 | } |
| 562 | |
| 563 | /*! |
| 564 | Client side method call that converts an argument of type to its corresponding value as a |
| 565 | valid type supported by the QtDBus type system. |
| 566 | |
| 567 | Supports conversion from a QVariant, QList, QMap, QHash, and custom user-defined types. |
| 568 | */ |
| 569 | QVariant ObjectEndPoint::toDBusVariant(const QByteArray& typeName, const QVariant& arg) |
| 570 | { |
| 571 | QVariant dbusVariant = arg; |
| 572 | |
| 573 | int type = QMetaType::type(typeName); |
| 574 | if (type == QMetaType::QVariant) { |
| 575 | // Wrap QVariants in a QDBusVariant |
| 576 | QDBusVariant replacement(arg); |
| 577 | dbusVariant = QVariant::fromValue(replacement); |
| 578 | } else if (type >= QMetaType::User) { |
| 579 | // Wrap custom types in a QDBusVariant of the type name and |
| 580 | // a buffer of its variant-wrapped data |
| 581 | QByteArray buffer; |
| 582 | QDataStream stream(&buffer, QIODevice::ReadWrite | QIODevice::Append); |
| 583 | stream << arg; |
| 584 | |
| 585 | QServiceUserTypeDBus customType; |
| 586 | customType.typeName = typeName; |
| 587 | customType.variantBuffer = buffer; |
| 588 | |
| 589 | QDBusVariant replacement(QVariant::fromValue(customType)); |
| 590 | dbusVariant = QVariant::fromValue(replacement); |
| 591 | } |
| 592 | |
| 593 | return dbusVariant; |
| 594 | } |
| 595 | |
| 596 | /*! |
| 597 | Client side method call that directly accesses the object through the DBus interface. |
| 598 | All arguments and return types are processed and converted accordingly so that all functions |
| 599 | satisfy the QtDBus type system. |
| 600 | */ |
| 601 | QVariant ObjectEndPoint::invokeRemote(int metaIndex, const QVariantList& args, int returnType) |
| 602 | { |
| 603 | QMetaMethod method = service->metaObject()->method(remoteToLocal[metaIndex]); |
| 604 | |
| 605 | Q_ASSERT(d->endPointType == ObjectEndPoint::Client); |
| 606 | |
| 607 | // Check is this is a signal relay |
| 608 | if (method.methodType() == QMetaMethod::Signal) { |
| 609 | // Convert custom arguments |
| 610 | QVariantList convertedList; |
| 611 | QList<QByteArray> params = method.parameterTypes(); |
| 612 | for (int i = 0; i < params.size(); i++) { |
| 613 | const QByteArray& type = params[i]; |
| 614 | int variantType = QMetaType::type(type); |
| 615 | if (variantType >= QMetaType::User || variantType == QMetaType::QVariant) { |
| 616 | QDBusVariant dbusVariant = qvariant_cast<QDBusVariant>(args[i]); |
| 617 | QVariant variant = dbusVariant.variant(); |
| 618 | |
| 619 | if (variantType == QMetaType::QVariant) { |
| 620 | convertedList << variant; |
| 621 | } else { |
| 622 | QByteArray buffer = variant.toByteArray(); |
| 623 | QDataStream stream(&buffer, QIODevice::ReadWrite); |
| 624 | QVariant *customType = new QVariant(variantType, (const void*)0); |
| 625 | QMetaType::load(stream, QMetaType::QVariant, customType); |
| 626 | convertedList << *customType; |
| 627 | } |
| 628 | } else { |
| 629 | convertedList << args[i]; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | // Signal relay |
| 634 | const int numArgs = convertedList.size(); |
| 635 | QVarLengthArray<void *, 32> a( numArgs+1 ); |
| 636 | a[0] = 0; |
| 637 | |
| 638 | const QList<QByteArray> pTypes = method.parameterTypes(); |
| 639 | for ( int arg = 0; arg < numArgs; ++arg ) { |
| 640 | if (pTypes.at(arg) == "QVariant") { |
| 641 | a[arg+1] = (void *)&( convertedList[arg] ); |
| 642 | } else { |
| 643 | a[arg+1] = (void *)( convertedList[arg].data() ); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | // Activate the service proxy signal call |
| 648 | QMetaObject::activate(service, remoteToLocal[metaIndex], a.data()); |
| 649 | return QVariant(); |
| 650 | } |
| 651 | |
| 652 | // Method call so process arguments and convert if not a supported DBus type |
| 653 | QVariantList convertedList; |
| 654 | QList<QByteArray> params = method.parameterTypes(); |
| 655 | for (int i = 0; i < params.size(); i++) { |
| 656 | QVariant converted = toDBusVariant(params[i], args[i]); |
| 657 | convertedList << converted; |
| 658 | } |
| 659 | |
| 660 | bool validDBus = false; |
| 661 | QDBusMessage msg; |
| 662 | |
| 663 | // Find the method name and try a direct DBus call |
| 664 | QString methodName(QLatin1String(method.methodSignature().constData())); |
| 665 | methodName.truncate(methodName.indexOf(QLatin1String("("))); |
| 666 | |
| 667 | if (method.methodType() == QMetaMethod::Slot || method.methodType() == QMetaMethod::Method) { |
| 668 | // Slot or Invokable method |
| 669 | msg = iface->callWithArgumentList(QDBus::Block, methodName, convertedList); |
| 670 | if (msg.type() == QDBusMessage::ReplyMessage) { |
| 671 | validDBus = true; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | // DBus call should only fail for methods with invalid type definitions |
| 676 | if (validDBus) { |
| 677 | if (returnType == QMetaType::Void) { |
| 678 | // Void method call |
| 679 | return QVariant(); |
| 680 | } |
| 681 | else { |
| 682 | // Use DBus message return value |
| 683 | QVariantList retList = msg.arguments(); |
| 684 | |
| 685 | // Process return |
| 686 | const QByteArray& retType = QByteArray(method.typeName()); |
| 687 | int variantType = QMetaType::type(retType); |
| 688 | if (variantType == QMetaType::QVariant) { |
| 689 | // QVariant return from QDBusVariant wrapper |
| 690 | QDBusVariant dbusVariant = qvariant_cast<QDBusVariant>(retList[0]); |
| 691 | return dbusVariant.variant(); |
| 692 | } else if (variantType >= QMetaType::User) { |
| 693 | // Custom return type |
| 694 | QDBusVariant dbusVariant = qvariant_cast<QDBusVariant>(retList[0]); |
| 695 | QVariant convert = dbusVariant.variant(); |
| 696 | |
| 697 | QServiceUserTypeDBus customType = qdbus_cast<QServiceUserTypeDBus>(convert); |
| 698 | QByteArray buffer = customType.variantBuffer; |
| 699 | QDataStream stream(&buffer, QIODevice::ReadWrite); |
| 700 | |
| 701 | // Load our buffered variant-wrapped custom return |
| 702 | QVariant *customReturn = new QVariant(variantType, (const void*)0); |
| 703 | QMetaType::load(stream, QMetaType::QVariant, customReturn); |
| 704 | |
| 705 | return QVariant(variantType, customReturn->data()); |
| 706 | } else { |
| 707 | // Standard return type |
| 708 | return retList[0]; |
| 709 | } |
| 710 | } |
| 711 | } else { |
| 712 | qWarning( "%s::%s cannot be called.", service->metaObject()->className(), method.methodSignature().constData()); |
| 713 | } |
| 714 | |
| 715 | return QVariant(); |
| 716 | } |
| 717 | |
| 718 | /*! |
| 719 | Client side waits for service side requested |
| 720 | */ |
| 721 | void ObjectEndPoint::waitForResponse(const QUuid& requestId) |
| 722 | { |
| 723 | Q_ASSERT(d->endPointType == ObjectEndPoint::Client); |
| 724 | |
| 725 | if (openRequests()->contains(requestId) ) { |
| 726 | Response* response = openRequests()->value(requestId); |
| 727 | QEventLoop* loop = new QEventLoop( this ); |
| 728 | connect(this, SIGNAL(pendingRequestFinished()), loop, SLOT(quit())); |
| 729 | |
| 730 | while (!response->isFinished) { |
| 731 | loop->exec(); |
| 732 | } |
| 733 | |
| 734 | delete loop; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | #include "moc_objectendpoint_dbus_p.cpp" |
| 739 | |
| 740 | QT_END_NAMESPACE |