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
qohoswantinfo.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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
6#include <QtHarmonyExtras/private/qohosharmonyextrasenums_p.h>
7
8#include <QtCore/qbytearray.h>
9#include <QtCore/qcoreapplication_platform.h>
10#include <QtCore/qharmonyappjscontext.h>
11#include <QtCore/qharmonyjscontext.h>
12#include <QtCore/qharmonyjsonconversions.h>
13#include <QtCore/qjsonobject.h>
14#include <QtCore/qlist.h>
15#include <QtCore/qstring.h>
16#include <QtCore/qvariant.h>
17#include <QtCore/private/qcore_ohos_p.h>
18#include <QtCore/private/qnapi_p.h>
19#include <QtCore/private/qohoscommon_p.h>
20#include <QtCore/private/qohoslogger_p.h>
21
22#include <cstddef>
23#include <cstdint>
24#include <memory>
25#include <optional>
26#include <string>
27#include <vector>
28
29#include <database/udmf/udmf_meta.h>
30#include <database/udmf/utd.h>
31
32QT_BEGIN_NAMESPACE
33
34namespace QtHarmonyExtras::Private {
35
36detail::WantInfoPriv::WantInfoPriv() = default;
37detail::WantInfoPriv::~WantInfoPriv() = default;
38
39namespace {
40
41template<typename T>
42std::optional<T> getOptionalProperty(const QNapi::Object &object, const std::string &propName)
43{
44 auto optPropValue = QNapi::getOptionalPropOrEmpty<T>(object, propName);
45 return !optPropValue.IsEmpty()
46 ? std::make_optional(optPropValue)
47 : std::nullopt;
48}
49
50std::optional<std::string> tryMapUtdTypeIdToMimeType(const std::string &utdTypeId)
51{
52 std::shared_ptr<::OH_Utd> utd(
53 ::OH_Utd_Create(utdTypeId.c_str()),
54 [](::OH_Utd *utd) {
55 if (utd != nullptr)
56 ::OH_Utd_Destroy(utd);
57 });
58
59 std::vector<std::string> mimeTypes;
60 if (utd) {
61 unsigned int typesCount = 0;
62 const char **rawMimeTypes = ::OH_Utd_GetMimeTypes(utd.get(), &typesCount);
63 if (rawMimeTypes == nullptr && typesCount != 0) {
64 qOhosReportFatalErrorAndAbort(
65 "%s: got inconsistent result from OH_Utd_GetMimeTypes() call: array is null, size is %u",
66 Q_FUNC_INFO, typesCount);
67 }
68 if (rawMimeTypes != nullptr)
69 mimeTypes = std::vector<std::string>(rawMimeTypes, rawMimeTypes + typesCount);
70 }
71
72 return !mimeTypes.empty()
73 ? std::make_optional(mimeTypes.front())
74 : std::nullopt;
75}
76
77std::optional<detail::SharedRecord> tryConvertNapiObjectToSharedRecord(QNapi::Object record)
78{
79 auto tryGetOptionalStringProp = [](const QNapi::Object &object, const std::string &propName) -> std::optional<QString> {
80 auto optProp = getOptionalProperty<QNapi::String>(object, propName);
81 return optProp.has_value()
82 ? std::make_optional(QString::fromStdString(optProp.value()))
83 : std::nullopt;
84 };
85
86 auto tryGetOptionalByteArrayProp = [](const QNapi::Object &object, const std::string &propName) -> std::optional<QByteArray> {
87 auto optProp = getOptionalProperty<QNapi::TypedArrayOf<std::uint8_t>>(object, propName);
88 return optProp.has_value()
89 ? std::make_optional(QByteArray(
90 reinterpret_cast<const char *>(optProp.value().Data()),
91 optProp.value().ByteLength()))
92 : std::nullopt;
93 };
94
95 auto tryGetOptionalJsonObjectProp = [](const QNapi::Object &object, const std::string &propName) -> std::optional<QJsonObject> {
96 auto optProp = getOptionalProperty<QNapi::Object>(object, propName);
97 return optProp.has_value()
98 ? std::make_optional(QtHarmony::mapJsObjectToQJsonObject(optProp.value().Env(), optProp.value()))
99 : std::nullopt;
100 };
101
102 std::string utd = record.get<QNapi::String>("utd");
103 auto optMimeType = utd != UDMF_META_HYPERLINK
104 ? tryMapUtdTypeIdToMimeType(utd)
105 : std::optional<std::string>("text/uri-list");
106 if (!optMimeType.has_value()) {
107 qOhosPrintfWarning(
108 "%s: can't map utd '%s' to mimetype, not mapping the record",
109 Q_FUNC_INFO, utd.c_str());
110 return std::nullopt;
111 }
112
113 auto content = tryGetOptionalStringProp(record, "content");
114 auto uri = tryGetOptionalStringProp(record, "uri");
115 if (!content.has_value() && !uri.has_value()) {
116 qOhosPrintfWarning(
117 "%s: cannot create Shared Record, content and uri properties are empty", Q_FUNC_INFO);
118 return std::nullopt;
119 }
120
121 auto optExtraDataJson = tryGetOptionalJsonObjectProp(record, "extraData");
122
123 return std::make_optional(
124 detail::SharedRecord{
125 .mimeType = QString::fromStdString(optMimeType.value()),
126 .content = content,
127 .filePath = uri,
128 .title = tryGetOptionalStringProp(record, "title"),
129 .label = tryGetOptionalStringProp(record, "label"),
130 .description = tryGetOptionalStringProp(record, "description"),
131 .thumbnail = tryGetOptionalByteArrayProp(record, "thumbnail"),
132 .thumbnailFilePath = tryGetOptionalStringProp(record, "thumbnailUri"),
133 .extraData = optExtraDataJson.has_value()
134 ? std::make_optional(optExtraDataJson.value().toVariantMap())
135 : std::nullopt,
136 });
137}
138
139std::optional<detail::WantInfoPriv::LaunchReason> tryMapOhosLaunchReasonToWantInfoEnum(
140 QtOhosHarmonyExtras::enums::ohos::app::ability::AbilityConstant::LaunchReason ohosLaunchReason)
141{
142 using OhosLaunchReason = QtOhosHarmonyExtras::enums::ohos::app::ability::AbilityConstant::LaunchReason;
143 using WantInfoPriv = detail::WantInfoPriv;
144
145 switch (ohosLaunchReason) {
146 case OhosLaunchReason::START_ABILITY:
147 return std::make_optional(WantInfoPriv::LaunchReason::START_ABILITY);
148 case OhosLaunchReason::CONTINUATION:
149 return std::make_optional(WantInfoPriv::LaunchReason::CONTINUATION);
150 case OhosLaunchReason::PREPARE_CONTINUATION:
151 return std::make_optional(WantInfoPriv::LaunchReason::PREPARE_CONTINUATION);
152 case OhosLaunchReason::PRELOAD:
153 return std::make_optional(WantInfoPriv::LaunchReason::PRELOAD);
154 case OhosLaunchReason::UNKNOWN:
155 case OhosLaunchReason::CALL:
156 case OhosLaunchReason::APP_RECOVERY:
157 case OhosLaunchReason::SHARE:
158 case OhosLaunchReason::AUTO_STARTUP:
159 case OhosLaunchReason::INSIGHT_INTENT:
160 return std::make_optional(WantInfoPriv::LaunchReason::UNKNOWN);
161 }
162
163 return {};
164}
165
166detail::WantInfoPriv::LaunchReason mapJsLaunchReasonToWantInfoEnumWithFallback(
167 QOhosJsState &jsState, QNapi::Number jsLaunchReason)
168{
169 auto optLaunchReasonJsEnum =
170 jsState.tryMapOhosEnumFromJs<QtOhosHarmonyExtras::enums::ohos::app::ability::AbilityConstant::LaunchReason>(jsLaunchReason);
171 auto optLaunchReason =
172 optLaunchReasonJsEnum.has_value()
173 ? tryMapOhosLaunchReasonToWantInfoEnum(optLaunchReasonJsEnum.value())
174 : std::nullopt;
175 return optLaunchReason.value_or(detail::WantInfoPriv::LaunchReason::UNKNOWN);
176}
177
178class WantInfoImpl : public detail::WantInfoPriv
179{
180public:
181 WantInfoImpl(QNapi::Object want, LaunchReason launchReason);
182
183 QJsonObject jsonObject() const override;
184
185 std::optional<QList<detail::SharedRecord>> tryGetSharedDataRecords() const override;
186
187 std::optional<ContactInfo> tryGetContactInfo() const override;
188
189 LaunchReason launchReason() const override;
190
191private:
192 struct JsScopeData
193 {
194 QNapi::Reference<QNapi::Object> want;
195 };
196
197 std::shared_ptr<JsScopeData> m_jsScopeData;
198 QJsonObject m_jsonObject;
199 LaunchReason m_launchReason;
200};
201
202WantInfoImpl::WantInfoImpl(QNapi::Object want, LaunchReason launchReason)
204 , m_jsScopeData(
205 QtOhos::makeProxyWithJsThreadDeleter(
206 QtOhos::moveToSharedPtr(
207 JsScopeData {
208 .want = QNapi::Reference<>::makePersistentFrom(want),
209 })))
210 , m_jsonObject(QtHarmony::mapJsObjectToQJsonObject(want.Env(), want))
211 , m_launchReason(launchReason)
212{
213}
214
215QJsonObject WantInfoImpl::jsonObject() const
216{
217 return m_jsonObject;
218}
219
220std::optional<QList<detail::SharedRecord>> WantInfoImpl::tryGetSharedDataRecords() const
221{
222 using SharedRecord = detail::SharedRecord;
223
224 return QOhosJsThreadGateway::evalWithPromise<std::optional<QList<SharedRecord>>>(
225 [&](QOhosJsState &jsState, QOhosTaskPromise<std::optional<QList<SharedRecord>>> evalPromise) {
226 auto thenCatchPromises = std::move(evalPromise).makeThenCatchBranches(Q_FUNC_INFO);
227 jsState.evalToPromiseOrRejectOnThrow(
228 "@kit.ShareKit.systemShare.getSharedData(*)", {m_jsScopeData->want.Value()})
229 .onThen(
230 [thenPromise = std::move(thenCatchPromises.first)](const QOhosCallbackInfo &cbInfo) {
231 QNapi::Object sharedData = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
232
233 auto optRecords = QNapi::getArrayElements<QList<std::optional<SharedRecord>>, QNapi::Object>(
234 sharedData.eval<QNapi::Array>("getRecords()"), &tryConvertNapiObjectToSharedRecord);
235
236 QList<SharedRecord> records;
237 for (const auto &optRecord : optRecords) {
238 if (optRecord.has_value())
239 records.append(optRecord.value());
240 }
241
242 std::size_t unconvertedRecordsCount = optRecords.size() - records.size();
243 if (unconvertedRecordsCount != 0) {
244 qOhosPrintfWarning(
245 "%s: can't convert %zu Shared Records, ignoring them",
246 Q_FUNC_INFO, unconvertedRecordsCount);
247 }
248
249 thenPromise(std::make_optional(records));
250 })
251 .onCatch(
252 [catchPromise = std::move(thenCatchPromises.second)](const QOhosCallbackInfo &cbInfo) {
253 QtOhos::logJsCallbackError(cbInfo, "@kit.ShareKit.systemShare.getSharedData() failed");
254 catchPromise(std::nullopt);
255 });
256 },
257 Q_FUNC_INFO);
258}
259
260std::optional<detail::WantInfoPriv::ContactInfo> WantInfoImpl::tryGetContactInfo() const
261{
262 using ContactInfo = detail::WantInfoPriv::ContactInfo;
263
264 return QOhosJsThreadGateway::evalWithPromise<std::optional<ContactInfo>>(
265 [&](QOhosJsState &jsState, QOhosTaskPromise<std::optional<ContactInfo>> evalPromise) {
266 auto thenCatchPromises = std::move(evalPromise).makeThenCatchBranches(Q_FUNC_INFO);
267 jsState.evalToPromiseOrRejectOnThrow(
268 "@kit.ShareKit.systemShare.getContactInfo(*)", {m_jsScopeData->want.Value()})
269 .onThen(
270 [thenPromise = std::move(thenCatchPromises.first)](const QOhosCallbackInfo &cbInfo) {
271 auto contactInfoObj = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
272 ContactInfo contactInfo = {
273 .contactType = QString::fromStdString(
274 contactInfoObj.get<QNapi::String>("contactType")),
275 .contactId = QString::fromStdString(
276 contactInfoObj.get<QNapi::String>("contactId")),
277 };
278 thenPromise(std::make_optional(contactInfo));
279 })
280 .onCatch(
281 [catchPromise = std::move(thenCatchPromises.second)](const QOhosCallbackInfo &cbInfo) {
282 QtOhos::logJsCallbackError(cbInfo, "@kit.ShareKit.systemShare.getContactInfo() failed");
283 catchPromise(std::nullopt);
284 });
285 },
286 Q_FUNC_INFO);
287}
288
289detail::WantInfoPriv::LaunchReason WantInfoImpl::launchReason() const
290{
291 return m_launchReason;
292}
293
294}
295
297{
298 return QOhosJsThreadGateway::eval(
299 [](QOhosJsState &jsState) {
300 auto optAppLaunchParam = jsState.optAppLaunchParam();
301 auto optAppLaunchReason = optAppLaunchParam.has_value()
302 ? std::make_optional(mapJsLaunchReasonToWantInfoEnumWithFallback(
303 jsState, optAppLaunchParam.value().get<QNapi::Number>("launchReason")))
304 : std::nullopt;
305 auto appLaunchReason = optAppLaunchReason.value_or(detail::WantInfoPriv::LaunchReason::UNKNOWN);
306 return QSharedPointer<WantInfoImpl>::create(jsState.appLaunchWant(), appLaunchReason);
307 },
308 Q_FUNC_INFO);
309}
310
311void addNewWantConsumer(QObject *context, QOhosConsumer<QJsonObject> wantConsumer)
312{
313 auto sharedWantConsumer = QtOhos::moveToSharedPtr(std::move(wantConsumer));
314 addNewWantConsumer(
315 context,
316 [sharedWantConsumer](QSharedPointer<detail::WantInfoPriv> wantInfo) {
317 (*sharedWantConsumer)(wantInfo->jsonObject());
318 });
319}
320
322 QObject *context, QOhosConsumer<QSharedPointer<detail::WantInfoPriv>> wantConsumer)
323{
324 auto contextRef = QtOhos::makeQThreadSafeRef(context);
325 auto sharedWantConsumer = QtOhos::moveToSharedPtr(std::move(wantConsumer));
326 auto consumerHandle = QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait(
327 [&](QHarmonyJsContext &jsContext) {
328 auto delegate = QNapi::Object::New(jsContext.env());
329 delegate.set(
330 "onNewWant",
331 [contextRef, sharedWantConsumer](const QOhosCallbackInfo &cbInfo) {
332 QNapi::Object ability;
333 QNapi::Object want;
334 QNapi::Object launchParam;
335 cbInfo.getLeadingArgs(Q_FUNC_INFO, ability, want, launchParam);
336
337 auto launchReason = mapJsLaunchReasonToWantInfoEnumWithFallback(
338 cbInfo.jsState(), launchParam.get<QNapi::Number>("launchReason"));
339 auto wantInfo = QSharedPointer<WantInfoImpl>::create(want, launchReason);
340 contextRef.visitInQtThreadIfAlive(
341 [sharedWantConsumer, wantInfo](auto &) {
342 (*sharedWantConsumer)(wantInfo);
343 });
344 });
345 return QHarmonyAppJsContext::fromJsContext(jsContext).addUiAbilityDelegate(delegate);
346 });
347
348 QObject::connect(
349 context, &QObject::destroyed,
350 [consumerHandle = std::move(consumerHandle)]() mutable {
351 consumerHandle.reset();
352 });
353}
354
355}
356
357QT_END_NAMESPACE
void addNewWantConsumer(QObject *context, QOhosConsumer< QJsonObject > wantConsumer)
QSharedPointer< detail::WantInfoPriv > makeAppLaunchWantInfo()