5#include <QtCore/private/qcore_ohos_p.h>
6#include <QtCore/private/qohosabilitylaunchparamstore_p.h>
7#include <QtCore/private/qohoslogger_p.h>
16 return QOhosJsThreadOps::instance().jsState();
22 return jsState().registerUiAbilityDelegate(delegate);
23 }
catch (
const Napi::Error &error) {
24 qOhosPrintfError(
"%s: registering the delegate failed: %s", Q_FUNC_INFO, error.what());
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
53QHarmonyAppJsContext::QHarmonyAppJsContext() =
default;
55QHarmonyAppJsContext::~QHarmonyAppJsContext() =
default;
58
59
60
61
62
63
64
65QHarmonyAppJsContext &QHarmonyAppJsContext::fromEnv(napi_env env)
67 Q_ASSERT(env == jsState().env());
69 static QHarmonyAppJsContext appJsContext;
74
75
76
77QHarmonyAppJsContext &QHarmonyAppJsContext::fromJsContext(QHarmonyJsContext &jsContext)
79 return fromEnv(jsContext.env());
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99napi_value QHarmonyAppJsContext::defaultAbility()
101 return jsState().defaultQAbility().value_or(QNapi::Object());
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121napi_value QHarmonyAppJsContext::launchParam(napi_value abilityValue)
123 if (!abilityValue || !Napi::Value(env(), abilityValue).IsObject())
126 auto &launchParamStore = QOhosAbilityLaunchParamStore::instance(jsState());
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());
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
175std::shared_ptr<
void> QHarmonyAppJsContext::addUiAbilityDelegate(napi_value delegateValue)
177 const char *funcInfo = Q_FUNC_INFO;
179 if (!delegateValue || !Napi::Value(env(), delegateValue).IsObject()) {
180 qOhosPrintfError(
"%s: the delegate is not a JS object, not registering it", funcInfo);
184 auto registrationHandle = tryRegisterUiAbilityDelegate(QNapi::Object(env(), delegateValue));
185 if (!registrationHandle)
188 return QtOhos::makeDestroyNotifier(
189 [registrationHandle, jsThreadId = std::this_thread::get_id(), funcInfo]()
mutable {
190 if (std::this_thread::get_id() != jsThreadId) {
193 "%s: the delegate handle is released off the JS thread, waiting for the JS thread",
196 QOhosJsThreadGateway::runAndWait(
197 [&](QOhosJsState &) {
198 registrationHandle.reset();
std::shared_ptr< void > tryRegisterUiAbilityDelegate(QNapi::Object delegate)