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 } // namespace
35
ToJson(nlohmann::json &jsonObject) const36 void PreInstallBundleInfo::ToJson(nlohmann::json &jsonObject) const
37 {
38 jsonObject[BUNDLE_NAME] = bundleName_;
39 jsonObject[VERSION_CODE] = versionCode_;
40 jsonObject[BUNDLE_PATHS] = bundlePaths_;
41 jsonObject[APP_TYPE] = appType_;
42 jsonObject[REMOVABLE] = removable_;
43 jsonObject[IS_UNINSTALLED] = isUninstalled_;
44 jsonObject[MODULE_NAME] = moduleName_;
45 jsonObject[LABEL_ID] = labelId_;
46 jsonObject[ICON_ID] = iconId_;
47 jsonObject[SYSTEM_APP] = systemApp_;
48 jsonObject[BUNDLE_TYPE] = bundleType_;
49 }
50
FromJson(const nlohmann::json &jsonObject)51 int32_t PreInstallBundleInfo::FromJson(const nlohmann::json &jsonObject)
52 {
53 const auto &jsonObjectEnd = jsonObject.end();
54 int32_t parseResult = ERR_OK;
55 BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, BUNDLE_NAME,
56 bundleName_, true, parseResult);
57 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, VERSION_CODE,
58 versionCode_, JsonType::NUMBER, true, parseResult, ArrayType::NOT_ARRAY);
59 GetValueIfFindKey<std::vector<std::string>>(jsonObject, jsonObjectEnd, BUNDLE_PATHS,
60 bundlePaths_, JsonType::ARRAY, true, parseResult, ArrayType::STRING);
61 GetValueIfFindKey<Constants::AppType>(jsonObject, jsonObjectEnd, APP_TYPE,
62 appType_, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
63 BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, jsonObjectEnd, REMOVABLE,
64 removable_, false, parseResult);
65 BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, jsonObjectEnd, IS_UNINSTALLED,
66 isUninstalled_, false, parseResult);
67 BMSJsonUtil::GetStrValueIfFindKey(jsonObject, jsonObjectEnd, MODULE_NAME,
68 moduleName_, false, parseResult);
69 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, LABEL_ID,
70 labelId_, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
71 GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, ICON_ID,
72 iconId_, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
73 BMSJsonUtil::GetBoolValueIfFindKey(jsonObject, jsonObjectEnd, SYSTEM_APP,
74 systemApp_, false, parseResult);
75 GetValueIfFindKey<BundleType>(jsonObject, jsonObjectEnd, BUNDLE_TYPE,
76 bundleType_, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
77 return parseResult;
78 }
79
ToString() const80 std::string PreInstallBundleInfo::ToString() const
81 {
82 nlohmann::json jsonObject;
83 jsonObject[BUNDLE_NAME] = bundleName_;
84 jsonObject[VERSION_CODE] = versionCode_;
85 jsonObject[BUNDLE_PATHS] = bundlePaths_;
86 jsonObject[APP_TYPE] = appType_;
87 jsonObject[REMOVABLE] = removable_;
88 jsonObject[IS_UNINSTALLED] = isUninstalled_;
89 jsonObject[MODULE_NAME] = moduleName_;
90 jsonObject[LABEL_ID] = labelId_;
91 jsonObject[ICON_ID] = iconId_;
92 jsonObject[SYSTEM_APP] = systemApp_;
93 jsonObject[BUNDLE_TYPE] = bundleType_;
94 return jsonObject.dump();
95 }
96
CalculateHapTotalSize()97 void PreInstallBundleInfo::CalculateHapTotalSize()
98 {
99 hapTotalSize_ = 0;
100 for (const auto &bundlePath : bundlePaths_) {
101 hapTotalSize_ += BundleUtil::CalculateFileSize(bundlePath);
102 }
103 }
104 } // namespace AppExecFwk
105 } // namespace OHOS
106