| 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 "proxyobject_p.h" |
| 41 |
#include <private/qmetaobjectbuilder_p.h> |
| 42 |
#include "qremoteserviceregisterentry_p.h" |
| 43 |
#include "qservicedebuglog_p.h" |
| 44 |
|
| 45 |
#include <qmetaobject.h> |
| 46 |
#include <qtimer.h> |
| 47 |
#include <qcoreevent.h> |
| 48 |
|
| 49 |
#include <QDebug> |
| 50 |
#include <QCoreApplication> |
| 51 |
|
| 52 |
#include <stdlib.h> |
| 53 |
|
| 54 |
QT_BEGIN_NAMESPACE |
| 55 |
|
| 56 |
class QServiceProxyPrivate |
| 57 |
{ |
| 58 |
public: |
| 59 |
QByteArray metadata; |
| 60 |
QMetaObject* meta; |
| 61 |
ObjectEndPoint* endPoint; |
| 62 |
int *localToRemote; |
| 63 |
int *remoteToLocal; |
| 64 |
|
| 65 |
}; |
| 66 |
|
| 67 |
QServiceProxy::QServiceProxy(const QByteArray& metadata, ObjectEndPoint* endPoint, QObject* parent) |
| 68 |
: QServiceProxyBase(endPoint, parent) |
| 69 |
{ |
| 70 |
Q_ASSERT(endPoint); |
| 71 |
d = new QServiceProxyPrivate(); |
| 72 |
d->metadata = metadata; |
| 73 |
d->meta = 0; |
| 74 |
d->endPoint = endPoint; |
| 75 |
d->localToRemote = 0; |
| 76 |
d->remoteToLocal = 0; |
| 77 |
|
| 78 |
QDataStream stream(d->metadata); |
| 79 |
QMetaObjectBuilder builder; |
| 80 |
QMap<QByteArray, const QMetaObject*> refs; |
| 81 |
|
| 82 |
builder.deserialize(stream, refs); |
| 83 |
if (stream.status() != QDataStream::Ok) { |
| 84 |
qWarning() << "Invalid metaObject for service received"; |
| 85 |
} else { |
| 86 |
QMetaObject *remote = builder.toMetaObject(); |
| 87 |
|
| 88 |
builder.setSuperClass(QServiceProxyBase::metaObject()); |
| 89 |
|
| 90 |
QMetaObject *local = builder.toMetaObject(); |
| 91 |
|
| 92 |
d->remoteToLocal = new int[local->methodCount()]; |
| 93 |
d->localToRemote = new int[local->methodCount()]; |
| 94 |
|
| 95 |
for (int i = 0; i < local->methodCount(); i++){ |
| 96 |
const QMetaMethod m = local->method(i); |
| 97 |
int r = remote->indexOfMethod(m.methodSignature().constData()); |
| 98 |
d->localToRemote[i] = r; |
| 99 |
if (r > 0) |
| 100 |
d->remoteToLocal[r] = i; |
| 101 |
} |
| 102 |
|
| 103 |
#if defined(QT_SFW_IPC_DEBUG) && defined(QT_SFW_IPC_DEBUG_VERBOSE) |
| 104 |
QString mapping = QString::fromLatin1("%%% QWE Doing lookup table for ") + endPoint->objectName(); |
| 105 |
for (int i = 0; i < local->methodCount(); i++){ |
| 106 |
const QMetaMethod m = local->method(i); |
| 107 |
int r = d->localToRemote[i]; |
| 108 |
mapping.append(QString::fromLatin1("\n%%%Mapping %1 from %2 to %3").arg(QString::fromLatin1(m.signature())).arg(i).arg(r)); |
| 109 |
} |
| 110 |
QServiceDebugLog::instance()->appendToLog(mapping); |
| 111 |
#endif |
| 112 |
|
| 113 |
d->meta = local; |
| 114 |
|
| 115 |
endPoint->setLookupTable(d->localToRemote, d->remoteToLocal); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
QServiceProxy::~QServiceProxy() |
| 120 |
{ |
| 121 |
qServiceLog() << "event" << "delete" |
| 122 |
<< "class" << "QServiceProxy" |
| 123 |
<< "name" << objectName(); |
| 124 |
|
| 125 |
delete[] d->remoteToLocal; |
| 126 |
delete[] d->localToRemote; |
| 127 |
if (d->meta) |
| 128 |
free(d->meta); |
| 129 |
delete d; |
| 130 |
} |
| 131 |
|
| 132 |
//provide custom Q_OBJECT implementation |
| 133 |
const QMetaObject* QServiceProxy::metaObject() const |
| 134 |
{ |
| 135 |
return d->meta; |
| 136 |
} |
| 137 |
|
| 138 |
int QServiceProxy::qt_metacall(QMetaObject::Call c, int id, void **a) |
| 139 |
{ |
| 140 |
id = QServiceProxyBase::qt_metacall(c, id, a); |
| 141 |
if (id < 0 || !d->meta) |
| 142 |
return id; |
| 143 |
|
| 144 |
if (c == QMetaObject::InvokeMetaMethod) { |
| 145 |
|
| 146 |
const int mcount = d->meta->methodCount() - d->meta->methodOffset(); |
| 147 |
const int metaIndex = id + d->meta->methodOffset(); |
| 148 |
|
| 149 |
QMetaMethod method = d->meta->method(metaIndex); |
| 150 |
|
| 151 |
const int returnType = method.returnType(); |
| 152 |
|
| 153 |
//process arguments |
| 154 |
const QList<QByteArray> pTypes = method.parameterTypes(); |
| 155 |
const int pTypesCount = pTypes.count(); |
| 156 |
QVariantList args ; |
| 157 |
if (pTypesCount > 10) { |
| 158 |
qWarning() << "Cannot call" << method.methodSignature() << ". More than 10 parameter."; |
| 159 |
return id; |
| 160 |
} |
| 161 |
for (int i=0; i < pTypesCount; i++) { |
| 162 |
const QByteArray& t = pTypes[i]; |
| 163 |
|
| 164 |
int variantType = QMetaType::type(t); |
| 165 |
|
| 166 |
if (variantType == QMetaType::QVariant) { //ignore whether QVariant is declared as metatype |
| 167 |
args << *reinterpret_cast<const QVariant(*)>(a[i+1]); |
| 168 |
} else if ( variantType == 0 ){ |
| 169 |
qWarning("%s: argument %s has unknown type. Use qRegisterMetaType to register it.", |
| 170 |
method.methodSignature().constData(), t.data()); |
| 171 |
return id; |
| 172 |
} else { |
| 173 |
args << QVariant(variantType, a[i+1]); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
if (returnType == QMetaType::Void) { |
| 178 |
|
| 179 |
qServiceLog() << "event" << "nonblocking void call" |
| 180 |
<< "method" << QString::fromLatin1(method.methodSignature()) |
| 181 |
<< "endpoint" << d->endPoint->objectName(); |
| 182 |
|
| 183 |
d->endPoint->invokeRemote(d->localToRemote[metaIndex], args, returnType); |
| 184 |
} else { |
| 185 |
//TODO: invokeRemote() parameter list needs review |
| 186 |
|
| 187 |
qServiceLog() << "event" << "nonblocking call" |
| 188 |
<< "method" << QString::fromLatin1(method.methodSignature()) |
| 189 |
<< "endpoint" << d->endPoint->objectName(); |
| 190 |
|
| 191 |
QVariant result = d->endPoint->invokeRemote(d->localToRemote[metaIndex], args, returnType); |
| 192 |
if (result.type() != QVariant::Invalid){ |
| 193 |
if (returnType != QMetaType::QVariant) { |
| 194 |
QByteArray buffer; |
| 195 |
QDataStream stream(&buffer, QIODevice::ReadWrite); |
| 196 |
QMetaType::save(stream, returnType, result.constData()); |
| 197 |
stream.device()->seek(0); |
| 198 |
QMetaType::load(stream, returnType, a[0]); |
| 199 |
} else { |
| 200 |
if (a[0]) *reinterpret_cast< QVariant*>(a[0]) = result; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
id-=mcount; |
| 205 |
} else if ( c == QMetaObject::ReadProperty |
| 206 |
|| c == QMetaObject::WriteProperty |
| 207 |
|| c == QMetaObject::ResetProperty ) { |
| 208 |
const int pCount = d->meta->propertyCount() - d->meta->propertyOffset(); |
| 209 |
const int metaIndex = id + d->meta->propertyOffset(); |
| 210 |
QMetaProperty property = d->meta->property(metaIndex); |
| 211 |
if (property.isValid()) { |
| 212 |
int pType = property.userType(); |
| 213 |
|
| 214 |
QVariant arg; |
| 215 |
if ( c == QMetaObject::WriteProperty ) { |
| 216 |
|
| 217 |
qServiceLog() << "event" << "property write" |
| 218 |
<< "property" << property.name() |
| 219 |
<< "endpoint" << d->endPoint->objectName(); |
| 220 |
|
| 221 |
if (pType == QMetaType::QVariant) |
| 222 |
arg = *reinterpret_cast<const QVariant(*)>(a[0]); |
| 223 |
else if (pType == 0) { |
| 224 |
qWarning("%s: property %s has unkown type", property.name(), property.typeName()); |
| 225 |
return id; |
| 226 |
} else { |
| 227 |
arg = QVariant(pType, a[0]); |
| 228 |
} |
| 229 |
} |
| 230 |
QVariant result; |
| 231 |
if (c == QMetaObject::ReadProperty) { |
| 232 |
|
| 233 |
qServiceLog() << "event" << "property read" |
| 234 |
<< "property" << property.name() |
| 235 |
<< "endpoint" << d->endPoint->objectName(); |
| 236 |
|
| 237 |
result = d->endPoint->invokeRemoteProperty(metaIndex, arg, pType, c); |
| 238 |
//wrap result for client |
| 239 |
if (pType != 0) { |
| 240 |
QByteArray buffer; |
| 241 |
QDataStream stream(&buffer, QIODevice::ReadWrite); |
| 242 |
QMetaType::save(stream, pType, result.constData()); |
| 243 |
stream.device()->seek(0); |
| 244 |
QMetaType::load(stream, pType, a[0]); |
| 245 |
} else { |
| 246 |
if (a[0]) *reinterpret_cast< QVariant*>(a[0]) = result; |
| 247 |
} |
| 248 |
} else { |
| 249 |
d->endPoint->invokeRemoteProperty(metaIndex, arg, pType, c); |
| 250 |
} |
| 251 |
} |
| 252 |
id-=pCount; |
| 253 |
} else if ( c == QMetaObject::QueryPropertyDesignable |
| 254 |
|| c == QMetaObject::QueryPropertyScriptable |
| 255 |
|| c == QMetaObject::QueryPropertyStored |
| 256 |
|| c == QMetaObject::QueryPropertyEditable |
| 257 |
|| c == QMetaObject::QueryPropertyUser ) |
| 258 |
{ |
| 259 |
//Nothing to do? |
| 260 |
//These values are part of the transferred meta object already |
| 261 |
} else { |
| 262 |
//TODO |
| 263 |
qWarning() << "MetaCall type" << c << "not yet handled"; |
| 264 |
} |
| 265 |
return id; |
| 266 |
} |
| 267 |
|
| 268 |
void *QServiceProxy::qt_metacast(const char* className) |
| 269 |
{ |
| 270 |
if (!className) return 0; |
| 271 |
//this object should not be castable to anything but it's super type |
| 272 |
return QServiceProxyBase::qt_metacast(className); |
| 273 |
} |
| 274 |
|
| 275 |
class QServiceProxyBasePrivate |
| 276 |
{ |
| 277 |
public: |
| 278 |
QServiceProxy *p; |
| 279 |
QMetaObject* meta; |
| 280 |
ObjectEndPoint* endPoint; |
| 281 |
int ipcfailure; |
| 282 |
QMetaMethod ipcFailureSignal; |
| 283 |
int timerId; |
| 284 |
}; |
| 285 |
|
| 286 |
QServiceProxyBase::QServiceProxyBase(ObjectEndPoint *endpoint, QObject *parent) |
| 287 |
: QObject(parent), d(0) |
| 288 |
{ |
| 289 |
|
| 290 |
d = new QServiceProxyBasePrivate(); |
| 291 |
d->meta = 0; |
| 292 |
d->endPoint = endpoint; |
| 293 |
d->ipcfailure = -1; |
| 294 |
d->timerId = startTimer(1000); |
| 295 |
|
| 296 |
QMetaObjectBuilder sup; |
| 297 |
sup.setClassName("QServiceProxyBase"); |
| 298 |
QMetaMethodBuilder b = sup.addSignal("errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)"); |
| 299 |
d->ipcfailure = b.index(); |
| 300 |
d->meta = sup.toMetaObject(); |
| 301 |
d->ipcFailureSignal = d->meta->method(d->meta->methodOffset()); |
| 302 |
Q_ASSERT(d->ipcFailureSignal.methodSignature() == "errorUnrecoverableIPCFault(QService::UnrecoverableIPCError)"); |
| 303 |
} |
| 304 |
|
| 305 |
QServiceProxyBase::~QServiceProxyBase() |
| 306 |
{ |
| 307 |
qServiceLog() << "event" << "delete" |
| 308 |
<< "class" << "QServiceProxyBase" |
| 309 |
<< "name" << objectName(); |
| 310 |
|
| 311 |
if (d->meta) |
| 312 |
free(d->meta); |
| 313 |
delete d; |
| 314 |
} |
| 315 |
|
| 316 |
void QServiceProxyBase::connectNotify(const QMetaMethod &signal) |
| 317 |
{ |
| 318 |
if (d->timerId > 0 && signal == d->ipcFailureSignal) { |
| 319 |
killTimer(d->timerId); |
| 320 |
d->timerId = -1; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
void QServiceProxyBase::timerEvent(QTimerEvent *e) |
| 325 |
{ |
| 326 |
if (e->timerId() == d->timerId) { |
| 327 |
qWarning() << this << "Someone is using SFW incorrectly. No one connected to errorUnrecoverableIPCFault for class" << this->metaObject()->className() << "in" << qApp->applicationFilePath(); |
| 328 |
killTimer(d->timerId); |
| 329 |
d->timerId = -1; |
| 330 |
} else { |
| 331 |
QObject::timerEvent(e); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
//provide custom Q_OBJECT implementation |
| 336 |
const QMetaObject* QServiceProxyBase::metaObject() const |
| 337 |
{ |
| 338 |
return d->meta; |
| 339 |
} |
| 340 |
|
| 341 |
void *QServiceProxyBase::qt_metacast(const char* className) |
| 342 |
{ |
| 343 |
if (!className) return 0; |
| 344 |
//this object should not be castable to anything but QObject |
| 345 |
return QObject::qt_metacast(className); |
| 346 |
} |
| 347 |
|
| 348 |
int QServiceProxyBase::qt_metacall(QMetaObject::Call c, int id, void **a) |
| 349 |
{ |
| 350 |
id = QObject::qt_metacall(c, id, a); |
| 351 |
if (id < 0 || !d->meta) |
| 352 |
return id; |
| 353 |
|
| 354 |
if (c == QMetaObject::InvokeMetaMethod) { |
| 355 |
// ipcfailure is the local offset, so is id |
| 356 |
const int mcount = d->meta->methodCount() - d->meta->methodOffset(); |
| 357 |
int oldid = id; |
| 358 |
id-=mcount; |
| 359 |
if (oldid == d->ipcfailure) { |
| 360 |
QMetaObject::activate(this, d->meta, d->ipcfailure, a); |
| 361 |
} |
| 362 |
} |
| 363 |
return id; |
| 364 |
} |
| 365 |
|
| 366 |
QT_END_NAMESPACE |