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