1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "insight_intent_info_for_query.h"
17
18 #include "string_wrapper.h"
19 #include "hilog_tag_wrapper.h"
20 #include "json_util.h"
21
22 namespace OHOS {
23 namespace AbilityRuntime {
24 using JsonType = AppExecFwk::JsonType;
25 using ArrayType = AppExecFwk::ArrayType;
26 namespace {
27 int32_t g_parseResult = ERR_OK;
28 std::mutex g_extraMutex;
29
30 const std::map<AppExecFwk::ExecuteMode, std::string> EXECUTE_MODE_STRING_MAP = {
31 {AppExecFwk::ExecuteMode::UI_ABILITY_FOREGROUND, "UI_ABILITY_FOREGROUND"},
32 {AppExecFwk::ExecuteMode::UI_ABILITY_BACKGROUND, "UI_ABILITY_BACKGROUND"},
33 {AppExecFwk::ExecuteMode::UI_EXTENSION_ABILITY, "UI_EXTENSION_ABILITY"},
34 {AppExecFwk::ExecuteMode::SERVICE_EXTENSION_ABILITY, "SERVICE_EXTENSION_ABILITY"}
35 };
36 const std::map<std::string, AppExecFwk::ExecuteMode> STRING_EXECUTE_MODE_MAP = {
37 {"UI_ABILITY_FOREGROUND", AppExecFwk::ExecuteMode::UI_ABILITY_FOREGROUND},
38 {"UI_ABILITY_BACKGROUND", AppExecFwk::ExecuteMode::UI_ABILITY_BACKGROUND},
39 {"UI_EXTENSION_ABILITY", AppExecFwk::ExecuteMode::UI_EXTENSION_ABILITY},
40 {"SERVICE_EXTENSION_ABILITY", AppExecFwk::ExecuteMode::SERVICE_EXTENSION_ABILITY}
41 };
42 }
43
from_json(const nlohmann::json & jsonObject,LinkInfoForQuery & linkInfo)44 void from_json(const nlohmann::json &jsonObject, LinkInfoForQuery &linkInfo)
45 {
46 TAG_LOGD(AAFwkTag::INTENT, "LinkInfoForQuery from json");
47 const auto &jsonObjectEnd = jsonObject.end();
48 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
49 jsonObjectEnd,
50 INSIGHT_INTENTS_URI,
51 linkInfo.uri,
52 true,
53 g_parseResult);
54 }
55
to_json(nlohmann::json & jsonObject,const LinkInfoForQuery & info)56 void to_json(nlohmann::json& jsonObject, const LinkInfoForQuery &info)
57 {
58 TAG_LOGD(AAFwkTag::INTENT, "LinkInfoForQuery to json");
59 jsonObject = nlohmann::json {
60 {INSIGHT_INTENTS_URI, info.uri}
61 };
62 }
63
from_json(const nlohmann::json & jsonObject,PageInfoForQuery & pageInfo)64 void from_json(const nlohmann::json &jsonObject, PageInfoForQuery &pageInfo)
65 {
66 TAG_LOGD(AAFwkTag::INTENT, "PageInfoForQuery from json");
67 const auto &jsonObjectEnd = jsonObject.end();
68 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
69 jsonObjectEnd,
70 INSIGHT_INTENT_UI_ABILITY,
71 pageInfo.uiAbility,
72 true,
73 g_parseResult);
74 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
75 jsonObjectEnd,
76 INSIGHT_INTENT_PAGE_PATH,
77 pageInfo.pagePath,
78 true,
79 g_parseResult);
80 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
81 jsonObjectEnd,
82 INSIGHT_INTENT_NAVIGATION_ID,
83 pageInfo.navigationId,
84 true,
85 g_parseResult);
86 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
87 jsonObjectEnd,
88 INSIGHT_INTENT_NAV_DESTINATION_NAME,
89 pageInfo.navDestinationName,
90 true,
91 g_parseResult);
92 }
93
to_json(nlohmann::json & jsonObject,const PageInfoForQuery & info)94 void to_json(nlohmann::json& jsonObject, const PageInfoForQuery &info)
95 {
96 TAG_LOGD(AAFwkTag::INTENT, "PageInfoForQuery to json");
97 jsonObject = nlohmann::json {
98 {INSIGHT_INTENT_UI_ABILITY, info.uiAbility},
99 {INSIGHT_INTENT_PAGE_PATH, info.pagePath},
100 {INSIGHT_INTENT_NAVIGATION_ID, info.navigationId},
101 {INSIGHT_INTENT_NAV_DESTINATION_NAME, info.navDestinationName}
102 };
103 }
104
from_json(const nlohmann::json & jsonObject,EntryInfoForQuery & entryInfo)105 void from_json(const nlohmann::json &jsonObject, EntryInfoForQuery &entryInfo)
106 {
107 TAG_LOGD(AAFwkTag::INTENT, "EntryInfoForQuery from json");
108 const auto &jsonObjectEnd = jsonObject.end();
109 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
110 jsonObjectEnd,
111 INSIGHT_INTENT_ABILITY_NAME,
112 entryInfo.abilityName,
113 true,
114 g_parseResult);
115 if (jsonObject.find(INSIGHT_INTENT_EXECUTE_MODE) != jsonObjectEnd) {
116 const auto &modeArray = jsonObject[INSIGHT_INTENT_EXECUTE_MODE];
117 for (const auto &modeStr : modeArray) {
118 auto it = STRING_EXECUTE_MODE_MAP.find(modeStr.get<std::string>());
119 if (it != STRING_EXECUTE_MODE_MAP.end()) {
120 entryInfo.executeMode.push_back(it->second);
121 } else {
122 TAG_LOGW(AAFwkTag::INTENT, "Unknown ExecuteMode: %{public}s", modeStr.dump().c_str());
123 }
124 }
125 }
126 }
127
to_json(nlohmann::json & jsonObject,const EntryInfoForQuery & info)128 void to_json(nlohmann::json& jsonObject, const EntryInfoForQuery &info)
129 {
130 TAG_LOGD(AAFwkTag::INTENT, "EntryInfoForQuery to json");
131 std::vector<std::string> modeStrings;
132 for (const auto &mode : info.executeMode) {
133 auto it = EXECUTE_MODE_STRING_MAP.find(mode);
134 if (it != EXECUTE_MODE_STRING_MAP.end()) {
135 modeStrings.push_back(it->second);
136 } else {
137 modeStrings.push_back("UNKNOWN");
138 }
139 }
140 jsonObject = nlohmann::json {
141 {INSIGHT_INTENT_ABILITY_NAME, info.abilityName},
142 {INSIGHT_INTENT_EXECUTE_MODE, modeStrings}
143 };
144 }
145
from_json(const nlohmann::json & jsonObject,FormInfoForQuery & formInfo)146 void from_json(const nlohmann::json &jsonObject, FormInfoForQuery &formInfo)
147 {
148 TAG_LOGD(AAFwkTag::INTENT, "FormInfoForQuery from json");
149 const auto &jsonObjectEnd = jsonObject.end();
150 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
151 jsonObjectEnd,
152 INSIGHT_INTENT_ABILITY_NAME,
153 formInfo.abilityName,
154 true,
155 g_parseResult);
156 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
157 jsonObjectEnd,
158 INSIGHT_INTENT_FORM_NAME,
159 formInfo.formName,
160 true,
161 g_parseResult);
162 }
163
to_json(nlohmann::json & jsonObject,const FormInfoForQuery & info)164 void to_json(nlohmann::json& jsonObject, const FormInfoForQuery &info)
165 {
166 TAG_LOGD(AAFwkTag::INTENT, "FormInfoForQuery to json");
167 jsonObject = nlohmann::json {
168 {INSIGHT_INTENT_ABILITY_NAME, info.abilityName},
169 {INSIGHT_INTENT_FORM_NAME, info.formName}
170 };
171 }
172
from_json(const nlohmann::json & jsonObject,EntityInfoForQuery & entityInfo)173 void from_json(const nlohmann::json &jsonObject, EntityInfoForQuery &entityInfo)
174 {
175 TAG_LOGD(AAFwkTag::INTENT, "EntityInfoForQuery from json");
176 const auto &jsonObjectEnd = jsonObject.end();
177 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
178 jsonObjectEnd,
179 INSIGHT_INTENT_ENTITY_CLASS_NAME,
180 entityInfo.className,
181 true,
182 g_parseResult);
183 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
184 jsonObjectEnd,
185 INSIGHT_INTENT_ENTITY_ID,
186 entityInfo.entityId,
187 true,
188 g_parseResult);
189 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
190 jsonObjectEnd,
191 INSIGHT_INTENT_ENTITY_CATEGORY,
192 entityInfo.entityCategory,
193 false,
194 g_parseResult);
195 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
196 jsonObjectEnd,
197 INSIGHT_INTENT_ENTITY_PARAMETERS,
198 entityInfo.parameters,
199 false,
200 g_parseResult);
201 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
202 jsonObjectEnd,
203 INSIGHT_INTENT_ENTITY_PARENT_CLASS_NAME,
204 entityInfo.parentClassName,
205 false,
206 g_parseResult);
207 }
208
to_json(nlohmann::json & jsonObject,const EntityInfoForQuery & info)209 void to_json(nlohmann::json& jsonObject, const EntityInfoForQuery &info)
210 {
211 TAG_LOGD(AAFwkTag::INTENT, "EntityInfoForQuery to json");
212 jsonObject = nlohmann::json {
213 {INSIGHT_INTENT_ENTITY_CLASS_NAME, info.className},
214 {INSIGHT_INTENT_ENTITY_ID, info.entityId},
215 {INSIGHT_INTENT_ENTITY_CATEGORY, info.entityCategory},
216 {INSIGHT_INTENT_PARAMETERS, info.parameters},
217 {INSIGHT_INTENT_ENTITY_PARENT_CLASS_NAME, info.parentClassName}
218 };
219 }
220
from_json(const nlohmann::json & jsonObject,InsightIntentInfoForQuery & insightIntentInfo)221 void from_json(const nlohmann::json &jsonObject, InsightIntentInfoForQuery &insightIntentInfo)
222 {
223 TAG_LOGD(AAFwkTag::INTENT, "InsightIntentInfoForQuery from json");
224 const auto &jsonObjectEnd = jsonObject.end();
225 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
226 jsonObjectEnd,
227 INSIGHT_INTENT_BUNDLE_NAME,
228 insightIntentInfo.bundleName,
229 true,
230 g_parseResult);
231 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
232 jsonObjectEnd,
233 INSIGHT_INTENT_MODULE_NAME,
234 insightIntentInfo.moduleName,
235 true,
236 g_parseResult);
237 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
238 jsonObjectEnd,
239 INSIGHT_INTENT_INTENT_NAME,
240 insightIntentInfo.intentName,
241 true,
242 g_parseResult);
243 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
244 jsonObjectEnd,
245 INSIGHT_INTENT_DOMAIN,
246 insightIntentInfo.domain,
247 true,
248 g_parseResult);
249 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
250 jsonObjectEnd,
251 INSIGHT_INTENT_INTENT_VERSION,
252 insightIntentInfo.intentVersion,
253 true,
254 g_parseResult);
255 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
256 jsonObjectEnd,
257 INSIGHT_INTENT_DISPLAY_NAME,
258 insightIntentInfo.displayName,
259 true,
260 g_parseResult);
261 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
262 jsonObjectEnd,
263 INSIGHT_INTENT_DISPLAY_DESCRIPTION,
264 insightIntentInfo.displayDescription,
265 false,
266 g_parseResult);
267 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
268 jsonObjectEnd,
269 INSIGHT_INTENT_SCHEMA,
270 insightIntentInfo.schema,
271 false,
272 g_parseResult);
273 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
274 jsonObjectEnd,
275 INSIGHT_INTENT_ICON,
276 insightIntentInfo.icon,
277 false,
278 g_parseResult);
279 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
280 jsonObjectEnd,
281 INSIGHT_INTENT_LLM_DESCRIPTION,
282 insightIntentInfo.llmDescription,
283 false,
284 g_parseResult);
285 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
286 jsonObjectEnd,
287 INSIGHT_INTENT_INTENT_TYPE,
288 insightIntentInfo.intentType,
289 true,
290 g_parseResult);
291 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
292 jsonObjectEnd,
293 INSIGHT_INTENT_PARAMETERS,
294 insightIntentInfo.parameters,
295 false,
296 g_parseResult);
297 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
298 jsonObjectEnd,
299 INSIGHT_INTENT_RESULT,
300 insightIntentInfo.result,
301 false,
302 g_parseResult);
303 AppExecFwk::GetValueIfFindKey<std::vector<std::string>>(jsonObject,
304 jsonObjectEnd,
305 INSIGHT_INTENT_KEYWORDS,
306 insightIntentInfo.keywords,
307 JsonType::ARRAY,
308 false,
309 g_parseResult,
310 ArrayType::STRING);
311 AppExecFwk::GetValueIfFindKey<std::vector<EntityInfoForQuery>>(jsonObject,
312 jsonObjectEnd,
313 INSIGHT_INTENT_ENTITY_INFO,
314 insightIntentInfo.entities,
315 JsonType::ARRAY,
316 false,
317 g_parseResult,
318 ArrayType::OBJECT);
319
320 if (insightIntentInfo.intentType == INSIGHT_INTENTS_TYPE_LINK) {
321 AppExecFwk::GetValueIfFindKey<LinkInfoForQuery>(jsonObject,
322 jsonObjectEnd,
323 INSIGHT_INTENT_LINK_INFO,
324 insightIntentInfo.linkInfo,
325 JsonType::OBJECT,
326 false,
327 g_parseResult,
328 ArrayType::NOT_ARRAY);
329 } else if (insightIntentInfo.intentType == INSIGHT_INTENTS_TYPE_PAGE) {
330 AppExecFwk::GetValueIfFindKey<PageInfoForQuery>(jsonObject,
331 jsonObjectEnd,
332 INSIGHT_INTENT_PAGE_INFO,
333 insightIntentInfo.pageInfo,
334 JsonType::OBJECT,
335 false,
336 g_parseResult,
337 ArrayType::NOT_ARRAY);
338 } else if (insightIntentInfo.intentType == INSIGHT_INTENTS_TYPE_ENTRY) {
339 AppExecFwk::GetValueIfFindKey<EntryInfoForQuery>(jsonObject,
340 jsonObjectEnd,
341 INSIGHT_INTENT_ENTRY_INFO,
342 insightIntentInfo.entryInfo,
343 JsonType::OBJECT,
344 false,
345 g_parseResult,
346 ArrayType::NOT_ARRAY);
347 } else if (insightIntentInfo.intentType == INSIGHT_INTENTS_TYPE_FORM) {
348 AppExecFwk::GetValueIfFindKey<FormInfoForQuery>(jsonObject,
349 jsonObjectEnd,
350 INSIGHT_INTENT_FORM_INFO,
351 insightIntentInfo.formInfo,
352 JsonType::OBJECT,
353 false,
354 g_parseResult,
355 ArrayType::NOT_ARRAY);
356 }
357 }
358
to_json(nlohmann::json & jsonObject,const InsightIntentInfoForQuery & info)359 void to_json(nlohmann::json& jsonObject, const InsightIntentInfoForQuery &info)
360 {
361 TAG_LOGD(AAFwkTag::INTENT, "InsightIntentInfoForQuery to json");
362 jsonObject = nlohmann::json {
363 {INSIGHT_INTENT_BUNDLE_NAME, info.bundleName},
364 {INSIGHT_INTENT_MODULE_NAME, info.moduleName},
365 {INSIGHT_INTENT_INTENT_NAME, info.intentName},
366 {INSIGHT_INTENT_DOMAIN, info.domain},
367 {INSIGHT_INTENT_INTENT_VERSION, info.intentVersion},
368 {INSIGHT_INTENT_DISPLAY_NAME, info.displayName},
369 {INSIGHT_INTENT_DISPLAY_DESCRIPTION, info.displayDescription},
370 {INSIGHT_INTENT_SCHEMA, info.schema},
371 {INSIGHT_INTENT_ICON, info.icon},
372 {INSIGHT_INTENT_LLM_DESCRIPTION, info.llmDescription},
373 {INSIGHT_INTENT_INTENT_TYPE, info.intentType},
374 {INSIGHT_INTENT_PARAMETERS, info.parameters},
375 {INSIGHT_INTENT_RESULT, info.result},
376 {INSIGHT_INTENT_KEYWORDS, info.keywords},
377 {INSIGHT_INTENT_LINK_INFO, info.linkInfo},
378 {INSIGHT_INTENT_PAGE_INFO, info.pageInfo},
379 {INSIGHT_INTENT_ENTRY_INFO, info.entryInfo},
380 {INSIGHT_INTENT_FORM_INFO, info.formInfo},
381 {INSIGHT_INTENT_ENTITY_INFO, info.entities}
382 };
383 }
384
ReadFromParcel(Parcel & parcel)385 bool InsightIntentInfoForQuery::ReadFromParcel(Parcel &parcel)
386 {
387 MessageParcel *messageParcel = reinterpret_cast<MessageParcel *>(&parcel);
388 if (!messageParcel) {
389 TAG_LOGE(AAFwkTag::INTENT, "Type conversion failed");
390 return false;
391 }
392 uint32_t length = messageParcel->ReadUint32();
393 if (length == 0) {
394 TAG_LOGE(AAFwkTag::INTENT, "Invalid data length");
395 return false;
396 }
397 const char *data = reinterpret_cast<const char *>(messageParcel->ReadRawData(length));
398 TAG_LOGD(AAFwkTag::INTENT, "ReadFromParcel data: %{public}s", data);
399 if (!data) {
400 TAG_LOGE(AAFwkTag::INTENT, "Fail read raw length = %{public}d", length);
401 return false;
402 }
403 nlohmann::json jsonObject = nlohmann::json::parse(data, nullptr, false);
404 if (jsonObject.is_discarded()) {
405 TAG_LOGE(AAFwkTag::INTENT, "failed to parse BundleInfo");
406 return false;
407 }
408 std::lock_guard<std::mutex> lock(g_extraMutex);
409 g_parseResult = ERR_OK;
410 *this = jsonObject.get<InsightIntentInfoForQuery>();
411 if (g_parseResult != ERR_OK) {
412 TAG_LOGE(AAFwkTag::INTENT, "parse result: %{public}d", g_parseResult);
413 g_parseResult = ERR_OK;
414 return false;
415 }
416 return true;
417 }
418
Marshalling(Parcel & parcel) const419 bool InsightIntentInfoForQuery::Marshalling(Parcel &parcel) const
420 {
421 MessageParcel *messageParcel = reinterpret_cast<MessageParcel *>(&parcel);
422 if (!messageParcel) {
423 TAG_LOGE(AAFwkTag::INTENT, "Conversion failed");
424 return false;
425 }
426 nlohmann::json jsonObject = *this;
427 std::string str = jsonObject.dump();
428 TAG_LOGD(AAFwkTag::INTENT, "Marshalling str: %{public}s", str.c_str());
429 if (!messageParcel->WriteUint32(str.size() + 1)) {
430 TAG_LOGE(AAFwkTag::INTENT, "Write intent info size failed");
431 return false;
432 }
433 if (!messageParcel->WriteRawData(str.c_str(), str.size() + 1)) {
434 TAG_LOGE(AAFwkTag::INTENT, "Write intent info failed");
435 return false;
436 }
437 return true;
438 }
439
Unmarshalling(Parcel & parcel)440 InsightIntentInfoForQuery *InsightIntentInfoForQuery::Unmarshalling(Parcel &parcel)
441 {
442 InsightIntentInfoForQuery *info = new (std::nothrow) InsightIntentInfoForQuery();
443 if (info == nullptr) {
444 return nullptr;
445 }
446
447 if (!info->ReadFromParcel(parcel)) {
448 delete info;
449 info = nullptr;
450 }
451 return info;
452 }
453 } // namespace AbilityRuntime
454 } // namespace OHOS
455