• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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.h"
17 
18 #include "ability_manager_helper.h"
19 #include "account_helper.h"
20 #include "app_control_constants.h"
21 #include "app_control_manager_rdb.h"
22 #include "app_jump_interceptor_manager_rdb.h"
23 #include "app_log_tag_wrapper.h"
24 #include "bundle_mgr_service.h"
25 #include "bundle_parser.h"
26 #include "hitrace_meter.h"
27 #include "parameters.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 namespace {
32     constexpr const char* APP_MARKET_CALLING = "app market";
33     constexpr const char* INVALID_MESSAGE = "INVALID_MESSAGE";
34     constexpr const char* ATOMIC_SERVICE = "atomicservice";
35 }
36 
AppControlManager()37 AppControlManager::AppControlManager()
38 {
39     appControlManagerDb_ = std::make_shared<AppControlManagerRdb>();
40     commonEventMgr_ = std::make_shared<BundleCommonEventMgr>();
41     std::string configPath = BundleUtil::GetNoDisablingConfigPath();
42     ErrCode ret = BundleParser::ParseNoDisablingList(configPath, noControllingList_);
43     if (ret != ERR_OK) {
44         LOG_W(BMS_TAG_DEFAULT, "GetNoDisablingList failed");
45     }
46 }
47 
~AppControlManager()48 AppControlManager::~AppControlManager()
49 {
50 }
51 
AddAppInstallControlRule(const std::string & callingName,const std::vector<std::string> & appIds,const std::string & controlRuleType,int32_t userId)52 ErrCode AppControlManager::AddAppInstallControlRule(const std::string &callingName,
53     const std::vector<std::string> &appIds, const std::string &controlRuleType, int32_t userId)
54 {
55     LOG_D(BMS_TAG_DEFAULT, "AddAppInstallControlRule");
56     auto ret = appControlManagerDb_->AddAppInstallControlRule(callingName, appIds, controlRuleType, userId);
57     if ((ret == ERR_OK) && !isAppInstallControlEnabled_) {
58         isAppInstallControlEnabled_ = true;
59     }
60     return ret;
61 }
62 
DeleteAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,const std::vector<std::string> & appIds,int32_t userId)63 ErrCode AppControlManager::DeleteAppInstallControlRule(const std::string &callingName,
64     const std::string &controlRuleType, const std::vector<std::string> &appIds, int32_t userId)
65 {
66     LOG_D(BMS_TAG_DEFAULT, "DeleteAppInstallControlRule");
67     auto ret = appControlManagerDb_->DeleteAppInstallControlRule(callingName, controlRuleType, appIds, userId);
68     if ((ret == ERR_OK) && isAppInstallControlEnabled_) {
69         SetAppInstallControlStatus();
70     }
71     return ret;
72 }
73 
DeleteAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,int32_t userId)74 ErrCode AppControlManager::DeleteAppInstallControlRule(const std::string &callingName,
75     const std::string &controlRuleType, int32_t userId)
76 {
77     LOG_D(BMS_TAG_DEFAULT, "CleanInstallControlRule");
78     auto ret = appControlManagerDb_->DeleteAppInstallControlRule(callingName, controlRuleType, userId);
79     if ((ret == ERR_OK) && isAppInstallControlEnabled_) {
80         SetAppInstallControlStatus();
81     }
82     return ret;
83 }
84 
GetAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,int32_t userId,std::vector<std::string> & appIds)85 ErrCode AppControlManager::GetAppInstallControlRule(const std::string &callingName,
86     const std::string &controlRuleType, int32_t userId, std::vector<std::string> &appIds)
87 {
88     LOG_D(BMS_TAG_DEFAULT, "GetAppInstallControlRule");
89     return appControlManagerDb_->GetAppInstallControlRule(callingName, controlRuleType, userId, appIds);
90 }
91 
AddAppRunningControlRule(const std::string & callingName,const std::vector<AppRunningControlRule> & controlRules,int32_t userId)92 ErrCode AppControlManager::AddAppRunningControlRule(const std::string &callingName,
93     const std::vector<AppRunningControlRule> &controlRules, int32_t userId)
94 {
95     LOG_D(BMS_TAG_DEFAULT, "AddAppRunningControlRule");
96     std::lock_guard<std::mutex> lock(appRunningControlMutex_);
97     ErrCode ret = appControlManagerDb_->AddAppRunningControlRule(callingName, controlRules, userId);
98     if (ret == ERR_OK) {
99         for (const auto &rule : controlRules) {
100             std::string key = rule.appId + std::string("_") + std::to_string(userId);
101             auto iter = appRunningControlRuleResult_.find(key);
102             if (iter != appRunningControlRuleResult_.end()) {
103                 appRunningControlRuleResult_.erase(iter);
104             }
105         }
106         KillRunningApp(controlRules, userId);
107     }
108     return ret;
109 }
110 
DeleteAppRunningControlRule(const std::string & callingName,const std::vector<AppRunningControlRule> & controlRules,int32_t userId)111 ErrCode AppControlManager::DeleteAppRunningControlRule(const std::string &callingName,
112     const std::vector<AppRunningControlRule> &controlRules, int32_t userId)
113 {
114     std::lock_guard<std::mutex> lock(appRunningControlMutex_);
115     auto ret = appControlManagerDb_->DeleteAppRunningControlRule(callingName, controlRules, userId);
116     if (ret == ERR_OK) {
117         for (const auto &rule : controlRules) {
118             std::string key = rule.appId + std::string("_") + std::to_string(userId);
119             auto iter = appRunningControlRuleResult_.find(key);
120             if (iter != appRunningControlRuleResult_.end()) {
121                 appRunningControlRuleResult_.erase(iter);
122             }
123         }
124     }
125     return ret;
126 }
127 
DeleteAppRunningControlRule(const std::string & callingName,int32_t userId)128 ErrCode AppControlManager::DeleteAppRunningControlRule(const std::string &callingName, int32_t userId)
129 {
130     ErrCode res = appControlManagerDb_->DeleteAppRunningControlRule(callingName, userId);
131     if (res != ERR_OK) {
132         LOG_E(BMS_TAG_DEFAULT, "DeleteAppRunningControlRule failed");
133         return res;
134     }
135     std::lock_guard<std::mutex> lock(appRunningControlMutex_);
136     for (auto it = appRunningControlRuleResult_.begin(); it != appRunningControlRuleResult_.end();) {
137         std::string key = it->first;
138         std::string targetUserId = std::to_string(userId);
139         if (key.size() >= targetUserId.size() &&
140             key.compare(key.size() - targetUserId.size(), targetUserId.size(), targetUserId) == 0) {
141             it = appRunningControlRuleResult_.erase(it);
142         } else {
143             ++it;
144         }
145     }
146     return res;
147 }
148 
GetAppRunningControlRule(const std::string & callingName,int32_t userId,std::vector<std::string> & appIds)149 ErrCode AppControlManager::GetAppRunningControlRule(
150     const std::string &callingName, int32_t userId, std::vector<std::string> &appIds)
151 {
152     return appControlManagerDb_->GetAppRunningControlRule(callingName, userId, appIds);
153 }
154 
ConfirmAppJumpControlRule(const std::string & callerBundleName,const std::string & targetBundleName,int32_t userId)155 ErrCode AppControlManager::ConfirmAppJumpControlRule(const std::string &callerBundleName,
156     const std::string &targetBundleName, int32_t userId)
157 {
158     if (appJumpInterceptorManagerDb_ == nullptr) {
159         LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
160         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
161     }
162     return appJumpInterceptorManagerDb_->ConfirmAppJumpControlRule(callerBundleName, targetBundleName, userId);
163 }
164 
AddAppJumpControlRule(const std::vector<AppJumpControlRule> & controlRules,int32_t userId)165 ErrCode AppControlManager::AddAppJumpControlRule(const std::vector<AppJumpControlRule> &controlRules, int32_t userId)
166 {
167     if (appJumpInterceptorManagerDb_ == nullptr) {
168         LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
169         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
170     }
171     return appJumpInterceptorManagerDb_->AddAppJumpControlRule(controlRules, userId);
172 }
173 
DeleteAppJumpControlRule(const std::vector<AppJumpControlRule> & controlRules,int32_t userId)174 ErrCode AppControlManager::DeleteAppJumpControlRule(const std::vector<AppJumpControlRule> &controlRules, int32_t userId)
175 {
176     if (appJumpInterceptorManagerDb_ == nullptr) {
177         LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
178         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
179     }
180     return appJumpInterceptorManagerDb_->DeleteAppJumpControlRule(controlRules, userId);
181 }
182 
DeleteRuleByCallerBundleName(const std::string & callerBundleName,int32_t userId)183 ErrCode AppControlManager::DeleteRuleByCallerBundleName(const std::string &callerBundleName, int32_t userId)
184 {
185     if (appJumpInterceptorManagerDb_ == nullptr) {
186         LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
187         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
188     }
189     return appJumpInterceptorManagerDb_->DeleteRuleByCallerBundleName(callerBundleName, userId);
190 }
191 
DeleteRuleByTargetBundleName(const std::string & targetBundleName,int32_t userId)192 ErrCode AppControlManager::DeleteRuleByTargetBundleName(const std::string &targetBundleName, int32_t userId)
193 {
194     if (appJumpInterceptorManagerDb_ == nullptr) {
195         LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
196         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
197     }
198     return appJumpInterceptorManagerDb_->DeleteRuleByTargetBundleName(targetBundleName, userId);
199 }
200 
GetAppJumpControlRule(const std::string & callerBundleName,const std::string & targetBundleName,int32_t userId,AppJumpControlRule & controlRule)201 ErrCode AppControlManager::GetAppJumpControlRule(const std::string &callerBundleName,
202     const std::string &targetBundleName, int32_t userId, AppJumpControlRule &controlRule)
203 {
204     if (appJumpInterceptorManagerDb_ == nullptr) {
205         LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
206         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
207     }
208     auto ret = appJumpInterceptorManagerDb_->GetAppJumpControlRule(callerBundleName, targetBundleName,
209         userId, controlRule);
210     return ret;
211 }
212 
SetDisposedStatus(const std::string & appId,const Want & want,int32_t userId)213 ErrCode AppControlManager::SetDisposedStatus(const std::string &appId, const Want& want, int32_t userId)
214 {
215     if (!CheckCanDispose(appId, userId)) {
216         LOG_E(BMS_TAG_DEFAULT, "appid in white-list");
217         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
218     }
219     auto ret = appControlManagerDb_->SetDisposedStatus(APP_MARKET_CALLING, appId, want, userId);
220     if (ret != ERR_OK) {
221         LOG_E(BMS_TAG_DEFAULT, "SetDisposedStatus to rdb failed");
222         return ret;
223     }
224     std::string key = appId + std::string("_") + std::to_string(userId);
225     std::lock_guard<std::mutex> lock(appRunningControlMutex_);
226     auto iter = appRunningControlRuleResult_.find(key);
227     if (iter != appRunningControlRuleResult_.end()) {
228         appRunningControlRuleResult_.erase(iter);
229     }
230 
231     commonEventMgr_->NotifySetDiposedRule(appId, userId, want.ToString(), Constants::MAIN_APP_INDEX);
232     return ERR_OK;
233 }
234 
DeleteDisposedStatus(const std::string & appId,int32_t userId)235 ErrCode AppControlManager::DeleteDisposedStatus(const std::string &appId, int32_t userId)
236 {
237     auto ret = appControlManagerDb_->DeleteDisposedStatus(APP_MARKET_CALLING, appId, userId);
238     if (ret != ERR_OK) {
239         LOG_E(BMS_TAG_DEFAULT, "DeleteDisposedStatus to rdb failed");
240         return ret;
241     }
242     std::string key = appId + std::string("_") + std::to_string(userId);
243     std::lock_guard<std::mutex> lock(appRunningControlMutex_);
244     auto iter = appRunningControlRuleResult_.find(key);
245     if (iter != appRunningControlRuleResult_.end()) {
246         appRunningControlRuleResult_.erase(iter);
247     }
248     commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, Constants::MAIN_APP_INDEX);
249     return ERR_OK;
250 }
251 
GetDisposedStatus(const std::string & appId,Want & want,int32_t userId)252 ErrCode AppControlManager::GetDisposedStatus(const std::string &appId, Want& want, int32_t userId)
253 {
254     return appControlManagerDb_->GetDisposedStatus(
255         APP_MARKET_CALLING, appId, want, userId);
256 }
257 
GetAppRunningControlRule(const std::string & bundleName,int32_t userId,AppRunningControlRuleResult & controlRuleResult)258 ErrCode AppControlManager::GetAppRunningControlRule(
259     const std::string &bundleName, int32_t userId, AppRunningControlRuleResult &controlRuleResult)
260 {
261     HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
262     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
263     if (dataMgr == nullptr) {
264         LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
265         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
266     }
267     std::string appId;
268     ErrCode ret = dataMgr->GetAppIdByBundleName(bundleName, appId);
269     if (ret != ERR_OK) {
270         LOG_W(BMS_TAG_DEFAULT, "getAppId failed,-n:%{public}s", bundleName.c_str());
271         return ret;
272     }
273     std::string key = appId + std::string("_") + std::to_string(userId);
274     std::lock_guard<std::mutex> lock(appRunningControlMutex_);
275     if (appRunningControlRuleResult_.find(key) != appRunningControlRuleResult_.end()) {
276         controlRuleResult = appRunningControlRuleResult_[key];
277         if (controlRuleResult.controlMessage == INVALID_MESSAGE) {
278             controlRuleResult.controlMessage = std::string();
279             return ERR_BUNDLE_MANAGER_BUNDLE_NOT_SET_CONTROL;
280         }
281         return ERR_OK;
282     }
283     ret = appControlManagerDb_->GetAppRunningControlRule(appId, userId, controlRuleResult);
284     if (ret != ERR_OK) {
285         controlRuleResult.controlMessage = INVALID_MESSAGE;
286     }
287     appRunningControlRuleResult_.emplace(key, controlRuleResult);
288     return ret;
289 }
290 
KillRunningApp(const std::vector<AppRunningControlRule> & rules,int32_t userId) const291 void AppControlManager::KillRunningApp(const std::vector<AppRunningControlRule> &rules, int32_t userId) const
292 {
293     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
294     if (dataMgr == nullptr) {
295         LOG_E(BMS_TAG_DEFAULT, "dataMgr is null");
296         return;
297     }
298     std::for_each(rules.cbegin(), rules.cend(), [&dataMgr, userId](const auto &rule) {
299         std::string bundleName = dataMgr->GetBundleNameByAppId(rule.appId);
300         if (bundleName.empty()) {
301             LOG_W(BMS_TAG_DEFAULT, "GetBundleNameByAppId failed");
302             return;
303         }
304         BundleInfo bundleInfo;
305         ErrCode ret = dataMgr->GetBundleInfoV9(
306             bundleName, static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE), bundleInfo, userId);
307         if (ret != ERR_OK) {
308             LOG_W(BMS_TAG_DEFAULT, "GetBundleInfoV9 failed");
309             return;
310         }
311         AbilityManagerHelper::UninstallApplicationProcesses(bundleName, bundleInfo.uid);
312     });
313 }
314 
IsAppInstallControlEnabled() const315 bool AppControlManager::IsAppInstallControlEnabled() const
316 {
317     return isAppInstallControlEnabled_;
318 }
319 
SetAppInstallControlStatus()320 void AppControlManager::SetAppInstallControlStatus()
321 {
322     isAppInstallControlEnabled_ = false;
323     std::vector<std::string> appIds;
324     int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
325     if (currentUserId == Constants::INVALID_USERID) {
326         currentUserId = Constants::START_USERID;
327     }
328     ErrCode ret = appControlManagerDb_->GetAppInstallControlRule(AppControlConstants::EDM_CALLING,
329         AppControlConstants::APP_DISALLOWED_UNINSTALL, currentUserId, appIds);
330     if ((ret == ERR_OK) && !appIds.empty()) {
331         isAppInstallControlEnabled_ = true;
332     }
333 }
334 
SetDisposedRule(const std::string & callerName,const std::string & appId,const DisposedRule & rule,int32_t appIndex,int32_t userId)335 ErrCode AppControlManager::SetDisposedRule(const std::string &callerName, const std::string &appId,
336     const DisposedRule& rule, int32_t appIndex, int32_t userId)
337 {
338     if (!CheckCanDispose(appId, userId)) {
339         LOG_E(BMS_TAG_DEFAULT, "%{public}s set rule, user:%{public}d index:%{public}d",
340             callerName.c_str(), userId, appIndex);
341         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
342     }
343     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
344     if (dataMgr == nullptr) {
345         LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
346         return ERR_APPEXECFWK_NULL_PTR;
347     }
348     std::string transformedAppId = dataMgr->AppIdAndAppIdentifierTransform(appId);
349     ErrCode ret = appControlManagerDb_->DeleteDisposedRule(callerName, { appId, transformedAppId }, appIndex, userId);
350     if (ret != ERR_OK) {
351         LOG_E(BMS_TAG_DEFAULT, "delete failed caller:%{public}s id:%{private}s user:%{public}d index:%{public}d",
352             callerName.c_str(), appId.c_str(), userId, appIndex);
353         return ret;
354     }
355     ret = appControlManagerDb_->SetDisposedRule(callerName, appId, rule, appIndex, userId);
356     if (ret != ERR_OK) {
357         LOG_E(BMS_TAG_DEFAULT, "SetDisposedRule to rdb failed, %{public}s set rule, user:%{public}d index:%{public}d",
358             callerName.c_str(), userId, appIndex);
359         return ret;
360     }
361     std::string appIdKey = GenerateAppRunningRuleCacheKey(appId, userId, appIndex);
362     std::string transformedAppIdKey = GenerateAppRunningRuleCacheKey(transformedAppId, userId, appIndex);
363     DeleteAbilityRunningRuleCache({ appIdKey, transformedAppIdKey });
364     LOG_D(BMS_TAG_DEFAULT, "%{public}s set rule, user:%{public}d index:%{public}d",
365         callerName.c_str(), userId, appIndex);
366     commonEventMgr_->NotifySetDiposedRule(appId, userId, rule.ToString(), appIndex);
367     return ERR_OK;
368 }
369 
GetDisposedRule(const std::string & callerName,const std::string & appId,DisposedRule & rule,int32_t appIndex,int32_t userId)370 ErrCode AppControlManager::GetDisposedRule(
371     const std::string &callerName, const std::string &appId, DisposedRule& rule, int32_t appIndex, int32_t userId)
372 {
373     auto ret = appControlManagerDb_->GetDisposedRule(callerName, appId, rule, appIndex, userId);
374     if (ret != ERR_OK) {
375         LOG_E(BMS_TAG_DEFAULT, "GetDisposedRule to rdb failed");
376         return ret;
377     }
378     return ERR_OK;
379 }
380 
DeleteDisposedRule(const std::string & callerName,const std::string & appId,int32_t appIndex,int32_t userId)381 ErrCode AppControlManager::DeleteDisposedRule(
382     const std::string &callerName, const std::string &appId, int32_t appIndex, int32_t userId)
383 {
384     auto ret = appControlManagerDb_->DeleteDisposedRule(callerName, { appId }, appIndex, userId);
385     if (ret != ERR_OK) {
386         LOG_E(BMS_TAG_DEFAULT, "failed caller:%{public}s id:%{private}s user:%{public}d index:%{public}d",
387             callerName.c_str(), appId.c_str(), userId, appIndex);
388         return ret;
389     }
390     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
391     if (dataMgr == nullptr) {
392         LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
393         return ERR_APPEXECFWK_NULL_PTR;
394     }
395     std::string transformedAppId = dataMgr->AppIdAndAppIdentifierTransform(appId);
396     std::string appIdKey = GenerateAppRunningRuleCacheKey(appId, userId, appIndex);
397     std::string transformedAppIdKey = GenerateAppRunningRuleCacheKey(transformedAppId, userId, appIndex);
398     DeleteAbilityRunningRuleCache({ appIdKey, transformedAppIdKey});
399     commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, appIndex);
400     return ERR_OK;
401 }
402 
DeleteAllDisposedRuleByBundle(const InnerBundleInfo & bundleInfo,int32_t appIndex,int32_t userId)403 ErrCode AppControlManager::DeleteAllDisposedRuleByBundle(const InnerBundleInfo &bundleInfo, int32_t appIndex,
404     int32_t userId)
405 {
406     std::string appIdentifier = bundleInfo.GetAppIdentifier();
407     std::string appId = bundleInfo.GetAppId();
408     if (appControlManagerDb_ == nullptr) {
409         LOG_E(BMS_TAG_DEFAULT, "appControlManagerDb is nullptr");
410         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
411     }
412     auto ret = appControlManagerDb_->DeleteAllDisposedRuleByBundle({ appId, appIdentifier }, appIndex, userId);
413     if (ret != ERR_OK) {
414         LOG_E(BMS_TAG_DEFAULT, "rdb delete failed ret:%{public}d, appId:%{private}s", ret, appId.c_str());
415         return ret;
416     }
417     DeleteAbilityRunningRuleBmsCache(appId);
418     std::string key = appId + std::string("_") + std::to_string(userId);
419     DeleteAppRunningRuleCache(key);
420     key = key + std::string("_");
421     std::string cacheKey = key + std::to_string(appIndex);
422     DeleteAbilityRunningRuleCache({ cacheKey });
423     commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, appIndex);
424     if (appIndex != Constants::MAIN_APP_INDEX) {
425         return ERR_OK;
426     }
427     // if appIndex is main app clear all clone app cache
428     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
429     if (dataMgr == nullptr) {
430         LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
431         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
432     }
433     std::string bundleName = bundleInfo.GetBundleName();
434     std::vector<int32_t> appIndexVec = dataMgr->GetCloneAppIndexes(bundleName, Constants::ALL_USERID);
435     for (const int32_t index : appIndexVec) {
436         std::string ruleCacheKey = key + std::to_string(index);
437         DeleteAbilityRunningRuleCache({ ruleCacheKey });
438         commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, index);
439     }
440     return ERR_OK;
441 }
442 
DeleteAppRunningRuleCache(std::string & key)443 void AppControlManager::DeleteAppRunningRuleCache(std::string &key)
444 {
445     std::lock_guard<std::mutex> lock(appRunningControlMutex_);
446     auto iter = appRunningControlRuleResult_.find(key);
447     if (iter != appRunningControlRuleResult_.end()) {
448         appRunningControlRuleResult_.erase(iter);
449     }
450 }
451 
GetAbilityRunningRuleCache(const std::string & key,std::vector<DisposedRule> & disposedRules)452 bool AppControlManager::GetAbilityRunningRuleCache(const std::string &key, std::vector<DisposedRule> &disposedRules)
453 {
454     std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
455     auto iter = abilityRunningControlRuleCache_.find(key);
456     if (iter != abilityRunningControlRuleCache_.end()) {
457         disposedRules = iter->second;
458         return true;
459     }
460     return false;
461 }
462 
SetAbilityRunningRuleCache(const std::string & key,const std::vector<DisposedRule> & disposedRules)463 void AppControlManager::SetAbilityRunningRuleCache(const std::string &key,
464     const std::vector<DisposedRule> &disposedRules)
465 {
466     std::lock_guard<std::mutex> cacheLock(abilityRunningControlRuleMutex_);
467     abilityRunningControlRuleCache_[key] = disposedRules;
468 }
469 
DeleteAbilityRunningRuleCache(const std::vector<std::string> & keyList)470 void AppControlManager::DeleteAbilityRunningRuleCache(const std::vector<std::string> &keyList)
471 {
472     for (const std::string &key : keyList) {
473         std::lock_guard<std::mutex> cacheLock(abilityRunningControlRuleMutex_);
474         auto cacheIter = abilityRunningControlRuleCache_.find(key);
475         if (cacheIter != abilityRunningControlRuleCache_.end()) {
476             abilityRunningControlRuleCache_.erase(cacheIter);
477         }
478     }
479 }
GetAbilityRunningControlRule(const std::string & bundleName,int32_t appIndex,int32_t userId,std::vector<DisposedRule> & disposedRules)480 ErrCode AppControlManager::GetAbilityRunningControlRule(
481     const std::string &bundleName, int32_t appIndex, int32_t userId, std::vector<DisposedRule> &disposedRules)
482 {
483     HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr);
484     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
485     if (dataMgr == nullptr) {
486         LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
487         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
488     }
489     std::string appId;
490     std::string appIdentifier;
491     ErrCode ret = dataMgr->GetAppIdAndAppIdentifierByBundleName(bundleName, appId, appIdentifier);
492     if (ret != ERR_OK) {
493         LOG_W(BMS_TAG_DEFAULT, "DataMgr GetBundleInfoAppId failed");
494         return ret;
495     }
496     std::string key = GenerateAppRunningRuleCacheKey(appId, userId, appIndex);
497     bool findCache = GetAbilityRunningRuleCache(key, disposedRules);
498     if (findCache) {
499         PrintDisposedRuleInfo(disposedRules, appId);
500         return ERR_OK;
501     }
502     std::vector<std::string> appIdList;
503     if (!appId.empty()) {
504         appIdList.emplace_back(appId);
505     }
506     if (!appIdentifier.empty()) {
507         appIdList.emplace_back(appIdentifier);
508     }
509     ret = appControlManagerDb_->GetAbilityRunningControlRule(appIdList, appIndex, userId, disposedRules);
510     if (ret != ERR_OK) {
511         LOG_W(BMS_TAG_DEFAULT, "GetAbilityRunningControlRule from rdb failed");
512         return ret;
513     }
514     if (GetDisposedRuleOnlyForBms(appId, disposedRules)) {
515         LOG_I(BMS_TAG_DEFAULT, "find from bms cache -n %{public}s", bundleName.c_str());
516     };
517     SetAbilityRunningRuleCache(key, disposedRules);
518     PrintDisposedRuleInfo(disposedRules, appId);
519     return ret;
520 }
521 
CheckCanDispose(const std::string & appId,int32_t userId)522 bool AppControlManager::CheckCanDispose(const std::string &appId, int32_t userId)
523 {
524     auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
525     if (dataMgr == nullptr) {
526         LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
527         return false;
528     }
529     for (const auto &bundleName : noControllingList_) {
530         BundleInfo bundleInfo;
531         ErrCode ret = dataMgr->GetBundleInfoV9(bundleName,
532             static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE), bundleInfo, userId);
533         if (ret != ERR_OK) {
534             continue;
535         }
536         if (appId == bundleInfo.appId) {
537             return false;
538         }
539     }
540     return true;
541 }
542 
GetDisposedRuleOnlyForBms(const std::string & appId,std::vector<DisposedRule> & disposedRules)543 bool AppControlManager::GetDisposedRuleOnlyForBms(const std::string &appId, std::vector<DisposedRule> &disposedRules)
544 {
545     std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
546     auto iterBms = abilityRunningControlRuleCacheForBms_.find(appId);
547     if (iterBms != abilityRunningControlRuleCacheForBms_.end()) {
548         disposedRules.emplace_back(iterBms->second);
549         return true;
550     };
551     return false;
552 }
553 
SetDisposedRuleOnlyForBms(const std::string & appId)554 void AppControlManager::SetDisposedRuleOnlyForBms(const std::string &appId)
555 {
556     std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
557     for (auto iter = abilityRunningControlRuleCache_.begin(); iter != abilityRunningControlRuleCache_.end();) {
558         if (iter->first.find(appId) == 0) {
559             iter = abilityRunningControlRuleCache_.erase(iter);
560         } else {
561             ++iter;
562         }
563     }
564 
565     DisposedRule disposedRule;
566     disposedRule.componentType = ComponentType::UI_ABILITY;
567     disposedRule.disposedType = DisposedType::BLOCK_APPLICATION;
568     disposedRule.controlType = ControlType::DISALLOWED_LIST;
569     abilityRunningControlRuleCacheForBms_[appId] = disposedRule;
570 }
571 
DeleteDisposedRuleOnlyForBms(const std::string & appId)572 void AppControlManager::DeleteDisposedRuleOnlyForBms(const std::string &appId)
573 {
574     std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
575     for (auto iter = abilityRunningControlRuleCache_.begin(); iter != abilityRunningControlRuleCache_.end();) {
576         if (iter->first.find(appId) == 0) {
577             iter = abilityRunningControlRuleCache_.erase(iter);
578         } else {
579             ++iter;
580         }
581     }
582 
583     auto iterBms = abilityRunningControlRuleCacheForBms_.find(appId);
584     if (iterBms != abilityRunningControlRuleCacheForBms_.end()) {
585         abilityRunningControlRuleCacheForBms_.erase(iterBms);
586     }
587 }
588 
589 
DeleteAbilityRunningRuleBmsCache(const std::string & appId)590 void AppControlManager::DeleteAbilityRunningRuleBmsCache(const std::string &appId)
591 {
592     std::lock_guard<std::mutex> cacheLock(abilityRunningControlRuleMutex_);
593     auto iterBms = abilityRunningControlRuleCacheForBms_.find(appId);
594     if (iterBms != abilityRunningControlRuleCacheForBms_.end()) {
595         abilityRunningControlRuleCacheForBms_.erase(iterBms);
596     }
597 }
598 
SetUninstallDisposedRule(const std::string & callerName,const std::string & appIdentifier,const UninstallDisposedRule & rule,int32_t appIndex,int32_t userId)599 ErrCode AppControlManager::SetUninstallDisposedRule(const std::string &callerName, const std::string &appIdentifier,
600     const UninstallDisposedRule& rule, int32_t appIndex, int32_t userId)
601 {
602     auto ret = appControlManagerDb_->SetUninstallDisposedRule(callerName, appIdentifier, rule, appIndex, userId);
603     if (ret != ERR_OK) {
604         LOG_E(BMS_TAG_DEFAULT, "set to rdb failed");
605         return ret;
606     }
607     LOG_I(BMS_TAG_DEFAULT, "%{public}s set uninstall rule, user:%{public}d index:%{public}d",
608         callerName.c_str(), userId, appIndex);
609     return ERR_OK;
610 }
611 
GetUninstallDisposedRule(const std::string & appIdentifier,int32_t appIndex,int32_t userId,UninstallDisposedRule & rule)612 ErrCode AppControlManager::GetUninstallDisposedRule(const std::string &appIdentifier, int32_t appIndex,
613     int32_t userId, UninstallDisposedRule& rule)
614 {
615     auto ret = appControlManagerDb_->GetUninstallDisposedRule(appIdentifier, appIndex, userId, rule);
616     if (ret != ERR_OK) {
617         LOG_E(BMS_TAG_DEFAULT, "get from rdb failed");
618         return ret;
619     }
620     return ERR_OK;
621 }
622 
DeleteUninstallDisposedRule(const std::string & callerName,const std::string & appIdentifier,int32_t appIndex,int32_t userId)623 ErrCode AppControlManager::DeleteUninstallDisposedRule(
624     const std::string &callerName, const std::string &appIdentifier, int32_t appIndex, int32_t userId)
625 {
626     auto ret = appControlManagerDb_->DeleteUninstallDisposedRule(callerName, appIdentifier, appIndex, userId);
627     if (ret != ERR_OK) {
628         LOG_E(BMS_TAG_DEFAULT, "delete from rdb failed");
629         return ret;
630     }
631     return ERR_OK;
632 }
633 
PrintDisposedRuleInfo(const std::vector<DisposedRule> & disposedRules,const std::string & key)634 void AppControlManager::PrintDisposedRuleInfo(const std::vector<DisposedRule> &disposedRules, const std::string &key)
635 {
636     if (key.find(ATOMIC_SERVICE) != std::string::npos) {
637         LOG_NOFUNC_I(BMS_TAG_DEFAULT, "get rule by %{public}s", key.c_str());
638     }
639     for (const auto &rule : disposedRules) {
640         LOG_NOFUNC_I(BMS_TAG_DEFAULT, "control rule caller:%{public}s time:%{public}" PRId64,
641         rule.callerName.c_str(), rule.setTime);
642     }
643 }
644 
GenerateAppRunningRuleCacheKey(const std::string & appId,int32_t userId,int32_t appIndex)645 std::string AppControlManager::GenerateAppRunningRuleCacheKey(
646     const std::string &appId, int32_t userId, int32_t appIndex)
647 {
648     return appId + std::string("_") + std::to_string(userId) + std::string("_") + std::to_string(appIndex);
649 }
650 }
651 }