1 /*
2 * Copyright (c) 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 "shortcut_data_storage_rdb.h"
17
18 #include "app_log_wrapper.h"
19 #include "json_serializer.h"
20 #include "scope_guard.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
24 namespace {
25 const std::string SHORTCUT_RDB_TABLE_NAME = "shortcut_info";
26 const std::string BUNDLE_NAME = "BUNDLE_NAME";
27 const std::string SHORTCUT_ID = "SHORTCUT_ID";
28 const std::string USER_ID = "USER_ID";
29 const std::string APP_INDEX = "APP_INDEX";
30 const std::string SHORTCUT_INFO = "SHORTCUT_INFO";
31 const int32_t SHORTCUT_INFO_INDEX = 5;
32 }
ShortcutDataStorageRdb()33 ShortcutDataStorageRdb::ShortcutDataStorageRdb()
34 {
35 APP_LOGI("ShortcutDataStorageRdb instance is created");
36 BmsRdbConfig bmsRdbConfig;
37 bmsRdbConfig.dbName = ServiceConstants::BUNDLE_RDB_NAME;
38 bmsRdbConfig.tableName = SHORTCUT_RDB_TABLE_NAME;
39 bmsRdbConfig.createTableSql = std::string(
40 "CREATE TABLE IF NOT EXISTS "
41 + SHORTCUT_RDB_TABLE_NAME
42 + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, BUNDLE_NAME TEXT NOT NULL, "
43 + "SHORTCUT_ID TEXT NOT NULL, USER_ID INTEGER, "
44 + "APP_INDEX INTEGER, SHORTCUT_INFO TEXT NOT NULL);");
45 rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
46 rdbDataManager_->CreateTable();
47 }
48
~ShortcutDataStorageRdb()49 ShortcutDataStorageRdb::~ShortcutDataStorageRdb()
50 {
51 APP_LOGI("ShortcutDataStorageRdb instance is destroyed");
52 }
53
AddDesktopShortcutInfo(const ShortcutInfo & shortcutInfo,int32_t userId,bool & isIdIllegal)54 bool ShortcutDataStorageRdb::AddDesktopShortcutInfo(const ShortcutInfo &shortcutInfo, int32_t userId, bool &isIdIllegal)
55 {
56 if (rdbDataManager_ == nullptr) {
57 APP_LOGE("rdbDataManager is null");
58 return false;
59 }
60 if (!ShortcutIdVerification(shortcutInfo, userId)) {
61 APP_LOGD("ShortcutIdVerification is fail");
62 isIdIllegal = true;
63 return false;
64 }
65 nlohmann::json jsonObject;
66 to_json(jsonObject, shortcutInfo);
67 std::string value = jsonObject.dump();
68 NativeRdb::ValuesBucket valuesBucket;
69 valuesBucket.PutString(BUNDLE_NAME, shortcutInfo.bundleName);
70 valuesBucket.PutString(SHORTCUT_ID, shortcutInfo.id);
71 valuesBucket.PutInt(USER_ID, userId);
72 valuesBucket.PutInt(APP_INDEX, shortcutInfo.appIndex);
73 valuesBucket.PutString(SHORTCUT_INFO, value);
74 bool ret = rdbDataManager_->InsertData(valuesBucket);
75 APP_LOGD("AddDesktopShortcutInfo %{public}d", ret);
76 return ret;
77 }
78
DeleteDesktopShortcutInfo(const ShortcutInfo & shortcutInfo,int32_t userId)79 bool ShortcutDataStorageRdb::DeleteDesktopShortcutInfo(const ShortcutInfo &shortcutInfo, int32_t userId)
80 {
81 if (rdbDataManager_ == nullptr) {
82 APP_LOGE("rdbDataManager is null");
83 return false;
84 }
85 NativeRdb::AbsRdbPredicates absRdbPredicates(SHORTCUT_RDB_TABLE_NAME);
86 absRdbPredicates.EqualTo(BUNDLE_NAME, shortcutInfo.bundleName);
87 absRdbPredicates.EqualTo(SHORTCUT_ID, shortcutInfo.id);
88 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
89 absRdbPredicates.EqualTo(APP_INDEX, std::to_string(shortcutInfo.appIndex));
90 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
91 APP_LOGD("DeleteDesktopShortcutInfo %{public}d", ret);
92 return ret;
93 }
94
GetAllDesktopShortcutInfo(int32_t userId,std::vector<ShortcutInfo> & shortcutInfos)95 void ShortcutDataStorageRdb::GetAllDesktopShortcutInfo(int32_t userId, std::vector<ShortcutInfo> &shortcutInfos)
96 {
97 if (rdbDataManager_ == nullptr) {
98 APP_LOGE("rdbDataManager is null");
99 return;
100 }
101 NativeRdb::AbsRdbPredicates absRdbPredicates(SHORTCUT_RDB_TABLE_NAME);
102 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
103 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
104 if (absSharedResultSet == nullptr) {
105 APP_LOGE("absSharedResultSet is null.");
106 return;
107 }
108 ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
109 auto ret = absSharedResultSet->GoToFirstRow();
110 if (ret != NativeRdb::E_OK) {
111 APP_LOGE("GoToFirstRow failed, ret: %{public}d", ret);
112 return;
113 }
114 do {
115 std::string value;
116 ret = absSharedResultSet->GetString(SHORTCUT_INFO_INDEX, value);
117 if (ret != NativeRdb::E_OK) {
118 APP_LOGE("GetString shortcutInfo failed, ret: %{public}d", ret);
119 return;
120 }
121 nlohmann::json jsonObject = nlohmann::json::parse(value, nullptr, false);
122 if (jsonObject.is_discarded()) {
123 APP_LOGE("Shortcut jsonObject is discarded");
124 return;
125 }
126 ShortcutInfo shortcutInfo;
127 from_json(jsonObject, shortcutInfo);
128 shortcutInfos.emplace_back(shortcutInfo);
129 } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
130 if (userId != 0) {
131 GetDesktopShortcutInfosByDefaultUserId(shortcutInfos);
132 }
133 }
134
DeleteDesktopShortcutInfo(const std::string & bundleName)135 bool ShortcutDataStorageRdb::DeleteDesktopShortcutInfo(const std::string &bundleName)
136 {
137 if (rdbDataManager_ == nullptr) {
138 APP_LOGE("rdbDataManager is null");
139 return false;
140 }
141 NativeRdb::AbsRdbPredicates absRdbPredicates(SHORTCUT_RDB_TABLE_NAME);
142 absRdbPredicates.EqualTo(BUNDLE_NAME, bundleName);
143 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
144 APP_LOGD("DeleteDesktopShortcutInfo by bundleName %{public}d", ret);
145 return ret;
146 }
147
DeleteDesktopShortcutInfo(const std::string & bundleName,int32_t userId,int32_t appIndex)148 bool ShortcutDataStorageRdb::DeleteDesktopShortcutInfo(const std::string &bundleName, int32_t userId, int32_t appIndex)
149 {
150 if (rdbDataManager_ == nullptr) {
151 APP_LOGE("rdbDataManager is null");
152 return false;
153 }
154 NativeRdb::AbsRdbPredicates absRdbPredicates(SHORTCUT_RDB_TABLE_NAME);
155 absRdbPredicates.EqualTo(BUNDLE_NAME, bundleName);
156 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
157 absRdbPredicates.EqualTo(APP_INDEX, std::to_string(appIndex));
158 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
159 APP_LOGD("DeleteDesktopShortcutInfo by remove cloneApp %{public}d", ret);
160 return ret;
161 }
162
ShortcutIdVerification(const ShortcutInfo & shortcutInfo,int32_t userId)163 bool ShortcutDataStorageRdb::ShortcutIdVerification(const ShortcutInfo &shortcutInfo, int32_t userId)
164 {
165 if (shortcutInfo.id.empty()) {
166 APP_LOGD("ShortcutInfo id is empty");
167 return false;
168 }
169 NativeRdb::AbsRdbPredicates absRdbPredicates(SHORTCUT_RDB_TABLE_NAME);
170 absRdbPredicates.EqualTo(BUNDLE_NAME, shortcutInfo.bundleName);
171 absRdbPredicates.EqualTo(SHORTCUT_ID, shortcutInfo.id);
172 absRdbPredicates.EqualTo(APP_INDEX, shortcutInfo.appIndex);
173 absRdbPredicates.EqualTo(USER_ID, userId);
174 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
175 if (absSharedResultSet == nullptr) {
176 APP_LOGE("absSharedResultSet is null");
177 return true;
178 }
179 ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
180 int32_t count = 0;
181 auto ret = absSharedResultSet->GetRowCount(count);
182 if (ret != NativeRdb::E_OK) {
183 APP_LOGE("GetRowCount failed, ret: %{public}d", ret);
184 return true;
185 }
186 if (count == 0) {
187 APP_LOGD("GetRowCount count is 0");
188 return true;
189 }
190 return false;
191 }
192
GetDesktopShortcutInfosByDefaultUserId(std::vector<ShortcutInfo> & shortcutInfos)193 void ShortcutDataStorageRdb::GetDesktopShortcutInfosByDefaultUserId(std::vector<ShortcutInfo> &shortcutInfos)
194 {
195 if (rdbDataManager_ == nullptr) {
196 APP_LOGE("rdbDataManager is null");
197 return;
198 }
199 NativeRdb::AbsRdbPredicates absRdbPredicates(SHORTCUT_RDB_TABLE_NAME);
200 int32_t userId = 0;
201 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
202 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
203 if (absSharedResultSet == nullptr) {
204 APP_LOGE("absSharedResultSet is null.");
205 return;
206 }
207 ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); });
208 auto ret = absSharedResultSet->GoToFirstRow();
209 if (ret != NativeRdb::E_OK) {
210 APP_LOGE("GoToFirstRow failed, ret: %{public}d", ret);
211 return;
212 }
213 do {
214 std::string value;
215 ret = absSharedResultSet->GetString(SHORTCUT_INFO_INDEX, value);
216 if (ret != NativeRdb::E_OK) {
217 APP_LOGE("GetString shortcutInfo failed, ret: %{public}d", ret);
218 return;
219 }
220 nlohmann::json jsonObject = nlohmann::json::parse(value, nullptr, false);
221 if (jsonObject.is_discarded()) {
222 APP_LOGE("Shortcut jsonObject is discarded");
223 return;
224 }
225 ShortcutInfo shortcutInfo;
226 from_json(jsonObject, shortcutInfo);
227 shortcutInfos.emplace_back(shortcutInfo);
228 } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
229 }
230 } // namespace AppExecFwk
231 } // namespace OHOS