From 47576f9c43de8cc1a2d18c94d683accc17f0508a Mon Sep 17 00:00:00 2001 From: guowei Date: Wed, 3 Sep 2025 10:28:07 +0800 Subject: [PATCH 1/3] =?UTF-8?q?stoi=E6=B7=BB=E5=8A=A0=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=8D=95=E8=8E=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guowei --- napi/settings/napi_settings.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/napi/settings/napi_settings.cpp b/napi/settings/napi_settings.cpp index 41d0d1c9..8aed246a 100644 --- a/napi/settings/napi_settings.cpp +++ b/napi/settings/napi_settings.cpp @@ -1853,7 +1853,15 @@ napi_value napi_can_show_floating(napi_env env, napi_callback_info info) // get uri for stage model std::string GetStageUriStr(std::string tableName, std::string idStr, std::string keyStr) { - if (std::stoi(idStr) < USERID_HELPER_NUMBER) { + try { + if (std::stoi(idStr) < USERID_HELPER_NUMBER) { + idStr = "100"; + } + } catch (std::invalid_argument&) { + SETTING_LOG_ERROR("GSUS_I"); + idStr = "100"; + } catch (...) { + SETTING_LOG_ERROR("GSUS_I"); idStr = "100"; } if (tableName == "global") { @@ -1879,7 +1887,15 @@ std::string GetStageUriStr(std::string tableName, std::string idStr, std::string // get proxy uri std::string GetProxyUriStr(std::string tableName, std::string idStr) { - if (std::stoi(idStr) < USERID_HELPER_NUMBER) { + try { + if (std::stoi(idStr) < USERID_HELPER_NUMBER) { + idStr = "100"; + } + } catch (std::invalid_argument&) { + SETTING_LOG_ERROR("GSUS_I"); + idStr = "100"; + } catch (...) { + SETTING_LOG_ERROR("GSUS_I"); idStr = "100"; } if (tableName == "global") { -- Gitee From 29c0072ca2dbaf8af07b4b116ca344893a40986c Mon Sep 17 00:00:00 2001 From: guowei Date: Wed, 3 Sep 2025 14:28:24 +0800 Subject: [PATCH 2/3] =?UTF-8?q?settings=5Fstoi=E5=91=8A=E8=AD=A6=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guowei --- napi/settings/napi_settings.cpp | 93 ++++++++++-------------- napi/settings/napi_settings.h | 4 +- napi/settings/napi_settings_observer.cpp | 15 ++-- 3 files changed, 47 insertions(+), 65 deletions(-) diff --git a/napi/settings/napi_settings.cpp b/napi/settings/napi_settings.cpp index 8aed246a..f7724a03 100644 --- a/napi/settings/napi_settings.cpp +++ b/napi/settings/napi_settings.cpp @@ -203,22 +203,21 @@ napi_value napi_get_uri_sync(napi_env env, napi_callback_info info) SETTING_LOG_INFO("ARGS_TWO"); std::string keyStr = unwrap_string_from_js(env, args[PARAM0]); // get userId string - std::vector tmpId; int currentUserId = -1; OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromProcess(currentUserId); - std::string tmpIdStr = "100"; + int tmpId = 100; if (currentUserId > 0) { - tmpIdStr = std::to_string(currentUserId); - SETTING_LOG_INFO("userId is %{public}s", tmpIdStr.c_str()); + tmpId = currentUserId; + SETTING_LOG_INFO("userId is %{public}d", tmpId); } else if (currentUserId == 0) { OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(currentUserId); - tmpIdStr = std::to_string(currentUserId); - SETTING_LOG_INFO("user0 userId is %{public}s", tmpIdStr.c_str()); + tmpId = currentUserId; + SETTING_LOG_INFO("user0 userId is %{public}d", tmpId); } else { SETTING_LOG_ERROR("userid is invalid, use id 100 instead"); } std::string tableName = unwrap_string_from_js(env, args[PARAM1]); - std::string retStr = GetStageUriStr(tableName, tmpIdStr, keyStr); + std::string retStr = GetStageUriStr(tableName, tmpId, keyStr); retUri = wrap_string_to_js(env, retStr); return retUri; } else { @@ -290,17 +289,16 @@ napi_value napi_get_uri(napi_env env, napi_callback_info info) } std::string keyStr = unwrap_string_from_js(env, args[PARAM0]); // get userId string - std::vector tmpId; int currentUserId = -1; OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromProcess(currentUserId); - std::string tmpIdStr = "100"; + int tmpId = 100; if (currentUserId > 0) { - tmpIdStr = std::to_string(currentUserId); - SETTING_LOG_INFO("userId is %{public}s", tmpIdStr.c_str()); + tmpId = currentUserId; + SETTING_LOG_INFO("userId is %{public}d", tmpId); } else if (currentUserId == 0) { OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(currentUserId); - tmpIdStr = std::to_string(currentUserId); - SETTING_LOG_INFO("user0 userId is %{public}s", tmpIdStr.c_str()); + tmpId = currentUserId; + SETTING_LOG_INFO("user0 userId is %{public}d", tmpId); } else { SETTING_LOG_ERROR("userid is invalid, use id 100 instead"); } @@ -312,7 +310,7 @@ napi_value napi_get_uri(napi_env env, napi_callback_info info) } else { tableName = "global"; } - std::string retStr = GetStageUriStr(tableName, tmpIdStr, keyStr); + std::string retStr = GetStageUriStr(tableName, tmpId, keyStr); asyncCallbackInfo->uri = retStr; SETTING_LOG_INFO("uri aft is %{public}s", asyncCallbackInfo->uri.c_str()); @@ -449,22 +447,21 @@ std::shared_ptr getDataShareHelper( AsyncCallbackInfo *asyncCallbackInfo) { std::shared_ptr dataShareHelper = nullptr; - std::vector tmpId; int currentUserId = -1; OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromProcess(currentUserId); - std::string tmpIdStr = "100"; + int tmpId = 100; if (currentUserId > 0) { - tmpIdStr = std::to_string(currentUserId); - SETTING_LOG_INFO("userId is %{public}s", tmpIdStr.c_str()); + tmpId = currentUserId; + SETTING_LOG_INFO("userId is %{public}d", tmpId); } else if (currentUserId == 0) { OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(currentUserId); - tmpIdStr = std::to_string(currentUserId); - SETTING_LOG_INFO("user0 userId is %{public}s", tmpIdStr.c_str()); + tmpId = currentUserId; + SETTING_LOG_INFO("user0 userId is %{public}d", tmpId); } else { SETTING_LOG_ERROR("userid is invalid, use id 100 instead"); } std::string strUri = "datashare:///com.ohos.settingsdata.DataAbility"; - std::string strProxyUri = GetProxyUriStr(tableName, tmpIdStr); + std::string strProxyUri = GetProxyUriStr(tableName, tmpId); OHOS::Uri proxyUri(strProxyUri); SETTING_LOG_INFO(" strProxyUri: %{public}s", strProxyUri.c_str()); auto contextS = OHOS::AbilityRuntime::GetStageModeContext(env, context); @@ -546,20 +543,20 @@ void GetValueExecuteExt(napi_env env, void *data) asyncCallbackInfo->status = STATUS_ERROR_CODE; return; } - - std::vector tmpId; + int currentUserId = -1; OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromProcess(currentUserId); - std::string tmpIdStr = "100"; + int tmpId = 100; if (currentUserId > 0) { - tmpIdStr = std::to_string(currentUserId); + tmpId = currentUserId; } else if (currentUserId == 0) { OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(currentUserId); - tmpIdStr = std::to_string(currentUserId); + tmpIdStr = currentUserId; } else { SETTING_LOG_ERROR("userid is invalid, use id 100 instead"); } - std::string strUri = GetStageUriStr(asyncCallbackInfo->tableName, tmpIdStr, asyncCallbackInfo->key); + std::string strUri = GetStageUriStr(asyncCallbackInfo->tableName, tmpId, + asyncCallbackInfo->key); SETTING_LOG_INFO( "Get uri : %{public}s, key: %{public}s", strUri.c_str(), (asyncCallbackInfo->key).c_str()); OHOS::Uri uri(strUri); @@ -642,20 +639,20 @@ void SetValueExecuteExt(napi_env env, void *data, const std::string setValue) OHOS::DataShare::DataShareValuesBucket val; val.Put(SETTINGS_DATA_FIELD_KEYWORD, asyncCallbackInfo->key); val.Put(SETTINGS_DATA_FIELD_VALUE, setValue); - - std::vector tmpId; + int currentUserId = -1; OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromProcess(currentUserId); - std::string tmpIdStr = "100"; + int tmpId = 100; if (currentUserId > 0) { - tmpIdStr = std::to_string(currentUserId); + tmpId = currentUserId; } else if (currentUserId == 0) { OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(currentUserId); - tmpIdStr = std::to_string(currentUserId); + tmpId = currentUserId; } else { SETTING_LOG_ERROR("userid is invalid, use id 100 instead"); } - std::string strUri = GetStageUriStr(asyncCallbackInfo->tableName, tmpIdStr, asyncCallbackInfo->key); + std::string strUri = GetStageUriStr(asyncCallbackInfo->tableName, tmpId, + asyncCallbackInfo->key); SETTING_LOG_WARN( "Set key: %{public}s value: %{public}s", (asyncCallbackInfo->key).c_str(), setValue.c_str()); OHOS::Uri uri(strUri); @@ -1851,19 +1848,12 @@ napi_value napi_can_show_floating(napi_env env, napi_callback_info info) } // get uri for stage model -std::string GetStageUriStr(std::string tableName, std::string idStr, std::string keyStr) +std::string GetStageUriStr(std::string tableName, int id, std::string keyStr) { - try { - if (std::stoi(idStr) < USERID_HELPER_NUMBER) { - idStr = "100"; - } - } catch (std::invalid_argument&) { - SETTING_LOG_ERROR("GSUS_I"); - idStr = "100"; - } catch (...) { - SETTING_LOG_ERROR("GSUS_I"); - idStr = "100"; + if (id < USERID_HELPER_NUMBER) { + id = USERID_HELPER_NUMBER; } + std::string idStr = std::to_string(id); if (tableName == "global") { std::string retStr = "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=" + keyStr; @@ -1885,19 +1875,12 @@ std::string GetStageUriStr(std::string tableName, std::string idStr, std::string } // get proxy uri -std::string GetProxyUriStr(std::string tableName, std::string idStr) +std::string GetProxyUriStr(std::string tableName, int id) { - try { - if (std::stoi(idStr) < USERID_HELPER_NUMBER) { - idStr = "100"; - } - } catch (std::invalid_argument&) { - SETTING_LOG_ERROR("GSUS_I"); - idStr = "100"; - } catch (...) { - SETTING_LOG_ERROR("GSUS_I"); - idStr = "100"; + if (id < USERID_HELPER_NUMBER) { + id = USERID_HELPER_NUMBER; } + std::string idStr = std::to_string(id); if (tableName == "global") { // return global uri std::string retStr = "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true"; diff --git a/napi/settings/napi_settings.h b/napi/settings/napi_settings.h index 274415e4..57d95764 100644 --- a/napi/settings/napi_settings.h +++ b/napi/settings/napi_settings.h @@ -187,8 +187,8 @@ napi_value napi_enable_airplane_mode(napi_env env, napi_callback_info info); * @return napi_value the return value from NAPI C++ to JS for the module. */ napi_value napi_can_show_floating(napi_env env, napi_callback_info info); -std::string GetStageUriStr(std::string tableName, std::string idStr, std::string keyStr); -std::string GetProxyUriStr(std::string tableName, std::string idStr); +std::string GetStageUriStr(std::string tableName, int id, std::string keyStr); +std::string GetProxyUriStr(std::string tableName, int id); bool IsTableNameInvalid(std::string tableName); std::shared_ptr getDataShareHelper( napi_env env, const napi_value context, const bool stageMode, std::string tableName = "global", diff --git a/napi/settings/napi_settings_observer.cpp b/napi/settings/napi_settings_observer.cpp index 748244bf..c4863229 100644 --- a/napi/settings/napi_settings_observer.cpp +++ b/napi/settings/napi_settings_observer.cpp @@ -124,23 +124,22 @@ namespace Settings { } } - std::string GetObserverIdStr() + int GetObserverIdStr() { - std::vector tmpId; int currentUserId = -1; OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromProcess(currentUserId); - std::string tmpIdStr = "100"; + int tmpId = 100; if (currentUserId > 0) { - tmpIdStr = std::to_string(currentUserId); - SETTING_LOG_INFO("userId is %{public}s", tmpIdStr.c_str()); + tmpId = currentUserId; + SETTING_LOG_INFO("userId is %{public}d", tmpId); } else if (currentUserId == 0) { OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(currentUserId); - tmpIdStr = std::to_string(currentUserId); - SETTING_LOG_INFO("user0 userId is %{public}s", tmpIdStr.c_str()); + tmpId = currentUserId; + SETTING_LOG_INFO("user0 userId is %{public}d", tmpId); } else { SETTING_LOG_INFO("%{public}s, user id 100.", __func__); } - return tmpIdStr; + return tmpId; } void CleanUp(void* data) -- Gitee From 461a71c92b0104ab29428a656647822130712ab4 Mon Sep 17 00:00:00 2001 From: guowei Date: Wed, 3 Sep 2025 16:13:34 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guowei --- napi/settings/napi_settings.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/napi/settings/napi_settings.cpp b/napi/settings/napi_settings.cpp index 039f67d7..b8116022 100644 --- a/napi/settings/napi_settings.cpp +++ b/napi/settings/napi_settings.cpp @@ -549,7 +549,7 @@ void GetValueExecuteExt(napi_env env, void *data) tmpId = currentUserId; } else if (currentUserId == 0) { OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(currentUserId); - tmpIdStr = currentUserId; + tmpId = currentUserId; } else { SETTING_LOG_ERROR("userid is invalid, use id 100 instead"); } -- Gitee