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