diff --git a/frameworks/src/bundle_state_query.cpp b/frameworks/src/bundle_state_query.cpp index 59bf313bfae65768e8f25575c31fc1ac2782d07d..93d08b287a5bc7a3e78fe51c95029dcce09b35fb 100644 --- a/frameworks/src/bundle_state_query.cpp +++ b/frameworks/src/bundle_state_query.cpp @@ -102,7 +102,7 @@ napi_value IsIdleState(napi_env env, napi_callback_info info) AsyncCallbackInfoIsIdleState *asyncCallbackInfo = (AsyncCallbackInfoIsIdleState *)data; if (asyncCallbackInfo != nullptr) { asyncCallbackInfo->state = BundleActiveClient::GetInstance().IsBundleIdle( - asyncCallbackInfo->bundleName); + asyncCallbackInfo->bundleName, asyncCallbackInfo->info.errorCode); } else { BUNDLE_ACTIVE_LOGE("IsIdleState, asyncCallbackInfo == nullptr"); } diff --git a/interfaces/innerkits/include/bundle_active_client.h b/interfaces/innerkits/include/bundle_active_client.h index d14ef815f8262f9b43c7c05598eb2a3687565578..778b52f2ffb70b0ba0e210fb3e77cff41a539940 100644 --- a/interfaces/innerkits/include/bundle_active_client.h +++ b/interfaces/innerkits/include/bundle_active_client.h @@ -26,7 +26,7 @@ class BundleActiveClient { public: int ReportEvent(std::string& bundleName, std::string& abilityName, std::string abilityId, const std::string& continuousTask, const int userId, const int eventId); - bool IsBundleIdle(const std::string& bundleName); + bool IsBundleIdle(const std::string& bundleName, int32_t& errCode); std::vector QueryPackageStats(const int intervalType, 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); diff --git a/interfaces/innerkits/include/bundle_active_proxy.h b/interfaces/innerkits/include/bundle_active_proxy.h index 2508e26439b1bedaa8899d26cd3037e624bb1003..122ed9654b023523dd04b04b4a3da468af2ea9ae 100644 --- a/interfaces/innerkits/include/bundle_active_proxy.h +++ b/interfaces/innerkits/include/bundle_active_proxy.h @@ -25,7 +25,7 @@ class BundleActiveProxy : public IRemoteProxy { public: int ReportEvent(std::string& bundleName, std::string& abilityName, std::string abilityId, const std::string& continuousTask, const int userId, const int eventId) override; - bool IsBundleIdle(const std::string& bundleName) override; + bool IsBundleIdle(const std::string& bundleName, int32_t& errCode) override; std::vector QueryPackageStats(const int intervalType, const int64_t beginTime, const int64_t endTime, int32_t& errCode) override; std::vector QueryEvents(const int64_t beginTime, const int64_t endTime, diff --git a/interfaces/innerkits/src/bundle_active_client.cpp b/interfaces/innerkits/src/bundle_active_client.cpp index 32fbbeadc30d2fb2f73bfe219d4e38f2b3066f50..c7de9cc5517b9c4d20fd73a8f67b854da0204286 100644 --- a/interfaces/innerkits/src/bundle_active_client.cpp +++ b/interfaces/innerkits/src/bundle_active_client.cpp @@ -56,12 +56,12 @@ int BundleActiveClient::ReportEvent(std::string& bundleName, std::string& abilit return bundleActiveProxy_->ReportEvent(bundleName, abilityName, abilityId, continuousTask, userId, eventId); } -bool BundleActiveClient::IsBundleIdle(const std::string& bundleName) +bool BundleActiveClient::IsBundleIdle(const std::string& bundleName, int32_t& errCode) { if (!GetBundleActiveProxy()) { return -1; } - return bundleActiveProxy_->IsBundleIdle(bundleName); + return bundleActiveProxy_->IsBundleIdle(bundleName, errCode); } std::vector BundleActiveClient::QueryPackageStats(const int intervalType, diff --git a/interfaces/innerkits/src/bundle_active_proxy.cpp b/interfaces/innerkits/src/bundle_active_proxy.cpp index 21aef49baf0ecab67003bd01309a7fa6d89ea0ac..3e4b95612681c4bbda308c96ddf5582c31bf433d 100644 --- a/interfaces/innerkits/src/bundle_active_proxy.cpp +++ b/interfaces/innerkits/src/bundle_active_proxy.cpp @@ -40,7 +40,7 @@ int BundleActiveProxy::ReportEvent(std::string& bundleName, std::string& ability return result; } -bool BundleActiveProxy::IsBundleIdle(const std::string& bundleName) +bool BundleActiveProxy::IsBundleIdle(const std::string& bundleName, int32_t& errCode) { MessageParcel data; MessageParcel reply; @@ -50,8 +50,8 @@ bool BundleActiveProxy::IsBundleIdle(const std::string& bundleName) } data.WriteString(bundleName); Remote() -> SendRequest(IS_BUNDLE_IDLE, data, reply, option); - int32_t result = reply.ReadInt32(); - BUNDLE_ACTIVE_LOGI("result is %{public}d", result); + bool result = reply.ReadInt32(); + errCode = reply.ReadInt32(); return result; } diff --git a/interfaces/kits/bundlestats/napi/include/bundle_state_data.h b/interfaces/kits/bundlestats/napi/include/bundle_state_data.h index 5d4f5652b1f3c6e9ae96871eaabc9736e7b7dfb6..f960bbd01cd2f34d079220bf0b2939c028d3ee90 100644 --- a/interfaces/kits/bundlestats/napi/include/bundle_state_data.h +++ b/interfaces/kits/bundlestats/napi/include/bundle_state_data.h @@ -54,7 +54,7 @@ struct AsyncCallbackInfoIsIdleState { napi_env env = nullptr; napi_async_work asyncWork = nullptr; std::string bundleName; - bool state; + bool state = false; CallbackPromiseInfo info; }; diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index 3c93f0ad3e065bd2941aab28a875149d95995675..69ce8546eacc404a2fcb802b5b1f50e7948c43fb 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -38,7 +38,7 @@ public: using BundleFlag = OHOS::AppExecFwk::BundleFlag; int ReportEvent(std::string& bundleName, std::string& abilityName, std::string abilityId, const std::string& continuousTask, const int userId, const int eventId) override; - bool IsBundleIdle(const std::string& bundleName) override; + bool IsBundleIdle(const std::string& bundleName, int32_t& errCode) override; std::vector QueryPackageStats(const int intervalType, const int64_t beginTime, const int64_t endTime, int32_t& errCode) override; std::vector QueryEvents(const int64_t beginTime, const int64_t endTime, diff --git a/services/common/include/ibundle_active_service.h b/services/common/include/ibundle_active_service.h index 3740ca1d8e0c0b4faff252bce4df45c1d0eaee49..eab5794eba28aac0f2dfe011af09d4294b3449f6 100644 --- a/services/common/include/ibundle_active_service.h +++ b/services/common/include/ibundle_active_service.h @@ -44,7 +44,7 @@ class IBundleActiveService : public IRemoteBroker { public: virtual int ReportEvent(std::string& bundleName, std::string& abilityName, std::string abilityId, const std::string& continuousTask, const int userId, const int eventId) = 0; - virtual bool IsBundleIdle(const std::string& bundleName) = 0; + virtual bool IsBundleIdle(const std::string& bundleName, int32_t& errCode) = 0; virtual std::vector QueryPackageStats(const int intervalType, const int64_t beginTime, const int64_t endTime, int32_t& errCode) = 0; virtual std::vector QueryEvents(const int64_t beginTime, const int64_t endTime, diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index d5dc82d6842f64ab6a1bcf667d0734a3abbc6c7f..70218c003a1563b0965254f11f8491dc9dae9971 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -223,17 +223,39 @@ int BundleActiveService::ReportEvent(std::string& bundleName, std::string& abili bool BundleActiveService::IsBundleIdle(const std::string& bundleName) { // get uid - BUNDLE_ACTIVE_LOGI("Is bundle active called"); - int callingUid = OHOS::IPCSkeleton::GetCallingUid(); - BUNDLE_ACTIVE_LOGI("UID is %{public}d", callingUid); + int32_t callingUid = OHOS::IPCSkeleton::GetCallingUid(); + AccessToken::AccessTokenID tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + if (!GetBundleMgrProxy()) { + BUNDLE_ACTIVE_LOGE("get bundle manager proxy failed!"); + return false; + } + std::string callingBundleName = ""; + sptrBundleMgr_->GetBundleNameForUid(callingUid, callingBundleName); + BUNDLE_ACTIVE_LOGI("UID is %{public}d, bundle name is %{public}s", callingUid, callingBundleName.c_str()); // get user id int userId = -1; int result = -1; - OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); - if (ret == ERR_OK && userId != -1) { - result = bundleActiveCore_->IsBundleIdle(bundleName, userId); + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); + if (userId != -1 && !callingBundleName.empty()) { + if (callingBundleName == bundleName) { + BUNDLE_ACTIVE_LOGI("%{public}s check its own idle state", bundleName.c_str()); + result = bundleActiveCore_->IsBundleIdle(bundleName, userId); + } else { + bool isSystemAppAndHasPermission = CheckBundleIsSystemAppAndHasPermission(callingUid, tokenId, errCode); + BUNDLE_ACTIVE_LOGI("check other bundle idle state"); + if (isSystemAppAndHasPermission) { + errCode = 0; + result = bundleActiveCore_->IsBundleIdle(bundleName, userId); + } else { + errCode = -1; + return false; + } + } + } else { + errCode = -1; + return false; } - if (result == 0) { + if (result == 0 || result == -1) { return false; } return true; diff --git a/services/common/src/bundle_active_stub.cpp b/services/common/src/bundle_active_stub.cpp index f24e1a25940afd7caf9fedce05b78f69985f9eef..8c498665b9941fa363bee65db6e16ff1ee3357b8 100644 --- a/services/common/src/bundle_active_stub.cpp +++ b/services/common/src/bundle_active_stub.cpp @@ -38,8 +38,10 @@ int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Me } case IS_BUNDLE_IDLE: { std::string bundleName = data.ReadString(); - int result = IsBundleIdle(bundleName); - return reply.WriteInt32(result); + int32_t errCode = data.ReadInt32(); + int32_t result = IsBundleIdle(bundleName, errCode); + reply.WriteInt32(result); + return reply.WriteInt32(errCode); } case QUERY_USAGE_STATS: { std::vector result; diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index 277df46b48e36cead9a477d9ba27f8e6bd10cb47..4be06cf2fd15f4a93df31869eb24b94472b755ac 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -140,8 +140,10 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryPackagesStats */ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_IsBundleIdle_001, Function | MediumTest | Level0) { - bool result = BundleActiveClient::GetInstance().IsBundleIdle(DEFAULT_BUNDLENAME); - EXPECT_EQ(result, true); + int32_t errCode = 0; + bool result = BundleActiveClient::GetInstance().IsBundleIdle(DEFAULT_BUNDLENAME, errCode); + EXPECT_EQ(result, false); + EXPECT_EQ(errCode, -1); } /*