Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qharmonynativeinterface.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
4
#
include
<
QtCore
/
qcoreapplication_platform
.
h
>
5
6
#
include
<
QtCore
/
private
/
qcore_ohos_p
.
h
>
7
#
include
<
QtCore
/
private
/
qnativeinterface_p
.
h
>
8
#
include
<
QtCore
/
private
/
qohosjstools_p
.
h
>
9
#
include
<
functional
>
10
11
QT_BEGIN_NAMESPACE
12
13
namespace
{
14
15
void
runTaskLoggingExceptions
(
16
QOhosJsState &jsState,
const
char
*callerContextName,
17
const
std::function<
void
(QHarmonyJsContext &)> &task)
18
{
19
runQOhosJsCodeLoggingExceptions(
20
jsState.env(), callerContextName,
21
[&]() {
22
task(QHarmonyJsContext::fromEnv(jsState.env()));
23
});
24
}
25
26
}
27
28
/*!
29
\class QNativeInterface::QHarmonyApplication
30
\brief Native interface to a core application on HarmonyOS.
31
32
Accessed through QCoreApplication::nativeInterface().
33
34
On HarmonyOS, the ArkTS runtime and the platform APIs run on the main
35
thread of the process, called the JS thread. The Qt application runs on
36
a separate thread, called the Qt thread. This is the thread of the
37
QCoreApplication object. This interface runs application code on the JS
38
thread, where the code receives a QHarmonyJsContext. It also brings
39
results back to the Qt thread.
40
41
Code that runs on the JS thread must follow these rules:
42
\list
43
\li It must not wait for the Qt thread, which may itself be waiting in
44
runOnJsThreadAndWait(). Send results back asynchronously, with
45
runOnQtThread() or through QPromise.
46
\li It must not access Qt-thread objects directly, not even through a
47
QPointer. A QPointer is created, checked, and dereferenced only on
48
the thread of its object. The JS thread only passes copies of it
49
along. The functor posted to the Qt thread checks the QPointer
50
there.
51
\li A JS callback may run after the Qt event loop has finished. Tasks
52
that it queues with runOnQtThread() then never run.
53
\li A \c napi_value that it obtains is valid until the task returns, and
54
must not leave the JS thread. To keep a JS object longer, use a
55
QHarmonyJsObjectRef.
56
\li Resources bound to the JS engine, such as references and listeners,
57
must be released on the JS thread.
58
\li The JS thread also runs the platform UI, so tasks must be short.
59
\endlist
60
61
Each task runs inside its own Node-API handle scope. A C++ exception
62
that a task throws and a JS exception that it leaves pending are caught
63
and logged on the JS thread. A task that needs to react to a failure
64
must handle it itself.
65
66
\inmodule QtCore
67
\inheaderfile QCoreApplication
68
\ingroup native-interfaces
69
\ingroup native-interfaces-qcoreapplication
70
71
\sa {Qt for HarmonyOS Platform API}
72
*/
73
QT_DEFINE_NATIVE_INTERFACE
(
QHarmonyApplication
);
74
75
/*!
76
Queues \a task for execution on the JS thread and returns immediately.
77
You can call this function from any thread. This is the primary way to
78
reach the platform APIs. \a task is left empty, and the task it held,
79
with everything it captures, is destroyed on the JS thread.
80
81
Tasks queued with runOnJsThread() and runOnJsThreadAndWait() run one at a
82
time, in the order in which they were queued.
83
*/
84
void
QNativeInterface::QHarmonyApplication::runOnJsThread(std::function<
void
(QHarmonyJsContext &)> &&task)
85
{
86
const
char
*funcInfo = Q_FUNC_INFO;
87
88
QOhosJsThreadGateway::invoke(
89
[sharedTask = QtOhos::takeFunctionToSharedPtr(task), funcInfo](QOhosJsState &jsState) {
90
const
auto
takenTask = std::exchange(*sharedTask,
nullptr
);
91
runTaskLoggingExceptions(jsState, funcInfo, takenTask);
92
});
93
}
94
95
/*!
96
Runs \a task on the JS thread and blocks the calling thread until
97
\a task returns. You can call this function from any thread. \a task
98
runs after the tasks queued before it with runOnJsThread() or
99
runOnJsThreadAndWait(). When called on the JS thread, it runs \a task
100
directly, before any task that is still queued. \a task must not wait for
101
the calling thread.
102
*/
103
void
QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait(
const
std::function<
void
(QHarmonyJsContext &)> &task)
104
{
105
const
char
*funcInfo = Q_FUNC_INFO;
106
107
QOhosJsThreadGateway::runAndWait(
108
[&](QOhosJsState &jsState) {
109
runTaskLoggingExceptions(jsState, funcInfo, task);
110
},
111
funcInfo);
112
}
113
114
/*!
115
\fn template <typename Func, typename Result> Result QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait(Func &&task)
116
117
Runs \a task on the JS thread, blocks the calling thread until \a task
118
returns, and returns its result. \a task is ordered like in the overload
119
without a result. If \a task throws an exception or leaves a JS
120
exception pending, the exception is logged on the JS thread and a
121
default-constructed value is returned. The result must not be a
122
\c napi_value or a \c napi_ref. To bring JS data back, convert it with
123
the QtHarmony functions or keep it in a QHarmonyJsObjectRef.
124
*/
125
126
/*!
127
Queues \a task for execution on the Qt thread, the thread of the
128
QCoreApplication object, and returns immediately. \a task is left empty.
129
You can call this function from any thread at any time. Use it to bring
130
results back from the JS thread. Tasks queued with runOnQtThread() run
131
one at a time, in the order in which they were queued. A task queued
132
after the Qt event loop has finished for good never runs.
133
*/
134
void
QNativeInterface::QHarmonyApplication::runOnQtThread(std::function<
void
()> &&task)
135
{
136
QtOhos::invokeInQtThread(std::move(task));
137
}
138
139
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE::runTaskLoggingExceptions
void runTaskLoggingExceptions(QOhosJsState &jsState, const char *callerContextName, const std::function< void(QHarmonyJsContext &)> &task)
Definition
qharmonynativeinterface.cpp:15
QT_DEFINE_NATIVE_INTERFACE
QT_DEFINE_NATIVE_INTERFACE(QHarmonyApplication)
qtbase
src
corelib
platform
ohos
qharmonynativeinterface.cpp
Generated on
for Qt by
1.18.0