Debian Codegraph @

liblomiri-app-launch/jobs-base.h

raw
1 /*
2 * Copyright © 2016 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Ted Gould <[email protected]>
18 */
19
20 #pragma once
21
22 #include "application.h"
23 #include "registry.h"
24 #include "signal-unsubscriber.h"
25 #include "string-util.h"
26
27 #include <core/signal.h>
28 #include <gio/gio.h>
29 #include <map>
30 #include <set>
31
32 namespace lomiri
33 {
34 namespace app_launch
35 {
36 namespace jobs
37 {
38 namespace instance
39 {
40
41 class Base : public Application::Instance
42 {
43 public:
44 Base(const AppID& appId,
45 const std::string& job,
46 const std::string& instance,
47 const std::vector<Application::URL>& urls,
48 const std::shared_ptr<Registry::Impl>& registry);
49 virtual ~Base() = default;
50
51 bool isRunning() override;
52 bool hasPid(pid_t pid) override;
53 void pause() override;
54 void resume() override;
55 void focus() override;
56
57 const std::string& getInstanceId() const
58 {
59 return instance_;
60 }
61
62 const AppID& getAppId() const
63 {
64 return appId_;
65 }
66
67 /* OOM Functions */
68 void setOomAdjustment(const oom::Score score) override;
69 oom::Score getOomAdjustment() override;
70
71 protected:
72 /** Application ID */
73 const AppID appId_;
74 /** Upstart job name */
75 const std::string job_;
76 /** Instance ID environment value, empty if none */
77 const std::string instance_;
78 /** The URLs that this was launched for. Only valid on launched jobs, we
79 should look at perhaps changing that. */
80 std::vector<Application::URL> urls_;
81 /** A link to the registry we're using for connections */
82 std::shared_ptr<Registry::Impl> registry_;
83
84 std::vector<pid_t> forAllPids(std::function<void(pid_t)> eachPid);
85 static void pidListToDbus(const std::shared_ptr<Registry::Impl>& reg,
86 const AppID& appid,
87 const std::string& instanceid,
88 const std::vector<pid_t>& pids,
89 const std::string& signal);
90 static void signalToPid(pid_t pid, int signal);
91 static void oomValueToPid(pid_t pid, const oom::Score oomvalue);
92 static void oomValueToPidHelper(pid_t pid, const oom::Score oomvalue);
93 static std::string pidToOomPath(pid_t pid);
94 static GCharVUPtr urlsToStrv(const std::vector<Application::URL>& urls);
95 };
96
97 } // namespace instance
98
99 namespace manager
100 {
101
102 /** Flag for whether we should include the testing environment variables */
103 enum class launchMode
104 {
105 STANDARD, /**< Standard variable set */
106 TEST /**< Include testing environment vars */
107 };
108
109 class Base
110 {
111 public:
112 Base(const std::shared_ptr<Registry::Impl>& registry);
113 virtual ~Base();
114
115 virtual std::shared_ptr<Application::Instance> launch(
116 const AppID& appId,
117 const std::string& job,
118 const std::string& instance,
119 const std::vector<Application::URL>& urls,
120 launchMode mode,
121 std::function<std::list<std::pair<std::string, std::string>>(void)>& getenv) = 0;
122
123 virtual std::shared_ptr<Application::Instance> existing(const AppID& appId,
124 const std::string& job,
125 const std::string& instance,
126 const std::vector<Application::URL>& urls) = 0;
127
128 virtual std::list<std::shared_ptr<Application>> runningApps();
129 virtual std::list<std::shared_ptr<Helper>> runningHelpers(const Helper::Type& type);
130
131 virtual std::list<std::string> runningAppIds(const std::list<std::string>& jobs) = 0;
132
133 virtual std::vector<std::shared_ptr<instance::Base>> instances(const AppID& appID, const std::string& job) = 0;
134
135 const std::list<std::string>& getAllApplicationJobs() const;
136
137 static std::shared_ptr<Base> determineFactory(const std::shared_ptr<Registry::Impl>& registry);
138
139 /* Signals to apps */
140 virtual core::Signal<const std::shared_ptr<Application>&, const std::shared_ptr<Application::Instance>&>&
141 appStarted();
142 virtual core::Signal<const std::shared_ptr<Application>&, const std::shared_ptr<Application::Instance>&>&
143 appStopped();
144 virtual core::Signal<const std::shared_ptr<Application>&,
145 const std::shared_ptr<Application::Instance>&,
146 Registry::FailureType>&
147 appFailed();
148 virtual core::Signal<const std::shared_ptr<Application>&,
149 const std::shared_ptr<Application::Instance>&,
150 const std::vector<pid_t>&>&
151 appPaused();
152 virtual core::Signal<const std::shared_ptr<Application>&,
153 const std::shared_ptr<Application::Instance>&,
154 const std::vector<pid_t>&>&
155 appResumed();
156 /* Signals to helpers */
157 virtual core::Signal<const std::shared_ptr<Helper>&, const std::shared_ptr<Helper::Instance>&>& helperStarted(
158 Helper::Type type);
159 virtual core::Signal<const std::shared_ptr<Helper>&, const std::shared_ptr<Helper::Instance>&>& helperStopped(
160 Helper::Type type);
161 virtual core::
162 Signal<const std::shared_ptr<Helper>&, const std::shared_ptr<Helper::Instance>&, Registry::FailureType>&
163 helperFailed(Helper::Type type);
164 /* Job signals from implementations */
165 virtual core::Signal<const std::string&, const std::string&, const std::string&>& jobStarted() = 0;
166 virtual core::Signal<const std::string&, const std::string&, const std::string&>& jobStopped() = 0;
167 virtual core::Signal<const std::string&, const std::string&, const std::string&, Registry::FailureType>&
168 jobFailed() = 0;
169
170 /* App manager */
171 virtual void setManager(std::shared_ptr<Registry::Manager> manager);
172 virtual void clearManager();
173
174 protected:
175 /** Accessor function to the registry that ensures we can still
176 get it, which we always should be able to, but in case. */
177 std::shared_ptr<Registry::Impl> getReg()
178 {
179 auto reg = registry_.lock();
180 if (G_UNLIKELY(!reg))
181 {
182 throw std::runtime_error{"Jobs manager lost track of the Registry that owns it"};
183 }
184 return reg;
185 }
186
187 /** Application manager instance */
188 std::shared_ptr<Registry::Manager> manager_;
189
190 private:
191 /** A link to the registry */
192 std::weak_ptr<Registry::Impl> registry_;
193
194 /** A set of all the job names used by applications */
195 std::list<std::string> allApplicationJobs_;
196
197 /** Signal object for applications started */
198 core::Signal<const std::shared_ptr<Application>&, const std::shared_ptr<Application::Instance>&> sig_appStarted;
199 /** Signal object for applications stopped */
200 core::Signal<const std::shared_ptr<Application>&, const std::shared_ptr<Application::Instance>&> sig_appStopped;
201 /** Signal object for applications failed */
202 core::Signal<const std::shared_ptr<Application>&,
203 const std::shared_ptr<Application::Instance>&,
204 Registry::FailureType>
205 sig_appFailed;
206 /** Signal object for applications paused */
207 core::Signal<const std::shared_ptr<Application>&,
208 const std::shared_ptr<Application::Instance>&,
209 const std::vector<pid_t>&>
210 sig_appPaused;
211 /** Signal object for applications resumed */
212 core::Signal<const std::shared_ptr<Application>&,
213 const std::shared_ptr<Application::Instance>&,
214 const std::vector<pid_t>&>
215 sig_appResumed;
216
217 std::map<std::string,
218 std::shared_ptr<core::Signal<const std::shared_ptr<Helper>&, const std::shared_ptr<Helper::Instance>&>>>
219 sig_helpersStarted;
220 std::map<std::string,
221 std::shared_ptr<core::Signal<const std::shared_ptr<Helper>&, const std::shared_ptr<Helper::Instance>&>>>
222 sig_helpersStopped;
223 std::map<std::string,
224 std::shared_ptr<core::Signal<const std::shared_ptr<Helper>&,
225 const std::shared_ptr<Helper::Instance>&,
226 Registry::FailureType>>>
227 sig_helpersFailed;
228
229 ManagedDBusSignalConnection handle_managerSignalFocus{
230 DBusSignalUnsubscriber{}}; /**< GDBus signal watcher handle for app focused signal */
231 ManagedDBusSignalConnection handle_managerSignalResume{
232 DBusSignalUnsubscriber{}}; /**< GDBus signal watcher handle for app resumed signal */
233 ManagedDBusSignalConnection handle_managerSignalStarting{
234 DBusSignalUnsubscriber{}}; /**< GDBus signal watcher handle for app starting signal */
235 ManagedDBusSignalConnection handle_appPaused{
236 DBusSignalUnsubscriber{}}; /**< GDBus signal watcher handle for app paused signal */
237 ManagedDBusSignalConnection handle_appResumed{
238 DBusSignalUnsubscriber{}}; /**< GDBus signal watcher handle for app resumed signal */
239
240 std::once_flag flag_managerSignals; /**< Variable to track to see if signal handlers are installed for the manager
241 signals of focused, resumed and starting */
242 std::once_flag flag_appStarted; /**< Variable to track to see if signal handlers are installed for application
243 started */
244 std::once_flag flag_appStopped; /**< Variable to track to see if signal handlers are installed for application
245 stopped */
246 std::once_flag
247 flag_appFailed; /**< Variable to track to see if signal handlers are installed for application failed */
248 std::once_flag
249 flag_appPaused; /**< Variable to track to see if signal handlers are installed for application paused */
250 std::once_flag flag_appResumed; /**< Variable to track to see if signal handlers are installed for application
251 resumed */
252
253 void pauseEventEmitted(core::Signal<const std::shared_ptr<Application>&,
254 const std::shared_ptr<Application::Instance>&,
255 const std::vector<pid_t>&>& signal,
256 const std::shared_ptr<GVariant>& params,
257 const std::shared_ptr<Registry::Impl>& reg);
258
259 static std::tuple<std::shared_ptr<Application>, std::shared_ptr<Application::Instance>> managerParams(
260 const std::shared_ptr<GVariant>& params, const std::shared_ptr<Registry::Impl>& reg);
261 guint managerSignalHelper(const std::string& signalname,
262 std::function<void(const std::shared_ptr<Registry::Impl>& reg,
263 const std::shared_ptr<Application>& app,
264 const std::shared_ptr<Application::Instance>& instance,
265 const std::shared_ptr<GDBusConnection>&,
266 const std::string&,
267 const std::shared_ptr<GVariant>&)> responsefunc);
268 };
269
270 } // namespace manager
271 } // namespace jobs
272 } // namespace app_launch
273 } // namespace lomiri