[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the QtDBus module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at qt-info@nokia.com.
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | //
|
---|
| 43 | // W A R N I N G
|
---|
| 44 | // -------------
|
---|
| 45 | //
|
---|
| 46 | // This file is not part of the public API. This header file may
|
---|
| 47 | // change from version to version without notice, or even be
|
---|
| 48 | // removed.
|
---|
| 49 | //
|
---|
| 50 | // We mean it.
|
---|
| 51 | //
|
---|
| 52 | //
|
---|
| 53 |
|
---|
| 54 | #ifndef QDBUSCONNECTION_P_H
|
---|
| 55 | #define QDBUSCONNECTION_P_H
|
---|
| 56 |
|
---|
| 57 | #include <qdbuserror.h>
|
---|
| 58 | #include <qdbusconnection.h>
|
---|
| 59 |
|
---|
| 60 | #include <QtCore/qatomic.h>
|
---|
| 61 | #include <QtCore/qhash.h>
|
---|
| 62 | #include <QtCore/qmutex.h>
|
---|
| 63 | #include <QtCore/qobject.h>
|
---|
| 64 | #include <QtCore/qpointer.h>
|
---|
| 65 | #include <QtCore/qreadwritelock.h>
|
---|
| 66 | #include <QtCore/qstringlist.h>
|
---|
| 67 | #include <QtCore/qvarlengtharray.h>
|
---|
| 68 | #include <QtCore/qvector.h>
|
---|
| 69 |
|
---|
[561] | 70 | #include "qdbus_symbols_p.h"
|
---|
[2] | 71 |
|
---|
| 72 | #include <qdbusmessage.h>
|
---|
| 73 |
|
---|
[846] | 74 | #ifndef QT_NO_DBUS
|
---|
| 75 |
|
---|
[2] | 76 | QT_BEGIN_NAMESPACE
|
---|
| 77 |
|
---|
| 78 | class QDBusMessage;
|
---|
| 79 | class QSocketNotifier;
|
---|
| 80 | class QTimerEvent;
|
---|
| 81 | class QDBusObjectPrivate;
|
---|
| 82 | class QDBusCallDeliveryEvent;
|
---|
| 83 | class QDBusActivateObjectEvent;
|
---|
| 84 | class QMetaMethod;
|
---|
| 85 | class QDBusInterfacePrivate;
|
---|
| 86 | struct QDBusMetaObject;
|
---|
| 87 | class QDBusAbstractInterface;
|
---|
| 88 | class QDBusConnectionInterface;
|
---|
| 89 | class QDBusPendingCallPrivate;
|
---|
| 90 |
|
---|
| 91 | class QDBusErrorInternal
|
---|
| 92 | {
|
---|
| 93 | mutable DBusError error;
|
---|
| 94 | Q_DISABLE_COPY(QDBusErrorInternal)
|
---|
| 95 | public:
|
---|
| 96 | inline QDBusErrorInternal() { q_dbus_error_init(&error); }
|
---|
| 97 | inline ~QDBusErrorInternal() { q_dbus_error_free(&error); }
|
---|
| 98 | inline bool operator !() const { return !q_dbus_error_is_set(&error); }
|
---|
| 99 | inline operator DBusError *() { q_dbus_error_free(&error); return &error; }
|
---|
| 100 | inline operator QDBusError() const { QDBusError err(&error); q_dbus_error_free(&error); return err; }
|
---|
| 101 | };
|
---|
| 102 |
|
---|
| 103 | // QDBusConnectionPrivate holds the DBusConnection and
|
---|
| 104 | // can have many QDBusConnection objects referring to it
|
---|
| 105 |
|
---|
| 106 | class QDBusConnectionPrivate: public QObject
|
---|
| 107 | {
|
---|
| 108 | Q_OBJECT
|
---|
| 109 | public:
|
---|
| 110 | // structs and enums
|
---|
| 111 | enum ConnectionMode { InvalidMode, ServerMode, ClientMode, PeerMode }; // LocalMode
|
---|
| 112 |
|
---|
| 113 | struct Watcher
|
---|
| 114 | {
|
---|
| 115 | Watcher(): watch(0), read(0), write(0) {}
|
---|
| 116 | DBusWatch *watch;
|
---|
| 117 | QSocketNotifier *read;
|
---|
| 118 | QSocketNotifier *write;
|
---|
| 119 | };
|
---|
| 120 |
|
---|
| 121 | struct SignalHook
|
---|
| 122 | {
|
---|
| 123 | inline SignalHook() : obj(0), midx(-1) { }
|
---|
[561] | 124 | QString service, path, signature;
|
---|
[2] | 125 | QObject* obj;
|
---|
| 126 | int midx;
|
---|
| 127 | QList<int> params;
|
---|
[561] | 128 | QStringList argumentMatch;
|
---|
[2] | 129 | QByteArray matchRule;
|
---|
| 130 | };
|
---|
| 131 |
|
---|
| 132 | struct ObjectTreeNode
|
---|
| 133 | {
|
---|
| 134 | typedef QVector<ObjectTreeNode> DataList;
|
---|
| 135 |
|
---|
| 136 | inline ObjectTreeNode() : obj(0), flags(0) { }
|
---|
| 137 | inline ObjectTreeNode(const QString &n) // intentionally implicit
|
---|
| 138 | : name(n), obj(0), flags(0) { }
|
---|
| 139 | inline ~ObjectTreeNode() { }
|
---|
| 140 | inline bool operator<(const QString &other) const
|
---|
| 141 | { return name < other; }
|
---|
| 142 | inline bool operator<(const QStringRef &other) const
|
---|
| 143 | { return QStringRef(&name) < other; }
|
---|
| 144 |
|
---|
| 145 | QString name;
|
---|
| 146 | QObject* obj;
|
---|
| 147 | int flags;
|
---|
| 148 | DataList children;
|
---|
| 149 | };
|
---|
| 150 |
|
---|
| 151 | public:
|
---|
| 152 | // typedefs
|
---|
| 153 | typedef QMultiHash<int, Watcher> WatcherHash;
|
---|
| 154 | typedef QHash<int, DBusTimeout *> TimeoutHash;
|
---|
| 155 | typedef QList<QPair<DBusTimeout *, int> > PendingTimeoutList;
|
---|
| 156 |
|
---|
| 157 | typedef QMultiHash<QString, SignalHook> SignalHookHash;
|
---|
| 158 | typedef QHash<QString, QDBusMetaObject* > MetaObjectHash;
|
---|
| 159 | typedef QHash<QByteArray, int> MatchRefCountHash;
|
---|
| 160 |
|
---|
[561] | 161 | struct WatchedServiceData {
|
---|
| 162 | WatchedServiceData() : refcount(0) {}
|
---|
[846] | 163 | WatchedServiceData(const QString &owner, int refcount = 0)
|
---|
| 164 | : owner(owner), refcount(refcount)
|
---|
| 165 | {}
|
---|
[561] | 166 | QString owner;
|
---|
| 167 | int refcount;
|
---|
| 168 | };
|
---|
| 169 | typedef QHash<QString, WatchedServiceData> WatchedServicesHash;
|
---|
| 170 |
|
---|
[2] | 171 | public:
|
---|
| 172 | // public methods are entry points from other objects
|
---|
| 173 | explicit QDBusConnectionPrivate(QObject *parent = 0);
|
---|
| 174 | ~QDBusConnectionPrivate();
|
---|
| 175 | void deleteYourself();
|
---|
| 176 |
|
---|
| 177 | void setBusService(const QDBusConnection &connection);
|
---|
| 178 | void setPeer(DBusConnection *connection, const QDBusErrorInternal &error);
|
---|
| 179 | void setConnection(DBusConnection *connection, const QDBusErrorInternal &error);
|
---|
| 180 | void setServer(DBusServer *server, const QDBusErrorInternal &error);
|
---|
| 181 | void closeConnection();
|
---|
| 182 |
|
---|
| 183 | QString getNameOwner(const QString &service);
|
---|
| 184 |
|
---|
| 185 | int send(const QDBusMessage &message);
|
---|
| 186 | QDBusMessage sendWithReply(const QDBusMessage &message, int mode, int timeout = -1);
|
---|
| 187 | QDBusMessage sendWithReplyLocal(const QDBusMessage &message);
|
---|
| 188 | QDBusPendingCallPrivate *sendWithReplyAsync(const QDBusMessage &message, int timeout = -1);
|
---|
| 189 | int sendWithReplyAsync(const QDBusMessage &message, QObject *receiver,
|
---|
| 190 | const char *returnMethod, const char *errorMethod, int timeout = -1);
|
---|
[561] | 191 | bool connectSignal(const QString &service, const QString &path, const QString& interface,
|
---|
| 192 | const QString &name, const QStringList &argumentMatch, const QString &signature,
|
---|
| 193 | QObject *receiver, const char *slot);
|
---|
[2] | 194 | void connectSignal(const QString &key, const SignalHook &hook);
|
---|
| 195 | SignalHookHash::Iterator disconnectSignal(SignalHookHash::Iterator &it);
|
---|
[561] | 196 | bool disconnectSignal(const QString &service, const QString &path, const QString& interface,
|
---|
| 197 | const QString &name, const QStringList &argumentMatch, const QString &signature,
|
---|
| 198 | QObject *receiver, const char *slot);
|
---|
[2] | 199 | void registerObject(const ObjectTreeNode *node);
|
---|
[561] | 200 | void connectRelay(const QString &service,
|
---|
[2] | 201 | const QString &path, const QString &interface,
|
---|
| 202 | QDBusAbstractInterface *receiver, const char *signal);
|
---|
[561] | 203 | void disconnectRelay(const QString &service,
|
---|
[2] | 204 | const QString &path, const QString &interface,
|
---|
| 205 | QDBusAbstractInterface *receiver, const char *signal);
|
---|
[846] | 206 | void registerService(const QString &serviceName);
|
---|
| 207 | void unregisterService(const QString &serviceName);
|
---|
[2] | 208 |
|
---|
| 209 | bool handleMessage(const QDBusMessage &msg);
|
---|
| 210 | void waitForFinished(QDBusPendingCallPrivate *pcall);
|
---|
| 211 |
|
---|
| 212 | QDBusMetaObject *findMetaObject(const QString &service, const QString &path,
|
---|
| 213 | const QString &interface, QDBusError &error);
|
---|
| 214 |
|
---|
| 215 | void postEventToThread(int action, QObject *target, QEvent *event);
|
---|
| 216 |
|
---|
| 217 | inline void serverConnection(const QDBusConnection &connection)
|
---|
| 218 | { emit newServerConnection(connection); }
|
---|
| 219 |
|
---|
| 220 | private:
|
---|
| 221 | void checkThread();
|
---|
| 222 | bool handleError(const QDBusErrorInternal &error);
|
---|
| 223 |
|
---|
| 224 | void handleSignal(const QString &key, const QDBusMessage &msg);
|
---|
| 225 | void handleSignal(const QDBusMessage &msg);
|
---|
| 226 | void handleObjectCall(const QDBusMessage &message);
|
---|
| 227 |
|
---|
| 228 | void activateSignal(const SignalHook& hook, const QDBusMessage &msg);
|
---|
| 229 | void activateObject(ObjectTreeNode &node, const QDBusMessage &msg, int pathStartPos);
|
---|
| 230 | bool activateInternalFilters(const ObjectTreeNode &node, const QDBusMessage &msg);
|
---|
| 231 | bool activateCall(QObject *object, int flags, const QDBusMessage &msg);
|
---|
| 232 |
|
---|
| 233 | void sendError(const QDBusMessage &msg, QDBusError::ErrorType code);
|
---|
| 234 | void deliverCall(QObject *object, int flags, const QDBusMessage &msg,
|
---|
| 235 | const QList<int> &metaTypes, int slotIdx);
|
---|
| 236 |
|
---|
| 237 | bool isServiceRegisteredByThread(const QString &serviceName) const;
|
---|
| 238 |
|
---|
[561] | 239 | QString getNameOwnerNoCache(const QString &service);
|
---|
| 240 |
|
---|
[2] | 241 | protected:
|
---|
| 242 | void customEvent(QEvent *e);
|
---|
| 243 | void timerEvent(QTimerEvent *e);
|
---|
| 244 |
|
---|
| 245 | public slots:
|
---|
| 246 | // public slots
|
---|
| 247 | void doDispatch();
|
---|
| 248 | void socketRead(int);
|
---|
| 249 | void socketWrite(int);
|
---|
| 250 | void objectDestroyed(QObject *o);
|
---|
| 251 | void relaySignal(QObject *obj, const QMetaObject *, int signalId, const QVariantList &args);
|
---|
| 252 |
|
---|
[846] | 253 | private slots:
|
---|
| 254 | void serviceOwnerChangedNoLock(const QString &name, const QString &oldOwner, const QString &newOwner);
|
---|
| 255 | void registerServiceNoLock(const QString &serviceName);
|
---|
| 256 | void unregisterServiceNoLock(const QString &serviceName);
|
---|
| 257 |
|
---|
[2] | 258 | signals:
|
---|
| 259 | void serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner);
|
---|
| 260 | void callWithCallbackFailed(const QDBusError &error, const QDBusMessage &message);
|
---|
| 261 | void newServerConnection(const QDBusConnection &connection);
|
---|
| 262 |
|
---|
| 263 | public:
|
---|
| 264 | QAtomicInt ref;
|
---|
| 265 | QString name; // this connection's name
|
---|
| 266 | QString baseService; // this connection's base service
|
---|
| 267 |
|
---|
| 268 | ConnectionMode mode;
|
---|
| 269 |
|
---|
| 270 | // members accessed in unlocked mode (except for deletion)
|
---|
| 271 | // connection and server provide their own locking mechanisms
|
---|
| 272 | // busService doesn't have state to be changed
|
---|
| 273 | DBusConnection *connection;
|
---|
| 274 | DBusServer *server;
|
---|
| 275 | QDBusConnectionInterface *busService;
|
---|
| 276 |
|
---|
| 277 | // watchers and timeouts are accessed from any thread
|
---|
| 278 | // but the corresponding timer and QSocketNotifier must be handled
|
---|
| 279 | // only in the object's thread
|
---|
| 280 | QMutex watchAndTimeoutLock;
|
---|
| 281 | WatcherHash watchers;
|
---|
| 282 | TimeoutHash timeouts;
|
---|
| 283 | PendingTimeoutList timeoutsPendingAdd;
|
---|
| 284 |
|
---|
| 285 | // members accessed through a lock
|
---|
| 286 | QMutex dispatchLock;
|
---|
| 287 | QReadWriteLock lock;
|
---|
| 288 | QDBusError lastError;
|
---|
| 289 |
|
---|
| 290 | QStringList serviceNames;
|
---|
[561] | 291 | WatchedServicesHash watchedServices;
|
---|
[2] | 292 | SignalHookHash signalHooks;
|
---|
| 293 | MatchRefCountHash matchRefCounts;
|
---|
| 294 | ObjectTreeNode rootNode;
|
---|
| 295 | MetaObjectHash cachedMetaObjects;
|
---|
| 296 |
|
---|
| 297 | QMutex callDeliveryMutex;
|
---|
| 298 | QDBusCallDeliveryEvent *callDeliveryState; // protected by the callDeliveryMutex mutex
|
---|
| 299 |
|
---|
| 300 | public:
|
---|
| 301 | // static methods
|
---|
| 302 | static int findSlot(QObject *obj, const QByteArray &normalizedName, QList<int>& params);
|
---|
| 303 | static bool prepareHook(QDBusConnectionPrivate::SignalHook &hook, QString &key,
|
---|
[561] | 304 | const QString &service,
|
---|
[2] | 305 | const QString &path, const QString &interface, const QString &name,
|
---|
[561] | 306 | const QStringList &argMatch,
|
---|
[2] | 307 | QObject *receiver, const char *signal, int minMIdx,
|
---|
| 308 | bool buildSignature);
|
---|
| 309 | static DBusHandlerResult messageFilter(DBusConnection *, DBusMessage *, void *);
|
---|
[846] | 310 | static bool checkReplyForDelivery(QDBusConnectionPrivate *target, QObject *object,
|
---|
| 311 | int idx, const QList<int> &metaTypes,
|
---|
| 312 | const QDBusMessage &msg);
|
---|
[2] | 313 | static QDBusCallDeliveryEvent *prepareReply(QDBusConnectionPrivate *target, QObject *object,
|
---|
| 314 | int idx, const QList<int> &metaTypes,
|
---|
| 315 | const QDBusMessage &msg);
|
---|
| 316 | static void processFinishedCall(QDBusPendingCallPrivate *call);
|
---|
| 317 |
|
---|
| 318 | static QDBusConnectionPrivate *d(const QDBusConnection& q) { return q.d; }
|
---|
| 319 | static QDBusConnection q(QDBusConnectionPrivate *connection) { return QDBusConnection(connection); }
|
---|
| 320 |
|
---|
| 321 | static void setSender(const QDBusConnectionPrivate *s);
|
---|
| 322 |
|
---|
| 323 | friend class QDBusActivateObjectEvent;
|
---|
| 324 | friend class QDBusCallDeliveryEvent;
|
---|
| 325 | };
|
---|
| 326 |
|
---|
| 327 | // in qdbusmisc.cpp
|
---|
| 328 | extern int qDBusParametersForMethod(const QMetaMethod &mm, QList<int>& metaTypes);
|
---|
| 329 | extern int qDBusNameToTypeId(const char *name);
|
---|
| 330 | extern bool qDBusCheckAsyncTag(const char *tag);
|
---|
[561] | 331 | extern bool qDBusInterfaceInObject(QObject *obj, const QString &interface_name);
|
---|
| 332 | extern QString qDBusInterfaceFromMetaObject(const QMetaObject *mo);
|
---|
[2] | 333 |
|
---|
| 334 | // in qdbusinternalfilters.cpp
|
---|
| 335 | extern QString qDBusIntrospectObject(const QDBusConnectionPrivate::ObjectTreeNode &node);
|
---|
| 336 | extern QDBusMessage qDBusPropertyGet(const QDBusConnectionPrivate::ObjectTreeNode &node,
|
---|
| 337 | const QDBusMessage &msg);
|
---|
| 338 | extern QDBusMessage qDBusPropertySet(const QDBusConnectionPrivate::ObjectTreeNode &node,
|
---|
| 339 | const QDBusMessage &msg);
|
---|
| 340 | extern QDBusMessage qDBusPropertyGetAll(const QDBusConnectionPrivate::ObjectTreeNode &node,
|
---|
| 341 | const QDBusMessage &msg);
|
---|
| 342 |
|
---|
| 343 | QT_END_NAMESPACE
|
---|
| 344 |
|
---|
[846] | 345 | #endif // QT_NO_DBUS
|
---|
[2] | 346 | #endif
|
---|