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 const std::string BUNDLE_NAME = "bundleName";
24 const std::string VERSION_CODE = "versionCode";
25 const std::string BUNDLE_PATHS = "bundlePaths";
26 const std::string APP_TYPE = "appType";
27 const std::string REMOVABLE = "removable";
28 const std::string IS_UNINSTALLED = "isUninstalled";
29 const std::string MODULE_NAME = "moduleName";
30 const std::string LABEL_ID = "labelId";
31 const std::string ICON_ID = "iconId";
32 const std::string SYSTEM_APP = "systemApp";
33 const std::string 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 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, BUNDLE_NAME,
56 bundleName_, JsonType::STRING, true, parseResult, ArrayType::NOT_ARRAY);
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 GetValueIfFindKey<bool>(jsonObject, jsonObjectEnd, REMOVABLE,
64 removable_, JsonType::BOOLEAN, false, parseResult, ArrayType::NOT_ARRAY);
65 GetValueIfFindKey<bool>(jsonObject, jsonObjectEnd, IS_UNINSTALLED,
66 isUninstalled_, JsonType::BOOLEAN, false, parseResult, ArrayType::NOT_ARRAY);
67 GetValueIfFindKey<std::string>(jsonObject, jsonObjectEnd, MODULE_NAME,
68 moduleName_, JsonType::STRING, false, parseResult, ArrayType::NOT_ARRAY);
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 GetValueIfFindKey<bool>(jsonObject, jsonObjectEnd, SYSTEM_APP,
74 systemApp_, JsonType::BOOLEAN, false, parseResult, ArrayType::NOT_ARRAY);
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