6#include <QtHarmonyExtras/private/qohosharmonyextrasenums_p.h>
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>
29#include <database/udmf/udmf_meta.h>
30#include <database/udmf/utd.h>
42std::optional<T> getOptionalProperty(
const QNapi::Object &object,
const std::string &propName)
44 auto optPropValue = QNapi::getOptionalPropOrEmpty<T>(object, propName);
45 return !optPropValue.IsEmpty()
46 ?
std::make_optional(optPropValue)
50std::optional<
std::string> tryMapUtdTypeIdToMimeType(
const std::string &utdTypeId)
52 std::shared_ptr<::OH_Utd> utd(
53 ::OH_Utd_Create(utdTypeId.c_str()),
56 ::OH_Utd_Destroy(utd);
59 std::vector<std::string> mimeTypes;
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);
68 if (rawMimeTypes !=
nullptr)
69 mimeTypes = std::vector<std::string>(rawMimeTypes, rawMimeTypes + typesCount);
72 return !mimeTypes.empty()
73 ?
std::make_optional(mimeTypes.front())
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()))
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()))
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()))
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()) {
108 "%s: can't map utd '%s' to mimetype, not mapping the record",
109 Q_FUNC_INFO, utd.c_str());
113 auto content = tryGetOptionalStringProp(record,
"content");
114 auto uri = tryGetOptionalStringProp(record,
"uri");
115 if (!content.has_value() && !uri.has_value()) {
117 "%s: cannot create Shared Record, content and uri properties are empty", Q_FUNC_INFO);
121 auto optExtraDataJson = tryGetOptionalJsonObjectProp(record,
"extraData");
123 return std::make_optional(
124 detail::SharedRecord{
125 .mimeType = QString::fromStdString(optMimeType.value()),
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())
140 QtOhosHarmonyExtras::enums::ohos::app::ability::AbilityConstant::LaunchReason ohosLaunchReason)
142 using OhosLaunchReason = QtOhosHarmonyExtras::enums::ohos::app::ability::AbilityConstant::LaunchReason;
145 switch (ohosLaunchReason) {
146 case OhosLaunchReason::START_ABILITY:
148 case OhosLaunchReason::CONTINUATION:
150 case OhosLaunchReason::PREPARE_CONTINUATION:
152 case OhosLaunchReason::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:
167 QOhosJsState &jsState, QNapi::Number jsLaunchReason)
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())
181 WantInfoImpl(QNapi::Object want,
LaunchReason launchReason);
183 QJsonObject jsonObject()
const override;
185 std::optional<QList<detail::SharedRecord>> tryGetSharedDataRecords()
const override;
194 QNapi::Reference<QNapi::Object> want;
197 std::shared_ptr<JsScopeData> m_jsScopeData;
198 QJsonObject m_jsonObject;
202WantInfoImpl::WantInfoImpl(QNapi::Object want,
LaunchReason launchReason)
205 QtOhos::makeProxyWithJsThreadDeleter(
206 QtOhos::moveToSharedPtr(
208 .want = QNapi::Reference<>::makePersistentFrom(want),
210 , m_jsonObject(QtHarmony::mapJsObjectToQJsonObject(want.Env(), want))
211 , m_launchReason(launchReason)
215QJsonObject WantInfoImpl::jsonObject()
const
220std::optional<QList<detail::SharedRecord>> WantInfoImpl::tryGetSharedDataRecords()
const
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()})
230 [thenPromise = std::move(thenCatchPromises.first)](
const QOhosCallbackInfo &cbInfo) {
231 QNapi::Object sharedData = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
233 auto optRecords = QNapi::getArrayElements<QList<std::optional<SharedRecord>>, QNapi::Object>(
234 sharedData.eval<QNapi::Array>(
"getRecords()"), &tryConvertNapiObjectToSharedRecord);
236 QList<SharedRecord> records;
237 for (
const auto &optRecord : optRecords) {
238 if (optRecord.has_value())
239 records.append(optRecord.value());
242 std::size_t unconvertedRecordsCount = optRecords.size() - records.size();
243 if (unconvertedRecordsCount != 0) {
245 "%s: can't convert %zu Shared Records, ignoring them",
246 Q_FUNC_INFO, unconvertedRecordsCount);
249 thenPromise(std::make_optional(records));
252 [catchPromise = std::move(thenCatchPromises.second)](
const QOhosCallbackInfo &cbInfo) {
253 QtOhos::logJsCallbackError(cbInfo,
"@kit.ShareKit.systemShare.getSharedData() failed");
254 catchPromise(std::nullopt);
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()})
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")),
278 thenPromise(std::make_optional(contactInfo));
281 [catchPromise = std::move(thenCatchPromises.second)](
const QOhosCallbackInfo &cbInfo) {
282 QtOhos::logJsCallbackError(cbInfo,
"@kit.ShareKit.systemShare.getContactInfo() failed");
283 catchPromise(std::nullopt);
291 return m_launchReason;
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")))
305 auto appLaunchReason = optAppLaunchReason.value_or(detail::WantInfoPriv::LaunchReason::UNKNOWN);
306 return QSharedPointer<WantInfoImpl>::create(jsState.appLaunchWant(), appLaunchReason);
313 auto sharedWantConsumer = QtOhos::moveToSharedPtr(std::move(wantConsumer));
316 [sharedWantConsumer](QSharedPointer<detail::WantInfoPriv> wantInfo) {
317 (*sharedWantConsumer)(wantInfo->jsonObject());
322 QObject *context, QOhosConsumer<QSharedPointer<detail::WantInfoPriv>> wantConsumer)
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());
331 [contextRef, sharedWantConsumer](
const QOhosCallbackInfo &cbInfo) {
332 QNapi::Object ability;
334 QNapi::Object launchParam;
335 cbInfo.getLeadingArgs(Q_FUNC_INFO, ability, want, launchParam);
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);
345 return QHarmonyAppJsContext::fromJsContext(jsContext).addUiAbilityDelegate(delegate);
349 context, &QObject::destroyed,
350 [consumerHandle = std::move(consumerHandle)]()
mutable {
351 consumerHandle.reset();