diff --git a/services/bundlemgr/include/bundle_mgr_service_event_handler.h b/services/bundlemgr/include/bundle_mgr_service_event_handler.h index 7ba8c3ea66a0c5edaf0924080c64f65a14f02eb8..b6108e3443bcca41d0945b2e95130336571ad52f 100644 --- a/services/bundlemgr/include/bundle_mgr_service_event_handler.h +++ b/services/bundlemgr/include/bundle_mgr_service_event_handler.h @@ -495,6 +495,10 @@ private: void InnerProcessCheckCloudShaderDir(); void ProcessNewBackupDir(); + void PrepareBundleDirQuota(const std::string &bundleName, const int32_t uid, + const std::string &bundleDataDirPath, const int32_t limitSize) const; + void RefreshQuotaForAllUid(); + bool InnerProcessUninstallModule(const BundleInfo &bundleInfo, const std::unordered_map &infos); diff --git a/services/bundlemgr/src/bundle_mgr_service_event_handler.cpp b/services/bundlemgr/src/bundle_mgr_service_event_handler.cpp index fe864adc84df16875aff11afb56d3b93ef78f178..59ae2a18f3ff9f58b91712556b220ec3f621b532 100644 --- a/services/bundlemgr/src/bundle_mgr_service_event_handler.cpp +++ b/services/bundlemgr/src/bundle_mgr_service_event_handler.cpp @@ -63,6 +63,10 @@ #include "want.h" #include "user_unlocked_event_subscriber.h" #include "json_util.h" +#ifdef STORAGE_SERVICE_ENABLE +#include "storage_manager_proxy.h" +#include "iservice_registry.h" +#endif namespace OHOS { namespace AppExecFwk { @@ -113,6 +117,16 @@ std::set extensiontype_; bool hasLoadPreInstallProFile_ = false; std::vector bundleNameList_; +#ifdef STORAGE_SERVICE_ENABLE +#ifdef QUOTA_PARAM_SET_ENABLE +const std::string SYSTEM_PARAM_ATOMICSERVICE_DATASIZE_THRESHOLD = + "persist.sys.bms.aging.policy.atomicservice.datasize.threshold"; +const int32_t THRESHOLD_VAL_LEN = 20; +#endif // QUOTA_PARAM_SET_ENABLE +const int32_t STORAGE_MANAGER_MANAGER_ID = 5003; +#endif // STORAGE_SERVICE_ENABLE +const int32_t ATOMIC_SERVICE_DATASIZE_THRESHOLD_MB_PRESET = 200; + void MoveTempPath(const std::vector &fromPaths, const std::string &bundleName, std::vector &toPaths) { @@ -1089,6 +1103,7 @@ void BMSEventHandler::ProcessRebootBundle() ProcessCheckShaderCacheDir(); ProcessCheckCloudShaderDir(); ProcessNewBackupDir(); + RefreshQuotaForAllUid(); } void BMSEventHandler::ProcessRebootDeleteArkAp() @@ -1451,6 +1466,93 @@ void BMSEventHandler::InnerProcessCheckCloudShaderDir() LOG_I(BMS_TAG_DEFAULT, "Create cloud shader cache result: %{public}d", result); } +static void SendToStorageQuota(const std::string &bundleName, const int32_t uid, + const std::string &bundleDataDirPath, const int32_t limitSizeMb) +{ +#ifdef STORAGE_SERVICE_ENABLE + auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + LOG_W(BMS_TAG_DEFAULT, "SendToStorageQuota, systemAbilityManager error"); + return; + } + + auto remote = systemAbilityManager->CheckSystemAbility(STORAGE_MANAGER_MANAGER_ID); + if (!remote) { + LOG_W(BMS_TAG_DEFAULT, "SendToStorageQuota, CheckSystemAbility error"); + return; + } + + auto proxy = iface_cast(remote); + if (!proxy) { + LOG_W(BMS_TAG_DEFAULT, "SendToStorageQuotactl, proxy get error"); + return; + } + + LOG_I(BMS_TAG_DEFAULT, "SendToStorageQuota bundleName=%{public}s, uid=%{public}d, bundleDataDirPath=%{public}s," + "limitSizeMb=%{public}d", bundleName.c_str(), uid, bundleDataDirPath.c_str(), limitSizeMb); + int err = proxy->SetBundleQuota(bundleName, uid, bundleDataDirPath, limitSizeMb); + if (err != ERR_OK) { + LOG_W(BMS_TAG_DEFAULT, "SendToStorageQuota, SetBundleQuota error, err=%{public}d, uid=%{public}d", err, uid); + } +#endif // STORAGE_SERVICE_ENABLE +} + +void BMSEventHandler::PrepareBundleDirQuota(const std::string &bundleName, const int32_t uid, + const std::string &bundleDataDirPath, const int32_t limitSize) const +{ + if (limitSize == 0) { + LOG_I(BMS_TAG_DEFAULT, "cancel bundleName:%{public}s uid:%{public}d quota", bundleName.c_str(), uid); + SendToStorageQuota(bundleName, uid, bundleDataDirPath, 0); + return; + } + int32_t atomicserviceDatasizeThreshold = limitSize; +#ifdef STORAGE_SERVICE_ENABLE +#ifdef QUOTA_PARAM_SET_ENABLE + char szAtomicDatasizeThresholdMb[THRESHOLD_VAL_LEN] = {0}; + int32_t ret = GetParameter(SYSTEM_PARAM_ATOMICSERVICE_DATASIZE_THRESHOLD.c_str(), "", + szAtomicDatasizeThresholdMb, THRESHOLD_VAL_LEN); + if (ret <= 0) { + LOG_I(BMS_TAG_DEFAULT, "GetParameter failed"); + } else if (strcmp(szAtomicDatasizeThresholdMb, "") != 0) { + atomicserviceDatasizeThreshold = atoi(szAtomicDatasizeThresholdMb); + LOG_I(BMS_TAG_DEFAULT, "InstalldQuotaUtils init atomicserviceDataThreshold mb success"); + } + if (atomicserviceDatasizeThreshold <= 0) { + LOG_W(BMS_TAG_DEFAULT, "no need to prepare quota"); + return; + } +#endif // QUOTA_PARAM_SET_ENABLE +#endif // STORAGE_SERVICE_ENABLE + SendToStorageQuota(bundleName, uid, bundleDataDirPath, atomicserviceDatasizeThreshold); +} + +void BMSEventHandler::RefreshQuotaForAllUid() +{ + auto dataMgr = DelayedSingleton::GetInstance()->GetDataMgr(); + if (dataMgr == nullptr) { + LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr"); + return; + } + std::map infos = dataMgr->GetAllInnerBundleInfos(); + for (auto &infoPair : infos) { + auto &info = infoPair.second; + std::map userInfos = info.GetInnerBundleUserInfos(); + for (auto &userInfoPair : userInfos) { + auto &userInfo = userInfoPair.second; + std::string bundleDataDir = ServiceConstants::BUNDLE_APP_DATA_BASE_DIR + ServiceConstants::BUNDLE_EL[0] + + ServiceConstants::PATH_SEPARATOR + std::to_string(userInfo.bundleUserInfo.userId) + + ServiceConstants::BASE + info.GetBundleName(); + if (info.GetApplicationBundleType() != BundleType::ATOMIC_SERVICE) { + PrepareBundleDirQuota(info.GetBundleName(), info.GetUid(userInfo.bundleUserInfo.userId), + bundleDataDir, 0); + } else { + PrepareBundleDirQuota(info.GetBundleName(), info.GetUid(userInfo.bundleUserInfo.userId), + bundleDataDir, ATOMIC_SERVICE_DATASIZE_THRESHOLD_MB_PRESET); + } + } + } +} + bool BMSEventHandler::LoadAllPreInstallBundleInfos() { if (hasLoadAllPreInstallBundleInfosFromDb_) {