Merge lp:~dandrader/ubuntu-keyboard/improve_kbd_info_ipc into lp:ubuntu-keyboard

Proposed by Daniel d'Andrada
Status: Merged
Approved by: Michał Sawicz
Approved revision: 75
Merged at revision: 75
Proposed branch: lp:~dandrader/ubuntu-keyboard/improve_kbd_info_ipc
Merge into: lp:ubuntu-keyboard
Diff against target: 169 lines (+79/-23)
2 files modified
src/plugin/ubuntuapplicationapiwrapper.cpp (+73/-22)
src/plugin/ubuntuapplicationapiwrapper.h (+6/-1)
To merge this branch: bzr merge lp:~dandrader/ubuntu-keyboard/improve_kbd_info_ipc
Reviewer Review Type Date Requested Status
Michał Sawicz Approve
Günter Schwann (community) Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+190418@code.launchpad.net

Commit message

Various improvements to ubuntu-keyboard-info socket server

Make it more robust

Description of the change

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Michał Sawicz (saviq) wrote :

Only thing - please put the socket in mode 600 in $XDG_RUNTIME_DIR, as mentioned in https://code.launchpad.net/~dandrader/unity-mir/improve_osk_ipc/+merge/190417/comments/437227

review: Needs Fixing
Revision history for this message
Günter Schwann (schwann) wrote :

tests/dynamic-layout does not compile:
/home/phablet/ubuntu-keyboard/tests/dynamic-layout/../../src/plugin/libubuntu-keyboard-plugin.so: undefined reference to `vtable for UbuntuApplicationApiWrapper'

And when starting maliit-server, I get a crash:
WARNING: bool MIMPluginManagerPrivate::loadPlugin(const QDir&, const QString&) Error loading plugin from "/usr/lib/maliit/plugins/libubuntu-keyboard-plugin.so" "Cannot load library /usr/lib/maliit/plugins/libubuntu-keyboard-plugin.so: (/usr/lib/maliit/plugins/libubuntu-keyboard-plugin.so: undefined symbol: _ZTV27UbuntuApplicationApiWrapper)"

review: Needs Fixing
Revision history for this message
Günter Schwann (schwann) wrote :

My build and run error was a problem in the build script - solved.
Code does, apart from the comment from saviq, looking good.

review: Approve
75. By Daniel d'Andrada

Use XDG_RUNTIME_DIR instead of /tmp

Revision history for this message
Daniel d'Andrada (dandrader) wrote :

> Only thing - please put the socket in mode 600 in $XDG_RUNTIME_DIR, as
> mentioned in https://code.launchpad.net/~dandrader/unity-
> mir/improve_osk_ipc/+merge/190417/comments/437227

Done.

Revision history for this message
Michał Sawicz (saviq) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/plugin/ubuntuapplicationapiwrapper.cpp'
2--- src/plugin/ubuntuapplicationapiwrapper.cpp 2013-10-09 15:28:52 +0000
3+++ src/plugin/ubuntuapplicationapiwrapper.cpp 2013-10-11 15:34:57 +0000
4@@ -25,6 +25,8 @@
5 #define HAVE_UBUNTU_PLATFORM_API
6 #endif
7
8+#include <QDir>
9+#include <QFile>
10 #include <QtGlobal>
11 #include <QByteArray>
12
13@@ -41,14 +43,34 @@
14 }
15
16 if (m_runningOnMir) {
17- connect(&m_localServer, &QLocalServer::newConnection,
18- this, &UbuntuApplicationApiWrapper::onNewConnection);
19- bool ok = m_localServer.listen(gServerName);
20- if (!ok) {
21- qWarning() << "UbuntuApplicationApiWrapper: failed to listen for connections on"
22- << gServerName;
23+ startLocalServer();
24+ }
25+}
26+
27+void UbuntuApplicationApiWrapper::startLocalServer()
28+{
29+ QString socketFilePath = buildSocketFilePath();
30+
31+ {
32+ QFile socketFile(socketFilePath);
33+ if (socketFile.exists()) {
34+ // It's a leftover from a previous run of maliit-server. let's get rid of it.
35+ // The other possibility is that another instance of maliit-server is already
36+ // running, but that's absolutely unsupported.
37+ if (!socketFile.remove()) {
38+ qWarning() << "UbuntuApplicationApiWrapper: unable to remove pre-existing"
39+ << socketFilePath ;
40+ }
41 }
42 }
43+
44+ connect(&m_localServer, &QLocalServer::newConnection,
45+ this, &UbuntuApplicationApiWrapper::onNewConnection);
46+ bool ok = m_localServer.listen(socketFilePath);
47+ if (!ok) {
48+ qWarning() << "UbuntuApplicationApiWrapper: failed to listen for connections on"
49+ << socketFilePath;
50+ }
51 }
52
53 void UbuntuApplicationApiWrapper::reportOSKVisible(const int x, const int y, const int width, const int height)
54@@ -96,6 +118,13 @@
55 struct SharedInfo sharedInfo;
56 sharedInfo.keyboardWidth = width;
57 sharedInfo.keyboardHeight = height;
58+
59+ if (sharedInfo == m_lastInfoShared) {
60+ // do not flood our listener with redundant info. This also means that
61+ // were are getting called unnecessarily
62+ return;
63+ }
64+
65 const qint64 sharedInfoSize = sizeof(struct SharedInfo);
66 qint64 bytesWritten = m_clientConnection->write(reinterpret_cast<char *>(&sharedInfo),
67 sharedInfoSize);
68@@ -108,32 +137,54 @@
69 qWarning() << "UbuntuApplicationApiWrapper: tried to write" << sharedInfoSize << "bytes"
70 "but only" << bytesWritten << "went through";
71 }
72+
73+ m_lastInfoShared = sharedInfo;
74 }
75
76 void UbuntuApplicationApiWrapper::onNewConnection()
77 {
78- if (m_clientConnection)
79+ QLocalSocket *newConnection = m_localServer.nextPendingConnection();
80+
81+ if (m_clientConnection) {
82+ qWarning() << "UbuntuApplicationApiWrapper: Refusing incoming connection as we "
83+ "already have an active one.";
84+ delete newConnection;
85 return; // ignore it. for simplicity we care to serve only one client (unity8-mir)
86+ }
87+ m_clientConnection = newConnection;
88+ m_lastInfoShared.reset();
89
90- m_clientConnection = m_localServer.nextPendingConnection();
91 connect(m_clientConnection, &QLocalSocket::disconnected,
92 this, &UbuntuApplicationApiWrapper::onClientDisconnected);
93-
94- typedef void (QLocalSocket::*MemberFunctionType)(QLocalSocket::LocalSocketError);
95- MemberFunctionType funcPointer = &QLocalSocket::error;
96- connect(m_clientConnection, funcPointer,
97- this, &UbuntuApplicationApiWrapper::onClientError);
98 }
99
100 void UbuntuApplicationApiWrapper::onClientDisconnected()
101 {
102- delete m_clientConnection;
103- m_clientConnection = 0;
104-}
105-
106-void UbuntuApplicationApiWrapper::onClientError(QLocalSocket::LocalSocketError socketError)
107-{
108- Q_UNUSED(socketError);
109- delete m_clientConnection;
110- m_clientConnection = 0;
111+ m_clientConnection->deleteLater();
112+ m_clientConnection = 0;
113+}
114+
115+QString UbuntuApplicationApiWrapper::buildSocketFilePath() const
116+{
117+ char *xdgRuntimeDir = getenv("XDG_RUNTIME_DIR");
118+
119+ if (xdgRuntimeDir) {
120+ return QDir(xdgRuntimeDir).filePath(gServerName);
121+ } else {
122+ return QDir("/tmp").filePath(gServerName);
123+ }
124+}
125+
126+// ------------------------------- SharedInfo ----------------------------
127+
128+bool UbuntuApplicationApiWrapper::SharedInfo::operator ==(const struct SharedInfo &other)
129+{
130+ return keyboardWidth == other.keyboardWidth
131+ && keyboardHeight == other.keyboardHeight;
132+}
133+
134+void UbuntuApplicationApiWrapper::SharedInfo::reset()
135+{
136+ keyboardWidth = 0;
137+ keyboardHeight = 0;
138 }
139
140=== modified file 'src/plugin/ubuntuapplicationapiwrapper.h'
141--- src/plugin/ubuntuapplicationapiwrapper.h 2013-10-09 15:28:52 +0000
142+++ src/plugin/ubuntuapplicationapiwrapper.h 2013-10-11 15:34:57 +0000
143@@ -44,7 +44,6 @@
144 private Q_SLOTS:
145 void onNewConnection();
146 void onClientDisconnected();
147- void onClientError(QLocalSocket::LocalSocketError socketError);
148
149 private:
150 // NB! Must match the definition in unity-mir. Not worth creating a shared header
151@@ -52,12 +51,18 @@
152 struct SharedInfo {
153 qint32 keyboardWidth;
154 qint32 keyboardHeight;
155+
156+ bool operator ==(const struct SharedInfo &other);
157+ void reset();
158 };
159 void sendInfoToClientConnection(int width, int height);
160+ void startLocalServer();
161+ QString buildSocketFilePath() const;
162
163 bool m_runningOnMir;
164 QLocalServer m_localServer;
165 QLocalSocket *m_clientConnection;
166+ struct SharedInfo m_lastInfoShared;
167 };
168
169 #endif // UBUNTUAPPLICATIONAPIWRAPPER_H

Subscribers

People subscribed via source and target branches