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 constexpr const char* APP_MARKET_CALLING = "app market";
33 constexpr const char* INVALID_MESSAGE = "INVALID_MESSAGE";
34 }
35
AppControlManager()36 AppControlManager::AppControlManager()
37 {
38 appControlManagerDb_ = std::make_shared<AppControlManagerRdb>();
39 commonEventMgr_ = std::make_shared<BundleCommonEventMgr>();
40 std::string configPath = BundleUtil::GetNoDisablingConfigPath();
41 ErrCode ret = BundleParser::ParseNoDisablingList(configPath, noControllingList_);
42 if (ret != ERR_OK) {
43 LOG_W(BMS_TAG_DEFAULT, "GetNoDisablingList failed");
44 }
45 }
46
~AppControlManager()47 AppControlManager::~AppControlManager()
48 {
49 }
50
AddAppInstallControlRule(const std::string & callingName,const std::vector<std::string> & appIds,const std::string & controlRuleType,int32_t userId)51 ErrCode AppControlManager::AddAppInstallControlRule(const std::string &callingName,
52 const std::vector<std::string> &appIds, const std::string &controlRuleType, int32_t userId)
53 {
54 LOG_D(BMS_TAG_DEFAULT, "AddAppInstallControlRule");
55 auto ret = appControlManagerDb_->AddAppInstallControlRule(callingName, appIds, controlRuleType, userId);
56 if ((ret == ERR_OK) && !isAppInstallControlEnabled_) {
57 isAppInstallControlEnabled_ = true;
58 }
59 return ret;
60 }
61
DeleteAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,const std::vector<std::string> & appIds,int32_t userId)62 ErrCode AppControlManager::DeleteAppInstallControlRule(const std::string &callingName,
63 const std::string &controlRuleType, const std::vector<std::string> &appIds, int32_t userId)
64 {
65 LOG_D(BMS_TAG_DEFAULT, "DeleteAppInstallControlRule");
66 auto ret = appControlManagerDb_->DeleteAppInstallControlRule(callingName, controlRuleType, appIds, userId);
67 if ((ret == ERR_OK) && isAppInstallControlEnabled_) {
68 SetAppInstallControlStatus();
69 }
70 return ret;
71 }
72
DeleteAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,int32_t userId)73 ErrCode AppControlManager::DeleteAppInstallControlRule(const std::string &callingName,
74 const std::string &controlRuleType, int32_t userId)
75 {
76 LOG_D(BMS_TAG_DEFAULT, "CleanInstallControlRule");
77 auto ret = appControlManagerDb_->DeleteAppInstallControlRule(callingName, controlRuleType, userId);
78 if ((ret == ERR_OK) && isAppInstallControlEnabled_) {
79 SetAppInstallControlStatus();
80 }
81 return ret;
82 }
83
GetAppInstallControlRule(const std::string & callingName,const std::string & controlRuleType,int32_t userId,std::vector<std::string> & appIds)84 ErrCode AppControlManager::GetAppInstallControlRule(const std::string &callingName,
85 const std::string &controlRuleType, int32_t userId, std::vector<std::string> &appIds)
86 {
87 LOG_D(BMS_TAG_DEFAULT, "GetAppInstallControlRule");
88 return appControlManagerDb_->GetAppInstallControlRule(callingName, controlRuleType, userId, appIds);
89 }
90
AddAppRunningControlRule(const std::string & callingName,const std::vector<AppRunningControlRule> & controlRules,int32_t userId)91 ErrCode AppControlManager::AddAppRunningControlRule(const std::string &callingName,
92 const std::vector<AppRunningControlRule> &controlRules, int32_t userId)
93 {
94 LOG_D(BMS_TAG_DEFAULT, "AddAppRunningControlRule");
95 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
96 ErrCode ret = appControlManagerDb_->AddAppRunningControlRule(callingName, controlRules, userId);
97 if (ret == ERR_OK) {
98 for (const auto &rule : controlRules) {
99 std::string key = rule.appId + std::string("_") + std::to_string(userId);
100 auto iter = appRunningControlRuleResult_.find(key);
101 if (iter != appRunningControlRuleResult_.end()) {
102 appRunningControlRuleResult_.erase(iter);
103 }
104 }
105 KillRunningApp(controlRules, userId);
106 }
107 return ret;
108 }
109
DeleteAppRunningControlRule(const std::string & callingName,const std::vector<AppRunningControlRule> & controlRules,int32_t userId)110 ErrCode AppControlManager::DeleteAppRunningControlRule(const std::string &callingName,
111 const std::vector<AppRunningControlRule> &controlRules, int32_t userId)
112 {
113 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
114 auto ret = appControlManagerDb_->DeleteAppRunningControlRule(callingName, controlRules, userId);
115 if (ret == ERR_OK) {
116 for (const auto &rule : controlRules) {
117 std::string key = rule.appId + std::string("_") + std::to_string(userId);
118 auto iter = appRunningControlRuleResult_.find(key);
119 if (iter != appRunningControlRuleResult_.end()) {
120 appRunningControlRuleResult_.erase(iter);
121 }
122 }
123 }
124 return ret;
125 }
126
DeleteAppRunningControlRule(const std::string & callingName,int32_t userId)127 ErrCode AppControlManager::DeleteAppRunningControlRule(const std::string &callingName, int32_t userId)
128 {
129 ErrCode res = appControlManagerDb_->DeleteAppRunningControlRule(callingName, userId);
130 if (res != ERR_OK) {
131 LOG_E(BMS_TAG_DEFAULT, "DeleteAppRunningControlRule failed");
132 return res;
133 }
134 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
135 for (auto it = appRunningControlRuleResult_.begin(); it != appRunningControlRuleResult_.end();) {
136 std::string key = it->first;
137 std::string targetUserId = std::to_string(userId);
138 if (key.size() >= targetUserId.size() &&
139 key.compare(key.size() - targetUserId.size(), targetUserId.size(), targetUserId) == 0) {
140 it = appRunningControlRuleResult_.erase(it);
141 } else {
142 ++it;
143 }
144 }
145 return res;
146 }
147
GetAppRunningControlRule(const std::string & callingName,int32_t userId,std::vector<std::string> & appIds)148 ErrCode AppControlManager::GetAppRunningControlRule(
149 const std::string &callingName, int32_t userId, std::vector<std::string> &appIds)
150 {
151 return appControlManagerDb_->GetAppRunningControlRule(callingName, userId, appIds);
152 }
153
ConfirmAppJumpControlRule(const std::string & callerBundleName,const std::string & targetBundleName,int32_t userId)154 ErrCode AppControlManager::ConfirmAppJumpControlRule(const std::string &callerBundleName,
155 const std::string &targetBundleName, int32_t userId)
156 {
157 if (appJumpInterceptorManagerDb_ == nullptr) {
158 LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
159 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
160 }
161 return appJumpInterceptorManagerDb_->ConfirmAppJumpControlRule(callerBundleName, targetBundleName, userId);
162 }
163
AddAppJumpControlRule(const std::vector<AppJumpControlRule> & controlRules,int32_t userId)164 ErrCode AppControlManager::AddAppJumpControlRule(const std::vector<AppJumpControlRule> &controlRules, 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_->AddAppJumpControlRule(controlRules, userId);
171 }
172
DeleteAppJumpControlRule(const std::vector<AppJumpControlRule> & controlRules,int32_t userId)173 ErrCode AppControlManager::DeleteAppJumpControlRule(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_->DeleteAppJumpControlRule(controlRules, userId);
180 }
181
DeleteRuleByCallerBundleName(const std::string & callerBundleName,int32_t userId)182 ErrCode AppControlManager::DeleteRuleByCallerBundleName(const std::string &callerBundleName, 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_->DeleteRuleByCallerBundleName(callerBundleName, userId);
189 }
190
DeleteRuleByTargetBundleName(const std::string & targetBundleName,int32_t userId)191 ErrCode AppControlManager::DeleteRuleByTargetBundleName(const std::string &targetBundleName, 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_->DeleteRuleByTargetBundleName(targetBundleName, userId);
198 }
199
GetAppJumpControlRule(const std::string & callerBundleName,const std::string & targetBundleName,int32_t userId,AppJumpControlRule & controlRule)200 ErrCode AppControlManager::GetAppJumpControlRule(const std::string &callerBundleName,
201 const std::string &targetBundleName, int32_t userId, AppJumpControlRule &controlRule)
202 {
203 if (appJumpInterceptorManagerDb_ == nullptr) {
204 LOG_E(BMS_TAG_DEFAULT, "DataMgr rdb is not init");
205 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
206 }
207 auto ret = appJumpInterceptorManagerDb_->GetAppJumpControlRule(callerBundleName, targetBundleName,
208 userId, controlRule);
209 return ret;
210 }
211
SetDisposedStatus(const std::string & appId,const Want & want,int32_t userId)212 ErrCode AppControlManager::SetDisposedStatus(const std::string &appId, const Want& want, int32_t userId)
213 {
214 if (!CheckCanDispose(appId, userId)) {
215 LOG_E(BMS_TAG_DEFAULT, "appid in white-list");
216 return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
217 }
218 auto ret = appControlManagerDb_->SetDisposedStatus(APP_MARKET_CALLING, appId, want, userId);
219 if (ret != ERR_OK) {
220 LOG_E(BMS_TAG_DEFAULT, "SetDisposedStatus to rdb failed");
221 return ret;
222 }
223 std::string key = appId + std::string("_") + std::to_string(userId);
224 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
225 auto iter = appRunningControlRuleResult_.find(key);
226 if (iter != appRunningControlRuleResult_.end()) {
227 appRunningControlRuleResult_.erase(iter);
228 }
229 commonEventMgr_->NotifySetDiposedRule(appId, userId, want.ToString(), Constants::MAIN_APP_INDEX);
230 return ERR_OK;
231 }
232
DeleteDisposedStatus(const std::string & appId,int32_t userId)233 ErrCode AppControlManager::DeleteDisposedStatus(const std::string &appId, int32_t userId)
234 {
235 auto ret = appControlManagerDb_->DeleteDisposedStatus(APP_MARKET_CALLING, appId, userId);
236 if (ret != ERR_OK) {
237 LOG_E(BMS_TAG_DEFAULT, "DeleteDisposedStatus to rdb failed");
238 return ret;
239 }
240 std::string key = appId + std::string("_") + std::to_string(userId);
241 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
242 auto iter = appRunningControlRuleResult_.find(key);
243 if (iter != appRunningControlRuleResult_.end()) {
244 appRunningControlRuleResult_.erase(iter);
245 }
246 commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, Constants::MAIN_APP_INDEX);
247 return ERR_OK;
248 }
249
GetDisposedStatus(const std::string & appId,Want & want,int32_t userId)250 ErrCode AppControlManager::GetDisposedStatus(const std::string &appId, Want& want, int32_t userId)
251 {
252 return appControlManagerDb_->GetDisposedStatus(
253 APP_MARKET_CALLING, appId, want, userId);
254 }
255
GetAppRunningControlRule(const std::string & bundleName,int32_t userId,AppRunningControlRuleResult & controlRuleResult)256 ErrCode AppControlManager::GetAppRunningControlRule(
257 const std::string &bundleName, int32_t userId, AppRunningControlRuleResult &controlRuleResult)
258 {
259 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
260 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
261 if (dataMgr == nullptr) {
262 LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
263 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
264 }
265 std::string appId;
266 ErrCode ret = dataMgr->GetAppIdByBundleName(bundleName, appId);
267 if (ret != ERR_OK) {
268 LOG_W(BMS_TAG_DEFAULT, "getAppId failed,-n:%{public}s", bundleName.c_str());
269 return ret;
270 }
271 std::string key = appId + std::string("_") + std::to_string(userId);
272 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
273 if (appRunningControlRuleResult_.find(key) != appRunningControlRuleResult_.end()) {
274 controlRuleResult = appRunningControlRuleResult_[key];
275 if (controlRuleResult.controlMessage == INVALID_MESSAGE) {
276 controlRuleResult.controlMessage = std::string();
277 return ERR_BUNDLE_MANAGER_BUNDLE_NOT_SET_CONTROL;
278 }
279 return ERR_OK;
280 }
281 ret = appControlManagerDb_->GetAppRunningControlRule(appId, userId, controlRuleResult);
282 if (ret != ERR_OK) {
283 controlRuleResult.controlMessage = INVALID_MESSAGE;
284 }
285 appRunningControlRuleResult_.emplace(key, controlRuleResult);
286 return ret;
287 }
288
KillRunningApp(const std::vector<AppRunningControlRule> & rules,int32_t userId) const289 void AppControlManager::KillRunningApp(const std::vector<AppRunningControlRule> &rules, int32_t userId) const
290 {
291 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
292 if (dataMgr == nullptr) {
293 LOG_E(BMS_TAG_DEFAULT, "dataMgr is null");
294 return;
295 }
296 std::for_each(rules.cbegin(), rules.cend(), [&dataMgr, userId](const auto &rule) {
297 std::string bundleName = dataMgr->GetBundleNameByAppId(rule.appId);
298 if (bundleName.empty()) {
299 LOG_W(BMS_TAG_DEFAULT, "GetBundleNameByAppId failed");
300 return;
301 }
302 BundleInfo bundleInfo;
303 ErrCode ret = dataMgr->GetBundleInfoV9(
304 bundleName, static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE), bundleInfo, userId);
305 if (ret != ERR_OK) {
306 LOG_W(BMS_TAG_DEFAULT, "GetBundleInfoV9 failed");
307 return;
308 }
309 AbilityManagerHelper::UninstallApplicationProcesses(bundleName, bundleInfo.uid);
310 });
311 }
312
IsAppInstallControlEnabled() const313 bool AppControlManager::IsAppInstallControlEnabled() const
314 {
315 return isAppInstallControlEnabled_;
316 }
317
SetAppInstallControlStatus()318 void AppControlManager::SetAppInstallControlStatus()
319 {
320 isAppInstallControlEnabled_ = false;
321 std::vector<std::string> appIds;
322 int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
323 if (currentUserId == Constants::INVALID_USERID) {
324 currentUserId = Constants::START_USERID;
325 }
326 ErrCode ret = appControlManagerDb_->GetAppInstallControlRule(AppControlConstants::EDM_CALLING,
327 AppControlConstants::APP_DISALLOWED_UNINSTALL, currentUserId, appIds);
328 if ((ret == ERR_OK) && !appIds.empty()) {
329 isAppInstallControlEnabled_ = true;
330 }
331 }
332
SetDisposedRule(const std::string & callerName,const std::string & appId,const DisposedRule & rule,int32_t appIndex,int32_t userId)333 ErrCode AppControlManager::SetDisposedRule(const std::string &callerName, const std::string &appId,
334 const DisposedRule& rule, int32_t appIndex, int32_t userId)
335 {
336 if (!CheckCanDispose(appId, userId)) {
337 LOG_E(BMS_TAG_DEFAULT, "appid in white-list");
338 return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
339 }
340 auto ret = appControlManagerDb_->SetDisposedRule(callerName, appId, rule, appIndex, userId);
341 if (ret != ERR_OK) {
342 LOG_E(BMS_TAG_DEFAULT, "SetDisposedStatus to rdb failed");
343 return ret;
344 }
345 std::string key = appId + std::string("_") + std::to_string(userId) + std::string("_") + std::to_string(appIndex);
346 {
347 std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
348 auto iter = abilityRunningControlRuleCache_.find(key);
349 if (iter != abilityRunningControlRuleCache_.end()) {
350 abilityRunningControlRuleCache_.erase(iter);
351 }
352 }
353 LOG_I(BMS_TAG_DEFAULT, "%{public}s set rule, user:%{public}d index:%{public}d",
354 callerName.c_str(), userId, appIndex);
355 commonEventMgr_->NotifySetDiposedRule(appId, userId, rule.ToString(), appIndex);
356 return ERR_OK;
357 }
358
GetDisposedRule(const std::string & callerName,const std::string & appId,DisposedRule & rule,int32_t appIndex,int32_t userId)359 ErrCode AppControlManager::GetDisposedRule(
360 const std::string &callerName, const std::string &appId, DisposedRule& rule, int32_t appIndex, int32_t userId)
361 {
362 auto ret = appControlManagerDb_->GetDisposedRule(callerName, appId, rule, appIndex, userId);
363 if (ret != ERR_OK) {
364 LOG_E(BMS_TAG_DEFAULT, "GetDisposedRule to rdb failed");
365 return ret;
366 }
367 return ERR_OK;
368 }
369
DeleteDisposedRule(const std::string & callerName,const std::string & appId,int32_t appIndex,int32_t userId)370 ErrCode AppControlManager::DeleteDisposedRule(
371 const std::string &callerName, const std::string &appId, int32_t appIndex, int32_t userId)
372 {
373 auto ret = appControlManagerDb_->DeleteDisposedRule(callerName, appId, appIndex, userId);
374 if (ret != ERR_OK) {
375 LOG_E(BMS_TAG_DEFAULT, "DeleteDisposedRule to rdb failed");
376 return ret;
377 }
378 std::string key = appId + std::string("_") + std::to_string(userId) + std::string("_") + std::to_string(appIndex);
379 {
380 std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
381 auto iter = abilityRunningControlRuleCache_.find(key);
382 if (iter != abilityRunningControlRuleCache_.end()) {
383 abilityRunningControlRuleCache_.erase(iter);
384 }
385 }
386 commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, appIndex);
387 return ERR_OK;
388 }
389
DeleteAllDisposedRuleByBundle(const InnerBundleInfo & bundleInfo,int32_t appIndex,int32_t userId)390 ErrCode AppControlManager::DeleteAllDisposedRuleByBundle(const InnerBundleInfo &bundleInfo, int32_t appIndex,
391 int32_t userId)
392 {
393 std::string appIdentifier = bundleInfo.GetAppIdentifier();
394 if (!appIdentifier.empty() && appControlManagerDb_) {
395 auto result = appControlManagerDb_->DeleteAllDisposedRuleByBundle(appIdentifier, appIndex, userId);
396 if (result != ERR_OK) {
397 LOG_W(BMS_TAG_DEFAULT, "delete failed. bundleName:%{public}s", bundleInfo.GetBundleName().c_str());
398 }
399 }
400 std::string appId = bundleInfo.GetAppId();
401 auto ret = appControlManagerDb_->DeleteAllDisposedRuleByBundle(appId, appIndex, userId);
402 if (ret != ERR_OK) {
403 LOG_E(BMS_TAG_DEFAULT, "DeleteAllDisposedRuleByBundle to rdb failed");
404 return ret;
405 }
406 DeleteAbilityRunningRuleBmsCache(appId);
407 std::string key = appId + std::string("_") + std::to_string(userId);
408 DeleteAppRunningRuleCache(key);
409 key = key + std::string("_");
410 std::string cacheKey = key + std::to_string(appIndex);
411 DeleteAbilityRunningRuleCache(cacheKey);
412 commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, appIndex);
413 if (appIndex != Constants::MAIN_APP_INDEX) {
414 return ERR_OK;
415 }
416 // if appIndex is main app clear all clone app cache
417 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
418 if (dataMgr == nullptr) {
419 LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
420 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
421 }
422 std::string bundleName = bundleInfo.GetBundleName();
423 std::vector<int32_t> appIndexVec = dataMgr->GetCloneAppIndexes(bundleName, Constants::ALL_USERID);
424 for (const int32_t index : appIndexVec) {
425 std::string ruleCacheKey = key + std::to_string(index);
426 DeleteAbilityRunningRuleCache(ruleCacheKey);
427 commonEventMgr_->NotifyDeleteDiposedRule(appId, userId, index);
428 }
429 return ERR_OK;
430 }
431
DeleteAppRunningRuleCache(std::string & key)432 void AppControlManager::DeleteAppRunningRuleCache(std::string &key)
433 {
434 std::lock_guard<std::mutex> lock(appRunningControlMutex_);
435 auto iter = appRunningControlRuleResult_.find(key);
436 if (iter != appRunningControlRuleResult_.end()) {
437 appRunningControlRuleResult_.erase(iter);
438 }
439 }
440
DeleteAbilityRunningRuleCache(std::string & key)441 void AppControlManager::DeleteAbilityRunningRuleCache(std::string &key)
442 {
443 std::lock_guard<std::mutex> cacheLock(abilityRunningControlRuleMutex_);
444 auto cacheIter = abilityRunningControlRuleCache_.find(key);
445 if (cacheIter != abilityRunningControlRuleCache_.end()) {
446 abilityRunningControlRuleCache_.erase(cacheIter);
447 }
448 }
449
GetAbilityRunningControlRule(const std::string & bundleName,int32_t appIndex,int32_t userId,std::vector<DisposedRule> & disposedRules)450 ErrCode AppControlManager::GetAbilityRunningControlRule(
451 const std::string &bundleName, int32_t appIndex, int32_t userId, std::vector<DisposedRule> &disposedRules)
452 {
453 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
454 auto dataMgr = DelayedSingleton<BundleMgrService>::GetInstance()->GetDataMgr();
455 if (dataMgr == nullptr) {
456 LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr");
457 return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
458 }
459
460 std::string appId;
461 ErrCode ret = dataMgr->GetAppIdByBundleName(bundleName, appId);
462 if (ret != ERR_OK) {
463 LOG_W(BMS_TAG_DEFAULT, "DataMgr GetBundleInfoAppId failed");
464 return ret;
465 }
466 std::string key = appId + std::string("_") + std::to_string(userId) + std::string("_")
467 + std::to_string(appIndex);
468 std::lock_guard<std::mutex> lock(abilityRunningControlRuleMutex_);
469 auto iter = abilityRunningControlRuleCache_.find(key);
470 if (iter != abilityRunningControlRuleCache_.end()) {
471 disposedRules = iter->second;
472 PrintDisposedRuleInfo(disposedRules);
473 return ERR_OK;
474 }
475 ret = appControlManagerDb_->GetAbilityRunningControlRule(appId, appIndex, userId, disposedRules);
476 if (ret != ERR_OK) {
477 LOG_W(BMS_TAG_DEFAULT, "GetAbilityRunningControlRule from rdb failed");
478 return ret;
479 }
480 auto iterBms = abilityRunningControlRuleCacheForBms_.find(appId);
481 if (iterBms != abilityRunningControlRuleCacheForBms_.end()) {
482 LOG_I(BMS_TAG_DEFAULT, "find from bms cache -n %{public}s", bundleName.c_str());
483 disposedRules.emplace_back(iterBms->second);
484 };
485 abilityRunningControlRuleCache_[key] = disposedRules;
486 PrintDisposedRuleInfo(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
SetUninstallDisposedRule(const std::string & callerName,const std::string & appIdentifier,const UninstallDisposedRule & rule,int32_t appIndex,int32_t userId)556 ErrCode AppControlManager::SetUninstallDisposedRule(const std::string &callerName, const std::string &appIdentifier,
557 const UninstallDisposedRule& rule, int32_t appIndex, int32_t userId)
558 {
559 auto ret = appControlManagerDb_->SetUninstallDisposedRule(callerName, appIdentifier, rule, appIndex, userId);
560 if (ret != ERR_OK) {
561 LOG_E(BMS_TAG_DEFAULT, "set to rdb failed");
562 return ret;
563 }
564 LOG_I(BMS_TAG_DEFAULT, "%{public}s set uninstall rule, user:%{public}d index:%{public}d",
565 callerName.c_str(), userId, appIndex);
566 return ERR_OK;
567 }
568
GetUninstallDisposedRule(const std::string & appIdentifier,int32_t appIndex,int32_t userId,UninstallDisposedRule & rule)569 ErrCode AppControlManager::GetUninstallDisposedRule(const std::string &appIdentifier, int32_t appIndex,
570 int32_t userId, UninstallDisposedRule& rule)
571 {
572 auto ret = appControlManagerDb_->GetUninstallDisposedRule(appIdentifier, appIndex, userId, rule);
573 if (ret != ERR_OK) {
574 LOG_E(BMS_TAG_DEFAULT, "get from rdb failed");
575 return ret;
576 }
577 return ERR_OK;
578 }
579
DeleteUninstallDisposedRule(const std::string & callerName,const std::string & appIdentifier,int32_t appIndex,int32_t userId)580 ErrCode AppControlManager::DeleteUninstallDisposedRule(
581 const std::string &callerName, const std::string &appIdentifier, int32_t appIndex, int32_t userId)
582 {
583 auto ret = appControlManagerDb_->DeleteUninstallDisposedRule(callerName, appIdentifier, appIndex, userId);
584 if (ret != ERR_OK) {
585 LOG_E(BMS_TAG_DEFAULT, "delete from rdb failed");
586 return ret;
587 }
588 return ERR_OK;
589 }
590
PrintDisposedRuleInfo(const std::vector<DisposedRule> & disposedRules)591 void AppControlManager::PrintDisposedRuleInfo(const std::vector<DisposedRule> &disposedRules)
592 {
593 for (const auto &rule : disposedRules) {
594 LOG_NOFUNC_I(BMS_TAG_DEFAULT, "control rule caller:%{public}s time:%{public}" PRId64,
595 rule.callerName.c_str(), rule.setTime);
596 }
597 }
598 }
599 }