diff --git a/frameworks/src/bundle_state_query.cpp b/frameworks/src/bundle_state_query.cpp index f472641ba84390a762186222276d0a37b6665865..ab28cbc76e3133436726fa3ab2dd741ce5b902b7 100644 --- a/frameworks/src/bundle_state_query.cpp +++ b/frameworks/src/bundle_state_query.cpp @@ -221,7 +221,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->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 90765762a540f5065d450430e0d3f37c12e0dace..a21e779499bbaf5f39bef4d0c49b1e7b509e31a4 100644 --- a/interfaces/innerkits/include/bundle_active_client.h +++ b/interfaces/innerkits/include/bundle_active_client.h @@ -44,7 +44,7 @@ public: * parameters: bundleName * return: if bundle is idle, return true. if bundle is not idle, return false. */ - bool IsBundleIdle(const std::string& bundleName); + bool IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId = -1); /* * function: QueryPackageStats, query all bundle usage statistics in specific time span for calling user. * parameters: intervalType, beginTime, endTime, errCode diff --git a/interfaces/innerkits/include/bundle_active_proxy.h b/interfaces/innerkits/include/bundle_active_proxy.h index a0b90b397062d4ae317c99858be4e47d09ecba20..9e0a205ce81a8235735cf91e4a6b1fbf90184c24 100644 --- a/interfaces/innerkits/include/bundle_active_proxy.h +++ b/interfaces/innerkits/include/bundle_active_proxy.h @@ -38,7 +38,7 @@ public: * parameters: bundleName * return: if bundle is idle, return true. if bundle is not idle, return false. */ - bool IsBundleIdle(const std::string& bundleName) override; + bool IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId = -1) override; /* * function: QueryPackageStats, query all bundle usage statistics in specific time span for calling user. * parameters: intervalType, beginTime, endTime, errCode diff --git a/interfaces/innerkits/src/bundle_active_client.cpp b/interfaces/innerkits/src/bundle_active_client.cpp index af44281d75da3577b92882524384ac42586760fa..0cd2f54efec4c8c82ac2e2dbd2f91d0ec46332b0 100644 --- a/interfaces/innerkits/src/bundle_active_client.cpp +++ b/interfaces/innerkits/src/bundle_active_client.cpp @@ -75,12 +75,12 @@ int32_t BundleActiveClient::ReportEvent(BundleActiveEvent event, const int32_t u return bundleActiveProxy_->ReportEvent(event, userId); } -bool BundleActiveClient::IsBundleIdle(const std::string& bundleName) +bool BundleActiveClient::IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId) { if (!GetBundleActiveProxy()) { return -1; } - return bundleActiveProxy_->IsBundleIdle(bundleName); + return bundleActiveProxy_->IsBundleIdle(bundleName, errCode, userId); } std::vector BundleActiveClient::QueryPackageStats(const int32_t intervalType, diff --git a/interfaces/innerkits/src/bundle_active_proxy.cpp b/interfaces/innerkits/src/bundle_active_proxy.cpp index 69d4cd19d68665d369a9cf3de4c1827f92ed8902..f9844895001348c194ef4ec0632d141556093fdf 100644 --- a/interfaces/innerkits/src/bundle_active_proxy.cpp +++ b/interfaces/innerkits/src/bundle_active_proxy.cpp @@ -33,18 +33,20 @@ int32_t BundleActiveProxy::ReportEvent(BundleActiveEvent& event, const int32_t u return result; } -bool BundleActiveProxy::IsBundleIdle(const std::string& bundleName) +bool BundleActiveProxy::IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId) { MessageParcel data; MessageParcel reply; MessageOption option; - if (!data.WriteInterfaceToken(GetDescriptor())) { + if (!data.WriteInterfaceToken(GetDescriptor()) || + !data.WriteString(bundleName) || + !data.WriteInt32(errCode) || + !data.WriteInt32(userId)) { return false; } - data.WriteString(bundleName); Remote() -> SendRequest(IS_BUNDLE_IDLE, data, reply, option); - int32_t result = reply.ReadInt32(); - BUNDLE_ACTIVE_LOGD("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 6856cf08635cb38c41cdbd9f9226e3db2c0e5721..8118aabfa5b1cbab30e3af08253c699ffc96f8e1 100644 --- a/interfaces/kits/bundlestats/napi/include/bundle_state_data.h +++ b/interfaces/kits/bundlestats/napi/include/bundle_state_data.h @@ -71,7 +71,7 @@ struct CallbackReceiveDataWorker { struct AsyncCallbackInfoIsIdleState : public AsyncWorkData { explicit AsyncCallbackInfoIsIdleState(napi_env env) : AsyncWorkData(env) {} std::string bundleName = ""; - bool state = true; + bool state = false; }; struct AsyncCallbackInfoPriorityGroup : public AsyncWorkData { diff --git a/interfaces/test/unittest/device_usage_statistics_jsunittest/device_usage_statistics_jsunit.test.js b/interfaces/test/unittest/device_usage_statistics_jsunittest/device_usage_statistics_jsunit.test.js index 84079eae9aa0ce6bcb2f89f86f86dcbb7e135c26..37027dc83bdaa80ae885de3fd4f5cf3f31f9ba56 100644 --- a/interfaces/test/unittest/device_usage_statistics_jsunittest/device_usage_statistics_jsunit.test.js +++ b/interfaces/test/unittest/device_usage_statistics_jsunittest/device_usage_statistics_jsunit.test.js @@ -467,7 +467,7 @@ describe("DeviceUsageStatisticsJsTest", function () { setTimeout(()=>{ done(); - }, 500); + }, 500); }) /* @@ -498,7 +498,7 @@ describe("DeviceUsageStatisticsJsTest", function () { done(); }, 500); }) - + /* * @tc.name: DeviceUsageStatisticsJsTest020 * @tc.desc: test registerGroupCallBack callback. @@ -527,7 +527,7 @@ describe("DeviceUsageStatisticsJsTest", function () { setTimeout(()=>{ done(); - }, 500); + }, 500); }) /* @@ -571,7 +571,7 @@ describe("DeviceUsageStatisticsJsTest", function () { setTimeout(()=>{ done(); - }, 500); + }, 500); }) /* diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index ca7a6de41c338e1f6d94cdc5b798f4aebf25f833..ebe6615a511bac3b0b0c26af20b27304d643e221 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -58,7 +58,7 @@ public: * parameters: bundleName * return: if bundle is idle, return true. if bundle is not idle, return false. */ - bool IsBundleIdle(const std::string& bundleName) override; + bool IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId = -1) override; /* * function: QueryPackageStats, query all bundle usage statistics in specific time span for calling user. * parameters: intervalType, beginTime, endTime, errCode diff --git a/services/common/include/ibundle_active_service.h b/services/common/include/ibundle_active_service.h index eff21f7fec82534d0480ce2f20a1df0ecf7eba59..49351605d79f9dcc9913b15debde2966eb6f1c14 100644 --- a/services/common/include/ibundle_active_service.h +++ b/services/common/include/ibundle_active_service.h @@ -61,7 +61,7 @@ public: * parameters: bundleName * return: if bundle is idle, return true. if bundle is not idle, return false. */ - virtual bool IsBundleIdle(const std::string& bundleName) = 0; + virtual bool IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId) = 0; /* * function: QueryPackageStats, query all bundle usage statistics in specific time span for calling user. * parameters: intervalType, beginTime, endTime, errCode @@ -120,7 +120,7 @@ public: */ virtual int32_t QueryEventStats(int64_t beginTime, int64_t endTime, std::vector& eventStats, int32_t userId) = 0; - + /* * function: QueryAppNotificationNumber, query all app notification number in specific time span for calling user. * parameters: beginTime, endTime, eventStats, userId, default userId is -1 for JS API, diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 204bbf5aac90f99303b965b52aae1960ee75cf52..6d082fce7fe989e1bc781e6f675613b6b8f0e58a 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -249,19 +249,45 @@ int32_t BundleActiveService::ReportEvent(BundleActiveEvent& event, const int32_t } } -bool BundleActiveService::IsBundleIdle(const std::string& bundleName) +bool BundleActiveService::IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId) { // get uid int32_t callingUid = OHOS::IPCSkeleton::GetCallingUid(); - BUNDLE_ACTIVE_LOGD("UID is %{public}d", callingUid); + 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 - int32_t userId = -1; int32_t result = -1; - OHOS::ErrCode ret = BundleActiveAccountHelper::GetUserId(callingUid, userId); - if (ret == ERR_OK && userId != -1) { - result = bundleActiveCore_->IsBundleIdle(bundleName, userId); + if (userId == -1) { + BundleActiveAccountHelper::GetUserId(callingUid, userId); + } + if (userId != -1) { + 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 || + AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId) == + AccessToken::TypeATokenTypeEnum::TOKEN_NATIVE) { + 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 25fe267908e9313fab795bb4ead5180280ad35ec..4745fe8592afec43b3a9a22ea86b5bdcbdfd4f35 100644 --- a/services/common/src/bundle_active_stub.cpp +++ b/services/common/src/bundle_active_stub.cpp @@ -40,8 +40,11 @@ int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Me } case IS_BUNDLE_IDLE: { std::string bundleName = data.ReadString(); - int32_t result = IsBundleIdle(bundleName); - return reply.WriteInt32(result); + int32_t errCode = data.ReadInt32(); + int32_t userId = data.ReadInt32(); + int32_t result = IsBundleIdle(bundleName, errCode, userId); + 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 01fbb0b4bbada19038e93ff7479b64eb511c2201..7c8142b84229a1edc99ad90a17d6ee87ad94dd68 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -150,8 +150,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, DEFAULT_USERID); + EXPECT_EQ(result, false); + EXPECT_EQ(errCode, 0); } /*