1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
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 | **
|
---|
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.
|
---|
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 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
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 |
|
---|
70 | #include "qdbus_symbols_p.h"
|
---|
71 |
|
---|
72 | #include <qdbusmessage.h>
|
---|
73 |
|
---|
74 | QT_BEGIN_NAMESPACE
|
---|
75 |
|
---|
76 | class QDBusMessage;
|
---|
77 | class QSocketNotifier;
|
---|
78 | class QTimerEvent;
|
---|
79 | class QDBusObjectPrivate;
|
---|
80 | class QDBusCallDeliveryEvent;
|
---|
81 | class QDBusActivateObjectEvent;
|
---|
82 | class QMetaMethod;
|
---|
83 | class QDBusInterfacePrivate;
|
---|
84 | struct QDBusMetaObject;
|
---|
85 | class QDBusAbstractInterface;
|
---|
86 | class QDBusConnectionInterface;
|
---|
87 | class QDBusPendingCallPrivate;
|
---|
88 |
|
---|
89 | class QDBusErrorInternal
|
---|
90 | {
|
---|
91 | mutable DBusError error;
|
---|
92 | Q_DISABLE_COPY(QDBusErrorInternal)
|
---|
93 | public:
|
---|
94 | inline QDBusErrorInternal() { q_dbus_error_init(&error); }
|
---|
95 | inline ~QDBusErrorInternal() { q_dbus_error_free(&error); }
|
---|
96 | inline bool operator !() const { return !q_dbus_error_is_set(&error); }
|
---|
97 | inline operator DBusError *() { q_dbus_error_free(&error); return &error; }
|
---|
98 | inline operator QDBusError() const { QDBusError err(&error); q_dbus_error_free(&error); return err; }
|
---|
99 | };
|
---|
100 |
|
---|
101 | // QDBusConnectionPrivate holds the DBusConnection and
|
---|
102 | // can have many QDBusConnection objects referring to it
|
---|
103 |
|
---|
104 | class QDBusConnectionPrivate: public QObject
|
---|
105 | {
|
---|
106 | Q_OBJECT
|
---|
107 | public:
|
---|
108 | // structs and enums
|
---|
109 | enum ConnectionMode { InvalidMode, ServerMode, ClientMode, PeerMode }; // LocalMode
|
---|
110 |
|
---|
111 | struct Watcher
|
---|
112 | {
|
---|
113 | Watcher(): watch(0), read(0), write(0) {}
|
---|
114 | DBusWatch *watch;
|
---|
115 | QSocketNotifier *read;
|
---|
116 | QSocketNotifier *write;
|
---|
117 | };
|
---|
118 |
|
---|
119 | struct SignalHook
|
---|
120 | {
|
---|
121 | inline SignalHook() : obj(0), midx(-1) { }
|
---|
122 | QString service, path, signature;
|
---|
123 | QObject* obj;
|
---|
124 | int midx;
|
---|
125 | QList<int> params;
|
---|
126 | QStringList argumentMatch;
|
---|
127 | QByteArray matchRule;
|
---|
128 | };
|
---|
129 |
|
---|
130 | struct ObjectTreeNode
|
---|
131 | {
|
---|
132 | typedef QVector<ObjectTreeNode> DataList;
|
---|
133 |
|
---|
134 | inline ObjectTreeNode() : obj(0), flags(0) { }
|
---|
135 | inline ObjectTreeNode(const QString &n) // intentionally implicit
|
---|
136 | : name(n), obj(0), flags(0) { }
|
---|
137 | inline ~ObjectTreeNode() { }
|
---|
138 | inline bool operator<(const QString &other) const
|
---|
139 | { return name < other; }
|
---|
140 | inline bool operator<(const QStringRef &other) const
|
---|
141 | { return QStringRef(&name) < other; }
|
---|
142 |
|
---|
143 | QString name;
|
---|
144 | QObject* obj;
|
---|
145 | int flags;
|
---|
146 | DataList children;
|
---|
147 | };
|
---|
148 |
|
---|
149 | public:
|
---|
150 | // typedefs
|
---|
151 | typedef QMultiHash<int, Watcher> WatcherHash;
|
---|
152 | typedef QHash<int, DBusTimeout *> TimeoutHash;
|
---|
153 | typedef QList<QPair<DBusTimeout *, int> > PendingTimeoutList;
|
---|
154 |
|
---|
155 | typedef QMultiHash<QString, SignalHook> SignalHookHash;
|
---|
156 | typedef QHash<QString, QDBusMetaObject* > MetaObjectHash;
|
---|
157 | typedef QHash<QByteArray, int> MatchRefCountHash;
|
---|
158 |
|
---|
159 | struct WatchedServiceData {
|
---|
160 | WatchedServiceData() : refcount(0) {}
|
---|
161 | QString owner;
|
---|
162 | int refcount;
|
---|
163 | };
|
---|
164 | typedef QHash<QString, WatchedServiceData> WatchedServicesHash;
|
---|
165 |
|
---|
166 | public:
|
---|
167 | // public methods are entry points from other objects
|
---|
168 | explicit QDBusConnectionPrivate(QObject *parent = 0);
|
---|
169 | ~QDBusConnectionPrivate();
|
---|
170 | void deleteYourself();
|
---|
171 |
|
---|
172 | void setBusService(const QDBusConnection &connection);
|
---|
173 | void setPeer(DBusConnection *connection, const QDBusErrorInternal &error);
|
---|
174 | void setConnection(DBusConnection *connection, const QDBusErrorInternal &error);
|
---|
175 | void setServer(DBusServer *server, const QDBusErrorInternal &error);
|
---|
176 | void closeConnection();
|
---|
177 |
|
---|
178 | QString getNameOwner(const QString &service);
|
---|
179 |
|
---|
180 | int send(const QDBusMessage &message);
|
---|
181 | QDBusMessage sendWithReply(const QDBusMessage &message, int mode, int timeout = -1);
|
---|
182 | QDBusMessage sendWithReplyLocal(const QDBusMessage &message);
|
---|
183 | QDBusPendingCallPrivate *sendWithReplyAsync(const QDBusMessage &message, int timeout = -1);
|
---|
184 | int sendWithReplyAsync(const QDBusMessage &message, QObject *receiver,
|
---|
185 | const char *returnMethod, const char *errorMethod, int timeout = -1);
|
---|
186 | bool connectSignal(const QString &service, const QString &path, const QString& interface,
|
---|
187 | const QString &name, const QStringList &argumentMatch, const QString &signature,
|
---|
188 | QObject *receiver, const char *slot);
|
---|
189 | void connectSignal(const QString &key, const SignalHook &hook);
|
---|
190 | SignalHookHash::Iterator disconnectSignal(SignalHookHash::Iterator &it);
|
---|
191 | bool disconnectSignal(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);
|
---|
194 | void registerObject(const ObjectTreeNode *node);
|
---|
195 | void connectRelay(const QString &service,
|
---|
196 | const QString &path, const QString &interface,
|
---|
197 | QDBusAbstractInterface *receiver, const char *signal);
|
---|
198 | void disconnectRelay(const QString &service,
|
---|
199 | const QString &path, const QString &interface,
|
---|
200 | QDBusAbstractInterface *receiver, const char *signal);
|
---|
201 |
|
---|
202 | bool handleMessage(const QDBusMessage &msg);
|
---|
203 | void waitForFinished(QDBusPendingCallPrivate *pcall);
|
---|
204 |
|
---|
205 | QDBusMetaObject *findMetaObject(const QString &service, const QString &path,
|
---|
206 | const QString &interface, QDBusError &error);
|
---|
207 |
|
---|
208 | void postEventToThread(int action, QObject *target, QEvent *event);
|
---|
209 |
|
---|
210 | inline void serverConnection(const QDBusConnection &connection)
|
---|
211 | { emit newServerConnection(connection); }
|
---|
212 |
|
---|
213 | private:
|
---|
214 | void checkThread();
|
---|
215 | bool handleError(const QDBusErrorInternal &error);
|
---|
216 |
|
---|
217 | void handleSignal(const QString &key, const QDBusMessage &msg);
|
---|
218 | void handleSignal(const QDBusMessage &msg);
|
---|
219 | void handleObjectCall(const QDBusMessage &message);
|
---|
220 |
|
---|
221 | void activateSignal(const SignalHook& hook, const QDBusMessage &msg);
|
---|
222 | void activateObject(ObjectTreeNode &node, const QDBusMessage &msg, int pathStartPos);
|
---|
223 | bool activateInternalFilters(const ObjectTreeNode &node, const QDBusMessage &msg);
|
---|
224 | bool activateCall(QObject *object, int flags, const QDBusMessage &msg);
|
---|
225 |
|
---|
226 | void sendError(const QDBusMessage &msg, QDBusError::ErrorType code);
|
---|
227 | void deliverCall(QObject *object, int flags, const QDBusMessage &msg,
|
---|
228 | const QList<int> &metaTypes, int slotIdx);
|
---|
229 |
|
---|
230 | bool isServiceRegisteredByThread(const QString &serviceName) const;
|
---|
231 |
|
---|
232 | QString getNameOwnerNoCache(const QString &service);
|
---|
233 |
|
---|
234 | protected:
|
---|
235 | void customEvent(QEvent *e);
|
---|
236 | void timerEvent(QTimerEvent *e);
|
---|
237 |
|
---|
238 | public slots:
|
---|
239 | // public slots
|
---|
240 | void doDispatch();
|
---|
241 | void socketRead(int);
|
---|
242 | void socketWrite(int);
|
---|
243 | void objectDestroyed(QObject *o);
|
---|
244 | void relaySignal(QObject *obj, const QMetaObject *, int signalId, const QVariantList &args);
|
---|
245 | void _q_serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner);
|
---|
246 | void registerService(const QString &serviceName);
|
---|
247 | void unregisterService(const QString &serviceName);
|
---|
248 |
|
---|
249 | signals:
|
---|
250 | void serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner);
|
---|
251 | void callWithCallbackFailed(const QDBusError &error, const QDBusMessage &message);
|
---|
252 | void newServerConnection(const QDBusConnection &connection);
|
---|
253 |
|
---|
254 | public:
|
---|
255 | QAtomicInt ref;
|
---|
256 | QString name; // this connection's name
|
---|
257 | QString baseService; // this connection's base service
|
---|
258 |
|
---|
259 | ConnectionMode mode;
|
---|
260 |
|
---|
261 | // members accessed in unlocked mode (except for deletion)
|
---|
262 | // connection and server provide their own locking mechanisms
|
---|
263 | // busService doesn't have state to be changed
|
---|
264 | DBusConnection *connection;
|
---|
265 | DBusServer *server;
|
---|
266 | QDBusConnectionInterface *busService;
|
---|
267 |
|
---|
268 | // watchers and timeouts are accessed from any thread
|
---|
269 | // but the corresponding timer and QSocketNotifier must be handled
|
---|
270 | // only in the object's thread
|
---|
271 | QMutex watchAndTimeoutLock;
|
---|
272 | WatcherHash watchers;
|
---|
273 | TimeoutHash timeouts;
|
---|
274 | PendingTimeoutList timeoutsPendingAdd;
|
---|
275 |
|
---|
276 | // members accessed through a lock
|
---|
277 | QMutex dispatchLock;
|
---|
278 | QReadWriteLock lock;
|
---|
279 | QDBusError lastError;
|
---|
280 |
|
---|
281 | QStringList serviceNames;
|
---|
282 | WatchedServicesHash watchedServices;
|
---|
283 | SignalHookHash signalHooks;
|
---|
284 | MatchRefCountHash matchRefCounts;
|
---|
285 | ObjectTreeNode rootNode;
|
---|
286 | MetaObjectHash cachedMetaObjects;
|
---|
287 |
|
---|
288 | QMutex callDeliveryMutex;
|
---|
289 | QDBusCallDeliveryEvent *callDeliveryState; // protected by the callDeliveryMutex mutex
|
---|
290 |
|
---|
291 | public:
|
---|
292 | // static methods
|
---|
293 | static int findSlot(QObject *obj, const QByteArray &normalizedName, QList<int>& params);
|
---|
294 | static bool prepareHook(QDBusConnectionPrivate::SignalHook &hook, QString &key,
|
---|
295 | const QString &service,
|
---|
296 | const QString &path, const QString &interface, const QString &name,
|
---|
297 | const QStringList &argMatch,
|
---|
298 | QObject *receiver, const char *signal, int minMIdx,
|
---|
299 | bool buildSignature);
|
---|
300 | static DBusHandlerResult messageFilter(DBusConnection *, DBusMessage *, void *);
|
---|
301 | static QDBusCallDeliveryEvent *prepareReply(QDBusConnectionPrivate *target, QObject *object,
|
---|
302 | int idx, const QList<int> &metaTypes,
|
---|
303 | const QDBusMessage &msg);
|
---|
304 | static void processFinishedCall(QDBusPendingCallPrivate *call);
|
---|
305 |
|
---|
306 | static QDBusConnectionPrivate *d(const QDBusConnection& q) { return q.d; }
|
---|
307 | static QDBusConnection q(QDBusConnectionPrivate *connection) { return QDBusConnection(connection); }
|
---|
308 |
|
---|
309 | static void setSender(const QDBusConnectionPrivate *s);
|
---|
310 | static void setConnection(const QString &name, QDBusConnectionPrivate *c);
|
---|
311 |
|
---|
312 | friend class QDBusActivateObjectEvent;
|
---|
313 | friend class QDBusCallDeliveryEvent;
|
---|
314 | };
|
---|
315 |
|
---|
316 | // in qdbusmisc.cpp
|
---|
317 | extern int qDBusParametersForMethod(const QMetaMethod &mm, QList<int>& metaTypes);
|
---|
318 | extern int qDBusNameToTypeId(const char *name);
|
---|
319 | extern bool qDBusCheckAsyncTag(const char *tag);
|
---|
320 | extern bool qDBusInterfaceInObject(QObject *obj, const QString &interface_name);
|
---|
321 | extern QString qDBusInterfaceFromMetaObject(const QMetaObject *mo);
|
---|
322 |
|
---|
323 | // in qdbusinternalfilters.cpp
|
---|
324 | extern QString qDBusIntrospectObject(const QDBusConnectionPrivate::ObjectTreeNode &node);
|
---|
325 | extern QDBusMessage qDBusPropertyGet(const QDBusConnectionPrivate::ObjectTreeNode &node,
|
---|
326 | const QDBusMessage &msg);
|
---|
327 | extern QDBusMessage qDBusPropertySet(const QDBusConnectionPrivate::ObjectTreeNode &node,
|
---|
328 | const QDBusMessage &msg);
|
---|
329 | extern QDBusMessage qDBusPropertyGetAll(const QDBusConnectionPrivate::ObjectTreeNode &node,
|
---|
330 | const QDBusMessage &msg);
|
---|
331 |
|
---|
332 | QT_END_NAMESPACE
|
---|
333 |
|
---|
334 | #endif
|
---|