| 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 "lomiri-app-launch.h" |
| 21 |
|
| 22 |
#include "application.h" |
| 23 |
#include "info-watcher.h" |
| 24 |
#include "jobs-base.h" |
| 25 |
#include "registry-impl.h" |
| 26 |
#include "registry.h" |
| 27 |
|
| 28 |
#include <functional> |
| 29 |
#include <iomanip> |
| 30 |
#include <iostream> |
| 31 |
#include <locale> |
| 32 |
#include <regex> |
| 33 |
|
| 34 |
namespace lomiri |
| 35 |
{ |
| 36 |
namespace app_launch |
| 37 |
{ |
| 38 |
|
| 39 |
std::shared_ptr<Application> Application::create(const AppID& appid, const std::shared_ptr<Registry>& registry) |
| 40 |
{ |
| 41 |
if (appid.empty()) |
| 42 |
{ |
| 43 |
throw std::runtime_error("AppID is empty"); |
| 44 |
} |
| 45 |
|
| 46 |
if (!registry || !registry->impl) |
| 47 |
{ |
| 48 |
throw std::runtime_error("Invalid registry object"); |
| 49 |
} |
| 50 |
|
| 51 |
return registry->impl->createApp(appid); |
| 52 |
} |
| 53 |
|
| 54 |
AppID::AppID() |
| 55 |
: package(Package::from_raw({})) |
| 56 |
, appname(AppName::from_raw({})) |
| 57 |
, version(Version::from_raw({})) |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
AppID::AppID(Package pkg, AppName app, Version ver) |
| 62 |
: package(pkg) |
| 63 |
, appname(app) |
| 64 |
, version(ver) |
| 65 |
{ |
| 66 |
} |
| 67 |
|
| 68 |
#define REGEX_PKGNAME "([a-z0-9][a-z0-9+.-]+)" |
| 69 |
#define REGEX_APPNAME "([A-Za-z0-9+-.:~-][\\sA-Za-z0-9+-.:~-]+)" |
| 70 |
#define REGEX_VERSION "([\\d+:]?[A-Za-z0-9.+:~-]+?(?:-[A-Za-z0-9+.~]+)?)" |
| 71 |
|
| 72 |
const std::regex full_appid_regex("^" REGEX_PKGNAME "_" REGEX_APPNAME "_" REGEX_VERSION "$"); |
| 73 |
const std::regex short_appid_regex("^" REGEX_PKGNAME "_" REGEX_APPNAME "$"); |
| 74 |
const std::regex legacy_appid_regex("^" REGEX_APPNAME "$"); |
| 75 |
|
| 76 |
AppID AppID::parse(const std::string& sappid) |
| 77 |
{ |
| 78 |
std::smatch match; |
| 79 |
|
| 80 |
if (std::regex_match(sappid, match, full_appid_regex)) |
| 81 |
{ |
| 82 |
return {AppID::Package::from_raw(match[1].str()), AppID::AppName::from_raw(match[2].str()), |
| 83 |
AppID::Version::from_raw(match[3].str())}; |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
/* Allow returning an empty AppID with empty internal */ |
| 88 |
return {AppID::Package::from_raw({}), AppID::AppName::from_raw({}), AppID::Version::from_raw({})}; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
bool AppID::valid(const std::string& sappid) |
| 93 |
{ |
| 94 |
return std::regex_match(sappid, full_appid_regex); |
| 95 |
} |
| 96 |
|
| 97 |
AppID AppID::find(const std::string& sappid) |
| 98 |
{ |
| 99 |
auto registry = Registry::getDefault(); |
| 100 |
return registry->impl->find(sappid); |
| 101 |
} |
| 102 |
|
| 103 |
AppID AppID::find(const std::shared_ptr<Registry>& registry, const std::string& sappid) |
| 104 |
{ |
| 105 |
return registry->impl->find(sappid); |
| 106 |
} |
| 107 |
|
| 108 |
AppID Registry::Impl::find(const std::string& sappid) |
| 109 |
{ |
| 110 |
std::smatch match; |
| 111 |
|
| 112 |
if (std::regex_match(sappid, match, full_appid_regex)) |
| 113 |
{ |
| 114 |
return {AppID::Package::from_raw(match[1].str()), AppID::AppName::from_raw(match[2].str()), |
| 115 |
AppID::Version::from_raw(match[3].str())}; |
| 116 |
} |
| 117 |
else if (std::regex_match(sappid, match, short_appid_regex)) |
| 118 |
{ |
| 119 |
return discover(match[1].str(), match[2].str(), AppID::VersionWildcard::CURRENT_USER_VERSION); |
| 120 |
} |
| 121 |
else if (std::regex_match(sappid, match, legacy_appid_regex)) |
| 122 |
{ |
| 123 |
return {AppID::Package::from_raw({}), AppID::AppName::from_raw(sappid), AppID::Version::from_raw({})}; |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
return {AppID::Package::from_raw({}), AppID::AppName::from_raw({}), AppID::Version::from_raw({})}; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
AppID::operator std::string() const |
| 132 |
{ |
| 133 |
if (package.value().empty() && version.value().empty()) |
| 134 |
{ |
| 135 |
if (appname.value().empty()) |
| 136 |
{ |
| 137 |
return {}; |
| 138 |
} |
| 139 |
else |
| 140 |
{ |
| 141 |
return appname.value(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return package.value() + "_" + appname.value() + "_" + version.value(); |
| 146 |
} |
| 147 |
|
| 148 |
std::string AppID::persistentID() const |
| 149 |
{ |
| 150 |
if (package.value().empty()) |
| 151 |
{ |
| 152 |
if (appname.value().empty()) |
| 153 |
{ |
| 154 |
return {}; |
| 155 |
} |
| 156 |
else |
| 157 |
{ |
| 158 |
return appname.value(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return package.value() + "_" + appname.value(); |
| 163 |
} |
| 164 |
|
| 165 |
std::string AppID::dbusID() const |
| 166 |
{ |
| 167 |
std::string bytes = operator std::string(); |
| 168 |
std::string encoded; |
| 169 |
|
| 170 |
for (size_t i = 0; i < bytes.size(); ++i) { |
| 171 |
char chr = bytes[i]; |
| 172 |
|
| 173 |
if (std::isalpha(chr, std::locale::classic()) || |
| 174 |
(std::isdigit(chr, std::locale::classic()) && i != 0)) { |
| 175 |
encoded += chr; |
| 176 |
} else { |
| 177 |
std::ostringstream hex; |
| 178 |
hex << std::setw(2) << std::setfill('0') << std::hex; |
| 179 |
hex << int(chr); |
| 180 |
encoded += '_' + hex.str(); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return encoded; |
| 185 |
} |
| 186 |
|
| 187 |
AppID AppID::parseDBusID(const std::string& dbusid) |
| 188 |
{ |
| 189 |
std::string decoded; |
| 190 |
|
| 191 |
for (size_t i = 0; i < dbusid.size(); ++i) { |
| 192 |
char chr = dbusid[i]; |
| 193 |
|
| 194 |
if (chr == '_' && i + 2 < dbusid.size()) { |
| 195 |
int result; |
| 196 |
std::istringstream hex(dbusid.substr(i + 1, 2)); |
| 197 |
hex >> std::hex >> result; |
| 198 |
decoded += (char)result; |
| 199 |
i += 2; |
| 200 |
} else { |
| 201 |
decoded += chr; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
return parse(decoded); |
| 206 |
} |
| 207 |
|
| 208 |
bool operator==(const AppID& a, const AppID& b) |
| 209 |
{ |
| 210 |
return a.package.value() == b.package.value() && a.appname.value() == b.appname.value() && |
| 211 |
a.version.value() == b.version.value(); |
| 212 |
} |
| 213 |
|
| 214 |
bool operator!=(const AppID& a, const AppID& b) |
| 215 |
{ |
| 216 |
return a.package.value() != b.package.value() || a.appname.value() != b.appname.value() || |
| 217 |
a.version.value() != b.version.value(); |
| 218 |
} |
| 219 |
|
| 220 |
/** Convert each AppID to a string and then compare the strings */ |
| 221 |
bool operator<(const AppID& a, const AppID& b) |
| 222 |
{ |
| 223 |
return std::string(a) < std::string(b); |
| 224 |
} |
| 225 |
|
| 226 |
bool AppID::empty() const |
| 227 |
{ |
| 228 |
return package.value().empty() && appname.value().empty() && version.value().empty(); |
| 229 |
} |
| 230 |
|
| 231 |
AppID AppID::discover(const std::shared_ptr<Registry>& registry, |
| 232 |
const std::string& package, |
| 233 |
const std::string& appname, |
| 234 |
const std::string& version) |
| 235 |
{ |
| 236 |
return registry->impl->discover(package, appname, version); |
| 237 |
} |
| 238 |
|
| 239 |
AppID Registry::Impl::discover(const std::string& package, const std::string& appname, const std::string& version) |
| 240 |
{ |
| 241 |
auto pkg = AppID::Package::from_raw(package); |
| 242 |
|
| 243 |
for (const auto& appStore : appStores()) |
| 244 |
{ |
| 245 |
/* Figure out which type we have */ |
| 246 |
try |
| 247 |
{ |
| 248 |
if (appStore->verifyPackage(pkg)) |
| 249 |
{ |
| 250 |
auto app = AppID::AppName::from_raw({}); |
| 251 |
|
| 252 |
if (appname.empty() || appname == "first-listed-app") |
| 253 |
{ |
| 254 |
app = appStore->findAppname(pkg, AppID::ApplicationWildcard::FIRST_LISTED); |
| 255 |
} |
| 256 |
else if (appname == "last-listed-app") |
| 257 |
{ |
| 258 |
app = appStore->findAppname(pkg, AppID::ApplicationWildcard::LAST_LISTED); |
| 259 |
} |
| 260 |
else if (appname == "only-listed-app") |
| 261 |
{ |
| 262 |
app = appStore->findAppname(pkg, AppID::ApplicationWildcard::ONLY_LISTED); |
| 263 |
} |
| 264 |
else |
| 265 |
{ |
| 266 |
app = AppID::AppName::from_raw(appname); |
| 267 |
if (!appStore->verifyAppname(pkg, app)) |
| 268 |
{ |
| 269 |
throw std::runtime_error("App name passed in is not valid for this package type"); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
auto ver = AppID::Version::from_raw({}); |
| 274 |
if (version.empty() || version == "current-user-version") |
| 275 |
{ |
| 276 |
ver = appStore->findVersion(pkg, app); |
| 277 |
} |
| 278 |
else |
| 279 |
{ |
| 280 |
ver = AppID::Version::from_raw(version); |
| 281 |
if (!appStore->hasAppId({pkg, app, ver})) |
| 282 |
{ |
| 283 |
throw std::runtime_error("Invalid version passed for this package type"); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
return AppID{pkg, app, ver}; |
| 288 |
} |
| 289 |
} |
| 290 |
catch (std::runtime_error& e) |
| 291 |
{ |
| 292 |
continue; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
return {}; |
| 297 |
} |
| 298 |
|
| 299 |
AppID AppID::discover(const std::shared_ptr<Registry>& registry, |
| 300 |
const std::string& package, |
| 301 |
ApplicationWildcard appwildcard, |
| 302 |
VersionWildcard versionwildcard) |
| 303 |
{ |
| 304 |
return registry->impl->discover(package, appwildcard, versionwildcard); |
| 305 |
} |
| 306 |
|
| 307 |
AppID Registry::Impl::discover(const std::string& package, |
| 308 |
AppID::ApplicationWildcard appwildcard, |
| 309 |
AppID::VersionWildcard versionwildcard) |
| 310 |
{ |
| 311 |
auto pkg = AppID::Package::from_raw(package); |
| 312 |
|
| 313 |
for (const auto& appStore : appStores()) |
| 314 |
{ |
| 315 |
try |
| 316 |
{ |
| 317 |
if (appStore->verifyPackage(pkg)) |
| 318 |
{ |
| 319 |
auto app = appStore->findAppname(pkg, appwildcard); |
| 320 |
auto ver = appStore->findVersion(pkg, app); |
| 321 |
return AppID{pkg, app, ver}; |
| 322 |
} |
| 323 |
} |
| 324 |
catch (std::runtime_error& e) |
| 325 |
{ |
| 326 |
/* Normal, try another */ |
| 327 |
continue; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
return {}; |
| 332 |
} |
| 333 |
|
| 334 |
AppID AppID::discover(const std::shared_ptr<Registry>& registry, |
| 335 |
const std::string& package, |
| 336 |
const std::string& appname, |
| 337 |
VersionWildcard versionwildcard) |
| 338 |
{ |
| 339 |
return registry->impl->discover(package, appname, versionwildcard); |
| 340 |
} |
| 341 |
|
| 342 |
AppID Registry::Impl::discover(const std::string& package, |
| 343 |
const std::string& appname, |
| 344 |
AppID::VersionWildcard versionwildcard) |
| 345 |
{ |
| 346 |
auto pkg = AppID::Package::from_raw(package); |
| 347 |
auto app = AppID::AppName::from_raw(appname); |
| 348 |
|
| 349 |
for (const auto& appStore : appStores()) |
| 350 |
{ |
| 351 |
try |
| 352 |
{ |
| 353 |
if (appStore->verifyPackage(pkg) && appStore->verifyAppname(pkg, app)) |
| 354 |
{ |
| 355 |
auto ver = appStore->findVersion(pkg, app); |
| 356 |
return AppID{pkg, app, ver}; |
| 357 |
} |
| 358 |
} |
| 359 |
catch (std::runtime_error& e) |
| 360 |
{ |
| 361 |
/* Normal, try another */ |
| 362 |
continue; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
return {}; |
| 367 |
} |
| 368 |
|
| 369 |
AppID AppID::discover(const std::string& package, const std::string& appname, const std::string& version) |
| 370 |
{ |
| 371 |
auto registry = Registry::getDefault(); |
| 372 |
return registry->impl->discover(package, appname, version); |
| 373 |
} |
| 374 |
|
| 375 |
AppID AppID::discover(const std::string& package, ApplicationWildcard appwildcard, VersionWildcard versionwildcard) |
| 376 |
{ |
| 377 |
auto registry = Registry::getDefault(); |
| 378 |
return registry->impl->discover(package, appwildcard, versionwildcard); |
| 379 |
} |
| 380 |
|
| 381 |
AppID AppID::discover(const std::string& package, const std::string& appname, VersionWildcard versionwildcard) |
| 382 |
{ |
| 383 |
auto registry = Registry::getDefault(); |
| 384 |
return registry->impl->discover(package, appname, versionwildcard); |
| 385 |
} |
| 386 |
|
| 387 |
enum class oom::Score : std::int32_t |
| 388 |
{ |
| 389 |
FOCUSED = 100, |
| 390 |
UNTRUSTED_HELPER = 200, |
| 391 |
PAUSED = 900, |
| 392 |
}; |
| 393 |
|
| 394 |
oom::Score oom::focused() |
| 395 |
{ |
| 396 |
return oom::Score::FOCUSED; |
| 397 |
} |
| 398 |
|
| 399 |
oom::Score oom::paused() |
| 400 |
{ |
| 401 |
return oom::Score::PAUSED; |
| 402 |
} |
| 403 |
|
| 404 |
oom::Score oom::fromLabelAndValue(std::int32_t value, const std::string& label) |
| 405 |
{ |
| 406 |
g_debug("Creating new OOM value type '%s' with a value of: '%d'", label.c_str(), value); |
| 407 |
|
| 408 |
if (value < static_cast<std::int32_t>(oom::Score::FOCUSED)) |
| 409 |
{ |
| 410 |
g_warning("The new OOM type '%s' is giving higher priority than focused apps!", label.c_str()); |
| 411 |
} |
| 412 |
if (value > static_cast<std::int32_t>(oom::Score::PAUSED)) |
| 413 |
{ |
| 414 |
g_warning("The new OOM type '%s' is giving lower priority than paused apps!", label.c_str()); |
| 415 |
} |
| 416 |
|
| 417 |
if (value < -1000 || value > 1000) |
| 418 |
{ |
| 419 |
throw std::runtime_error("OOM type '" + label + "' is not in the valid range of [-1000, 1000] at " + |
| 420 |
std::to_string(value)); |
| 421 |
} |
| 422 |
|
| 423 |
return static_cast<oom::Score>(value); |
| 424 |
} |
| 425 |
|
| 426 |
bool Application::operator==(const Application& b) const |
| 427 |
{ |
| 428 |
return appId() == b.appId(); |
| 429 |
} |
| 430 |
|
| 431 |
bool Application::operator!=(const Application& b) const |
| 432 |
{ |
| 433 |
return appId() != b.appId(); |
| 434 |
} |
| 435 |
|
| 436 |
bool Application::Instance::operator==(const Application::Instance& b) const |
| 437 |
{ |
| 438 |
auto ja = dynamic_cast<const jobs::instance::Base*>(this); |
| 439 |
auto jb = dynamic_cast<const jobs::instance::Base*>(&b); |
| 440 |
|
| 441 |
return ja->getAppId() == jb->getAppId() && ja->getInstanceId() == jb->getInstanceId(); |
| 442 |
} |
| 443 |
|
| 444 |
bool Application::Instance::operator!=(const Application::Instance& b) const |
| 445 |
{ |
| 446 |
auto ja = dynamic_cast<const jobs::instance::Base*>(this); |
| 447 |
auto jb = dynamic_cast<const jobs::instance::Base*>(&b); |
| 448 |
|
| 449 |
return ja->getAppId() != jb->getAppId() || ja->getInstanceId() != jb->getInstanceId(); |
| 450 |
} |
| 451 |
|
| 452 |
} // namespace app_launch |
| 453 |
} // namespace lomiri |