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.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 const std::string APP_MARKET_CALLING = "app market";
33 const std::string INVALID_MESSAGE = "INVALID_MESSAGE";
34 }
35
AppControlManager()36 AppControlManager::AppControlManager()
37 {
38 appControlManagerDb_ = std::make_shared<AppControlManagerRdb>();
39 bool isAppJumpEnabled = OHOS::system::GetBoolParameter(
40 OHOS::AppExecFwk::PARAMETER_APP_JUMP_INTERCEPTOR_ENABLE, false);
41 if (isAppJumpEnabled) {
42 LOG_I(BMS_TAG_DEFAULT, "App jump intercetor enabled, start init to AppJumpInterceptorManagerRdb");
43 appJumpInterceptorManagerDb_ = std::make_shared<AppJumpInterceptorManagerRdb>();
44 appJumpInterceptorManagerDb_->SubscribeCommonEvent();
45 } else {
46 LOG_I(BMS_TAG_DEFAULT, "App jump intercetor disabled");
47 }
48 commonEventMgr_ = std::make_shared<BundleCommonEventMgr>();
49 std::string configPath = BundleUtil::GetNoDisablingConfigPath();
50 ErrCode ret = BundleParser::ParseNoDisablingList(configPath, noControllingList_);
51 if (ret != ERR_OK) {
52 LOG_W(BMS_TAG_DEFAULT, "GetNoDisablingList failed");
53 }
54 }
55
~AppControlManager()56 AppControlManager::~AppControlManager()
57 {
58 }
59
AddAppInstallControlRule(const std::string & callingName,const std::vector<std::string> & appIds,const std::string & controlRuleType,int32_t userId)60 ErrCode AppControlManager::AddAppInstallControlRule(const std::string &callingName,
61 const std::vector<std::string> &appIds, const std::string &controlRuleType, int32_t userId)
62 {
63 LOG_D(BMS_TAG_DEFAULT, "AddAppInstallControlRule");
64 auto ret = appControlManagerDb_->AddAppInstallControlRule(callingName, appIds, controlRuleType, userId);
65 if ((ret == ERR_OK) && !isAppInstallControlEnabled_) {
66 isAppInstallControlEnabled_ = true;
67 }
68 return ret;
69 }
70
DeleteAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,const std::vector<std::string> & appIds,int32_t userId)71 ErrCode AppControlManager::DeleteAppInstallControlRule(const std::string &callingName,
72 const std::string &controlRuleType, const std::vector<std::string> &appIds, int32_t userId)
73 {
74 LOG_D(BMS_TAG_DEFAULT, "DeleteAppInstallControlRule");
75 auto ret = appControlManagerDb_->DeleteAppInstallControlRule(callingName, controlRuleType, appIds, userId);
76 if ((ret == ERR_OK) && isAppInstallControlEnabled_) {
77 SetAppInstallControlStatus();
78 }
79 return ret;
80 }
81
DeleteAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,int32_t userId)82 ErrCode AppControlManager::DeleteAppInstallControlRule(const std::string &callingName,
83 const std::string &controlRuleType, int32_t userId)
84 {
85 LOG_D(BMS_TAG_DEFAULT, "CleanInstallControlRule");
86 auto ret = appControlManagerDb_->DeleteAppInstallControlRule(callingName, controlRuleType, userId);
87 if ((ret == ERR_OK) && isAppInstallControlEnabled_) {
88 SetAppInstallControlStatus();
89 }
90 return ret;
91 }
92
GetAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,int32_t userId,std::vector<std::string> & appIds)93 ErrCode AppControlManager::GetAppInstallControlRule(const std::string &callingName,
94 const std::string &controlRuleType, int32_t userId, std::vector<std::string> &appIds)
95 {
96 LOG_D(BMS_TAG_DEFAULT, "GetAppInstallControlRule");
97 return appControlManagerDb_->GetAppInstallControlRule(callingName, controlRuleType, userId, appIds);
98 }
99
AddAppRunningControlRule(const std::string & callingName,const std::vector<AppRunningControlRule> & controlRules,int32_t userId)100 ErrCode AppControlManager::AddAppRunningControlRule(const std::string &callingName,
101 const std::vector<AppRunningControlRule> &controlRules, int32_t userId)
102 {
103 LOG_D(BMS_TAG_DEFAULT, "AddAppRunningControlRule");
104 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
105 ErrCode ret = appControlManagerDb_->AddAppRunningControlRule(callingName, controlRules, userId);
106 if (ret == ERR_OK) {
107 for (const auto &rule : controlRules) {
108 std::string key = rule.appId + std::string("_") + std::to_string(userId);
109 auto iter = appRunningControlRuleResult_.find(key);
110 if (iter != appRunningControlRuleResult_.end()) {
111 appRunningControlRuleResult_.erase(iter);
112 }
113 }
114 KillRunningApp(controlRules, userId);
115 }
116 return ret;
117 }
118
DeleteAppRunningControlRule(const std::string & callingName,const std::vector<AppRunningControlRule> & controlRules,int32_t userId)119 ErrCode AppControlManager::DeleteAppRunningControlRule(const std::string &callingName,
120 const std::vector<AppRunningControlRule> &controlRules, int32_t userId)
121 {
122 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
123 auto ret = appControlManagerDb_->DeleteAppRunningControlRule(callingName, controlRules, userId);
124 if (ret == ERR_OK) {
125 for (const auto &rule : controlRules) {
126 std::string key = rule.appId + std::string("_") + std::to_string(userId);
127 auto iter = appRunningControlRuleResult_.find(key);
128 if (iter != appRunningControlRuleResult_.end()) {
129 appRunningControlRuleResult_.erase(iter);
130 }
131 }
132 }
133 return ret;
134 }
135
DeleteAppRunningControlRule(const std::string & callingName,int32_t userId)136 ErrCode AppControlManager::DeleteAppRunningControlRule(const std::string &callingName, int32_t userId)
137 {
138 ErrCode res = appControlManagerDb_->DeleteAppRunningControlRule(callingName, userId);
139 if (res != ERR_OK) {
140 LOG_E(BMS_TAG_DEFAULT, "DeleteAppRunningControlRule failed");
141 return res;
142 }
143 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
144 for (auto it = appRunningControlRuleResult_.begin(); it != appRunningControlRuleResult_.end();) {
145 std::string key = it->first;
146 std::string targetUserId = std::to_string(userId);
147 if (key.size() >= targetUserId.size() &&
148 key.compare(key.size() - targetUserId.size(), targetUserId.size(), targetUserId) == 0) {
149 it = appRunningControlRuleResult_.erase(it);
150 } else {
151 ++it;
152 }
153 }
154 return res;
155 }
156
GetAppRunningControlRule(const std::string & callingName,int32_t userId,std::vector<std::string> & appIds)157 ErrCode AppControlManager::GetAppRunningControlRule(
158 const std::string &callingName, int32_t userId, std::vector<std::string> &appIds)
159 {
160 return appControlManagerDb_->GetAppRunningControlRule(callingName, userId, appIds);
161 }
162
ConfirmAppJumpControlRule(const std::string & callerBundleName,const std::string & targetBundleName,int32_t userId)163 ErrCode AppControlManager::ConfirmAppJumpControlRule(const std::string &callerBundleName,
164 const std::string &targetBundleName, int32_t userId)
165 {
166 if (appJumpInterceptorManagerDb_ == nullptr) {
167 LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
168 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
169 }
170 return appJumpInterceptorManagerDb_->ConfirmAppJumpControlRule(callerBundleName, targetBundleName, userId);
171 }
172
AddAppJumpControlRule(const std::vector<AppJumpControlRule> & controlRules,int32_t userId)173 ErrCode AppControlManager::AddAppJumpControlRule(const std::vector<AppJumpControlRule> &controlRules, int32_t userId)
174 {
175 if (appJumpInterceptorManagerDb_ == nullptr) {
176 LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
177 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
178 }
179 return appJumpInterceptorManagerDb_->AddAppJumpControlRule(controlRules, userId);
180 }
181
DeleteAppJumpControlRule(const std::vector<AppJumpControlRule> & controlRules,int32_t userId)182 ErrCode AppControlManager::DeleteAppJumpControlRule(const std::vector<AppJumpControlRule> &controlRules, int32_t userId)
183 {
184 if (appJumpInterceptorManagerDb_ == nullptr) {
185 LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
186 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
187 }
188 return appJumpInterceptorManagerDb_->DeleteAppJumpControlRule(controlRules, userId);
189 }
190
DeleteRuleByCallerBundleName(const std::string & callerBundleName,int32_t userId)191 ErrCode AppControlManager::DeleteRuleByCallerBundleName(const std::string &callerBundleName, int32_t userId)
192 {
193 if (appJumpInterceptorManagerDb_ == nullptr) {
194 LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
195 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
196 }
197 return appJumpInterceptorManagerDb_->DeleteRuleByCallerBundleName(callerBundleName, userId);
198 }
199
DeleteRuleByTargetBundleName(const std::string & targetBundleName,int32_t userId)200 ErrCode AppControlManager::DeleteRuleByTargetBundleName(const std::string &targetBundleName, int32_t userId)
201 {
202 if (appJumpInterceptorManagerDb_ == nullptr) {
203 LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
204 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
205 }
206 return appJumpInterceptorManagerDb_->DeleteRuleByTargetBundleName(targetBundleName, userId);
207 }
208
GetAppJumpControlRule(const std::string & callerBundleName,const std::string & targetBundleName,int32_t userId,AppJumpControlRule & controlRule)209 ErrCode AppControlManager::GetAppJumpControlRule(const std::string &callerBundleName,
210 const std::string &targetBundleName, int32_t userId, AppJumpControlRule &controlRule)
211 {
212 if (appJumpInterceptorManagerDb_ == nullptr) {
213 LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
214 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
215 }
216 auto ret = appJumpInterceptorManagerDb_->GetAppJumpControlRule(callerBundleName, targetBundleName,
217 userId, controlRule);
218 return ret;
219 }
220
SetDisposedStatus(const std::string & appId,const Want & want,int32_t userId)221 ErrCode AppControlManager::SetDisposedStatus(const std::string &appId, const Want& want, int32_t userId)
222 {
223 if (!CheckCanDispose(appId, userId)) {
224 LOG_E(BMS_TAG_DEFAULT, "appid in white-list");
225 return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
226 }
227 auto ret = appControlManagerDb_->SetDisposedStatus(APP_MARKET_CALLING, appId, want, userId);
228 if (ret != ERR_OK) {
229 LOG_E(BMS_TAG_DEFAULT, "SetDisposedStatus to rdb failed");
230 return ret;
231 }
232 std::string key = appId + std::string("_") + std::to_string(userId);
233 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
234 auto iter = appRunningControlRuleResult_.find(key);
235 if (iter != appRunningControlRuleResult_.end()) {
236 appRunningControlRuleResult_.erase(iter);
237 }
238 commonEventMgr_->NotifySetDiposedRule(appId, userId, want.ToString(), Constants::MAIN_APP_INDEX);
239 return ERR_OK;
240 }
241
DeleteDisposedStatus(const std::string & appId,int32_t userId)242 ErrCode AppControlManager::DeleteDisposedStatus(const std::string &appId, int32_t userId)
243 {
244 auto ret = appControlManagerDb_->DeleteDisposedStatus(APP_MARKET_CALLING, appId, userId);
245 if (ret != ERR_OK) {
246 LOG_E(BMS_TAG_DEFAULT, "DeleteDisposedStatus to rdb failed");
247 return ret;
248 }
249 std::string key = appId + std::string("_") + std::to_string(userId);
250 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
251 auto iter = appRunningControlRuleResult_.find(key);
252 if (iter != appRunningControlRuleResult_.end()) {
253 appRunningControlRuleResult_.erase(iter);
254 }
255 commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, Constants::MAIN_APP_INDEX);
256 return ERR_OK;
257 }
258
GetDisposedStatus(const std::string & appId,Want & want,int32_t userId)259 ErrCode AppControlManager::GetDisposedStatus(const std::string &appId, Want& want, int32_t userId)
260 {
261 return appControlManagerDb_->GetDisposedStatus(
262 APP_MARKET_CALLING, appId, want, userId);
263 }
264
GetAppRunningControlRule(const std::string & bundleName,int32_t userId,AppRunningControlRuleResult & controlRuleResult)265 ErrCode AppControlManager::GetAppRunningControlRule(
266 const std::string &bundleName, int32_t userId, AppRunningControlRuleResult &controlRuleResult)
267 {
268 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
269 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
270 if (dataMgr == nullptr) {
271 LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
272 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
273 }
274 std::string appId;
275 ErrCode ret = dataMgr->GetAppIdByBundleName(bundleName, appId);
276 if (ret != ERR_OK) {
277 LOG_W(BMS_TAG_DEFAULT, "getAppId failed,-n:%{public}s", bundleName.c_str());
278 return ret;
279 }
280 std::string key = appId + std::string("_") + std::to_string(userId);
281 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
282 if (appRunningControlRuleResult_.find(key) != appRunningControlRuleResult_.end()) {
283 controlRuleResult = appRunningControlRuleResult_[key];
284 if (controlRuleResult.controlMessage == INVALID_MESSAGE) {
285 controlRuleResult.controlMessage = std::string();
286 return ERR_BUNDLE_MANAGER_BUNDLE_NOT_SET_CONTROL;
287 }
288 return ERR_OK;
289 }
290 ret = appControlManagerDb_->GetAppRunningControlRule(appId, userId, controlRuleResult);
291 if (ret != ERR_OK) {
292 controlRuleResult.controlMessage = INVALID_MESSAGE;
293 }
294 appRunningControlRuleResult_.emplace(key, controlRuleResult);
295 return ret;
296 }
297
KillRunningApp(const std::vector<AppRunningControlRule> & rules,int32_t userId) const298 void AppControlManager::KillRunningApp(const std::vector<AppRunningControlRule> &rules, int32_t userId) const
299 {
300 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
301 if (dataMgr == nullptr) {
302 LOG_E(BMS_TAG_DEFAULT, "dataMgr is null");
303 return;
304 }
305 std::for_each(rules.cbegin(), rules.cend(), [&dataMgr, userId](const auto &rule) {
306 std::string bundleName = dataMgr->GetBundleNameByAppId(rule.appId);
307 if (bundleName.empty()) {
308 LOG_W(BMS_TAG_DEFAULT, "GetBundleNameByAppId failed");
309 return;
310 }
311 BundleInfo bundleInfo;
312 ErrCode ret = dataMgr->GetBundleInfoV9(
313 bundleName, static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE), bundleInfo, userId);
314 if (ret != ERR_OK) {
315 LOG_W(BMS_TAG_DEFAULT, "GetBundleInfoV9 failed");
316 return;
317 }
318 AbilityManagerHelper::UninstallApplicationProcesses(bundleName, bundleInfo.uid);
319 });
320 }
321
IsAppInstallControlEnabled() const322 bool AppControlManager::IsAppInstallControlEnabled() const
323 {
324 return isAppInstallControlEnabled_;
325 }
326
SetAppInstallControlStatus()327 void AppControlManager::SetAppInstallControlStatus()
328 {
329 isAppInstallControlEnabled_ = false;
330 std::vector<std::string> appIds;
331 int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
332 if (currentUserId == Constants::INVALID_USERID) {
333 currentUserId = Constants::START_USERID;
334 }
335 ErrCode ret = appControlManagerDb_->GetAppInstallControlRule(AppControlConstants::EDM_CALLING,
336 AppControlConstants::APP_DISALLOWED_UNINSTALL, currentUserId, appIds);
337 if ((ret == ERR_OK) && !appIds.empty()) {
338 isAppInstallControlEnabled_ = true;
339 }
340 }
341
SetDisposedRule(const std::string & callerName,const std::string & appId,const DisposedRule & rule,int32_t appIndex,int32_t userId)342 ErrCode AppControlManager::SetDisposedRule(const std::string &callerName, const std::string &appId,
343 const DisposedRule& rule, int32_t appIndex, int32_t userId)
344 {
345 if (!CheckCanDispose(appId, userId)) {
346 LOG_E(BMS_TAG_DEFAULT, "appid in white-list");
347 return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
348 }
349 auto ret = appControlManagerDb_->SetDisposedRule(callerName, appId, rule, appIndex, userId);
350 if (ret != ERR_OK) {
351 LOG_E(BMS_TAG_DEFAULT, "SetDisposedStatus to rdb failed");
352 return ret;
353 }
354 std::string key = appId + std::string("_") + std::to_string(userId) + std::string("_") + std::to_string(appIndex);
355 {
356 std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
357 auto iter = abilityRunningControlRuleCache_.find(key);
358 if (iter != abilityRunningControlRuleCache_.end()) {
359 abilityRunningControlRuleCache_.erase(iter);
360 }
361 }
362 LOG_I(BMS_TAG_DEFAULT, "%{public}s set rule, user:%{public}d index:%{public}d",
363 callerName.c_str(), userId, appIndex);
364 commonEventMgr_->NotifySetDiposedRule(appId, userId, rule.ToString(), appIndex);
365 return ERR_OK;
366 }
367
GetDisposedRule(const std::string & callerName,const std::string & appId,DisposedRule & rule,int32_t appIndex,int32_t userId)368 ErrCode AppControlManager::GetDisposedRule(
369 const std::string &callerName, const std::string &appId, DisposedRule& rule, int32_t appIndex, int32_t userId)
370 {
371 auto ret = appControlManagerDb_->GetDisposedRule(callerName, appId, rule, appIndex, userId);
372 if (ret != ERR_OK) {
373 LOG_E(BMS_TAG_DEFAULT, "GetDisposedRule to rdb failed");
374 return ret;
375 }
376 return ERR_OK;
377 }
378
DeleteDisposedRule(const std::string & callerName,const std::string & appId,int32_t appIndex,int32_t userId)379 ErrCode AppControlManager::DeleteDisposedRule(
380 const std::string &callerName, const std::string &appId, int32_t appIndex, int32_t userId)
381 {
382 auto ret = appControlManagerDb_->DeleteDisposedRule(callerName, appId, appIndex, userId);
383 if (ret != ERR_OK) {
384 LOG_E(BMS_TAG_DEFAULT, "DeleteDisposedRule to rdb failed");
385 return ret;
386 }
387 std::string key = appId + std::string("_") + std::to_string(userId) + std::string("_") + std::to_string(appIndex);
388 {
389 std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
390 auto iter = abilityRunningControlRuleCache_.find(key);
391 if (iter != abilityRunningControlRuleCache_.end()) {
392 abilityRunningControlRuleCache_.erase(iter);
393 }
394 }
395 commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, appIndex);
396 return ERR_OK;
397 }
398
DeleteAllDisposedRuleByBundle(const InnerBundleInfo & bundleInfo,int32_t appIndex,int32_t userId)399 ErrCode AppControlManager::DeleteAllDisposedRuleByBundle(const InnerBundleInfo &bundleInfo, int32_t appIndex,
400 int32_t userId)
401 {
402 std::string appId = bundleInfo.GetAppId();
403 auto ret = appControlManagerDb_->DeleteAllDisposedRuleByBundle(appId, appIndex, userId);
404 if (ret != ERR_OK) {
405 LOG_E(BMS_TAG_DEFAULT, "DeleteAllDisposedRuleByBundle to rdb failed");
406 return ret;
407 }
408 DeleteAbilityRunningRuleBmsCache(appId);
409 std::string key = appId + std::string("_") + std::to_string(userId);
410 DeleteAppRunningRuleCache(key);
411 key = key + std::string("_");
412 std::string cacheKey = key + std::to_string(appIndex);
413 DeleteAbilityRunningRuleCache(cacheKey);
414 commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, appIndex);
415 if (appIndex != Constants::MAIN_APP_INDEX) {
416 return ERR_OK;
417 }
418 // if appIndex is main app clear all clone app cache
419 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
420 if (dataMgr == nullptr) {
421 LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
422 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
423 }
424 std::string bundleName = bundleInfo.GetBundleName();
425 std::vector<int32_t> appIndexVec = dataMgr->GetCloneAppIndexes(bundleName, Constants::ALL_USERID);
426 for (const int32_t index : appIndexVec) {
427 std::string ruleCacheKey = key + std::to_string(index);
428 DeleteAbilityRunningRuleCache(ruleCacheKey);
429 commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, index);
430 }
431 return ERR_OK;
432 }
433
DeleteAppRunningRuleCache(std::string & key)434 void AppControlManager::DeleteAppRunningRuleCache(std::string &key)
435 {
436 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
437 auto iter = appRunningControlRuleResult_.find(key);
438 if (iter != appRunningControlRuleResult_.end()) {
439 appRunningControlRuleResult_.erase(iter);
440 }
441 }
442
DeleteAbilityRunningRuleCache(std::string & key)443 void AppControlManager::DeleteAbilityRunningRuleCache(std::string &key)
444 {
445 std::lock_guard<std::mutex> cacheLock(abilityRunningControlRuleMutex_);
446 auto cacheIter = abilityRunningControlRuleCache_.find(key);
447 if (cacheIter != abilityRunningControlRuleCache_.end()) {
448 abilityRunningControlRuleCache_.erase(cacheIter);
449 }
450 }
451
GetAbilityRunningControlRule(const std::string & bundleName,int32_t appIndex,int32_t userId,std::vector<DisposedRule> & disposedRules)452 ErrCode AppControlManager::GetAbilityRunningControlRule(
453 const std::string &bundleName, int32_t appIndex, int32_t userId, std::vector<DisposedRule> &disposedRules)
454 {
455 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
456 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
457 if (dataMgr == nullptr) {
458 LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
459 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
460 }
461
462 std::string appId;
463 ErrCode ret = dataMgr->GetAppIdByBundleName(bundleName, appId);
464 if (ret != ERR_OK) {
465 LOG_W(BMS_TAG_DEFAULT, "DataMgr GetBundleInfoAppId failed");
466 return ret;
467 }
468 std::string key = appId + std::string("_") + std::to_string(userId) + std::string("_")
469 + std::to_string(appIndex);
470 std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
471 auto iter = abilityRunningControlRuleCache_.find(key);
472 if (iter != abilityRunningControlRuleCache_.end()) {
473 disposedRules = iter->second;
474 return ERR_OK;
475 }
476 ret = appControlManagerDb_->GetAbilityRunningControlRule(appId, appIndex, userId, disposedRules);
477 if (ret != ERR_OK) {
478 LOG_W(BMS_TAG_DEFAULT, "GetAbilityRunningControlRule from rdb failed");
479 return ret;
480 }
481 auto iterBms = abilityRunningControlRuleCacheForBms_.find(appId);
482 if (iterBms != abilityRunningControlRuleCacheForBms_.end()) {
483 LOG_I(BMS_TAG_DEFAULT, "find from bms cache -n %{public}s", bundleName.c_str());
484 disposedRules.emplace_back(iterBms->second);
485 };
486 abilityRunningControlRuleCache_[key] = disposedRules;
487 return ret;
488 }
489
CheckCanDispose(const std::string & appId,int32_t userId)490 bool AppControlManager::CheckCanDispose(const std::string &appId, int32_t userId)
491 {
492 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
493 if (dataMgr == nullptr) {
494 LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
495 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
496 }
497 for (const auto &bundleName : noControllingList_) {
498 BundleInfo bundleInfo;
499 ErrCode ret = dataMgr->GetBundleInfoV9(bundleName,
500 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE), bundleInfo, userId);
501 if (ret != ERR_OK) {
502 continue;
503 }
504 if (appId == bundleInfo.appId) {
505 return false;
506 }
507 }
508 return true;
509 }
510
SetDisposedRuleOnlyForBms(const std::string & appId)511 void AppControlManager::SetDisposedRuleOnlyForBms(const std::string &appId)
512 {
513 std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
514 for (auto iter = abilityRunningControlRuleCache_.begin(); iter != abilityRunningControlRuleCache_.end();) {
515 if (iter->first.find(appId) == 0) {
516 iter = abilityRunningControlRuleCache_.erase(iter);
517 } else {
518 ++iter;
519 }
520 }
521
522 DisposedRule disposedRule;
523 disposedRule.componentType = ComponentType::UI_ABILITY;
524 disposedRule.disposedType = DisposedType::BLOCK_APPLICATION;
525 disposedRule.controlType = ControlType::DISALLOWED_LIST;
526 abilityRunningControlRuleCacheForBms_[appId] = disposedRule;
527 }
528
DeleteDisposedRuleOnlyForBms(const std::string & appId)529 void AppControlManager::DeleteDisposedRuleOnlyForBms(const std::string &appId)
530 {
531 std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
532 for (auto iter = abilityRunningControlRuleCache_.begin(); iter != abilityRunningControlRuleCache_.end();) {
533 if (iter->first.find(appId) == 0) {
534 iter = abilityRunningControlRuleCache_.erase(iter);
535 } else {
536 ++iter;
537 }
538 }
539
540 auto iterBms = abilityRunningControlRuleCacheForBms_.find(appId);
541 if (iterBms != abilityRunningControlRuleCacheForBms_.end()) {
542 abilityRunningControlRuleCacheForBms_.erase(iterBms);
543 }
544 }
545
546
DeleteAbilityRunningRuleBmsCache(const std::string & appId)547 void AppControlManager::DeleteAbilityRunningRuleBmsCache(const std::string &appId)
548 {
549 std::lock_guard<std::mutex> cacheLock(abilityRunningControlRuleMutex_);
550 auto iterBms = abilityRunningControlRuleCacheForBms_.find(appId);
551 if (iterBms != abilityRunningControlRuleCacheForBms_.end()) {
552 abilityRunningControlRuleCacheForBms_.erase(iterBms);
553 }
554 }
555 }
556 }