1 /*
2 * Copyright (c) 2021-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 "ability_info.h"
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include "bundle_constants.h"
24 #include "json_util.h"
25 #include "nlohmann/json.hpp"
26 #include "parcel_macro.h"
27 #include "string_ex.h"
28
29 namespace OHOS {
30 namespace AppExecFwk {
31 namespace {
32 const std::string APPLICAION_INFO = "applicationInfo";
33 const std::string JSON_KEY_PACKAGE = "package";
34 const std::string JSON_KEY_NAME = "name";
35 const std::string JSON_KEY_BUNDLE_NAME = "bundleName";
36 const std::string JSON_KEY_APPLICATION_NAME = "applicationName";
37 const std::string JSON_KEY_LABEL = "label";
38 const std::string JSON_KEY_DESCRIPTION = "description";
39 const std::string JSON_KEY_ICON_PATH = "iconPath";
40 const std::string JSON_KEY_THEME = "theme";
41 const std::string JSON_KEY_VISIBLE = "visible";
42 const std::string JSON_KEY_ABILITY_CONTINUABLE = "continuable";
43 const std::string PRIORITY = "priority";
44 const std::string JSON_KEY_KIND = "kind";
45 const std::string JSON_KEY_TYPE = "type";
46 const std::string JSON_KEY_ORIENTATION = "orientation";
47 const std::string JSON_KEY_LAUNCH_MODE = "launchMode";
48 const std::string JSON_KEY_CODE_PATH = "codePath";
49 const std::string JSON_KEY_RESOURCE_PATH = "resourcePath";
50 const std::string JSON_KEY_LIB_PATH = "libPath";
51 const std::string JSON_KEY_PERMISSIONS = "permissions";
52 const std::string JSON_KEY_PROCESS = "process";
53 const std::string JSON_KEY_DEVICE_TYPES = "deviceTypes";
54 const std::string JSON_KEY_DEVICE_CAPABILITIES = "deviceCapabilities";
55 const std::string JSON_KEY_URI = "uri";
56 const std::string JSON_KEY_MODULE_NAME = "moduleName";
57 const std::string JSON_KEY_DEVICE_ID = "deviceId";
58 const std::string JSON_KEY_IS_LAUNCHER_ABILITY = "isLauncherAbility";
59 const std::string JSON_KEY_IS_NATIVE_ABILITY = "isNativeAbility";
60 const std::string JSON_KEY_ENABLED = "enabled";
61 const std::string JSON_KEY_SUPPORT_PIP_MODE = "supportPipMode";
62 const std::string JSON_KEY_TARGET_ABILITY = "targetAbility";
63 const std::string JSON_KEY_READ_PERMISSION = "readPermission";
64 const std::string JSON_KEY_WRITE_PERMISSION = "writePermission";
65 const std::string JSON_KEY_CONFIG_CHANGES = "configChanges";
66 const std::string JSON_KEY_FORM = "form";
67 const std::string JSON_KEY_FORM_ENTITY = "formEntity";
68 const std::string JSON_KEY_MIN_FORM_HEIGHT = "minFormHeight";
69 const std::string JSON_KEY_DEFAULT_FORM_HEIGHT = "defaultFormHeight";
70 const std::string JSON_KEY_MIN_FORM_WIDTH = "minFormWidth";
71 const std::string JSON_KEY_DEFAULT_FORM_WIDTH = "defaultFormWidth";
72 const std::string JSON_KEY_BACKGROUND_MODES = "backgroundModes";
73 const std::string JSON_KEY_CUSTOMIZE_DATA = "customizeData";
74 const std::string JSON_KEY_META_DATA = "metaData";
75 const std::string JSON_KEY_META_VALUE = "value";
76 const std::string JSON_KEY_META_EXTRA = "extra";
77 const std::string JSON_KEY_LABEL_ID = "labelId";
78 const std::string JSON_KEY_DESCRIPTION_ID = "descriptionId";
79 const std::string JSON_KEY_ICON_ID = "iconId";
80 const std::string JSON_KEY_FORM_ENABLED = "formEnabled";
81 const std::string JSON_KEY_SRC_PATH = "srcPath";
82 const std::string JSON_KEY_SRC_LANGUAGE = "srcLanguage";
83 const std::string META_DATA = "metadata";
84 const std::string META_DATA_NAME = "name";
85 const std::string META_DATA_VALUE = "value";
86 const std::string META_DATA_RESOURCE = "resource";
87 const std::string SRC_ENTRANCE = "srcEntrance";
88 const std::string IS_MODULE_JSON = "isModuleJson";
89 const std::string IS_STAGE_BASED_MODEL = "isStageBasedModel";
90 } // namespace
91
ReadFromParcel(Parcel & parcel)92 bool AbilityInfo::ReadFromParcel(Parcel &parcel)
93 {
94 name = Str16ToStr8(parcel.ReadString16());
95 label = Str16ToStr8(parcel.ReadString16());
96 description = Str16ToStr8(parcel.ReadString16());
97 iconPath = Str16ToStr8(parcel.ReadString16());
98 theme = Str16ToStr8(parcel.ReadString16());
99 kind = Str16ToStr8(parcel.ReadString16());
100 uri = Str16ToStr8(parcel.ReadString16());
101 package = Str16ToStr8(parcel.ReadString16());
102 bundleName = Str16ToStr8(parcel.ReadString16());
103 moduleName = Str16ToStr8(parcel.ReadString16());
104 applicationName = Str16ToStr8(parcel.ReadString16());
105 process = Str16ToStr8(parcel.ReadString16());
106 deviceId = Str16ToStr8(parcel.ReadString16());
107 codePath = Str16ToStr8(parcel.ReadString16());
108 resourcePath = Str16ToStr8(parcel.ReadString16());
109 libPath = Str16ToStr8(parcel.ReadString16());
110 targetAbility = Str16ToStr8(parcel.ReadString16());
111 readPermission = Str16ToStr8(parcel.ReadString16());
112 writePermission = Str16ToStr8(parcel.ReadString16());
113 srcPath = Str16ToStr8(parcel.ReadString16());
114 srcLanguage = Str16ToStr8(parcel.ReadString16());
115 visible = parcel.ReadBool();
116 continuable = parcel.ReadBool();
117 priority = parcel.ReadInt32();
118 isLauncherAbility = parcel.ReadBool();
119 isNativeAbility = parcel.ReadBool();
120 enabled = parcel.ReadBool();
121 supportPipMode = parcel.ReadBool();
122 formEnabled = parcel.ReadBool();
123 int32_t configChangesSize;
124 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, configChangesSize);
125 for (int32_t i = 0; i < configChangesSize; i++) {
126 configChanges.emplace_back(Str16ToStr8(parcel.ReadString16()));
127 }
128 formEntity = parcel.ReadInt32();
129 minFormHeight = parcel.ReadInt32();
130 defaultFormHeight = parcel.ReadInt32();
131 minFormWidth = parcel.ReadInt32();
132 defaultFormWidth = parcel.ReadInt32();
133 backgroundModes = parcel.ReadInt32();
134 labelId = parcel.ReadInt32();
135 descriptionId = parcel.ReadInt32();
136 iconId = parcel.ReadInt32();
137
138 int32_t typeData;
139 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, typeData);
140 type = static_cast<AbilityType>(typeData);
141
142 int32_t extensionTypeData;
143 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, extensionTypeData);
144 extensionAbilityType = static_cast<ExtensionAbilityType>(extensionTypeData);
145
146 int32_t orientationData;
147 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, orientationData);
148 orientation = static_cast<DisplayOrientation>(orientationData);
149
150 int32_t launchModeData;
151 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, launchModeData);
152 launchMode = static_cast<LaunchMode>(launchModeData);
153
154 int32_t permissionsSize;
155 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, permissionsSize);
156 for (int32_t i = 0; i < permissionsSize; i++) {
157 permissions.emplace_back(Str16ToStr8(parcel.ReadString16()));
158 }
159
160 uint32_t deviceTypesSize;
161 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, deviceTypesSize);
162 for (uint32_t i = 0; i < deviceTypesSize; i++) {
163 deviceTypes.emplace_back(Str16ToStr8(parcel.ReadString16()));
164 }
165
166 int32_t deviceCapabilitiesSize;
167 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, deviceCapabilitiesSize);
168 for (int32_t i = 0; i < deviceCapabilitiesSize; i++) {
169 deviceCapabilities.emplace_back(Str16ToStr8(parcel.ReadString16()));
170 }
171
172 int32_t customizeDataSize;
173 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, customizeDataSize);
174 for (auto i = 0; i < customizeDataSize; i++) {
175 std::unique_ptr<CustomizeData> customizeData(parcel.ReadParcelable<CustomizeData>());
176 if (!customizeData) {
177 APP_LOGE("ReadParcelable<CustomizeData> failed");
178 return false;
179 }
180 metaData.customizeData.emplace_back(*customizeData);
181 }
182
183 std::unique_ptr<ApplicationInfo> appInfo(parcel.ReadParcelable<ApplicationInfo>());
184 if (!appInfo) {
185 APP_LOGE("ReadParcelable<ApplicationInfo> failed");
186 return false;
187 }
188 applicationInfo = *appInfo;
189
190 srcEntrance = Str16ToStr8(parcel.ReadString16());
191 int32_t metadataSize;
192 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, metadataSize);
193 for (auto i = 0; i < metadataSize; ++i) {
194 std::unique_ptr<Metadata> data(parcel.ReadParcelable<Metadata>());
195 if (!data) {
196 APP_LOGE("ReadParcelable<CustomizeData> failed");
197 return false;
198 }
199 metadata.emplace_back(*data);
200 }
201 isModuleJson = parcel.ReadBool();
202 isStageBasedModel = parcel.ReadBool();
203 return true;
204 }
205
Unmarshalling(Parcel & parcel)206 AbilityInfo *AbilityInfo::Unmarshalling(Parcel &parcel)
207 {
208 AbilityInfo *info = new (std::nothrow) AbilityInfo();
209 if (info && !info->ReadFromParcel(parcel)) {
210 APP_LOGW("read from parcel failed");
211 delete info;
212 info = nullptr;
213 }
214 return info;
215 }
216
Marshalling(Parcel & parcel) const217 bool AbilityInfo::Marshalling(Parcel &parcel) const
218 {
219 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(name));
220 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(label));
221 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(description));
222 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(iconPath));
223 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(theme));
224 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(kind));
225 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(uri));
226 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(package));
227 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
228 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
229 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(applicationName));
230 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(process));
231 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(deviceId));
232 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(codePath));
233 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(resourcePath));
234 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(libPath));
235 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(targetAbility));
236 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(readPermission));
237 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(writePermission));
238 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(srcPath));
239 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(srcLanguage));
240 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, visible);
241 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, continuable);
242 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, priority);
243 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isLauncherAbility);
244 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isNativeAbility);
245 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, enabled);
246 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, supportPipMode);
247 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, formEnabled);
248 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, configChanges.size());
249 for (auto &configChange : configChanges) {
250 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(configChange));
251 }
252 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, formEntity);
253 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, minFormHeight);
254 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, defaultFormHeight);
255 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, minFormWidth);
256 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, defaultFormWidth);
257 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, backgroundModes);
258 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, labelId);
259 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, descriptionId);
260 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, iconId);
261 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(type));
262 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(extensionAbilityType));
263 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(orientation));
264 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(launchMode));
265
266 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, permissions.size());
267 for (auto &permission : permissions) {
268 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(permission));
269 }
270
271 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint32, parcel, deviceTypes.size());
272 for (auto &deviceType : deviceTypes) {
273 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(deviceType));
274 }
275
276 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, deviceCapabilities.size());
277 for (auto &deviceCapability : deviceCapabilities) {
278 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(deviceCapability));
279 }
280
281 const auto customizeDataSize = static_cast<int32_t>(metaData.customizeData.size());
282 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, customizeDataSize);
283 for (auto i = 0; i < customizeDataSize; i++) {
284 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &metaData.customizeData[i]);
285 }
286
287 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &applicationInfo);
288
289 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(srcEntrance));
290 const auto metadataSize = static_cast<int32_t>(metadata.size());
291 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, metadataSize);
292 for (auto i = 0; i < metadataSize; i++) {
293 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &metadata[i]);
294 }
295 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isModuleJson);
296 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, isStageBasedModel);
297 return true;
298 }
299
Dump(std::string prefix,int fd)300 void AbilityInfo::Dump(std::string prefix, int fd)
301 {
302 APP_LOGI("called dump Abilityinfo");
303 if (fd < 0) {
304 APP_LOGE("dump Abilityinfo fd error");
305 return;
306 }
307 int flags = fcntl(fd, F_GETFL);
308 if (flags < 0) {
309 APP_LOGE("dump Abilityinfo fcntl error, errno : %{public}d", errno);
310 return;
311 }
312 uint uflags = static_cast<uint>(flags);
313 uflags &= O_ACCMODE;
314 if ((uflags == O_WRONLY) || (uflags == O_RDWR)) {
315 nlohmann::json jsonObject = *this;
316 std::string result;
317 result.append(prefix);
318 result.append(jsonObject.dump(Constants::DUMP_INDENT));
319 int ret = TEMP_FAILURE_RETRY(write(fd, result.c_str(), result.size()));
320 if (ret < 0) {
321 APP_LOGE("dump Abilityinfo write error, errno : %{public}d", errno);
322 }
323 }
324 return;
325 }
326
to_json(nlohmann::json & jsonObject,const CustomizeData & customizeData)327 void to_json(nlohmann::json &jsonObject, const CustomizeData &customizeData)
328 {
329 jsonObject = nlohmann::json {
330 {JSON_KEY_NAME, customizeData.name},
331 {JSON_KEY_META_VALUE, customizeData.value},
332 {JSON_KEY_META_EXTRA, customizeData.extra}
333 };
334 }
335
to_json(nlohmann::json & jsonObject,const MetaData & metaData)336 void to_json(nlohmann::json &jsonObject, const MetaData &metaData)
337 {
338 jsonObject = nlohmann::json {
339 {JSON_KEY_CUSTOMIZE_DATA, metaData.customizeData}
340 };
341 }
342
to_json(nlohmann::json & jsonObject,const Metadata & metadata)343 void to_json(nlohmann::json &jsonObject, const Metadata &metadata)
344 {
345 jsonObject = nlohmann::json {
346 {META_DATA_NAME, metadata.name},
347 {META_DATA_VALUE, metadata.value},
348 {META_DATA_RESOURCE, metadata.resource}
349 };
350 }
351
to_json(nlohmann::json & jsonObject,const AbilityInfo & abilityInfo)352 void to_json(nlohmann::json &jsonObject, const AbilityInfo &abilityInfo)
353 {
354 jsonObject = nlohmann::json {
355 {JSON_KEY_NAME, abilityInfo.name},
356 {JSON_KEY_LABEL, abilityInfo.label},
357 {JSON_KEY_DESCRIPTION, abilityInfo.description},
358 {JSON_KEY_ICON_PATH, abilityInfo.iconPath},
359 {JSON_KEY_THEME, abilityInfo.theme},
360 {JSON_KEY_VISIBLE, abilityInfo.visible},
361 {JSON_KEY_ABILITY_CONTINUABLE, abilityInfo.continuable},
362 {PRIORITY, abilityInfo.priority},
363 {JSON_KEY_IS_LAUNCHER_ABILITY, abilityInfo.isLauncherAbility},
364 {JSON_KEY_IS_NATIVE_ABILITY, abilityInfo.isNativeAbility},
365 {JSON_KEY_ENABLED, abilityInfo.enabled},
366 {JSON_KEY_SUPPORT_PIP_MODE, abilityInfo.supportPipMode},
367 {JSON_KEY_READ_PERMISSION, abilityInfo.readPermission},
368 {JSON_KEY_WRITE_PERMISSION, abilityInfo.writePermission},
369 {JSON_KEY_SRC_PATH, abilityInfo.srcPath},
370 {JSON_KEY_SRC_LANGUAGE, abilityInfo.srcLanguage},
371 {JSON_KEY_CONFIG_CHANGES, abilityInfo.configChanges},
372 {JSON_KEY_FORM_ENTITY, abilityInfo.formEntity},
373 {JSON_KEY_MIN_FORM_HEIGHT, abilityInfo.minFormHeight},
374 {JSON_KEY_DEFAULT_FORM_HEIGHT, abilityInfo.defaultFormHeight},
375 {JSON_KEY_BACKGROUND_MODES, abilityInfo.backgroundModes},
376 {JSON_KEY_MIN_FORM_WIDTH, abilityInfo.minFormWidth},
377 {JSON_KEY_DEFAULT_FORM_WIDTH, abilityInfo.defaultFormWidth},
378 {JSON_KEY_LABEL_ID, abilityInfo.labelId},
379 {JSON_KEY_DESCRIPTION_ID, abilityInfo.descriptionId},
380 {JSON_KEY_ICON_ID, abilityInfo.iconId},
381 {JSON_KEY_KIND, abilityInfo.kind},
382 {JSON_KEY_TYPE, abilityInfo.type},
383 {JSON_KEY_ORIENTATION, abilityInfo.orientation},
384 {JSON_KEY_LAUNCH_MODE, abilityInfo.launchMode},
385 {JSON_KEY_PERMISSIONS, abilityInfo.permissions},
386 {JSON_KEY_PROCESS, abilityInfo.process},
387 {JSON_KEY_DEVICE_TYPES, abilityInfo.deviceTypes},
388 {JSON_KEY_DEVICE_CAPABILITIES, abilityInfo.deviceCapabilities},
389 {JSON_KEY_URI, abilityInfo.uri},
390 {JSON_KEY_TARGET_ABILITY, abilityInfo.targetAbility},
391 {APPLICAION_INFO, abilityInfo.applicationInfo},
392 {JSON_KEY_PACKAGE, abilityInfo.package},
393 {JSON_KEY_BUNDLE_NAME, abilityInfo.bundleName},
394 {JSON_KEY_MODULE_NAME, abilityInfo.moduleName},
395 {JSON_KEY_APPLICATION_NAME, abilityInfo.applicationName},
396 {JSON_KEY_DEVICE_ID, abilityInfo.deviceId},
397 {JSON_KEY_CODE_PATH, abilityInfo.codePath},
398 {JSON_KEY_RESOURCE_PATH, abilityInfo.resourcePath},
399 {JSON_KEY_LIB_PATH, abilityInfo.libPath},
400 {JSON_KEY_META_DATA, abilityInfo.metaData},
401 {JSON_KEY_FORM_ENABLED, abilityInfo.formEnabled},
402 {SRC_ENTRANCE, abilityInfo.srcEntrance},
403 {META_DATA, abilityInfo.metadata},
404 {IS_MODULE_JSON, abilityInfo.isModuleJson},
405 {IS_STAGE_BASED_MODEL, abilityInfo.isStageBasedModel}
406 };
407 }
408
from_json(const nlohmann::json & jsonObject,CustomizeData & customizeData)409 void from_json(const nlohmann::json &jsonObject, CustomizeData &customizeData)
410 {
411 const auto &jsonObjectEnd = jsonObject.end();
412 int32_t parseResult = ERR_OK;
413 GetValueIfFindKey<std::string>(jsonObject,
414 jsonObjectEnd,
415 JSON_KEY_NAME,
416 customizeData.name,
417 JsonType::STRING,
418 false,
419 parseResult,
420 ArrayType::NOT_ARRAY);
421 GetValueIfFindKey<std::string>(jsonObject,
422 jsonObjectEnd,
423 JSON_KEY_META_VALUE,
424 customizeData.value,
425 JsonType::STRING,
426 false,
427 parseResult,
428 ArrayType::NOT_ARRAY);
429 GetValueIfFindKey<std::string>(jsonObject,
430 jsonObjectEnd,
431 JSON_KEY_META_EXTRA,
432 customizeData.extra,
433 JsonType::STRING,
434 false,
435 parseResult,
436 ArrayType::NOT_ARRAY);
437 }
438
from_json(const nlohmann::json & jsonObject,MetaData & metaData)439 void from_json(const nlohmann::json &jsonObject, MetaData &metaData)
440 {
441 const auto &jsonObjectEnd = jsonObject.end();
442 int32_t parseResult = ERR_OK;
443 GetValueIfFindKey<std::vector<CustomizeData>>(jsonObject,
444 jsonObjectEnd,
445 JSON_KEY_CUSTOMIZE_DATA,
446 metaData.customizeData,
447 JsonType::ARRAY,
448 false,
449 parseResult,
450 ArrayType::OBJECT);
451 }
452
from_json(const nlohmann::json & jsonObject,Metadata & metadata)453 void from_json(const nlohmann::json &jsonObject, Metadata &metadata)
454 {
455 const auto &jsonObjectEnd = jsonObject.end();
456 int32_t parseResult = ERR_OK;
457 GetValueIfFindKey<std::string>(jsonObject,
458 jsonObjectEnd,
459 META_DATA_NAME,
460 metadata.name,
461 JsonType::STRING,
462 false,
463 parseResult,
464 ArrayType::NOT_ARRAY);
465 GetValueIfFindKey<std::string>(jsonObject,
466 jsonObjectEnd,
467 META_DATA_VALUE,
468 metadata.value,
469 JsonType::STRING,
470 false,
471 parseResult,
472 ArrayType::NOT_ARRAY);
473 GetValueIfFindKey<std::string>(jsonObject,
474 jsonObjectEnd,
475 META_DATA_RESOURCE,
476 metadata.resource,
477 JsonType::STRING,
478 false,
479 parseResult,
480 ArrayType::NOT_ARRAY);
481 if (parseResult != ERR_OK) {
482 APP_LOGE("read Ability Metadata from database error, error code : %{public}d", parseResult);
483 }
484 }
485
from_json(const nlohmann::json & jsonObject,AbilityInfo & abilityInfo)486 void from_json(const nlohmann::json &jsonObject, AbilityInfo &abilityInfo)
487 {
488 const auto &jsonObjectEnd = jsonObject.end();
489 int32_t parseResult = ERR_OK;
490 GetValueIfFindKey<std::string>(jsonObject,
491 jsonObjectEnd,
492 JSON_KEY_NAME,
493 abilityInfo.name,
494 JsonType::STRING,
495 false,
496 parseResult,
497 ArrayType::NOT_ARRAY);
498 GetValueIfFindKey<std::string>(jsonObject,
499 jsonObjectEnd,
500 JSON_KEY_LABEL,
501 abilityInfo.label,
502 JsonType::STRING,
503 false,
504 parseResult,
505 ArrayType::NOT_ARRAY);
506 GetValueIfFindKey<uint32_t>(jsonObject,
507 jsonObjectEnd,
508 JSON_KEY_BACKGROUND_MODES,
509 abilityInfo.backgroundModes,
510 JsonType::NUMBER,
511 false,
512 parseResult,
513 ArrayType::NOT_ARRAY);
514 GetValueIfFindKey<std::string>(jsonObject,
515 jsonObjectEnd,
516 JSON_KEY_DESCRIPTION,
517 abilityInfo.description,
518 JsonType::STRING,
519 false,
520 parseResult,
521 ArrayType::NOT_ARRAY);
522 GetValueIfFindKey<std::string>(jsonObject,
523 jsonObjectEnd,
524 JSON_KEY_ICON_PATH,
525 abilityInfo.iconPath,
526 JsonType::STRING,
527 false,
528 parseResult,
529 ArrayType::NOT_ARRAY);
530 GetValueIfFindKey<std::string>(jsonObject,
531 jsonObjectEnd,
532 JSON_KEY_THEME,
533 abilityInfo.theme,
534 JsonType::STRING,
535 false,
536 parseResult,
537 ArrayType::NOT_ARRAY);
538 GetValueIfFindKey<ApplicationInfo>(jsonObject,
539 jsonObjectEnd,
540 APPLICAION_INFO,
541 abilityInfo.applicationInfo,
542 JsonType::OBJECT,
543 false,
544 parseResult,
545 ArrayType::NOT_ARRAY);
546 GetValueIfFindKey<bool>(jsonObject,
547 jsonObjectEnd,
548 JSON_KEY_VISIBLE,
549 abilityInfo.visible,
550 JsonType::BOOLEAN,
551 false,
552 parseResult,
553 ArrayType::NOT_ARRAY);
554 GetValueIfFindKey<bool>(jsonObject,
555 jsonObjectEnd,
556 JSON_KEY_ABILITY_CONTINUABLE,
557 abilityInfo.continuable,
558 JsonType::BOOLEAN,
559 false,
560 parseResult,
561 ArrayType::NOT_ARRAY);
562 GetValueIfFindKey<int32_t>(jsonObject,
563 jsonObjectEnd,
564 PRIORITY,
565 abilityInfo.priority,
566 JsonType::NUMBER,
567 false,
568 parseResult,
569 ArrayType::NOT_ARRAY);
570 GetValueIfFindKey<bool>(jsonObject,
571 jsonObjectEnd,
572 JSON_KEY_IS_LAUNCHER_ABILITY,
573 abilityInfo.isLauncherAbility,
574 JsonType::BOOLEAN,
575 false,
576 parseResult,
577 ArrayType::NOT_ARRAY);
578 GetValueIfFindKey<bool>(jsonObject,
579 jsonObjectEnd,
580 JSON_KEY_IS_NATIVE_ABILITY,
581 abilityInfo.isNativeAbility,
582 JsonType::BOOLEAN,
583 false,
584 parseResult,
585 ArrayType::NOT_ARRAY);
586 GetValueIfFindKey<bool>(jsonObject,
587 jsonObjectEnd,
588 JSON_KEY_ENABLED,
589 abilityInfo.enabled,
590 JsonType::BOOLEAN,
591 false,
592 parseResult,
593 ArrayType::NOT_ARRAY);
594 GetValueIfFindKey<bool>(jsonObject,
595 jsonObjectEnd,
596 JSON_KEY_SUPPORT_PIP_MODE,
597 abilityInfo.supportPipMode,
598 JsonType::BOOLEAN,
599 false,
600 parseResult,
601 ArrayType::NOT_ARRAY);
602 GetValueIfFindKey<std::string>(jsonObject,
603 jsonObjectEnd,
604 JSON_KEY_READ_PERMISSION,
605 abilityInfo.readPermission,
606 JsonType::STRING,
607 false,
608 parseResult,
609 ArrayType::NOT_ARRAY);
610 GetValueIfFindKey<std::string>(jsonObject,
611 jsonObjectEnd,
612 JSON_KEY_WRITE_PERMISSION,
613 abilityInfo.writePermission,
614 JsonType::STRING,
615 false,
616 parseResult,
617 ArrayType::NOT_ARRAY);
618 GetValueIfFindKey<std::string>(jsonObject,
619 jsonObjectEnd,
620 JSON_KEY_SRC_PATH,
621 abilityInfo.srcPath,
622 JsonType::STRING,
623 false,
624 parseResult,
625 ArrayType::NOT_ARRAY);
626 GetValueIfFindKey<std::string>(jsonObject,
627 jsonObjectEnd,
628 JSON_KEY_SRC_LANGUAGE,
629 abilityInfo.srcLanguage,
630 JsonType::STRING,
631 false,
632 parseResult,
633 ArrayType::NOT_ARRAY);
634 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
635 jsonObjectEnd,
636 JSON_KEY_CONFIG_CHANGES,
637 abilityInfo.configChanges,
638 JsonType::ARRAY,
639 false,
640 parseResult,
641 ArrayType::STRING);
642 GetValueIfFindKey<uint32_t>(jsonObject,
643 jsonObjectEnd,
644 JSON_KEY_FORM_ENTITY,
645 abilityInfo.formEntity,
646 JsonType::NUMBER,
647 false,
648 parseResult,
649 ArrayType::NOT_ARRAY);
650 GetValueIfFindKey<int32_t>(jsonObject,
651 jsonObjectEnd,
652 JSON_KEY_MIN_FORM_HEIGHT,
653 abilityInfo.minFormHeight,
654 JsonType::NUMBER,
655 false,
656 parseResult,
657 ArrayType::NOT_ARRAY);
658 GetValueIfFindKey<int32_t>(jsonObject,
659 jsonObjectEnd,
660 JSON_KEY_DEFAULT_FORM_HEIGHT,
661 abilityInfo.defaultFormHeight,
662 JsonType::NUMBER,
663 false,
664 parseResult,
665 ArrayType::NOT_ARRAY);
666 GetValueIfFindKey<int32_t>(jsonObject,
667 jsonObjectEnd,
668 JSON_KEY_MIN_FORM_WIDTH,
669 abilityInfo.minFormWidth,
670 JsonType::NUMBER,
671 false,
672 parseResult,
673 ArrayType::NOT_ARRAY);
674 GetValueIfFindKey<int32_t>(jsonObject,
675 jsonObjectEnd,
676 JSON_KEY_DEFAULT_FORM_WIDTH,
677 abilityInfo.defaultFormWidth,
678 JsonType::NUMBER,
679 false,
680 parseResult,
681 ArrayType::NOT_ARRAY);
682 GetValueIfFindKey<int32_t>(jsonObject,
683 jsonObjectEnd,
684 JSON_KEY_LABEL_ID,
685 abilityInfo.labelId,
686 JsonType::NUMBER,
687 false,
688 parseResult,
689 ArrayType::NOT_ARRAY);
690 GetValueIfFindKey<int32_t>(jsonObject,
691 jsonObjectEnd,
692 JSON_KEY_DESCRIPTION_ID,
693 abilityInfo.descriptionId,
694 JsonType::NUMBER,
695 false,
696 parseResult,
697 ArrayType::NOT_ARRAY);
698 GetValueIfFindKey<int32_t>(jsonObject,
699 jsonObjectEnd,
700 JSON_KEY_ICON_ID,
701 abilityInfo.iconId,
702 JsonType::NUMBER,
703 false,
704 parseResult,
705 ArrayType::NOT_ARRAY);
706 GetValueIfFindKey<std::string>(jsonObject,
707 jsonObjectEnd,
708 JSON_KEY_KIND,
709 abilityInfo.kind,
710 JsonType::STRING,
711 false,
712 parseResult,
713 ArrayType::NOT_ARRAY);
714 GetValueIfFindKey<AbilityType>(jsonObject,
715 jsonObjectEnd,
716 JSON_KEY_TYPE,
717 abilityInfo.type,
718 JsonType::NUMBER,
719 false,
720 parseResult,
721 ArrayType::NOT_ARRAY);
722 GetValueIfFindKey<DisplayOrientation>(jsonObject,
723 jsonObjectEnd,
724 JSON_KEY_ORIENTATION,
725 abilityInfo.orientation,
726 JsonType::NUMBER,
727 false,
728 parseResult,
729 ArrayType::NOT_ARRAY);
730 GetValueIfFindKey<LaunchMode>(jsonObject,
731 jsonObjectEnd,
732 JSON_KEY_LAUNCH_MODE,
733 abilityInfo.launchMode,
734 JsonType::NUMBER,
735 false,
736 parseResult,
737 ArrayType::NOT_ARRAY);
738 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
739 jsonObjectEnd,
740 JSON_KEY_PERMISSIONS,
741 abilityInfo.permissions,
742 JsonType::ARRAY,
743 false,
744 parseResult,
745 ArrayType::STRING);
746 GetValueIfFindKey<std::string>(jsonObject,
747 jsonObjectEnd,
748 JSON_KEY_PROCESS,
749 abilityInfo.process,
750 JsonType::STRING,
751 false,
752 parseResult,
753 ArrayType::NOT_ARRAY);
754 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
755 jsonObjectEnd,
756 JSON_KEY_DEVICE_TYPES,
757 abilityInfo.deviceTypes,
758 JsonType::ARRAY,
759 false,
760 parseResult,
761 ArrayType::STRING);
762 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
763 jsonObjectEnd,
764 JSON_KEY_DEVICE_CAPABILITIES,
765 abilityInfo.deviceCapabilities,
766 JsonType::ARRAY,
767 false,
768 parseResult,
769 ArrayType::STRING);
770 GetValueIfFindKey<std::string>(jsonObject,
771 jsonObjectEnd,
772 JSON_KEY_URI,
773 abilityInfo.uri,
774 JsonType::STRING,
775 false,
776 parseResult,
777 ArrayType::NOT_ARRAY);
778 GetValueIfFindKey<std::string>(jsonObject,
779 jsonObjectEnd,
780 JSON_KEY_TARGET_ABILITY,
781 abilityInfo.targetAbility,
782 JsonType::STRING,
783 false,
784 parseResult,
785 ArrayType::NOT_ARRAY);
786 GetValueIfFindKey<std::string>(jsonObject,
787 jsonObjectEnd,
788 JSON_KEY_PACKAGE,
789 abilityInfo.package,
790 JsonType::STRING,
791 false,
792 parseResult,
793 ArrayType::NOT_ARRAY);
794 GetValueIfFindKey<std::string>(jsonObject,
795 jsonObjectEnd,
796 JSON_KEY_BUNDLE_NAME,
797 abilityInfo.bundleName,
798 JsonType::STRING,
799 false,
800 parseResult,
801 ArrayType::NOT_ARRAY);
802 GetValueIfFindKey<std::string>(jsonObject,
803 jsonObjectEnd,
804 JSON_KEY_MODULE_NAME,
805 abilityInfo.moduleName,
806 JsonType::STRING,
807 false,
808 parseResult,
809 ArrayType::NOT_ARRAY);
810 GetValueIfFindKey<std::string>(jsonObject,
811 jsonObjectEnd,
812 JSON_KEY_APPLICATION_NAME,
813 abilityInfo.applicationName,
814 JsonType::STRING,
815 false,
816 parseResult,
817 ArrayType::NOT_ARRAY);
818 GetValueIfFindKey<std::string>(jsonObject,
819 jsonObjectEnd,
820 JSON_KEY_DEVICE_ID,
821 abilityInfo.deviceId,
822 JsonType::STRING,
823 false,
824 parseResult,
825 ArrayType::NOT_ARRAY);
826 GetValueIfFindKey<std::string>(jsonObject,
827 jsonObjectEnd,
828 JSON_KEY_CODE_PATH,
829 abilityInfo.codePath,
830 JsonType::STRING,
831 false,
832 parseResult,
833 ArrayType::NOT_ARRAY);
834 GetValueIfFindKey<std::string>(jsonObject,
835 jsonObjectEnd,
836 JSON_KEY_RESOURCE_PATH,
837 abilityInfo.resourcePath,
838 JsonType::STRING,
839 false,
840 parseResult,
841 ArrayType::NOT_ARRAY);
842 GetValueIfFindKey<std::string>(jsonObject,
843 jsonObjectEnd,
844 JSON_KEY_LIB_PATH,
845 abilityInfo.libPath,
846 JsonType::STRING,
847 false,
848 parseResult,
849 ArrayType::NOT_ARRAY);
850 GetValueIfFindKey<MetaData>(jsonObject,
851 jsonObjectEnd,
852 JSON_KEY_META_DATA,
853 abilityInfo.metaData,
854 JsonType::OBJECT,
855 false,
856 parseResult,
857 ArrayType::NOT_ARRAY);
858 GetValueIfFindKey<bool>(jsonObject,
859 jsonObjectEnd,
860 JSON_KEY_FORM_ENABLED,
861 abilityInfo.formEnabled,
862 JsonType::BOOLEAN,
863 false,
864 parseResult,
865 ArrayType::NOT_ARRAY);
866 GetValueIfFindKey<std::string>(jsonObject,
867 jsonObjectEnd,
868 SRC_ENTRANCE,
869 abilityInfo.srcEntrance,
870 JsonType::STRING,
871 false,
872 parseResult,
873 ArrayType::NOT_ARRAY);
874 GetValueIfFindKey<std::vector<Metadata>>(jsonObject,
875 jsonObjectEnd,
876 META_DATA,
877 abilityInfo.metadata,
878 JsonType::ARRAY,
879 false,
880 parseResult,
881 ArrayType::OBJECT);
882 GetValueIfFindKey<bool>(jsonObject,
883 jsonObjectEnd,
884 IS_MODULE_JSON,
885 abilityInfo.isModuleJson,
886 JsonType::BOOLEAN,
887 false,
888 parseResult,
889 ArrayType::NOT_ARRAY);
890 GetValueIfFindKey<bool>(jsonObject,
891 jsonObjectEnd,
892 IS_STAGE_BASED_MODEL,
893 abilityInfo.isStageBasedModel,
894 JsonType::BOOLEAN,
895 false,
896 parseResult,
897 ArrayType::NOT_ARRAY);
898 if (parseResult != ERR_OK) {
899 APP_LOGE("read Ability from database error, error code : %{public}d", parseResult);
900 }
901 }
902
ConvertToCompatiableAbilityInfo(CompatibleAbilityInfo & compatibleAbilityInfo) const903 void AbilityInfo::ConvertToCompatiableAbilityInfo(CompatibleAbilityInfo& compatibleAbilityInfo) const
904 {
905 APP_LOGE("AbilityInfo::ConvertToCompatiableAbilityInfo called");
906 compatibleAbilityInfo.package = package;
907 compatibleAbilityInfo.name = name;
908 compatibleAbilityInfo.label = label;
909 compatibleAbilityInfo.description = description;
910 compatibleAbilityInfo.iconPath = iconPath;
911 compatibleAbilityInfo.uri = uri;
912 compatibleAbilityInfo.moduleName = moduleName;
913 compatibleAbilityInfo.process = process;
914 compatibleAbilityInfo.targetAbility = targetAbility;
915 compatibleAbilityInfo.appName = appName;
916 compatibleAbilityInfo.privacyUrl = privacyUrl;
917 compatibleAbilityInfo.privacyName = privacyName;
918 compatibleAbilityInfo.downloadUrl = downloadUrl;
919 compatibleAbilityInfo.versionName = versionName;
920 compatibleAbilityInfo.backgroundModes = backgroundModes;
921 compatibleAbilityInfo.packageSize = packageSize;
922 compatibleAbilityInfo.visible = visible;
923 compatibleAbilityInfo.formEnabled = formEnabled;
924 compatibleAbilityInfo.multiUserShared = multiUserShared;
925 compatibleAbilityInfo.type = type;
926 compatibleAbilityInfo.subType = subType;
927 compatibleAbilityInfo.orientation = orientation;
928 compatibleAbilityInfo.launchMode = launchMode;
929 compatibleAbilityInfo.permissions = permissions;
930 compatibleAbilityInfo.deviceTypes = deviceTypes;
931 compatibleAbilityInfo.deviceCapabilities = deviceCapabilities;
932 compatibleAbilityInfo.supportPipMode = supportPipMode;
933 compatibleAbilityInfo.grantPermission = grantPermission;
934 compatibleAbilityInfo.readPermission = readPermission;
935 compatibleAbilityInfo.writePermission = writePermission;
936 compatibleAbilityInfo.uriPermissionMode = uriPermissionMode;
937 compatibleAbilityInfo.uriPermissionPath = uriPermissionPath;
938 compatibleAbilityInfo.directLaunch = directLaunch;
939 compatibleAbilityInfo.bundleName = bundleName;
940 compatibleAbilityInfo.className = className;
941 compatibleAbilityInfo.originalClassName = originalClassName;
942 compatibleAbilityInfo.deviceId = deviceId;
943 CompatibleApplicationInfo convertedCompatibleApplicationInfo;
944 applicationInfo.ConvertToCompatibleApplicationInfo(convertedCompatibleApplicationInfo);
945 compatibleAbilityInfo.applicationInfo = convertedCompatibleApplicationInfo;
946 compatibleAbilityInfo.formEntity = formEntity;
947 compatibleAbilityInfo.minFormHeight = minFormHeight;
948 compatibleAbilityInfo.defaultFormHeight = defaultFormHeight;
949 compatibleAbilityInfo.minFormWidth = minFormWidth;
950 compatibleAbilityInfo.defaultFormWidth = defaultFormWidth;
951 compatibleAbilityInfo.iconId = iconId;
952 compatibleAbilityInfo.labelId = labelId;
953 compatibleAbilityInfo.descriptionId = descriptionId;
954 compatibleAbilityInfo.enabled = enabled;
955 }
956 } // namespace AppExecFwk
957 } // namespace OHOS
958