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