1 /*
2 * Copyright (c) 2021-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 "pre_install_bundle_info.h"
17
18 #include "bundle_util.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 constexpr const char* BUNDLE_NAME = "bundleName";
24 constexpr const char* VERSION_CODE = "versionCode";
25 constexpr const char* BUNDLE_PATHS = "bundlePaths";
26 constexpr const char* APP_TYPE = "appType";
27 constexpr const char* REMOVABLE = "removable";
28 constexpr const char* IS_UNINSTALLED = "isUninstalled";
29 constexpr const char* MODULE_NAME = "moduleName";
30 constexpr const char* LABEL_ID = "labelId";
31 constexpr const char* ICON_ID = "iconId";
32 constexpr const char* SYSTEM_APP = "systemApp";
33 constexpr const char* BUNDLE_TYPE = "bundleType";
34 constexpr const char* FORCE_UNINSTALL_USERS = "forceUninstallUsers";
35 constexpr const char* U1_ENABLE = "u1Enable";
36
37 } // namespace
38
ToJson(nlohmann::json & jsonObject) const39 void PreInstallBundleInfo::ToJson(nlohmann::json &jsonObject) const
40 {
41 jsonObject[BUNDLE_NAME] = bundleName_;
42 jsonObject[VERSION_CODE] = versionCode_;
43 jsonObject[BUNDLE_PATHS] = bundlePaths_;
44 jsonObject[FORCE_UNINSTALL_USERS] = forceUninstalledUsers_;
45 jsonObject[APP_TYPE] = appType_;
46 jsonObject[REMOVABLE] = removable_;
47 jsonObject[IS_UNINSTALLED] = isUninstalled_;
48 jsonObject[MODULE_NAME] = moduleName_;
49 jsonObject[LABEL_ID] = labelId_;
50 jsonObject[ICON_ID] = iconId_;
51 jsonObject[SYSTEM_APP] = systemApp_;
52 jsonObject[BUNDLE_TYPE] = bundleType_;
53 jsonObject[U1_ENABLE] = u1Enable_;
54 }
55
FromJson(const nlohmann::json & jsonObject)56 int32_t PreInstallBundleInfo::FromJson(const nlohmann::json &jsonObject)
57 {
58 const auto &jsonObjectEnd = jsonObject.end();
59 int32_t parseResult = ERR_OK;
60 BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, BUNDLE_NAME,
61 bundleName_, true, parseResult);
62 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, VERSION_CODE,
63 versionCode_, JsonType::NUMBER, true, parseResult, ArrayType::NOT_ARRAY);
64 GetValueIfFindKey<std::vector<std::string>>(jsonObject, jsonObjectEnd, BUNDLE_PATHS,
65 bundlePaths_, JsonType::ARRAY, true, parseResult, ArrayType::STRING);
66 GetValueIfFindKey<std::vector<int32_t>>(jsonObject, jsonObjectEnd, FORCE_UNINSTALL_USERS,
67 forceUninstalledUsers_, JsonType::ARRAY, false, parseResult, ArrayType::NUMBER);
68 GetValueIfFindKey<Constants::AppType>(jsonObject, jsonObjectEnd, APP_TYPE,
69 appType_, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
70 BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, jsonObjectEnd, REMOVABLE,
71 removable_, false, parseResult);
72 BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, jsonObjectEnd, IS_UNINSTALLED,
73 isUninstalled_, false, parseResult);
74 BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, MODULE_NAME,
75 moduleName_, false, parseResult);
76 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, LABEL_ID,
77 labelId_, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
78 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, ICON_ID,
79 iconId_, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
80 BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, jsonObjectEnd, SYSTEM_APP,
81 systemApp_, false, parseResult);
82 GetValueIfFindKey<BundleType>(jsonObject, jsonObjectEnd, BUNDLE_TYPE,
83 bundleType_, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
84 BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, jsonObjectEnd, U1_ENABLE,
85 u1Enable_, false, parseResult);
86 return parseResult;
87 }
88
ToString() const89 std::string PreInstallBundleInfo::ToString() const
90 {
91 nlohmann::json jsonObject;
92 jsonObject[BUNDLE_NAME] = bundleName_;
93 jsonObject[VERSION_CODE] = versionCode_;
94 jsonObject[BUNDLE_PATHS] = bundlePaths_;
95 jsonObject[FORCE_UNINSTALL_USERS] = forceUninstalledUsers_;
96 jsonObject[APP_TYPE] = appType_;
97 jsonObject[REMOVABLE] = removable_;
98 jsonObject[IS_UNINSTALLED] = isUninstalled_;
99 jsonObject[MODULE_NAME] = moduleName_;
100 jsonObject[LABEL_ID] = labelId_;
101 jsonObject[ICON_ID] = iconId_;
102 jsonObject[SYSTEM_APP] = systemApp_;
103 jsonObject[BUNDLE_TYPE] = bundleType_;
104 jsonObject[U1_ENABLE] = u1Enable_;
105 return jsonObject.dump();
106 }
107
CalculateHapTotalSize()108 void PreInstallBundleInfo::CalculateHapTotalSize()
109 {
110 hapTotalSize_ = 0;
111 for (const auto &bundlePath : bundlePaths_) {
112 hapTotalSize_ += BundleUtil::CalculateFileSize(bundlePath);
113 }
114 }
115 } // namespace AppExecFwk
116 } // namespace OHOS
117