diff --git a/interfaces/innerkits/include/bundle_active_client.h b/interfaces/innerkits/include/bundle_active_client.h index 0c62f66e1194f366fc0ad838effaff0c94641a16..85a531818ec51406d21559d225699fdfe115921a 100644 --- a/interfaces/innerkits/include/bundle_active_client.h +++ b/interfaces/innerkits/include/bundle_active_client.h @@ -46,13 +46,14 @@ public: * return: vector of bundle usage statistics. */ std::vector QueryPackageStats(const int intervalType, const int64_t beginTime, - const int64_t endTime, int32_t& errCode); + const int64_t endTime, int32_t& errCode, int userId = -1); /* * function: QueryEvents, query all events in specific time span for calling user. * parameters: beginTime, endTime, errCode * return: vector of events. */ - std::vector QueryEvents(const int64_t beginTime, const int64_t endTime, int32_t& errCode); + std::vector QueryEvents(const int64_t beginTime, const int64_t endTime, + int32_t& errCode, int userId = -1); /* * function: SetBundleGroup, set specific bundle of specific user to a priority group. * parameters: bundleName, newGroup, userId diff --git a/interfaces/innerkits/include/bundle_active_proxy.h b/interfaces/innerkits/include/bundle_active_proxy.h index 36277488f85892319e62018a434d470b55da361d..de35a8328fce9439d3df9e62182041913d4e644a 100644 --- a/interfaces/innerkits/include/bundle_active_proxy.h +++ b/interfaces/innerkits/include/bundle_active_proxy.h @@ -44,14 +44,14 @@ public: * return: vector of bundle usage statistics. */ std::vector QueryPackageStats(const int intervalType, const int64_t beginTime, - const int64_t endTime, int32_t& errCode) override; + const int64_t endTime, int32_t& errCode, int userId = -1) override; /* * function: QueryEvents, query all events in specific time span for calling user. * parameters: beginTime, endTime, errCode * return: vector of events. */ - std::vector QueryEvents(const int64_t beginTime, const int64_t endTime, - int32_t& errCode) override; + std::vector QueryEvents(const int64_t beginTime, const int64_t endTime, + int32_t& errCode, int userId = -1) override; /* * function: SetBundleGroup, set specific bundle of specific user to a priority group. * parameters: bundleName, newGroup, userId diff --git a/interfaces/innerkits/src/bundle_active_client.cpp b/interfaces/innerkits/src/bundle_active_client.cpp index f6bff8d10f9a76c59cb68c47151d1a8b29d950cd..ef7bb21bcc9ae8b9082bf0efc5c03ab24b2fa032 100644 --- a/interfaces/innerkits/src/bundle_active_client.cpp +++ b/interfaces/innerkits/src/bundle_active_client.cpp @@ -63,21 +63,21 @@ bool BundleActiveClient::IsBundleIdle(const std::string& bundleName) } std::vector BundleActiveClient::QueryPackageStats(const int intervalType, - const int64_t beginTime, const int64_t endTime, int32_t& errCode) + const int64_t beginTime, const int64_t endTime, int32_t& errCode, int userId) { if (!GetBundleActiveProxy()) { return std::vector(0); } - return bundleActiveProxy_->QueryPackageStats(intervalType, beginTime, endTime, errCode); + return bundleActiveProxy_->QueryPackageStats(intervalType, beginTime, endTime, errCode, userId); } std::vector BundleActiveClient::QueryEvents(const int64_t beginTime, - const int64_t endTime, int32_t& errCode) + const int64_t endTime, int32_t& errCode, int userId) { if (!GetBundleActiveProxy()) { return std::vector(0); } - return bundleActiveProxy_->QueryEvents(beginTime, endTime, errCode); + return bundleActiveProxy_->QueryEvents(beginTime, endTime, errCode, userId); } void BundleActiveClient::SetBundleGroup(std::string bundleName, const int newGroup, const int userId) diff --git a/interfaces/innerkits/src/bundle_active_proxy.cpp b/interfaces/innerkits/src/bundle_active_proxy.cpp index 12aa1168e99742086c735004c67455a36e8e4384..f74fb0487c01b11a3bd49a4a4125fab113d69bfe 100644 --- a/interfaces/innerkits/src/bundle_active_proxy.cpp +++ b/interfaces/innerkits/src/bundle_active_proxy.cpp @@ -49,7 +49,7 @@ bool BundleActiveProxy::IsBundleIdle(const std::string& bundleName) } std::vector BundleActiveProxy::QueryPackageStats(const int intervalType, - const int64_t beginTime, const int64_t endTime, int32_t& errCode) + const int64_t beginTime, const int64_t endTime, int32_t& errCode, int userId) { MessageParcel data; MessageParcel reply; @@ -61,6 +61,7 @@ std::vector BundleActiveProxy::QueryPackageStats(const data.WriteInt32(intervalType); data.WriteInt64(beginTime); data.WriteInt64(endTime); + data.WriteInt32(userId); data.WriteInt32(errCode); Remote() -> SendRequest(QUERY_USAGE_STATS, data, reply, option); errCode = reply.ReadInt32(); @@ -85,7 +86,7 @@ std::vector BundleActiveProxy::QueryPackageStats(const } std::vector BundleActiveProxy::QueryEvents(const int64_t beginTime, - const int64_t endTime, int32_t& errCode) + const int64_t endTime, int32_t& errCode, int userId) { MessageParcel data; MessageParcel reply; @@ -96,6 +97,7 @@ std::vector BundleActiveProxy::QueryEvents(const int64_t begi } data.WriteInt64(beginTime); data.WriteInt64(endTime); + data.WriteInt32(userId); data.WriteInt32(errCode); Remote() -> SendRequest(QUERY_EVENTS, data, reply, option); errCode = reply.ReadInt32(); diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index e89222427478e70a3514ba99411baf25f4eccac9..558434d26324bf532812a6fe194e737932ebbecd 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -58,14 +58,14 @@ public: * return: vector of bundle usage statistics. */ std::vector QueryPackageStats(const int intervalType, const int64_t beginTime, - const int64_t endTime, int32_t& errCode) override; + const int64_t endTime, int32_t& errCode, int userId = -1) override; /* * function: QueryEvents, query all events in specific time span for calling user. * parameters: beginTime, endTime, errCode * return: vector of events. */ std::vector QueryEvents(const int64_t beginTime, const int64_t endTime, - int32_t& errCode) override; + int32_t& errCode, int userId = -1) override; /* * function: SetBundleGroup, set specific bundle of specific user to a priority group. * parameters: bundleName, newGroup, userId diff --git a/services/common/include/ibundle_active_service.h b/services/common/include/ibundle_active_service.h index 2655275613d9a3d8269eada04e0d744d0dd57124..d223bee7c8c95616b7c0d3a7a467f542157d4b24 100644 --- a/services/common/include/ibundle_active_service.h +++ b/services/common/include/ibundle_active_service.h @@ -62,14 +62,14 @@ public: * return: vector of bundle usage statistics. */ virtual std::vector QueryPackageStats(const int intervalType, const int64_t beginTime, - const int64_t endTime, int32_t& errCode) = 0; + const int64_t endTime, int32_t& errCode, int userId) = 0; /* * function: QueryEvents, query all events in specific time span for calling user. * parameters: beginTime, endTime, errCode * return: vector of events. */ virtual std::vector QueryEvents(const int64_t beginTime, const int64_t endTime, - int32_t& errCode) = 0; + int32_t& errCode, int userId) = 0; /* * function: QueryCurrentPackageStats, query bundle usage statistics in specific time span for calling bundle. * parameters: intervalType, beginTime, endTime diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index a2ac0d2f4a4ee8ee0f643efb8463246e79984f88..28983c75c436573045bea7d6ff8728003e2730e0 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -253,7 +253,7 @@ bool BundleActiveService::IsBundleIdle(const std::string& bundleName) } std::vector BundleActiveService::QueryPackageStats(const int intervalType, - const int64_t beginTime, const int64_t endTime, int32_t& errCode) + const int64_t beginTime, const int64_t endTime, int32_t& errCode, int userId) { BUNDLE_ACTIVE_LOGI("QueryPackageStats stats called, intervaltype is %{public}d", intervalType); @@ -261,13 +261,21 @@ std::vector BundleActiveService::QueryPackageStats(con // get uid int callingUid = OHOS::IPCSkeleton::GetCallingUid(); BUNDLE_ACTIVE_LOGI("QueryPackageStats UID is %{public}d", callingUid); - // get userid - int userId = -1; - OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); - if (ret == ERR_OK && userId != -1) { + if (userId == -1) { + // get userid + OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); + if (ret != ERR_OK) { + errCode = -1; + return result; + } + } + if (userId != -1) { BUNDLE_ACTIVE_LOGI("QueryPackageStats user id is %{public}d", userId); bool isSystemAppAndHasPermission = CheckBundleIsSystemAppAndHasPermission(callingUid, userId, errCode); - if (isSystemAppAndHasPermission == true) { + AccessToken::AccessTokenID tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + if (isSystemAppAndHasPermission == true || + AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId) == + AccessToken::TypeATokenTypeEnum::TOKEN_NATIVE) { int convertedIntervalType = ConvertIntervalType(intervalType); result = bundleActiveCore_->QueryPackageStats(userId, convertedIntervalType, beginTime, endTime, ""); } @@ -276,20 +284,28 @@ std::vector BundleActiveService::QueryPackageStats(con } std::vector BundleActiveService::QueryEvents(const int64_t beginTime, - const int64_t endTime, int32_t& errCode) + const int64_t endTime, int32_t& errCode, int userId) { BUNDLE_ACTIVE_LOGI("QueryEvents stats called"); std::vector result; // get uid int callingUid = OHOS::IPCSkeleton::GetCallingUid(); BUNDLE_ACTIVE_LOGI("QueryEvents UID is %{public}d", callingUid); - // get userid - int userId = -1; - OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); - if (ret == ERR_OK && userId != -1) { + if (userId == -1) { + // get userid + OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); + if (ret != ERR_OK) { + errCode = -1; + return result; + } + } + if (userId != -1) { BUNDLE_ACTIVE_LOGI("QueryEvents userid is %{public}d", userId); bool isSystemAppAndHasPermission = CheckBundleIsSystemAppAndHasPermission(callingUid, userId, errCode); - if (isSystemAppAndHasPermission == true) { + AccessToken::AccessTokenID tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + if (isSystemAppAndHasPermission == true || + AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId) == + AccessToken::TypeATokenTypeEnum::TOKEN_NATIVE) { result = bundleActiveCore_->QueryEvents(userId, beginTime, endTime, ""); } } diff --git a/services/common/src/bundle_active_stub.cpp b/services/common/src/bundle_active_stub.cpp index 46c5503f617968f455ec2c1df5c292ff22d1ff55..fb112730ee7f7d4fdcd1b8cd2f9e25769d4ee315 100644 --- a/services/common/src/bundle_active_stub.cpp +++ b/services/common/src/bundle_active_stub.cpp @@ -50,8 +50,9 @@ int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Me intervalType); int64_t beginTime = data.ReadInt64(); int64_t endTime = data.ReadInt64(); + int32_t userId = data.ReadInt32(); int32_t errCode = data.ReadInt32(); - result = QueryPackageStats(intervalType, beginTime, endTime, errCode); + result = QueryPackageStats(intervalType, beginTime, endTime, errCode, userId); reply.WriteInt32(errCode); size = static_cast(result.size()); BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_USAGE_STATS result size is %{public}d", size); @@ -69,8 +70,9 @@ int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Me int size = 0; int64_t beginTime = data.ReadInt64(); int64_t endTime = data.ReadInt64(); + int32_t userId = data.ReadInt32(); int32_t errCode = data.ReadInt32(); - result = QueryEvents(beginTime, endTime, errCode); + result = QueryEvents(beginTime, endTime, errCode, userId); size = static_cast(result.size()); reply.WriteInt32(errCode); reply.WriteInt32(size);