source: trunk/src/dbus/qdbusconnection_p.h@ 754

Last change on this file since 754 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 12.9 KB
Line 
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
74QT_BEGIN_NAMESPACE
75
76class QDBusMessage;
77class QSocketNotifier;
78class QTimerEvent;
79class QDBusObjectPrivate;
80class QDBusCallDeliveryEvent;
81class QDBusActivateObjectEvent;
82class QMetaMethod;
83class QDBusInterfacePrivate;
84struct QDBusMetaObject;
85class QDBusAbstractInterface;
86class QDBusConnectionInterface;
87class QDBusPendingCallPrivate;
88
89class QDBusErrorInternal
90{
91 mutable DBusError error;
92 Q_DISABLE_COPY(QDBusErrorInternal)
93public:
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
104class QDBusConnectionPrivate: public QObject
105{
106 Q_OBJECT
107public:
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
149public:
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
166public:
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
213private:
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
234protected:
235 void customEvent(QEvent *e);
236 void timerEvent(QTimerEvent *e);
237
238public 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
249signals:
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
254public:
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
291public:
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
317extern int qDBusParametersForMethod(const QMetaMethod &mm, QList<int>& metaTypes);
318extern int qDBusNameToTypeId(const char *name);
319extern bool qDBusCheckAsyncTag(const char *tag);
320extern bool qDBusInterfaceInObject(QObject *obj, const QString &interface_name);
321extern QString qDBusInterfaceFromMetaObject(const QMetaObject *mo);
322
323// in qdbusinternalfilters.cpp
324extern QString qDBusIntrospectObject(const QDBusConnectionPrivate::ObjectTreeNode &node);
325extern QDBusMessage qDBusPropertyGet(const QDBusConnectionPrivate::ObjectTreeNode &node,
326 const QDBusMessage &msg);
327extern QDBusMessage qDBusPropertySet(const QDBusConnectionPrivate::ObjectTreeNode &node,
328 const QDBusMessage &msg);
329extern QDBusMessage qDBusPropertyGetAll(const QDBusConnectionPrivate::ObjectTreeNode &node,
330 const QDBusMessage &msg);
331
332QT_END_NAMESPACE
333
334#endif
Note: See TracBrowser for help on using the repository browser.