~ci-train-bot/ubuntu-app-launch/ubuntu-app-launch-ubuntu-zesty-2384

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 * Copyright © 2016 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *     Ted Gould <ted.gould@canonical.com>
 */

extern "C" {
#include "ubuntu-app-launch.h"
}

#include "application-impl-click.h"
#include "application-impl-legacy.h"
#include "application-impl-libertine.h"
#ifdef ENABLE_SNAPPY
#include "application-impl-snap.h"
#endif
#include "application.h"
#include "jobs-base.h"
#include "registry-impl.h"
#include "registry.h"

#include <functional>
#include <iostream>
#include <regex>

namespace ubuntu
{
namespace app_launch
{

std::shared_ptr<Application> Application::create(const AppID& appid, const std::shared_ptr<Registry>& registry)
{
    if (appid.empty())
    {
        throw std::runtime_error("AppID is empty");
    }

    if (!registry || !registry->impl)
    {
        throw std::runtime_error("Invalid registry object");
    }

    if (!registry->impl->jobs)
    {
        registry->impl->jobs = jobs::manager::Base::determineFactory(registry);
    }

    if (app_impls::Click::hasAppId(appid, registry))
    {
        return std::make_shared<app_impls::Click>(appid, registry);
    }
#ifdef ENABLE_SNAPPY
    else if (app_impls::Snap::hasAppId(appid, registry))
    {
        return std::make_shared<app_impls::Snap>(appid, registry);
    }
#endif
    else if (app_impls::Libertine::hasAppId(appid, registry))
    {
        return std::make_shared<app_impls::Libertine>(appid.package, appid.appname, registry);
    }
    else if (app_impls::Legacy::hasAppId(appid, registry))
    {
        return std::make_shared<app_impls::Legacy>(appid.appname, registry);
    }
    else
    {
        throw std::runtime_error("Invalid app ID: " + std::string(appid));
    }
}

AppID::AppID()
    : package(Package::from_raw({}))
    , appname(AppName::from_raw({}))
    , version(Version::from_raw({}))
{
}

AppID::AppID(Package pkg, AppName app, Version ver)
    : package(pkg)
    , appname(app)
    , version(ver)
{
}

#define REGEX_PKGNAME "([a-z0-9][a-z0-9+.-]+)"
#define REGEX_APPNAME "([A-Za-z0-9+-.:~-][\\sA-Za-z0-9+-.:~-]+)"
#define REGEX_VERSION "([\\d+:]?[A-Za-z0-9.+:~-]+?(?:-[A-Za-z0-9+.~]+)?)"

const std::regex full_appid_regex("^" REGEX_PKGNAME "_" REGEX_APPNAME "_" REGEX_VERSION "$");
const std::regex short_appid_regex("^" REGEX_PKGNAME "_" REGEX_APPNAME "$");
const std::regex legacy_appid_regex("^" REGEX_APPNAME "$");

AppID AppID::parse(const std::string& sappid)
{
    std::smatch match;

    if (std::regex_match(sappid, match, full_appid_regex))
    {
        return {AppID::Package::from_raw(match[1].str()), AppID::AppName::from_raw(match[2].str()),
                AppID::Version::from_raw(match[3].str())};
    }
    else
    {
        /* Allow returning an empty AppID with empty internal */
        return {AppID::Package::from_raw({}), AppID::AppName::from_raw({}), AppID::Version::from_raw({})};
    }
}

bool AppID::valid(const std::string& sappid)
{
    return std::regex_match(sappid, full_appid_regex);
}

AppID AppID::find(const std::string& sappid)
{
    auto registry = Registry::getDefault();
    return find(registry, sappid);
}

AppID AppID::find(const std::shared_ptr<Registry>& registry, const std::string& sappid)
{
    std::smatch match;

    if (std::regex_match(sappid, match, full_appid_regex))
    {
        return {AppID::Package::from_raw(match[1].str()), AppID::AppName::from_raw(match[2].str()),
                AppID::Version::from_raw(match[3].str())};
    }
    else if (std::regex_match(sappid, match, short_appid_regex))
    {
        return discover(registry, match[1].str(), match[2].str());
    }
    else if (std::regex_match(sappid, match, legacy_appid_regex))
    {
        return {AppID::Package::from_raw({}), AppID::AppName::from_raw(sappid), AppID::Version::from_raw({})};
    }
    else
    {
        return {AppID::Package::from_raw({}), AppID::AppName::from_raw({}), AppID::Version::from_raw({})};
    }
}

AppID::operator std::string() const
{
    if (package.value().empty() && version.value().empty())
    {
        if (appname.value().empty())
        {
            return {};
        }
        else
        {
            return appname.value();
        }
    }

    return package.value() + "_" + appname.value() + "_" + version.value();
}

bool operator==(const AppID& a, const AppID& b)
{
    return a.package.value() == b.package.value() && a.appname.value() == b.appname.value() &&
           a.version.value() == b.version.value();
}

bool operator!=(const AppID& a, const AppID& b)
{
    return a.package.value() != b.package.value() || a.appname.value() != b.appname.value() ||
           a.version.value() != b.version.value();
}

/** Convert each AppID to a string and then compare the strings */
bool operator<(const AppID& a, const AppID& b)
{
    return std::string(a) < std::string(b);
}

bool AppID::empty() const
{
    return package.value().empty() && appname.value().empty() && version.value().empty();
}

/** Basically we're making our own VTable of static functions. Static
    functions don't go in the normal VTables, so we can't use our class
    inheritance here to help. So we're just packing these puppies into
    a data structure and iterating over it. */
struct DiscoverTools
{
    std::function<bool(const AppID::Package& package, const std::shared_ptr<Registry>& registry)> verifyPackage;
    std::function<bool(
        const AppID::Package& package, const AppID::AppName& appname, const std::shared_ptr<Registry>& registry)>
        verifyAppname;
    std::function<AppID::AppName(
        const AppID::Package& package, AppID::ApplicationWildcard card, const std::shared_ptr<Registry>& registry)>
        findAppname;
    std::function<AppID::Version(
        const AppID::Package& package, const AppID::AppName& appname, const std::shared_ptr<Registry>& registry)>
        findVersion;
    std::function<bool(const AppID& appid, const std::shared_ptr<Registry>& registry)> hasAppId;
};

/** The tools in order that they should be used */
static const std::vector<DiscoverTools> discoverTools{
    /* Click */
    {app_impls::Click::verifyPackage, app_impls::Click::verifyAppname, app_impls::Click::findAppname,
     app_impls::Click::findVersion, app_impls::Click::hasAppId},
#ifdef ENABLE_SNAPPY
    /* Snap */
    {app_impls::Snap::verifyPackage, app_impls::Snap::verifyAppname, app_impls::Snap::findAppname,
     app_impls::Snap::findVersion, app_impls::Snap::hasAppId},
#endif
    /* Libertine */
    {app_impls::Libertine::verifyPackage, app_impls::Libertine::verifyAppname, app_impls::Libertine::findAppname,
     app_impls::Libertine::findVersion, app_impls::Libertine::hasAppId},
    /* Legacy */
    {app_impls::Legacy::verifyPackage, app_impls::Legacy::verifyAppname, app_impls::Legacy::findAppname,
     app_impls::Legacy::findVersion, app_impls::Legacy::hasAppId}};

AppID AppID::discover(const std::shared_ptr<Registry>& registry,
                      const std::string& package,
                      const std::string& appname,
                      const std::string& version)
{
    auto pkg = AppID::Package::from_raw(package);

    for (const auto& tools : discoverTools)
    {
        /* Figure out which type we have */
        try
        {
            if (tools.verifyPackage(pkg, registry))
            {
                auto app = AppID::AppName::from_raw({});

                if (appname.empty() || appname == "first-listed-app")
                {
                    app = tools.findAppname(pkg, ApplicationWildcard::FIRST_LISTED, registry);
                }
                else if (appname == "last-listed-app")
                {
                    app = tools.findAppname(pkg, ApplicationWildcard::LAST_LISTED, registry);
                }
                else if (appname == "only-listed-app")
                {
                    app = tools.findAppname(pkg, ApplicationWildcard::ONLY_LISTED, registry);
                }
                else
                {
                    app = AppID::AppName::from_raw(appname);
                    if (!tools.verifyAppname(pkg, app, registry))
                    {
                        throw std::runtime_error("App name passed in is not valid for this package type");
                    }
                }

                auto ver = AppID::Version::from_raw({});
                if (version.empty() || version == "current-user-version")
                {
                    ver = tools.findVersion(pkg, app, registry);
                }
                else
                {
                    ver = AppID::Version::from_raw(version);
                    if (!tools.hasAppId({pkg, app, ver}, registry))
                    {
                        throw std::runtime_error("Invalid version passed for this package type");
                    }
                }

                return AppID{pkg, app, ver};
            }
        }
        catch (std::runtime_error& e)
        {
            continue;
        }
    }

    return {};
}

AppID AppID::discover(const std::shared_ptr<Registry>& registry,
                      const std::string& package,
                      ApplicationWildcard appwildcard,
                      VersionWildcard versionwildcard)
{
    auto pkg = AppID::Package::from_raw(package);

    for (const auto& tools : discoverTools)
    {
        try
        {
            if (tools.verifyPackage(pkg, registry))
            {
                auto app = tools.findAppname(pkg, appwildcard, registry);
                auto ver = tools.findVersion(pkg, app, registry);
                return AppID{pkg, app, ver};
            }
        }
        catch (std::runtime_error& e)
        {
            /* Normal, try another */
            continue;
        }
    }

    return {};
}

AppID AppID::discover(const std::shared_ptr<Registry>& registry,
                      const std::string& package,
                      const std::string& appname,
                      VersionWildcard versionwildcard)
{
    auto pkg = AppID::Package::from_raw(package);
    auto app = AppID::AppName::from_raw(appname);

    for (const auto& tools : discoverTools)
    {
        try
        {
            if (tools.verifyPackage(pkg, registry) && tools.verifyAppname(pkg, app, registry))
            {
                auto ver = tools.findVersion(pkg, app, registry);
                return AppID{pkg, app, ver};
            }
        }
        catch (std::runtime_error& e)
        {
            /* Normal, try another */
            continue;
        }
    }

    return {};
}

AppID AppID::discover(const std::string& package, const std::string& appname, const std::string& version)
{
    auto registry = Registry::getDefault();
    return discover(registry, package, appname, version);
}

AppID AppID::discover(const std::string& package, ApplicationWildcard appwildcard, VersionWildcard versionwildcard)
{
    auto registry = Registry::getDefault();
    return discover(registry, package, appwildcard, versionwildcard);
}

AppID AppID::discover(const std::string& package, const std::string& appname, VersionWildcard versionwildcard)
{
    auto registry = Registry::getDefault();
    return discover(registry, package, appname, versionwildcard);
}

enum class oom::Score : std::int32_t
{
    FOCUSED = 100,
    UNTRUSTED_HELPER = 200,
    PAUSED = 900,
};

const oom::Score oom::focused()
{
    return oom::Score::FOCUSED;
}

const oom::Score oom::paused()
{
    return oom::Score::PAUSED;
}

const oom::Score oom::fromLabelAndValue(std::int32_t value, const std::string& label)
{
    g_debug("Creating new OOM value type '%s' with a value of: '%d'", label.c_str(), value);

    if (value < static_cast<std::int32_t>(oom::Score::FOCUSED))
    {
        g_warning("The new OOM type '%s' is giving higher priority than focused apps!", label.c_str());
    }
    if (value > static_cast<std::int32_t>(oom::Score::PAUSED))
    {
        g_warning("The new OOM type '%s' is giving lower priority than paused apps!", label.c_str());
    }

    if (value < -1000 || value > 1000)
    {
        throw std::runtime_error("OOM type '" + label + "' is not in the valid range of [-1000, 1000] at " +
                                 std::to_string(value));
    }

    return static_cast<oom::Score>(value);
}

}  // namespace app_launch
}  // namespace ubuntu