From 43bf731a7b9894915c1042b8bfc14cb81132fbb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E4=BC=9F?= Date: Thu, 17 Apr 2025 07:33:21 +0000 Subject: [PATCH 1/8] update frameworks/IWorkSchedService.idl. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 胡伟 --- frameworks/IWorkSchedService.idl | 1 + 1 file changed, 1 insertion(+) diff --git a/frameworks/IWorkSchedService.idl b/frameworks/IWorkSchedService.idl index 9a2d657..deb3a8b 100644 --- a/frameworks/IWorkSchedService.idl +++ b/frameworks/IWorkSchedService.idl @@ -21,6 +21,7 @@ interface OHOS.WorkScheduler.IWorkSchedService { void StopAndClearWorks(); void IsLastWorkTimeout([in] int workId, [out] boolean isTimeout); void ObtainAllWorks([out] List workInfos); + void ObtainWorksByUidAndWorkIdForInner([in] int uid, [out] List workInfos, [in] int workId); void GetWorkStatus([in] int workId, [out] WorkInfo workInfo); void GetAllRunningWorks([out] List workInfos); void PauseRunningWorks([in] int uid); -- Gitee From 216addd7d4ef421da3884e594e06e1a5b4035a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E4=BC=9F?= Date: Thu, 17 Apr 2025 07:34:21 +0000 Subject: [PATCH 2/8] update frameworks/include/workscheduler_srv_client.h. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 胡伟 --- frameworks/include/workscheduler_srv_client.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/frameworks/include/workscheduler_srv_client.h b/frameworks/include/workscheduler_srv_client.h index a165eba..2684bfe 100644 --- a/frameworks/include/workscheduler_srv_client.h +++ b/frameworks/include/workscheduler_srv_client.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -80,6 +80,16 @@ public: */ ErrCode ObtainAllWorks(std::list> &workInfos); + /** + * @brief Obtain works by uid and workId for inner. + * @param uid The uid. + * @param workInfos The infos of work. + * @param workId The id of work. + * @return error code, ERR_OK if success. + */ + ErrCode ObtainWorksByUidAndWorkIdForInner(int32_t uid, + std::list> &workInfos, int32_t workId = -1); + /** * @brief Get the Running Work Scheduler Work object * -- Gitee From f6138bdcec70a73d94847cc2f072a51d1632feaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E4=BC=9F?= Date: Thu, 17 Apr 2025 07:35:10 +0000 Subject: [PATCH 3/8] update frameworks/src/workscheduler_srv_client.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 胡伟 --- frameworks/src/workscheduler_srv_client.cpp | 23 ++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/frameworks/src/workscheduler_srv_client.cpp b/frameworks/src/workscheduler_srv_client.cpp index 03d8c12..ef18bb7 100644 --- a/frameworks/src/workscheduler_srv_client.cpp +++ b/frameworks/src/workscheduler_srv_client.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -151,6 +151,27 @@ ErrCode WorkSchedulerSrvClient::ObtainAllWorks(std::list> &workInfos, int32_t workId) +{ + WS_HILOGD("Obtain Works By uid and workId for inner"); + std::lock_guard lock(mutex_); + ErrCode errCode = Connect(); + if (errCode != ERR_OK) { + return errCode; + } + if (uid < 0) { + WS_HILOGE("param uid: %{public}d invaild.", uid); + return E_PARAM_INVAILD_UID; + } + std::vector vectorWorkInfos; + ErrCode ret = iWorkSchedService_->ObtainWorksByUidAndWorkIdForInner(uid, vectorWorkInfos, workId); + for (const auto& workInfo : vectorWorkInfos) { + workInfos.push_back(std::make_shared(workInfo)); + } + return ret; +} + ErrCode WorkSchedulerSrvClient::GetWorkStatus(int32_t workId, std::shared_ptr &workInfo) { WS_HILOGD("Get Work Status"); -- Gitee From 3317dc897732b4a3ffaf1228a6b061ffa179d5dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E4=BC=9F?= Date: Thu, 17 Apr 2025 07:36:00 +0000 Subject: [PATCH 4/8] update frameworks/test/unittest/src/work_sched_client_test.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 胡伟 --- .../unittest/src/work_sched_client_test.cpp | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/frameworks/test/unittest/src/work_sched_client_test.cpp b/frameworks/test/unittest/src/work_sched_client_test.cpp index c0826ae..c4bcaa9 100644 --- a/frameworks/test/unittest/src/work_sched_client_test.cpp +++ b/frameworks/test/unittest/src/work_sched_client_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -241,5 +241,24 @@ HWTEST_F(WorkSchedClientTest, WorkSchedClientTest_015, TestSize.Level0) ErrCode ret = WorkSchedulerSrvClient::GetInstance().SetWorkSchedulerConfig(configData, sourceType); EXPECT_NE(ret, ERR_OK); } + +/** + * @tc.name: ObtainWorksByUidAndWorkIdForInner_016 + * @tc.desc: Test ObtainWorksByUidAndWorkIdForInner + * @tc.type: FUNC + * @tc.require: issueIC1RS6 + */ +HWTEST_F(WorkSchedClientTest, ObtainWorksByUidAndWorkIdForInner_016, TestSize.Level0) +{ + std::list> workInfos; + int32_t uid = -1; + ErrCode ret = ERR_OK; + ret = WorkSchedulerSrvClient::GetInstance().ObtainWorksByUidAndWorkIdForInner(uid, workInfos); + EXPECT_EQ(ret, E_PARAM_INVAILD_UID); + + uid = 1; + ret = WorkSchedulerSrvClient::GetInstance().ObtainWorksByUidAndWorkIdForInner(uid, workInfos); + EXPECT_EQ(ret, ERR_OK); +} } // namespace WorkScheduler } // namespace OHOS -- Gitee From fbd26532b5e16027ddf1408c00c964de13bb591b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E4=BC=9F?= Date: Thu, 17 Apr 2025 07:37:06 +0000 Subject: [PATCH 5/8] update services/native/include/work_scheduler_service.h. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 胡伟 --- services/native/include/work_scheduler_service.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/services/native/include/work_scheduler_service.h b/services/native/include/work_scheduler_service.h index b006c29..bd84725 100644 --- a/services/native/include/work_scheduler_service.h +++ b/services/native/include/work_scheduler_service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -100,6 +100,14 @@ public: * @return error code, ERR_OK if success. */ int32_t ObtainAllWorks(std::vector& workInfos) override; + /** + * @brief Obtain works by uid and workId for inner. + * @param uid The uid. + * @param workInfos The infos of work. + * @param workId The id of work. + * @return error code, ERR_OK if success. + */ + int32_t ObtainWorksByUidAndWorkIdForInner(int32_t uid, std::vector& workInfos, int32_t workId) override; /** * @brief Get the status of work. * @@ -340,10 +348,12 @@ private: bool GetAppIndexAndBundleNameByUid(int32_t uid, int32_t &appIndex, std::string &bundleName); bool CheckExtensionInfos(WorkInfo &workInfo, int32_t uid); void DumpLoadSaWorks(const std::string &saIdStr, const std::string &uidStr, std::string &result); + void DumpGetWorks(const std::string &uidStr, const std::string &workIdStr, std::string &result); std::string DumpExemptionBundles(); void LoadMinRepeatTimeFromFile(const char *path); int32_t SetTimer(); void CancelTimer(int32_t id); + bool CheckCallingToken(); private: std::set whitelist_; -- Gitee From 911009085b74deaf9356ddc2f7f3552c197d9645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E4=BC=9F?= Date: Thu, 17 Apr 2025 07:39:22 +0000 Subject: [PATCH 6/8] update services/native/src/work_scheduler_service.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 胡伟 --- .../native/src/work_scheduler_service.cpp | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/services/native/src/work_scheduler_service.cpp b/services/native/src/work_scheduler_service.cpp index b59c99d..bd7129c 100644 --- a/services/native/src/work_scheduler_service.cpp +++ b/services/native/src/work_scheduler_service.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -887,6 +887,31 @@ int32_t WorkSchedulerService::ObtainAllWorks(std::vector& workInfos) return ERR_OK; } +int32_t WorkSchedulerService::ObtainWorksByUidAndWorkIdForInner(int32_t uid, + std::vector& workInfos, int32_t workId) +{ + HitraceScoped traceScoped(HITRACE_TAG_OHOS, "WorkSchedulerService::ObtainWorksByUidAndWorkIdForInner"); + if (!ready_) { + WS_HILOGE("service is not ready."); + return E_SERVICE_NOT_READY; + } + if (!CheckCallingToken()) { + WS_HILOGE("ObtainWorksByUidAndWorkIdForInner not allowed."); + return E_PERMISSION_DENIED; + } + if (workId != -1) { + std::shared_ptr workInfoPtr = workPolicyManager_->GetWorkStatus(uid, workId); + if (workInfoPtr != nullptr) { + workInfos.push_back(*workInfoPtr); + return ERR_OK; + } else { + return E_WORK_NOT_EXIST_FAILED; + } + } + workInfos = workPolicyManager_->ObtainAllWorks(uid); + return ERR_OK; +} + int32_t WorkSchedulerService::GetWorkStatus(int32_t workId, WorkInfo& workInfo) { HitraceScoped traceScoped(HITRACE_TAG_OHOS, "WorkSchedulerService::GetWorkStatus"); @@ -980,6 +1005,8 @@ void WorkSchedulerService::DumpProcessForEngMode(std::vector &argsI DumpRunningWorks(argsInStr[DUMP_PARAM_INDEX], argsInStr[DUMP_VALUE_INDEX], result); } else if (argsInStr[DUMP_OPTION] == "-s") { DumpLoadSaWorks(argsInStr[DUMP_PARAM_INDEX], argsInStr[DUMP_VALUE_INDEX], result); + } else if (argsInStr[DUMP_OPTION] == "-g") { + DumpGetWorks(argsInStr[DUMP_PARAM_INDEX], argsInStr[DUMP_VALUE_INDEX], result); } else { result.append("Error params."); } @@ -1549,6 +1576,33 @@ void WorkSchedulerService::DumpLoadSaWorks(const std::string &saIdStr, const std result.append("load sa failed."); } +void WorkSchedulerService::DumpGetWorks(const std::string &uidStr, const std::string &workIdStr, std::string &result) +{ + if (workIdStr.empty() || uidStr.empty()) { + result.append("param error."); + return; + } + int32_t workId = std::stoi(workIdStr); + int32_t uid = std::stoi(uidStr); + if (uid < 0) { + result.append("the parameter is invalid."); + return; + } + std::vector workInfos; + int32_t ret = ObtainWorksByUidAndWorkIdForInner(uid, workInfos, workId); + if (ret != ERR_OK) { + result.append("get work fail."); + return; + } + if (workInfos.size() == 0) { + result.append("no have workscheduler task."); + return; + } + for (auto &info : workInfos) { + info.Dump(result); + } +} + void WorkSchedulerService::HandleDeepIdleMsg() { if (!ready_) { @@ -1605,5 +1659,16 @@ void WorkSchedulerService::CancelTimer(int32_t id) return; #endif } + +bool WorkSchedulerService::CheckCallingToken() +{ + Security::AccessToken::AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID(); + auto tokenFlag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId); + if (tokenFlag == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE || + tokenFlag == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL) { + return true; + } + return false; +} } // namespace WorkScheduler } // namespace OHOS -- Gitee From 6272e3781648c92842a70b0f10401354a966d82c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E4=BC=9F?= Date: Thu, 17 Apr 2025 07:40:19 +0000 Subject: [PATCH 7/8] update services/test/src/workschedulerservice_test.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 胡伟 --- .../test/src/workschedulerservice_test.cpp | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/services/test/src/workschedulerservice_test.cpp b/services/test/src/workschedulerservice_test.cpp index 6e12089..4b20368 100644 --- a/services/test/src/workschedulerservice_test.cpp +++ b/services/test/src/workschedulerservice_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -112,6 +112,8 @@ class MyWorkSchedulerService : public WorkSchedServiceStub { int32_t StopAndClearWorks() { return 0; } int32_t IsLastWorkTimeout(int32_t workId, bool &result) { return 0; } int32_t ObtainAllWorks(std::vector& workInfos) { return 0; } + int32_t ObtainWorksByUidAndWorkIdForInner(int32_t uid, std::vector& workInfos, + int32_t workId) { return 0; } int32_t GetWorkStatus(int32_t workId, WorkInfo& workInfo) { return 0; } int32_t GetAllRunningWorks(std::vector& workInfos) { return 0; } int32_t PauseRunningWorks(int32_t uid) {return 0; } @@ -277,6 +279,31 @@ HWTEST_F(WorkSchedulerServiceTest, GetWorkStatus_001, TestSize.Level0) EXPECT_EQ(ret, E_WORK_NOT_EXIST_FAILED); } +/** + * @tc.name: ObtainWorksByUidAndWorkIdForInner_001 + * @tc.desc: Test WorkSchedulerService ObtainWorksByUidAndWorkIdForInner. + * @tc.type: FUNC + * @tc.require: IA4HTC + */ +HWTEST_F(WorkSchedulerServiceTest, ObtainWorksByUidAndWorkIdForInner_001, TestSize.Level0) +{ + std::vector workInfos; + int32_t uid = 1; + int32_t workId = 1; + workSchedulerService_->ready_ = false; + int32_t ret = -1; + ret = workSchedulerService_->ObtainWorksByUidAndWorkIdForInner(uid, workInfos, workId); + EXPECT_EQ(ret, E_SERVICE_NOT_READY); + + workSchedulerService_->ready_ = true; + ret = workSchedulerService_->ObtainWorksByUidAndWorkIdForInner(uid, workInfos, workId); + EXPECT_EQ(ret, E_WORK_NOT_EXIST_FAILED); + + workId = -1; + ret = workSchedulerService_->ObtainWorksByUidAndWorkIdForInner(uid, workInfos, workId); + EXPECT_EQ(ret, ERR_OK); +} + /** * @tc.name: GetAllRunningWorks_001 * @tc.desc: Test WorkSchedulerService GetAllRunningWorks. -- Gitee From 7906eba1e166114e5f53f693843ea004925f26bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E4=BC=9F?= Date: Thu, 17 Apr 2025 07:41:21 +0000 Subject: [PATCH 8/8] update utils/native/include/work_sched_errors.h. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 胡伟 --- utils/native/include/work_sched_errors.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/utils/native/include/work_sched_errors.h b/utils/native/include/work_sched_errors.h index 3a18cd4..9c18e8b 100644 --- a/utils/native/include/work_sched_errors.h +++ b/utils/native/include/work_sched_errors.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -35,6 +35,7 @@ enum { E_WORK_EXCEED_UPPER_LIMIT, E_REPEAT_CYCLE_TIME_ERR, E_INVALID_PROCESS_NAME, + E_PARAM_INVAILD_UID, // inner error code E_INNER_ERR, E_GROUP_CHANGE_NOT_MATCH_HAP, @@ -73,6 +74,7 @@ const inline std::map saErrCodeMsgMap = { {E_REPEAT_CYCLE_TIME_ERR, "StartWork failed. The repeatTime must be greater than or equal to 20 minutes."}, {E_WORK_EXCEED_UPPER_LIMIT, "StartWork failed. Each uid can add up to 10 works."}, {E_INVALID_PROCESS_NAME, "Pause or resume verify failed. caller process name invaild."}, + {E_PARAM_INVAILD_UID, "GetWork failed. The param uid invaild."}, {E_WORK_NOT_EXIST_FAILED, "The workId do not exist."}, }; -- Gitee