From 70a09346ddf9526a53394be674905bda2154b3b3 Mon Sep 17 00:00:00 2001 From: youshugen Date: Sun, 27 Apr 2025 19:16:36 +0800 Subject: [PATCH] fix: alarming improve Signed-off-by: youshugen --- frameworks/napi/power/BUILD.gn | 16 ++++++- frameworks/napi/power/power_napi.cpp | 19 +++++--- frameworks/native/power_mgr_client.cpp | 12 ++--- .../inner_api/native/include/ipower_mgr.h | 6 +-- .../native/include/power_mgr_client.h | 6 +-- powermgr.gni | 13 +++++- services/native/include/power_mgr_service.h | 6 +-- services/native/src/power_mgr_service.cpp | 45 ++++++++++++++++--- services/zidl/include/power_mgr_proxy.h | 6 +-- services/zidl/include/power_mgr_stub.h | 2 +- services/zidl/src/power_mgr_proxy.cpp | 28 ++++++++++-- services/zidl/src/power_mgr_stub.cpp | 13 +++--- utils/appmgr/include/app_manager_utils.h | 1 + utils/appmgr/src/app_manager_utils.cpp | 43 ++++++++++++++++++ 14 files changed, 174 insertions(+), 42 deletions(-) diff --git a/frameworks/napi/power/BUILD.gn b/frameworks/napi/power/BUILD.gn index e3f24ce9..cfd6453e 100644 --- a/frameworks/napi/power/BUILD.gn +++ b/frameworks/napi/power/BUILD.gn @@ -14,6 +14,13 @@ 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", @@ -27,7 +34,10 @@ ohos_shared_library("power") { "${powermgr_utils_path}:utils_config", "${powermgr_utils_path}:coverage_flags", ] - deps = [ "${powermgr_inner_api}:powermgr_client" ] + deps = [ + "${powermgr_inner_api}:powermgr_client", + "${powermgr_utils_path}/appmgr:power_appmgr", + ] deps_ex = [ "c_utils:utils", @@ -42,6 +52,10 @@ 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 738886c6..2b41a1bc 100644 --- a/frameworks/napi/power/power_napi.cpp +++ b/frameworks/napi/power/power_napi.cpp @@ -23,6 +23,7 @@ #include #include #include +#include "app_manager_utils.h" #define SET_REBOOT _IOW(BOOT_DETECTOR_IOCTL_BASE, 109, int) @@ -72,7 +73,9 @@ 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()); - PowerErrors code = g_powerMgrClient.WakeupDevice(WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, detail); + int32_t apiVersion = AppManagerUtils::GetApiTargetVersion(); + PowerErrors code = g_powerMgrClient.WakeupDevice( + WakeupDeviceType::WAKEUP_DEVICE_APPLICATION, detail, std::to_string(apiVersion)); if (code != PowerErrors::ERR_OK) { error.ThrowError(env, code); } @@ -99,10 +102,12 @@ 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(); + code = g_powerMgrClient.ForceSuspendDevice(std::to_string(apiVersion)); } else { - code = g_powerMgrClient.SuspendDevice(); + code = g_powerMgrClient.SuspendDevice( + SuspendDeviceType::SUSPEND_DEVICE_REASON_APPLICATION, false, std::to_string(apiVersion)); } if (code != PowerErrors::ERR_OK) { POWER_HILOGE(FEATURE_WAKEUP, "Suspend Device fail, isForce:%{public}d", isForce); @@ -130,7 +135,8 @@ napi_value PowerNapi::Hibernate(napi_env env, napi_callback_info info) bool clearMemory = false; napi_get_value_bool(env, argv[0], &clearMemory); - PowerErrors code = g_powerMgrClient.Hibernate(clearMemory); + int32_t apiVersion = AppManagerUtils::GetApiTargetVersion(); + PowerErrors code = g_powerMgrClient.Hibernate(clearMemory, std::to_string(apiVersion)); if (code != PowerErrors::ERR_OK) { POWER_HILOGE(FEATURE_WAKEUP, "Hibernate failed."); error.ThrowError(env, code); @@ -324,10 +330,11 @@ 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(); + code = g_powerMgrClient.RestoreScreenOffTime(std::to_string(apiVersion)); } else { - code = g_powerMgrClient.OverrideScreenOffTime(timeout); + code = g_powerMgrClient.OverrideScreenOffTime(timeout, std::to_string(apiVersion)); } 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 d79aa8be..5a26099a 100644 --- a/frameworks/native/power_mgr_client.cpp +++ b/frameworks/native/power_mgr_client.cpp @@ -235,7 +235,7 @@ bool PowerMgrClient::RefreshActivity(UserActivityType type) return ret; } -PowerErrors PowerMgrClient::OverrideScreenOffTime(int64_t timeout) +PowerErrors PowerMgrClient::OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion) { if (timeout <= 0) { POWER_HILOGW(COMP_FWK, "Invalid timeout, timeout=%{public}" PRId64 "", timeout); @@ -243,16 +243,16 @@ PowerErrors PowerMgrClient::OverrideScreenOffTime(int64_t timeout) } sptr proxy = GetPowerMgrProxy(); RETURN_IF_WITH_RET(proxy == nullptr, PowerErrors::ERR_CONNECTION_FAIL); - PowerErrors ret = proxy_->OverrideScreenOffTime(timeout); + PowerErrors ret = proxy->OverrideScreenOffTime(timeout, apiVersion); POWER_HILOGD(COMP_FWK, "Calling OverrideScreenOffTime Success"); return ret; } -PowerErrors PowerMgrClient::RestoreScreenOffTime() +PowerErrors PowerMgrClient::RestoreScreenOffTime(const std::string& apiVersion) { sptr proxy = GetPowerMgrProxy(); RETURN_IF_WITH_RET(proxy == nullptr, PowerErrors::ERR_CONNECTION_FAIL); - PowerErrors ret = proxy_->RestoreScreenOffTime(); + PowerErrors ret = proxy->RestoreScreenOffTime(apiVersion); POWER_HILOGD(COMP_FWK, "Calling RestoreScreenOffTime Success"); return ret; } @@ -470,11 +470,11 @@ bool PowerMgrClient::SetDisplaySuspend(bool enable) return ret; } -PowerErrors PowerMgrClient::Hibernate(bool clearMemory) +PowerErrors PowerMgrClient::Hibernate(bool clearMemory, const std::string& apiVersion) { sptr proxy = GetPowerMgrProxy(); RETURN_IF_WITH_RET(proxy == nullptr, PowerErrors::ERR_CONNECTION_FAIL); - return proxy_->Hibernate(clearMemory); + return proxy->Hibernate(clearMemory, apiVersion); } 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 bb63680b..3f728c63 100644 --- a/interfaces/inner_api/native/include/ipower_mgr.h +++ b/interfaces/inner_api/native/include/ipower_mgr.h @@ -63,8 +63,8 @@ public: const std::string& details, const std::string& apiVersion) = 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) = 0; - virtual PowerErrors RestoreScreenOffTime() = 0; + virtual PowerErrors OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion) = 0; + virtual PowerErrors RestoreScreenOffTime(const std::string& apiVersion) = 0; virtual PowerState GetState() = 0; virtual bool IsScreenOn(bool needPrintLog = true) = 0; virtual bool IsFoldScreenOn() = 0; @@ -88,7 +88,7 @@ public: virtual bool UnRegisterScreenStateCallback(const sptr& callback) = 0; virtual bool SetDisplaySuspend(bool enable) = 0; - virtual PowerErrors Hibernate(bool clearMemory) = 0; + virtual PowerErrors Hibernate(bool clearMemory, const std::string& apiVersion) = 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 e5a4edd9..a29135e1 100644 --- a/interfaces/inner_api/native/include/power_mgr_client.h +++ b/interfaces/inner_api/native/include/power_mgr_client.h @@ -97,12 +97,12 @@ public: * Windows overwrite timeout * @param timeout Specifies the timeout duration. */ - PowerErrors OverrideScreenOffTime(int64_t timeout); + PowerErrors OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion = "-1"); /** * Windows restores timeout */ - PowerErrors RestoreScreenOffTime(); + PowerErrors RestoreScreenOffTime(const std::string& apiVersion = "-1"); /** * Check whether the device screen is on. The result may be true or false, depending on the system state. @@ -144,7 +144,7 @@ public: * Hibernate the device. * @param clearMemory Indicates whether to clear the memory before the device hibernates. */ - PowerErrors Hibernate(bool clearMemory); + PowerErrors Hibernate(bool clearMemory, const std::string& apiVersion = "-1"); /* Set the device mode. * diff --git a/powermgr.gni b/powermgr.gni index 5b271b71..73ef5e31 100644 --- a/powermgr.gni +++ b/powermgr.gni @@ -41,6 +41,10 @@ declare_args() { defines = [] +if (power_manager_feature_enable_s4) { + defines += [ "POWER_MANAGER_POWER_ENABLE_S4" ] +} + if (power_manager_feature_audio_lock_unproxy) { defines += [ "POWER_MANAGER_AUDIO_LOCK_UNPROXY" ] } @@ -100,9 +104,14 @@ if (!defined(global_parts_info) || has_dfx_hiview_part = false } -if (power_manager_feature_enable_s4) { - defines += [ "POWER_MANAGER_POWER_ENABLE_S4" ] +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 42fbaaae..5cbdf15e 100644 --- a/services/native/include/power_mgr_service.h +++ b/services/native/include/power_mgr_service.h @@ -84,14 +84,14 @@ public: 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) override; - virtual PowerErrors RestoreScreenOffTime() override; + virtual PowerErrors OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion = "-1") override; + virtual PowerErrors RestoreScreenOffTime(const std::string& apiVersion = "-1") 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) override; + virtual PowerErrors Hibernate(bool clearMemory, const std::string& apiVersion = "-1") 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 ffa644ff..9e72802a 100644 --- a/services/native/src/power_mgr_service.cpp +++ b/services/native/src/power_mgr_service.cpp @@ -80,6 +80,7 @@ SysParam::BootCompletedCallback g_bootCompletedCallback; #ifdef POWER_PICKUP_ENABLE bool g_isPickUpOpen = false; #endif +constexpr int32_t API18 = 18; } // namespace std::atomic_bool PowerMgrService::isBootCompleted_ = false; @@ -1009,8 +1010,14 @@ PowerErrors PowerMgrService::SuspendDevice( 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) { @@ -1034,8 +1041,14 @@ PowerErrors PowerMgrService::WakeupDevice( { 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(); @@ -1089,29 +1102,39 @@ bool PowerMgrService::RefreshActivityInner(int64_t callTimeMs, UserActivityType return true; } -PowerErrors PowerMgrService::OverrideScreenOffTime(int64_t timeout) +PowerErrors PowerMgrService::OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion) { 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() +PowerErrors PowerMgrService::RestoreScreenOffTime(const std::string& apiVersion) { 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; @@ -1160,6 +1183,11 @@ 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; @@ -1169,13 +1197,18 @@ PowerErrors PowerMgrService::ForceSuspendDevice(int64_t callTimeMs, const std::s return PowerErrors::ERR_OK; } -PowerErrors PowerMgrService::Hibernate(bool clearMemory) +PowerErrors PowerMgrService::Hibernate(bool clearMemory, const std::string& apiVersion) { 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 5f7929b9..7f75e708 100644 --- a/services/zidl/include/power_mgr_proxy.h +++ b/services/zidl/include/power_mgr_proxy.h @@ -65,8 +65,8 @@ public: const std::string& details, const std::string& apiVersion = "-1") 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) override; - virtual PowerErrors RestoreScreenOffTime() override; + virtual PowerErrors OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion = "-1") override; + virtual PowerErrors RestoreScreenOffTime(const std::string& apiVersion = "-1") override; virtual PowerState GetState() override; virtual bool IsScreenOn(bool needPrintLog = true) override; virtual bool IsFoldScreenOn() override; @@ -85,7 +85,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) override; + virtual PowerErrors Hibernate(bool clearMemory, const std::string& apiVersion = "-1") 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 f8318635..ffdf6551 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& reply); + int32_t RestoreScreenOffTimeStub(MessageParcel& data, 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 0f738b5a..1b118ccf 100644 --- a/services/zidl/src/power_mgr_proxy.cpp +++ b/services/zidl/src/power_mgr_proxy.cpp @@ -27,8 +27,11 @@ 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,6 +478,7 @@ PowerErrors PowerMgrProxy::SetSuspendTag(const std::string& tag) PowerErrors PowerMgrProxy::SuspendDevice( int64_t callTimeMs, SuspendDeviceType reason, bool suspendImmed, const std::string& apiVersion) { + 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); @@ -491,6 +495,8 @@ 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); @@ -506,6 +512,7 @@ PowerErrors PowerMgrProxy::SuspendDevice( PowerErrors PowerMgrProxy::WakeupDevice( int64_t callTimeMs, WakeupDeviceType reason, const std::string& details, const std::string& apiVersion) { + 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); @@ -522,6 +529,8 @@ 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); @@ -587,8 +596,9 @@ bool PowerMgrProxy::RefreshActivity(int64_t callTimeMs, UserActivityType type, b return true; } -PowerErrors PowerMgrProxy::OverrideScreenOffTime(int64_t timeout) +PowerErrors PowerMgrProxy::OverrideScreenOffTime(int64_t timeout, const std::string& apiVersion) { + 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); @@ -602,6 +612,8 @@ PowerErrors PowerMgrProxy::OverrideScreenOffTime(int64_t timeout) } 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), @@ -616,8 +628,9 @@ PowerErrors PowerMgrProxy::OverrideScreenOffTime(int64_t timeout) return static_cast(error); } -PowerErrors PowerMgrProxy::RestoreScreenOffTime() +PowerErrors PowerMgrProxy::RestoreScreenOffTime(const std::string& apiVersion) { + 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); @@ -630,6 +643,9 @@ PowerErrors PowerMgrProxy::RestoreScreenOffTime() 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); @@ -645,6 +661,7 @@ PowerErrors PowerMgrProxy::RestoreScreenOffTime() PowerErrors PowerMgrProxy::ForceSuspendDevice(int64_t callTimeMs, const std::string& apiVersion) { + 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); @@ -658,6 +675,8 @@ 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()); @@ -1130,8 +1149,9 @@ bool PowerMgrProxy::SetDisplaySuspend(bool enable) return true; } -PowerErrors PowerMgrProxy::Hibernate(bool clearMemory) +PowerErrors PowerMgrProxy::Hibernate(bool clearMemory, const std::string& apiVersion) { + 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); @@ -1145,6 +1165,8 @@ PowerErrors PowerMgrProxy::Hibernate(bool clearMemory) } 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 4618d88d..38f1561b 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(reply); + ret = RestoreScreenOffTimeStub(data, reply); break; case static_cast(PowerMgr::PowerMgrInterfaceCode::GET_STATE): ret = GetStateStub(reply); @@ -429,8 +429,9 @@ 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); + PowerErrors error = OverrideScreenOffTime(timeout, apiVersion); if (!reply.WriteInt32(static_cast(error))) { POWER_HILOGE(COMP_FWK, "WriteInt32 fail"); return E_WRITE_PARCEL_ERROR; @@ -438,9 +439,10 @@ int32_t PowerMgrStub::OverrideScreenOffTimeStub(MessageParcel& data, MessageParc return ERR_OK; } -int32_t PowerMgrStub::RestoreScreenOffTimeStub(MessageParcel& reply) +int32_t PowerMgrStub::RestoreScreenOffTimeStub(MessageParcel& data, MessageParcel& reply) { - PowerErrors error = RestoreScreenOffTime(); + std::string apiVersion = Str16ToStr8(data.ReadString16()); + PowerErrors error = RestoreScreenOffTime(apiVersion); if (!reply.WriteInt32(static_cast(error))) { POWER_HILOGE(COMP_FWK, "WriteInt32 fail"); return E_WRITE_PARCEL_ERROR; @@ -648,9 +650,10 @@ 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); + PowerErrors error = Hibernate(clearMemory, apiVersion); 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 fa74e4c2..46605094 100644 --- a/utils/appmgr/include/app_manager_utils.h +++ b/utils/appmgr/include/app_manager_utils.h @@ -28,6 +28,7 @@ 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 c763cdb3..ef8a2ebc 100644 --- a/utils/appmgr/src/app_manager_utils.cpp +++ b/utils/appmgr/src/app_manager_utils.cpp @@ -14,6 +14,10 @@ */ #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 @@ -25,6 +29,9 @@ 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() { @@ -83,5 +90,41 @@ 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