summaryrefslogtreecommitdiffstats
path: root/src/manager-lib/application.cpp
blob: e4cb72922dadcbbba6a00ad58257b6deff642a70 (plain)
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Luxoft Application Manager.
**
** $QT_BEGIN_LICENSE:LGPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/

#include "application.h"
#include "abstractruntime.h"
#include "applicationinfo.h"
#include "exception.h"
#include "logging.h"

#include <QDebug>

#if defined(Q_OS_UNIX)
#  include <signal.h>
#endif

/*!
    \qmltype ApplicationObject
    \inqmlmodule QtApplicationManager.SystemUI
    \ingroup system-ui-non-instantiable
    \brief The handle for an application known to the ApplicationManager.

    Most of the read-only properties map directly to values read from the application's
    \c info.yaml file - these are documented in the \l{Manifest Definition}.
*/

/*!
    \qmlproperty string ApplicationObject::id
    \readonly

    This property returns the unique id of the application.
*/
/*!
    \qmlproperty string ApplicationObject::runtimeName
    \readonly

    This property holds the name of the runtime, necessary to run the application's code.
*/
/*!
    \qmlproperty object ApplicationObject::runtimeParameters
    \readonly

    This property holds a QVariantMap that is passed onto, and interpreted by the application's
    runtime.
*/
/*!
    \qmlproperty url ApplicationObject::icon
    \readonly

    The URL of the application's icon - can be used as the source property of an \l Image.
*/
/*!
    \qmlproperty string ApplicationObject::documentUrl
    \readonly

    This property always returns the default \c documentUrl specified in the manifest file, even if
    a different URL was used to start the application.
*/
/*!
    \qmlproperty bool ApplicationObject::builtIn
    \readonly

    This property describes, if this application is part of the built-in set of applications of the
    current System-UI.
*/
/*!
    \qmlproperty bool ApplicationObject::alias
    \readonly

    Will return \c true if this ApplicationObject object is an alias to another one.

    \sa nonAliased
*/
/*!
    \qmlproperty ApplicationObject ApplicationObject::nonAliased
    \readonly

    If this ApplicationObject is an alias, then you can access the non-alias, base ApplicationObject
    via this property, otherwise it contains a reference to itself. This means that if you're interested
    in accessing the base application regardless of whether the object at hand is just an alias, you
    can always safely refer to this property.

    If you want to know whether this object is an alias or a base ApplicationObject, use
    ApplicationObject::alias instead.
*/
/*!
    \qmlproperty list<string> ApplicationObject::capabilities
    \readonly

    A list of special access rights for the application - these capabilities can later be queried
    and verified by the middleware via the application-manager.
*/
/*!
    \qmlproperty list<string> ApplicationObject::supportedMimeTypes
    \readonly

    An array of MIME types the application can handle.
*/
/*!
    \qmlproperty list<string> ApplicationObject::categories
    \readonly

    A list of category names the application should be associated with. This is mainly for the
    automated app-store uploads as well as displaying the application within a fixed set of
    categories in the System-UI.
*/
/*!
    \qmlproperty object ApplicationObject::applicationProperties
    \readonly

    All user-defined properties of this application as listed in the private and protected sections
    of the \c applicationProperties field in the manifest file.
*/
/*!
    \qmlproperty Runtime ApplicationObject::runtime
    \readonly

    Will return a valid \l Runtime object, if the application is currently starting, running or
    shutting down. May return a \c null object, if the application was not yet started.
*/
/*!
    \qmlproperty int ApplicationObject::lastExitCode
    \readonly

    This property holds the last exit-code of the application's process in multi-process mode.
    On successful application shutdown, this value should normally be \c 0, but can be whatever
    the application returns from its \c main() function.
*/
/*!
    \qmlproperty enumeration ApplicationObject::lastExitStatus
    \readonly

    This property returns the last exit-status of the application's process in multi-process mode.

    \list
    \li Am.NormalExit - The application exited normally.
    \li Am.CrashExit - The application crashed.
    \li Am.ForcedExit - The application was killed by the application-manager, since it
                        ignored the quit request originating from a call to
                        ApplicationManager::stopApplication.
    \endlist

    \sa ApplicationInterface::quit, ApplicationInterface::acknowledgeQuit
*/
/*!
    \qmlproperty string ApplicationObject::version
    \readonly

    Holds the version of the application as a string.
*/
/*!
    \qmlproperty string ApplicationObject::codeDir
    \readonly

    The absolute path to the application's installation directory. Please note this directory might
    not always be available for applications that were installed onto removable media.

    \sa {Installation Locations}
*/
/*!
    \qmlproperty enumeration ApplicationObject::state
    \readonly

    This property holds the current installation state of the application. It can be one of:

    \list
    \li ApplicationObject.Installed - The application is completely installed and ready to be used.
    \li ApplicationObject.BeingInstalled - The application is currently in the process of being installed.
    \li ApplicationObject.BeingUpdated - The application is currently in the process of being updated.
    \li ApplicationObject.BeingDowngraded - The application is currently in the process of being downgraded.
                                            That can only happen for a built-in application that was previously
                                            upgraded. It will then be brought back to its original, built-in,
                                            version and its state will go back to ApplicationObject.Installed.
    \li ApplicationObject.BeingRemoved - The application is currently in the process of being removed.
    \endlist
*/
/*!
    \qmlproperty enumeration ApplicationObject::runState
    \readonly

    This property holds the current run state of the application. It can be one of:

    \list
    \li Am.NotRunning - the application has not been started yet.
    \li Am.StartingUp - the application has been started and is initializing.
    \li Am.Running - the application is running.
    \li Am.ShuttingDown - the application has been stopped and is cleaning up (in multi-process mode
                          this state is only reached, if the application is terminating gracefully).
    \endlist
*/
/*!
    \qmlsignal ApplicationObject::activated()

    This signal is emitted when the application is started or when it's already running but has
    been requested to be brought to foreground or raised.
*/
/*!
    \qmlmethod string ApplicationObject::name(string language)

    Returns the name of the application in the given \a language, as provided in the the info.yaml
    file.
*/
/*!
    \qmlmethod bool ApplicationObject::start(string document)

    Starts the application. The optional argument \a document will be supplied to the application as
    is - most commonly this is used to refer to a document to display.

    \sa ApplicationManager::startApplication
*/
/*!
    \qmlmethod bool ApplicationObject::debug(string debugWrapper, string document)

    Same as start() with the difference that it is started via the given \a debugWrapper.
    Please see the \l{Debugging} page for more information on how to setup and use these debug-wrappers.

    \sa ApplicationManager::debugApplication
*/
/*!
    \qmlmethod ApplicationObject::stop(bool forceKill)

    Stops the application. The meaning of the \a forceKill parameter is runtime dependent, but in
    general you should always try to stop an application with \a forceKill set to \c false first
    in order to allow a clean shutdown. Use \a forceKill set to \c true only as a last resort to
    kill hanging applications.

    \sa ApplicationManager::stopApplication
*/

QT_BEGIN_NAMESPACE_AM

///////////////////////////////////////////////////////////////////////////////////////////////////
// AbstractApplication
///////////////////////////////////////////////////////////////////////////////////////////////////

AbstractApplication::AbstractApplication(AbstractApplicationInfo *info)
    : m_info(info)
{
}

QString AbstractApplication::id() const
{
    return m_info->id();
}

QUrl AbstractApplication::icon() const
{
    if (info()->icon().isEmpty())
        return QUrl();

    auto appInfo = this->nonAliasedInfo();
    QDir dir;
    switch (state()) {
    default:
    case Installed:
        dir = appInfo->manifestDir();
        break;
    case BeingInstalled:
    case BeingUpdated:
        dir = QDir(appInfo->codeDir().absolutePath() + QLatin1Char('+'));
        break;
    case BeingRemoved:
        dir = QDir(appInfo->codeDir().absolutePath() + QLatin1Char('-'));
        break;
    }
    return QUrl::fromLocalFile(dir.absoluteFilePath(info()->icon()));
}

QString AbstractApplication::documentUrl() const
{
    return info()->documentUrl();
}

bool AbstractApplication::isBuiltIn() const
{
    return nonAliasedInfo()->isBuiltIn();
}

bool AbstractApplication::isAlias() const
{
    return info()->isAlias();
}

QStringList AbstractApplication::capabilities() const
{
    return nonAliasedInfo()->capabilities();
}

QStringList AbstractApplication::supportedMimeTypes() const
{
    return nonAliasedInfo()->supportedMimeTypes();
}

QStringList AbstractApplication::categories() const
{
    return nonAliasedInfo()->categories();
}

QVariantMap AbstractApplication::applicationProperties() const
{
    return info()->applicationProperties();
}

bool AbstractApplication::supportsApplicationInterface() const
{
    return nonAliasedInfo()->supportsApplicationInterface();
}

QString AbstractApplication::version() const
{
    return nonAliasedInfo()->version();
}

QString AbstractApplication::codeDir() const
{
    switch (state()) {
    default:
    case Installed:
        return nonAliasedInfo()->codeDir().absolutePath();
    case BeingInstalled:
    case BeingUpdated:
        return nonAliasedInfo()->codeDir().absolutePath() + QLatin1Char('+');
    case BeingRemoved:
        return nonAliasedInfo()->codeDir().absolutePath() + QLatin1Char('-');
    }
}

QString AbstractApplication::name(const QString &language) const
{
    return info()->name(language);
}

void AbstractApplication::start(const QString &documentUrl)
{
    emit requests.startRequested(documentUrl);
}

void AbstractApplication::debug(const QString &debugWrapper, const QString &documentUrl)
{
    emit requests.debugRequested(debugWrapper, documentUrl);
}

void AbstractApplication::stop(bool forceKill)
{
    emit requests.stopRequested(forceKill);
}

QVector<AbstractApplication *> AbstractApplication::fromApplicationInfoVector(
        QVector<AbstractApplicationInfo *> &appInfoVector)
{
    QVector<AbstractApplication *> apps;

    auto findAppWithId = [&apps] (const QString &id) -> AbstractApplication*
    {
        for (AbstractApplication *app : apps) {
            if (app->id() == id)
                return app;
        }
        return nullptr;
    };

    auto extractBaseId = [] (const QString &id) -> QString
    {
        return id.section(qL1C('@'), 0, 0);
    };

    for (auto *appInfo : appInfoVector) {
        QScopedPointer<AbstractApplication> app;
        if (appInfo->isAlias()) {
            auto *originalApp = findAppWithId(extractBaseId(appInfo->id()));
            if (!originalApp)
                throw Exception(Error::Parse, "Could not find base app for alias id %2").arg(appInfo->id());
            Q_ASSERT(!originalApp->isAlias());
            app.reset(new ApplicationAlias(static_cast<Application*>(originalApp),
                                           static_cast<ApplicationAliasInfo*>(appInfo)));
        } else {
            AbstractApplication *otherAbsApp = findAppWithId(appInfo->id());
            if (otherAbsApp) {
                // There's already another ApplicationInfo with the same id. It's probably an update for a
                // built-in app, in which case we use the same Application instance to hold both
                // ApplicationInfo instances.
                if (!otherAbsApp->isAlias()) {
                    auto otherApp = static_cast<Application*>(otherAbsApp);
                    auto fullAppInfo = static_cast<ApplicationInfo*>(appInfo);
                    if (otherApp->isBuiltIn() && !fullAppInfo->isBuiltIn() && !otherApp->updatedInfo()) {
                        otherApp->setUpdatedInfo(static_cast<ApplicationInfo*>(appInfo));
                    } else if (!otherApp->isBuiltIn() && fullAppInfo->isBuiltIn() && !otherApp->updatedInfo()) {
                        auto currentBaseInfo = otherApp->takeBaseInfo();
                        otherApp->setBaseInfo(static_cast<ApplicationInfo*>(appInfo));
                        otherApp->setUpdatedInfo(currentBaseInfo);
                    }
                } else {
                    qCWarning(LogSystem) << "Found a second application with id" << appInfo->id()
                                         << "which is not an update for a built-in one. Ignoring it.";
                }
            } else {
                app.reset(new Application(static_cast<ApplicationInfo*>(appInfo)));
            }
        }

        if (!app.isNull())
            apps << app.take();
    }

    return apps;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Application
///////////////////////////////////////////////////////////////////////////////////////////////////

Application::Application(ApplicationInfo *info, State initialState)
    : AbstractApplication(info)
    , m_state(initialState)
{
}

void Application::setCurrentRuntime(AbstractRuntime *rt)
{
    if (m_runtime == rt)
        return;

    if (m_runtime)
        disconnect(m_runtime, nullptr, this, nullptr);

    m_runtime = rt;
    emit runtimeChanged();

    if (m_runtime) {
        connect(m_runtime, &AbstractRuntime::finished, this, &Application::setLastExitCodeAndStatus);
        connect(m_runtime, &QObject::destroyed, this, [this]() {
            this->setCurrentRuntime(nullptr);
        });
    } else
        setRunState(Am::NotRunning);
}

bool Application::isBlocked() const
{
    return m_blocked.load() == 1;
}

bool Application::block()
{
    return m_blocked.testAndSetOrdered(0, 1);
}

bool Application::unblock()
{
    return m_blocked.testAndSetOrdered(1, 0);
}

void Application::setRunState(Am::RunState runState)
{
    if (runState != m_runState) {
        m_runState = runState;
        emit runStateChanged(m_runState);
    }
}

QString Application::runtimeName() const
{
    return Application::nonAliasedInfo()->runtimeName();
}

QVariantMap Application::runtimeParameters() const
{
    return Application::nonAliasedInfo()->runtimeParameters();
}

AbstractApplicationInfo *Application::info() const
{
    return m_updatedInfo ? m_updatedInfo.data() : m_info.data();
}

ApplicationInfo *Application::nonAliasedInfo() const
{
    return static_cast<ApplicationInfo*>(Application::info());
}

void Application::setLastExitCodeAndStatus(int exitCode, Am::ExitStatus exitStatus)
{
    if (m_lastExitCode != exitCode) {
        m_lastExitCode = exitCode;
        emit lastExitCodeChanged();
    }

    Am::ExitStatus newStatus;
    if (exitStatus == Am::CrashExit) {
#if defined(Q_OS_UNIX)
        newStatus = (exitCode == SIGTERM || exitCode == SIGKILL) ? Am::ForcedExit : Am::CrashExit;
#else
        newStatus = Am::CrashExit;
#endif
    } else {
        newStatus = Am::NormalExit;
    }

    if (m_lastExitStatus != newStatus) {
        m_lastExitStatus = newStatus;
        emit lastExitStatusChanged();
    }
}

void Application::setBaseInfo(ApplicationInfo *info)
{
    m_info.reset(info);
    emit bulkChange();
}

ApplicationInfo *Application::takeBaseInfo()
{
    return static_cast<ApplicationInfo*>(m_info.take());
}

void Application::setUpdatedInfo(ApplicationInfo* info)
{
    Q_ASSERT(!info || (m_info && info->id() == m_info->id()));

    m_updatedInfo.reset(info);
    emit bulkChange();
}

ApplicationInfo *Application::takeUpdatedInfo()
{
    return m_updatedInfo.take();
}

void Application::setState(State state)
{
    if (m_state != state) {
        m_state = state;
        emit stateChanged(m_state);
    }
}

void Application::setProgress(qreal value)
{
    m_progress = value;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// ApplicationAlias
///////////////////////////////////////////////////////////////////////////////////////////////////

ApplicationAlias::ApplicationAlias(Application* app, ApplicationAliasInfo* info)
    : AbstractApplication(info)
    , m_application(app)
{
    connect(m_application, &AbstractApplication::runtimeChanged, this, &AbstractApplication::runtimeChanged);
    connect(m_application, &AbstractApplication::lastExitCodeChanged, this, &AbstractApplication::lastExitCodeChanged);
    connect(m_application, &AbstractApplication::lastExitStatusChanged, this, &AbstractApplication::lastExitStatusChanged);
    connect(m_application, &AbstractApplication::stateChanged, this, &AbstractApplication::stateChanged);
    connect(m_application, &AbstractApplication::runStateChanged, this, &AbstractApplication::runStateChanged);
}

QString ApplicationAlias::runtimeName() const
{
    return m_application->runtimeName();
}

QVariantMap ApplicationAlias::runtimeParameters() const
{
    return m_application->runtimeParameters();
}

ApplicationInfo *ApplicationAlias::nonAliasedInfo() const
{
    return m_application->nonAliasedInfo();
}

QT_END_NAMESPACE_AM

QDebug operator<<(QDebug debug, const QT_PREPEND_NAMESPACE_AM(AbstractApplication) *app)
{
    debug << "Application Object:";
    if (app)
        debug << app->info()->toVariantMap();
    else
        debug << "(null)";
    return debug;
}