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#include <QtCore/private/qohosabilitylaunchparamstore_p.h>
7#include <QtCore/private/qohoslogger_p.h>
8#include <thread>
9
10QT_BEGIN_NAMESPACE
11
12namespace {
13
15{
16 return QOhosJsThreadOps::instance().jsState();
17}
18
19std::shared_ptr<void> tryRegisterUiAbilityDelegate(QNapi::Object delegate)
20{
21 try {
22 return jsState().registerUiAbilityDelegate(delegate);
23 } catch (const Napi::Error &error) {
24 qOhosPrintfError("%s: registering the delegate failed: %s", Q_FUNC_INFO, error.what());
25 return nullptr;
26 }
27}
28
29}
30
31/*!
32 \class QHarmonyAppJsContext
33 \inmodule QtCore
34 \brief The QHarmonyAppJsContext class gives code on the JS thread of a
35 HarmonyOS application access to the application's state.
36
37 QHarmonyJsContext describes the JS runtime. QHarmonyAppJsContext holds
38 the state of the application in that runtime, such as its default
39 \c UIAbility instance. Keeping the two apart makes function signatures
40 meaningful. A function that takes a QHarmonyAppJsContext depends on the
41 application's state. A function that takes a QHarmonyJsContext needs
42 only the runtime. A task gets the application context once, with
43 fromJsContext(), and passes it on.
44
45 The class derives from QHarmonyJsContext, and there is only one object.
46 QHarmonyJsContext::fromEnv() returns it as the base class. All member
47 functions must be called on the JS thread. References to it and its
48 \c napi_value results follow the lifetime rules of QHarmonyJsContext.
49
50 \sa {Qt for HarmonyOS Platform API}
51*/
52
53QHarmonyAppJsContext::QHarmonyAppJsContext() = default;
54
55QHarmonyAppJsContext::~QHarmonyAppJsContext() = default;
56
57/*!
58 Returns the application context of \a env. \a env must be the
59 environment of the JS thread. The returned context is valid as long as
60 \a env.
61
62 Use this function in a JS callback created with
63 \c napi_create_function(), which receives only the \c napi_env.
64*/
65QHarmonyAppJsContext &QHarmonyAppJsContext::fromEnv(napi_env env)
66{
67 Q_ASSERT(env == jsState().env());
68
69 static QHarmonyAppJsContext appJsContext;
70 return appJsContext;
71}
72
73/*!
74 Returns the application context of \a jsContext. The returned context is
75 valid as long as \a jsContext.
76*/
77QHarmonyAppJsContext &QHarmonyAppJsContext::fromJsContext(QHarmonyJsContext &jsContext)
78{
79 return fromEnv(jsContext.env());
80}
81
82/*!
83 Returns the default ArkTS \c UIAbility instance of the application. This
84 is the first instance created for the application or, once that instance
85 is destroyed, another live instance. The result can therefore change
86 over the lifetime of the process. Returns \nullptr when no ability is
87 left, which happens at some stages of application shutdown. Also returns
88 \nullptr in a child process started without a UI, where no ability
89 exists.
90
91 The ability is the entry point to the ability-bound platform APIs. Its
92 \c context property is the \c UIAbilityContext. APIs such as
93 \c {notificationManager.requestEnableNotification()},
94 \c {systemShare.ShareController.show()}, and \c {iap.createPurchase()}
95 take this context. \c context.getApplicationContext() returns the
96 \c ApplicationContext, which provides \c restartApp() and the
97 application directories.
98*/
99napi_value QHarmonyAppJsContext::defaultAbility()
100{
101 return jsState().defaultQAbility().value_or(QNapi::Object());
102}
103
104/*!
105 Returns the \c launchParam that \a abilityValue received in its
106 \c onCreate() call. Returns \nullptr if \a abilityValue is not a
107 \c UIAbility instance whose \c onCreate() Qt handled.
108
109 The \c launchParam is an
110 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-app-ability-abilityconstant#launchparam}{AbilityConstant.LaunchParam}
111 object. It describes why the ability was created, for example in its
112 \c launchReason property. Like the \c launchWant property of the
113 ability, it does not change when the ability receives a new Want.
114
115 To keep the Want and the reason that the application was launched
116 with, read \c launchWant and launchParam() of defaultAbility() at
117 startup. For example, in \c main(), pass a task that reads them to
118 QNativeInterface::QHarmonyApplication::runOnJsThread() right after
119 creating the application object.
120*/
121napi_value QHarmonyAppJsContext::launchParam(napi_value abilityValue)
122{
123 if (!abilityValue || !Napi::Value(env(), abilityValue).IsObject())
124 return nullptr;
125
126 auto &launchParamStore = QOhosAbilityLaunchParamStore::instance(jsState());
127 try {
128 return launchParamStore.getOrEmpty(QNapi::Object(env(), abilityValue));
129 } catch (const Napi::Error &error) {
130 qOhosPrintfError("%s: reading the launchParam failed: %s", Q_FUNC_INFO, error.what());
131 return nullptr;
132 }
133}
134
135/*!
136 Registers \a delegateValue as a delegate for the methods of the
137 application's \c UIAbility class and returns a handle that keeps the
138 registration. \a delegateValue must be a JS object.
139
140 Qt forwards a \c UIAbility method by calling the delegate method of the
141 same name with the \c UIAbility instance as the first argument, followed
142 by the arguments the \c UIAbility method received. It looks the method
143 up at each call and calls it as a method of the delegate. A delegate
144 defines a method when the property of that name is a function. It may
145 define any subset of the forwarded methods. Delegates are called in
146 registration order. A method with a result has a default, which Qt
147 returns when no delegate defines the method or when the delegate method
148 throws. If a delegate method throws, the exception is logged and
149 cleared. The throw does not change which other delegates are called.
150
151 The forwarded methods are:
152
153 \list
154 \li \c onNewWant(): called on every delegate. The return value is
155 ignored. The delegate must not modify the arguments. Qt does not
156 forward the Wants that it sends to its own abilities.
157 \li \c onContinue(): called on the first delegate that defines it. Its
158 result is returned to the system. The default is
159 \c {AbilityConstant.OnContinueResult.REJECT}.
160 \endlist
161
162 Methods not listed are not forwarded. Later versions may forward more
163 methods.
164
165 Qt holds a strong reference to the delegate until the handle is
166 released. Releasing it unregisters the delegate, and no delegate method
167 is called afterwards. A delegate method may release the handle.
168
169 The handle can be released on any thread. Releasing it on another
170 thread blocks that thread until the JS thread has unregistered the
171 delegate.
172
173 \sa {Qt for HarmonyOS Platform API}
174*/
175std::shared_ptr<void> QHarmonyAppJsContext::addUiAbilityDelegate(napi_value delegateValue)
176{
177 const char *funcInfo = Q_FUNC_INFO;
178
179 if (!delegateValue || !Napi::Value(env(), delegateValue).IsObject()) {
180 qOhosPrintfError("%s: the delegate is not a JS object, not registering it", funcInfo);
181 return nullptr;
182 }
183
184 auto registrationHandle = tryRegisterUiAbilityDelegate(QNapi::Object(env(), delegateValue));
185 if (!registrationHandle)
186 return nullptr;
187
188 return QtOhos::makeDestroyNotifier(
189 [registrationHandle, jsThreadId = std::this_thread::get_id(), funcInfo]() mutable {
190 if (std::this_thread::get_id() != jsThreadId) {
191 qCDebug(
192 QtForOhos,
193 "%s: the delegate handle is released off the JS thread, waiting for the JS thread",
194 funcInfo);
195 }
196 QOhosJsThreadGateway::runAndWait(
197 [&](QOhosJsState &) {
198 registrationHandle.reset();
199 },
200 funcInfo);
201 });
202}
203
204QT_END_NAMESPACE
std::shared_ptr< void > tryRegisterUiAbilityDelegate(QNapi::Object delegate)