1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2015 The Qt Company Ltd and/or its subsidiary(-ies). |
4 | ** Contact: http://www.qt-project.org/legal |
5 | ** |
6 | ** This file is part of the QtSystems module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL21$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at http://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 2.1 or version 3 as published by the Free |
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and |
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the |
22 | ** following information to ensure the GNU Lesser General Public License |
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and |
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
25 | ** |
26 | ** As a special exception, The Qt Company gives you certain additional |
27 | ** rights. These rights are described in The Qt Company LGPL Exception |
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
29 | ** |
30 | ** $QT_END_LICENSE$ |
31 | ** |
32 | ****************************************************************************/ |
33 | |
34 | #ifndef QREMOTESERVICEREGISTER_DBUS_P_H |
35 | #define QREMOTESERVICEREGISTER_DBUS_P_H |
36 | |
37 | // |
38 | // W A R N I N G |
39 | // ------------- |
40 | // |
41 | // This file is not part of the Qt API. It exists purely as an |
42 | // implementation detail. This header file may change from version to |
43 | // version without notice, or even be removed. |
44 | // |
45 | // We mean it. |
46 | // |
47 | |
48 | #include <QUuid> |
49 | #include <QtDBus/QtDBus> |
50 | |
51 | #include "qremoteserviceregister.h" |
52 | #include "instancemanager_p.h" |
53 | #include "qserviceinterfacedescriptor.h" |
54 | #include "ipcendpoint_p.h" |
55 | #include "objectendpoint_dbus_p.h" |
56 | |
57 | QT_BEGIN_NAMESPACE |
58 | |
59 | #define SERVER 0 |
60 | #define CLIENT 1 |
61 | |
62 | class ObjectEndPoint; |
63 | |
64 | class DBusSession: public QObject, protected QDBusContext |
65 | { |
66 | Q_OBJECT |
67 | public: |
68 | DBusSession(QObject* parent = 0) |
69 | : QObject(parent) |
70 | { |
71 | m_accept = true; |
72 | } |
73 | ~DBusSession() {} |
74 | |
75 | public Q_SLOTS: |
76 | QByteArray writePackage(const QByteArray &package, int type, const QString &id) { |
77 | |
78 | QDataStream data(package); |
79 | QServicePackage pack; |
80 | data >> pack; |
81 | |
82 | if (type == CLIENT && pack.d->packageType == 0) { |
83 | // Use the client DBus connection as the Id |
84 | QDBusReply<QString> reply = |
85 | connection().interface()->serviceOwner(name: message().service()); |
86 | QString clientId = reply.value(); |
87 | pack.d->payload = QVariant(clientId); |
88 | |
89 | QByteArray block; |
90 | QDataStream out(&block, QIODevice::WriteOnly); |
91 | out.setVersion(QDataStream::Qt_4_6); |
92 | out << pack; |
93 | |
94 | emit packageReceived(package: block, type, id, pid: -1, uid: -1); |
95 | return block; |
96 | |
97 | } |
98 | |
99 | if (!m_accept) |
100 | return QByteArray(); |
101 | |
102 | int pid = connection().interface()->servicePid(serviceName: message().service()); |
103 | int uid = connection().interface()->serviceUid(serviceName: message().service()); |
104 | |
105 | emit packageReceived(package, type, id, pid, uid); |
106 | return package; |
107 | } |
108 | |
109 | bool processIncoming() { |
110 | int pid = connection().interface()->servicePid(serviceName: message().service()); |
111 | int uid = connection().interface()->serviceUid(serviceName: message().service()); |
112 | emit newConnection(pid, uid); |
113 | return m_accept; |
114 | } |
115 | |
116 | void acceptIncoming(bool accept) { |
117 | m_accept = accept; |
118 | } |
119 | |
120 | void closeIncoming(const QString& instanceId) { |
121 | QDBusReply<QString> reply = |
122 | connection().interface()->serviceOwner(name: message().service()); |
123 | const QString& clientId = reply.value(); |
124 | emit closeConnection(clientId, instanceId); |
125 | } |
126 | |
127 | Q_SIGNALS: |
128 | void packageReceived(const QByteArray &package, int type, const QString &id, int pid, int uid); |
129 | void newConnection(int pid, int uid); |
130 | void closeConnection(const QString& clientId, const QString& instanceId); |
131 | |
132 | private: |
133 | bool m_accept; |
134 | }; |
135 | |
136 | |
137 | class QRemoteServiceRegisterDBusPrivate: public QRemoteServiceRegisterPrivate |
138 | { |
139 | Q_OBJECT |
140 | public: |
141 | QRemoteServiceRegisterDBusPrivate(QObject* parent); |
142 | ~QRemoteServiceRegisterDBusPrivate(); |
143 | void publishServices(const QString& ident ); |
144 | |
145 | public Q_SLOTS: |
146 | void processIncoming(int pid, int uid); |
147 | |
148 | private: |
149 | bool createServiceEndPoint(const QString& ident); |
150 | |
151 | QList<ObjectEndPoint*> pendingConnections; |
152 | QDBusInterface *iface; |
153 | DBusSession *session; |
154 | }; |
155 | |
156 | QT_END_NAMESPACE |
157 | |
158 | #endif //QREMOTESERVICEREGISTER_DBUS_P_H |
159 | |