Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qharmonyappjscontext.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QtCore/private/qcore_ohos_p.h>
6
7QT_BEGIN_NAMESPACE
8
9namespace {
10
12{
13 return QOhosJsThreadOps::instance().jsState();
14}
15
16}
17
18/*!
19 \class QHarmonyAppJsContext
20 \inmodule QtCore
21 \brief The QHarmonyAppJsContext class gives code on the JS thread of a
22 HarmonyOS application access to the application's state.
23
24 QHarmonyJsContext describes the JS runtime. QHarmonyAppJsContext holds
25 the state of the application in that runtime, such as its default
26 \c UIAbility instance. Keeping the two apart makes function signatures
27 meaningful. A function that takes a QHarmonyAppJsContext depends on the
28 application's state. A function that takes a QHarmonyJsContext needs
29 only the runtime. A task gets the application context once, with
30 fromJsContext(), and passes it on.
31
32 The class derives from QHarmonyJsContext, and there is only one object.
33 QHarmonyJsContext::fromEnv() returns it as the base class. All member
34 functions must be called on the JS thread. References to it and its
35 \c napi_value results follow the lifetime rules of QHarmonyJsContext.
36
37 \sa {Qt for HarmonyOS Platform API}
38*/
39
40QHarmonyAppJsContext::QHarmonyAppJsContext() = default;
41
42QHarmonyAppJsContext::~QHarmonyAppJsContext() = default;
43
44/*!
45 Returns the application context of \a env. \a env must be the
46 environment of the JS thread. The returned context is valid as long as
47 \a env.
48
49 Use this function in a JS callback created with
50 \c napi_create_function(), which receives only the \c napi_env.
51*/
52QHarmonyAppJsContext &QHarmonyAppJsContext::fromEnv(napi_env env)
53{
54 Q_ASSERT(env == jsState().env());
55
56 static QHarmonyAppJsContext appJsContext;
57 return appJsContext;
58}
59
60/*!
61 Returns the application context of \a jsContext. The returned context is
62 valid as long as \a jsContext.
63*/
64QHarmonyAppJsContext &QHarmonyAppJsContext::fromJsContext(QHarmonyJsContext &jsContext)
65{
66 return fromEnv(jsContext.env());
67}
68
69/*!
70 Returns the default ArkTS \c UIAbility instance of the application. This
71 is the first instance created for the application or, once that instance
72 is destroyed, another live instance. The result can therefore change
73 over the lifetime of the process. Returns \nullptr when no ability is
74 left, which happens at some stages of application shutdown. Also returns
75 \nullptr in a child process started without a UI, where no ability
76 exists.
77
78 The ability is the entry point to the ability-bound platform APIs. Its
79 \c context property is the \c UIAbilityContext. APIs such as
80 \c {notificationManager.requestEnableNotification()},
81 \c {systemShare.ShareController.show()}, and \c {iap.createPurchase()}
82 take this context. \c context.getApplicationContext() returns the
83 \c ApplicationContext, which provides \c restartApp() and the
84 application directories.
85*/
86napi_value QHarmonyAppJsContext::defaultAbility()
87{
88 return jsState().defaultQAbility().value_or(QNapi::Object());
89}
90
91QT_END_NAMESPACE