1 /*
2 * Copyright (c) 2022 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 "quick_fix_manager_rdb.h"
17
18 #include "app_log_tag_wrapper.h"
19 #include "json_serializer.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 constexpr const char* QUICK_FIX_RDB_TABLE_NAME = "quick_fix";
25 }
QuickFixManagerRdb()26 QuickFixManagerRdb::QuickFixManagerRdb()
27 {
28 LOG_I(BMS_TAG_DEFAULT, "create QuickFixManagerRdb");
29 BmsRdbConfig bmsRdbConfig;
30 bmsRdbConfig.dbName = ServiceConstants::BUNDLE_RDB_NAME;
31 bmsRdbConfig.tableName = QUICK_FIX_RDB_TABLE_NAME;
32 rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
33 rdbDataManager_->CreateTable();
34 }
35
~QuickFixManagerRdb()36 QuickFixManagerRdb::~QuickFixManagerRdb()
37 {
38 LOG_I(BMS_TAG_DEFAULT, "destroy QuickFixManagerRdb");
39 }
40
QueryAllInnerAppQuickFix(std::map<std::string,InnerAppQuickFix> & innerAppQuickFixes)41 bool QuickFixManagerRdb::QueryAllInnerAppQuickFix(std::map<std::string, InnerAppQuickFix> &innerAppQuickFixes)
42 {
43 LOG_I(BMS_TAG_DEFAULT, "begin to QueryAllInnerAppQuickFix");
44 bool ret = GetAllDataFromDb(innerAppQuickFixes);
45 if (!ret) {
46 LOG_E(BMS_TAG_DEFAULT, "GetAllDataFromDb failed");
47 return false;
48 }
49 return true;
50 }
51
QueryInnerAppQuickFix(const std::string & bundleName,InnerAppQuickFix & innerAppQuickFix)52 bool QuickFixManagerRdb::QueryInnerAppQuickFix(const std::string &bundleName, InnerAppQuickFix &innerAppQuickFix)
53 {
54 LOG_NOFUNC_I(BMS_TAG_DEFAULT, "begin to QueryAppQuickFix");
55 bool ret = GetDataFromDb(bundleName, innerAppQuickFix);
56 if (!ret) {
57 LOG_E(BMS_TAG_DEFAULT, "GetDataFromDb failed");
58 return false;
59 }
60 return true;
61 }
62
SaveInnerAppQuickFix(const InnerAppQuickFix & innerAppQuickFix)63 bool QuickFixManagerRdb::SaveInnerAppQuickFix(const InnerAppQuickFix &innerAppQuickFix)
64 {
65 LOG_I(BMS_TAG_DEFAULT, "begin to SaveInnerAppQuickFix");
66 bool ret = SaveDataToDb(innerAppQuickFix);
67 if (!ret) {
68 LOG_E(BMS_TAG_DEFAULT, "SaveDataToDb failed");
69 return false;
70 }
71 return true;
72 }
73
DeleteInnerAppQuickFix(const std::string & bundleName)74 bool QuickFixManagerRdb::DeleteInnerAppQuickFix(const std::string &bundleName)
75 {
76 LOG_I(BMS_TAG_DEFAULT, "begin to DeleteInnerAppQuickFix");
77 bool ret = DeleteDataFromDb(bundleName);
78 if (!ret) {
79 LOG_E(BMS_TAG_DEFAULT, "DeleteDataFromDb failed");
80 return false;
81 }
82 return true;
83 }
84
GetAllDataFromDb(std::map<std::string,InnerAppQuickFix> & innerAppQuickFixes)85 bool QuickFixManagerRdb::GetAllDataFromDb(std::map<std::string, InnerAppQuickFix> &innerAppQuickFixes)
86 {
87 if (rdbDataManager_ == nullptr) {
88 LOG_E(BMS_TAG_DEFAULT, "rdbDataManager is null");
89 return false;
90 }
91
92 std::map<std::string, std::string> values;
93 bool result = rdbDataManager_->QueryAllData(values);
94 if (!result) {
95 LOG_E(BMS_TAG_DEFAULT, "QueryAllData failed");
96 return false;
97 }
98 for (auto iter = values.begin(); iter != values.end(); ++iter) {
99 nlohmann::json jsonObject = nlohmann::json::parse(iter->second, nullptr, false);
100 InnerAppQuickFix appQuickFix;
101 if (jsonObject.is_discarded() || (appQuickFix.FromJson(jsonObject) != ERR_OK)) {
102 LOG_E(BMS_TAG_DEFAULT, "error key : %{public}s", iter->first.c_str());
103 rdbDataManager_->DeleteData(iter->first);
104 continue;
105 }
106 innerAppQuickFixes.insert({ iter->first, appQuickFix });
107 }
108 return true;
109 }
110
GetDataFromDb(const std::string & bundleName,InnerAppQuickFix & innerAppQuickFix)111 bool QuickFixManagerRdb::GetDataFromDb(const std::string &bundleName, InnerAppQuickFix &innerAppQuickFix)
112 {
113 if (rdbDataManager_ == nullptr) {
114 LOG_E(BMS_TAG_DEFAULT, "rdbDataManager is null");
115 return false;
116 }
117
118 std::string value;
119 bool result = rdbDataManager_->QueryData(bundleName, value);
120 if (!result) {
121 LOG_E(BMS_TAG_DEFAULT, "QueryData failed by bundleName %{public}s", bundleName.c_str());
122 return false;
123 }
124
125 nlohmann::json jsonObject = nlohmann::json::parse(value, nullptr, false);
126 if (jsonObject.is_discarded() || (innerAppQuickFix.FromJson(jsonObject) != ERR_OK)) {
127 LOG_E(BMS_TAG_DEFAULT, "error key : %{public}s", bundleName.c_str());
128 rdbDataManager_->DeleteData(bundleName);
129 return false;
130 }
131 return true;
132 }
133
SaveDataToDb(const InnerAppQuickFix & innerAppQuickFix)134 bool QuickFixManagerRdb::SaveDataToDb(const InnerAppQuickFix &innerAppQuickFix)
135 {
136 if (rdbDataManager_ == nullptr) {
137 LOG_E(BMS_TAG_DEFAULT, "rdbDataManager is null");
138 return false;
139 }
140
141 return rdbDataManager_->InsertData(innerAppQuickFix.GetAppQuickFix().bundleName, innerAppQuickFix.ToString());
142 }
143
DeleteDataFromDb(const std::string & bundleName)144 bool QuickFixManagerRdb::DeleteDataFromDb(const std::string &bundleName)
145 {
146 if (rdbDataManager_ == nullptr) {
147 LOG_E(BMS_TAG_DEFAULT, "rdbDataManager is null");
148 return false;
149 }
150
151 return rdbDataManager_->DeleteData(bundleName);
152 }
153 } // namespace AppExecFwk
154 } // namespace OHOS
155