1 /*
2 * Copyright (c) 2022 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 "json_util.h"
24 #include "nlohmann/json.hpp"
25 #include "parcel_macro.h"
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 APPLICATION_INFO = "applicationInfo";
48 const std::string RESOURCE_PATH = "resourcePath";
49 const std::string ENABLED = "enabled";
50 const std::string PROCESS = "process";
51 const std::string COMPILE_MODE = "compileMode";
52 }; // namespace
53
ReadFromParcel(Parcel & parcel)54 bool ExtensionAbilityInfo::ReadFromParcel(Parcel &parcel)
55 {
56 bundleName = Str16ToStr8(parcel.ReadString16());
57 moduleName = Str16ToStr8(parcel.ReadString16());
58 name = Str16ToStr8(parcel.ReadString16());
59 srcEntrance = Str16ToStr8(parcel.ReadString16());
60 icon = Str16ToStr8(parcel.ReadString16());
61 iconId = parcel.ReadInt32();
62 label = Str16ToStr8(parcel.ReadString16());
63 labelId = parcel.ReadInt32();
64 description = Str16ToStr8(parcel.ReadString16());
65 descriptionId = parcel.ReadInt32();
66 priority = parcel.ReadInt32();
67 int32_t permissionsSize;
68 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, permissionsSize);
69 CONTAINER_SECURITY_VERIFY(parcel, permissionsSize, &permissions);
70 for (auto i = 0; i < permissionsSize; i++) {
71 permissions.emplace_back(Str16ToStr8(parcel.ReadString16()));
72 }
73 readPermission = Str16ToStr8(parcel.ReadString16());
74 writePermission = Str16ToStr8(parcel.ReadString16());
75 uri = Str16ToStr8(parcel.ReadString16());
76 type = static_cast<ExtensionAbilityType>(parcel.ReadInt32());
77 visible = parcel.ReadBool();
78
79 int32_t metadataSize;
80 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, metadataSize);
81 CONTAINER_SECURITY_VERIFY(parcel, metadataSize, &metadata);
82 for (auto i = 0; i < metadataSize; i++) {
83 std::unique_ptr<Metadata> meta(parcel.ReadParcelable<Metadata>());
84 if (!meta) {
85 APP_LOGE("ReadParcelable<Metadata> failed");
86 return false;
87 }
88 metadata.emplace_back(*meta);
89 }
90
91 std::unique_ptr<ApplicationInfo> appInfo(parcel.ReadParcelable<ApplicationInfo>());
92 if (!appInfo) {
93 APP_LOGE("ReadParcelable<ApplicationInfo> failed");
94 return false;
95 }
96 applicationInfo = *appInfo;
97
98 resourcePath = Str16ToStr8(parcel.ReadString16());
99 hapPath = Str16ToStr8(parcel.ReadString16());
100 enabled = parcel.ReadBool();
101 process = Str16ToStr8(parcel.ReadString16());
102 compileMode = static_cast<CompileMode>(parcel.ReadInt32());
103 return true;
104 }
105
Unmarshalling(Parcel & parcel)106 ExtensionAbilityInfo *ExtensionAbilityInfo::Unmarshalling(Parcel &parcel)
107 {
108 ExtensionAbilityInfo *info = new (std::nothrow) ExtensionAbilityInfo();
109 if (info && !info->ReadFromParcel(parcel)) {
110 APP_LOGW("read from parcel failed");
111 delete info;
112 info = nullptr;
113 }
114 return info;
115 }
116
Marshalling(Parcel & parcel) const117 bool ExtensionAbilityInfo::Marshalling(Parcel &parcel) const
118 {
119 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
120 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
121 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(name));
122 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(srcEntrance));
123 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(icon));
124 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, iconId);
125 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(label));
126 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, labelId);
127 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(description));
128 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, descriptionId);
129 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, priority);
130 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, permissions.size());
131 for (auto &permission : permissions) {
132 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(permission));
133 }
134 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(readPermission));
135 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(writePermission));
136 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(uri));
137 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(type));
138 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, visible);
139 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, metadata.size());
140 for (auto &mete : metadata) {
141 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &mete);
142 }
143 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &applicationInfo);
144 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(resourcePath));
145 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(hapPath));
146 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, enabled);
147 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(process));
148 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(compileMode));
149 return true;
150 }
151
to_json(nlohmann::json & jsonObject,const ExtensionAbilityInfo & extensionInfo)152 void to_json(nlohmann::json &jsonObject, const ExtensionAbilityInfo &extensionInfo)
153 {
154 APP_LOGD("ExtensionAbilityInfo to_json begin");
155 jsonObject = nlohmann::json {
156 {Constants::BUNDLE_NAME, extensionInfo.bundleName},
157 {Constants::MODULE_NAME, extensionInfo.moduleName},
158 {NAME, extensionInfo.name},
159 {SRC_ENTRANCE, extensionInfo.srcEntrance},
160 {ICON, extensionInfo.icon},
161 {ICON_ID, extensionInfo.iconId},
162 {LABEL, extensionInfo.label},
163 {LABEL_ID, extensionInfo.labelId},
164 {DESCRIPTION, extensionInfo.description},
165 {DESCRIPTION_ID, extensionInfo.descriptionId},
166 {PRIORITY, extensionInfo.priority},
167 {TYPE, extensionInfo.type},
168 {READ_PERMISSION, extensionInfo.readPermission},
169 {WRITE_PERMISSION, extensionInfo.writePermission},
170 {URI, extensionInfo.uri},
171 {PERMISSIONS, extensionInfo.permissions},
172 {VISIBLE, extensionInfo.visible},
173 {META_DATA, extensionInfo.metadata},
174 {APPLICATION_INFO, extensionInfo.applicationInfo},
175 {RESOURCE_PATH, extensionInfo.resourcePath},
176 {Constants::HAP_PATH, extensionInfo.hapPath},
177 {ENABLED, extensionInfo.enabled},
178 {PROCESS, extensionInfo.process},
179 {COMPILE_MODE, extensionInfo.compileMode}
180 };
181 }
182
from_json(const nlohmann::json & jsonObject,ExtensionAbilityInfo & extensionInfo)183 void from_json(const nlohmann::json &jsonObject, ExtensionAbilityInfo &extensionInfo)
184 {
185 APP_LOGD("ExtensionAbilityInfo from_json begin");
186 const auto &jsonObjectEnd = jsonObject.end();
187 int32_t parseResult = ERR_OK;
188 GetValueIfFindKey<std::string>(jsonObject,
189 jsonObjectEnd,
190 Constants::BUNDLE_NAME,
191 extensionInfo.bundleName,
192 JsonType::STRING,
193 false,
194 parseResult,
195 ArrayType::NOT_ARRAY);
196 GetValueIfFindKey<std::string>(jsonObject,
197 jsonObjectEnd,
198 Constants::MODULE_NAME,
199 extensionInfo.moduleName,
200 JsonType::STRING,
201 false,
202 parseResult,
203 ArrayType::NOT_ARRAY);
204 GetValueIfFindKey<std::string>(jsonObject,
205 jsonObjectEnd,
206 NAME,
207 extensionInfo.name,
208 JsonType::STRING,
209 false,
210 parseResult,
211 ArrayType::NOT_ARRAY);
212 GetValueIfFindKey<std::string>(jsonObject,
213 jsonObjectEnd,
214 SRC_ENTRANCE,
215 extensionInfo.srcEntrance,
216 JsonType::STRING,
217 false,
218 parseResult,
219 ArrayType::NOT_ARRAY);
220 GetValueIfFindKey<std::string>(jsonObject,
221 jsonObjectEnd,
222 ICON,
223 extensionInfo.icon,
224 JsonType::STRING,
225 false,
226 parseResult,
227 ArrayType::NOT_ARRAY);
228 GetValueIfFindKey<int32_t>(jsonObject,
229 jsonObjectEnd,
230 ICON_ID,
231 extensionInfo.iconId,
232 JsonType::NUMBER,
233 false,
234 parseResult,
235 ArrayType::NOT_ARRAY);
236 GetValueIfFindKey<std::string>(jsonObject,
237 jsonObjectEnd,
238 LABEL,
239 extensionInfo.label,
240 JsonType::STRING,
241 false,
242 parseResult,
243 ArrayType::NOT_ARRAY);
244 GetValueIfFindKey<int32_t>(jsonObject,
245 jsonObjectEnd,
246 LABEL_ID,
247 extensionInfo.labelId,
248 JsonType::NUMBER,
249 false,
250 parseResult,
251 ArrayType::NOT_ARRAY);
252 GetValueIfFindKey<std::string>(jsonObject,
253 jsonObjectEnd,
254 DESCRIPTION,
255 extensionInfo.description,
256 JsonType::STRING,
257 false,
258 parseResult,
259 ArrayType::NOT_ARRAY);
260 GetValueIfFindKey<int32_t>(jsonObject,
261 jsonObjectEnd,
262 DESCRIPTION_ID,
263 extensionInfo.descriptionId,
264 JsonType::NUMBER,
265 false,
266 parseResult,
267 ArrayType::NOT_ARRAY);
268 GetValueIfFindKey<int32_t>(jsonObject,
269 jsonObjectEnd,
270 PRIORITY,
271 extensionInfo.priority,
272 JsonType::NUMBER,
273 false,
274 parseResult,
275 ArrayType::NOT_ARRAY);
276 GetValueIfFindKey<ExtensionAbilityType>(jsonObject,
277 jsonObjectEnd,
278 TYPE,
279 extensionInfo.type,
280 JsonType::NUMBER,
281 false,
282 parseResult,
283 ArrayType::NOT_ARRAY);
284 GetValueIfFindKey<std::string>(jsonObject,
285 jsonObjectEnd,
286 READ_PERMISSION,
287 extensionInfo.readPermission,
288 JsonType::STRING,
289 false,
290 parseResult,
291 ArrayType::NOT_ARRAY);
292 GetValueIfFindKey<std::string>(jsonObject,
293 jsonObjectEnd,
294 WRITE_PERMISSION,
295 extensionInfo.writePermission,
296 JsonType::STRING,
297 false,
298 parseResult,
299 ArrayType::NOT_ARRAY);
300 GetValueIfFindKey<std::string>(jsonObject,
301 jsonObjectEnd,
302 URI,
303 extensionInfo.uri,
304 JsonType::STRING,
305 false,
306 parseResult,
307 ArrayType::NOT_ARRAY);
308 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
309 jsonObjectEnd,
310 PERMISSIONS,
311 extensionInfo.permissions,
312 JsonType::ARRAY,
313 false,
314 parseResult,
315 ArrayType::STRING);
316 GetValueIfFindKey<bool>(jsonObject,
317 jsonObjectEnd,
318 VISIBLE,
319 extensionInfo.visible,
320 JsonType::BOOLEAN,
321 false,
322 parseResult,
323 ArrayType::NOT_ARRAY);
324 GetValueIfFindKey<std::vector<Metadata>>(jsonObject,
325 jsonObjectEnd,
326 META_DATA,
327 extensionInfo.metadata,
328 JsonType::ARRAY,
329 false,
330 parseResult,
331 ArrayType::OBJECT);
332 GetValueIfFindKey<ApplicationInfo>(jsonObject,
333 jsonObjectEnd,
334 APPLICATION_INFO,
335 extensionInfo.applicationInfo,
336 JsonType::OBJECT,
337 false,
338 parseResult,
339 ArrayType::NOT_ARRAY);
340 GetValueIfFindKey<std::string>(jsonObject,
341 jsonObjectEnd,
342 RESOURCE_PATH,
343 extensionInfo.resourcePath,
344 JsonType::STRING,
345 false,
346 parseResult,
347 ArrayType::NOT_ARRAY);
348 GetValueIfFindKey<std::string>(jsonObject,
349 jsonObjectEnd,
350 Constants::HAP_PATH,
351 extensionInfo.hapPath,
352 JsonType::STRING,
353 false,
354 parseResult,
355 ArrayType::NOT_ARRAY);
356 GetValueIfFindKey<bool>(jsonObject,
357 jsonObjectEnd,
358 ENABLED,
359 extensionInfo.enabled,
360 JsonType::BOOLEAN,
361 false,
362 parseResult,
363 ArrayType::NOT_ARRAY);
364 GetValueIfFindKey<std::string>(jsonObject,
365 jsonObjectEnd,
366 PROCESS,
367 extensionInfo.process,
368 JsonType::STRING,
369 false,
370 parseResult,
371 ArrayType::NOT_ARRAY);
372 GetValueIfFindKey<CompileMode>(jsonObject,
373 jsonObjectEnd,
374 COMPILE_MODE,
375 extensionInfo.compileMode,
376 JsonType::NUMBER,
377 false,
378 parseResult,
379 ArrayType::NOT_ARRAY);
380 if (parseResult != ERR_OK) {
381 APP_LOGE("ExtensionAbilityInfo from_json error, error code : %{public}d", parseResult);
382 }
383 }
384 } // namespace AppExecFwk
385 } // namespace OHOS