Debian Codegraph @

online-accounts-service/src/mir-helper.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-service
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 3, as published
10 * by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranties of
14 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <online-accounts-common/debug.h>
22 #include "mir-helper.h"
23
24 #include <mir_toolkit/mir_client_library.h>
25 #include <mir_toolkit/mir_prompt_session.h>
26
27 #include <QHash>
28 #include <QStandardPaths>
29 #include <QWeakPointer>
30
31 using namespace OnlineAccountsUi;
32
33 namespace OnlineAccountsUi {
34
35 static MirHelper *m_instance = 0;
36
37 class PromptSessionPrivate
38 {
39 public:
40 inline PromptSessionPrivate(MirPromptSession *session, pid_t initiatorPid);
41 inline ~PromptSessionPrivate();
42
43 void emitFinished() { Q_EMIT q_ptr->finished(); }
44
45 MirPromptSession *m_mirSession;
46 pid_t m_initiatorPid;
47 mutable PromptSession *q_ptr;
48 };
49
50 class MirHelperPrivate: public QObject
51 {
52 Q_OBJECT
53 Q_DECLARE_PUBLIC(MirHelper)
54
55 public:
56 inline MirHelperPrivate(MirHelper *helper);
57 inline ~MirHelperPrivate();
58
59 PromptSession *createPromptSession(pid_t initiatorPid);
60 void onSessionStopped(MirPromptSession *mirSession);
61
62 private:
63 friend class PromptSession;
64 MirConnection *m_connection;
65 QHash<pid_t,QWeakPointer<PromptSession> > m_sessions;
66 mutable MirHelper *q_ptr;
67 };
68
69 } // namespace
70
71 PromptSessionPrivate::PromptSessionPrivate(MirPromptSession *session,
72 pid_t initiatorPid):
73 m_mirSession(session),
74 m_initiatorPid(initiatorPid)
75 {
76 }
77
78 PromptSessionPrivate::~PromptSessionPrivate()
79 {
80 mir_prompt_session_release_sync(m_mirSession);
81 m_mirSession = 0;
82 }
83
84 PromptSession::PromptSession(PromptSessionPrivate *priv):
85 d_ptr(priv)
86 {
87 priv->q_ptr = this;
88 }
89
90 PromptSession::~PromptSession()
91 {
92 Q_D(PromptSession);
93 MirHelperPrivate *helperPrivate = MirHelper::instance()->d_ptr;
94 helperPrivate->m_sessions.remove(d->m_initiatorPid);
95 delete d_ptr;
96 }
97
98 QString PromptSession::requestSocket()
99 {
100 Q_D(PromptSession);
101
102 int fd = 0;
103 int n_fds =
104 mir_prompt_session_new_fds_for_prompt_providers_sync(d->m_mirSession,
105 1, &fd);
106 if (n_fds == 1) {
107 return QString("fd://%1").arg(fd);
108 } else {
109 return QString();
110 }
111 }
112
113 MirHelperPrivate::MirHelperPrivate(MirHelper *helper):
114 QObject(helper),
115 q_ptr(helper)
116 {
117 QString mirSocket =
118 QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) +
119 "/mir_socket_trusted";
120 m_connection = mir_connect_sync(mirSocket.toUtf8().constData(),
121 "lomiri-online-accounts-service");
122 if (Q_UNLIKELY(!mir_connection_is_valid(m_connection))) {
123 qWarning() << "Invalid Mir connection:" <<
124 mir_connection_get_error_message(m_connection);
125 return;
126 }
127 }
128
129 MirHelperPrivate::~MirHelperPrivate()
130 {
131 if (m_connection) {
132 mir_connection_release(m_connection);
133 m_connection = 0;
134 }
135 }
136
137 static void session_event_callback(MirPromptSession *mirSession,
138 MirPromptSessionState state,
139 void *self)
140 {
141 MirHelperPrivate *helper = reinterpret_cast<MirHelperPrivate*>(self);
142 DEBUG() << "Prompt Session state updated to" << state;
143 if (state == mir_prompt_session_state_stopped) {
144 helper->onSessionStopped(mirSession);
145 }
146 }
147
148 void MirHelperPrivate::onSessionStopped(MirPromptSession *mirSession)
149 {
150 Q_FOREACH(PromptSessionP session, m_sessions) {
151 if (mirSession == session->d_ptr->m_mirSession) {
152 session->d_ptr->emitFinished();
153 }
154 }
155 }
156
157 PromptSession *MirHelperPrivate::createPromptSession(pid_t initiatorPid)
158 {
159 if (Q_UNLIKELY(!m_connection)) return 0;
160
161 MirPromptSession *mirSession =
162 mir_connection_create_prompt_session_sync(m_connection,
163 initiatorPid,
164 session_event_callback,
165 this);
166 if (!mirSession) return 0;
167
168 if (Q_UNLIKELY(!mir_prompt_session_is_valid(mirSession))) {
169 qWarning() << "Invalid prompt session:" <<
170 mir_prompt_session_error_message(mirSession);
171 return 0;
172 }
173
174 return new PromptSession(new PromptSessionPrivate(mirSession, initiatorPid));
175 }
176
177 MirHelper::MirHelper(QObject *parent):
178 QObject(parent),
179 d_ptr(new MirHelperPrivate(this))
180 {
181 }
182
183 MirHelper::~MirHelper()
184 {
185 m_instance = 0;
186 }
187
188 MirHelper *MirHelper::instance()
189 {
190 if (!m_instance) {
191 m_instance = new MirHelper;
192 }
193 return m_instance;
194 }
195
196 PromptSessionP MirHelper::createPromptSession(pid_t initiatorPid)
197 {
198 Q_D(MirHelper);
199 PromptSessionP session = d->m_sessions.value(initiatorPid);
200 if (session.isNull()) {
201 PromptSession *s = d->createPromptSession(initiatorPid);
202 if (s) {
203 session = PromptSessionP(s);
204 d->m_sessions.insert(initiatorPid, session);
205 }
206 }
207 return session;
208 }
209
210 #include "mir-helper.moc"