1 /* 2 * Copyright (c) 2020 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 #ifndef OHOS_BUNDLE_PARSER_H 17 #define OHOS_BUNDLE_PARSER_H 18 19 #include "adapter.h" 20 #include "bundle_common.h" 21 #include "bundle_info.h" 22 #include "stdint.h" 23 24 #include <string> 25 26 #include "cJSON.h" 27 28 namespace OHOS { 29 class BundleParser { 30 public: 31 BundleParser() = default; 32 ~BundleParser() = default; 33 34 BundleInfo *ParseHapProfile(const char *path); 35 uint8_t ParseHapProfile(const std::string &path, Permissions &permissions, BundleRes &bundleRes, 36 BundleInfo **bundleInfo); 37 static int8_t ParseBundleParam(const char *path, char **bundleName, int32_t &versionCode); 38 private: 39 static uint8_t ParseJsonInfo(const cJSON *appObject, const cJSON *configObject, const cJSON *moduleObject, 40 BundleProfile &bundleProfile, BundleRes &bundleRes); 41 static uint8_t ParseDeviceConfig(const cJSON *configObject, BundleProfile &bundleProfile); 42 static uint8_t ParseModuleInfo(const cJSON *moduleObject, BundleProfile &bundleProfile, BundleRes &bundleRes); 43 static uint8_t ParseModuleMetaData(const cJSON *moduleObject, BundleProfile &bundleProfile); 44 static uint8_t ParseDeviceType(const cJSON *object, BundleProfile &bundleProfile); 45 static uint8_t ParseAbilityInfos(const cJSON *moduleObject, BundleProfile &bundleProfile, BundleRes &bundleRes); 46 static uint8_t ParseAllAbilityInfo(const cJSON *abilityObjects, BundleProfile &bundleProfile, 47 BundleRes &bundleRes); 48 static uint8_t ParseAbilityType(const cJSON *abilityObjectItem, AbilityInfo &abilityInfo); 49 static uint8_t ParseAbilityLauchMode(const cJSON *abilityObjectItem, AbilityInfo &abilityInfo); 50 static uint8_t ParseAbilityVisible(const cJSON *abilityObjectItem, AbilityInfo &abilityInfo); 51 static uint8_t ParseAbilityDeviceCap(const cJSON *abilityObjectItem, AbilityInfo &abilityInfo); 52 static uint8_t ParsePerAbilityInfo(const cJSON *abilityObjectItem, BundleProfile &bundleProfile, 53 AbilityRes &abilityRes, uint32_t index); 54 static uint8_t ParseModuleDesc(const cJSON *moduleObject, BundleProfile &bundleProfile, BundleRes &bundleRes); 55 static uint8_t ParseAbilityDesc(const cJSON *abilityObject, BundleProfile &bundleProfile, AbilityRes &abilityRes, 56 int32_t index); 57 static uint8_t ParseAbilityIcon(const cJSON *abilityObject, BundleProfile &bundleProfile, AbilityRes &abilityRes, 58 int32_t index); 59 static uint8_t ParseAbilityLabel(const cJSON *abilityObject, BundleProfile &bundleProfile, AbilityRes &abilityRes, 60 int32_t index); 61 static char *ParseValue(const cJSON *object, const char *key); 62 static int32_t ParseValue(const cJSON *object, const char *key, int32_t defaultValue); 63 static cJSON *ParseValue(const cJSON *object, const char *key, cJSON *defaultValue); 64 static bool CheckBundleNameIsValid(const char *bundleName); 65 static uint8_t ParsePermissions(const cJSON *object, Permissions &permissions); 66 static bool SetReqPermission(const cJSON *object, PermissionTrans *permission); 67 static bool CheckAbilityCapIsValid(AbilityInfo &abilityInfo, char sysCaps[][MAX_SYSCAP_NAME_LEN], 68 int sysNum); 69 static uint8_t CheckDeviceCapIsValid(BundleProfile &bundleInfo); 70 }; 71 72 #define CHECK_NULL(object, errorCode) \ 73 do { \ 74 if ((object) == nullptr) { \ 75 return errorCode; \ 76 } \ 77 } while (0) 78 79 #define CHECK_LENGTH(length, maxLength, errorCode) \ 80 do { \ 81 if ((length) > (maxLength)) { \ 82 return errorCode; \ 83 } \ 84 } while (0) 85 86 #define CHECK_IS_TRUE(result, errorCode) \ 87 do { \ 88 if (!(result)) { \ 89 return errorCode; \ 90 } \ 91 } while (0) 92 93 #define CHECK_PARSE_RESULT(errorCode, object, bundleProfile) \ 94 do { \ 95 if ((errorCode) != ERR_OK) { \ 96 FREE_BUNDLE_PROFILE(bundleProfile); \ 97 cJSON_Delete(object); \ 98 return errorCode; \ 99 } \ 100 } while (0) 101 102 #define FREE_BUNDLE_PROFILE(bundleProfile) \ 103 do { \ 104 AdapterFree((bundleProfile).abilityInfos); \ 105 for (uint8_t i = 0; i < METADATA_SIZE; i++) { \ 106 AdapterFree((bundleProfile).moduleInfo.metaData[i]); \ 107 } \ 108 } while (0) 109 } // namespace OHOS 110 #endif // OHOS_BUNDLE_PARSER_H 111