1 | // Copyright (C) 2017 Ford Motor Company |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qremoteobjectsource.h" |
5 | #include "qremoteobjectsource_p.h" |
6 | #include "qremoteobjectnode.h" |
7 | #include "qremoteobjectdynamicreplica.h" |
8 | |
9 | #include "qconnectionfactories_p.h" |
10 | #include "qremoteobjectsourceio_p.h" |
11 | #include "qremoteobjectabstractitemmodeladapter_p.h" |
12 | |
13 | #include <QtCore/qmetaobject.h> |
14 | #include <QtCore/qvarlengtharray.h> |
15 | #include <QtCore/qabstractitemmodel.h> |
16 | |
17 | #include <algorithm> |
18 | #include <iterator> |
19 | |
20 | QT_BEGIN_NAMESPACE |
21 | |
22 | using namespace QRemoteObjectPackets; |
23 | using namespace QRemoteObjectStringLiterals; |
24 | |
25 | const int QRemoteObjectSourceBase::qobjectPropertyOffset = QObject::staticMetaObject.propertyCount(); |
26 | const int QRemoteObjectSourceBase::qobjectMethodOffset = QObject::staticMetaObject.methodCount(); |
27 | static const QByteArray s_classinfoRemoteobjectSignature(QCLASSINFO_REMOTEOBJECT_SIGNATURE); |
28 | |
29 | namespace QtPrivate { |
30 | |
31 | // The stringData, methodMatch and QMetaObjectPrivate methods are modified versions of the code |
32 | // from qmetaobject_p.h/qmetaobject.cpp. The modifications are based on our custom need to match |
33 | // a method name that comes from the .rep file. |
34 | // The QMetaObjectPrivate struct should only have members appended to maintain binary compatibility, |
35 | // so we should be fine with only the listed version with the fields we use. |
36 | inline const QByteArray apiStringData(const QMetaObject *mo, int index) |
37 | { |
38 | uint offset = mo->d.stringdata[2*index]; |
39 | uint length = mo->d.stringdata[2*index + 1]; |
40 | const char *string = reinterpret_cast<const char *>(mo->d.stringdata) + offset; |
41 | return QByteArray::fromRawData(data: string, size: length); |
42 | } |
43 | |
44 | |
45 | // From QMetaMethod in qmetaobject.h |
46 | struct Data { |
47 | enum { Size = 6 }; |
48 | |
49 | uint name() const { return d[0]; } |
50 | uint argc() const { return d[1]; } |
51 | uint parameters() const { return d[2]; } |
52 | uint tag() const { return d[3]; } |
53 | uint flags() const { return d[4]; } |
54 | uint metaTypeOffset() const { return d[5]; } |
55 | bool operator==(const Data &other) const { return d == other.d; } |
56 | |
57 | const uint *d; |
58 | }; |
59 | |
60 | inline bool apiMethodMatch(const QMetaObject *m, const Data &data, const QByteArray &name, int argc, |
61 | const int *types) |
62 | { |
63 | if (data.argc() != uint(argc)) |
64 | return false; |
65 | if (apiStringData(mo: m, index: data.name()) != name) |
66 | return false; |
67 | for (int i = 0; i < argc; ++i) { |
68 | auto mt = QMetaType(m->d.metaTypes[data.metaTypeOffset() + i + 1]); |
69 | if (mt != QMetaType(types[i])) |
70 | return false; |
71 | } |
72 | return true; |
73 | } |
74 | |
75 | struct QMetaObjectPrivate |
76 | { |
77 | // revision 7 is Qt 5.0 everything lower is not supported |
78 | // revision 8 is Qt 5.12: It adds the enum name to QMetaEnum |
79 | enum { OutputRevision = 8 }; // Used by moc, qmetaobjectbuilder and qdbus |
80 | |
81 | int revision; |
82 | int className; |
83 | int classInfoCount, classInfoData; |
84 | int methodCount, methodData; |
85 | int propertyCount, propertyData; |
86 | int enumeratorCount, enumeratorData; |
87 | int constructorCount, constructorData; |
88 | int flags; |
89 | int signalCount; |
90 | }; |
91 | |
92 | inline Data fromRelativeMethodIndex(const QMetaObject *mobj, int index) |
93 | { |
94 | const auto priv = reinterpret_cast<const QMetaObjectPrivate*>(mobj->d.data); |
95 | return { .d: mobj->d.data + priv->methodData + index * Data::Size }; |
96 | } |
97 | |
98 | int qtro_method_index_impl(const QMetaObject * staticMetaObj, const char *className, |
99 | const char *methodName, int *count, int const **types) |
100 | { |
101 | int result = staticMetaObj->indexOfMethod(method: methodName); |
102 | if (result >= 0) |
103 | return result; |
104 | // We can have issues, specifically with enums, since the compiler can infer the class. Since |
105 | // indexOfMethod() is doing string comparisons for registered types, "MyEnum" and "MyClass::MyEnum" |
106 | // won't match. |
107 | // Below is similar to QMetaObject->indexOfMethod, but template magic has already matched parameter |
108 | // types, so we need to find a match for the API method name + parameters. Neither approach works |
109 | // 100%, as the below code doesn't match a parameter of type "size_t" (which the template match |
110 | // identifies as "ulong"). These subtleties can cause the below string comparison fails. |
111 | // There is no known case that would fail both methods. |
112 | // TODO: is there a way to make this a constexpr so a failure is detected at compile time? |
113 | int nameLength = strchr(s: methodName, c: '(') - methodName; |
114 | const auto name = QByteArray::fromRawData(data: methodName, size: nameLength); |
115 | for (const QMetaObject *m = staticMetaObj; m; m = m->d.superdata) { |
116 | const auto priv = reinterpret_cast<const QMetaObjectPrivate*>(m->d.data); |
117 | int i = (priv->methodCount - 1); |
118 | const int end = priv->signalCount; |
119 | for (; i >= end; --i) { |
120 | const Data data = fromRelativeMethodIndex(mobj: m, index: i); |
121 | if (apiMethodMatch(m, data, name, argc: *count, types: *types)) |
122 | return i + m->methodOffset(); |
123 | } |
124 | } |
125 | qWarning() << "No matching method for" << methodName << "in the provided metaclass" << className; |
126 | return -1; |
127 | } |
128 | |
129 | } // namespace QtPrivate |
130 | |
131 | QByteArray QtPrivate::qtro_classinfo_signature(const QMetaObject *metaObject) |
132 | { |
133 | if (!metaObject) |
134 | return QByteArray{}; |
135 | |
136 | for (int i = metaObject->classInfoOffset(); i < metaObject->classInfoCount(); ++i) { |
137 | auto ci = metaObject->classInfo(index: i); |
138 | if (s_classinfoRemoteobjectSignature == ci.name()) |
139 | return ci.value(); |
140 | } |
141 | return QByteArray{}; |
142 | } |
143 | |
144 | inline bool qtro_is_cloned_method(const QMetaObject *mobj, int index) |
145 | { |
146 | int local_method_index = index - mobj->methodOffset(); |
147 | if (local_method_index < 0 && mobj->superClass()) |
148 | return qtro_is_cloned_method(mobj: mobj->superClass(), index); |
149 | const QtPrivate::Data data = QtPrivate::fromRelativeMethodIndex(mobj, index: local_method_index); |
150 | if (data.flags() & 0x20 /*MethodFlags::MethodCloned*/) |
151 | return true; |
152 | return false; |
153 | } |
154 | |
155 | SourceApiMap::~SourceApiMap() |
156 | = default; |
157 | |
158 | QRemoteObjectSourceBase::QRemoteObjectSourceBase(QObject *obj, Private *d, const SourceApiMap *api, |
159 | QObject *adapter) |
160 | : QObject(obj), |
161 | m_object(obj), |
162 | m_adapter(adapter), |
163 | m_api(api), |
164 | d(d) |
165 | { |
166 | if (!obj) { |
167 | qCWarning(QT_REMOTEOBJECT) << "QRemoteObjectSourceBase: Cannot replicate a NULL object" << m_api->name(); |
168 | return; |
169 | } |
170 | |
171 | setConnections(); |
172 | |
173 | const auto nChildren = api->m_models.size() + api->m_subclasses.size(); |
174 | if (nChildren > 0) { |
175 | QList<int> roles; |
176 | const int numProperties = api->propertyCount(); |
177 | int modelIndex = 0, subclassIndex = 0; |
178 | for (int i = 0; i < numProperties; ++i) { |
179 | if (api->isAdapterProperty(i)) |
180 | continue; |
181 | const int index = api->sourcePropertyIndex(index: i); |
182 | const auto property = m_object->metaObject()->property(index); |
183 | const auto metaType = property.metaType(); |
184 | if (metaType.flags().testFlag(flag: QMetaType::PointerToQObject)) { |
185 | auto propertyMeta = metaType.metaObject(); |
186 | QObject *child = property.read(obj: m_object).value<QObject *>(); |
187 | const QMetaObject *meta = child ? child->metaObject() : propertyMeta; |
188 | if (!meta) |
189 | continue; |
190 | if (meta->inherits(metaObject: &QAbstractItemModel::staticMetaObject)) { |
191 | const auto modelInfo = api->m_models.at(i: modelIndex++); |
192 | QAbstractItemModel *model = qobject_cast<QAbstractItemModel *>(object: child); |
193 | QAbstractItemAdapterSourceAPI<QAbstractItemModel, QAbstractItemModelSourceAdapter> *modelApi = |
194 | new QAbstractItemAdapterSourceAPI<QAbstractItemModel, QAbstractItemModelSourceAdapter>(modelInfo.name); |
195 | if (!model) |
196 | m_children.insert(key: i, value: new QRemoteObjectSource(nullptr, d, modelApi, nullptr, api->name())); |
197 | else { |
198 | roles.clear(); |
199 | const auto knownRoles = model->roleNames(); |
200 | for (auto role : modelInfo.roles.split(sep: '|')) { |
201 | if (role.isEmpty()) |
202 | continue; |
203 | const int roleIndex = knownRoles.key(value: role, defaultKey: -1); |
204 | if (roleIndex == -1) { |
205 | qCWarning(QT_REMOTEOBJECT) << "Invalid role" << role << "for model" << model->metaObject()->className(); |
206 | qCWarning(QT_REMOTEOBJECT) << " known roles:" << knownRoles; |
207 | } else |
208 | roles << roleIndex; |
209 | } |
210 | auto adapter = new QAbstractItemModelSourceAdapter(model, nullptr, |
211 | roles.isEmpty() ? knownRoles.keys().toVector() : roles); |
212 | m_children.insert(key: i, value: new QRemoteObjectSource(model, d, modelApi, adapter, api->name())); |
213 | } |
214 | } else { |
215 | const auto classApi = api->m_subclasses.at(i: subclassIndex++); |
216 | m_children.insert(key: i, value: new QRemoteObjectSource(child, d, classApi, nullptr, api->name())); |
217 | } |
218 | } |
219 | } |
220 | } |
221 | } |
222 | |
223 | QRemoteObjectSource::QRemoteObjectSource(QObject *obj, Private *dd, const SourceApiMap *api, QObject *adapter, const QString &parentName) |
224 | : QRemoteObjectSourceBase(obj, dd, api, adapter) |
225 | , m_name(api->typeName() == QLatin1String("QAbstractItemModelAdapter" ) ? MODEL().arg(a: parentName + QLatin1String("::" ) + api->name()) : |
226 | CLASS().arg(a: parentName + QLatin1String("::" ) + api->name())) |
227 | { |
228 | if (obj) |
229 | d->m_sourceIo->registerSource(source: this); |
230 | } |
231 | |
232 | QRemoteObjectRootSource::QRemoteObjectRootSource(QObject *obj, const SourceApiMap *api, |
233 | QObject *adapter, QRemoteObjectSourceIo *sourceIo) |
234 | : QRemoteObjectSourceBase(obj, new Private(sourceIo, this), api, adapter) |
235 | , m_name(api->name()) |
236 | { |
237 | d->m_sourceIo->registerSource(source: this); |
238 | } |
239 | |
240 | QRemoteObjectSourceBase::~QRemoteObjectSourceBase() |
241 | { |
242 | delete m_api; |
243 | } |
244 | |
245 | void QRemoteObjectSourceBase::setConnections() |
246 | { |
247 | const QMetaObject *meta = m_object->metaObject(); |
248 | |
249 | const int index = meta->indexOfClassInfo(QCLASSINFO_REMOTEOBJECT_TYPE); |
250 | if (index != -1) { //We have an object created from repc or at least with QCLASSINFO defined |
251 | while (true) { |
252 | Q_ASSERT(meta->superClass()); //This recurses to QObject, which doesn't have QCLASSINFO_REMOTEOBJECT_TYPE |
253 | if (index != meta->superClass()->indexOfClassInfo(QCLASSINFO_REMOTEOBJECT_TYPE)) //At the point we don't find the same QCLASSINFO_REMOTEOBJECT_TYPE, |
254 | //we have the metaobject we should work from |
255 | break; |
256 | meta = meta->superClass(); |
257 | } |
258 | } |
259 | |
260 | for (int idx = 0; idx < m_api->signalCount(); ++idx) { |
261 | const int sourceIndex = m_api->sourceSignalIndex(index: idx); |
262 | const bool isAdapter = m_api->isAdapterSignal(idx); |
263 | const auto targetMeta = isAdapter ? m_adapter->metaObject() : meta; |
264 | |
265 | // don't connect cloned signals, or we end up with multiple emissions |
266 | if (qtro_is_cloned_method(mobj: targetMeta, index: sourceIndex)) |
267 | continue; |
268 | // This basically connects the parent Signals (note, all dynamic properties have onChange |
269 | //notifications, thus signals) to us. Normally each Signal is mapped to a unique index, |
270 | //but since we are forwarding them all, we keep the offset constant. |
271 | // |
272 | //We know no one will inherit from this class, so no need to worry about indices from |
273 | //derived classes. |
274 | const auto target = isAdapter ? m_adapter : m_object; |
275 | if (!QMetaObject::connect(sender: target, signal_index: sourceIndex, receiver: this, method_index: QRemoteObjectSource::qobjectMethodOffset+idx, type: Qt::DirectConnection, types: nullptr)) { |
276 | qCWarning(QT_REMOTEOBJECT) << "QRemoteObjectSourceBase: QMetaObject::connect returned false. Unable to connect." ; |
277 | return; |
278 | } |
279 | |
280 | qCDebug(QT_REMOTEOBJECT) << "Connection made" << idx << sourceIndex |
281 | << targetMeta->method(index: sourceIndex).name(); |
282 | } |
283 | } |
284 | |
285 | void QRemoteObjectSourceBase::resetObject(QObject *newObject) |
286 | { |
287 | if (m_object) |
288 | m_object->disconnect(receiver: this); |
289 | if (m_adapter) { |
290 | m_adapter->disconnect(receiver: this); |
291 | delete m_adapter; |
292 | m_adapter = nullptr; |
293 | } |
294 | // We need some dynamic replica specific code here, in case an object had null sub-classes that |
295 | // have been replaced with real objects. In this case, the ApiMap could be wrong and need updating. |
296 | if (newObject && qobject_cast<QRemoteObjectDynamicReplica *>(object: newObject) && m_api->isDynamic()) { |
297 | auto api = static_cast<const DynamicApiMap*>(m_api); |
298 | if (api->m_properties[0] == 0) { // 0 is an index into QObject itself, so this isn't a valid QtRO index |
299 | const auto rep = qobject_cast<QRemoteObjectDynamicReplica *>(object: newObject); |
300 | auto tmp = m_api; |
301 | m_api = new DynamicApiMap(newObject, rep->metaObject(), api->m_name, QLatin1String(rep->metaObject()->className())); |
302 | qCDebug(QT_REMOTEOBJECT) << " Reset m_api for" << api->m_name << "using new metaObject:" << rep->metaObject()->className(); |
303 | delete tmp; |
304 | } |
305 | } |
306 | |
307 | m_object = newObject; |
308 | auto model = qobject_cast<QAbstractItemModel *>(object: newObject); |
309 | if (model) { |
310 | d->m_sourceIo->registerSource(source: this); |
311 | m_adapter = new QAbstractItemModelSourceAdapter(model, nullptr, model->roleNames().keys().toVector()); |
312 | } |
313 | |
314 | setParent(newObject); |
315 | if (newObject) |
316 | setConnections(); |
317 | |
318 | const auto nChildren = m_api->m_models.size() + m_api->m_subclasses.size(); |
319 | if (nChildren == 0) |
320 | return; |
321 | |
322 | if (!newObject) { |
323 | for (auto child : m_children) |
324 | child->resetObject(newObject: nullptr); |
325 | return; |
326 | } |
327 | |
328 | for (int i : m_children.keys()) { |
329 | const int index = m_api->sourcePropertyIndex(index: i); |
330 | const auto property = m_object->metaObject()->property(index); |
331 | QObject *child = property.read(obj: m_object).value<QObject *>(); |
332 | m_children[i]->resetObject(newObject: child); |
333 | } |
334 | } |
335 | |
336 | QRemoteObjectSource::~QRemoteObjectSource() |
337 | { |
338 | for (auto it : m_children) { |
339 | // We used QPointers for m_children because we don't control the lifetime of child QObjects |
340 | // Since the this/source QObject's parent is the referenced QObject, it could have already |
341 | // been deleted |
342 | delete it; |
343 | } |
344 | } |
345 | |
346 | QRemoteObjectRootSource::~QRemoteObjectRootSource() |
347 | { |
348 | for (auto it : m_children) { |
349 | // We used QPointers for m_children because we don't control the lifetime of child QObjects |
350 | // Since the this/source QObject's parent is the referenced QObject, it could have already |
351 | // been deleted |
352 | delete it; |
353 | } |
354 | d->m_sourceIo->unregisterSource(source: this); |
355 | // removeListener tries to modify d->m_listeners, this is O(N²), |
356 | // so clear d->m_listeners prior to calling unregister (consume loop). |
357 | // We can do this, because we don't care about the return value of removeListener() here. |
358 | for (QtROIoDeviceBase *io : std::exchange(obj&: d->m_listeners, new_val: {})) { |
359 | removeListener(io, shouldSendRemove: true); |
360 | } |
361 | delete d; |
362 | } |
363 | |
364 | QVariantList* QRemoteObjectSourceBase::marshalArgs(int index, void **a) |
365 | { |
366 | QVariantList &list = m_marshalledArgs; |
367 | int N = m_api->signalParameterCount(index); |
368 | if (N == 1 && QMetaType(m_api->signalParameterType(sigIndex: index, paramIndex: 0)).flags().testFlag(flag: QMetaType::PointerToQObject)) |
369 | N = 0; // Don't try to send pointers, the will be handle by QRO_ |
370 | if (list.size() < N) |
371 | list.reserve(asize: N); |
372 | const int minFill = std::min(a: int(list.size()), b: N); |
373 | for (int i = 0; i < minFill; ++i) { |
374 | const int type = m_api->signalParameterType(sigIndex: index, paramIndex: i); |
375 | if (type == QMetaType::QVariant) |
376 | list[i] = *reinterpret_cast<QVariant *>(a[i + 1]); |
377 | else |
378 | list[i] = QVariant(QMetaType(type), a[i + 1]); |
379 | } |
380 | for (int i = int(list.size()); i < N; ++i) { |
381 | const int type = m_api->signalParameterType(sigIndex: index, paramIndex: i); |
382 | if (type == QMetaType::QVariant) |
383 | list << *reinterpret_cast<QVariant *>(a[i + 1]); |
384 | else |
385 | list << QVariant(QMetaType(type), a[i + 1]); |
386 | } |
387 | for (int i = N; i < list.size(); ++i) |
388 | list.removeLast(); |
389 | return &m_marshalledArgs; |
390 | } |
391 | |
392 | bool QRemoteObjectSourceBase::invoke(QMetaObject::Call c, int index, const QVariantList &args, QVariant* returnValue) |
393 | { |
394 | int status = -1; |
395 | int flags = 0; |
396 | bool forAdapter = (c == QMetaObject::InvokeMetaMethod ? m_api->isAdapterMethod(index) : m_api->isAdapterProperty(index)); |
397 | int resolvedIndex = (c == QMetaObject::InvokeMetaMethod ? m_api->sourceMethodIndex(index) : m_api->sourcePropertyIndex(index)); |
398 | if (resolvedIndex < 0) |
399 | return false; |
400 | QVarLengthArray<void*, 10> param(args.size() + 1); |
401 | |
402 | if (c == QMetaObject::InvokeMetaMethod) { |
403 | QMetaMethod method; |
404 | if (!forAdapter) |
405 | method = parent()->metaObject()->method(index: resolvedIndex); |
406 | |
407 | if (returnValue) { |
408 | if (!forAdapter && method.isValid() && method.returnType() == QMetaType::QVariant) |
409 | param[0] = const_cast<void*>(reinterpret_cast<const void*>(returnValue)); |
410 | else |
411 | param[0] = returnValue->data(); |
412 | } else { |
413 | param[0] = nullptr; |
414 | } |
415 | |
416 | auto argument = [&](int i) -> void * { |
417 | if ((forAdapter && m_api->methodParameterType(methodIndex: index, paramIndex: i) == QMetaType::QVariant) || |
418 | (method.isValid() && method.parameterType(index: i) == QMetaType::QVariant)) { |
419 | return const_cast<void*>(reinterpret_cast<const void*>(&args.at(i))); |
420 | } |
421 | return const_cast<void*>(args.at(i).data()); |
422 | }; |
423 | |
424 | for (int i = 0; i < args.size(); ++i) { |
425 | param[i + 1] = argument(i); |
426 | } |
427 | } else if (c == QMetaObject::WriteProperty || c == QMetaObject::ReadProperty) { |
428 | bool isQVariant = !forAdapter && parent()->metaObject()->property(index: resolvedIndex).userType() == QMetaType::QVariant; |
429 | for (int i = 0; i < args.size(); ++i) { |
430 | if (isQVariant) |
431 | param[i] = const_cast<void*>(reinterpret_cast<const void*>(&args.at(i))); |
432 | else |
433 | param[i] = const_cast<void*>(args.at(i).data()); |
434 | } |
435 | if (c == QMetaObject::WriteProperty) { |
436 | Q_ASSERT(param.size() == 2); // for return-value and setter value |
437 | // check QMetaProperty::write for an explanation of these |
438 | param.append(t: &status); |
439 | param.append(t: &flags); |
440 | } |
441 | } else { |
442 | // Better safe than sorry |
443 | return false; |
444 | } |
445 | int r = -1; |
446 | if (forAdapter) |
447 | r = m_adapter->qt_metacall(c, resolvedIndex, param.data()); |
448 | else |
449 | r = parent()->qt_metacall(c, resolvedIndex, param.data()); |
450 | return r == -1 && status == -1; |
451 | } |
452 | |
453 | void QRemoteObjectSourceBase::handleMetaCall(int index, QMetaObject::Call call, void **a) |
454 | { |
455 | if (d->m_listeners.empty()) |
456 | return; |
457 | |
458 | int propertyIndex = m_api->propertyIndexFromSignal(index); |
459 | if (propertyIndex >= 0) { |
460 | const int internalIndex = m_api->propertyRawIndexFromSignal(index); |
461 | const auto target = m_api->isAdapterProperty(internalIndex) ? m_adapter : m_object; |
462 | const QMetaProperty mp = target->metaObject()->property(index: propertyIndex); |
463 | qCDebug(QT_REMOTEOBJECT) << "Sending Invoke Property" << (m_api->isAdapterSignal(internalIndex) ? "via adapter" : "" ) << internalIndex << propertyIndex << mp.name() << mp.read(obj: target); |
464 | |
465 | d->codec->serializePropertyChangePacket(source: this, signalIndex: index); |
466 | propertyIndex = internalIndex; |
467 | } |
468 | |
469 | qCDebug(QT_REMOTEOBJECT) << "# Listeners" << d->m_listeners.size(); |
470 | qCDebug(QT_REMOTEOBJECT) << "Invoke args:" << m_object |
471 | << (call == 0 ? QLatin1String("InvokeMetaMethod" ) : QStringLiteral("Non-invoked call: %d" ).arg(a: call)) |
472 | << m_api->signalSignature(index) << *marshalArgs(index, a); |
473 | |
474 | d->codec->serializeInvokePacket(name: name(), call, index, args: *marshalArgs(index, a), serialId: -1, propertyIndex); |
475 | |
476 | d->codec->send(connections: d->m_listeners); |
477 | } |
478 | |
479 | void QRemoteObjectRootSource::addListener(QtROIoDeviceBase *io, bool dynamic) |
480 | { |
481 | d->m_listeners.append(t: io); |
482 | d->isDynamic = d->isDynamic || dynamic; |
483 | |
484 | if (dynamic) { |
485 | d->sentTypes.clear(); |
486 | d->codec->serializeInitDynamicPacket(this); |
487 | d->codec->send(connection: io); |
488 | } else { |
489 | d->codec->serializeInitPacket(this); |
490 | d->codec->send(connection: io); |
491 | } |
492 | } |
493 | |
494 | int QRemoteObjectRootSource::removeListener(QtROIoDeviceBase *io, bool shouldSendRemove) |
495 | { |
496 | d->m_listeners.removeAll(t: io); |
497 | if (shouldSendRemove) |
498 | { |
499 | d->codec->serializeRemoveObjectPacket(name: m_api->name()); |
500 | d->codec->send(connection: io); |
501 | } |
502 | return int(d->m_listeners.size()); |
503 | } |
504 | |
505 | int QRemoteObjectSourceBase::qt_metacall(QMetaObject::Call call, int methodId, void **a) |
506 | { |
507 | methodId = QObject::qt_metacall(call, methodId, a); |
508 | if (methodId < 0) |
509 | return methodId; |
510 | |
511 | if (call == QMetaObject::InvokeMetaMethod) |
512 | handleMetaCall(index: methodId, call, a); |
513 | |
514 | return -1; |
515 | } |
516 | |
517 | DynamicApiMap::DynamicApiMap(QObject *object, const QMetaObject *metaObject, const QString &name, const QString &typeName) |
518 | : m_name(name), |
519 | m_typeName(typeName), |
520 | m_metaObject(metaObject), |
521 | m_cachedMetamethodIndex(-1) |
522 | { |
523 | m_enumOffset = metaObject->enumeratorOffset(); |
524 | m_enumCount = metaObject->enumeratorCount() - m_enumOffset; |
525 | |
526 | const int propCount = metaObject->propertyCount(); |
527 | const int propOffset = metaObject->propertyOffset(); |
528 | m_properties.reserve(asize: propCount-propOffset); |
529 | QSet<int> invalidSignals; |
530 | for (int i = propOffset; i < propCount; ++i) { |
531 | const QMetaProperty property = metaObject->property(index: i); |
532 | const auto metaType = property.metaType(); |
533 | if (metaType.flags().testFlag(flag: QMetaType::PointerToQObject)) { |
534 | auto propertyMeta = metaType.metaObject(); |
535 | QObject *child = property.read(obj: object).value<QObject *>(); |
536 | const QMetaObject *meta = child ? child->metaObject() : propertyMeta; |
537 | if (!meta) { |
538 | const int notifyIndex = metaObject->property(index: i).notifySignalIndex(); |
539 | if (notifyIndex != -1) |
540 | invalidSignals << notifyIndex; |
541 | continue; |
542 | } |
543 | if (meta->inherits(metaObject: &QAbstractItemModel::staticMetaObject)) { |
544 | const QByteArray name = QByteArray::fromRawData(data: property.name(), |
545 | size: qsizetype(qstrlen(str: property.name()))); |
546 | const QByteArray infoName = name.toUpper() + QByteArrayLiteral("_ROLES" ); |
547 | const int infoIndex = metaObject->indexOfClassInfo(name: infoName.constData()); |
548 | QByteArray roleInfo; |
549 | if (infoIndex >= 0) { |
550 | auto ci = metaObject->classInfo(index: infoIndex); |
551 | roleInfo = QByteArray::fromRawData(data: ci.value(), size: qsizetype(qstrlen(str: ci.value()))); |
552 | } |
553 | m_models << ModelInfo({.ptr: qobject_cast<QAbstractItemModel *>(object: child), |
554 | .name: QString::fromLatin1(ba: property.name()), |
555 | .roles: roleInfo}); |
556 | } else { |
557 | QString typeName = QtRemoteObjects::getTypeNameAndMetaobjectFromClassInfo(meta); |
558 | if (typeName.isNull()) { |
559 | typeName = QString::fromLatin1(ba: meta->className()); |
560 | if (typeName.contains(s: QLatin1String("QQuick" ))) |
561 | typeName.remove(s: QLatin1String("QQuick" )); |
562 | else if (int index = typeName.indexOf(s: QLatin1String("_QMLTYPE_" ))) |
563 | typeName.truncate(pos: index); |
564 | // TODO better way to ensure we have consistent typenames between source/replicas? |
565 | else if (typeName.endsWith(s: QLatin1String("Source" ))) |
566 | typeName.chop(n: 6); |
567 | } |
568 | |
569 | m_subclasses << new DynamicApiMap(child, meta, QString::fromLatin1(ba: property.name()), typeName); |
570 | } |
571 | } |
572 | m_properties << i; |
573 | const int notifyIndex = metaObject->property(index: i).notifySignalIndex(); |
574 | if (notifyIndex != -1) { |
575 | m_signals << notifyIndex; |
576 | m_propertyAssociatedWithSignal.append(t: i-propOffset); |
577 | //The starting values of _signals will be the notify signals |
578 | //So if we are processing _signal with index i, api->sourcePropertyIndex(_propertyAssociatedWithSignal.at(i)) |
579 | //will be the property that changed. This is only valid if i < _propertyAssociatedWithSignal.size(). |
580 | } |
581 | } |
582 | const int methodCount = metaObject->methodCount(); |
583 | const int methodOffset = metaObject->methodOffset(); |
584 | for (int i = methodOffset; i < methodCount; ++i) { |
585 | const QMetaMethod mm = metaObject->method(index: i); |
586 | const QMetaMethod::MethodType m = mm.methodType(); |
587 | if (m == QMetaMethod::Signal) { |
588 | if (m_signals.indexOf(t: i) >= 0) // Already added as a property notifier |
589 | continue; |
590 | if (invalidSignals.contains(value: i)) // QObject with no metatype |
591 | continue; |
592 | m_signals << i; |
593 | } else if (m == QMetaMethod::Slot || m == QMetaMethod::Method) |
594 | m_methods << i; |
595 | } |
596 | |
597 | m_objectSignature = QtPrivate::qtro_classinfo_signature(metaObject); |
598 | } |
599 | |
600 | QByteArrayList DynamicApiMap::signalParameterNames(int index) const |
601 | { |
602 | const int objectIndex = m_signals.at(i: index); |
603 | checkCache(objectIndex); |
604 | return m_cachedMetamethod.parameterNames(); |
605 | } |
606 | |
607 | int DynamicApiMap::parameterCount(int objectIndex) const |
608 | { |
609 | checkCache(objectIndex); |
610 | return m_cachedMetamethod.parameterCount(); |
611 | } |
612 | |
613 | int DynamicApiMap::parameterType(int objectIndex, int paramIndex) const |
614 | { |
615 | checkCache(objectIndex); |
616 | return m_cachedMetamethod.parameterType(index: paramIndex); |
617 | } |
618 | |
619 | const QByteArray DynamicApiMap::signature(int objectIndex) const |
620 | { |
621 | checkCache(objectIndex); |
622 | return m_cachedMetamethod.methodSignature(); |
623 | } |
624 | |
625 | QMetaMethod::MethodType DynamicApiMap::methodType(int index) const |
626 | { |
627 | const int objectIndex = m_methods.at(i: index); |
628 | checkCache(objectIndex); |
629 | return m_cachedMetamethod.methodType(); |
630 | } |
631 | |
632 | const QByteArray DynamicApiMap::typeName(int index) const |
633 | { |
634 | const int objectIndex = m_methods.at(i: index); |
635 | checkCache(objectIndex); |
636 | return m_cachedMetamethod.typeName(); |
637 | } |
638 | |
639 | QByteArrayList DynamicApiMap::methodParameterNames(int index) const |
640 | { |
641 | const int objectIndex = m_methods.at(i: index); |
642 | checkCache(objectIndex); |
643 | return m_cachedMetamethod.parameterNames(); |
644 | } |
645 | |
646 | QRemoteObjectSourceBase::Private::Private(QRemoteObjectSourceIo *io, QRemoteObjectRootSource *root) |
647 | : m_sourceIo(io), codec(io->m_codec.data()), isDynamic(false), root(root) |
648 | { |
649 | } |
650 | |
651 | QT_END_NAMESPACE |
652 | |