• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "install_exception_mgr_rdb.h"
17 
18 #include "app_log_wrapper.h"
19 #include "appexecfwk_errors.h"
20 #include "json_util.h"
21 #include "nlohmann/json.hpp"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 const char* const INSTALL_EXCEPTION_MGR_TABLE_NAME = "install_exception_mgr";
27 const char* const INSTALL_RENAME_EXCEPTION_STATUS = "installRenameExceptionStatus";
28 const char* const VERSION_CODE = "versionCode";
29 }
30 
FromString(const std::string & installExceptionInfoStr)31 bool InstallExceptionInfo::FromString(const std::string &installExceptionInfoStr)
32 {
33     nlohmann::json jsonObject = nlohmann::json::parse(installExceptionInfoStr, nullptr, false);
34     if (jsonObject.is_discarded()) {
35         APP_LOGE("failed parse installExceptionInfoStr: %{public}s", installExceptionInfoStr.c_str());
36         return false;
37     }
38     const auto &jsonObjectEnd = jsonObject.end();
39     int32_t parseResult = ERR_OK;
40     GetValueIfFindKey<InstallRenameExceptionStatus>(jsonObject, jsonObjectEnd, INSTALL_RENAME_EXCEPTION_STATUS,
41         status, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
42     GetValueIfFindKey<uint32_t>(jsonObject, jsonObjectEnd, VERSION_CODE,
43         versionCode, JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
44     if (parseResult != ERR_OK) {
45         APP_LOGE("read InstallExceptionInfo from json error, error code : %{public}d", parseResult);
46         return false;
47     }
48     return true;
49 }
50 
ToString() const51 std::string InstallExceptionInfo::ToString() const
52 {
53     nlohmann::json jsonObject = nlohmann::json {
54         {INSTALL_RENAME_EXCEPTION_STATUS, status},
55         {VERSION_CODE, versionCode},
56     };
57     return jsonObject.dump();
58 }
59 
InstallExceptionMgrRdb()60 InstallExceptionMgrRdb::InstallExceptionMgrRdb()
61 {
62     APP_LOGI("exception mgr created");
63     BmsRdbConfig bmsRdbConfig;
64     bmsRdbConfig.dbName = ServiceConstants::BUNDLE_RDB_NAME;
65     bmsRdbConfig.tableName = INSTALL_EXCEPTION_MGR_TABLE_NAME;
66     rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
67     rdbDataManager_->CreateTable();
68 }
69 
~InstallExceptionMgrRdb()70 InstallExceptionMgrRdb::~InstallExceptionMgrRdb()
71 {
72 }
73 
SaveBundleExceptionInfo(const std::string & bundleName,const InstallExceptionInfo & installExceptionInfo)74 ErrCode InstallExceptionMgrRdb::SaveBundleExceptionInfo(
75     const std::string &bundleName, const InstallExceptionInfo &installExceptionInfo)
76 {
77     if (rdbDataManager_ == nullptr) {
78         APP_LOGE("rdbDataManager is null");
79         return ERR_APPEXECFWK_NULL_PTR;
80     }
81     if (bundleName.empty()) {
82         APP_LOGE("bundleName is empty");
83         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
84     }
85     if (!rdbDataManager_->InsertData(bundleName, installExceptionInfo.ToString())) {
86         APP_LOGE("bundle %{public}s insert %{public}s failed", bundleName.c_str(),
87             installExceptionInfo.ToString().c_str());
88         return ERR_APPEXECFWK_UPDATE_BUNDLE_ERROR;
89     }
90     return ERR_OK;
91 }
92 
DeleteBundleExceptionInfo(const std::string & bundleName)93 ErrCode InstallExceptionMgrRdb::DeleteBundleExceptionInfo(const std::string &bundleName)
94 {
95     if (rdbDataManager_ == nullptr) {
96         APP_LOGE("rdbDataManager is null");
97         return ERR_APPEXECFWK_NULL_PTR;
98     }
99     if (bundleName.empty()) {
100         APP_LOGE("bundleName is empty");
101         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
102     }
103     if (!rdbDataManager_->DeleteData(bundleName)) {
104         APP_LOGE("delete bundle %{public}s failed", bundleName.c_str());
105         return ERR_APPEXECFWK_UPDATE_BUNDLE_ERROR;
106     }
107     return ERR_OK;
108 }
109 
GetAllBundleExceptionInfo(std::map<std::string,InstallExceptionInfo> & bundleExceptionInfos)110 void InstallExceptionMgrRdb::GetAllBundleExceptionInfo(
111     std::map<std::string, InstallExceptionInfo> &bundleExceptionInfos)
112 {
113     if (rdbDataManager_ == nullptr) {
114         APP_LOGE("rdbDataManager is null");
115         return;
116     }
117     std::map<std::string, std::string> datas;
118     if (!rdbDataManager_->QueryAllData(datas) || datas.empty()) {
119         APP_LOGI("installExceptionInfo not exist");
120         return;
121     }
122     for (const auto &item : datas) {
123         InstallExceptionInfo installExceptionInfo;
124         if (installExceptionInfo.FromString(item.second)) {
125             bundleExceptionInfos.emplace(item.first, installExceptionInfo);
126         }
127     }
128 }
129 } // AppExecFwk
130 } // OHOS
131