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 "app_control_manager_rdb.h"
17
18 #include "app_control_constants.h"
19 #include "app_log_tag_wrapper.h"
20 #include "bms_extension_client.h"
21 #include "bundle_util.h"
22 #include "event_report.h"
23 #include "hitrace_meter.h"
24 #include "scope_guard.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 const std::string APP_CONTROL_RDB_TABLE_NAME = "app_control";
30 const std::string RUNNING_CONTROL = "RunningControl";
31 const std::string DISPOSED_RULE = "DisposedRule";
32 constexpr const char* UNINSTALL_DISPOSED_RULE = "UninstallDisposedRule";
33 const std::string APP_CONTROL_EDM_DEFAULT_MESSAGE = "The app has been disabled by EDM";
34 const std::string DEFAULT = "default";
35 const int32_t CALLING_NAME_INDEX = 1;
36 const int32_t APP_ID_INDEX = 4;
37 const int32_t CONTROL_MESSAGE_INDEX = 5;
38 const int32_t DISPOSED_STATUS_INDEX = 6;
39 constexpr int8_t TIME_STAMP_INDEX = 8;
40 // app control table key
41 const std::string CALLING_NAME = "CALLING_NAME";
42 const std::string APP_CONTROL_LIST = "APP_CONTROL_LIST";
43 const std::string USER_ID = "USER_ID";
44 const std::string APP_ID = "APP_ID";
45 const std::string CONTROL_MESSAGE = "CONTROL_MESSAGE";
46 const std::string DISPOSED_STATUS = "DISPOSED_STATUS";
47 const std::string PRIORITY = "PRIORITY";
48 const std::string TIME_STAMP = "TIME_STAMP";
49 const std::string APP_INDEX = "APP_INDEX";
50
51 enum class PRIORITY {
52 EDM = 100,
53 APP_MARKET = 200,
54 };
55 }
AppControlManagerRdb()56 AppControlManagerRdb::AppControlManagerRdb()
57 {
58 LOG_D(BMS_TAG_DEFAULT, "create AppControlManagerRdb");
59 BmsRdbConfig bmsRdbConfig;
60 bmsRdbConfig.dbName = ServiceConstants::BUNDLE_RDB_NAME;
61 bmsRdbConfig.tableName = APP_CONTROL_RDB_TABLE_NAME;
62 bmsRdbConfig.createTableSql = std::string(
63 "CREATE TABLE IF NOT EXISTS "
64 + APP_CONTROL_RDB_TABLE_NAME
65 + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, CALLING_NAME TEXT NOT NULL, "
66 + "APP_CONTROL_LIST TEXT, USER_ID INTEGER, APP_ID TEXT, CONTROL_MESSAGE TEXT, "
67 + "DISPOSED_STATUS TEXT, PRIORITY INTEGER, TIME_STAMP INTEGER);");
68 bmsRdbConfig.insertColumnSql.push_back(std::string("ALTER TABLE " + APP_CONTROL_RDB_TABLE_NAME +
69 " ADD APP_INDEX INTEGER DEFAULT 0;"));
70 rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
71 rdbDataManager_->CreateTable();
72 }
73
~AppControlManagerRdb()74 AppControlManagerRdb::~AppControlManagerRdb()
75 {
76 LOG_D(BMS_TAG_DEFAULT, "destroy AppControlManagerRdb");
77 }
78
AddAppInstallControlRule(const std::string & callingName,const std::vector<std::string> & appIds,const std::string & controlRuleType,int32_t userId)79 ErrCode AppControlManagerRdb::AddAppInstallControlRule(const std::string &callingName,
80 const std::vector<std::string> &appIds, const std::string &controlRuleType, int32_t userId)
81 {
82 int64_t timeStamp = BundleUtil::GetCurrentTime();
83 std::vector<NativeRdb::ValuesBucket> valuesBuckets;
84 for (auto appId : appIds) {
85 ErrCode result = DeleteOldControlRule(callingName, controlRuleType, appId, userId);
86 if (result != ERR_OK) {
87 LOG_E(BMS_TAG_DEFAULT, "DeleteOldControlRule failed");
88 return result;
89 }
90 NativeRdb::ValuesBucket valuesBucket;
91 valuesBucket.PutString(CALLING_NAME, callingName);
92 valuesBucket.PutString(APP_CONTROL_LIST, controlRuleType);
93 valuesBucket.PutInt(USER_ID, static_cast<int>(userId));
94 valuesBucket.PutString(APP_ID, appId);
95 valuesBucket.PutInt(TIME_STAMP, timeStamp);
96 valuesBuckets.emplace_back(valuesBucket);
97 }
98 int64_t insertNum = 0;
99 bool ret = rdbDataManager_->BatchInsert(insertNum, valuesBuckets);
100 if (!ret) {
101 LOG_E(BMS_TAG_DEFAULT, "BatchInsert failed");
102 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
103 }
104 EventInfo info;
105 info.callingName = callingName;
106 info.userId = userId;
107 info.appIds = appIds;
108 info.rule = controlRuleType;
109 info.operationType = static_cast<int32_t>(OPERATION_TYPE_ENUM::OPERATION_TYPE_ADD_RULE);
110 info.actionType = static_cast<int32_t>(ACTION_TYPE_ENUM::ACTION_TYPE_OF_INSTALL);
111 EventReport::SendAppControlRuleEvent(info);
112 if (valuesBuckets.size() != static_cast<uint64_t>(insertNum)) {
113 LOG_E(BMS_TAG_DEFAULT, "BatchInsert size not expected");
114 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
115 }
116 return ERR_OK;
117 }
118
DeleteAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,const std::vector<std::string> & appIds,int32_t userId)119 ErrCode AppControlManagerRdb::DeleteAppInstallControlRule(const std::string &callingName,
120 const std::string &controlRuleType, const std::vector<std::string> &appIds, int32_t userId)
121 {
122 for (const auto &appId : appIds) {
123 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
124 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
125 absRdbPredicates.EqualTo(APP_CONTROL_LIST, controlRuleType);
126 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
127 absRdbPredicates.EqualTo(APP_ID, appId);
128 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
129 if (!ret) {
130 LOG_E(BMS_TAG_DEFAULT, "Delete failed callingName:%{public}s appId:%{private}s userId:%{public}d",
131 callingName.c_str(), appId.c_str(), userId);
132 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
133 }
134 EventInfo info;
135 info.callingName = callingName;
136 info.userId = userId;
137 info.appIds.push_back(appId);
138 info.rule = controlRuleType;
139 info.operationType = static_cast<int32_t>(OPERATION_TYPE_ENUM::OPERATION_TYPE_REMOVE_RULE);
140 info.actionType = static_cast<int32_t>(ACTION_TYPE_ENUM::ACTION_TYPE_OF_INSTALL);
141 EventReport::SendAppControlRuleEvent(info);
142 }
143 return ERR_OK;
144 }
145
DeleteAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,int32_t userId)146 ErrCode AppControlManagerRdb::DeleteAppInstallControlRule(const std::string &callingName,
147 const std::string &controlRuleType, int32_t userId)
148 {
149 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
150 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
151 absRdbPredicates.EqualTo(APP_CONTROL_LIST, controlRuleType);
152 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
153 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
154 if (!ret) {
155 LOG_E(BMS_TAG_DEFAULT, "DeleteData callingName:%{public}s controlRuleType:%{public}s failed",
156 callingName.c_str(), controlRuleType.c_str());
157 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
158 }
159 EventInfo info;
160 info.callingName = callingName;
161 info.userId = userId;
162 info.rule = controlRuleType;
163 info.operationType = static_cast<int32_t>(OPERATION_TYPE_ENUM::OPERATION_TYPE_REMOVE_RULE);
164 info.actionType = static_cast<int32_t>(ACTION_TYPE_ENUM::ACTION_TYPE_OF_INSTALL);
165 EventReport::SendAppControlRuleEvent(info);
166 return ERR_OK;
167 }
168
GetAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,int32_t userId,std::vector<std::string> & appIds)169 ErrCode AppControlManagerRdb::GetAppInstallControlRule(const std::string &callingName,
170 const std::string &controlRuleType, int32_t userId, std::vector<std::string> &appIds)
171 {
172 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
173 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
174 absRdbPredicates.EqualTo(APP_CONTROL_LIST, controlRuleType);
175 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
176 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
177 if (absSharedResultSet == nullptr) {
178 LOG_E(BMS_TAG_DEFAULT, "GetAppInstallControlRule failed");
179 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
180 }
181 ScopeGuard stateGuard([&] { absSharedResultSet->Close(); });
182 int32_t count;
183 int ret = absSharedResultSet->GetRowCount(count);
184 if (ret != NativeRdb::E_OK) {
185 LOG_E(BMS_TAG_DEFAULT, "GetRowCount failed, ret: %{public}d", ret);
186 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
187 }
188 if (count == 0) {
189 LOG_D(BMS_TAG_DEFAULT, "GetAppInstallControlRule size 0");
190 return ERR_OK;
191 }
192
193 ret = absSharedResultSet->GoToFirstRow();
194 if (ret != NativeRdb::E_OK) {
195 LOG_E(BMS_TAG_DEFAULT, "GoToFirstRow failed, ret: %{public}d", ret);
196 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
197 }
198 do {
199 std::string appId;
200 ret = absSharedResultSet->GetString(APP_ID_INDEX, appId);
201 if (ret != NativeRdb::E_OK) {
202 LOG_E(BMS_TAG_DEFAULT, "GetString appId failed, ret: %{public}d", ret);
203 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
204 }
205 appIds.push_back(appId);
206 } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
207 return ERR_OK;
208 }
209
AddAppRunningControlRule(const std::string & callingName,const std::vector<AppRunningControlRule> & controlRules,int32_t userId)210 ErrCode AppControlManagerRdb::AddAppRunningControlRule(const std::string &callingName,
211 const std::vector<AppRunningControlRule> &controlRules, int32_t userId)
212 {
213 int64_t timeStamp = BundleUtil::GetCurrentTime();
214 std::vector<NativeRdb::ValuesBucket> valuesBuckets;
215 for (auto &controlRule : controlRules) {
216 ErrCode result = DeleteOldControlRule(callingName, RUNNING_CONTROL, controlRule.appId, userId);
217 if (result != ERR_OK) {
218 LOG_E(BMS_TAG_DEFAULT, "DeleteOldControlRule failed");
219 return result;
220 }
221 NativeRdb::ValuesBucket valuesBucket;
222 valuesBucket.PutString(CALLING_NAME, callingName);
223 valuesBucket.PutString(APP_CONTROL_LIST, RUNNING_CONTROL);
224 valuesBucket.PutInt(USER_ID, static_cast<int>(userId));
225 valuesBucket.PutString(APP_ID, controlRule.appId);
226 valuesBucket.PutString(CONTROL_MESSAGE, controlRule.controlMessage);
227 valuesBucket.PutInt(PRIORITY, static_cast<int>(PRIORITY::EDM));
228 valuesBucket.PutInt(TIME_STAMP, timeStamp);
229 valuesBuckets.emplace_back(valuesBucket);
230 }
231 int64_t insertNum = 0;
232 bool ret = rdbDataManager_->BatchInsert(insertNum, valuesBuckets);
233 if (!ret) {
234 LOG_E(BMS_TAG_DEFAULT, "BatchInsert AddAppRunningControlRule failed");
235 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
236 }
237 for (auto &controlRule : controlRules) {
238 EventInfo info;
239 info.callingName = callingName;
240 info.userId = userId;
241 info.rule = controlRule.controlMessage;
242 info.appIds.push_back(controlRule.appId);
243 info.operationType = static_cast<int32_t>(OPERATION_TYPE_ENUM::OPERATION_TYPE_ADD_RULE);
244 info.actionType = static_cast<int32_t>(ACTION_TYPE_ENUM::ACTION_TYPE_OF_RUNUING);
245 EventReport::SendAppControlRuleEvent(info);
246 }
247 if (valuesBuckets.size() != static_cast<uint64_t>(insertNum)) {
248 LOG_E(BMS_TAG_DEFAULT, "BatchInsert size not expected");
249 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
250 }
251 return ERR_OK;
252 }
253
DeleteAppRunningControlRule(const std::string & callingName,const std::vector<AppRunningControlRule> & controlRules,int32_t userId)254 ErrCode AppControlManagerRdb::DeleteAppRunningControlRule(const std::string &callingName,
255 const std::vector<AppRunningControlRule> &controlRules, int32_t userId)
256 {
257 for (auto &rule : controlRules) {
258 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
259 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
260 absRdbPredicates.EqualTo(APP_CONTROL_LIST, RUNNING_CONTROL);
261 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
262 absRdbPredicates.EqualTo(APP_ID, rule.appId);
263 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
264 if (!ret) {
265 LOG_E(BMS_TAG_DEFAULT, "Delete failed callingName:%{public}s appid:%{private}s userId:%{public}d",
266 callingName.c_str(), rule.appId.c_str(), userId);
267 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
268 }
269 EventInfo info;
270 info.callingName = callingName;
271 info.userId = userId;
272 info.appIds.push_back(rule.appId);
273 info.operationType = static_cast<int32_t>(OPERATION_TYPE_ENUM::OPERATION_TYPE_REMOVE_RULE);
274 info.actionType = static_cast<int32_t>(ACTION_TYPE_ENUM::ACTION_TYPE_OF_RUNUING);
275 EventReport::SendAppControlRuleEvent(info);
276 }
277 return ERR_OK;
278 }
DeleteAppRunningControlRule(const std::string & callingName,int32_t userId)279 ErrCode AppControlManagerRdb::DeleteAppRunningControlRule(const std::string &callingName, int32_t userId)
280 {
281 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
282 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
283 absRdbPredicates.EqualTo(APP_CONTROL_LIST, RUNNING_CONTROL);
284 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
285 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
286 if (!ret) {
287 LOG_E(BMS_TAG_DEFAULT, "DeleteAppRunningControlRule callingName:%{public}s userId:%{public}d failed",
288 callingName.c_str(), userId);
289 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
290 }
291 EventInfo info;
292 info.callingName = callingName;
293 info.userId = userId;
294 info.operationType = static_cast<int32_t>(OPERATION_TYPE_ENUM::OPERATION_TYPE_REMOVE_RULE);
295 info.actionType = static_cast<int32_t>(ACTION_TYPE_ENUM::ACTION_TYPE_OF_RUNUING);
296 EventReport::SendAppControlRuleEvent(info);
297 return ERR_OK;
298 }
299
GetAppRunningControlRule(const std::string & callingName,int32_t userId,std::vector<std::string> & appIds)300 ErrCode AppControlManagerRdb::GetAppRunningControlRule(const std::string &callingName,
301 int32_t userId, std::vector<std::string> &appIds)
302 {
303 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
304 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
305 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
306 absRdbPredicates.EqualTo(APP_CONTROL_LIST, RUNNING_CONTROL);
307 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
308 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
309 if (absSharedResultSet == nullptr) {
310 LOG_E(BMS_TAG_DEFAULT, "QueryData failed");
311 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
312 }
313 ScopeGuard stateGuard([&] { absSharedResultSet->Close(); });
314 int32_t count;
315 int ret = absSharedResultSet->GetRowCount(count);
316 if (ret != NativeRdb::E_OK) {
317 LOG_E(BMS_TAG_DEFAULT, "GetRowCount failed, ret: %{public}d", ret);
318 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
319 }
320 if (count == 0) {
321 LOG_D(BMS_TAG_DEFAULT, "GetAppRunningControlRule size 0");
322 return ERR_OK;
323 }
324 ret = absSharedResultSet->GoToFirstRow();
325 if (ret != NativeRdb::E_OK) {
326 LOG_E(BMS_TAG_DEFAULT, "GoToFirstRow failed, ret: %{public}d", ret);
327 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
328 }
329 do {
330 std::string appId;
331 ret = absSharedResultSet->GetString(APP_ID_INDEX, appId);
332 if (ret != NativeRdb::E_OK) {
333 LOG_E(BMS_TAG_DEFAULT, "GetString appId failed, ret: %{public}d", ret);
334 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
335 }
336 appIds.push_back(appId);
337 } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
338 return ERR_OK;
339 }
340
GetAppRunningControlRule(const std::string & appId,int32_t userId,AppRunningControlRuleResult & controlRuleResult)341 ErrCode AppControlManagerRdb::GetAppRunningControlRule(const std::string &appId,
342 int32_t userId, AppRunningControlRuleResult &controlRuleResult)
343 {
344 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
345 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
346 absRdbPredicates.EqualTo(APP_ID, appId);
347 absRdbPredicates.EqualTo(APP_CONTROL_LIST, RUNNING_CONTROL);
348 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
349 absRdbPredicates.OrderByAsc(PRIORITY); // ascending
350 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
351 if (absSharedResultSet == nullptr) {
352 LOG_E(BMS_TAG_DEFAULT, "QueryData failed");
353 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
354 }
355 ScopeGuard stateGuard([&] { absSharedResultSet->Close(); });
356 int32_t count;
357 int ret = absSharedResultSet->GetRowCount(count);
358 if (ret != NativeRdb::E_OK) {
359 LOG_E(BMS_TAG_DEFAULT, "GetRowCount failed, ret: %{public}d", ret);
360 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
361 }
362 if (count == 0) {
363 LOG_NOFUNC_W(BMS_TAG_DEFAULT, "control rule invalid size 0");
364 return ERR_BUNDLE_MANAGER_BUNDLE_NOT_SET_CONTROL;
365 }
366 ret = absSharedResultSet->GoToFirstRow();
367 if (ret != NativeRdb::E_OK) {
368 LOG_E(BMS_TAG_DEFAULT, "GoToFirstRow failed, ret: %{public}d", ret);
369 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
370 }
371 std::string callingName;
372 if (absSharedResultSet->GetString(CALLING_NAME_INDEX, callingName) != NativeRdb::E_OK) {
373 LOG_E(BMS_TAG_DEFAULT, "GetString callingName failed, ret: %{public}d", ret);
374 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
375 }
376 ret = absSharedResultSet->GetString(CONTROL_MESSAGE_INDEX, controlRuleResult.controlMessage);
377 if (ret != NativeRdb::E_OK) {
378 LOG_W(BMS_TAG_DEFAULT, "GetString controlMessage failed, ret: %{public}d", ret);
379 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
380 }
381 if (controlRuleResult.controlMessage.empty() && callingName == AppControlConstants::EDM_CALLING) {
382 LOG_D(BMS_TAG_DEFAULT, "GetString controlMessage default");
383 controlRuleResult.controlMessage = APP_CONTROL_EDM_DEFAULT_MESSAGE;
384 }
385 std::string wantString;
386 if (absSharedResultSet->GetString(DISPOSED_STATUS_INDEX, wantString) != NativeRdb::E_OK) {
387 LOG_E(BMS_TAG_DEFAULT, "GetString controlWant failed, ret: %{public}d", ret);
388 }
389 if (!wantString.empty()) {
390 controlRuleResult.controlWant = std::make_shared<Want>(*Want::FromString(wantString));
391 }
392 if (callingName == AppControlConstants::EDM_CALLING) {
393 controlRuleResult.isEdm = true;
394 }
395 return ERR_OK;
396 }
397
SetDisposedStatus(const std::string & callingName,const std::string & appId,const Want & want,int32_t userId)398 ErrCode AppControlManagerRdb::SetDisposedStatus(const std::string &callingName,
399 const std::string &appId, const Want &want, int32_t userId)
400 {
401 LOG_D(BMS_TAG_DEFAULT, "rdb begin to SetDisposedStatus");
402 ErrCode code = DeleteDisposedStatus(callingName, appId, userId);
403 if (code != ERR_OK) {
404 LOG_E(BMS_TAG_DEFAULT, "DeleteDisposedStatus failed");
405 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
406 }
407 int64_t timeStamp = BundleUtil::GetCurrentTime();
408 NativeRdb::ValuesBucket valuesBucket;
409 valuesBucket.PutString(CALLING_NAME, callingName);
410 valuesBucket.PutString(APP_CONTROL_LIST, RUNNING_CONTROL);
411 valuesBucket.PutString(APP_ID, appId);
412 valuesBucket.PutString(DISPOSED_STATUS, want.ToString());
413 valuesBucket.PutInt(PRIORITY, static_cast<int>(PRIORITY::APP_MARKET));
414 valuesBucket.PutInt(TIME_STAMP, timeStamp);
415 valuesBucket.PutString(USER_ID, std::to_string(userId));
416 bool ret = rdbDataManager_->InsertData(valuesBucket);
417 if (!ret) {
418 LOG_E(BMS_TAG_DEFAULT, "SetDisposedStatus callingName:%{public}s appId:%{private}s failed",
419 callingName.c_str(), appId.c_str());
420 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
421 }
422 EventInfo info;
423 info.callingName = callingName;
424 info.userId = userId;
425 info.appIds.push_back(appId);
426 info.rule = want.ToString();
427 info.operationType = static_cast<int32_t>(OPERATION_TYPE_ENUM::OPERATION_TYPE_ADD_RULE);
428 info.actionType = static_cast<int32_t>(ACTION_TYPE_ENUM::ACTION_TYPE_DISPOSE_STATUS);
429 EventReport::SendAppControlRuleEvent(info);
430 return ERR_OK;
431 }
432
DeleteDisposedStatus(const std::string & callingName,const std::string & appId,int32_t userId)433 ErrCode AppControlManagerRdb::DeleteDisposedStatus(const std::string &callingName,
434 const std::string &appId, int32_t userId)
435 {
436 LOG_D(BMS_TAG_DEFAULT, "rdb begin to DeleteDisposedStatus");
437 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
438 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
439 absRdbPredicates.EqualTo(APP_CONTROL_LIST, RUNNING_CONTROL);
440 absRdbPredicates.EqualTo(APP_ID, appId);
441 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
442 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
443 if (!ret) {
444 LOG_E(BMS_TAG_DEFAULT, "DeleteDisposedStatus callingName:%{public}s appId:%{private}s failed",
445 callingName.c_str(), appId.c_str());
446 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
447 }
448 EventInfo info;
449 info.callingName = callingName;
450 info.userId = userId;
451 info.appIds.push_back(appId);
452 info.operationType = static_cast<int32_t>(OPERATION_TYPE_ENUM::OPERATION_TYPE_REMOVE_RULE);
453 info.actionType = static_cast<int32_t>(ACTION_TYPE_ENUM::ACTION_TYPE_DISPOSE_STATUS);
454 EventReport::SendAppControlRuleEvent(info);
455 return ERR_OK;
456 }
457
GetDisposedStatus(const std::string & callingName,const std::string & appId,Want & want,int32_t userId)458 ErrCode AppControlManagerRdb::GetDisposedStatus(const std::string &callingName,
459 const std::string &appId, Want &want, int32_t userId)
460 {
461 LOG_D(BMS_TAG_DEFAULT, "rdb begin to GetDisposedStatus");
462 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
463 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
464 absRdbPredicates.EqualTo(APP_CONTROL_LIST, RUNNING_CONTROL);
465 absRdbPredicates.EqualTo(APP_ID, appId);
466 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
467 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
468 if (absSharedResultSet == nullptr) {
469 LOG_E(BMS_TAG_DEFAULT, "GetAppInstallControlRule failed");
470 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
471 }
472 ScopeGuard stateGuard([&] { absSharedResultSet->Close(); });
473 int32_t count;
474 int ret = absSharedResultSet->GetRowCount(count);
475 if (ret != NativeRdb::E_OK) {
476 LOG_E(BMS_TAG_DEFAULT, "GetRowCount failed, ret: %{public}d", ret);
477 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
478 }
479 if (count == 0) {
480 LOG_D(BMS_TAG_DEFAULT, "GetAppRunningControlRule size 0");
481 return ERR_OK;
482 }
483 ret = absSharedResultSet->GoToFirstRow();
484 if (ret != NativeRdb::E_OK) {
485 LOG_E(BMS_TAG_DEFAULT, "GoToFirstRow failed, ret: %{public}d", ret);
486 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
487 }
488 std::string wantString;
489 ret = absSharedResultSet->GetString(DISPOSED_STATUS_INDEX, wantString);
490 if (ret != NativeRdb::E_OK) {
491 LOG_E(BMS_TAG_DEFAULT, "GetString DisposedStatus failed, ret: %{public}d", ret);
492 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
493 }
494 want = *Want::FromString(wantString);
495 return ERR_OK;
496 }
497
DeleteOldControlRule(const std::string & callingName,const std::string & controlRuleType,const std::string & appId,int32_t userId)498 ErrCode AppControlManagerRdb::DeleteOldControlRule(const std::string &callingName, const std::string &controlRuleType,
499 const std::string &appId, int32_t userId)
500 {
501 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
502 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
503 absRdbPredicates.EqualTo(APP_CONTROL_LIST, controlRuleType);
504 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
505 absRdbPredicates.EqualTo(APP_ID, appId);
506 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
507 if (!ret) {
508 LOG_E(BMS_TAG_DEFAULT, "DeleteOldControlRule %{public}s, %{public}s, %{public}s, %{public}d failed",
509 callingName.c_str(), appId.c_str(), controlRuleType.c_str(), userId);
510 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
511 }
512 EventInfo info;
513 info.callingName = callingName;
514 info.userId = userId;
515 info.appIds.push_back(appId);
516 info.rule = controlRuleType;
517 info.operationType = static_cast<int32_t>(OPERATION_TYPE_ENUM::OPERATION_TYPE_REMOVE_RULE);
518 EventReport::SendAppControlRuleEvent(info);
519 return ERR_OK;
520 }
521
SetDisposedRule(const std::string & callingName,const std::string & appId,const DisposedRule & rule,int32_t appIndex,int32_t userId)522 ErrCode AppControlManagerRdb::SetDisposedRule(const std::string &callingName,
523 const std::string &appId, const DisposedRule &rule, int32_t appIndex, int32_t userId)
524 {
525 ErrCode code = DeleteDisposedRule(callingName, appId, appIndex, userId);
526 if (code != ERR_OK) {
527 LOG_E(BMS_TAG_DEFAULT, "DeleteDisposedStatus failed.");
528 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
529 }
530 int64_t timeStamp = BundleUtil::GetCurrentTime();
531 NativeRdb::ValuesBucket valuesBucket;
532 valuesBucket.PutString(CALLING_NAME, callingName);
533 valuesBucket.PutString(APP_CONTROL_LIST, DISPOSED_RULE);
534 valuesBucket.PutString(APP_ID, appId);
535 valuesBucket.PutString(DISPOSED_STATUS, rule.ToString());
536 valuesBucket.PutInt(PRIORITY, rule.priority);
537 valuesBucket.PutInt(TIME_STAMP, timeStamp);
538 valuesBucket.PutString(USER_ID, std::to_string(userId));
539 valuesBucket.PutString(APP_INDEX, std::to_string(appIndex));
540 bool ret = rdbDataManager_->InsertData(valuesBucket);
541 if (!ret) {
542 LOG_E(BMS_TAG_DEFAULT, "SetDisposedStatus callingName:%{public}s appId:%{private}s failed.",
543 callingName.c_str(), appId.c_str());
544 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
545 }
546 EventInfo info;
547 info.callingName = callingName;
548 info.userId = userId;
549 info.appIds.push_back(appId);
550 info.rule = rule.ToString();
551 info.operationType = static_cast<int32_t>(OPERATION_TYPE_ENUM::OPERATION_TYPE_ADD_RULE);
552 info.actionType = static_cast<int32_t>(ACTION_TYPE_ENUM::ACTION_TYPE_DISPOSE_RULE);
553 info.appIndex = appIndex;
554 EventReport::SendAppControlRuleEvent(info);
555 return ERR_OK;
556 }
557
DeleteDisposedRule(const std::string & callingName,const std::string & appId,int32_t appIndex,int32_t userId)558 ErrCode AppControlManagerRdb::DeleteDisposedRule(const std::string &callingName,
559 const std::string &appId, int32_t appIndex, int32_t userId)
560 {
561 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
562 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
563 absRdbPredicates.EqualTo(APP_CONTROL_LIST, DISPOSED_RULE);
564 absRdbPredicates.EqualTo(APP_ID, appId);
565 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
566 absRdbPredicates.EqualTo(APP_INDEX, std::to_string(appIndex));
567 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
568 if (!ret) {
569 LOG_E(BMS_TAG_DEFAULT, "DeleteDisposedStatus callingName:%{public}s appId:%{private}s failed",
570 callingName.c_str(), appId.c_str());
571 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
572 }
573 EventInfo info;
574 info.callingName = callingName;
575 info.userId = userId;
576 info.appIds.push_back(appId);
577 info.operationType = static_cast<int32_t>(OPERATION_TYPE_ENUM::OPERATION_TYPE_REMOVE_RULE);
578 info.actionType = static_cast<int32_t>(ACTION_TYPE_ENUM::ACTION_TYPE_DISPOSE_RULE);
579 info.appIndex = appIndex;
580 EventReport::SendAppControlRuleEvent(info);
581 return ERR_OK;
582 }
583
DeleteAllDisposedRuleByBundle(const std::string & appId,int32_t appIndex,int32_t userId)584 ErrCode AppControlManagerRdb::DeleteAllDisposedRuleByBundle(const std::string &appId, int32_t appIndex, int32_t userId)
585 {
586 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
587 std::vector<std::string> controlList = {DISPOSED_RULE, RUNNING_CONTROL, UNINSTALL_DISPOSED_RULE};
588 absRdbPredicates.In(APP_CONTROL_LIST, controlList);
589 absRdbPredicates.EqualTo(APP_ID, appId);
590 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
591 // if appIndex is main app also clear all clone app
592 if (appIndex != Constants::MAIN_APP_INDEX) {
593 absRdbPredicates.EqualTo(APP_INDEX, std::to_string(appIndex));
594 }
595 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
596 if (!ret) {
597 LOG_E(BMS_TAG_DEFAULT, "DeleteAllDisposedRuleByBundle appId:%{private}s failed", appId.c_str());
598 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
599 }
600 EventInfo info;
601 info.userId = userId;
602 info.appIds.push_back(appId);
603 info.operationType = static_cast<int32_t>(OPERATION_TYPE_ENUM::OPERATION_TYPE_REMOVE_RULE);
604 info.actionType = static_cast<int32_t>(ACTION_TYPE_ENUM::ACTION_TYPE_DISPOSE_RULE);
605 info.appIndex = appIndex;
606 EventReport::SendAppControlRuleEvent(info);
607 return ERR_OK;
608 }
609
OptimizeDisposedPredicates(const std::string & callingName,const std::string & appId,int32_t userId,int32_t appIndex,NativeRdb::AbsRdbPredicates & absRdbPredicates)610 ErrCode AppControlManagerRdb::OptimizeDisposedPredicates(const std::string &callingName, const std::string &appId,
611 int32_t userId, int32_t appIndex, NativeRdb::AbsRdbPredicates &absRdbPredicates)
612 {
613 auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
614 return bmsExtensionClient->OptimizeDisposedPredicates(callingName, appId, userId, appIndex, absRdbPredicates);
615 }
616
GetDisposedRule(const std::string & callingName,const std::string & appId,DisposedRule & rule,int32_t appIndex,int32_t userId)617 ErrCode AppControlManagerRdb::GetDisposedRule(const std::string &callingName,
618 const std::string &appId, DisposedRule &rule, int32_t appIndex, int32_t userId)
619 {
620 LOG_D(BMS_TAG_DEFAULT, "rdb begin to GetDisposedRule");
621 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
622 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
623 absRdbPredicates.EqualTo(APP_CONTROL_LIST, DISPOSED_RULE);
624 absRdbPredicates.EqualTo(APP_ID, appId);
625 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
626 absRdbPredicates.EqualTo(APP_INDEX, std::to_string(appIndex));
627 OptimizeDisposedPredicates(callingName, appId, userId, appIndex, absRdbPredicates);
628 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
629 if (absSharedResultSet == nullptr) {
630 LOG_E(BMS_TAG_DEFAULT, "GetAppInstallControlRule failed");
631 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
632 }
633 ScopeGuard stateGuard([&] { absSharedResultSet->Close(); });
634 int32_t count;
635 int ret = absSharedResultSet->GetRowCount(count);
636 if (ret != NativeRdb::E_OK) {
637 LOG_E(BMS_TAG_DEFAULT, "GetRowCount failed, ret: %{public}d", ret);
638 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
639 }
640 if (count == 0) {
641 LOG_D(BMS_TAG_DEFAULT, "GetDisposedRule size 0");
642 return ERR_OK;
643 }
644 ret = absSharedResultSet->GoToFirstRow();
645 if (ret != NativeRdb::E_OK) {
646 LOG_E(BMS_TAG_DEFAULT, "GoToFirstRow failed, ret: %{public}d", ret);
647 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
648 }
649 std::string ruleString;
650 ret = absSharedResultSet->GetString(DISPOSED_STATUS_INDEX, ruleString);
651 if (ret != NativeRdb::E_OK) {
652 LOG_E(BMS_TAG_DEFAULT, "GetString DisposedStatus failed, ret: %{public}d", ret);
653 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
654 }
655 DisposedRule::FromString(ruleString, rule);
656 return ERR_OK;
657 }
658
GetAbilityRunningControlRule(const std::string & appId,int32_t appIndex,int32_t userId,std::vector<DisposedRule> & disposedRules)659 ErrCode AppControlManagerRdb::GetAbilityRunningControlRule(
660 const std::string &appId, int32_t appIndex, int32_t userId, std::vector<DisposedRule>& disposedRules)
661 {
662 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
663 LOG_D(BMS_TAG_DEFAULT, "rdb begin to GetAbilityRunningControlRule");
664 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
665 absRdbPredicates.EqualTo(APP_CONTROL_LIST, DISPOSED_RULE);
666 absRdbPredicates.EqualTo(APP_ID, appId);
667 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
668 absRdbPredicates.EqualTo(APP_INDEX, std::to_string(appIndex));
669 absRdbPredicates.OrderByAsc(PRIORITY); // ascending
670 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
671 if (absSharedResultSet == nullptr) {
672 LOG_E(BMS_TAG_DEFAULT, "GetAppInstallControlRule failed");
673 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
674 }
675 ScopeGuard stateGuard([&] { absSharedResultSet->Close(); });
676 int32_t count;
677 int ret = absSharedResultSet->GetRowCount(count);
678 if (ret != NativeRdb::E_OK) {
679 LOG_E(BMS_TAG_DEFAULT, "GetRowCount failed, ret: %{public}d", ret);
680 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
681 }
682 if (count == 0) {
683 LOG_D(BMS_TAG_DEFAULT, "GetDisposedRule size 0");
684 return ERR_OK;
685 }
686 ret = absSharedResultSet->GoToFirstRow();
687 if (ret != NativeRdb::E_OK) {
688 LOG_E(BMS_TAG_DEFAULT, "GoToFirstRow failed, ret: %{public}d", ret);
689 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
690 }
691 do {
692 ret = GetDisposedRuleFromResultSet(absSharedResultSet, disposedRules);
693 if (ret != ERR_OK) {
694 return ret;
695 }
696 } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
697 return ERR_OK;
698 }
699
GetDisposedRuleFromResultSet(std::shared_ptr<NativeRdb::ResultSet> absSharedResultSet,std::vector<DisposedRule> & disposedRules)700 ErrCode AppControlManagerRdb::GetDisposedRuleFromResultSet(
701 std::shared_ptr<NativeRdb::ResultSet> absSharedResultSet, std::vector<DisposedRule> &disposedRules)
702 {
703 if (absSharedResultSet == nullptr) {
704 LOG_E(BMS_TAG_DEFAULT, "GetAppInstallControlRule failed");
705 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
706 }
707 std::string ruleString;
708 ErrCode ret = absSharedResultSet->GetString(DISPOSED_STATUS_INDEX, ruleString);
709 if (ret != NativeRdb::E_OK) {
710 LOG_E(BMS_TAG_DEFAULT, "GetString appId failed, ret: %{public}d", ret);
711 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
712 }
713 std::string callerName;
714 ret = absSharedResultSet->GetString(CALLING_NAME_INDEX, callerName);
715 if (ret != NativeRdb::E_OK) {
716 callerName = "";
717 LOG_I(BMS_TAG_DEFAULT, "GetString callerName failed, ret: %{public}d", ret);
718 }
719 int32_t setTime = 0;
720 ret = absSharedResultSet->GetInt(TIME_STAMP_INDEX, setTime);
721 if (ret != NativeRdb::E_OK) {
722 setTime = 0;
723 LOG_I(BMS_TAG_DEFAULT, "GetInt setTime failed, ret: %{public}d", ret);
724 }
725 DisposedRule rule;
726 bool parseRet = DisposedRule::FromString(ruleString, rule);
727 if (!parseRet) {
728 LOG_W(BMS_TAG_DEFAULT, "parse DisposedRule failed");
729 }
730 rule.callerName = callerName;
731 rule.setTime = setTime;
732 disposedRules.push_back(rule);
733 return ERR_OK;
734 }
735
SetUninstallDisposedRule(const std::string & callingName,const std::string & appIdentifier,const UninstallDisposedRule & rule,int32_t appIndex,int32_t userId)736 ErrCode AppControlManagerRdb::SetUninstallDisposedRule(const std::string &callingName,
737 const std::string &appIdentifier, const UninstallDisposedRule &rule, int32_t appIndex, int32_t userId)
738 {
739 LOG_D(BMS_TAG_DEFAULT, "begin");
740 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
741 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
742 absRdbPredicates.EqualTo(APP_CONTROL_LIST, UNINSTALL_DISPOSED_RULE);
743 absRdbPredicates.EqualTo(APP_ID, appIdentifier);
744 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
745 absRdbPredicates.EqualTo(APP_INDEX, std::to_string(appIndex));
746
747 int64_t timeStamp = BundleUtil::GetCurrentTime();
748 NativeRdb::ValuesBucket valuesBucket;
749 valuesBucket.PutString(CALLING_NAME, callingName);
750 valuesBucket.PutString(APP_CONTROL_LIST, UNINSTALL_DISPOSED_RULE);
751 valuesBucket.PutString(APP_ID, appIdentifier);
752 valuesBucket.PutString(DISPOSED_STATUS, rule.ToString());
753 valuesBucket.PutInt(PRIORITY, rule.priority);
754 valuesBucket.PutInt(TIME_STAMP, timeStamp);
755 valuesBucket.PutString(USER_ID, std::to_string(userId));
756 valuesBucket.PutString(APP_INDEX, std::to_string(appIndex));
757 bool ret = rdbDataManager_->UpdateOrInsertData(valuesBucket, absRdbPredicates);
758 if (!ret) {
759 LOG_E(BMS_TAG_DEFAULT, "callingName:%{public}s appIdentifier:%{private}s failed.",
760 callingName.c_str(), appIdentifier.c_str());
761 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
762 }
763 return ERR_OK;
764 }
765
DeleteUninstallDisposedRule(const std::string & callingName,const std::string & appIdentifier,int32_t appIndex,int32_t userId)766 ErrCode AppControlManagerRdb::DeleteUninstallDisposedRule(const std::string &callingName,
767 const std::string &appIdentifier, int32_t appIndex, int32_t userId)
768 {
769 LOG_D(BMS_TAG_DEFAULT, "begin");
770 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
771 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
772 absRdbPredicates.EqualTo(APP_CONTROL_LIST, UNINSTALL_DISPOSED_RULE);
773 absRdbPredicates.EqualTo(APP_ID, appIdentifier);
774 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
775 absRdbPredicates.EqualTo(APP_INDEX, std::to_string(appIndex));
776 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
777 if (!ret) {
778 LOG_E(BMS_TAG_DEFAULT, "callingName:%{public}s appIdentifier:%{private}s failed",
779 callingName.c_str(), appIdentifier.c_str());
780 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
781 }
782 return ERR_OK;
783 }
784
GetUninstallDisposedRule(const std::string & appIdentifier,int32_t appIndex,int32_t userId,UninstallDisposedRule & rule)785 ErrCode AppControlManagerRdb::GetUninstallDisposedRule(const std::string &appIdentifier,
786 int32_t appIndex, int32_t userId, UninstallDisposedRule &rule)
787 {
788 LOG_D(BMS_TAG_DEFAULT, "begin");
789 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
790 absRdbPredicates.EqualTo(APP_CONTROL_LIST, UNINSTALL_DISPOSED_RULE);
791 absRdbPredicates.EqualTo(APP_ID, appIdentifier);
792 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
793 absRdbPredicates.EqualTo(APP_INDEX, std::to_string(appIndex));
794 absRdbPredicates.OrderByAsc(PRIORITY);
795 absRdbPredicates.OrderByAsc(TIME_STAMP);
796 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
797 if (absSharedResultSet == nullptr) {
798 LOG_E(BMS_TAG_DEFAULT, "null absSharedResultSet");
799 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
800 }
801 ScopeGuard stateGuard([&] { absSharedResultSet->Close(); });
802 int32_t count;
803 int ret = absSharedResultSet->GetRowCount(count);
804 if (ret != NativeRdb::E_OK) {
805 LOG_E(BMS_TAG_DEFAULT, "GetRowCount failed, ret: %{public}d", ret);
806 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
807 }
808 if (count == 0) {
809 LOG_D(BMS_TAG_DEFAULT, "count size 0");
810 return ERR_OK;
811 }
812 ret = absSharedResultSet->GoToFirstRow();
813 if (ret != NativeRdb::E_OK) {
814 LOG_E(BMS_TAG_DEFAULT, "GoToFirstRow failed, ret: %{public}d", ret);
815 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
816 }
817 std::string ruleString;
818 ret = absSharedResultSet->GetString(DISPOSED_STATUS_INDEX, ruleString);
819 if (ret != NativeRdb::E_OK) {
820 LOG_E(BMS_TAG_DEFAULT, "GetString failed, ret: %{public}d", ret);
821 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
822 }
823 UninstallDisposedRule::FromString(ruleString, rule);
824 return ERR_OK;
825 }
826 }
827 }