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