Debian Codegraph @

liblomiri-app-launch/application-impl-snap.cpp

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 #include <regex>
21
22 #include "application-impl-snap.h"
23 #include "application-info-desktop.h"
24 #include "registry-impl.h"
25 #include "string-util.h"
26
27 #include <lomiri/util/GlibMemory.h>
28
29 using namespace lomiri::util;
30
31 namespace lomiri
32 {
33 namespace app_launch
34 {
35 namespace app_impls
36 {
37
38 /************************
39 ** Interface Lists
40 ************************/
41
42 /** All the interfaces that use X11 translation layers */
43 const std::set<std::string> X11_INTERFACES{"unity7", "x11"};
44 /** The interface to indicate direct Mir support */
45 const std::string MIR_INTERFACE{"mir"};
46 /** The interface to indicate Lomiri lifecycle support */
47 const std::string LIFECYCLE_INTERFACE{"lomiri"};
48 /** Snappy has more restrictive appnames than everyone else */
49 const std::regex appnameRegex{"^[a-zA-Z0-9](?:-?[a-zA-Z0-9])*$"};
50
51 /************************
52 ** Info support
53 ************************/
54
55 /** Subclassing the desktop info object so that we can override a couple
56 of properties with interface definitions. This may grow as we add more
57 fields to the desktop spec that come from Snappy interfaces. */
58 class SnapInfo : public app_info::Desktop
59 {
60 /** AppID of snap */
61 AppID appId_;
62
63 public:
64 SnapInfo(const AppID& appid,
65 const std::shared_ptr<Registry::Impl>& registry,
66 const Snap::InterfaceInfo& interfaceInfo,
67 const std::string& snapDir)
68 : Desktop(appid,
69 [appid, snapDir]() -> std::shared_ptr<GKeyFile> {
70 /* This is a function to get the keyfile out of the snap using
71 the paths that snappy places things inside the dir. */
72 std::string path = snapDir + "/meta/gui/" + appid.appname.value() + ".desktop";
73 auto keyfile = share_glib(g_key_file_new());
74 GError* error = nullptr;
75 g_key_file_load_from_file(keyfile.get(), path.c_str(), G_KEY_FILE_NONE, &error);
76 if (error != nullptr)
77 {
78 auto perror = unique_glib(error);
79 error = nullptr;
80 throw std::runtime_error("Unable to find keyfile for '" + std::string(appid) + "' at '" +
81 path + "' because: " + perror.get()->message);
82 }
83
84 /* For bad reasons the Icon values in snaps have gotten to be a
85 bit crazy. We're going to try to un-fu-bar a few common patterns
86 here, but eh, we're just encouraging bad behavior */
87 auto iconvalue =
88 unique_gchar(g_key_file_get_string(keyfile.get(), "Desktop Entry", "Icon", nullptr));
89 if (iconvalue)
90 {
91 const gchar* prefix{nullptr};
92 if (g_str_has_prefix(iconvalue.get(), "${SNAP}/"))
93 {
94 /* There isn't environment parsing in desktop file values :-( */
95 prefix = "${SNAP}/";
96 }
97
98 auto currentdir = std::string{"/snap/"} + appid.package.value() + "/current/";
99 if (g_str_has_prefix(iconvalue.get(), currentdir.c_str()))
100 {
101 /* What? Why would we encode the snap path from root in a package
102 format that is supposed to be relocatable? */
103 prefix = currentdir.c_str();
104 }
105
106 if (prefix != nullptr)
107 {
108 g_key_file_set_string(keyfile.get(), "Desktop Entry", "Icon",
109 iconvalue.get() + strlen(prefix) - 1);
110 /* -1 to leave trailing slash */
111 }
112 }
113
114 return keyfile;
115 }(),
116 snapDir,
117 snapDir,
118 app_info::DesktopFlags::NONE,
119 registry)
120 , appId_(appid)
121 {
122 _xMirEnable = std::get<0>(interfaceInfo);
123 _lomiriLifecycle = std::get<1>(interfaceInfo);
124 }
125
126 /** Figures out the exec line for a snappy command. We're not using
127 the Exec in the desktop file exactly, but assuming that it is kinda
128 what we want to be run. So we're replacing that with the script, which
129 we have to use as we can't get the command that is in the snap
130 metadata as Snapd won't give it to us. So we're parsing the Exec line
131 and replacing the first entry. Then putting it back together again. */
132 Exec execLine() override
133 {
134 std::string keyfile = _exec.value();
135 GError* error = nullptr;
136
137 GCharVUPtr parsed(nullptr, &g_strfreev);
138 {
139 gchar** tmp = nullptr;
140 g_shell_parse_argv(keyfile.c_str(), nullptr, &tmp, &error);
141 parsed = unique_gcharv(tmp);
142 }
143
144 if (error != nullptr)
145 {
146 g_warning("Unable to parse exec line '%s': %s", keyfile.c_str(), error->message);
147 g_error_free(error);
148 return Exec::from_raw({});
149 }
150
151 if (g_strv_length(parsed.get()) == 0)
152 {
153 g_warning("Parse resulted in a blank line");
154 return Exec::from_raw({});
155 }
156
157 /* Skip the first entry */
158 gchar** parsedpp = &(parsed.get()[1]);
159
160 auto params = unique_gchar(g_strjoinv(" ", parsedpp));
161
162 std::string binname;
163 if (appId_.package.value() == appId_.appname.value())
164 {
165 binname = appId_.package.value();
166 }
167 else
168 {
169 binname = appId_.package.value() + "." + appId_.appname.value();
170 }
171
172 binname = "/snap/bin/" + binname + " " + params.get();
173
174 return Exec::from_raw(binname);
175 }
176 };
177
178 /************************
179 ** Snap implementation
180 ************************/
181
182 /** Creates a Snap application object. Will throw exceptions if the AppID
183 doesn't resolve into a valid package or that package doesn't have a desktop
184 file that matches the app name.
185
186 \param appid Application ID of the snap
187 \param registry Registry to use for persistent connections
188 \param interfaceInfo Metadata gleaned from the snap's interfaces
189 */
190 Snap::Snap(const AppID& appid, const std::shared_ptr<Registry::Impl>& registry, const InterfaceInfo& interfaceInfo)
191 : Base(registry)
192 , appid_(appid)
193 {
194 pkgInfo_ = registry->snapdInfo.pkgInfo(appid.package);
195 if (!pkgInfo_)
196 {
197 throw std::runtime_error("Unable to get snap package info for AppID: " + std::string(appid));
198 }
199
200 if (!checkPkgInfo(pkgInfo_, appid_))
201 {
202 throw std::runtime_error("AppID does not match installed package for: " + std::string(appid));
203 }
204
205 info_ = std::make_shared<SnapInfo>(appid_, registry_, interfaceInfo, pkgInfo_->directory);
206
207 g_debug("Application Snap object for AppID '%s'", std::string(appid).c_str());
208 }
209
210 /** Uses the findInterfaceInfo() function to find the interface if we don't
211 have one.
212
213 \param appid Application ID of the snap
214 \param registry Registry to use for persistent connections
215 */
216 Snap::Snap(const AppID& appid, const std::shared_ptr<Registry::Impl>& registry)
217 : Snap(appid, registry, findInterfaceInfo(appid, registry))
218 {
219 }
220
221 /** Returns the stored AppID */
222 AppID Snap::appId() const
223 {
224 return appid_;
225 }
226
227 /** Asks Snapd for the interfaces to determine which ones the application
228 can support.
229
230 \param appid Application ID of the snap
231 \param registry Registry to use for persistent connections
232 */
233 Snap::InterfaceInfo Snap::findInterfaceInfo(const AppID& appid, const std::shared_ptr<Registry::Impl>& registry)
234 {
235 auto ifaceset = registry->snapdInfo.interfacesForAppId(appid);
236 auto xMirEnable = app_info::Desktop::XMirEnable::from_raw(false);
237 auto lomiriLifecycle = Application::Info::LomiriLifecycle::from_raw(false);
238
239 if (ifaceset.find(LIFECYCLE_INTERFACE) != ifaceset.end())
240 {
241 lomiriLifecycle = Application::Info::LomiriLifecycle::from_raw(true);
242 }
243
244 return std::make_tuple(xMirEnable, lomiriLifecycle);
245 }
246
247 /** Checks a PkgInfo structure to ensure that it matches the AppID */
248 bool Snap::checkPkgInfo(const std::shared_ptr<snapd::Info::PkgInfo>& pkginfo, const AppID& appid)
249 {
250 if (!pkginfo)
251 {
252 return false;
253 }
254
255 return pkginfo->revision == appid.version.value() &&
256 pkginfo->appnames.find(appid.appname) != pkginfo->appnames.end();
257 }
258
259 /** Returns a reference to the info for the snap */
260 std::shared_ptr<Application::Info> Snap::info()
261 {
262 return info_;
263 }
264
265 /** Get all of the instances of this snap package that are running */
266 std::vector<std::shared_ptr<Application::Instance>> Snap::instances()
267 {
268 auto vbase = registry_->jobs()->instances(appId(), "application-snap");
269 return std::vector<std::shared_ptr<Application::Instance>>(vbase.begin(), vbase.end());
270 }
271
272 /** Return the launch environment for this snap. */
273 std::list<std::pair<std::string, std::string>> Snap::launchEnv()
274 {
275 g_debug("Getting snap specific environment");
276 std::list<std::pair<std::string, std::string>> retval;
277
278 /* The helpers are setup by the snap stuff */
279 retval.emplace_back(std::make_pair("APP_EXEC", info_->execLine().value()));
280
281 return retval;
282 }
283
284 /** Create a new instance of this Snap
285
286 \param urls URLs to pass to the command
287 */
288 std::shared_ptr<Application::Instance> Snap::launch(const std::vector<Application::URL>& urls)
289 {
290 auto instance = getInstance(info_);
291 std::function<std::list<std::pair<std::string, std::string>>(void)> envfunc = [this]() { return launchEnv(); };
292 return registry_->jobs()->launch(appid_, "application-snap", instance, urls, jobs::manager::launchMode::STANDARD,
293 envfunc);
294 }
295
296 /** Create a new instance of this Snap with a testing environment
297 setup for it.
298
299 \param urls URLs to pass to the command
300 */
301 std::shared_ptr<Application::Instance> Snap::launchTest(const std::vector<Application::URL>& urls)
302 {
303 auto instance = getInstance(info_);
304 std::function<std::list<std::pair<std::string, std::string>>(void)> envfunc = [this]() { return launchEnv(); };
305 return registry_->jobs()->launch(appid_, "application-snap", instance, urls, jobs::manager::launchMode::TEST,
306 envfunc);
307 }
308
309 std::shared_ptr<Application::Instance> Snap::findInstance(const std::string& instanceid)
310 {
311 return registry_->jobs()->existing(appId(), "application-snap", instanceid, std::vector<Application::URL>{});
312 }
313
314 } // namespace app_impls
315 } // namespace app_launch
316 } // namespace lomiri