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_profile.h"
17
18 #include "hilog_tag_wrapper.h"
19 #include "json_util.h"
20
21 namespace OHOS {
22 namespace AbilityRuntime {
23 using JsonType = AppExecFwk::JsonType;
24 using ArrayType = AppExecFwk::ArrayType;
25
26 namespace {
27 int32_t g_parseResult = ERR_OK;
28 std::mutex g_mutex;
29
30 const std::string INSIGHT_INTENTS = "insightIntents";
31 const std::string INSIGHT_INTENT_NAME = "intentName";
32 const std::string INSIGHT_INTENT_DOMAIN = "domain";
33 const std::string INSIGHT_INTENT_VERSION = "intentVersion";
34 const std::string INSIGHT_INTENT_SRC_ENTRY = "srcEntry";
35 const std::string INSIGHT_INTENT_UI_ABILITY = "uiAbility";
36 const std::string INSIGHT_INTENT_UI_EXTENSION = "uiExtension";
37 const std::string INSIGHT_INTENT_SERVICE_EXTENSION = "serviceExtension";
38 const std::string INSIGHT_INTENT_FORM = "form";
39 const std::string INSIGHT_INTENT_ABILITY = "ability";
40 const std::string INSIGHT_INTENT_EXECUTE_MODE = "executeMode";
41 const std::string INSIGHT_INTENT_FORM_NAME = "formName";
42
43 const std::map<std::string, ExecuteMode> executeModeMap = {
44 {"foreground", ExecuteMode::UI_ABILITY_FOREGROUND},
45 {"background", ExecuteMode::UI_ABILITY_BACKGROUND}
46 };
47
48 struct UIAbilityProfileInfo {
49 std::string abilityName;
50 std::vector<std::string> supportExecuteMode {};
51 };
52
53 struct UIExtensionProfileInfo {
54 std::string abilityName;
55 };
56
57 struct ServiceExtensionProfileInfo {
58 std::string abilityName;
59 };
60
61 struct FormProfileInfo {
62 std::string abilityName;
63 std::string formName;
64 };
65
66 struct InsightIntentProfileInfo {
67 std::string intentName;
68 std::string intentDomain;
69 std::string intentVersion;
70 std::string srcEntry;
71 UIAbilityProfileInfo uiAbilityProfileInfo;
72 UIExtensionProfileInfo uiExtensionProfileInfo;
73 ServiceExtensionProfileInfo serviceExtensionProfileInfo;
74 FormProfileInfo formProfileInfo;
75 };
76
77 struct InsightIntentProfileInfoVec {
78 std::vector<InsightIntentProfileInfo> insightIntents {};
79 };
80
from_json(const nlohmann::json & jsonObject,UIAbilityProfileInfo & info)81 void from_json(const nlohmann::json &jsonObject, UIAbilityProfileInfo &info)
82 {
83 const auto &jsonObjectEnd = jsonObject.end();
84 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
85 jsonObjectEnd,
86 INSIGHT_INTENT_ABILITY,
87 info.abilityName,
88 true,
89 g_parseResult);
90 AppExecFwk::GetValueIfFindKey<std::vector<std::string>>(jsonObject,
91 jsonObjectEnd,
92 INSIGHT_INTENT_EXECUTE_MODE,
93 info.supportExecuteMode,
94 JsonType::ARRAY,
95 true,
96 g_parseResult,
97 ArrayType::STRING);
98 }
99
from_json(const nlohmann::json & jsonObject,UIExtensionProfileInfo & info)100 void from_json(const nlohmann::json &jsonObject, UIExtensionProfileInfo &info)
101 {
102 const auto &jsonObjectEnd = jsonObject.end();
103 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
104 jsonObjectEnd,
105 INSIGHT_INTENT_ABILITY,
106 info.abilityName,
107 true,
108 g_parseResult);
109 }
110
from_json(const nlohmann::json & jsonObject,ServiceExtensionProfileInfo & info)111 void from_json(const nlohmann::json &jsonObject, ServiceExtensionProfileInfo &info)
112 {
113 const auto &jsonObjectEnd = jsonObject.end();
114 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
115 jsonObjectEnd,
116 INSIGHT_INTENT_ABILITY,
117 info.abilityName,
118 true,
119 g_parseResult);
120 }
121
from_json(const nlohmann::json & jsonObject,FormProfileInfo & info)122 void from_json(const nlohmann::json &jsonObject, FormProfileInfo &info)
123 {
124 const auto &jsonObjectEnd = jsonObject.end();
125 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
126 jsonObjectEnd,
127 INSIGHT_INTENT_ABILITY,
128 info.abilityName,
129 true,
130 g_parseResult);
131 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
132 jsonObjectEnd,
133 INSIGHT_INTENT_FORM_NAME,
134 info.formName,
135 true,
136 g_parseResult);
137 }
138
from_json(const nlohmann::json & jsonObject,InsightIntentProfileInfo & insightIntentInfo)139 void from_json(const nlohmann::json &jsonObject, InsightIntentProfileInfo &insightIntentInfo)
140 {
141 const auto &jsonObjectEnd = jsonObject.end();
142 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
143 jsonObjectEnd,
144 INSIGHT_INTENT_NAME,
145 insightIntentInfo.intentName,
146 true,
147 g_parseResult);
148 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
149 jsonObjectEnd,
150 INSIGHT_INTENT_DOMAIN,
151 insightIntentInfo.intentDomain,
152 true,
153 g_parseResult);
154 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
155 jsonObjectEnd,
156 INSIGHT_INTENT_VERSION,
157 insightIntentInfo.intentVersion,
158 true,
159 g_parseResult);
160 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
161 jsonObjectEnd,
162 INSIGHT_INTENT_SRC_ENTRY,
163 insightIntentInfo.srcEntry,
164 true,
165 g_parseResult);
166 AppExecFwk::GetValueIfFindKey<UIAbilityProfileInfo>(jsonObject,
167 jsonObjectEnd,
168 INSIGHT_INTENT_UI_ABILITY,
169 insightIntentInfo.uiAbilityProfileInfo,
170 JsonType::OBJECT,
171 false,
172 g_parseResult,
173 ArrayType::NOT_ARRAY);
174 AppExecFwk::GetValueIfFindKey<UIExtensionProfileInfo>(jsonObject,
175 jsonObjectEnd,
176 INSIGHT_INTENT_UI_EXTENSION,
177 insightIntentInfo.uiExtensionProfileInfo,
178 JsonType::OBJECT,
179 false,
180 g_parseResult,
181 ArrayType::NOT_ARRAY);
182 AppExecFwk::GetValueIfFindKey<ServiceExtensionProfileInfo>(jsonObject,
183 jsonObjectEnd,
184 INSIGHT_INTENT_SERVICE_EXTENSION,
185 insightIntentInfo.serviceExtensionProfileInfo,
186 JsonType::OBJECT,
187 false,
188 g_parseResult,
189 ArrayType::NOT_ARRAY);
190 AppExecFwk::GetValueIfFindKey<FormProfileInfo>(jsonObject,
191 jsonObjectEnd,
192 INSIGHT_INTENT_FORM,
193 insightIntentInfo.formProfileInfo,
194 JsonType::OBJECT,
195 false,
196 g_parseResult,
197 ArrayType::NOT_ARRAY);
198 }
199
from_json(const nlohmann::json & jsonObject,InsightIntentProfileInfoVec & infos)200 void from_json(const nlohmann::json &jsonObject, InsightIntentProfileInfoVec &infos)
201 {
202 const auto &jsonObjectEnd = jsonObject.end();
203 AppExecFwk::GetValueIfFindKey<std::vector<InsightIntentProfileInfo>>(jsonObject,
204 jsonObjectEnd,
205 INSIGHT_INTENTS,
206 infos.insightIntents,
207 JsonType::ARRAY,
208 false,
209 g_parseResult,
210 ArrayType::OBJECT);
211 }
212
TransformToInsightIntentInfo(const InsightIntentProfileInfo & insightIntent,InsightIntentInfo & info)213 bool TransformToInsightIntentInfo(const InsightIntentProfileInfo &insightIntent, InsightIntentInfo &info)
214 {
215 if (insightIntent.intentName.empty()) {
216 TAG_LOGE(AAFwkTag::INTENT, "empty intentName");
217 return false;
218 }
219
220 info.intentName = insightIntent.intentName;
221 info.intentDomain = insightIntent.intentDomain;
222 info.intentVersion = insightIntent.intentVersion;
223 info.srcEntry = insightIntent.srcEntry;
224
225 info.uiAbilityIntentInfo.abilityName = insightIntent.uiAbilityProfileInfo.abilityName;
226 for (const auto &executeMode: insightIntent.uiAbilityProfileInfo.supportExecuteMode) {
227 auto mode = std::find_if(std::begin(executeModeMap), std::end(executeModeMap),
228 [&executeMode](const auto &item) {
229 return item.first == executeMode;
230 });
231 if (mode == executeModeMap.end()) {
232 continue;
233 }
234 info.uiAbilityIntentInfo.supportExecuteMode.emplace_back(mode->second);
235 }
236
237 info.uiExtensionIntentInfo.abilityName = insightIntent.uiExtensionProfileInfo.abilityName;
238 info.serviceExtensionIntentInfo.abilityName = insightIntent.serviceExtensionProfileInfo.abilityName;
239 info.formIntentInfo.abilityName = insightIntent.formProfileInfo.abilityName;
240 info.formIntentInfo.formName = insightIntent.formProfileInfo.formName;
241 return true;
242 }
243
TransformToInfos(const InsightIntentProfileInfoVec & profileInfos,std::vector<InsightIntentInfo> & intentInfos)244 bool TransformToInfos(const InsightIntentProfileInfoVec &profileInfos, std::vector<InsightIntentInfo> &intentInfos)
245 {
246 TAG_LOGD(AAFwkTag::INTENT, "called");
247 for (const auto &insightIntent : profileInfos.insightIntents) {
248 InsightIntentInfo info;
249 if (!TransformToInsightIntentInfo(insightIntent, info)) {
250 return false;
251 }
252 intentInfos.push_back(info);
253 }
254 return true;
255 }
256 } // namespace
257
TransformTo(const std::string & profileStr,std::vector<InsightIntentInfo> & intentInfos)258 bool InsightIntentProfile::TransformTo(const std::string &profileStr, std::vector<InsightIntentInfo> &intentInfos)
259 {
260 TAG_LOGD(AAFwkTag::INTENT, "called");
261 auto jsonObject = nlohmann::json::parse(profileStr, nullptr, false);
262 if (jsonObject.is_discarded()) {
263 TAG_LOGE(AAFwkTag::INTENT, "discarded jsonObject");
264 return false;
265 }
266
267 InsightIntentProfileInfoVec profileInfos;
268 {
269 std::lock_guard<std::mutex> lock(g_mutex);
270 g_parseResult = ERR_OK;
271 profileInfos = jsonObject.get<InsightIntentProfileInfoVec>();
272 if (g_parseResult != ERR_OK) {
273 TAG_LOGE(AAFwkTag::INTENT, "g_parseResult :%{public}d", g_parseResult);
274 int32_t ret = g_parseResult;
275 // need recover parse result to ERR_OK
276 g_parseResult = ERR_OK;
277 return ret;
278 }
279 }
280
281 return TransformToInfos(profileInfos, intentInfos);
282 }
283 } // namespace AbilityRuntime
284 } // namespace OHOS
285