Debian Codegraph @

liblomiri-app-launch/app-store-snap.cpp

raw
1 /*
2 * Copyright © 2017 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 "app-store-snap.h"
21 #include "application-impl-snap.h"
22 #include "registry-impl.h"
23
24 #include <regex>
25
26 namespace lomiri
27 {
28 namespace app_launch
29 {
30 namespace app_store
31 {
32
33 /************************
34 ** Interface Lists
35 ************************/
36
37 /** All the interfaces that we run XMir for by default */
38 const std::set<std::string> X11_INTERFACES{"unity7", "x11"};
39 /** The interface to indicate direct Mir support */
40 const std::string MIR_INTERFACE{"mir"};
41 /** The interface to indicate Lomiri lifecycle support */
42 const std::string LIFECYCLE_INTERFACE{"lomiri"};
43 /** Snappy has more restrictive appnames than everyone else */
44 const std::regex appnameRegex{"^[a-zA-Z0-9](?:-?[a-zA-Z0-9])*$"};
45
46 Snap::Snap(const std::shared_ptr<Registry::Impl>& registry)
47 : Base(registry)
48 {
49 }
50
51 Snap::~Snap()
52 {
53 }
54
55 /** Checks if an AppID could be a snap. Note it doesn't look for a desktop
56 file just the package, app and version. This is done to make the lookup
57 quickly, as this function can be used to select which backend to use
58 and we want to reject quickly.
59
60 \param appid Application ID of the snap
61 \param registry Registry to use for persistent connections
62 */
63 bool Snap::hasAppId(const AppID& appId)
64 {
65 if (appId.package.value().empty() || appId.version.value().empty())
66 {
67 return false;
68 }
69
70 if (!std::regex_match(appId.appname.value(), appnameRegex))
71 {
72 return false;
73 }
74
75 auto pkginfo = getReg()->snapdInfo.pkgInfo(appId.package);
76 return app_impls::Snap::checkPkgInfo(pkginfo, appId);
77 }
78
79 /** Look to see if a package is a valid Snap package name
80
81 \param package Package name
82 \param registry Registry to use for persistent connections
83 */
84 bool Snap::verifyPackage(const AppID::Package& package)
85 {
86 try
87 {
88 auto pkgInfo = getReg()->snapdInfo.pkgInfo(package);
89 return pkgInfo != nullptr;
90 }
91 catch (std::runtime_error& e)
92 {
93 return false;
94 }
95 }
96
97 /** Look to see if an appname is a valid for a Snap package
98
99 \param package Package name
100 \param appname Command name
101 \param registry Registry to use for persistent connections
102 */
103 bool Snap::verifyAppname(const AppID::Package& package, const AppID::AppName& appname)
104 {
105 if (!std::regex_match(appname.value(), appnameRegex))
106 {
107 return false;
108 }
109
110 auto pkgInfo = getReg()->snapdInfo.pkgInfo(package);
111
112 if (!pkgInfo)
113 {
114 return false;
115 }
116
117 return pkgInfo->appnames.find(appname) != pkgInfo->appnames.end();
118 }
119
120 /** Look for an application name on a Snap package based on a
121 wildcard type.
122
123 \param package Package name
124 \param card Wildcard to use for finding the appname
125 \param registry Registry to use for persistent connections
126 */
127 AppID::AppName Snap::findAppname(const AppID::Package& package, AppID::ApplicationWildcard card)
128 {
129 auto pkgInfo = getReg()->snapdInfo.pkgInfo(package);
130
131 if (!pkgInfo)
132 {
133 throw std::runtime_error("Package '" + package.value() + "' doesn't have valid info.");
134 }
135
136 if (pkgInfo->appnames.empty())
137 {
138 throw std::runtime_error("No apps in package '" + package.value() + "' to find");
139 }
140
141 switch (card)
142 {
143 case AppID::ApplicationWildcard::FIRST_LISTED:
144 return AppID::AppName::from_raw(*pkgInfo->appnames.begin());
145 case AppID::ApplicationWildcard::LAST_LISTED:
146 return AppID::AppName::from_raw(*pkgInfo->appnames.rbegin());
147 case AppID::ApplicationWildcard::ONLY_LISTED:
148 if (pkgInfo->appnames.size() != 1)
149 {
150 throw std::runtime_error("More than a single app in package '" + package.value() +
151 "' when requested to find only app");
152 }
153 return AppID::AppName::from_raw(*pkgInfo->appnames.begin());
154 }
155
156 throw std::logic_error("Got a value of the app wildcard enum that can't exist");
157 }
158
159 /** Look for a version of a Snap package
160
161 \param package Package name
162 \param appname Not used for snaps
163 \param registry Registry to use for persistent connections
164 */
165 AppID::Version Snap::findVersion(const AppID::Package& package, const AppID::AppName& appname)
166 {
167 auto pkgInfo = getReg()->snapdInfo.pkgInfo(package);
168 if (pkgInfo)
169 {
170 return AppID::Version::from_raw(pkgInfo->revision);
171 }
172 else
173 {
174 return AppID::Version::from_raw({});
175 }
176 }
177
178 /** Operator to compare apps for our sets */
179 struct appcompare
180 {
181 bool operator()(const std::shared_ptr<Application>& a, const std::shared_ptr<Application>& b) const
182 {
183 return a->appId() < b->appId();
184 }
185 };
186
187 /** Lists all the Snappy apps that are using one of our supported interfaces.
188 Also makes sure they're valid.
189
190 \param registry Registry to use for persistent connections
191 */
192 std::list<std::shared_ptr<Application>> Snap::list()
193 {
194 std::set<std::shared_ptr<Application>, appcompare> apps;
195 auto reg = getReg();
196
197 auto lifecycleApps = reg->snapdInfo.appsForInterface(LIFECYCLE_INTERFACE);
198
199 auto lifecycleForApp = [&](const AppID& appID) {
200 auto iterator = lifecycleApps.find(appID);
201 if (iterator == lifecycleApps.end())
202 {
203 return Application::Info::LomiriLifecycle::from_raw(false);
204 }
205 else
206 {
207 return Application::Info::LomiriLifecycle::from_raw(true);
208 }
209 };
210
211 auto addAppsForInterface = [&](const std::string& interface, app_info::Desktop::XMirEnable xMirEnable) {
212 for (const auto& id : reg->snapdInfo.appsForInterface(interface))
213 {
214 auto interfaceInfo = std::make_tuple(xMirEnable, lifecycleForApp(id));
215 try
216 {
217 auto app = std::make_shared<app_impls::Snap>(id, reg, interfaceInfo);
218 apps.emplace(app);
219 }
220 catch (std::runtime_error& e)
221 {
222 g_debug("Unable to make Snap object for '%s': %s", std::string(id).c_str(), e.what());
223 }
224 }
225 };
226
227 addAppsForInterface(MIR_INTERFACE, app_info::Desktop::XMirEnable::from_raw(false));
228
229 /* If an app has both, this will get rejected */
230 for (const auto& interface : X11_INTERFACES)
231 {
232 addAppsForInterface(interface, app_info::Desktop::XMirEnable::from_raw(false));
233 }
234
235 return std::list<std::shared_ptr<Application>>(apps.begin(), apps.end());
236 }
237
238 std::shared_ptr<app_impls::Base> Snap::create(const AppID& appid)
239 {
240 return std::make_shared<app_impls::Snap>(appid, getReg());
241 }
242
243 } // namespace app_store
244 } // namespace app_launch
245 } // namespace lomiri