diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index d91c14327aff7dbaea2cb0cc5de5f86fdd68d845..cf33fee8da661c367e4a8b53b0ec0024910231bb 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -251,6 +251,9 @@ private: void DumpUsage(std::string &result); bool AllowDump(); int32_t ShellDump(const std::vector &dumpOption, std::vector &dumpInfo); + int32_t DumpEvents(const std::vector &dumpOption, std::vector &dumpInfo); + int32_t DumpPackageUsage(const std::vector &dumpOption, std::vector &dumpInfo); + int32_t DumpModuleUsage(const std::vector &dumpOption, std::vector &dumpInfo); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index d187bea37c0502c50ff05cae31d321a1494afee3..d9203cabafbb4004c1ef56c92778eb0ba01194af 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -32,6 +32,7 @@ #include "bundle_active_shutdown_callback_service.h" #include "tokenid_kit.h" #include "xcollie/watchdog.h" +#include "bundle_active_util.h" #include "bundle_active_service.h" @@ -546,13 +547,8 @@ ErrCode BundleActiveService::CheckBundleIsSystemAppAndHasPermission(const int32_ ErrCode BundleActiveService::CheckNativePermission(OHOS::Security::AccessToken::AccessTokenID tokenId) { - int32_t ret = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, "ohos.permission.DUMP"); - if (ret == Security::AccessToken::PermissionState::PERMISSION_GRANTED) { - BUNDLE_ACTIVE_LOGD("check native permission success, request from dump"); - return ERR_OK; - } int32_t bundleHasPermission = AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, NEEDED_PERMISSION); - if (bundleHasPermission != 0) { + if (bundleHasPermission != Security::AccessToken::PermissionState::PERMISSION_GRANTED) { BUNDLE_ACTIVE_LOGE("check native permission not have permission"); return ERR_PERMISSION_DENIED; } @@ -743,46 +739,76 @@ int32_t BundleActiveService::Dump(int32_t fd, const std::vector int32_t BundleActiveService::ShellDump(const std::vector &dumpOption, std::vector &dumpInfo) { int32_t ret = -1; + if (!bundleActiveCore_) { + return ret; + } if (dumpOption[1] == "Events") { - std::vector eventResult; - if (static_cast(dumpOption.size()) != EVENTS_PARAM) { - return ret; - } - int64_t beginTime = std::stoll(dumpOption[2]); - int64_t endTime = std::stoll(dumpOption[3]); - int32_t userId = std::stoi(dumpOption[4]); - this->QueryBundleEvents(eventResult, beginTime, endTime, userId); - for (auto& oneEvent : eventResult) { - dumpInfo.emplace_back(oneEvent.ToString()); - } + ret = DumpEvents(dumpOption, dumpInfo); } else if (dumpOption[1] == "PackageUsage") { - std::vector packageUsageResult; - if (static_cast(dumpOption.size()) != PACKAGE_USAGE_PARAM) { - return ret; - } - int32_t intervalType = std::stoi(dumpOption[2]); - int64_t beginTime = std::stoll(dumpOption[3]); - int64_t endTime = std::stoll(dumpOption[4]); - int32_t userId = std::stoi(dumpOption[5]); - this->QueryBundleStatsInfoByInterval(packageUsageResult, intervalType, beginTime, endTime, userId); - for (auto& onePackageRecord : packageUsageResult) { - dumpInfo.emplace_back(onePackageRecord.ToString()); - } + ret = DumpPackageUsage(dumpOption, dumpInfo); } else if (dumpOption[1] == "ModuleUsage") { - std::vector moduleResult; - if (static_cast(dumpOption.size()) != MODULE_USAGE_PARAM) { - return ret; - } - int32_t maxNum = std::stoi(dumpOption[2]); - int32_t userId = std::stoi(dumpOption[3]); - BUNDLE_ACTIVE_LOGI("M is %{public}d, u is %{public}d", maxNum, userId); - ret = this->QueryModuleUsageRecords(maxNum, moduleResult, userId); - for (auto& oneModuleRecord : moduleResult) { - dumpInfo.emplace_back(oneModuleRecord.ToString()); - for (uint32_t i = 0; i < oneModuleRecord.formRecords_.size(); i++) { - std::string oneFormInfo = "form " + std::to_string(static_cast(i) + 1) + ", "; - dumpInfo.emplace_back(oneFormInfo + oneModuleRecord.formRecords_[i].ToString()); - } + ret = DumpModuleUsage(dumpOption, dumpInfo); + } + return ret; +} + +int32_t BundleActiveService::DumpEvents(const std::vector &dumpOption, std::vector &dumpInfo) +{ + int32_t ret = -1; + std::vector eventResult; + if (static_cast(dumpOption.size()) != EVENTS_PARAM) { + return ret; + } + int64_t beginTime = BundleActiveUtil::StringToInt64(dumpOption[2]); + int64_t endTime = BundleActiveUtil::StringToInt64(dumpOption[3]); + int32_t userId = BundleActiveUtil::StringToInt32(dumpOption[4]); + bundleActiveCore_->QueryBundleEvents(eventResult, userId, beginTime, endTime, ""); + for (auto& oneEvent : eventResult) { + dumpInfo.emplace_back(oneEvent.ToString()); + } + return ret; +} + +int32_t BundleActiveService::DumpPackageUsage(const std::vector &dumpOption, + std::vector &dumpInfo) +{ + int32_t ret = -1; + std::vector packageUsageResult; + if (static_cast(dumpOption.size()) != PACKAGE_USAGE_PARAM) { + return ret; + } + int32_t intervalType = ConvertIntervalType(BundleActiveUtil::StringToInt32(dumpOption[2])); + int64_t beginTime = BundleActiveUtil::StringToInt64(dumpOption[3]); + int64_t endTime = BundleActiveUtil::StringToInt64(dumpOption[4]); + int32_t userId = BundleActiveUtil::StringToInt32(dumpOption[5]); + bundleActiveCore_->QueryBundleStatsInfos( + packageUsageResult, userId, intervalType, beginTime, endTime, ""); + for (auto& onePackageRecord : packageUsageResult) { + dumpInfo.emplace_back(onePackageRecord.ToString()); + } + return ret; +} + +int32_t BundleActiveService::DumpModuleUsage(const std::vector &dumpOption, + std::vector &dumpInfo) +{ + int32_t ret = -1; + std::vector moduleResult; + if (static_cast(dumpOption.size()) != MODULE_USAGE_PARAM) { + return ret; + } + int32_t maxNum = BundleActiveUtil::StringToInt32(dumpOption[2]); + int32_t userId = BundleActiveUtil::StringToInt32(dumpOption[3]); + BUNDLE_ACTIVE_LOGI("M is %{public}d, u is %{public}d", maxNum, userId); + ret = bundleActiveCore_->QueryModuleUsageRecords(maxNum, moduleResult, userId); + for (auto& oneResult : moduleResult) { + QueryModuleRecordInfos(oneResult); + } + for (auto& oneModuleRecord : moduleResult) { + dumpInfo.emplace_back(oneModuleRecord.ToString()); + for (uint32_t i = 0; i < oneModuleRecord.formRecords_.size(); i++) { + std::string oneFormInfo = "form " + std::to_string(static_cast(i) + 1) + ", "; + dumpInfo.emplace_back(oneFormInfo + oneModuleRecord.formRecords_[i].ToString()); } } return ret; diff --git a/utils/include/bundle_active_util.h b/utils/include/bundle_active_util.h index 12a2c04fbcd80f45960590d4ee295ebfe2d318db..67f3805957976f19cd4b9ee0dae0fa7e72212a85 100644 --- a/utils/include/bundle_active_util.h +++ b/utils/include/bundle_active_util.h @@ -30,6 +30,8 @@ public: static std::string GetBundleUsageKey(const std::string& bundleName, const int32_t uid); static int64_t GetFFRTDelayTime(const int64_t& delayTime); static int64_t GetIntervalTypeStartTime(const int64_t& timeStamp, const int32_t& intervalType); + static int32_t StringToInt32(const std::string& str); + static int64_t StringToInt64(const std::string& str); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/utils/src/bundle_active_util.cpp b/utils/src/bundle_active_util.cpp index a92f91fb05132116c87b7ed49b4e1c9412fa6bde..8bc74445d9a2a75ed4398e11985ef7366aaa042e 100644 --- a/utils/src/bundle_active_util.cpp +++ b/utils/src/bundle_active_util.cpp @@ -14,6 +14,7 @@ */ #include +#include #include "bundle_active_util.h" namespace OHOS { @@ -74,6 +75,30 @@ int64_t BundleActiveUtil::GetIntervalTypeStartTime(const int64_t& timeStamp, con } return mktime(tm_time) * SECOND_TO_MILLISECOND; } + +int32_t BundleActiveUtil::StringToInt32(const std::string& str) +{ + char* pEnd = nullptr; + errno = 0; + int64_t res = std::strtol(str.c_str(), &pEnd, 10); + if (errno == ERANGE || pEnd == str.c_str() || *pEnd != '\0' || + (res < std::numeric_limits::min()) || + res > std::numeric_limits::max()) { + return 0; + } + return static_cast(res); +} + +int64_t BundleActiveUtil::StringToInt64(const std::string& str) +{ + char* pEnd = nullptr; + errno = 0; + int64_t res = std::strtol(str.c_str(), &pEnd, 10); + if (errno == ERANGE || pEnd == str.c_str() || *pEnd != '\0') { + return 0; + } + return res; +} } // namespace DeviceUsageStats } // namespace OHOS