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 appIdentifier = bundleInfo.GetAppIdentifier();
403 if (!appIdentifier.empty() && appControlManagerDb_) {
404 auto result = appControlManagerDb_->DeleteAllDisposedRuleByBundle(appIdentifier, appIndex, userId);
405 if (result != ERR_OK) {
406 LOG_W(BMS_TAG_DEFAULT, "delete failed. bundleName:%{public}s", bundleInfo.GetBundleName().c_str());
407 }
408 }
409 std::string appId = bundleInfo.GetAppId();
410 auto ret = appControlManagerDb_->DeleteAllDisposedRuleByBundle(appId, appIndex, userId);
411 if (ret != ERR_OK) {
412 LOG_E(BMS_TAG_DEFAULT, "DeleteAllDisposedRuleByBundle to rdb failed");
413 return ret;
414 }
415 DeleteAbilityRunningRuleBmsCache(appId);
416 std::string key = appId + std::string("_") + std::to_string(userId);
417 DeleteAppRunningRuleCache(key);
418 key = key + std::string("_");
419 std::string cacheKey = key + std::to_string(appIndex);
420 DeleteAbilityRunningRuleCache(cacheKey);
421 commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, appIndex);
422 if (appIndex != Constants::MAIN_APP_INDEX) {
423 return ERR_OK;
424 }
425 // if appIndex is main app clear all clone app cache
426 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
427 if (dataMgr == nullptr) {
428 LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
429 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
430 }
431 std::string bundleName = bundleInfo.GetBundleName();
432 std::vector<int32_t> appIndexVec = dataMgr->GetCloneAppIndexes(bundleName, Constants::ALL_USERID);
433 for (const int32_t index : appIndexVec) {
434 std::string ruleCacheKey = key + std::to_string(index);
435 DeleteAbilityRunningRuleCache(ruleCacheKey);
436 commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, index);
437 }
438 return ERR_OK;
439 }
440
DeleteAppRunningRuleCache(std::string & key)441 void AppControlManager::DeleteAppRunningRuleCache(std::string &key)
442 {
443 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
444 auto iter = appRunningControlRuleResult_.find(key);
445 if (iter != appRunningControlRuleResult_.end()) {
446 appRunningControlRuleResult_.erase(iter);
447 }
448 }
449
DeleteAbilityRunningRuleCache(std::string & key)450 void AppControlManager::DeleteAbilityRunningRuleCache(std::string &key)
451 {
452 std::lock_guard<std::mutex> cacheLock(abilityRunningControlRuleMutex_);
453 auto cacheIter = abilityRunningControlRuleCache_.find(key);
454 if (cacheIter != abilityRunningControlRuleCache_.end()) {
455 abilityRunningControlRuleCache_.erase(cacheIter);
456 }
457 }
458
GetAbilityRunningControlRule(const std::string & bundleName,int32_t appIndex,int32_t userId,std::vector<DisposedRule> & disposedRules)459 ErrCode AppControlManager::GetAbilityRunningControlRule(
460 const std::string &bundleName, int32_t appIndex, int32_t userId, std::vector<DisposedRule> &disposedRules)
461 {
462 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
463 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
464 if (dataMgr == nullptr) {
465 LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
466 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
467 }
468
469 std::string appId;
470 ErrCode ret = dataMgr->GetAppIdByBundleName(bundleName, appId);
471 if (ret != ERR_OK) {
472 LOG_W(BMS_TAG_DEFAULT, "DataMgr GetBundleInfoAppId failed");
473 return ret;
474 }
475 std::string key = appId + std::string("_") + std::to_string(userId) + std::string("_")
476 + std::to_string(appIndex);
477 std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
478 auto iter = abilityRunningControlRuleCache_.find(key);
479 if (iter != abilityRunningControlRuleCache_.end()) {
480 disposedRules = iter->second;
481 PrintDisposedRuleInfo(disposedRules);
482 return ERR_OK;
483 }
484 ret = appControlManagerDb_->GetAbilityRunningControlRule(appId, appIndex, userId, disposedRules);
485 if (ret != ERR_OK) {
486 LOG_W(BMS_TAG_DEFAULT, "GetAbilityRunningControlRule from rdb failed");
487 return ret;
488 }
489 auto iterBms = abilityRunningControlRuleCacheForBms_.find(appId);
490 if (iterBms != abilityRunningControlRuleCacheForBms_.end()) {
491 LOG_I(BMS_TAG_DEFAULT, "find from bms cache -n %{public}s", bundleName.c_str());
492 disposedRules.emplace_back(iterBms->second);
493 };
494 abilityRunningControlRuleCache_[key] = disposedRules;
495 PrintDisposedRuleInfo(disposedRules);
496 return ret;
497 }
498
CheckCanDispose(const std::string & appId,int32_t userId)499 bool AppControlManager::CheckCanDispose(const std::string &appId, int32_t userId)
500 {
501 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
502 if (dataMgr == nullptr) {
503 LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
504 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
505 }
506 for (const auto &bundleName : noControllingList_) {
507 BundleInfo bundleInfo;
508 ErrCode ret = dataMgr->GetBundleInfoV9(bundleName,
509 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE), bundleInfo, userId);
510 if (ret != ERR_OK) {
511 continue;
512 }
513 if (appId == bundleInfo.appId) {
514 return false;
515 }
516 }
517 return true;
518 }
519
SetDisposedRuleOnlyForBms(const std::string & appId)520 void AppControlManager::SetDisposedRuleOnlyForBms(const std::string &appId)
521 {
522 std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
523 for (auto iter = abilityRunningControlRuleCache_.begin(); iter != abilityRunningControlRuleCache_.end();) {
524 if (iter->first.find(appId) == 0) {
525 iter = abilityRunningControlRuleCache_.erase(iter);
526 } else {
527 ++iter;
528 }
529 }
530
531 DisposedRule disposedRule;
532 disposedRule.componentType = ComponentType::UI_ABILITY;
533 disposedRule.disposedType = DisposedType::BLOCK_APPLICATION;
534 disposedRule.controlType = ControlType::DISALLOWED_LIST;
535 abilityRunningControlRuleCacheForBms_[appId] = disposedRule;
536 }
537
DeleteDisposedRuleOnlyForBms(const std::string & appId)538 void AppControlManager::DeleteDisposedRuleOnlyForBms(const std::string &appId)
539 {
540 std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
541 for (auto iter = abilityRunningControlRuleCache_.begin(); iter != abilityRunningControlRuleCache_.end();) {
542 if (iter->first.find(appId) == 0) {
543 iter = abilityRunningControlRuleCache_.erase(iter);
544 } else {
545 ++iter;
546 }
547 }
548
549 auto iterBms = abilityRunningControlRuleCacheForBms_.find(appId);
550 if (iterBms != abilityRunningControlRuleCacheForBms_.end()) {
551 abilityRunningControlRuleCacheForBms_.erase(iterBms);
552 }
553 }
554
555
DeleteAbilityRunningRuleBmsCache(const std::string & appId)556 void AppControlManager::DeleteAbilityRunningRuleBmsCache(const std::string &appId)
557 {
558 std::lock_guard<std::mutex> cacheLock(abilityRunningControlRuleMutex_);
559 auto iterBms = abilityRunningControlRuleCacheForBms_.find(appId);
560 if (iterBms != abilityRunningControlRuleCacheForBms_.end()) {
561 abilityRunningControlRuleCacheForBms_.erase(iterBms);
562 }
563 }
564
PrintDisposedRuleInfo(const std::vector<DisposedRule> & disposedRules)565 void AppControlManager::PrintDisposedRuleInfo(const std::vector<DisposedRule> &disposedRules)
566 {
567 for (const auto &rule : disposedRules) {
568 LOG_NOFUNC_I(BMS_TAG_DEFAULT, "control rule caller:%{public}s time:%{public}" PRId64,
569 rule.callerName.c_str(), rule.setTime);
570 }
571 }
572
SetUninstallDisposedRule(const std::string & callerName,const std::string & appIdentifier,const UninstallDisposedRule & rule,int32_t appIndex,int32_t userId)573 ErrCode AppControlManager::SetUninstallDisposedRule(const std::string &callerName, const std::string &appIdentifier,
574 const UninstallDisposedRule& rule, int32_t appIndex, int32_t userId)
575 {
576 auto ret = appControlManagerDb_->SetUninstallDisposedRule(callerName, appIdentifier, rule, appIndex, userId);
577 if (ret != ERR_OK) {
578 LOG_E(BMS_TAG_DEFAULT, "set to rdb failed");
579 return ret;
580 }
581 LOG_I(BMS_TAG_DEFAULT, "%{public}s set uninstall rule, user:%{public}d index:%{public}d",
582 callerName.c_str(), userId, appIndex);
583 return ERR_OK;
584 }
585
GetUninstallDisposedRule(const std::string & appIdentifier,int32_t appIndex,int32_t userId,UninstallDisposedRule & rule)586 ErrCode AppControlManager::GetUninstallDisposedRule(const std::string &appIdentifier, int32_t appIndex,
587 int32_t userId, UninstallDisposedRule& rule)
588 {
589 auto ret = appControlManagerDb_->GetUninstallDisposedRule(appIdentifier, appIndex, userId, rule);
590 if (ret != ERR_OK) {
591 LOG_E(BMS_TAG_DEFAULT, "get from rdb failed");
592 return ret;
593 }
594 return ERR_OK;
595 }
596
DeleteUninstallDisposedRule(const std::string & callerName,const std::string & appIdentifier,int32_t appIndex,int32_t userId)597 ErrCode AppControlManager::DeleteUninstallDisposedRule(
598 const std::string &callerName, const std::string &appIdentifier, int32_t appIndex, int32_t userId)
599 {
600 auto ret = appControlManagerDb_->DeleteUninstallDisposedRule(callerName, appIdentifier, appIndex, userId);
601 if (ret != ERR_OK) {
602 LOG_E(BMS_TAG_DEFAULT, "delete from rdb failed");
603 return ret;
604 }
605 return ERR_OK;
606 }
607 }
608 }