diff --git a/services/adapter/settings_data_provider/common/include/settings_data_utils.h b/services/adapter/settings_data_provider/common/include/settings_data_utils.h index 96914a9f..6b9961ef 100644 --- a/services/adapter/settings_data_provider/common/include/settings_data_utils.h +++ b/services/adapter/settings_data_provider/common/include/settings_data_utils.h @@ -30,6 +30,11 @@ namespace OHOS { namespace MiscServices { +constexpr const char *SETTING_COLUMN_KEYWORD = "KEYWORD"; +constexpr const char *SETTING_COLUMN_VALUE = "VALUE"; +const std::string SETTING_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/" + "SETTINGSDATA?Proxy=true"; +const std::string SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility"; struct UserImeConfig : public Serializable { std::string userId; std::vector identities; @@ -43,11 +48,12 @@ struct UserImeConfig : public Serializable { class SettingsDataUtils : public RefBase { public: static sptr GetInstance(); - std::shared_ptr CreateDataShareHelper(); + std::shared_ptr CreateDataShareHelper(const std::string &uriProxy); int32_t CreateAndRegisterObserver(const std::string &key, SettingsDataObserver::CallbackFunc func); - int32_t GetStringValue(const std::string &key, std::string &value); + int32_t GetStringValue(const std::string &uriProxy, const std::string &key, std::string &value); + int32_t SetStringValue(const std::string &uriProxy, const std::string &key, const std::string &value); bool ReleaseDataShareHelper(std::shared_ptr &helper); - Uri GenerateTargetUri(const std::string &key); + Uri GenerateTargetUri(const std::string &uriProxy, const std::string &key); bool EnableIme(int32_t userId, const std::string &bundleName); private: diff --git a/services/adapter/settings_data_provider/common/src/settings_data_utils.cpp b/services/adapter/settings_data_provider/common/src/settings_data_utils.cpp index f956a0ad..5ccfe854 100644 --- a/services/adapter/settings_data_provider/common/src/settings_data_utils.cpp +++ b/services/adapter/settings_data_provider/common/src/settings_data_utils.cpp @@ -23,11 +23,6 @@ namespace OHOS { namespace MiscServices { std::mutex SettingsDataUtils::instanceMutex_; sptr SettingsDataUtils::instance_ = nullptr; -constexpr const char *SETTING_COLUMN_KEYWORD = "KEYWORD"; -constexpr const char *SETTING_COLUMN_VALUE = "VALUE"; -constexpr const char *SETTING_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/" - "SETTINGSDATA?Proxy=true"; -constexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility"; SettingsDataUtils::~SettingsDataUtils() { remoteObj_ = nullptr; @@ -73,8 +68,8 @@ int32_t SettingsDataUtils::RegisterObserver(const sptr &ob return ErrorCode::ERROR_NULL_POINTER; } - auto uri = GenerateTargetUri(observer->GetKey()); - auto helper = SettingsDataUtils::CreateDataShareHelper(); + auto uri = GenerateTargetUri(SETTING_URI_PROXY, observer->GetKey()); + auto helper = SettingsDataUtils::CreateDataShareHelper(SETTING_URI_PROXY); if (helper == nullptr) { IMSA_HILOGE("helper is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; @@ -90,8 +85,8 @@ int32_t SettingsDataUtils::RegisterObserver(const sptr &ob int32_t SettingsDataUtils::UnregisterObserver(const sptr &observer) { - auto uri = GenerateTargetUri(observer->GetKey()); - auto helper = SettingsDataUtils::CreateDataShareHelper(); + auto uri = GenerateTargetUri(SETTING_URI_PROXY, observer->GetKey()); + auto helper = SettingsDataUtils::CreateDataShareHelper(SETTING_URI_PROXY); if (helper == nullptr) { return ErrorCode::ERROR_ENABLE_IME; } @@ -101,7 +96,7 @@ int32_t SettingsDataUtils::UnregisterObserver(const sptr & return ErrorCode::NO_ERROR; } -std::shared_ptr SettingsDataUtils::CreateDataShareHelper() +std::shared_ptr SettingsDataUtils::CreateDataShareHelper(const std::string &uriProxy) { auto remoteObj = GetToken(); if (remoteObj == nullptr) { @@ -109,9 +104,9 @@ std::shared_ptr SettingsDataUtils::CreateDataShareHe return nullptr; } - auto helper = DataShare::DataShareHelper::Creator(remoteObj_, SETTING_URI_PROXY, SETTINGS_DATA_EXT_URI); + auto helper = DataShare::DataShareHelper::Creator(remoteObj_, uriProxy, SETTINGS_DATA_EXT_URI); if (helper == nullptr) { - IMSA_HILOGE("create helper failed, uri: %{public}s!", SETTING_URI_PROXY); + IMSA_HILOGE("create helper failed, uri: %{public}s!", uriProxy.c_str()); return nullptr; } return helper; @@ -130,16 +125,43 @@ bool SettingsDataUtils::ReleaseDataShareHelper(std::shared_ptrUpdate(uri, predicates, bucket) <= 0) { + int index = helper->Insert(uri, bucket); + IMSA_HILOGI("no data exists, insert ret index: %{public}d", index); + } else { + IMSA_HILOGI("data exits"); + } + bool ret = ReleaseDataShareHelper(helper); + IMSA_HILOGI("ReleaseDataShareHelper isSuccess: %{public}d", ret); + return ret; +} + +int32_t SettingsDataUtils::GetStringValue(const std::string &uriProxy, const std::string &key, std::string &value) +{ + IMSA_HILOGD("start."); + auto helper = CreateDataShareHelper(uriProxy); if (helper == nullptr) { IMSA_HILOGE("helper is nullptr."); return ErrorCode::ERROR_NULL_POINTER; @@ -147,7 +169,7 @@ int32_t SettingsDataUtils::GetStringValue(const std::string &key, std::string &v std::vector columns = { SETTING_COLUMN_VALUE }; DataShare::DataSharePredicates predicates; predicates.EqualTo(SETTING_COLUMN_KEYWORD, key); - Uri uri(GenerateTargetUri(key)); + Uri uri(GenerateTargetUri(uriProxy, key)); auto resultSet = helper->Query(uri, predicates, columns); ReleaseDataShareHelper(helper); if (resultSet == nullptr) { @@ -201,11 +223,9 @@ bool SettingsDataUtils::EnableIme(int32_t userId, const std::string &bundleName) IMSA_HILOGE("user is not main."); return false; } - const char *SETTING_COLUMN_KEYWORD = "KEYWORD"; - const char *SETTING_COLUMN_VALUE = "VALUE"; const char *settingKey = "settings.inputmethod.enable_ime"; std::string settingValue = ""; - GetStringValue(settingKey, settingValue); + GetStringValue(SETTING_URI_PROXY, settingKey, settingValue); IMSA_HILOGI("settingValue: %{public}s", settingValue.c_str()); std::string value = ""; if (settingValue == "") { @@ -214,27 +234,7 @@ bool SettingsDataUtils::EnableIme(int32_t userId, const std::string &bundleName) value = SetSettingValues(settingValue, bundleName); } IMSA_HILOGI("value: %{public}s", value.c_str()); - auto helper = CreateDataShareHelper(); - if (helper == nullptr) { - IMSA_HILOGE("helper is nullptr."); - return false; - } - DataShare::DataShareValueObject keyObj(settingKey); - DataShare::DataShareValueObject valueObj(value); - DataShare::DataShareValuesBucket bucket; - bucket.Put(SETTING_COLUMN_KEYWORD, keyObj); - bucket.Put(SETTING_COLUMN_VALUE, valueObj); - DataShare::DataSharePredicates predicates; - predicates.EqualTo(SETTING_COLUMN_KEYWORD, settingKey); - Uri uri(GenerateTargetUri(settingKey)); - if (helper->Update(uri, predicates, bucket) <= 0) { - int index = helper->Insert(uri, bucket); - IMSA_HILOGI("no data exists, insert ret index: %{public}d", index); - } else { - IMSA_HILOGI("data exits"); - } - bool ret = ReleaseDataShareHelper(helper); - IMSA_HILOGI("ReleaseDataShareHelper isSuccess: %{public}d", ret); + auto ret = SetStringValue(SETTING_URI_PROXY, settingKey, value); return ret; } diff --git a/services/adapter/settings_data_provider/include/enable_ime_data_parser.h b/services/adapter/settings_data_provider/include/enable_ime_data_parser.h index 2db23db2..6ccc0d50 100644 --- a/services/adapter/settings_data_provider/include/enable_ime_data_parser.h +++ b/services/adapter/settings_data_provider/include/enable_ime_data_parser.h @@ -89,6 +89,8 @@ private: EnableImeDataParser() = default; ~EnableImeDataParser(); int32_t UpdateEnableData(int32_t userId, const std::string &key); + int32_t CoverGlobalTable(const std::string &uriProxy, const std::string &key, std::string &valueStr); + std::string ParseEnableValueUserId(const std::string &valueStr); int32_t GetEnableImeFromCache(std::vector &enableVec); bool ParseEnableIme(const std::string &valueStr, int32_t userId, std::vector &enableVec); bool ParseEnableKeyboard(const std::string &valueStr, int32_t userId, std::vector &enableVec); diff --git a/services/adapter/settings_data_provider/src/enable_ime_data_parser.cpp b/services/adapter/settings_data_provider/src/enable_ime_data_parser.cpp index bcc637e0..b040deeb 100644 --- a/services/adapter/settings_data_provider/src/enable_ime_data_parser.cpp +++ b/services/adapter/settings_data_provider/src/enable_ime_data_parser.cpp @@ -182,28 +182,76 @@ bool EnableImeDataParser::CheckTargetEnableName(const std::string &key, const st return true; } -int32_t EnableImeDataParser::GetEnableData(const std::string &key, std::vector &enableVec, - const int32_t userId) +int32_t EnableImeDataParser::CoverGlobalTable( + const std::string &uriProxy, const std::string &key, std::string &valueStr) +{ + int32_t ret; + if (!valueStr.empty()) { + std::string userId = ParseEnableValueUserId(valueStr); + if (userId != "") { + if (userId == std::to_string(currentUserId_)) { + return ErrorCode::NO_ERROR; + } + ret = SettingsDataUtils::GetInstance()->SetStringValue(uriProxy + userId + "?Proxy=true", key, valueStr); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGW("global cover user table failed"); + } + } + } + ret = SettingsDataUtils::GetInstance()->GetStringValue( + uriProxy + std::to_string(currentUserId_) + "?Proxy=true", key, valueStr); + if (ret == ErrorCode::ERROR_KEYWORD_NOT_FOUND) { + IMSA_HILOGW("user table no keyword exist"); + return ErrorCode::NO_ERROR; + } + if (ret != ErrorCode::NO_ERROR || valueStr.empty()) { + IMSA_HILOGW("get current user table value failed, or valueStr is empty."); + return ErrorCode::ERROR_ENABLE_IME; + } + SettingsDataUtils::GetInstance()->SetStringValue(SETTING_URI_PROXY, key, valueStr); + return ErrorCode::NO_ERROR; +} + +int32_t EnableImeDataParser::GetEnableData( + const std::string &key, std::vector &enableVec, const int32_t userId) { if (key != std::string(ENABLE_IME) && key != std::string(ENABLE_KEYBOARD) && key != std::string(TEMP_IME)) { IMSA_HILOGD("invalid key: %{public}s.", key.c_str()); return ErrorCode::ERROR_ENABLE_IME; } - IMSA_HILOGD("userId: %{public}d, key: %{public}s.", userId, key.c_str()); std::string valueStr; - int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue(key, valueStr); + int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, key, valueStr); if (ret == ErrorCode::ERROR_KEYWORD_NOT_FOUND) { IMSA_HILOGW("no keyword exist"); enableVec.clear(); return ErrorCode::NO_ERROR; } - if (ret != ErrorCode::NO_ERROR || valueStr.empty()) { + if (key != ENABLE_IME && (ret != ErrorCode::NO_ERROR || valueStr.empty())) { IMSA_HILOGW("get value failed, or valueStr is empty."); return ErrorCode::ERROR_ENABLE_IME; } auto parseRet = false; if (key == ENABLE_IME) { + std::string uriProxy = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_"; + CoverGlobalTable(uriProxy, key, valueStr); + if (currentUserId_ != userId) { + ret = SettingsDataUtils::GetInstance()->GetStringValue( + uriProxy + std::to_string(userId) + "?Proxy=true", key, valueStr); + if (ret == ErrorCode::ERROR_KEYWORD_NOT_FOUND) { + IMSA_HILOGW("no keyword exist"); + enableVec.clear(); + return ErrorCode::NO_ERROR; + } + if (ret != ErrorCode::NO_ERROR || valueStr.empty()) { + IMSA_HILOGW("get user table value failed, or valueStr is empty."); + return ErrorCode::ERROR_ENABLE_IME; + } + } else if (valueStr.empty()) { + IMSA_HILOGW("get enable ime, value is empty"); + enableVec.clear(); + return ErrorCode::NO_ERROR; + } parseRet = ParseEnableIme(valueStr, userId, enableVec); } if (key == ENABLE_KEYBOARD) { @@ -215,6 +263,21 @@ int32_t EnableImeDataParser::GetEnableData(const std::string &key, std::vectorchild == nullptr) { + IMSA_HILOGW("subNode has not child"); + return ""; + } + return subNode->child->string; +} + bool EnableImeDataParser::ParseTempIme(const std::string &valueStr, int32_t userId, std::vector &tempVector) { diff --git a/services/adapter/settings_data_provider/src/security_mode_parser.cpp b/services/adapter/settings_data_provider/src/security_mode_parser.cpp index 6e92da89..6f5caa60 100644 --- a/services/adapter/settings_data_provider/src/security_mode_parser.cpp +++ b/services/adapter/settings_data_provider/src/security_mode_parser.cpp @@ -63,7 +63,7 @@ int32_t SecurityModeParser::UpdateFullModeList(int32_t userId) { IMSA_HILOGD("key: %{public}s.", SECURITY_MODE); std::string valueStr; - int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue(SECURITY_MODE, valueStr); + int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, SECURITY_MODE, valueStr); if (ret != ErrorCode::NO_ERROR || valueStr.empty()) { IMSA_HILOGW("get value failed, or valueStr is empty"); return ErrorCode::ERROR_ENABLE_SECURITY_MODE; diff --git a/services/json/include/serializable.h b/services/json/include/serializable.h index ed39ea1c..784e3e1d 100644 --- a/services/json/include/serializable.h +++ b/services/json/include/serializable.h @@ -83,8 +83,6 @@ public: } return ret; } - -private: static cJSON *GetSubNode(cJSON *node, const std::string &name); }; } // namespace MiscServices diff --git a/test/fuzztest/settingsdatautils_fuzzer/settingsdatautils_fuzzer.cpp b/test/fuzztest/settingsdatautils_fuzzer/settingsdatautils_fuzzer.cpp index 306b096b..0ffe563f 100644 --- a/test/fuzztest/settingsdatautils_fuzzer/settingsdatautils_fuzzer.cpp +++ b/test/fuzztest/settingsdatautils_fuzzer/settingsdatautils_fuzzer.cpp @@ -28,7 +28,7 @@ void FuzzGetToken() void FuzzDataShareHelper() { - auto helper = SettingsDataUtils::GetInstance()->CreateDataShareHelper(); + auto helper = SettingsDataUtils::GetInstance()->CreateDataShareHelper(SETTING_URI_PROXY); SettingsDataUtils::GetInstance()->ReleaseDataShareHelper(helper); } @@ -51,7 +51,7 @@ void FuzzUnregisterObserver(const std::string &key, SettingsDataObserver::Callba void FuzzGenerateTargetUri(const std::string &key) { - SettingsDataUtils::GetInstance()->GenerateTargetUri(key); + SettingsDataUtils::GetInstance()->GenerateTargetUri(SETTING_URI_PROXY, key); } } // namespace OHOS diff --git a/test/unittest/cpp_test/common/src/tdd_util.cpp b/test/unittest/cpp_test/common/src/tdd_util.cpp index 12de8ace..5a85aac3 100644 --- a/test/unittest/cpp_test/common/src/tdd_util.cpp +++ b/test/unittest/cpp_test/common/src/tdd_util.cpp @@ -55,8 +55,6 @@ constexpr int32_t FIRST_PARAM_INDEX = 0; constexpr int32_t SECOND_PARAM_INDEX = 1; constexpr int32_t THIRD_PARAM_INDEX = 2; constexpr int32_t FOURTH_PARAM_INDEX = 3; -constexpr const char *SETTING_COLUMN_KEYWORD = "KEYWORD"; -constexpr const char *SETTING_COLUMN_VALUE = "VALUE"; static constexpr int32_t MAX_TIMEOUT_WAIT_FOCUS = 2000; uint64_t TddUtil::selfTokenID_ = 0; int32_t TddUtil::userID_ = INVALID_USER_ID; @@ -301,7 +299,7 @@ void TddUtil::GrantNativePermission() void TddUtil::PushEnableImeValue(const std::string &key, const std::string &value) { IMSA_HILOGI("key: %{public}s, value: %{public}s", key.c_str(), value.c_str()); - auto helper = SettingsDataUtils::GetInstance()->CreateDataShareHelper(); + auto helper = SettingsDataUtils::GetInstance()->CreateDataShareHelper(SETTING_URI_PROXY); if (helper == nullptr) { IMSA_HILOGE("helper is nullptr."); return; @@ -313,7 +311,7 @@ void TddUtil::PushEnableImeValue(const std::string &key, const std::string &valu bucket.Put(SETTING_COLUMN_VALUE, valueObj); DataShare::DataSharePredicates predicates; predicates.EqualTo(SETTING_COLUMN_KEYWORD, key); - Uri uri(SettingsDataUtils::GetInstance()->GenerateTargetUri(key)); + Uri uri(SettingsDataUtils::GetInstance()->GenerateTargetUri(SETTING_URI_PROXY, key)); if (helper->Update(uri, predicates, bucket) <= 0) { int index = helper->Insert(uri, bucket); IMSA_HILOGI("no data exists, insert ret index: %{public}d", index); @@ -326,7 +324,8 @@ void TddUtil::PushEnableImeValue(const std::string &key, const std::string &valu int32_t TddUtil::GetEnableData(std::string &value) { - auto ret = SettingsDataUtils::GetInstance()->GetStringValue(EnableImeDataParser::ENABLE_IME, value); + auto ret = + SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, EnableImeDataParser::ENABLE_IME, value); if (ret == ErrorCode::NO_ERROR) { IMSA_HILOGI("success, value: %{public}s", value.c_str()); } diff --git a/test/unittest/resource/bundle_dependencies/extImfBundle/entry/src/main/module.json b/test/unittest/resource/bundle_dependencies/extImfBundle/entry/src/main/module.json index b6e7f7e4..f6d8ac3d 100644 --- a/test/unittest/resource/bundle_dependencies/extImfBundle/entry/src/main/module.json +++ b/test/unittest/resource/bundle_dependencies/extImfBundle/entry/src/main/module.json @@ -58,6 +58,9 @@ }, { "name": "ohos.permission.MANAGE_SECURE_SETTINGS" + }, + { + "name": "ohos.permission.MANAGE_SETTINGS" } ] }