1 /*
2 * Copyright (c) 2023-2024 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_utils.h"
17
18 #include "ability_manager_errors.h"
19 #include "bundle_mgr_helper.h"
20 #include "hilog_tag_wrapper.h"
21 #include "in_process_call_wrapper.h"
22 #include "insight_intent_profile.h"
23 #include "os_account_manager_wrapper.h"
24
25 namespace OHOS {
26 namespace AbilityRuntime {
27 namespace {
CheckAbilityName(const InsightIntentInfo & info,const std::string & abilityName,const AppExecFwk::ExecuteMode & executeMode)28 bool CheckAbilityName(const InsightIntentInfo &info, const std::string &abilityName,
29 const AppExecFwk::ExecuteMode &executeMode)
30 {
31 bool matched = false;
32 switch (executeMode) {
33 case AppExecFwk::ExecuteMode::UI_ABILITY_FOREGROUND:
34 case AppExecFwk::ExecuteMode::UI_ABILITY_BACKGROUND:
35 matched = info.uiAbilityIntentInfo.abilityName == abilityName;
36 break;
37 case AppExecFwk::ExecuteMode::UI_EXTENSION_ABILITY:
38 matched = info.uiExtensionIntentInfo.abilityName == abilityName;
39 break;
40 case AppExecFwk::ExecuteMode::SERVICE_EXTENSION_ABILITY:
41 matched = info.serviceExtensionIntentInfo.abilityName == abilityName;
42 break;
43 default:
44 break;
45 }
46 if (!matched) {
47 TAG_LOGW(AAFwkTag::INTENT, "ability name mismatch");
48 }
49 return matched;
50 }
51 } // namespace
52
GetSrcEntry(const AppExecFwk::ElementName & elementName,const std::string & intentName,const AppExecFwk::ExecuteMode & executeMode,std::string & srcEntry)53 uint32_t InsightIntentUtils::GetSrcEntry(const AppExecFwk::ElementName &elementName, const std::string &intentName,
54 const AppExecFwk::ExecuteMode &executeMode, std::string &srcEntry)
55 {
56 TAG_LOGD(AAFwkTag::INTENT, "get srcEntry, elementName: %{public}s, intentName: %{public}s, mode: %{public}d",
57 elementName.GetURI().c_str(), intentName.c_str(), executeMode);
58 auto bundleName = elementName.GetBundleName();
59 auto moduleName = elementName.GetModuleName();
60 auto abilityName = elementName.GetAbilityName();
61 if (bundleName.empty() || moduleName.empty() || abilityName.empty() || intentName.empty()) {
62 TAG_LOGE(AAFwkTag::INTENT, "input param empty");
63 return ERR_INVALID_VALUE;
64 }
65
66 auto bundleMgrHelper = DelayedSingleton<AppExecFwk::BundleMgrHelper>::GetInstance();
67 if (bundleMgrHelper == nullptr) {
68 return ERR_NULL_OBJECT;
69 }
70
71 // Get json profile firstly
72 std::string profile;
73 auto ret = IN_PROCESS_CALL(bundleMgrHelper->GetJsonProfile(AppExecFwk::INTENT_PROFILE, bundleName, moduleName,
74 profile, AppExecFwk::OsAccountManagerWrapper::GetCurrentActiveAccountId()));
75 if (ret != ERR_OK) {
76 TAG_LOGE(AAFwkTag::INTENT, "failed code: %{public}d", ret);
77 return AAFwk::ERR_INSIGHT_INTENT_GET_PROFILE_FAILED;
78 }
79
80 // Transform json string
81 std::vector<InsightIntentInfo> infos;
82 if (!InsightIntentProfile::TransformTo(profile, infos)) {
83 TAG_LOGE(AAFwkTag::INTENT, "transform profile failed");
84 return ERR_INVALID_VALUE;
85 }
86
87 // Get srcEntry when intentName matched
88 for (const auto &info: infos) {
89 if (info.intentName == intentName && CheckAbilityName(info, abilityName, executeMode)) {
90 srcEntry = info.srcEntry;
91 TAG_LOGD(AAFwkTag::INTENT, "srcEntry: %{public}s", srcEntry.c_str());
92 return ERR_OK;
93 }
94 }
95
96 TAG_LOGE(AAFwkTag::INTENT, "get srcEntry failed");
97 return AAFwk::ERR_INSIGHT_INTENT_START_INVALID_COMPONENT;
98 }
ConvertExtractInsightIntentGenericInfo(ExtractInsightIntentGenericInfo & genericInfo,InsightIntentInfoForQuery & queryInfo)99 uint32_t InsightIntentUtils::ConvertExtractInsightIntentGenericInfo(
100 ExtractInsightIntentGenericInfo &genericInfo, InsightIntentInfoForQuery &queryInfo)
101 {
102 queryInfo.bundleName = genericInfo.bundleName;
103 queryInfo.moduleName = genericInfo.moduleName;
104 queryInfo.intentName = genericInfo.intentName;
105 queryInfo.displayName = genericInfo.displayName;
106 queryInfo.intentType = genericInfo.decoratorType;
107 if (genericInfo.decoratorType == INSIGHT_INTENTS_DECORATOR_TYPE_LINK) {
108 auto linkInfo = genericInfo.get<InsightIntentLinkInfo>();
109 queryInfo.linkInfo.uri = linkInfo.uri;
110 queryInfo.parameters = linkInfo.parameters;
111 } else if (genericInfo.decoratorType == INSIGHT_INTENTS_DECORATOR_TYPE_PAGE) {
112 auto pageInfo = genericInfo.get<InsightIntentPageInfo>();
113 queryInfo.pageInfo.uiAbility = pageInfo.uiAbility;
114 queryInfo.pageInfo.pagePath = pageInfo.pagePath;
115 queryInfo.pageInfo.navigationId = pageInfo.navigationId;
116 queryInfo.pageInfo.navDestinationName = pageInfo.navDestinationName;
117 queryInfo.parameters = pageInfo.parameters;
118 } else if (genericInfo.decoratorType == INSIGHT_INTENTS_DECORATOR_TYPE_ENTRY) {
119 auto entryInfo = genericInfo.get<InsightIntentEntryInfo>();
120 queryInfo.entryInfo.abilityName = entryInfo.abilityName;
121 for (auto mode : entryInfo.executeMode) {
122 queryInfo.entryInfo.executeMode.emplace_back(mode);
123 }
124 queryInfo.parameters = entryInfo.parameters;
125 } else if (genericInfo.decoratorType == INSIGHT_INTENTS_DECORATOR_TYPE_FUNCTION) {
126 auto functionInfo = genericInfo.get<InsightIntentFunctionInfo>();
127 queryInfo.parameters = functionInfo.parameters;
128 } else if (genericInfo.decoratorType == INSIGHT_INTENTS_DECORATOR_TYPE_FORM) {
129 auto formInfo = genericInfo.get<InsightIntentFormInfo>();
130 queryInfo.formInfo.abilityName = formInfo.abilityName;
131 queryInfo.formInfo.formName = formInfo.formName;
132 queryInfo.parameters = formInfo.parameters;
133 } else {
134 TAG_LOGE(AAFwkTag::INTENT, "invalid decoratorType:%{public}s", genericInfo.decoratorType.c_str());
135 return ERR_INVALID_VALUE;
136 }
137 return ERR_OK;
138 }
139
ConvertExtractInsightIntentInfo(ExtractInsightIntentInfo & intentInfo,InsightIntentInfoForQuery & queryInfo,bool getEntity)140 uint32_t InsightIntentUtils::ConvertExtractInsightIntentInfo(
141 ExtractInsightIntentInfo &intentInfo, InsightIntentInfoForQuery &queryInfo, bool getEntity)
142 {
143 ConvertExtractInsightIntentGenericInfo(intentInfo.genericInfo, queryInfo);
144 queryInfo.domain = intentInfo.domain;
145 queryInfo.intentVersion = intentInfo.intentVersion;
146 queryInfo.displayDescription = intentInfo.displayDescription;
147 queryInfo.schema = intentInfo.schema;
148 queryInfo.icon = intentInfo.icon;
149 queryInfo.llmDescription = intentInfo.llmDescription;
150 queryInfo.result = intentInfo.result;
151
152 for (auto &keyword : intentInfo.keywords) {
153 queryInfo.keywords.emplace_back(keyword);
154 }
155
156 if (getEntity) {
157 for (auto &entityInfo : intentInfo.entities) {
158 EntityInfoForQuery insightInfo;
159 insightInfo.className = entityInfo.className;
160 insightInfo.entityCategory = entityInfo.entityCategory;
161 insightInfo.entityId = entityInfo.entityId;
162 insightInfo.parameters = entityInfo.parameters;
163 insightInfo.parentClassName = entityInfo.parentClassName;
164 queryInfo.entities.emplace_back(insightInfo);
165 }
166 }
167
168 return ERR_OK;
169 }
170
ConvertExtractInsightIntentEntityInfo(ExtractInsightIntentInfo & intentInfo,InsightIntentInfoForQuery & queryInfo)171 uint32_t InsightIntentUtils::ConvertExtractInsightIntentEntityInfo(
172 ExtractInsightIntentInfo &intentInfo, InsightIntentInfoForQuery &queryInfo)
173 {
174 ConvertExtractInsightIntentGenericInfo(intentInfo.genericInfo, queryInfo);
175
176 for (auto &entityInfo : intentInfo.entities) {
177 EntityInfoForQuery insightInfo;
178 insightInfo.className = entityInfo.className;
179 insightInfo.entityCategory = entityInfo.entityCategory;
180 insightInfo.entityId = entityInfo.entityId;
181 insightInfo.parameters = entityInfo.parameters;
182 insightInfo.parentClassName = entityInfo.parentClassName;
183 queryInfo.entities.emplace_back(insightInfo);
184 }
185
186 return ERR_OK;
187 }
188 } // namespace AbilityRuntime
189 } // namespace OHOS
190