From f14c4d3afb3b64ecfcdfc462dfe859255f41221e Mon Sep 17 00:00:00 2001 From: lixiangpeng5 Date: Sat, 27 Jul 2024 12:59:43 +0000 Subject: [PATCH 1/4] add version isolation Signed-off-by: lixiangpeng5 Change-Id: Ic1fa126bfbe54ba6242c2a14a1862f618f38bf8a --- .../js/napi/include/sensor_napi_utils.h | 1 + frameworks/js/napi/src/sensor_js.cpp | 2 + frameworks/js/napi/src/sensor_napi_utils.cpp | 45 ++++++++++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/frameworks/js/napi/include/sensor_napi_utils.h b/frameworks/js/napi/include/sensor_napi_utils.h index a3b857a5..ec8e56fa 100644 --- a/frameworks/js/napi/include/sensor_napi_utils.h +++ b/frameworks/js/napi/include/sensor_napi_utils.h @@ -59,6 +59,7 @@ bool CreateFailMessage(CallbackDataType type, int32_t code, string message, bool ConvertToBodyData(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); bool ConvertToCompass(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); void ReleaseCallback(sptr asyncCallbackInfo); +int32_t GetTargetSDKVersion(int32_t pid); #define CHKNCF(env, cond, message) \ diff --git a/frameworks/js/napi/src/sensor_js.cpp b/frameworks/js/napi/src/sensor_js.cpp index 655b95c1..cd773a03 100644 --- a/frameworks/js/napi/src/sensor_js.cpp +++ b/frameworks/js/napi/src/sensor_js.cpp @@ -1036,6 +1036,8 @@ static napi_value GetSensorListSync(napi_env env, napi_callback_info info) static napi_value GetSingleSensor(napi_env env, napi_callback_info info) { CALL_LOG_ENTER; + int32_t targetSdkVersion = GetTargetSDKVersion(getpid()); + SEN_HILOGE("targetSdkVersion = %{public}d", targetSdkVersion); size_t argc = 2; napi_value args[2] = { 0 }; napi_value thisVar = nullptr; diff --git a/frameworks/js/napi/src/sensor_napi_utils.cpp b/frameworks/js/napi/src/sensor_napi_utils.cpp index dbc0cc98..2e38ff53 100644 --- a/frameworks/js/napi/src/sensor_napi_utils.cpp +++ b/frameworks/js/napi/src/sensor_napi_utils.cpp @@ -19,12 +19,17 @@ #include #include +#include "app_mgr_client.h" +#include "bundle_mgr_client.h" +#include "os_account_manager.h" + #include "sensor_napi_error.h" namespace OHOS { namespace Sensors { namespace { - constexpr int32_t STRING_LENGTH_MAX = 64; +constexpr int32_t STRING_LENGTH_MAX = 64; +constexpr int32_t INVALID_TARGET_VERSION = -1; } // namespace bool IsSameValue(const napi_env &env, const napi_value &lhs, const napi_value &rhs) { @@ -607,5 +612,43 @@ void EmitPromiseWork(sptr asyncCallbackInfo) asyncCallbackInfo->DecStrongRef(nullptr); } } + +int32_t GetTargetSDKVersion(int32_t pid) +{ + AppExecFwk::RunningProcessInfo processinfo; + appMgrClientPtr_ = DelayedSingleton::GetInstance(); + if (appMgrClientPtr_ == nullptr) { + MISC_HILOGE("appMgrClientPtr is nullptr"); + return INVALID_TARGET_VERSION; + } + int32_t ret = appMgrClientPtr_->AppExecFwk::AppMgrClient::GetRunningProcessInfoByPid(pid, processinfo); + if (ret != ERR_OK) { + MISC_HILOGE("Getrunningprocessinfobypid failed"); + return INVALID_TARGET_VERSION; + } + std::vector activeUserIds; + int32_t retId = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserIds); + if (retId != 0) { + MISC_HILOGE("QueryActiveOsAccountIds failed %{public}d", retId); + return INVALID_TARGET_VERSION; + } + if (activeUserIds.empty()) { + MISC_HILOGE("activeUserId empty"); + return INVALID_TARGET_VERSION; + } + for (const auto &bundleName : processinfo.bundleNames) { + MISC_HILOGD("bundleName = %{public}s", bundleName.c_str()); + AppExecFwk::BundleMgrClient bundleMgrClient; + AppExecFwk::BundleInfo bundleInfo; + auto res = bundleMgrClient.AppExecFwk::BundleMgrClient::GetBundleInfo(bundleName, + AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, activeUserIds[0]); + if (!res) { + MISC_HILOGE("Getbundleinfo fail"); + return INVALID_TARGET_VERSION; + } + return bundleInfo.targetVersion; + } + return INVALID_TARGET_VERSION; +} } // namespace Sensors } // namespace OHOS \ No newline at end of file -- Gitee From f6a7e7bb3cbeb90f4ca527ca3ddd947f09e116ef Mon Sep 17 00:00:00 2001 From: lixiangpeng5 Date: Sat, 27 Jul 2024 14:00:58 +0000 Subject: [PATCH 2/4] fix by debug Signed-off-by: lixiangpeng5 Change-Id: I09786823f367321e8c2c1bfaa1a0121cc982cb1b --- bundle.json | 5 ++++- frameworks/js/napi/BUILD.gn | 3 +++ frameworks/js/napi/src/sensor_napi_utils.cpp | 18 +++++++----------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/bundle.json b/bundle.json index cccf86e7..97a5e358 100755 --- a/bundle.json +++ b/bundle.json @@ -19,6 +19,8 @@ ], "deps": { "components": [ + "ability_runtime", + "bundle_framework", "c_utils", "hilog", "build_framework", @@ -31,7 +33,8 @@ "memmgr", "safwk", "samgr", - "eventhandler" + "eventhandler", + "os_account" ], "third_party": [] }, diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 263d4b1f..7e3651a2 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -39,9 +39,12 @@ ohos_shared_library("libsensor") { } deps = [ "$SUBSYSTEM_DIR/frameworks/native:sensor_interface_native" ] external_deps = [ + "ability_runtime:app_manager", + "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", "napi:ace_napi", + "os_account:os_account_innerkits" ] relative_install_dir = "module" part_name = "sensor" diff --git a/frameworks/js/napi/src/sensor_napi_utils.cpp b/frameworks/js/napi/src/sensor_napi_utils.cpp index 2e38ff53..e82bdafe 100644 --- a/frameworks/js/napi/src/sensor_napi_utils.cpp +++ b/frameworks/js/napi/src/sensor_napi_utils.cpp @@ -616,34 +616,30 @@ void EmitPromiseWork(sptr asyncCallbackInfo) int32_t GetTargetSDKVersion(int32_t pid) { AppExecFwk::RunningProcessInfo processinfo; - appMgrClientPtr_ = DelayedSingleton::GetInstance(); - if (appMgrClientPtr_ == nullptr) { - MISC_HILOGE("appMgrClientPtr is nullptr"); - return INVALID_TARGET_VERSION; - } - int32_t ret = appMgrClientPtr_->AppExecFwk::AppMgrClient::GetRunningProcessInfoByPid(pid, processinfo); + AppExecFwk::AppMgrClient appMgrClient; + int32_t ret = appMgrClient.AppExecFwk::AppMgrClient::GetRunningProcessInfoByPid(pid, processinfo); if (ret != ERR_OK) { - MISC_HILOGE("Getrunningprocessinfobypid failed"); + SEN_HILOGE("Getrunningprocessinfobypid failed"); return INVALID_TARGET_VERSION; } std::vector activeUserIds; int32_t retId = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserIds); if (retId != 0) { - MISC_HILOGE("QueryActiveOsAccountIds failed %{public}d", retId); + SEN_HILOGE("QueryActiveOsAccountIds failed %{public}d", retId); return INVALID_TARGET_VERSION; } if (activeUserIds.empty()) { - MISC_HILOGE("activeUserId empty"); + SEN_HILOGE("activeUserId empty"); return INVALID_TARGET_VERSION; } for (const auto &bundleName : processinfo.bundleNames) { - MISC_HILOGD("bundleName = %{public}s", bundleName.c_str()); + SEN_HILOGD("bundleName = %{public}s", bundleName.c_str()); AppExecFwk::BundleMgrClient bundleMgrClient; AppExecFwk::BundleInfo bundleInfo; auto res = bundleMgrClient.AppExecFwk::BundleMgrClient::GetBundleInfo(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, activeUserIds[0]); if (!res) { - MISC_HILOGE("Getbundleinfo fail"); + SEN_HILOGE("Getbundleinfo fail"); return INVALID_TARGET_VERSION; } return bundleInfo.targetVersion; -- Gitee From 7fc969445f4ef4645aa13faae38f1791406dc989 Mon Sep 17 00:00:00 2001 From: lixiangpeng5 Date: Sun, 28 Jul 2024 03:43:46 +0000 Subject: [PATCH 3/4] fix call func Signed-off-by: lixiangpeng5 Change-Id: Id351aa4c79e3a01ffbb782d0056ff3f2b14844d2 --- bundle.json | 4 +- frameworks/js/napi/BUILD.gn | 6 +- .../js/napi/include/sensor_napi_utils.h | 2 +- frameworks/js/napi/src/sensor_js.cpp | 13 ++++- frameworks/js/napi/src/sensor_napi_utils.cpp | 56 +++++++++---------- 5 files changed, 42 insertions(+), 39 deletions(-) diff --git a/bundle.json b/bundle.json index 97a5e358..72c47298 100755 --- a/bundle.json +++ b/bundle.json @@ -19,7 +19,6 @@ ], "deps": { "components": [ - "ability_runtime", "bundle_framework", "c_utils", "hilog", @@ -33,8 +32,7 @@ "memmgr", "safwk", "samgr", - "eventhandler", - "os_account" + "eventhandler" ], "third_party": [] }, diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 7e3651a2..9dd4e8bb 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -39,12 +39,14 @@ ohos_shared_library("libsensor") { } deps = [ "$SUBSYSTEM_DIR/frameworks/native:sensor_interface_native" ] external_deps = [ - "ability_runtime:app_manager", + "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", + "ipc:ipc_single", "napi:ace_napi", - "os_account:os_account_innerkits" + "safwk:system_ability_fwk", + "samgr:samgr_proxy", ] relative_install_dir = "module" part_name = "sensor" diff --git a/frameworks/js/napi/include/sensor_napi_utils.h b/frameworks/js/napi/include/sensor_napi_utils.h index ec8e56fa..a7ce7588 100644 --- a/frameworks/js/napi/include/sensor_napi_utils.h +++ b/frameworks/js/napi/include/sensor_napi_utils.h @@ -59,7 +59,7 @@ bool CreateFailMessage(CallbackDataType type, int32_t code, string message, bool ConvertToBodyData(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); bool ConvertToCompass(const napi_env &env, sptr asyncCallbackInfo, napi_value result[2]); void ReleaseCallback(sptr asyncCallbackInfo); -int32_t GetTargetSDKVersion(int32_t pid); +bool GetSelfTargetVersion(uint32_t &targetVersion); #define CHKNCF(env, cond, message) \ diff --git a/frameworks/js/napi/src/sensor_js.cpp b/frameworks/js/napi/src/sensor_js.cpp index cd773a03..26aaf92f 100644 --- a/frameworks/js/napi/src/sensor_js.cpp +++ b/frameworks/js/napi/src/sensor_js.cpp @@ -41,6 +41,7 @@ constexpr int32_t SENSOR_SUBSCRIBE_FAILURE = 1001; constexpr int32_t INPUT_ERROR = 202; constexpr float BODY_STATE_EXCEPT = 1.0f; constexpr float THRESHOLD = 0.000001f; +constexpr uint32_t COMPATIBILITY_CHANGE_VERSION_API12 = 12; } // namespace static std::map g_samplingPeriod = { {"normal", 200000000}, @@ -1036,8 +1037,12 @@ static napi_value GetSensorListSync(napi_env env, napi_callback_info info) static napi_value GetSingleSensor(napi_env env, napi_callback_info info) { CALL_LOG_ENTER; - int32_t targetSdkVersion = GetTargetSDKVersion(getpid()); - SEN_HILOGE("targetSdkVersion = %{public}d", targetSdkVersion); + uint32_t targetVersion = 0; + if (GetSelfTargetVersion(targetVersion)) { + SEN_HILOGI("targetVersion: %{public}d", targetVersion); + } else { + SEN_HILOGE("Get self target version fail"); + } size_t argc = 2; napi_value args[2] = { 0 }; napi_value thisVar = nullptr; @@ -1074,6 +1079,10 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) } } if (asyncCallbackInfo->sensorInfos.empty()) { + if (GetSelfTargetVersion(targetVersion) && (targetVersion < COMPATIBILITY_CHANGE_VERSION_API12)) { + ThrowErr(env, PARAMETER_ERROR, "The sensor is not supported by the device"); + return nullptr; + } ThrowErr(env, SENSOR_NO_SUPPORT, "The sensor is not supported by the device"); return nullptr; } diff --git a/frameworks/js/napi/src/sensor_napi_utils.cpp b/frameworks/js/napi/src/sensor_napi_utils.cpp index e82bdafe..507df764 100644 --- a/frameworks/js/napi/src/sensor_napi_utils.cpp +++ b/frameworks/js/napi/src/sensor_napi_utils.cpp @@ -19,9 +19,10 @@ #include #include -#include "app_mgr_client.h" -#include "bundle_mgr_client.h" -#include "os_account_manager.h" +#include "bundle_mgr_proxy.h" +#include "ipc_skeleton.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" #include "sensor_napi_error.h" @@ -29,7 +30,6 @@ namespace OHOS { namespace Sensors { namespace { constexpr int32_t STRING_LENGTH_MAX = 64; -constexpr int32_t INVALID_TARGET_VERSION = -1; } // namespace bool IsSameValue(const napi_env &env, const napi_value &lhs, const napi_value &rhs) { @@ -613,38 +613,32 @@ void EmitPromiseWork(sptr asyncCallbackInfo) } } -int32_t GetTargetSDKVersion(int32_t pid) +bool GetSelfTargetVersion(uint32_t &targetVersion) { - AppExecFwk::RunningProcessInfo processinfo; - AppExecFwk::AppMgrClient appMgrClient; - int32_t ret = appMgrClient.AppExecFwk::AppMgrClient::GetRunningProcessInfoByPid(pid, processinfo); - if (ret != ERR_OK) { - SEN_HILOGE("Getrunningprocessinfobypid failed"); - return INVALID_TARGET_VERSION; + sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + SEN_HILOGE("Samgr error"); + return false; } - std::vector activeUserIds; - int32_t retId = AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserIds); - if (retId != 0) { - SEN_HILOGE("QueryActiveOsAccountIds failed %{public}d", retId); - return INVALID_TARGET_VERSION; + auto bundleObj = samgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (bundleObj == nullptr) { + SEN_HILOGE("BundleObj error"); + return false; } - if (activeUserIds.empty()) { - SEN_HILOGE("activeUserId empty"); - return INVALID_TARGET_VERSION; + auto bundleMgrProxy = iface_cast(bundleObj); + if (bundleMgrProxy == nullptr) { + SEN_HILOGE("BundleMgrProxy error"); + return false; } - for (const auto &bundleName : processinfo.bundleNames) { - SEN_HILOGD("bundleName = %{public}s", bundleName.c_str()); - AppExecFwk::BundleMgrClient bundleMgrClient; - AppExecFwk::BundleInfo bundleInfo; - auto res = bundleMgrClient.AppExecFwk::BundleMgrClient::GetBundleInfo(bundleName, - AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, activeUserIds[0]); - if (!res) { - SEN_HILOGE("Getbundleinfo fail"); - return INVALID_TARGET_VERSION; - } - return bundleInfo.targetVersion; + AppExecFwk::BundleInfo bundleInfo; + ErrCode ret = bundleMgrProxy->GetBundleInfoForSelf(OHOS::AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo); + if (ret != ERR_OK) { + SEN_HILOGE("GetBundleInfoForSelf error"); + return false; } - return INVALID_TARGET_VERSION; + SEN_HILOGI("Bundle targetVersion is %{public}d", bundleInfo.targetVersion); + targetVersion = bundleInfo.targetVersion; + return true; } } // namespace Sensors } // namespace OHOS \ No newline at end of file -- Gitee From 7cc9e3f29ad1431b694bed01fe66ede463be7d4e Mon Sep 17 00:00:00 2001 From: lixiangpeng5 Date: Sun, 28 Jul 2024 07:23:55 +0000 Subject: [PATCH 4/4] fix by test Signed-off-by: lixiangpeng5 Change-Id: Ib132417ec4fbacf7e3e02bcd2f06c0f0dc0c91f1 --- frameworks/js/napi/src/sensor_js.cpp | 10 ++-------- frameworks/js/napi/src/sensor_napi_utils.cpp | 1 - test/unittest/interfaces/js/sensor/config.json | 4 ++-- 3 files changed, 4 insertions(+), 11 deletions(-) diff --git a/frameworks/js/napi/src/sensor_js.cpp b/frameworks/js/napi/src/sensor_js.cpp index 26aaf92f..35f75851 100644 --- a/frameworks/js/napi/src/sensor_js.cpp +++ b/frameworks/js/napi/src/sensor_js.cpp @@ -1037,12 +1037,6 @@ static napi_value GetSensorListSync(napi_env env, napi_callback_info info) static napi_value GetSingleSensor(napi_env env, napi_callback_info info) { CALL_LOG_ENTER; - uint32_t targetVersion = 0; - if (GetSelfTargetVersion(targetVersion)) { - SEN_HILOGI("targetVersion: %{public}d", targetVersion); - } else { - SEN_HILOGE("Get self target version fail"); - } size_t argc = 2; napi_value args[2] = { 0 }; napi_value thisVar = nullptr; @@ -1056,8 +1050,7 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) ThrowErr(env, PARAMETER_ERROR, "Wrong argument type, get number fail"); return nullptr; } - sptr asyncCallbackInfo = - new (std::nothrow) AsyncCallbackInfo(env, GET_SINGLE_SENSOR); + sptr asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfo(env, GET_SINGLE_SENSOR); CHKPP(asyncCallbackInfo); SensorInfo *sensorInfos = nullptr; int32_t count = 0; @@ -1079,6 +1072,7 @@ static napi_value GetSingleSensor(napi_env env, napi_callback_info info) } } if (asyncCallbackInfo->sensorInfos.empty()) { + uint32_t targetVersion = 0; if (GetSelfTargetVersion(targetVersion) && (targetVersion < COMPATIBILITY_CHANGE_VERSION_API12)) { ThrowErr(env, PARAMETER_ERROR, "The sensor is not supported by the device"); return nullptr; diff --git a/frameworks/js/napi/src/sensor_napi_utils.cpp b/frameworks/js/napi/src/sensor_napi_utils.cpp index 507df764..535d6050 100644 --- a/frameworks/js/napi/src/sensor_napi_utils.cpp +++ b/frameworks/js/napi/src/sensor_napi_utils.cpp @@ -636,7 +636,6 @@ bool GetSelfTargetVersion(uint32_t &targetVersion) SEN_HILOGE("GetBundleInfoForSelf error"); return false; } - SEN_HILOGI("Bundle targetVersion is %{public}d", bundleInfo.targetVersion); targetVersion = bundleInfo.targetVersion; return true; } diff --git a/test/unittest/interfaces/js/sensor/config.json b/test/unittest/interfaces/js/sensor/config.json index ed467a75..aabb3f4b 100644 --- a/test/unittest/interfaces/js/sensor/config.json +++ b/test/unittest/interfaces/js/sensor/config.json @@ -7,8 +7,8 @@ "name": "1.0" }, "apiVersion": { - "compatible": 4, - "target": 5 + "compatible": 12, + "target": 12 } }, "deviceConfig": {}, -- Gitee