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 "hitrace_meter.h"
23 #include "scope_guard.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 const std::string APP_CONTROL_RDB_TABLE_NAME = "app_control";
29 const std::string RUNNING_CONTROL = "RunningControl";
30 const std::string DISPOSED_RULE = "DisposedRule";
31 const std::string APP_CONTROL_EDM_DEFAULT_MESSAGE = "The app has been disabled by EDM";
32 const std::string DEFAULT = "default";
33 const int32_t CALLING_NAME_INDEX = 1;
34 const int32_t APP_ID_INDEX = 4;
35 const int32_t CONTROL_MESSAGE_INDEX = 5;
36 const int32_t DISPOSED_STATUS_INDEX = 6;
37 constexpr int8_t TIME_STAMP_INDEX = 8;
38 // app control table key
39 const std::string CALLING_NAME = "CALLING_NAME";
40 const std::string APP_CONTROL_LIST = "APP_CONTROL_LIST";
41 const std::string USER_ID = "USER_ID";
42 const std::string APP_ID = "APP_ID";
43 const std::string CONTROL_MESSAGE = "CONTROL_MESSAGE";
44 const std::string DISPOSED_STATUS = "DISPOSED_STATUS";
45 const std::string PRIORITY = "PRIORITY";
46 const std::string TIME_STAMP = "TIME_STAMP";
47 const std::string APP_INDEX = "APP_INDEX";
48
49 enum class PRIORITY {
50 EDM = 100,
51 APP_MARKET = 200,
52 };
53 }
AppControlManagerRdb()54 AppControlManagerRdb::AppControlManagerRdb()
55 {
56 LOG_D(BMS_TAG_DEFAULT, "create AppControlManagerRdb");
57 BmsRdbConfig bmsRdbConfig;
58 bmsRdbConfig.dbName = ServiceConstants::BUNDLE_RDB_NAME;
59 bmsRdbConfig.tableName = APP_CONTROL_RDB_TABLE_NAME;
60 bmsRdbConfig.createTableSql = std::string(
61 "CREATE TABLE IF NOT EXISTS "
62 + APP_CONTROL_RDB_TABLE_NAME
63 + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, CALLING_NAME TEXT NOT NULL, "
64 + "APP_CONTROL_LIST TEXT, USER_ID INTEGER, APP_ID TEXT, CONTROL_MESSAGE TEXT, "
65 + "DISPOSED_STATUS TEXT, PRIORITY INTEGER, TIME_STAMP INTEGER);");
66 bmsRdbConfig.insertColumnSql.push_back(std::string("ALTER TABLE " + APP_CONTROL_RDB_TABLE_NAME +
67 " ADD APP_INDEX INTEGER DEFAULT 0;"));
68 rdbDataManager_ = std::make_shared<RdbDataManager>(bmsRdbConfig);
69 rdbDataManager_->CreateTable();
70 }
71
~AppControlManagerRdb()72 AppControlManagerRdb::~AppControlManagerRdb()
73 {
74 LOG_D(BMS_TAG_DEFAULT, "destroy AppControlManagerRdb");
75 }
76
AddAppInstallControlRule(const std::string & callingName,const std::vector<std::string> & appIds,const std::string & controlRuleType,int32_t userId)77 ErrCode AppControlManagerRdb::AddAppInstallControlRule(const std::string &callingName,
78 const std::vector<std::string> &appIds, const std::string &controlRuleType, int32_t userId)
79 {
80 int64_t timeStamp = BundleUtil::GetCurrentTime();
81 std::vector<NativeRdb::ValuesBucket> valuesBuckets;
82 for (auto appId : appIds) {
83 ErrCode result = DeleteOldControlRule(callingName, controlRuleType, appId, userId);
84 if (result != ERR_OK) {
85 LOG_E(BMS_TAG_DEFAULT, "DeleteOldControlRule failed");
86 return result;
87 }
88 NativeRdb::ValuesBucket valuesBucket;
89 valuesBucket.PutString(CALLING_NAME, callingName);
90 valuesBucket.PutString(APP_CONTROL_LIST, controlRuleType);
91 valuesBucket.PutInt(USER_ID, static_cast<int>(userId));
92 valuesBucket.PutString(APP_ID, appId);
93 valuesBucket.PutInt(TIME_STAMP, timeStamp);
94 valuesBuckets.emplace_back(valuesBucket);
95 }
96 int64_t insertNum = 0;
97 bool ret = rdbDataManager_->BatchInsert(insertNum, valuesBuckets);
98 if (!ret) {
99 LOG_E(BMS_TAG_DEFAULT, "BatchInsert failed");
100 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
101 }
102 if (valuesBuckets.size() != static_cast<uint64_t>(insertNum)) {
103 LOG_E(BMS_TAG_DEFAULT, "BatchInsert size not expected");
104 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
105 }
106 return ERR_OK;
107 }
108
DeleteAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,const std::vector<std::string> & appIds,int32_t userId)109 ErrCode AppControlManagerRdb::DeleteAppInstallControlRule(const std::string &callingName,
110 const std::string &controlRuleType, const std::vector<std::string> &appIds, int32_t userId)
111 {
112 for (const auto &appId : appIds) {
113 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
114 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
115 absRdbPredicates.EqualTo(APP_CONTROL_LIST, controlRuleType);
116 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
117 absRdbPredicates.EqualTo(APP_ID, appId);
118 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
119 if (!ret) {
120 LOG_E(BMS_TAG_DEFAULT, "Delete failed callingName:%{public}s appId:%{private}s userId:%{public}d",
121 callingName.c_str(), appId.c_str(), userId);
122 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
123 }
124 }
125 return ERR_OK;
126 }
127
DeleteAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,int32_t userId)128 ErrCode AppControlManagerRdb::DeleteAppInstallControlRule(const std::string &callingName,
129 const std::string &controlRuleType, int32_t userId)
130 {
131 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
132 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
133 absRdbPredicates.EqualTo(APP_CONTROL_LIST, controlRuleType);
134 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
135 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
136 if (!ret) {
137 LOG_E(BMS_TAG_DEFAULT, "DeleteData callingName:%{public}s controlRuleType:%{public}s failed",
138 callingName.c_str(), controlRuleType.c_str());
139 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
140 }
141 return ERR_OK;
142 }
143
GetAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,int32_t userId,std::vector<std::string> & appIds)144 ErrCode AppControlManagerRdb::GetAppInstallControlRule(const std::string &callingName,
145 const std::string &controlRuleType, int32_t userId, std::vector<std::string> &appIds)
146 {
147 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
148 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
149 absRdbPredicates.EqualTo(APP_CONTROL_LIST, controlRuleType);
150 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
151 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
152 if (absSharedResultSet == nullptr) {
153 LOG_E(BMS_TAG_DEFAULT, "GetAppInstallControlRule failed");
154 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
155 }
156 ScopeGuard stateGuard([&] { absSharedResultSet->Close(); });
157 int32_t count;
158 int ret = absSharedResultSet->GetRowCount(count);
159 if (ret != NativeRdb::E_OK) {
160 LOG_E(BMS_TAG_DEFAULT, "GetRowCount failed, ret: %{public}d", ret);
161 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
162 }
163 if (count == 0) {
164 LOG_D(BMS_TAG_DEFAULT, "GetAppInstallControlRule size 0");
165 return ERR_OK;
166 }
167
168 ret = absSharedResultSet->GoToFirstRow();
169 if (ret != NativeRdb::E_OK) {
170 LOG_E(BMS_TAG_DEFAULT, "GoToFirstRow failed, ret: %{public}d", ret);
171 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
172 }
173 do {
174 std::string appId;
175 ret = absSharedResultSet->GetString(APP_ID_INDEX, appId);
176 if (ret != NativeRdb::E_OK) {
177 LOG_E(BMS_TAG_DEFAULT, "GetString appId failed, ret: %{public}d", ret);
178 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
179 }
180 appIds.push_back(appId);
181 } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
182 return ERR_OK;
183 }
184
AddAppRunningControlRule(const std::string & callingName,const std::vector<AppRunningControlRule> & controlRules,int32_t userId)185 ErrCode AppControlManagerRdb::AddAppRunningControlRule(const std::string &callingName,
186 const std::vector<AppRunningControlRule> &controlRules, int32_t userId)
187 {
188 int64_t timeStamp = BundleUtil::GetCurrentTime();
189 std::vector<NativeRdb::ValuesBucket> valuesBuckets;
190 for (auto &controlRule : controlRules) {
191 ErrCode result = DeleteOldControlRule(callingName, RUNNING_CONTROL, controlRule.appId, userId);
192 if (result != ERR_OK) {
193 LOG_E(BMS_TAG_DEFAULT, "DeleteOldControlRule failed");
194 return result;
195 }
196 NativeRdb::ValuesBucket valuesBucket;
197 valuesBucket.PutString(CALLING_NAME, callingName);
198 valuesBucket.PutString(APP_CONTROL_LIST, RUNNING_CONTROL);
199 valuesBucket.PutInt(USER_ID, static_cast<int>(userId));
200 valuesBucket.PutString(APP_ID, controlRule.appId);
201 valuesBucket.PutString(CONTROL_MESSAGE, controlRule.controlMessage);
202 valuesBucket.PutInt(PRIORITY, static_cast<int>(PRIORITY::EDM));
203 valuesBucket.PutInt(TIME_STAMP, timeStamp);
204 valuesBuckets.emplace_back(valuesBucket);
205 }
206 int64_t insertNum = 0;
207 bool ret = rdbDataManager_->BatchInsert(insertNum, valuesBuckets);
208 if (!ret) {
209 LOG_E(BMS_TAG_DEFAULT, "BatchInsert AddAppRunningControlRule failed");
210 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
211 }
212 if (valuesBuckets.size() != static_cast<uint64_t>(insertNum)) {
213 LOG_E(BMS_TAG_DEFAULT, "BatchInsert size not expected");
214 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
215 }
216 return ERR_OK;
217 }
218
DeleteAppRunningControlRule(const std::string & callingName,const std::vector<AppRunningControlRule> & controlRules,int32_t userId)219 ErrCode AppControlManagerRdb::DeleteAppRunningControlRule(const std::string &callingName,
220 const std::vector<AppRunningControlRule> &controlRules, int32_t userId)
221 {
222 for (auto &rule : controlRules) {
223 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
224 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
225 absRdbPredicates.EqualTo(APP_CONTROL_LIST, RUNNING_CONTROL);
226 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
227 absRdbPredicates.EqualTo(APP_ID, rule.appId);
228 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
229 if (!ret) {
230 LOG_E(BMS_TAG_DEFAULT, "Delete failed callingName:%{public}s appid:%{private}s userId:%{public}d",
231 callingName.c_str(), rule.appId.c_str(), userId);
232 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
233 }
234 }
235 return ERR_OK;
236 }
DeleteAppRunningControlRule(const std::string & callingName,int32_t userId)237 ErrCode AppControlManagerRdb::DeleteAppRunningControlRule(const std::string &callingName, int32_t userId)
238 {
239 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
240 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
241 absRdbPredicates.EqualTo(APP_CONTROL_LIST, RUNNING_CONTROL);
242 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
243 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
244 if (!ret) {
245 LOG_E(BMS_TAG_DEFAULT, "DeleteAppRunningControlRule callingName:%{public}s userId:%{public}d failed",
246 callingName.c_str(), userId);
247 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
248 }
249 return ERR_OK;
250 }
251
GetAppRunningControlRule(const std::string & callingName,int32_t userId,std::vector<std::string> & appIds)252 ErrCode AppControlManagerRdb::GetAppRunningControlRule(const std::string &callingName,
253 int32_t userId, std::vector<std::string> &appIds)
254 {
255 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
256 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
257 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
258 absRdbPredicates.EqualTo(APP_CONTROL_LIST, RUNNING_CONTROL);
259 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
260 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
261 if (absSharedResultSet == nullptr) {
262 LOG_E(BMS_TAG_DEFAULT, "QueryData failed");
263 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
264 }
265 ScopeGuard stateGuard([&] { absSharedResultSet->Close(); });
266 int32_t count;
267 int ret = absSharedResultSet->GetRowCount(count);
268 if (ret != NativeRdb::E_OK) {
269 LOG_E(BMS_TAG_DEFAULT, "GetRowCount failed, ret: %{public}d", ret);
270 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
271 }
272 if (count == 0) {
273 LOG_D(BMS_TAG_DEFAULT, "GetAppRunningControlRule size 0");
274 return ERR_OK;
275 }
276 ret = absSharedResultSet->GoToFirstRow();
277 if (ret != NativeRdb::E_OK) {
278 LOG_E(BMS_TAG_DEFAULT, "GoToFirstRow failed, ret: %{public}d", ret);
279 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
280 }
281 do {
282 std::string appId;
283 ret = absSharedResultSet->GetString(APP_ID_INDEX, appId);
284 if (ret != NativeRdb::E_OK) {
285 LOG_E(BMS_TAG_DEFAULT, "GetString appId failed, ret: %{public}d", ret);
286 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
287 }
288 appIds.push_back(appId);
289 } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
290 return ERR_OK;
291 }
292
GetAppRunningControlRule(const std::string & appId,int32_t userId,AppRunningControlRuleResult & controlRuleResult)293 ErrCode AppControlManagerRdb::GetAppRunningControlRule(const std::string &appId,
294 int32_t userId, AppRunningControlRuleResult &controlRuleResult)
295 {
296 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
297 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
298 absRdbPredicates.EqualTo(APP_ID, appId);
299 absRdbPredicates.EqualTo(APP_CONTROL_LIST, RUNNING_CONTROL);
300 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
301 absRdbPredicates.OrderByAsc(PRIORITY); // ascending
302 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
303 if (absSharedResultSet == nullptr) {
304 LOG_E(BMS_TAG_DEFAULT, "QueryData failed");
305 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
306 }
307 ScopeGuard stateGuard([&] { absSharedResultSet->Close(); });
308 int32_t count;
309 int ret = absSharedResultSet->GetRowCount(count);
310 if (ret != NativeRdb::E_OK) {
311 LOG_E(BMS_TAG_DEFAULT, "GetRowCount failed, ret: %{public}d", ret);
312 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
313 }
314 if (count == 0) {
315 LOG_NOFUNC_W(BMS_TAG_DEFAULT, "control rule invalid size 0");
316 return ERR_BUNDLE_MANAGER_BUNDLE_NOT_SET_CONTROL;
317 }
318 ret = absSharedResultSet->GoToFirstRow();
319 if (ret != NativeRdb::E_OK) {
320 LOG_E(BMS_TAG_DEFAULT, "GoToFirstRow failed, ret: %{public}d", ret);
321 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
322 }
323 std::string callingName;
324 if (absSharedResultSet->GetString(CALLING_NAME_INDEX, callingName) != NativeRdb::E_OK) {
325 LOG_E(BMS_TAG_DEFAULT, "GetString callingName failed, ret: %{public}d", ret);
326 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
327 }
328 ret = absSharedResultSet->GetString(CONTROL_MESSAGE_INDEX, controlRuleResult.controlMessage);
329 if (ret != NativeRdb::E_OK) {
330 LOG_W(BMS_TAG_DEFAULT, "GetString controlMessage failed, ret: %{public}d", ret);
331 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
332 }
333 if (controlRuleResult.controlMessage.empty() && callingName == AppControlConstants::EDM_CALLING) {
334 LOG_D(BMS_TAG_DEFAULT, "GetString controlMessage default");
335 controlRuleResult.controlMessage = APP_CONTROL_EDM_DEFAULT_MESSAGE;
336 }
337 std::string wantString;
338 if (absSharedResultSet->GetString(DISPOSED_STATUS_INDEX, wantString) != NativeRdb::E_OK) {
339 LOG_E(BMS_TAG_DEFAULT, "GetString controlWant failed, ret: %{public}d", ret);
340 }
341 if (!wantString.empty()) {
342 controlRuleResult.controlWant = std::make_shared<Want>(*Want::FromString(wantString));
343 }
344 if (callingName == AppControlConstants::EDM_CALLING) {
345 controlRuleResult.isEdm = true;
346 }
347 return ERR_OK;
348 }
349
SetDisposedStatus(const std::string & callingName,const std::string & appId,const Want & want,int32_t userId)350 ErrCode AppControlManagerRdb::SetDisposedStatus(const std::string &callingName,
351 const std::string &appId, const Want &want, int32_t userId)
352 {
353 LOG_D(BMS_TAG_DEFAULT, "rdb begin to SetDisposedStatus");
354 ErrCode code = DeleteDisposedStatus(callingName, appId, userId);
355 if (code != ERR_OK) {
356 LOG_E(BMS_TAG_DEFAULT, "DeleteDisposedStatus failed");
357 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
358 }
359 int64_t timeStamp = BundleUtil::GetCurrentTime();
360 NativeRdb::ValuesBucket valuesBucket;
361 valuesBucket.PutString(CALLING_NAME, callingName);
362 valuesBucket.PutString(APP_CONTROL_LIST, RUNNING_CONTROL);
363 valuesBucket.PutString(APP_ID, appId);
364 valuesBucket.PutString(DISPOSED_STATUS, want.ToString());
365 valuesBucket.PutInt(PRIORITY, static_cast<int>(PRIORITY::APP_MARKET));
366 valuesBucket.PutInt(TIME_STAMP, timeStamp);
367 valuesBucket.PutString(USER_ID, std::to_string(userId));
368 bool ret = rdbDataManager_->InsertData(valuesBucket);
369 if (!ret) {
370 LOG_E(BMS_TAG_DEFAULT, "SetDisposedStatus callingName:%{public}s appId:%{private}s failed",
371 callingName.c_str(), appId.c_str());
372 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
373 }
374 return ERR_OK;
375 }
376
DeleteDisposedStatus(const std::string & callingName,const std::string & appId,int32_t userId)377 ErrCode AppControlManagerRdb::DeleteDisposedStatus(const std::string &callingName,
378 const std::string &appId, int32_t userId)
379 {
380 LOG_D(BMS_TAG_DEFAULT, "rdb begin to DeleteDisposedStatus");
381 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
382 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
383 absRdbPredicates.EqualTo(APP_CONTROL_LIST, RUNNING_CONTROL);
384 absRdbPredicates.EqualTo(APP_ID, appId);
385 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
386 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
387 if (!ret) {
388 LOG_E(BMS_TAG_DEFAULT, "DeleteDisposedStatus callingName:%{public}s appId:%{private}s failed",
389 callingName.c_str(), appId.c_str());
390 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
391 }
392 return ERR_OK;
393 }
394
GetDisposedStatus(const std::string & callingName,const std::string & appId,Want & want,int32_t userId)395 ErrCode AppControlManagerRdb::GetDisposedStatus(const std::string &callingName,
396 const std::string &appId, Want &want, int32_t userId)
397 {
398 LOG_D(BMS_TAG_DEFAULT, "rdb begin to GetDisposedStatus");
399 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
400 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
401 absRdbPredicates.EqualTo(APP_CONTROL_LIST, RUNNING_CONTROL);
402 absRdbPredicates.EqualTo(APP_ID, appId);
403 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
404 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
405 if (absSharedResultSet == nullptr) {
406 LOG_E(BMS_TAG_DEFAULT, "GetAppInstallControlRule failed");
407 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
408 }
409 ScopeGuard stateGuard([&] { absSharedResultSet->Close(); });
410 int32_t count;
411 int ret = absSharedResultSet->GetRowCount(count);
412 if (ret != NativeRdb::E_OK) {
413 LOG_E(BMS_TAG_DEFAULT, "GetRowCount failed, ret: %{public}d", ret);
414 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
415 }
416 if (count == 0) {
417 LOG_D(BMS_TAG_DEFAULT, "GetAppRunningControlRule size 0");
418 return ERR_OK;
419 }
420 ret = absSharedResultSet->GoToFirstRow();
421 if (ret != NativeRdb::E_OK) {
422 LOG_E(BMS_TAG_DEFAULT, "GoToFirstRow failed, ret: %{public}d", ret);
423 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
424 }
425 std::string wantString;
426 ret = absSharedResultSet->GetString(DISPOSED_STATUS_INDEX, wantString);
427 if (ret != NativeRdb::E_OK) {
428 LOG_E(BMS_TAG_DEFAULT, "GetString DisposedStatus failed, ret: %{public}d", ret);
429 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
430 }
431 want = *Want::FromString(wantString);
432 return ERR_OK;
433 }
434
DeleteOldControlRule(const std::string & callingName,const std::string & controlRuleType,const std::string & appId,int32_t userId)435 ErrCode AppControlManagerRdb::DeleteOldControlRule(const std::string &callingName, const std::string &controlRuleType,
436 const std::string &appId, int32_t userId)
437 {
438 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
439 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
440 absRdbPredicates.EqualTo(APP_CONTROL_LIST, controlRuleType);
441 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
442 absRdbPredicates.EqualTo(APP_ID, appId);
443 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
444 if (!ret) {
445 LOG_E(BMS_TAG_DEFAULT, "DeleteOldControlRule %{public}s, %{public}s, %{public}s, %{public}d failed",
446 callingName.c_str(), appId.c_str(), controlRuleType.c_str(), userId);
447 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
448 }
449 return ERR_OK;
450 }
451
SetDisposedRule(const std::string & callingName,const std::string & appId,const DisposedRule & rule,int32_t appIndex,int32_t userId)452 ErrCode AppControlManagerRdb::SetDisposedRule(const std::string &callingName,
453 const std::string &appId, const DisposedRule &rule, int32_t appIndex, int32_t userId)
454 {
455 ErrCode code = DeleteDisposedRule(callingName, appId, appIndex, userId);
456 if (code != ERR_OK) {
457 LOG_E(BMS_TAG_DEFAULT, "DeleteDisposedStatus failed.");
458 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
459 }
460 int64_t timeStamp = BundleUtil::GetCurrentTime();
461 NativeRdb::ValuesBucket valuesBucket;
462 valuesBucket.PutString(CALLING_NAME, callingName);
463 valuesBucket.PutString(APP_CONTROL_LIST, DISPOSED_RULE);
464 valuesBucket.PutString(APP_ID, appId);
465 valuesBucket.PutString(DISPOSED_STATUS, rule.ToString());
466 valuesBucket.PutInt(PRIORITY, rule.priority);
467 valuesBucket.PutInt(TIME_STAMP, timeStamp);
468 valuesBucket.PutString(USER_ID, std::to_string(userId));
469 valuesBucket.PutString(APP_INDEX, std::to_string(appIndex));
470 bool ret = rdbDataManager_->InsertData(valuesBucket);
471 if (!ret) {
472 LOG_E(BMS_TAG_DEFAULT, "SetDisposedStatus callingName:%{public}s appId:%{private}s failed.",
473 callingName.c_str(), appId.c_str());
474 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
475 }
476 return ERR_OK;
477 }
478
DeleteDisposedRule(const std::string & callingName,const std::string & appId,int32_t appIndex,int32_t userId)479 ErrCode AppControlManagerRdb::DeleteDisposedRule(const std::string &callingName,
480 const std::string &appId, int32_t appIndex, int32_t userId)
481 {
482 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
483 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
484 absRdbPredicates.EqualTo(APP_CONTROL_LIST, DISPOSED_RULE);
485 absRdbPredicates.EqualTo(APP_ID, appId);
486 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
487 absRdbPredicates.EqualTo(APP_INDEX, std::to_string(appIndex));
488 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
489 if (!ret) {
490 LOG_E(BMS_TAG_DEFAULT, "DeleteDisposedStatus callingName:%{public}s appId:%{private}s failed",
491 callingName.c_str(), appId.c_str());
492 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
493 }
494 return ERR_OK;
495 }
496
DeleteAllDisposedRuleByBundle(const std::string & appId,int32_t appIndex,int32_t userId)497 ErrCode AppControlManagerRdb::DeleteAllDisposedRuleByBundle(const std::string &appId, int32_t appIndex, int32_t userId)
498 {
499 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
500 std::vector<std::string> controlList = {DISPOSED_RULE, RUNNING_CONTROL};
501 absRdbPredicates.In(APP_CONTROL_LIST, controlList);
502 absRdbPredicates.EqualTo(APP_ID, appId);
503 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
504 // if appIndex is main app also clear all clone app
505 if (appIndex != Constants::MAIN_APP_INDEX) {
506 absRdbPredicates.EqualTo(APP_INDEX, std::to_string(appIndex));
507 }
508 bool ret = rdbDataManager_->DeleteData(absRdbPredicates);
509 if (!ret) {
510 LOG_E(BMS_TAG_DEFAULT, "DeleteAllDisposedRuleByBundle appId:%{private}s failed", appId.c_str());
511 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
512 }
513 return ERR_OK;
514 }
515
OptimizeDisposedPredicates(const std::string & callingName,const std::string & appId,int32_t userId,int32_t appIndex,NativeRdb::AbsRdbPredicates & absRdbPredicates)516 ErrCode AppControlManagerRdb::OptimizeDisposedPredicates(const std::string &callingName, const std::string &appId,
517 int32_t userId, int32_t appIndex, NativeRdb::AbsRdbPredicates &absRdbPredicates)
518 {
519 auto bmsExtensionClient = std::make_shared<BmsExtensionClient>();
520 return bmsExtensionClient->OptimizeDisposedPredicates(callingName, appId, userId, appIndex, absRdbPredicates);
521 }
522
GetDisposedRule(const std::string & callingName,const std::string & appId,DisposedRule & rule,int32_t appIndex,int32_t userId)523 ErrCode AppControlManagerRdb::GetDisposedRule(const std::string &callingName,
524 const std::string &appId, DisposedRule &rule, int32_t appIndex, int32_t userId)
525 {
526 LOG_D(BMS_TAG_DEFAULT, "rdb begin to GetDisposedRule");
527 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
528 absRdbPredicates.EqualTo(CALLING_NAME, callingName);
529 absRdbPredicates.EqualTo(APP_CONTROL_LIST, DISPOSED_RULE);
530 absRdbPredicates.EqualTo(APP_ID, appId);
531 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
532 absRdbPredicates.EqualTo(APP_INDEX, std::to_string(appIndex));
533 OptimizeDisposedPredicates(callingName, appId, userId, appIndex, absRdbPredicates);
534 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
535 if (absSharedResultSet == nullptr) {
536 LOG_E(BMS_TAG_DEFAULT, "GetAppInstallControlRule failed");
537 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
538 }
539 ScopeGuard stateGuard([&] { absSharedResultSet->Close(); });
540 int32_t count;
541 int ret = absSharedResultSet->GetRowCount(count);
542 if (ret != NativeRdb::E_OK) {
543 LOG_E(BMS_TAG_DEFAULT, "GetRowCount failed, ret: %{public}d", ret);
544 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
545 }
546 if (count == 0) {
547 LOG_D(BMS_TAG_DEFAULT, "GetDisposedRule size 0");
548 return ERR_OK;
549 }
550 ret = absSharedResultSet->GoToFirstRow();
551 if (ret != NativeRdb::E_OK) {
552 LOG_E(BMS_TAG_DEFAULT, "GoToFirstRow failed, ret: %{public}d", ret);
553 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
554 }
555 std::string ruleString;
556 ret = absSharedResultSet->GetString(DISPOSED_STATUS_INDEX, ruleString);
557 if (ret != NativeRdb::E_OK) {
558 LOG_E(BMS_TAG_DEFAULT, "GetString DisposedStatus failed, ret: %{public}d", ret);
559 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
560 }
561 DisposedRule::FromString(ruleString, rule);
562 return ERR_OK;
563 }
564
GetAbilityRunningControlRule(const std::string & appId,int32_t appIndex,int32_t userId,std::vector<DisposedRule> & disposedRules)565 ErrCode AppControlManagerRdb::GetAbilityRunningControlRule(
566 const std::string &appId, int32_t appIndex, int32_t userId, std::vector<DisposedRule>& disposedRules)
567 {
568 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
569 LOG_D(BMS_TAG_DEFAULT, "rdb begin to GetAbilityRunningControlRule");
570 NativeRdb::AbsRdbPredicates absRdbPredicates(APP_CONTROL_RDB_TABLE_NAME);
571 absRdbPredicates.EqualTo(APP_CONTROL_LIST, DISPOSED_RULE);
572 absRdbPredicates.EqualTo(APP_ID, appId);
573 absRdbPredicates.EqualTo(USER_ID, std::to_string(userId));
574 absRdbPredicates.EqualTo(APP_INDEX, std::to_string(appIndex));
575 absRdbPredicates.OrderByAsc(PRIORITY); // ascending
576 auto absSharedResultSet = rdbDataManager_->QueryData(absRdbPredicates);
577 if (absSharedResultSet == nullptr) {
578 LOG_E(BMS_TAG_DEFAULT, "GetAppInstallControlRule failed");
579 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
580 }
581 ScopeGuard stateGuard([&] { absSharedResultSet->Close(); });
582 int32_t count;
583 int ret = absSharedResultSet->GetRowCount(count);
584 if (ret != NativeRdb::E_OK) {
585 LOG_E(BMS_TAG_DEFAULT, "GetRowCount failed, ret: %{public}d", ret);
586 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
587 }
588 if (count == 0) {
589 LOG_D(BMS_TAG_DEFAULT, "GetDisposedRule size 0");
590 return ERR_OK;
591 }
592 ret = absSharedResultSet->GoToFirstRow();
593 if (ret != NativeRdb::E_OK) {
594 LOG_E(BMS_TAG_DEFAULT, "GoToFirstRow failed, ret: %{public}d", ret);
595 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
596 }
597 do {
598 std::string ruleString;
599 ret = absSharedResultSet->GetString(DISPOSED_STATUS_INDEX, ruleString);
600 if (ret != NativeRdb::E_OK) {
601 LOG_E(BMS_TAG_DEFAULT, "GetString appId failed, ret: %{public}d", ret);
602 return ERR_BUNDLE_MANAGER_APP_CONTROL_INTERNAL_ERROR;
603 }
604 DisposedRule rule;
605 bool parseRet = DisposedRule::FromString(ruleString, rule);
606 if (!parseRet) {
607 LOG_W(BMS_TAG_DEFAULT, "parse DisposedRule failed");
608 }
609 disposedRules.push_back(rule);
610 PrintDisposedRuleInfo(absSharedResultSet, rule);
611 } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK);
612 return ERR_OK;
613 }
614
PrintDisposedRuleInfo(const std::shared_ptr<NativeRdb::ResultSet> absSharedResultSet,const DisposedRule & rule)615 void AppControlManagerRdb::PrintDisposedRuleInfo(
616 const std::shared_ptr<NativeRdb::ResultSet> absSharedResultSet, const DisposedRule &rule)
617 {
618 std::string callerName;
619 absSharedResultSet->GetString(CALLING_NAME_INDEX, callerName);
620 int32_t setRuleTime = 0;
621 absSharedResultSet->GetInt(TIME_STAMP_INDEX, setRuleTime);
622 LOG_NOFUNC_W(BMS_TAG_DEFAULT, "control rule caller:%{public}s time:%{public}d rule:%{public}s",
623 callerName.c_str(), setRuleTime, rule.ToString().c_str());
624 }
625 }
626 }