1 /*
2 * Copyright (c) 2021-2023 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 } // namespace
30
ToJson(nlohmann::json & jsonObject) const31 void PreInstallBundleInfo::ToJson(nlohmann::json &jsonObject) const
32 {
33 jsonObject[BUNDLE_NAME] = bundleName_;
34 jsonObject[VERSION_CODE] = versionCode_;
35 jsonObject[BUNDLE_PATHS] = bundlePaths_;
36 jsonObject[APP_TYPE] = appType_;
37 jsonObject[REMOVABLE] = removable_;
38 jsonObject[IS_UNINSTALLED] = isUninstalled_;
39 }
40
FromJson(const nlohmann::json & jsonObject)41 int32_t PreInstallBundleInfo::FromJson(const nlohmann::json &jsonObject)
42 {
43 const auto &jsonObjectEnd = jsonObject.end();
44 int32_t parseResult = ERR_OK;
45 GetValueIfFindKey<std::string>(jsonObject,
46 jsonObjectEnd,
47 BUNDLE_NAME,
48 bundleName_,
49 JsonType::STRING,
50 true,
51 parseResult,
52 ArrayType::NOT_ARRAY);
53 GetValueIfFindKey<uint32_t>(jsonObject,
54 jsonObjectEnd,
55 VERSION_CODE,
56 versionCode_,
57 JsonType::NUMBER,
58 true,
59 parseResult,
60 ArrayType::NOT_ARRAY);
61 GetValueIfFindKey<std::vector<std::string>>(jsonObject,
62 jsonObjectEnd,
63 BUNDLE_PATHS,
64 bundlePaths_,
65 JsonType::ARRAY,
66 true,
67 parseResult,
68 ArrayType::STRING);
69 GetValueIfFindKey<Constants::AppType>(jsonObject,
70 jsonObjectEnd,
71 APP_TYPE,
72 appType_,
73 JsonType::NUMBER,
74 false,
75 parseResult,
76 ArrayType::NOT_ARRAY);
77 GetValueIfFindKey<bool>(jsonObject,
78 jsonObjectEnd,
79 REMOVABLE,
80 removable_,
81 JsonType::BOOLEAN,
82 false,
83 parseResult,
84 ArrayType::NOT_ARRAY);
85 GetValueIfFindKey<bool>(jsonObject,
86 jsonObjectEnd,
87 IS_UNINSTALLED,
88 isUninstalled_,
89 JsonType::BOOLEAN,
90 false,
91 parseResult,
92 ArrayType::NOT_ARRAY);
93 return parseResult;
94 }
95
ToString() const96 std::string PreInstallBundleInfo::ToString() const
97 {
98 nlohmann::json j;
99 j[BUNDLE_NAME] = bundleName_;
100 j[VERSION_CODE] = versionCode_;
101 j[BUNDLE_PATHS] = bundlePaths_;
102 j[APP_TYPE] = appType_;
103 j[REMOVABLE] = removable_;
104 j[IS_UNINSTALLED] = isUninstalled_;
105 return j.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