From 5ce9d465d942ef087e6c6d74f21a09fd9888025e Mon Sep 17 00:00:00 2001 From: youshugen Date: Sun, 20 Jul 2025 15:11:36 +0800 Subject: [PATCH] fix: code improve Signed-off-by: youshugen --- frameworks/napi/power/BUILD.gn | 16 +----- frameworks/napi/power/power_napi.cpp | 19 +++---- frameworks/native/power_mgr_client.cpp | 26 +++++---- .../inner_api/native/include/ipower_mgr.h | 14 +++-- .../native/include/power_mgr_client.h | 12 ++--- powermgr.gni | 8 --- services/native/include/power_mgr_service.h | 14 +++-- services/native/src/power_mgr_service.cpp | 53 ++++--------------- services/zidl/include/power_mgr_proxy.h | 14 +++-- services/zidl/include/power_mgr_stub.h | 2 +- services/zidl/src/power_mgr_proxy.cpp | 36 +++---------- services/zidl/src/power_mgr_stub.cpp | 22 +++----- utils/appmgr/include/app_manager_utils.h | 1 - utils/appmgr/src/app_manager_utils.cpp | 43 --------------- 14 files changed, 67 insertions(+), 213 deletions(-) diff --git a/frameworks/napi/power/BUILD.gn b/frameworks/napi/power/BUILD.gn index cfd6453e..e3f24ce9 100644 --- a/frameworks/napi/power/BUILD.gn +++ b/frameworks/napi/power/BUILD.gn @@ -14,13 +14,6 @@ import("../../../powermgr.gni") ohos_shared_library("power") { - sanitize = { - cfi = true - cfi_cross_dso = true - debug = false - } - branch_protector_ret = "pac_ret" - include_dirs = [ "../utils" ] sources = [ "../utils/async_callback_info.cpp", @@ -34,10 +27,7 @@ ohos_shared_library("power") { "${powermgr_utils_path}:utils_config", "${powermgr_utils_path}:coverage_flags", ] - deps = [ - "${powermgr_inner_api}:powermgr_client", - "${powermgr_utils_path}/appmgr:power_appmgr", - ] + deps = [ "${powermgr_inner_api}:powermgr_client" ] deps_ex = [ "c_utils:utils", @@ -52,10 +42,6 @@ ohos_shared_library("power") { external_deps += [ "napi:ace_napi" ] } - if (has_ability_runtime_part) { - external_deps += [ "ability_runtime:app_manager" ] - } - relative_install_dir = "module" subsystem_name = "powermgr" diff --git a/frameworks/napi/power/power_napi.cpp b/frameworks/napi/power/power_napi.cpp index 2b41a1bc..738886c6 100644 --- a/frameworks/napi/power/power_napi.cpp +++ b/frameworks/napi/power/power_napi.cpp @@ -23,7 +23,6 @@ #include #include #include -#include "app_manager_utils.h" #define SET_REBOOT _IOW(BOOT_DETECTOR_IOCTL_BASE, 109, int) @@ -73,9 +72,7 @@ napi_value PowerNapi::Wakeup(napi_env env, napi_callback_info info) std::string detail = NapiUtils::GetStringFromNapi(env, argv[INDEX_0]); POWER_HILOGD(FEATURE_WAKEUP, "Wakeup type: APPLICATION, reason: %{public}s", detail.c_str()); - int32_t apiVersion = AppManagerUtils::GetApiTargetVersion(); - PowerErrors code = g_powerMgrClient.WakeupDevice( - WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, detail, std::to_string(apiVersion)); + PowerErrors code = g_powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, detail); if (code != PowerErrors::ERR_OK) { error.ThrowError(env, code); } @@ -102,12 +99,10 @@ napi_value PowerNapi::Suspend(napi_env env, napi_callback_info info) napi_get_value_bool(env, argv[0], &isForce); PowerErrors code; - int32_t apiVersion = AppManagerUtils::GetApiTargetVersion(); if (isForce) { - code = g_powerMgrClient.ForceSuspendDevice(std::to_string(apiVersion)); + code = g_powerMgrClient.ForceSuspendDevice(); } else { - code = g_powerMgrClient.SuspendDevice( - SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false, std::to_string(apiVersion)); + code = g_powerMgrClient.SuspendDevice(); } if (code != PowerErrors::ERR_OK) { POWER_HILOGE(FEATURE_WAKEUP, "Suspend Device fail, isForce:%{public}d", isForce); @@ -135,8 +130,7 @@ napi_value PowerNapi::Hibernate(napi_env env, napi_callback_info info) bool clearMemory = false; napi_get_value_bool(env, argv[0], &clearMemory); - int32_t apiVersion = AppManagerUtils::GetApiTargetVersion(); - PowerErrors code = g_powerMgrClient.Hibernate(clearMemory, std::to_string(apiVersion)); + PowerErrors code = g_powerMgrClient.Hibernate(clearMemory); if (code != PowerErrors::ERR_OK) { POWER_HILOGE(FEATURE_WAKEUP, "Hibernate failed."); error.ThrowError(env, code); @@ -330,11 +324,10 @@ napi_value PowerNapi::SetScreenOffTime(napi_env env, napi_callback_info info) } PowerErrors code; - int32_t apiVersion = AppManagerUtils::GetApiTargetVersion(); if (timeout == RESTORE_DEFAULT_SCREENOFF_TIME) { - code = g_powerMgrClient.RestoreScreenOffTime(std::to_string(apiVersion)); + code = g_powerMgrClient.RestoreScreenOffTime(); } else { - code = g_powerMgrClient.OverrideScreenOffTime(timeout, std::to_string(apiVersion)); + code = g_powerMgrClient.OverrideScreenOffTime(timeout); } if (code != PowerErrors::ERR_OK) { POWER_HILOGE(FEATURE_WAKEUP, "SetScreenOffTime failed."); diff --git a/frameworks/native/power_mgr_client.cpp b/frameworks/native/power_mgr_client.cpp index 5a26099a..a82bfb76 100644 --- a/frameworks/native/power_mgr_client.cpp +++ b/frameworks/native/power_mgr_client.cpp @@ -200,22 +200,20 @@ PowerErrors PowerMgrClient::SetSuspendTag(const std::string &tag) return proxy->SetSuspendTag(tag); } -PowerErrors PowerMgrClient::SuspendDevice( - SuspendDeviceType reason, bool suspendImmed, const std::string& apiVersion) +PowerErrors PowerMgrClient::SuspendDevice(SuspendDeviceType reason, bool suspendImmed) { sptr proxy = GetPowerMgrProxy(); RETURN_IF_WITH_RET(proxy == nullptr, PowerErrors::ERR_CONNECTION_FAIL); POWER_HILOGD(FEATURE_SUSPEND, " Calling SuspendDevice success"); - return proxy->SuspendDevice(GetTickCount(), reason, suspendImmed, apiVersion); + return proxy->SuspendDevice(GetTickCount(), reason, suspendImmed); } -PowerErrors PowerMgrClient::WakeupDevice( - WakeupDeviceType reason, const std::string& detail, const std::string& apiVersion) +PowerErrors PowerMgrClient::WakeupDevice(WakeupDeviceType reason, const std::string& detail) { sptr proxy = GetPowerMgrProxy(); RETURN_IF_WITH_RET(proxy == nullptr, PowerErrors::ERR_CONNECTION_FAIL); POWER_HILOGD(FEATURE_WAKEUP, " Calling WakeupDevice success"); - return proxy->WakeupDevice(GetTickCount(), reason, detail, apiVersion); + return proxy->WakeupDevice(GetTickCount(), reason, detail); } void PowerMgrClient::WakeupDeviceAsync(WakeupDeviceType reason, const std::string& detail) @@ -235,7 +233,7 @@ bool PowerMgrClient::RefreshActivity(UserActivityType type) return ret; } -PowerErrors PowerMgrClient::OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion) +PowerErrors PowerMgrClient::OverrideScreenOffTime(int64_t timeout) { if (timeout <= 0) { POWER_HILOGW(COMP_FWK, "Invalid timeout, timeout=%{public}" PRId64 "", timeout); @@ -243,16 +241,16 @@ PowerErrors PowerMgrClient::OverrideScreenOffTime(int64_t timeout, const std::st } sptr proxy = GetPowerMgrProxy(); RETURN_IF_WITH_RET(proxy == nullptr, PowerErrors::ERR_CONNECTION_FAIL); - PowerErrors ret = proxy->OverrideScreenOffTime(timeout, apiVersion); + PowerErrors ret = proxy->OverrideScreenOffTime(timeout); POWER_HILOGD(COMP_FWK, "Calling OverrideScreenOffTime Success"); return ret; } -PowerErrors PowerMgrClient::RestoreScreenOffTime(const std::string& apiVersion) +PowerErrors PowerMgrClient::RestoreScreenOffTime() { sptr proxy = GetPowerMgrProxy(); RETURN_IF_WITH_RET(proxy == nullptr, PowerErrors::ERR_CONNECTION_FAIL); - PowerErrors ret = proxy->RestoreScreenOffTime(apiVersion); + PowerErrors ret = proxy->RestoreScreenOffTime(); POWER_HILOGD(COMP_FWK, "Calling RestoreScreenOffTime Success"); return ret; } @@ -265,11 +263,11 @@ bool PowerMgrClient::IsRunningLockTypeSupported(RunningLockType type) return proxy->IsRunningLockTypeSupported(type); } -PowerErrors PowerMgrClient::ForceSuspendDevice(const std::string& apiVersion) +PowerErrors PowerMgrClient::ForceSuspendDevice() { sptr proxy = GetPowerMgrProxy(); RETURN_IF_WITH_RET(proxy == nullptr, PowerErrors::ERR_CONNECTION_FAIL); - PowerErrors ret = proxy->ForceSuspendDevice(GetTickCount(), apiVersion); + PowerErrors ret = proxy->ForceSuspendDevice(GetTickCount()); POWER_HILOGD(FEATURE_SUSPEND, "Calling ForceSuspendDevice Success"); return ret; } @@ -470,11 +468,11 @@ bool PowerMgrClient::SetDisplaySuspend(bool enable) return ret; } -PowerErrors PowerMgrClient::Hibernate(bool clearMemory, const std::string& apiVersion) +PowerErrors PowerMgrClient::Hibernate(bool clearMemory) { sptr proxy = GetPowerMgrProxy(); RETURN_IF_WITH_RET(proxy == nullptr, PowerErrors::ERR_CONNECTION_FAIL); - return proxy->Hibernate(clearMemory, apiVersion); + return proxy->Hibernate(clearMemory); } PowerErrors PowerMgrClient::SetDeviceMode(const PowerMode mode) diff --git a/interfaces/inner_api/native/include/ipower_mgr.h b/interfaces/inner_api/native/include/ipower_mgr.h index 3f728c63..5276f2a6 100644 --- a/interfaces/inner_api/native/include/ipower_mgr.h +++ b/interfaces/inner_api/native/include/ipower_mgr.h @@ -57,19 +57,17 @@ public: virtual PowerErrors RebootDeviceForDeprecated(const std::string& reason) = 0; virtual PowerErrors ShutDownDevice(const std::string& reason) = 0; virtual PowerErrors SetSuspendTag(const std::string& tag) = 0; - virtual PowerErrors SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, - bool suspendImmed, const std::string& apiVersion) = 0; - virtual PowerErrors WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, - const std::string& details, const std::string& apiVersion) = 0; + virtual PowerErrors SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) = 0; + virtual PowerErrors WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) = 0; virtual void WakeupDeviceAsync(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) = 0; virtual bool RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight) = 0; - virtual PowerErrors OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion) = 0; - virtual PowerErrors RestoreScreenOffTime(const std::string& apiVersion) = 0; + virtual PowerErrors OverrideScreenOffTime(int64_t timeout) = 0; + virtual PowerErrors RestoreScreenOffTime() = 0; virtual PowerState GetState() = 0; virtual bool IsScreenOn(bool needPrintLog = true) = 0; virtual bool IsFoldScreenOn() = 0; virtual bool IsCollaborationScreenOn() = 0; - virtual PowerErrors ForceSuspendDevice(int64_t callTimeMs, const std::string& apiVersion) = 0; + virtual PowerErrors ForceSuspendDevice(int64_t callTimeMs) = 0; virtual bool RegisterPowerStateCallback(const sptr& callback, bool isSync = true) = 0; virtual bool UnRegisterPowerStateCallback(const sptr& callback) = 0; @@ -88,7 +86,7 @@ public: virtual bool UnRegisterScreenStateCallback(const sptr& callback) = 0; virtual bool SetDisplaySuspend(bool enable) = 0; - virtual PowerErrors Hibernate(bool clearMemory, const std::string& apiVersion) = 0; + virtual PowerErrors Hibernate(bool clearMemory) = 0; virtual PowerErrors SetDeviceMode(const PowerMode& mode) = 0; virtual PowerMode GetDeviceMode() = 0; virtual std::string ShellDump(const std::vector& args, uint32_t argc) = 0; diff --git a/interfaces/inner_api/native/include/power_mgr_client.h b/interfaces/inner_api/native/include/power_mgr_client.h index a29135e1..345e6d86 100644 --- a/interfaces/inner_api/native/include/power_mgr_client.h +++ b/interfaces/inner_api/native/include/power_mgr_client.h @@ -66,7 +66,7 @@ public: * @param reason The reason why will you suspend the device, such as timeout/powerkey/forcesuspend and so on. */ PowerErrors SuspendDevice(SuspendDeviceType reason = SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, - bool suspendImmed = false, const std::string& apiVersion = "-1"); + bool suspendImmed = false); /** * Wake up the device and set the screen on. @@ -74,7 +74,7 @@ public: * @param reason The reason for waking up the device, such as powerkey/plugin/application. */ PowerErrors WakeupDevice(WakeupDeviceType reason = WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, - const std::string& detail = std::string("app call"), const std::string& apiVersion = "-1"); + const std::string& detail = std::string("app call")); /** * Wake up the device and set the screen on async. @@ -97,12 +97,12 @@ public: * Windows overwrite timeout * @param timeout Specifies the timeout duration. */ - PowerErrors OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion = "-1"); + PowerErrors OverrideScreenOffTime(int64_t timeout); /** * Windows restores timeout */ - PowerErrors RestoreScreenOffTime(const std::string& apiVersion = "-1"); + PowerErrors RestoreScreenOffTime(); /** * Check whether the device screen is on. The result may be true or false, depending on the system state. @@ -128,7 +128,7 @@ public: /** * Forcibly suspend the device into deepsleep, and return the suspend result. */ - PowerErrors ForceSuspendDevice(const std::string& apiVersion = "-1"); + PowerErrors ForceSuspendDevice(); /** * Check whether the type of running lock is supported @@ -144,7 +144,7 @@ public: * Hibernate the device. * @param clearMemory Indicates whether to clear the memory before the device hibernates. */ - PowerErrors Hibernate(bool clearMemory, const std::string& apiVersion = "-1"); + PowerErrors Hibernate(bool clearMemory); /* Set the device mode. * diff --git a/powermgr.gni b/powermgr.gni index 73ef5e31..4b7efb12 100644 --- a/powermgr.gni +++ b/powermgr.gni @@ -104,14 +104,6 @@ if (!defined(global_parts_info) || has_dfx_hiview_part = false } -if (!defined(global_parts_info) || - defined(global_parts_info.ability_ability_runtime)) { - has_ability_runtime_part = true - defines += [ "HAS_ABILITY_RUNTIME_PART" ] -} else { - has_ability_runtime_part = false -} - ability_runtime_path = "//foundation/ability/ability_runtime" ability_runtime_inner_api_path = "${ability_runtime_path}/interfaces/inner_api" diff --git a/services/native/include/power_mgr_service.h b/services/native/include/power_mgr_service.h index 404d0797..4ccbe28f 100644 --- a/services/native/include/power_mgr_service.h +++ b/services/native/include/power_mgr_service.h @@ -77,21 +77,19 @@ public: virtual PowerErrors RebootDeviceForDeprecated(const std::string& reason) override; virtual PowerErrors ShutDownDevice(const std::string& reason) override; virtual PowerErrors SetSuspendTag(const std::string& tag) override; - virtual PowerErrors SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, - bool suspendImmed, const std::string& apiVersion = "-1") override; - virtual PowerErrors WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, - const std::string& details, const std::string& apiVersion = "-1") override; + virtual PowerErrors SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) override; + virtual PowerErrors WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) override; virtual void WakeupDeviceAsync(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) override {}; virtual bool RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight) override; bool RefreshActivityInner(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight); - virtual PowerErrors OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion = "-1") override; - virtual PowerErrors RestoreScreenOffTime(const std::string& apiVersion = "-1") override; + virtual PowerErrors OverrideScreenOffTime(int64_t timeout) override; + virtual PowerErrors RestoreScreenOffTime() override; virtual PowerState GetState() override; virtual bool IsScreenOn(bool needPrintLog = true) override; virtual bool IsFoldScreenOn() override; virtual bool IsCollaborationScreenOn() override; - virtual PowerErrors ForceSuspendDevice(int64_t callTimeMs, const std::string& apiVersion = "-1") override; - virtual PowerErrors Hibernate(bool clearMemory, const std::string& apiVersion = "-1") override; + virtual PowerErrors ForceSuspendDevice(int64_t callTimeMs) override; + virtual PowerErrors Hibernate(bool clearMemory) override; virtual PowerErrors CreateRunningLock( const sptr& remoteObj, const RunningLockInfo& runningLockInfo) override; virtual bool ReleaseRunningLock(const sptr& remoteObj, const std::string& name = "") override; diff --git a/services/native/src/power_mgr_service.cpp b/services/native/src/power_mgr_service.cpp index f1811ba5..3eae1b27 100644 --- a/services/native/src/power_mgr_service.cpp +++ b/services/native/src/power_mgr_service.cpp @@ -81,7 +81,6 @@ static std::mutex powerInitMutex_; #ifdef POWER_PICKUP_ENABLE bool g_isPickUpOpen = false; #endif -constexpr int32_t API18 = 18; } // namespace std::atomic_bool PowerMgrService::isBootCompleted_ = false; @@ -1031,21 +1030,14 @@ PowerErrors PowerMgrService::SetSuspendTag(const std::string& tag) return PowerErrors::ERR_OK; } -PowerErrors PowerMgrService::SuspendDevice( - int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed, const std::string& apiVersion) +PowerErrors PowerMgrService::SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) { std::lock_guard lock(suspendMutex_); pid_t pid = IPCSkeleton::GetCallingPid(); auto uid = IPCSkeleton::GetCallingUid(); if (!Permission::IsSystem()) { - POWER_HILOGI(FEATURE_SUSPEND, "SuspendDevice failed, System permission intercept"); return PowerErrors::ERR_SYSTEM_API_DENIED; } - int32_t version = static_cast(strtol(apiVersion.c_str(), nullptr, 10)); - if (version >= API18 && !Permission::IsPermissionGranted("ohos.permission.POWER_MANAGER")) { - POWER_HILOGI(FEATURE_SUSPEND, "SuspendDevice failed, The application does not have the permission"); - return PowerErrors::ERR_PERMISSION_DENIED; - } #ifdef POWER_MANAGER_ENABLE_WATCH_BOOT_COMPLETED if (isBootCompleted_ == false) { @@ -1064,19 +1056,12 @@ PowerErrors PowerMgrService::SuspendDevice( return PowerErrors::ERR_OK; } -PowerErrors PowerMgrService::WakeupDevice( - int64_t callTimeMs, WakeupDeviceType reason, const std::string& details, const std::string& apiVersion) +PowerErrors PowerMgrService::WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) { std::lock_guard lock(wakeupMutex_); if (!Permission::IsSystem()) { - POWER_HILOGI(FEATURE_SUSPEND, "WakeupDevice failed, System permission intercept"); return PowerErrors::ERR_SYSTEM_API_DENIED; } - int32_t version = static_cast(strtol(apiVersion.c_str(), nullptr, 10)); - if (version >= API18 && !Permission::IsPermissionGranted("ohos.permission.POWER_MANAGER")) { - POWER_HILOGI(FEATURE_SUSPEND, "WakeupDevice failed, The application does not have the permission"); - return PowerErrors::ERR_PERMISSION_DENIED; - } pid_t pid = IPCSkeleton::GetCallingPid(); auto uid = IPCSkeleton::GetCallingUid(); @@ -1130,39 +1115,29 @@ bool PowerMgrService::RefreshActivityInner(int64_t callTimeMs, UserActivityType return true; } -PowerErrors PowerMgrService::OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion) +PowerErrors PowerMgrService::OverrideScreenOffTime(int64_t timeout) { std::lock_guard lock(screenMutex_); pid_t pid = IPCSkeleton::GetCallingPid(); auto uid = IPCSkeleton::GetCallingUid(); + POWER_HILOGI(COMP_SVC, + "Try to override screenOffTime, timeout=%{public}" PRId64 ", pid: %{public}d, uid: %{public}d", + timeout, pid, uid); if (!Permission::IsSystem()) { POWER_HILOGI(COMP_SVC, "OverrideScreenOffTime failed, System permission intercept"); return PowerErrors::ERR_SYSTEM_API_DENIED; } - int32_t version = static_cast(strtol(apiVersion.c_str(), nullptr, 10)); - if (version >= API18 && !Permission::IsPermissionGranted("ohos.permission.POWER_MANAGER")) { - POWER_HILOGI(FEATURE_SUSPEND, "OverrideScreenOffTime failed, The application does not have the permission"); - return PowerErrors::ERR_PERMISSION_DENIED; - } - POWER_HILOGI(COMP_SVC, - "Try to override screenOffTime, timeout=%{public}" PRId64 ", pid: %{public}d, uid: %{public}d", - timeout, pid, uid); return powerStateMachine_->OverrideScreenOffTimeInner(timeout) ? PowerErrors::ERR_OK : PowerErrors::ERR_FAILURE; } -PowerErrors PowerMgrService::RestoreScreenOffTime(const std::string& apiVersion) +PowerErrors PowerMgrService::RestoreScreenOffTime() { std::lock_guard lock(screenMutex_); if (!Permission::IsSystem()) { POWER_HILOGI(COMP_SVC, "RestoreScreenOffTime failed, System permission intercept"); return PowerErrors::ERR_SYSTEM_API_DENIED; } - int32_t version = static_cast(strtol(apiVersion.c_str(), nullptr, 10)); - if (version >= API18 && !Permission::IsPermissionGranted("ohos.permission.POWER_MANAGER")) { - POWER_HILOGI(FEATURE_SUSPEND, "RestoreScreenOffTime failed, The application does not have the permission"); - return PowerErrors::ERR_PERMISSION_DENIED; - } POWER_HILOGD(COMP_SVC, "Try to restore screen off time"); return powerStateMachine_->RestoreScreenOffTimeInner() ? PowerErrors::ERR_OK : PowerErrors::ERR_FAILURE; @@ -1202,7 +1177,7 @@ bool PowerMgrService::IsCollaborationScreenOn() return isCollaborationScreenOn; } -PowerErrors PowerMgrService::ForceSuspendDevice(int64_t callTimeMs, const std::string& apiVersion) +PowerErrors PowerMgrService::ForceSuspendDevice(int64_t callTimeMs) { std::lock_guard lock(suspendMutex_); pid_t pid = IPCSkeleton::GetCallingPid(); @@ -1211,11 +1186,6 @@ PowerErrors PowerMgrService::ForceSuspendDevice(int64_t callTimeMs, const std::s POWER_HILOGI(FEATURE_SUSPEND, "ForceSuspendDevice failed, System permission intercept"); return PowerErrors::ERR_SYSTEM_API_DENIED; } - int32_t version = static_cast(strtol(apiVersion.c_str(), nullptr, 10)); - if (version >= API18 && !Permission::IsPermissionGranted("ohos.permission.POWER_MANAGER")) { - POWER_HILOGI(FEATURE_SUSPEND, "ForceSuspendDevice failed, The application does not have the permission"); - return PowerErrors::ERR_PERMISSION_DENIED; - } if (shutdownController_->IsShuttingDown()) { POWER_HILOGI(FEATURE_SUSPEND, "System is shutting down, can't force suspend"); return PowerErrors::ERR_FAILURE; @@ -1225,18 +1195,13 @@ PowerErrors PowerMgrService::ForceSuspendDevice(int64_t callTimeMs, const std::s return PowerErrors::ERR_OK; } -PowerErrors PowerMgrService::Hibernate(bool clearMemory, const std::string& apiVersion) +PowerErrors PowerMgrService::Hibernate(bool clearMemory) { POWER_HILOGI(FEATURE_SUSPEND, "power mgr service hibernate begin."); if (!Permission::IsSystem()) { POWER_HILOGI(FEATURE_SUSPEND, "Hibernate failed, System permission intercept"); return PowerErrors::ERR_SYSTEM_API_DENIED; } - int32_t version = static_cast(strtol(apiVersion.c_str(), nullptr, 10)); - if (version >= API18 && !Permission::IsPermissionGranted("ohos.permission.POWER_MANAGER")) { - POWER_HILOGI(FEATURE_SUSPEND, "Hibernate failed, The application does not have the permission"); - return PowerErrors::ERR_PERMISSION_DENIED; - } #ifdef POWER_MANAGER_POWER_ENABLE_S4 std::lock_guard lock(hibernateMutex_); pid_t pid = IPCSkeleton::GetCallingPid(); diff --git a/services/zidl/include/power_mgr_proxy.h b/services/zidl/include/power_mgr_proxy.h index 7f75e708..dd501394 100644 --- a/services/zidl/include/power_mgr_proxy.h +++ b/services/zidl/include/power_mgr_proxy.h @@ -59,19 +59,17 @@ public: virtual bool ResetRunningLocks() override; virtual bool IsUsed(const sptr& remoteObj) override; // Use for PowerStateMachine - virtual PowerErrors SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, - bool suspendImmed, const std::string& apiVersion = "-1") override; - virtual PowerErrors WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, - const std::string& details, const std::string& apiVersion = "-1") override; + virtual PowerErrors SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) override; + virtual PowerErrors WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) override; virtual void WakeupDeviceAsync(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) override; virtual bool RefreshActivity(int64_t callTimeMs, UserActivityType type, bool needChangeBacklight) override; - virtual PowerErrors OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion = "-1") override; - virtual PowerErrors RestoreScreenOffTime(const std::string& apiVersion = "-1") override; + virtual PowerErrors OverrideScreenOffTime(int64_t timeout) override; + virtual PowerErrors RestoreScreenOffTime() override; virtual PowerState GetState() override; virtual bool IsScreenOn(bool needPrintLog = true) override; virtual bool IsFoldScreenOn() override; virtual bool IsCollaborationScreenOn() override; - virtual PowerErrors ForceSuspendDevice(int64_t callTimeMs, const std::string& apiVersion = "-1") override; + virtual PowerErrors ForceSuspendDevice(int64_t callTimeMs) override; virtual PowerErrors RebootDevice(const std::string& reason) override; virtual PowerErrors RebootDeviceForDeprecated(const std::string& reason) override; virtual PowerErrors ShutDownDevice(const std::string& reason) override; @@ -85,7 +83,7 @@ public: virtual bool RegisterRunningLockCallback(const sptr& callback) override; virtual bool UnRegisterRunningLockCallback(const sptr& callback) override; virtual bool SetDisplaySuspend(bool enable) override; - virtual PowerErrors Hibernate(bool clearMemory, const std::string& apiVersion = "-1") override; + virtual PowerErrors Hibernate(bool clearMemory) override; virtual PowerErrors SetDeviceMode(const PowerMode& mode) override; virtual PowerMode GetDeviceMode() override; virtual std::string ShellDump(const std::vector& args, uint32_t argc) override; diff --git a/services/zidl/include/power_mgr_stub.h b/services/zidl/include/power_mgr_stub.h index ffdf6551..f8318635 100644 --- a/services/zidl/include/power_mgr_stub.h +++ b/services/zidl/include/power_mgr_stub.h @@ -37,7 +37,7 @@ private: int32_t SuspendDeviceStub(MessageParcel& data, MessageParcel& reply); int32_t RefreshActivityStub(MessageParcel& data); int32_t OverrideScreenOffTimeStub(MessageParcel& data, MessageParcel& reply); - int32_t RestoreScreenOffTimeStub(MessageParcel& data, MessageParcel& reply); + int32_t RestoreScreenOffTimeStub(MessageParcel& reply); int32_t GetStateStub(MessageParcel& reply); int32_t IsScreenOnStub(MessageParcel& data, MessageParcel& reply); int32_t IsFoldScreenOnStub(MessageParcel& reply); diff --git a/services/zidl/src/power_mgr_proxy.cpp b/services/zidl/src/power_mgr_proxy.cpp index 1b118ccf..bb23defe 100644 --- a/services/zidl/src/power_mgr_proxy.cpp +++ b/services/zidl/src/power_mgr_proxy.cpp @@ -27,11 +27,8 @@ namespace OHOS { namespace PowerMgr { -namespace { constexpr int32_t MAX_PARAM_NUM = 2000; constexpr uint32_t MAX_PROXY_RUNNINGLOCK_NUM = 2000; -constexpr int32_t MAX_VERSION_STRING_SIZE = 4; -} PowerErrors PowerMgrProxy::CreateRunningLock(const sptr& remoteObj, const RunningLockInfo& runningLockInfo) { @@ -475,10 +472,8 @@ PowerErrors PowerMgrProxy::SetSuspendTag(const std::string& tag) return static_cast(error); } -PowerErrors PowerMgrProxy::SuspendDevice( - int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed, const std::string& apiVersion) +PowerErrors PowerMgrProxy::SuspendDevice(int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed) { - RETURN_IF_WITH_RET(apiVersion.size() >= MAX_VERSION_STRING_SIZE, PowerErrors::ERR_PARAM_INVALID); sptr remote = Remote(); RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL); @@ -495,8 +490,6 @@ PowerErrors PowerMgrProxy::SuspendDevice( RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Uint32, static_cast(reason), PowerErrors::ERR_CONNECTION_FAIL); RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, suspendImmed, PowerErrors::ERR_CONNECTION_FAIL); - RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET( - data, String16, Str8ToStr16(apiVersion), PowerErrors::ERR_CONNECTION_FAIL); int ret = remote->SendRequest( static_cast(PowerMgr::PowerMgrInterfaceCode::SUSPEND_DEVICE), data, reply, option); @@ -509,10 +502,8 @@ PowerErrors PowerMgrProxy::SuspendDevice( return static_cast(error); } -PowerErrors PowerMgrProxy::WakeupDevice( - int64_t callTimeMs, WakeupDeviceType reason, const std::string& details, const std::string& apiVersion) +PowerErrors PowerMgrProxy::WakeupDevice(int64_t callTimeMs, WakeupDeviceType reason, const std::string& details) { - RETURN_IF_WITH_RET(apiVersion.size() >= MAX_VERSION_STRING_SIZE, PowerErrors::ERR_PARAM_INVALID); sptr remote = Remote(); RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL); @@ -529,8 +520,6 @@ PowerErrors PowerMgrProxy::WakeupDevice( RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Uint32, static_cast(reason), PowerErrors::ERR_CONNECTION_FAIL); RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, String16, Str8ToStr16(details), PowerErrors::ERR_CONNECTION_FAIL); - RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET( - data, String16, Str8ToStr16(apiVersion), PowerErrors::ERR_CONNECTION_FAIL); int ret = remote->SendRequest( static_cast(PowerMgr::PowerMgrInterfaceCode::WAKEUP_DEVICE), data, reply, option); @@ -596,9 +585,8 @@ bool PowerMgrProxy::RefreshActivity(int64_t callTimeMs, UserActivityType type, b return true; } -PowerErrors PowerMgrProxy::OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion) +PowerErrors PowerMgrProxy::OverrideScreenOffTime(int64_t timeout) { - RETURN_IF_WITH_RET(apiVersion.size() >= MAX_VERSION_STRING_SIZE, PowerErrors::ERR_PARAM_INVALID); sptr remote = Remote(); RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL); @@ -612,8 +600,6 @@ PowerErrors PowerMgrProxy::OverrideScreenOffTime(int64_t timeout, const std::str } RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int64, timeout, PowerErrors::ERR_CONNECTION_FAIL); - RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET( - data, String16, Str8ToStr16(apiVersion), PowerErrors::ERR_CONNECTION_FAIL); int ret = remote->SendRequest( static_cast(PowerMgr::PowerMgrInterfaceCode::OVERRIDE_DISPLAY_OFF_TIME), @@ -628,9 +614,8 @@ PowerErrors PowerMgrProxy::OverrideScreenOffTime(int64_t timeout, const std::str return static_cast(error); } -PowerErrors PowerMgrProxy::RestoreScreenOffTime(const std::string& apiVersion) +PowerErrors PowerMgrProxy::RestoreScreenOffTime() { - RETURN_IF_WITH_RET(apiVersion.size() >= MAX_VERSION_STRING_SIZE, PowerErrors::ERR_PARAM_INVALID); sptr remote = Remote(); RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL); @@ -643,9 +628,6 @@ PowerErrors PowerMgrProxy::RestoreScreenOffTime(const std::string& apiVersion) return PowerErrors::ERR_CONNECTION_FAIL; } - RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET( - data, String16, Str8ToStr16(apiVersion), PowerErrors::ERR_CONNECTION_FAIL); - int ret = remote->SendRequest( static_cast(PowerMgr::PowerMgrInterfaceCode::RESTORE_DISPLAY_OFF_TIME), data, reply, option); @@ -659,9 +641,8 @@ PowerErrors PowerMgrProxy::RestoreScreenOffTime(const std::string& apiVersion) return static_cast(error); } -PowerErrors PowerMgrProxy::ForceSuspendDevice(int64_t callTimeMs, const std::string& apiVersion) +PowerErrors PowerMgrProxy::ForceSuspendDevice(int64_t callTimeMs) { - RETURN_IF_WITH_RET(apiVersion.size() >= MAX_VERSION_STRING_SIZE, PowerErrors::ERR_PARAM_INVALID); sptr remote = Remote(); RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL); @@ -675,8 +656,6 @@ PowerErrors PowerMgrProxy::ForceSuspendDevice(int64_t callTimeMs, const std::str } RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Int64, callTimeMs, PowerErrors::ERR_CONNECTION_FAIL); - RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET( - data, String16, Str8ToStr16(apiVersion), PowerErrors::ERR_CONNECTION_FAIL); sptr asyncCallback = new PowerMgrStubAsync(); data.WriteRemoteObject(asyncCallback->AsObject()); @@ -1149,9 +1128,8 @@ bool PowerMgrProxy::SetDisplaySuspend(bool enable) return true; } -PowerErrors PowerMgrProxy::Hibernate(bool clearMemory, const std::string& apiVersion) +PowerErrors PowerMgrProxy::Hibernate(bool clearMemory) { - RETURN_IF_WITH_RET(apiVersion.size() >= MAX_VERSION_STRING_SIZE, PowerErrors::ERR_PARAM_INVALID); sptr remote = Remote(); RETURN_IF_WITH_RET(remote == nullptr, PowerErrors::ERR_CONNECTION_FAIL); @@ -1165,8 +1143,6 @@ PowerErrors PowerMgrProxy::Hibernate(bool clearMemory, const std::string& apiVer } RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(data, Bool, clearMemory, PowerErrors::ERR_CONNECTION_FAIL); - RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET( - data, String16, Str8ToStr16(apiVersion), PowerErrors::ERR_CONNECTION_FAIL); sptr asyncCallback = new PowerMgrStubAsync(); data.WriteRemoteObject(asyncCallback->AsObject()); diff --git a/services/zidl/src/power_mgr_stub.cpp b/services/zidl/src/power_mgr_stub.cpp index 38f1561b..165b869d 100644 --- a/services/zidl/src/power_mgr_stub.cpp +++ b/services/zidl/src/power_mgr_stub.cpp @@ -93,7 +93,7 @@ int PowerMgrStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessagePar ret = OverrideScreenOffTimeStub(data, reply); break; case static_cast(PowerMgr::PowerMgrInterfaceCode::RESTORE_DISPLAY_OFF_TIME): - ret = RestoreScreenOffTimeStub(data, reply); + ret = RestoreScreenOffTimeStub(reply); break; case static_cast(PowerMgr::PowerMgrInterfaceCode::GET_STATE): ret = GetStateStub(reply); @@ -387,9 +387,8 @@ int32_t PowerMgrStub::WakeupDeviceStub(MessageParcel& data, MessageParcel& reply RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR); RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, reason, E_READ_PARCEL_ERROR); std::string details = Str16ToStr8(data.ReadString16()); - std::string apiVersion = Str16ToStr8(data.ReadString16()); - PowerErrors error = WakeupDevice(time, static_cast(reason), details, apiVersion); + PowerErrors error = WakeupDevice(time, static_cast(reason), details); RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast(error), E_WRITE_PARCEL_ERROR); return ERR_OK; } @@ -403,9 +402,8 @@ int32_t PowerMgrStub::SuspendDeviceStub(MessageParcel& data, MessageParcel& repl RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR); RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Uint32, reason, E_READ_PARCEL_ERROR); RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, suspendImmed, E_READ_PARCEL_ERROR); - std::string apiVersion = Str16ToStr8(data.ReadString16()); - PowerErrors error = SuspendDevice(time, static_cast(reason), suspendImmed, apiVersion); + PowerErrors error = SuspendDevice(time, static_cast(reason), suspendImmed); RETURN_IF_WRITE_PARCEL_FAILED_WITH_RET(reply, Int32, static_cast(error), E_WRITE_PARCEL_ERROR); return ERR_OK; } @@ -429,9 +427,8 @@ int32_t PowerMgrStub::OverrideScreenOffTimeStub(MessageParcel& data, MessageParc int64_t timeout = 0; RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int64, timeout, E_READ_PARCEL_ERROR); - std::string apiVersion = Str16ToStr8(data.ReadString16()); - PowerErrors error = OverrideScreenOffTime(timeout, apiVersion); + PowerErrors error = OverrideScreenOffTime(timeout); if (!reply.WriteInt32(static_cast(error))) { POWER_HILOGE(COMP_FWK, "WriteInt32 fail"); return E_WRITE_PARCEL_ERROR; @@ -439,10 +436,9 @@ int32_t PowerMgrStub::OverrideScreenOffTimeStub(MessageParcel& data, MessageParc return ERR_OK; } -int32_t PowerMgrStub::RestoreScreenOffTimeStub(MessageParcel& data, MessageParcel& reply) +int32_t PowerMgrStub::RestoreScreenOffTimeStub(MessageParcel& reply) { - std::string apiVersion = Str16ToStr8(data.ReadString16()); - PowerErrors error = RestoreScreenOffTime(apiVersion); + PowerErrors error = RestoreScreenOffTime(); if (!reply.WriteInt32(static_cast(error))) { POWER_HILOGE(COMP_FWK, "WriteInt32 fail"); return E_WRITE_PARCEL_ERROR; @@ -455,10 +451,9 @@ int32_t PowerMgrStub::ForceSuspendDeviceStub(MessageParcel& data, MessageParcel& int64_t time = 0; RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Int64, time, E_READ_PARCEL_ERROR); - std::string apiVersion = Str16ToStr8(data.ReadString16()); sptr powerProxy = iface_cast(data.ReadRemoteObject()); - PowerErrors error = ForceSuspendDevice(time, apiVersion); + PowerErrors error = ForceSuspendDevice(time); int result = static_cast(error); if (powerProxy != nullptr) { powerProxy->SendAsyncReply(result); @@ -650,10 +645,9 @@ int32_t PowerMgrStub::HibernateStub(MessageParcel& data, MessageParcel& reply) { bool clearMemory = false; RETURN_IF_READ_PARCEL_FAILED_WITH_RET(data, Bool, clearMemory, E_READ_PARCEL_ERROR); - std::string apiVersion = Str16ToStr8(data.ReadString16()); sptr powerProxy = iface_cast(data.ReadRemoteObject()); - PowerErrors error = Hibernate(clearMemory, apiVersion); + PowerErrors error = Hibernate(clearMemory); int result = static_cast(error); if (powerProxy != nullptr) { powerProxy->SendAsyncReply(result); diff --git a/utils/appmgr/include/app_manager_utils.h b/utils/appmgr/include/app_manager_utils.h index 46605094..fa74e4c2 100644 --- a/utils/appmgr/include/app_manager_utils.h +++ b/utils/appmgr/include/app_manager_utils.h @@ -28,7 +28,6 @@ public: static sptr GetAppManagerInstance(); static void GetForegroundApplications(std::vector& appsData); static bool IsForegroundApplication(const std::string& appName); - static int32_t GetApiTargetVersion(); private: static sptr appManagerInstance_; diff --git a/utils/appmgr/src/app_manager_utils.cpp b/utils/appmgr/src/app_manager_utils.cpp index ef8a2ebc..c763cdb3 100644 --- a/utils/appmgr/src/app_manager_utils.cpp +++ b/utils/appmgr/src/app_manager_utils.cpp @@ -14,10 +14,6 @@ */ #include "app_manager_utils.h" -#ifdef HAS_ABILITY_RUNTIME_PART -#include "bundle_mgr_interface.h" -#include "system_ability_definition.h" -#endif #include "power_log.h" #include @@ -29,9 +25,6 @@ namespace OHOS { namespace PowerMgr { static constexpr uint32_t APP_MGR_SERVICE_ID = 501; sptr AppManagerUtils::appManagerInstance_ = nullptr; -namespace { -const int32_t API_VERSION_MOD = 1000; -} sptr AppManagerUtils::GetAppManagerInstance() { @@ -90,41 +83,5 @@ bool AppManagerUtils::IsForegroundApplication(const std::string& appName) return IsForeground; } -int32_t AppManagerUtils::GetApiTargetVersion() -{ -#ifdef HAS_ABILITY_RUNTIME_PART - static int32_t apiTargetVersion = -1; - if (apiTargetVersion != -1) { - return apiTargetVersion; - } - sptr saManager = - OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (saManager == nullptr) { - POWER_HILOGE(FEATURE_UTIL, "Failed to get ISystemAbilityManager"); - return 0; - } - sptr remoteObject = saManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - sptr bundleMgrProxy = iface_cast(remoteObject); - if (bundleMgrProxy == nullptr) { - POWER_HILOGE(FEATURE_UTIL, "GetApiTargetVersion: bundleMgrProxy is nullptr"); - return 0; - } - OHOS::AppExecFwk::BundleInfo bundleInfo; - auto ret = - bundleMgrProxy->GetBundleInfoForSelf(OHOS::AppExecFwk::BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo); - if (ret != 0) { - POWER_HILOGI(FEATURE_UTIL, "GetApiTargetVersion: GetBundleInfoForSelf failed"); - return 0; - } - int32_t hapApiVersion = bundleInfo.applicationInfo.apiTargetVersion % API_VERSION_MOD; - apiTargetVersion = hapApiVersion; - POWER_HILOGI(FEATURE_UTIL, "GetApiTargetVersion: hapApiVersion is %{public}d", hapApiVersion); - return hapApiVersion; -#else - POWER_HILOGI(FEATURE_UTIL, "GetApiTargetVersion not support"); - return -1; -#endif -} - } // namespace PowerMgr } // namespace OHOS \ No newline at end of file -- Gitee