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 "extension_ability_info.h"
17 
18 #include <fcntl.h>
19 #include <string>
20 #include <unistd.h>
21 
22 #include "bundle_constants.h"
23 #include "hilog_tag_wrapper.h"
24 #include "json_util.h"
25 #include "nlohmann/json.hpp"
26 #include "string_ex.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 const std::string NAME = "name";
32 const std::string SRC_ENTRANCE = "srcEntrance";
33 const std::string ICON = "icon";
34 const std::string ICON_ID = "iconId";
35 const std::string LABEL = "label";
36 const std::string LABEL_ID = "labelId";
37 const std::string DESCRIPTION = "description";
38 const std::string DESCRIPTION_ID = "descriptionId";
39 const std::string PRIORITY = "priority";
40 const std::string TYPE = "type";
41 const std::string PERMISSIONS = "permissions";
42 const std::string READ_PERMISSION = "readPermission";
43 const std::string WRITE_PERMISSION = "writePermission";
44 const std::string URI = "uri";
45 const std::string VISIBLE = "visible";
46 const std::string META_DATA = "metadata";
47 const std::string RESOURCE_PATH = "resourcePath";
48 const std::string ENABLED = "enabled";
49 const std::string PROCESS = "process";
50 const std::string COMPILE_MODE = "compileMode";
51 const std::string UID = "uid";
52 }; // namespace
to_json(nlohmann::json & jsonObject,const ExtensionAbilityInfo & extensionInfo)53 void to_json(nlohmann::json &jsonObject, const ExtensionAbilityInfo &extensionInfo)
54 {
55     TAG_LOGD(AAFwkTag::ABILITY_SIM, "called");
56     jsonObject = nlohmann::json {
57         {Constants::BUNDLE_NAME, extensionInfo.bundleName},
58         {Constants::MODULE_NAME, extensionInfo.moduleName},
59         {NAME, extensionInfo.name},
60         {SRC_ENTRANCE, extensionInfo.srcEntrance},
61         {ICON, extensionInfo.icon},
62         {ICON_ID, extensionInfo.iconId},
63         {LABEL, extensionInfo.label},
64         {LABEL_ID, extensionInfo.labelId},
65         {DESCRIPTION, extensionInfo.description},
66         {DESCRIPTION_ID, extensionInfo.descriptionId},
67         {PRIORITY, extensionInfo.priority},
68         {TYPE, extensionInfo.type},
69         {READ_PERMISSION, extensionInfo.readPermission},
70         {WRITE_PERMISSION, extensionInfo.writePermission},
71         {URI, extensionInfo.uri},
72         {PERMISSIONS, extensionInfo.permissions},
73         {VISIBLE, extensionInfo.visible},
74         {META_DATA, extensionInfo.metadata},
75         {RESOURCE_PATH, extensionInfo.resourcePath},
76         {Constants::HAP_PATH, extensionInfo.hapPath},
77         {ENABLED, extensionInfo.enabled},
78         {PROCESS, extensionInfo.process},
79         {COMPILE_MODE, extensionInfo.compileMode},
80         {UID, extensionInfo.uid}
81     };
82 }
83 
from_json(const nlohmann::json & jsonObject,ExtensionAbilityInfo & extensionInfo)84 void from_json(const nlohmann::json &jsonObject, ExtensionAbilityInfo &extensionInfo)
85 {
86     TAG_LOGD(AAFwkTag::ABILITY_SIM, "called");
87     const auto &jsonObjectEnd = jsonObject.end();
88     int32_t parseResult = ERR_OK;
89     GetValueIfFindKey<std::string>(jsonObject,
90         jsonObjectEnd,
91         Constants::BUNDLE_NAME,
92         extensionInfo.bundleName,
93         JsonType::STRING,
94         false,
95         parseResult,
96         ArrayType::NOT_ARRAY);
97     GetValueIfFindKey<std::string>(jsonObject,
98         jsonObjectEnd,
99         Constants::MODULE_NAME,
100         extensionInfo.moduleName,
101         JsonType::STRING,
102         false,
103         parseResult,
104         ArrayType::NOT_ARRAY);
105     GetValueIfFindKey<std::string>(jsonObject,
106         jsonObjectEnd,
107         NAME,
108         extensionInfo.name,
109         JsonType::STRING,
110         false,
111         parseResult,
112         ArrayType::NOT_ARRAY);
113     GetValueIfFindKey<std::string>(jsonObject,
114         jsonObjectEnd,
115         SRC_ENTRANCE,
116         extensionInfo.srcEntrance,
117         JsonType::STRING,
118         false,
119         parseResult,
120         ArrayType::NOT_ARRAY);
121     GetValueIfFindKey<std::string>(jsonObject,
122         jsonObjectEnd,
123         ICON,
124         extensionInfo.icon,
125         JsonType::STRING,
126         false,
127         parseResult,
128         ArrayType::NOT_ARRAY);
129     GetValueIfFindKey<int32_t>(jsonObject,
130         jsonObjectEnd,
131         ICON_ID,
132         extensionInfo.iconId,
133         JsonType::NUMBER,
134         false,
135         parseResult,
136         ArrayType::NOT_ARRAY);
137     GetValueIfFindKey<std::string>(jsonObject,
138         jsonObjectEnd,
139         LABEL,
140         extensionInfo.label,
141         JsonType::STRING,
142         false,
143         parseResult,
144         ArrayType::NOT_ARRAY);
145     GetValueIfFindKey<int32_t>(jsonObject,
146         jsonObjectEnd,
147         LABEL_ID,
148         extensionInfo.labelId,
149         JsonType::NUMBER,
150         false,
151         parseResult,
152         ArrayType::NOT_ARRAY);
153     GetValueIfFindKey<std::string>(jsonObject,
154         jsonObjectEnd,
155         DESCRIPTION,
156         extensionInfo.description,
157         JsonType::STRING,
158         false,
159         parseResult,
160         ArrayType::NOT_ARRAY);
161     GetValueIfFindKey<int32_t>(jsonObject,
162         jsonObjectEnd,
163         DESCRIPTION_ID,
164         extensionInfo.descriptionId,
165         JsonType::NUMBER,
166         false,
167         parseResult,
168         ArrayType::NOT_ARRAY);
169     GetValueIfFindKey<int32_t>(jsonObject,
170         jsonObjectEnd,
171         PRIORITY,
172         extensionInfo.priority,
173         JsonType::NUMBER,
174         false,
175         parseResult,
176         ArrayType::NOT_ARRAY);
177     GetValueIfFindKey<ExtensionAbilityType>(jsonObject,
178         jsonObjectEnd,
179         TYPE,
180         extensionInfo.type,
181         JsonType::NUMBER,
182         false,
183         parseResult,
184         ArrayType::NOT_ARRAY);
185     GetValueIfFindKey<std::string>(jsonObject,
186         jsonObjectEnd,
187         READ_PERMISSION,
188         extensionInfo.readPermission,
189         JsonType::STRING,
190         false,
191         parseResult,
192         ArrayType::NOT_ARRAY);
193     GetValueIfFindKey<std::string>(jsonObject,
194         jsonObjectEnd,
195         WRITE_PERMISSION,
196         extensionInfo.writePermission,
197         JsonType::STRING,
198         false,
199         parseResult,
200         ArrayType::NOT_ARRAY);
201     GetValueIfFindKey<std::string>(jsonObject,
202         jsonObjectEnd,
203         URI,
204         extensionInfo.uri,
205         JsonType::STRING,
206         false,
207         parseResult,
208         ArrayType::NOT_ARRAY);
209     GetValueIfFindKey<std::vector<std::string>>(jsonObject,
210         jsonObjectEnd,
211         PERMISSIONS,
212         extensionInfo.permissions,
213         JsonType::ARRAY,
214         false,
215         parseResult,
216         ArrayType::STRING);
217     GetValueIfFindKey<bool>(jsonObject,
218         jsonObjectEnd,
219         VISIBLE,
220         extensionInfo.visible,
221         JsonType::BOOLEAN,
222         false,
223         parseResult,
224         ArrayType::NOT_ARRAY);
225     GetValueIfFindKey<std::vector<Metadata>>(jsonObject,
226         jsonObjectEnd,
227         META_DATA,
228         extensionInfo.metadata,
229         JsonType::ARRAY,
230         false,
231         parseResult,
232         ArrayType::OBJECT);
233     GetValueIfFindKey<std::string>(jsonObject,
234         jsonObjectEnd,
235         RESOURCE_PATH,
236         extensionInfo.resourcePath,
237         JsonType::STRING,
238         false,
239         parseResult,
240         ArrayType::NOT_ARRAY);
241     GetValueIfFindKey<std::string>(jsonObject,
242         jsonObjectEnd,
243         Constants::HAP_PATH,
244         extensionInfo.hapPath,
245         JsonType::STRING,
246         false,
247         parseResult,
248         ArrayType::NOT_ARRAY);
249     GetValueIfFindKey<bool>(jsonObject,
250         jsonObjectEnd,
251         ENABLED,
252         extensionInfo.enabled,
253         JsonType::BOOLEAN,
254         false,
255         parseResult,
256         ArrayType::NOT_ARRAY);
257     GetValueIfFindKey<std::string>(jsonObject,
258         jsonObjectEnd,
259         PROCESS,
260         extensionInfo.process,
261         JsonType::STRING,
262         false,
263         parseResult,
264         ArrayType::NOT_ARRAY);
265     GetValueIfFindKey<CompileMode>(jsonObject,
266         jsonObjectEnd,
267         COMPILE_MODE,
268         extensionInfo.compileMode,
269         JsonType::NUMBER,
270         false,
271         parseResult,
272         ArrayType::NOT_ARRAY);
273     GetValueIfFindKey<int32_t>(jsonObject,
274         jsonObjectEnd,
275         UID,
276         extensionInfo.uid,
277         JsonType::NUMBER,
278         false,
279         parseResult,
280         ArrayType::NOT_ARRAY);
281     if (parseResult != ERR_OK) {
282         TAG_LOGE(AAFwkTag::ABILITY_SIM, "ExtensionAbilityInfo error:%{public}d", parseResult);
283     }
284 }
285 }  // namespace AppExecFwk
286 }  // namespace OHOS