Debian Codegraph @

online-accounts-common/ipc.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2014 Canonical Ltd.
3 *
4 * Contact: Alberto Mardegan <[email protected]>
5 *
6 * This file is part of lomiri-online-accounts-ui and lomiri-online-accounts-
7 * service
8 *
9 * This program is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 3, as published
11 * by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranties of
15 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "ipc.h"
23
24 #include <QByteArray>
25 #include <QFile>
26 #include <QIODevice>
27 #include <QSocketNotifier>
28
29 using namespace OnlineAccountsUi;
30
31 static const QByteArray welcomeMessage = "OAUinitIPC";
32
33 namespace OnlineAccountsUi {
34
35 class IpcPrivate: public QObject
36 {
37 Q_OBJECT
38 Q_DECLARE_PUBLIC(Ipc)
39
40 public:
41 inline IpcPrivate(Ipc *ipc);
42 ~IpcPrivate() {};
43
44 void setChannels(QIODevice *readChannel, QIODevice *writeChannel);
45
46 private Q_SLOTS:
47 void onReadyRead();
48
49 private:
50 bool waitWelcomeMessage();
51
52 private:
53 QIODevice *m_readChannel;
54 QIODevice *m_writeChannel;
55 int m_expectedLength;
56 bool m_gotWelcomeMessage;
57 QByteArray m_readBuffer;
58 mutable Ipc *q_ptr;
59 };
60
61 }; // namespace
62
63 IpcPrivate::IpcPrivate(Ipc *ipc):
64 QObject(ipc),
65 m_readChannel(0),
66 m_writeChannel(0),
67 m_expectedLength(0),
68 m_gotWelcomeMessage(false),
69 q_ptr(ipc)
70 {
71 }
72
73 void IpcPrivate::setChannels(QIODevice *readChannel, QIODevice *writeChannel)
74 {
75 m_readChannel = readChannel;
76 m_writeChannel = writeChannel;
77 QObject::connect(m_readChannel, SIGNAL(readyRead()),
78 this, SLOT(onReadyRead()));
79 /* QFile need special handling */
80 QFile *file = qobject_cast<QFile*>(m_readChannel);
81 if (file != 0) {
82 QSocketNotifier *notifier = new QSocketNotifier(file->handle(),
83 QSocketNotifier::Read,
84 this);
85 QObject::connect(notifier, SIGNAL(activated(int)),
86 this, SLOT(onReadyRead()));
87 } else {
88 /* If the read channel is not a QFile, it means it's not the standard
89 * input, therefore we won't have the need to wait for the welcome
90 * message. */
91 m_gotWelcomeMessage = true;
92 }
93 onReadyRead();
94
95 file = qobject_cast<QFile*>(m_writeChannel);
96 if (file != 0) {
97 m_writeChannel->write(welcomeMessage);
98 }
99 }
100
101 void IpcPrivate::onReadyRead()
102 {
103 Q_Q(Ipc);
104
105 while (true) {
106 if (m_expectedLength == 0) {
107 /* We are beginning a new read */
108
109 /* skip all noise */
110 if (!waitWelcomeMessage()) break;
111
112 int length;
113 int bytesRead = m_readChannel->read((char *)&length,
114 sizeof(length));
115 if (bytesRead < int(sizeof(length))) break;
116 m_expectedLength = length;
117 m_readBuffer.clear();
118 }
119
120 int neededBytes = m_expectedLength - m_readBuffer.length();
121 QByteArray buffer = m_readChannel->read(neededBytes);
122 m_readBuffer += buffer;
123 if (buffer.length() < neededBytes) break;
124 if (m_readBuffer.length() == m_expectedLength) {
125 Q_EMIT q->dataReady(m_readBuffer);
126 m_expectedLength = 0;
127 }
128 }
129 }
130
131 bool IpcPrivate::waitWelcomeMessage()
132 {
133 if (m_gotWelcomeMessage) return true;
134
135 /* All Qt applications on the Nexus 4 write some dust to stdout when
136 * starting. So, skip all input until a well-defined welcome message is
137 * found */
138
139 QByteArray buffer;
140 int startCheckIndex = 0;
141 do {
142 buffer = m_readChannel->peek(40);
143 int found = buffer.indexOf(welcomeMessage, startCheckIndex);
144 int skip = (found >= 0) ? found : buffer.length() - welcomeMessage.length();
145 if (found >= 0) {
146 buffer = m_readChannel->read(skip + welcomeMessage.length());
147 m_gotWelcomeMessage = true;
148 return true;
149 }
150 if (skip > 0) {
151 buffer = m_readChannel->read(skip);
152 } else {
153 buffer.clear();
154 }
155 } while (!buffer.isEmpty());
156
157 return false;
158 }
159
160 Ipc::Ipc(QObject *parent):
161 QObject(parent),
162 d_ptr(new IpcPrivate(this))
163 {
164 }
165
166 Ipc::~Ipc()
167 {
168 }
169
170 void Ipc::setChannels(QIODevice *readChannel, QIODevice *writeChannel)
171 {
172 Q_D(Ipc);
173 d->setChannels(readChannel, writeChannel);
174 }
175
176 void Ipc::write(const QByteArray &data)
177 {
178 Q_D(Ipc);
179 int length = data.count();
180 d->m_writeChannel->write((char *)&length, sizeof(length));
181 d->m_writeChannel->write(data);
182 d->m_writeChannel->waitForBytesWritten(-1);
183 }
184
185 #include "ipc.moc"