| 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 QREMOTEOBJECTNODE_P_H |
| 6 | #define QREMOTEOBJECTNODE_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/private/qobject_p.h> |
| 20 | #include "qremoteobjectsourceio_p.h" |
| 21 | #include "qremoteobjectstructs_p.h" |
| 22 | #include "qremoteobjectreplica.h" |
| 23 | #include "qremoteobjectnode.h" |
| 24 | |
| 25 | #include <QtCore/qbasictimer.h> |
| 26 | #include <QtCore/qmutex.h> |
| 27 | |
| 28 | QT_BEGIN_NAMESPACE |
| 29 | |
| 30 | #define qRODebug(x) qCDebug(QT_REMOTEOBJECT) << qPrintable(QtPrivate::deref_for_methodcall(x).objectName()) |
| 31 | #define qROWarning(x) qCWarning(QT_REMOTEOBJECT) << qPrintable(QtPrivate::deref_for_methodcall(x).objectName()) |
| 32 | #define qROCritical(x) qCCritical(QT_REMOTEOBJECT) << qPrintable(QtPrivate::deref_for_methodcall(x).objectName()) |
| 33 | #define qROFatal(x) qCFatal(QT_REMOTEOBJECT) << qPrintable(QtPrivate::deref_for_methodcall(x).objectName()) |
| 34 | #define qROPrivDebug() qCDebug(QT_REMOTEOBJECT) << qPrintable(q_ptr->objectName()) |
| 35 | #define qROPrivWarning() qCWarning(QT_REMOTEOBJECT) << qPrintable(q_ptr->objectName()) |
| 36 | #define qROPrivCritical() qCCritical(QT_REMOTEOBJECT) << qPrintable(q_ptr->objectName()) |
| 37 | #define qROPrivFatal() qCFatal(QT_REMOTEOBJECT) << qPrintable(q_ptr->objectName()) |
| 38 | |
| 39 | class QRemoteObjectRegistry; |
| 40 | class QRegistrySource; |
| 41 | class QConnectedReplicaImplementation; |
| 42 | |
| 43 | QMetaObject *registerGadget(QObject *reference, const QRemoteObjectInternalTypes::GadgetData &gadget, QByteArray typeName); |
| 44 | QMetaObject *registerAndTrackDefinition(const QRemoteObjectInternalTypes::ClassData &data, QObject *reference); |
| 45 | bool trackAdditionalReference(QObject *reference, const QByteArray &typeName); |
| 46 | |
| 47 | class QRemoteObjectAbstractPersistedStorePrivate : public QObjectPrivate |
| 48 | { |
| 49 | public: |
| 50 | QRemoteObjectAbstractPersistedStorePrivate(); |
| 51 | virtual ~QRemoteObjectAbstractPersistedStorePrivate(); |
| 52 | |
| 53 | Q_DECLARE_PUBLIC(QRemoteObjectAbstractPersistedStore) |
| 54 | }; |
| 55 | |
| 56 | class QRemoteObjectMetaObjectManager |
| 57 | { |
| 58 | public: |
| 59 | QRemoteObjectMetaObjectManager() {} |
| 60 | ~QRemoteObjectMetaObjectManager(); |
| 61 | |
| 62 | const QMetaObject *metaObjectForType(const QString &type); |
| 63 | QMetaObject *addDynamicType(QtROIoDeviceBase* connection, QDataStream &in); |
| 64 | void addFromMetaObject(const QMetaObject *); |
| 65 | |
| 66 | private: |
| 67 | QHash<QString, QMetaObject*> dynamicTypes; |
| 68 | QHash<QString, const QMetaObject*> staticTypes; |
| 69 | QHash<QMetaObject *, QList<QMetaType>> enumTypes; |
| 70 | }; |
| 71 | |
| 72 | struct ProxyReplicaInfo; |
| 73 | class ProxyInfo : public QObject |
| 74 | { |
| 75 | Q_OBJECT |
| 76 | public: |
| 77 | ProxyInfo(QRemoteObjectNode *node, QRemoteObjectHostBase *parent, QRemoteObjectHostBase::RemoteObjectNameFilter filter); |
| 78 | ~ProxyInfo() override; |
| 79 | enum class ProxyDirection { Forward, Reverse }; |
| 80 | |
| 81 | bool setReverseProxy(QRemoteObjectHostBase::RemoteObjectNameFilter filter); |
| 82 | void proxyObject(const QRemoteObjectSourceLocation &entry, ProxyDirection direction = ProxyDirection::Forward); |
| 83 | void unproxyObject(const QRemoteObjectSourceLocation &entry); |
| 84 | |
| 85 | QRemoteObjectNode *proxyNode; |
| 86 | QRemoteObjectHostBase *parentNode; |
| 87 | QRemoteObjectHostBase::RemoteObjectNameFilter proxyFilter; |
| 88 | QRemoteObjectHostBase::RemoteObjectNameFilter reverseFilter; |
| 89 | QHash<QString, ProxyReplicaInfo*> proxiedReplicas; |
| 90 | |
| 91 | private: |
| 92 | void disableAndDeleteObject(ProxyReplicaInfo* info); |
| 93 | }; |
| 94 | |
| 95 | struct ProxyReplicaInfo |
| 96 | { |
| 97 | // We need QObject, so we can hold Dynamic Replicas and QAIM Adapters |
| 98 | QObject* replica; |
| 99 | ProxyInfo::ProxyDirection direction; |
| 100 | ~ProxyReplicaInfo() { delete replica; } |
| 101 | }; |
| 102 | |
| 103 | class QRemoteObjectNodePrivate : public QObjectPrivate |
| 104 | { |
| 105 | public: |
| 106 | QRemoteObjectNodePrivate(); |
| 107 | ~QRemoteObjectNodePrivate() override; |
| 108 | |
| 109 | virtual QRemoteObjectSourceLocations remoteObjectAddresses() const; |
| 110 | |
| 111 | void setReplicaImplementation(const QMetaObject *, QRemoteObjectReplica *, const QString &); |
| 112 | |
| 113 | void setLastError(QRemoteObjectNode::ErrorCode errorCode); |
| 114 | |
| 115 | void connectReplica(QObject *object, QRemoteObjectReplica *instance); |
| 116 | void openConnectionIfNeeded(const QString &name); |
| 117 | |
| 118 | bool initConnection(const QUrl &address); |
| 119 | bool hasInstance(const QString &name); |
| 120 | void setRegistry(QRemoteObjectRegistry *); |
| 121 | QVariant handlePointerToQObjectProperty(QConnectedReplicaImplementation *rep, int index, const QVariant &property); |
| 122 | void handlePointerToQObjectProperties(QConnectedReplicaImplementation *rep, QVariantList &properties); |
| 123 | |
| 124 | void onClientRead(QObject *obj); |
| 125 | void onRemoteObjectSourceAdded(const QRemoteObjectSourceLocation &entry); |
| 126 | void onRemoteObjectSourceRemoved(const QRemoteObjectSourceLocation &entry); |
| 127 | void onRegistryInitialized(); |
| 128 | void onShouldReconnect(QtROClientIoDevice *ioDevice); |
| 129 | |
| 130 | virtual QReplicaImplementationInterface *handleNewAcquire(const QMetaObject *meta, QRemoteObjectReplica *instance, const QString &name); |
| 131 | void handleReplicaConnection(const QString &name); |
| 132 | void handleReplicaConnection(const QByteArray &sourceSignature, QConnectedReplicaImplementation *rep, QtROIoDeviceBase *connection); |
| 133 | void initialize(); |
| 134 | bool setRegistryUrlNodeImpl(const QUrl ®istryAddr); |
| 135 | |
| 136 | private: |
| 137 | bool checkSignatures(const QByteArray &a, const QByteArray &b); |
| 138 | |
| 139 | public: |
| 140 | struct SourceInfo |
| 141 | { |
| 142 | QtROIoDeviceBase* device; |
| 143 | QString typeName; |
| 144 | QByteArray objectSignature; |
| 145 | }; |
| 146 | |
| 147 | QMutex mutex; |
| 148 | QUrl registryAddress; |
| 149 | QHash<QString, QWeakPointer<QReplicaImplementationInterface> > replicas; |
| 150 | QMap<QString, SourceInfo> connectedSources; |
| 151 | QMap<QString, QRemoteObjectNode::RemoteObjectSchemaHandler> schemaHandlers; |
| 152 | QSet<QtROClientIoDevice*> pendingReconnect; |
| 153 | QSet<QUrl> requestedUrls; |
| 154 | QRemoteObjectRegistry *registry; |
| 155 | int retryInterval; |
| 156 | QBasicTimer reconnectTimer; |
| 157 | QRemoteObjectNode::ErrorCode lastError; |
| 158 | QString rxName; |
| 159 | QRemoteObjectPackets::ObjectInfoList rxObjects; |
| 160 | QVariantList rxArgs; |
| 161 | QVariant rxValue; |
| 162 | QRemoteObjectAbstractPersistedStore *persistedStore; |
| 163 | int m_heartbeatInterval = 0; |
| 164 | QRemoteObjectMetaObjectManager dynamicTypeManager; |
| 165 | Q_DECLARE_PUBLIC(QRemoteObjectNode) |
| 166 | }; |
| 167 | |
| 168 | class QRemoteObjectHostBasePrivate : public QRemoteObjectNodePrivate |
| 169 | { |
| 170 | public: |
| 171 | QRemoteObjectHostBasePrivate(); |
| 172 | ~QRemoteObjectHostBasePrivate() override; |
| 173 | QReplicaImplementationInterface *handleNewAcquire(const QMetaObject *meta, QRemoteObjectReplica *instance, const QString &name) override; |
| 174 | |
| 175 | bool setHostUrlBaseImpl(const QUrl &hostAddress, |
| 176 | QRemoteObjectHostBase::AllowedSchemas allowedSchemas = |
| 177 | QRemoteObjectHostBase::BuiltInSchemasOnly); |
| 178 | |
| 179 | public: |
| 180 | QRemoteObjectSourceIo *remoteObjectIo; |
| 181 | ProxyInfo *proxyInfo = nullptr; |
| 182 | Q_DECLARE_PUBLIC(QRemoteObjectHostBase); |
| 183 | }; |
| 184 | |
| 185 | class QRemoteObjectHostPrivate : public QRemoteObjectHostBasePrivate |
| 186 | { |
| 187 | public: |
| 188 | QRemoteObjectHostPrivate(); |
| 189 | ~QRemoteObjectHostPrivate() override; |
| 190 | |
| 191 | bool setHostUrlHostImpl(const QUrl &hostAddress, |
| 192 | QRemoteObjectHostBase::AllowedSchemas allowedSchemas = |
| 193 | QRemoteObjectHostBase::BuiltInSchemasOnly); |
| 194 | |
| 195 | Q_DECLARE_PUBLIC(QRemoteObjectHost); |
| 196 | }; |
| 197 | |
| 198 | class QRemoteObjectRegistryHostPrivate : public QRemoteObjectHostBasePrivate |
| 199 | { |
| 200 | public: |
| 201 | QRemoteObjectRegistryHostPrivate(); |
| 202 | ~QRemoteObjectRegistryHostPrivate() override; |
| 203 | QRemoteObjectSourceLocations remoteObjectAddresses() const override; |
| 204 | QRegistrySource *registrySource; |
| 205 | |
| 206 | bool setRegistryUrlRegistryHostImpl(const QUrl ®istryUrl); |
| 207 | |
| 208 | Q_DECLARE_PUBLIC(QRemoteObjectRegistryHost); |
| 209 | }; |
| 210 | |
| 211 | QT_END_NAMESPACE |
| 212 | |
| 213 | #endif |
| 214 | |