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// Qt-Security score:significant reason:declarations-only
4
5#ifndef QREMOTEOBJECTSOURCE_P_H
6#define QREMOTEOBJECTSOURCE_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/qlist.h>
20#include <QtCore/qmetaobject.h>
21#include <QtCore/qobject.h>
22#include <QtCore/qpointer.h>
23#include "qremoteobjectsource.h"
24#include "qremoteobjectpacket_p.h"
25
26QT_BEGIN_NAMESPACE
27
28class QRemoteObjectSourceIo;
29class QtROIoDeviceBase;
30
31class QRemoteObjectSourceBase : public QObject
32{
33public:
34 ~QRemoteObjectSourceBase() override;
35
36 void setConnections();
37 void resetObject(QObject *newObject);
38 int qt_metacall(QMetaObject::Call call, int methodId, void **a) final;
39 QObject *m_object, *m_adapter;
40 const SourceApiMap *m_api;
41 QVariantList m_marshalledArgs;
42 bool hasAdapter() const { return m_adapter; }
43 virtual QString name() const = 0;
44 virtual bool isRoot() const = 0;
45
46 QVariantList* marshalArgs(int index, void **a);
47 void handleMetaCall(int index, QMetaObject::Call call, void **a);
48 bool invoke(QMetaObject::Call c, int index, const QVariantList& args, QVariant* returnValue = nullptr);
49 QByteArray m_objectChecksum;
50 QMap<int, QPointer<QRemoteObjectSourceBase>> m_children;
51 struct Private {
52 Private(QRemoteObjectSourceIo *io, QRemoteObjectRootSource *root);
53 QRemoteObjectSourceIo *m_sourceIo;
54 QList<QtROIoDeviceBase*> m_listeners;
55 // Pointer to codec, not owned by Private. We can assume it is valid.
56 QRemoteObjectPackets::CodecBase *codec;
57
58 // Types needed during recursively sending a root to a new listener
59 QSet<QString> sentTypes;
60 bool isDynamic;
61 QRemoteObjectRootSource *root;
62 };
63 Private *d;
64 static const int qobjectPropertyOffset;
65 static const int qobjectMethodOffset;
66protected:
67 explicit QRemoteObjectSourceBase(QObject *object, Private *d, const SourceApiMap *, QObject *adapter);
68};
69
70class QRemoteObjectSource : public QRemoteObjectSourceBase
71{
72public:
73 explicit QRemoteObjectSource(QObject *object, Private *d, const SourceApiMap *, QObject *adapter, const QString &parentName);
74 ~QRemoteObjectSource() override;
75
76 bool isRoot() const override { return false; }
77 QString name() const override { return m_name; }
78
79 QString m_name;
80};
81
82class QRemoteObjectRootSource : public QRemoteObjectSourceBase
83{
84public:
85 explicit QRemoteObjectRootSource(QObject *object, const SourceApiMap *,
86 QObject *adapter, QRemoteObjectSourceIo *sourceIo);
87 ~QRemoteObjectRootSource() override;
88
89 bool isRoot() const override { return true; }
90 QString name() const override { return m_name; }
91 void addListener(QtROIoDeviceBase *io, bool dynamic = false);
92 int removeListener(QtROIoDeviceBase *io, bool shouldSendRemove = false);
93
94 QString m_name;
95};
96
97class DynamicApiMap final : public SourceApiMap
98{
99public:
100 DynamicApiMap(QObject *object, const QMetaObject *metaObject, const QString &name, const QString &typeName);
101 ~DynamicApiMap() override {}
102 QString name() const override { return m_name; }
103 QString typeName() const override { return m_typeName; }
104 QByteArray className() const override { return QByteArray(m_metaObject->className()); }
105 int enumCount() const override { return m_enumCount; }
106 int propertyCount() const override { return m_properties.size(); }
107 int signalCount() const override { return m_signals.size(); }
108 int methodCount() const override { return m_methods.size(); }
109 int sourceEnumIndex(int index) const override
110 {
111 if (index < 0 || index >= enumCount())
112 return -1;
113 return m_enumOffset + index;
114 }
115 int sourcePropertyIndex(int index) const override
116 {
117 if (index < 0 || index >= propertyCount())
118 return -1;
119 return m_properties.at(i: index);
120 }
121 int sourceSignalIndex(int index) const override
122 {
123 if (index < 0 || index >= signalCount())
124 return -1;
125 return m_signals.at(i: index);
126 }
127 int sourceMethodIndex(int index) const override
128 {
129 if (index < 0 || index >= methodCount())
130 return -1;
131 return m_methods.at(i: index);
132 }
133 int signalParameterCount(int index) const override { return parameterCount(objectIndex: m_signals.at(i: index)); }
134 int signalParameterType(int sigIndex, int paramIndex) const override { return parameterType(objectIndex: m_signals.at(i: sigIndex), paramIndex); }
135 const QByteArray signalSignature(int index) const override { return signature(objectIndex: m_signals.at(i: index)); }
136 QByteArrayList signalParameterNames(int index) const override;
137
138 int methodParameterCount(int index) const override { return parameterCount(objectIndex: m_methods.at(i: index)); }
139 int methodParameterType(int methodIndex, int paramIndex) const override { return parameterType(objectIndex: m_methods.at(i: methodIndex), paramIndex); }
140 const QByteArray methodSignature(int index) const override { return signature(objectIndex: m_methods.at(i: index)); }
141 QMetaMethod::MethodType methodType(int index) const override;
142 const QByteArray typeName(int index) const override;
143 QByteArrayList methodParameterNames(int index) const override;
144
145 int propertyIndexFromSignal(int index) const override
146 {
147 if (index >= 0 && index < m_propertyAssociatedWithSignal.size())
148 return m_properties.at(i: m_propertyAssociatedWithSignal.at(i: index));
149 return -1;
150 }
151 int propertyRawIndexFromSignal(int index) const override
152 {
153 if (index >= 0 && index < m_propertyAssociatedWithSignal.size())
154 return m_propertyAssociatedWithSignal.at(i: index);
155 return -1;
156 }
157 QByteArray objectSignature() const override { return m_objectSignature; }
158
159 bool isDynamic() const override { return true; }
160
161 int parameterCount(int objectIndex) const;
162 int parameterType(int objectIndex, int paramIndex) const;
163 const QByteArray signature(int objectIndex) const;
164 inline void checkCache(int objectIndex) const
165 {
166 if (objectIndex != m_cachedMetamethodIndex) {
167 m_cachedMetamethodIndex = objectIndex;
168 m_cachedMetamethod = m_metaObject->method(index: objectIndex);
169 }
170 }
171
172 QString m_name;
173 QString m_typeName;
174 int m_enumCount;
175 int m_enumOffset;
176 QList<int> m_properties;
177 QList<int> m_signals;
178 QList<int> m_methods;
179 QList<int> m_propertyAssociatedWithSignal;
180 const QMetaObject *m_metaObject;
181 mutable QMetaMethod m_cachedMetamethod;
182 mutable int m_cachedMetamethodIndex;
183 QByteArray m_objectSignature;
184};
185
186QT_END_NAMESPACE
187
188#endif
189

source code of qtremoteobjects/src/remoteobjects/qremoteobjectsource_p.h