diff --git a/.clang-format b/.clang-format deleted file mode 100644 index 96f062c30ed150fb73004a39f3249894b286166d..0000000000000000000000000000000000000000 --- a/.clang-format +++ /dev/null @@ -1,40 +0,0 @@ -# Run command below to format a file -# clang-format -i --style=file - -# complete clang-format rule, reference: -# https://clang.llvm.org/docs/ClangFormatStyleOptions.html - -# WebKit format rule details, reference: -# https://webkit.org/code-style-guidelines/ -# https://gitee.com/mirrors/WebKit/blob/main/.clang-format -BasedOnStyle: Webkit -# works on C and C++ files -Language: Cpp - -PointerAlignment: Right -AlignTrailingComments: true -AlignConsecutiveMacros: Consecutive -# case statements indent one layer -IndentCaseLabels: true -BreakBeforeBinaryOperators: None -SpaceBeforeParens: ControlStatementsExceptControlMacros -SpacesInCStyleCastParentheses: false -AlignEscapedNewlines: Left -NamespaceIndentation: None -FixNamespaceComments: true -BreakConstructorInitializers: BeforeColon -AlignArrayOfStructures: Left -AllowShortFunctionsOnASingleLine: Empty -AllowShortLambdasOnASingleLine: Empty -AlwaysBreakTemplateDeclarations: true -BreakBeforeTernaryOperators: false -SpaceAroundPointerQualifiers: Both -# iterator macros declaretion,avoid being treated as function call -ForEachMacros: - - 'LIST_FOR_EACH_ENTRY' - - 'LIST_FOR_EACH_ENTRY_SAFE' - - 'LIST_FOR_EACH' - - 'LIST_FOR_EACH_SAFE' -SortIncludes: CaseInsensitive -AllowShortEnumsOnASingleLine: false -ColumnLimit: 120 \ No newline at end of file diff --git a/common/imf_hisysevent/src/imsa_hisysevent_reporter.cpp b/common/imf_hisysevent/src/imsa_hisysevent_reporter.cpp index 8a8f46e11528d32272cb2e57e9427166f96a3eea..b42299a439b2a9fbc6ae97b734f27c35f1dcda3b 100644 --- a/common/imf_hisysevent/src/imsa_hisysevent_reporter.cpp +++ b/common/imf_hisysevent/src/imsa_hisysevent_reporter.cpp @@ -74,8 +74,8 @@ void ImsaHiSysEventReporter::ReportStatisticsEvent() clientAttachInfo_ = ClientAttachAllInfo(COUNT_STATISTICS_INTERVAL_NUM, COUNT_STATISTICS_INTERVAL_NUM); clientShowInfo_ = ClientShowAllInfo(COUNT_STATISTICS_INTERVAL_NUM, COUNT_STATISTICS_INTERVAL_NUM); } - std::string attachStatistics; if (!clientAttachInfo.appNames.empty()) { + std::string attachStatistics; clientAttachInfo.succeedRateInfo.Marshall(attachStatistics); ImfHiSysEventUtil::ReportStatisticsEvent(GET_NAME(CLIENT_ATTACH_STATISTICS), clientAttachInfo.imeNames, clientAttachInfo.appNames, { attachStatistics }); diff --git a/common/include/global.h b/common/include/global.h index 3770c0141fe60b5118f041b86aac5ad7171361b2..3c18b753f1d0ae987f323dfc46e2b12bd8a4c7ee 100644 --- a/common/include/global.h +++ b/common/include/global.h @@ -148,6 +148,8 @@ enum { ERROR_DEVICE_UNSUPPORTED, ERROR_SCENE_UNSUPPORTED, ERROR_PRIVATE_COMMAND_IS_EMPTY, + ERROR_IME_NOT_FOUND, + ERROR_DISABLE_SYS_IME, ERROR_IMSA_END, }; }; // namespace ErrorCode diff --git a/common/include/inputmethod_message_handler.h b/common/include/inputmethod_message_handler.h index 48da8753be0f70431554e87762d8dd792ab1b4d7..f462bbeafa467c9a762de84de2fd4beccd0e21da 100644 --- a/common/include/inputmethod_message_handler.h +++ b/common/include/inputmethod_message_handler.h @@ -43,6 +43,7 @@ enum { MSG_ID_SELECT_BY_MOVEMENT, MSG_ID_HANDLE_EXTEND_ACTION, MSG_ID_USER_STOP, + MSG_ID_REGULAR_UPDATE_IME_INFO, MSG_ID_HIDE_KEYBOARD_SELF, // hide the current keyboard diff --git a/common/src/itypes_util.cpp b/common/src/itypes_util.cpp index 83102c605a147d0d6597c2f094e5c6c24c2a3498..47a46fb44ce7266970e7e03482d7ff2131d121bb 100644 --- a/common/src/itypes_util.cpp +++ b/common/src/itypes_util.cpp @@ -148,7 +148,8 @@ bool ITypesUtil::Unmarshalling(sptr &output, MessageParcel &data) bool ITypesUtil::Marshalling(const Property &input, MessageParcel &data) { - if (!Marshal(data, input.name, input.id, input.label, input.labelId, input.icon, input.iconId)) { + if (!Marshal(data, input.name, input.id, input.label, input.labelId, input.icon, input.iconId, + static_cast(input.status))) { IMSA_HILOGE("write Property to message parcel failed."); return false; } @@ -157,10 +158,12 @@ bool ITypesUtil::Marshalling(const Property &input, MessageParcel &data) bool ITypesUtil::Unmarshalling(Property &output, MessageParcel &data) { - if (!Unmarshal(data, output.name, output.id, output.label, output.labelId, output.icon, output.iconId)) { + int32_t status = 0; + if (!Unmarshal(data, output.name, output.id, output.label, output.labelId, output.icon, output.iconId, status)) { IMSA_HILOGE("read Property from message parcel failed."); return false; } + output.status = static_cast(status); return true; } diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp index d2503510ea16d5f3462de914fe579a226806c317..0bc5dfaaf1e06df201a8fc02f30aae9d95a877ff 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.cpp @@ -66,6 +66,7 @@ napi_value JsGetInputMethodSetting::Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("displayOptionalInputMethod", DisplayOptionalInputMethod), DECLARE_NAPI_FUNCTION("showOptionalInputMethods", ShowOptionalInputMethods), DECLARE_NAPI_FUNCTION("isPanelShown", IsPanelShown), + DECLARE_NAPI_FUNCTION("enableInputMethod", EnableInputMethod), DECLARE_NAPI_FUNCTION("getInputMethodState", GetInputMethodState), DECLARE_NAPI_FUNCTION("on", Subscribe), DECLARE_NAPI_FUNCTION("off", UnSubscribe), @@ -455,6 +456,43 @@ napi_value JsGetInputMethodSetting::IsPanelShown(napi_env env, napi_callback_inf return JsUtil::GetValue(env, isShown); } +napi_value JsGetInputMethodSetting::EnableInputMethod(napi_env env, napi_callback_info info) +{ + IMSA_HILOGD("run in"); + auto ctxt = std::make_shared(); + auto input = [ctxt](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { + PARAM_CHECK_RETURN(env, argc > 2, "at least three parameter is required!", TYPE_NONE, napi_invalid_arg); + PARAM_CHECK_RETURN(env, + JsUtil::GetType(env, argv[0]) == napi_string && JsUtil::GetValue(env, argv[0], ctxt->bundleName), + "bundleName type must be string!", TYPE_NONE, napi_invalid_arg); + PARAM_CHECK_RETURN(env, !ctxt->bundleName.empty(), "bundleName can not be empty!", TYPE_NONE, napi_invalid_arg); + PARAM_CHECK_RETURN(env, + JsUtil::GetType(env, argv[1]) == napi_string && JsUtil::GetValue(env, argv[1], ctxt->extName), + "extensionName type must be string!", TYPE_NONE, napi_invalid_arg); + PARAM_CHECK_RETURN(env, !ctxt->extName.empty(), "extensionName can not be empty!", TYPE_NONE, napi_invalid_arg); + int32_t status = 0; + PARAM_CHECK_RETURN(env, JsUtil::GetType(env, argv[2]) == napi_number && JsUtil::GetValue(env, argv[2], status), + "bundleName type must be EnabledState!", TYPE_NONE, napi_invalid_arg); + ctxt->enabledStatus = static_cast(status); + return napi_ok; + }; + auto output = [ctxt](napi_env env, napi_value *result) -> napi_status { return napi_ok; }; + auto exec = [ctxt](AsyncCall::Context *ctx) { + int32_t errCode = + InputMethodController::GetInstance()->EnableIme(ctxt->bundleName, ctxt->extName, ctxt->enabledStatus); + if (errCode == ErrorCode::NO_ERROR) { + ctxt->status = napi_ok; + ctxt->SetState(ctxt->status); + return; + } + ctxt->SetErrorCode(errCode); + }; + ctxt->SetAction(std::move(input), std::move(output)); + // 3 means JsAPI:enableInputMethod has 3 params at most. + AsyncCall asyncCall(env, info, ctxt, 3); + return asyncCall.Call(env, exec, "EnableInputMethod"); +} + napi_value JsGetInputMethodSetting::GetInputMethodState(napi_env env, napi_callback_info info) { auto ctxt = std::make_shared(); diff --git a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.h b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.h index 5e0b3f4ea3579f5316f81d3f3be3d7a2b47a868f..76429265b112501d07f84a14634ce1483d501e6c 100644 --- a/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.h +++ b/frameworks/js/napi/inputmethodclient/js_get_input_method_setting.h @@ -94,6 +94,29 @@ struct GetInputMethodControllerContext : public AsyncCall::Context { } }; +struct enableInputContext : public AsyncCall::Context { + std::string bundleName; + std::string extName; + EnabledStatus enabledStatus; + napi_status status = napi_generic_failure; + enableInputContext() : Context(nullptr, nullptr){}; + enableInputContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)){}; + + napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override + { + CHECK_RETURN(self != nullptr, "self is nullptr!", napi_invalid_arg); + return Context::operator()(env, argc, argv, self); + } + napi_status operator()(napi_env env, napi_value *result) override + { + if (status != napi_ok) { + output_ = nullptr; + return status; + } + return Context::operator()(env, result); + } +}; + struct GetInputMethodStateContext : public AsyncCall::Context { napi_status status = napi_generic_failure; EnabledStatus enableStatus = EnabledStatus::DISABLED; @@ -127,6 +150,7 @@ public: static napi_value DisplayOptionalInputMethod(napi_env env, napi_callback_info info); static napi_value ShowOptionalInputMethods(napi_env env, napi_callback_info info); static napi_value IsPanelShown(napi_env env, napi_callback_info info); + static napi_value EnableInputMethod(napi_env env, napi_callback_info info); static napi_value GetInputMethodState(napi_env env, napi_callback_info info); static napi_value Subscribe(napi_env env, napi_callback_info info); static napi_value UnSubscribe(napi_env env, napi_callback_info info); diff --git a/frameworks/js/napi/inputmethodclient/js_input_method.cpp b/frameworks/js/napi/inputmethodclient/js_input_method.cpp index 8cc3b0884de9e6396b63e73ca2a9923b92d98f9f..5097236b43381a8f527507688888df3eb7879245 100644 --- a/frameworks/js/napi/inputmethodclient/js_input_method.cpp +++ b/frameworks/js/napi/inputmethodclient/js_input_method.cpp @@ -130,6 +130,10 @@ napi_value JsInputMethod::GetJsInputMethodProperty(napi_env env, const Property napi_value labelId = nullptr; napi_create_uint32(env, property.labelId, &labelId); napi_set_named_property(env, prop, "labelId", labelId); + + napi_value status = nullptr; + napi_create_int32(env, static_cast(property.status), &status); + napi_set_named_property(env, prop, "enabledState", status); return prop; } diff --git a/frameworks/js/napi/inputmethodclient/js_utils.cpp b/frameworks/js/napi/inputmethodclient/js_utils.cpp index d8446aa23ad35f57e4f58cba3d025f89fd081f74..eec69b5a9a6499cd891b7fdeab583c664614e5cc 100644 --- a/frameworks/js/napi/inputmethodclient/js_utils.cpp +++ b/frameworks/js/napi/inputmethodclient/js_utils.cpp @@ -88,6 +88,8 @@ const std::map JsUtils::ERROR_CODE_MAP = { { ErrorCode::ERROR_IMSA_FORCE_STOP_IME_TIMEOUT, EXCEPTION_IMMS }, { ErrorCode::ERROR_IMC_NULLPTR, EXCEPTION_IMMS }, { ErrorCode::ERROR_DEVICE_UNSUPPORTED, EXCEPTION_UNSUPPORTED }, + { ErrorCode::ERROR_IME_NOT_FOUND, EXCEPTION_IME_NOT_FOUND }, + { ErrorCode::ERROR_DISABLE_SYS_IME, EXCEPTION_DISABLE_SYS_IME }, }; const std::map JsUtils::ERROR_CODE_CONVERT_MESSAGE_MAP = { @@ -112,6 +114,8 @@ const std::map JsUtils::ERROR_CODE_CONVERT_MESSAGE_MAP = { { EXCEPTION_REQUEST_NOT_ACCEPT, "the another side does not accept the request." }, { EXCEPTION_EDITABLE, "the edit mode need enable." }, { EXCEPTION_INVALID_PANEL_TYPE_FLAG, "invalid panel type or panel flag." }, + { EXCEPTION_IME_NOT_FOUND, "the input method does not exist." }, + { EXCEPTION_DISABLE_SYS_IME, " the sys ime does not allow to disabled" }, }; const std::map JsUtils::PARAMETER_TYPE = { diff --git a/frameworks/js/napi/inputmethodclient/js_utils.h b/frameworks/js/napi/inputmethodclient/js_utils.h index bb8894563b6cd3ae7083e77b92ac9e66f6f04129..2178130d88854206a3ba29c7b1ba31f42664f223 100644 --- a/frameworks/js/napi/inputmethodclient/js_utils.h +++ b/frameworks/js/napi/inputmethodclient/js_utils.h @@ -54,6 +54,8 @@ enum IMFErrorCode : int32_t { EXCEPTION_REQUEST_NOT_ACCEPT = 12800015, EXCEPTION_EDITABLE = 12800016, EXCEPTION_INVALID_PANEL_TYPE_FLAG = 12800017, + EXCEPTION_IME_NOT_FOUND = 12800018, + EXCEPTION_DISABLE_SYS_IME = 12800019, }; enum TypeCode : int32_t { diff --git a/frameworks/native/inputmethod_controller/include/input_method_property.h b/frameworks/native/inputmethod_controller/include/input_method_property.h index 2516eef2f65d124d817629d440fb29af6903b4fa..3838355c074b50f1cb616488a84041369736f2f1 100644 --- a/frameworks/native/inputmethod_controller/include/input_method_property.h +++ b/frameworks/native/inputmethod_controller/include/input_method_property.h @@ -13,15 +13,16 @@ * limitations under the License. */ +#ifndef INPUTMETHOD_IMF_INPUT_METHOD_PROPERTY_H +#define INPUTMETHOD_IMF_INPUT_METHOD_PROPERTY_H + #include #include #include +#include "input_method_status.h" #include "parcel.h" -#ifndef INPUTMETHOD_IMF_INPUT_METHOD_PROPERTY_H -#define INPUTMETHOD_IMF_INPUT_METHOD_PROPERTY_H - namespace OHOS { namespace MiscServices { struct Property : public Parcelable { @@ -31,6 +32,7 @@ struct Property : public Parcelable { uint32_t labelId = 0; // the labelId of inputMethod std::string icon; // the icon of inputMethod uint32_t iconId = 0; // the icon id of inputMethod + EnabledStatus status { EnabledStatus::DISABLED }; // the enabled status of inputMethod bool ReadFromParcel(Parcel &in) { @@ -40,6 +42,7 @@ struct Property : public Parcelable { labelId = in.ReadUint32(); icon = in.ReadString(); iconId = in.ReadUint32(); + status = static_cast(in.ReadInt32()); return true; } @@ -63,6 +66,9 @@ struct Property : public Parcelable { if (!out.WriteUint32(iconId)) { return false; } + if (!out.WriteInt32(static_cast(status))) { + return false; + } return true; } @@ -149,6 +155,7 @@ struct FullImeInfo { uint32_t tokenId { 0 }; std::string appId; uint32_t versionCode; + std::string installTime; Property prop; std::vector subProps; }; @@ -157,6 +164,16 @@ struct ImeInfo : public FullImeInfo { SubProperty subProp; bool isSpecificSubName { true }; }; + +struct SwitchInfo { + std::chrono::system_clock::time_point timestamp{}; + std::string bundleName; + std::string subName; + bool operator==(const SwitchInfo &info) const + { + return (timestamp == info.timestamp && bundleName == info.bundleName && subName == info.subName); + } +}; } // namespace MiscServices } // namespace OHOS diff --git a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp index 51ec411880474f14ada576c5afbe2d1c23b0fb24..aa305f903851b9b975e90f1c4937c3b1b6e1e9c4 100644 --- a/frameworks/native/inputmethod_controller/src/input_method_controller.cpp +++ b/frameworks/native/inputmethod_controller/src/input_method_controller.cpp @@ -569,7 +569,8 @@ bool InputMethodController::IsDefaultImeSet() return ret; } -bool InputMethodController::EnableIme(const std::string &bundleName) +int32_t InputMethodController::EnableIme( + const std::string &bundleName, const std::string &extensionName, EnabledStatus status) { IMSA_HILOGI("enter."); auto proxy = GetSystemAbilityProxy(); @@ -577,9 +578,7 @@ bool InputMethodController::EnableIme(const std::string &bundleName) IMSA_HILOGE("proxy is nullptr!"); return false; } - bool ret = false; - proxy->EnableIme(bundleName, ret); - return ret; + return proxy->EnableIme(bundleName, extensionName, static_cast(status)); } int32_t InputMethodController::StartInput( diff --git a/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h b/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h index b14b45adbb56fb8007fd19f4959e99de9cb84c62..9db094f7b7b4cf089447642b042a81edb2f1331d 100644 --- a/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h +++ b/interfaces/inner_api/inputmethod_controller/include/input_method_controller.h @@ -867,7 +867,8 @@ public: * * @since 13 */ - IMF_API bool EnableIme(const std::string &bundleName); + IMF_API int32_t EnableIme(const std::string &bundleName, const std::string &extensionName = "", + EnabledStatus status = EnabledStatus::BASIC_MODE); /** * @brief Send ArrayBuffer message to ime. diff --git a/services/adapter/settings_data_provider/BUILD.gn b/services/adapter/settings_data_provider/BUILD.gn index e4bf3920c4430dd19f5693024f6fc7119a38c278..207b928511d85e78b1c4f9b5aee994785c3909d1 100644 --- a/services/adapter/settings_data_provider/BUILD.gn +++ b/services/adapter/settings_data_provider/BUILD.gn @@ -36,8 +36,7 @@ ohos_static_library("settings_data_static") { sources = [ "common/src/settings_data_observer.cpp", "common/src/settings_data_utils.cpp", - "src/enable_ime_data_parser.cpp", - "src/security_mode_parser.cpp", + "src/ime_enabled_info_manager.cpp", ] public_configs = [ ":settings_data_config" ] @@ -51,7 +50,9 @@ ohos_static_library("settings_data_static") { "c_utils:utils", "data_share:datashare_common", "data_share:datashare_consumer", + "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "input:libmmi-client", "ipc:ipc_single", "resource_management:global_resmgr", 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 602cae5ba1f9f12f3917e70d6ab2b6149afb4252..cb74c20c98059551f4acbc0660530bc8ef4889fd 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 @@ -35,13 +35,11 @@ struct UserImeConfig : public Serializable { std::vector identities; bool Unmarshal(cJSON *node) override { - GetValue(node, userId, identities); - return true; + return GetValue(node, userId, identities); } bool Marshal(cJSON *node) const override { - SetValue(node, userId, identities); - return true; + return SetValue(node, userId, identities); } }; @@ -49,18 +47,21 @@ class SettingsDataUtils : public RefBase { public: static sptr GetInstance(); std::shared_ptr CreateDataShareHelper(const std::string &uriProxy); - int32_t CreateAndRegisterObserver(const std::string &key, SettingsDataObserver::CallbackFunc func); + int32_t CreateAndRegisterObserver( + const std::string &uriProxy, const std::string &key, SettingsDataObserver::CallbackFunc func); int32_t GetStringValue(const std::string &uriProxy, const std::string &key, std::string &value); bool SetStringValue(const std::string &uriProxy, const std::string &key, const std::string &value); bool ReleaseDataShareHelper(std::shared_ptr &helper); Uri GenerateTargetUri(const std::string &uriProxy, const std::string &key); bool EnableIme(int32_t userId, const std::string &bundleName); + void NotifyDataShareReady(); + bool IsDataShareReady(); private: SettingsDataUtils() = default; ~SettingsDataUtils(); - int32_t RegisterObserver(const sptr &observer); - int32_t UnregisterObserver(const sptr &observer); + int32_t RegisterObserver(const std::string &uriProxy, const sptr &observer); + int32_t UnregisterObserver(const std::string &uriProxy, const sptr &observer); sptr GetToken(); std::vector Split(const std::string &text, char separator); std::string SetSettingValues(const std::string &settingValue, const std::string &bundleName); @@ -71,7 +72,8 @@ private: std::mutex remoteObjMutex_; sptr remoteObj_ = nullptr; std::mutex observerListMutex_; - std::vector> observerList_; + std::vector>> observerList_; + std::atomic isDataShareReady_{ false }; }; } // namespace MiscServices } // namespace OHOS 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 26a9308e3df02620cbf4180096f20d1ad08588c1..18521898bd2c738313ddef5131e9b4a27b64a657 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 @@ -15,7 +15,6 @@ #include #include "settings_data_utils.h" -#include "ime_info_inquirer.h" #include "iservice_registry.h" #include "system_ability_definition.h" @@ -32,7 +31,7 @@ SettingsDataUtils::~SettingsDataUtils() std::lock_guard lock(observerListMutex_); if (!observerList_.empty()) { for (auto &iter : observerList_) { - UnregisterObserver(iter); + UnregisterObserver(iter.first, iter.second); } observerList_.clear(); } @@ -54,26 +53,27 @@ sptr SettingsDataUtils::GetInstance() return instance_; } -int32_t SettingsDataUtils::CreateAndRegisterObserver(const std::string &key, SettingsDataObserver::CallbackFunc func) +int32_t SettingsDataUtils::CreateAndRegisterObserver( + const std::string &uriProxy, const std::string &key, SettingsDataObserver::CallbackFunc func) { - IMSA_HILOGD("key: %{public}s.", key.c_str()); + IMSA_HILOGD("uriProxy:%{public}s, key: %{public}s.", uriProxy.c_str(), key.c_str()); sptr observer = new (std::nothrow) SettingsDataObserver(key, func); if (observer == nullptr) { IMSA_HILOGE("observer is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; } - return RegisterObserver(observer); + return RegisterObserver(uriProxy, observer); } -int32_t SettingsDataUtils::RegisterObserver(const sptr &observer) +int32_t SettingsDataUtils::RegisterObserver(const std::string &uriProxy, const sptr &observer) { if (observer == nullptr) { IMSA_HILOGE("observer is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; } - auto uri = GenerateTargetUri(std::string(SETTING_URI_PROXY), observer->GetKey()); - auto helper = SettingsDataUtils::CreateDataShareHelper(std::string(SETTING_URI_PROXY)); + auto uri = GenerateTargetUri(std::string(uriProxy), observer->GetKey()); + auto helper = SettingsDataUtils::CreateDataShareHelper(std::string(uriProxy)); if (helper == nullptr) { IMSA_HILOGE("helper is nullptr!"); return ErrorCode::ERROR_NULL_POINTER; @@ -83,14 +83,14 @@ int32_t SettingsDataUtils::RegisterObserver(const sptr &ob IMSA_HILOGD("succeed to register observer of uri: %{public}s.", uri.ToString().c_str()); std::lock_guard lock(observerListMutex_); - observerList_.push_back(observer); + observerList_.push_back(std::make_pair(uriProxy, observer)); return ErrorCode::NO_ERROR; } -int32_t SettingsDataUtils::UnregisterObserver(const sptr &observer) +int32_t SettingsDataUtils::UnregisterObserver(const std::string &uriProxy, const sptr &observer) { - auto uri = GenerateTargetUri(std::string(SETTING_URI_PROXY), observer->GetKey()); - auto helper = SettingsDataUtils::CreateDataShareHelper(std::string(SETTING_URI_PROXY)); + auto uri = GenerateTargetUri(std::string(uriProxy), observer->GetKey()); + auto helper = SettingsDataUtils::CreateDataShareHelper(std::string(uriProxy)); if (helper == nullptr) { return ErrorCode::ERROR_ENABLE_IME; } @@ -271,5 +271,15 @@ std::string SettingsDataUtils::SetSettingValues(const std::string &settingValue, } return value; } + +void SettingsDataUtils::NotifyDataShareReady() +{ + isDataShareReady_.store(true); +} + +bool SettingsDataUtils::IsDataShareReady() +{ + return isDataShareReady_.load(); +} } // namespace MiscServices } // namespace OHOS \ No newline at end of file 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 deleted file mode 100644 index 5e0f97344d00d3dc88755b94b1407d387689f7da..0000000000000000000000000000000000000000 --- a/services/adapter/settings_data_provider/include/enable_ime_data_parser.h +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ENABLE_IME_DATA_PARSER_H -#define ENABLE_IME_DATA_PARSER_H - -#include "input_method_status.h" -#include "settings_data_utils.h" - -namespace OHOS { -namespace MiscServices { -struct SwitchInfo { - std::chrono::system_clock::time_point timestamp{}; - std::string bundleName; - std::string subName; - bool operator==(const SwitchInfo &info) const - { - return (timestamp == info.timestamp && bundleName == info.bundleName && subName == info.subName); - } -}; - -struct EnableKeyBoardCfg : public Serializable { - UserImeConfig userImeCfg; - bool Unmarshal(cJSON *node) override - { - GetValue(node, GET_NAME(enableKeyboardList), userImeCfg); - return true; - } -}; - -struct EnableImeCfg : public Serializable { - UserImeConfig userImeCfg; - bool Unmarshal(cJSON *node) override - { - GetValue(node, GET_NAME(enableImeList), userImeCfg); - return true; - } - bool Marshal(cJSON *node) const override - { - SetValue(node, GET_NAME(enableImeList), userImeCfg); - return true; - } -}; - -struct TempImeCfg : public Serializable { - UserImeConfig tempImeList; - bool Unmarshal(cJSON *node) override - { - GetValue(node, GET_NAME(tempImeList), tempImeList); - return true; - } -}; - -class EnableImeDataParser : public RefBase { -public: - static sptr GetInstance(); - int32_t Initialize(const int32_t userId); - int32_t GetEnableData(const std::string &key, std::vector &enableVec, const int32_t userId); - int32_t GetEnableIme(int32_t userId, std::vector &enableVec); - // for enable list changed - bool CheckNeedSwitch(const std::string &key, SwitchInfo &switchInfo, const int32_t userId); - // for switch target ime - bool CheckNeedSwitch(const SwitchInfo &info, const int32_t userId); - void OnUserChanged(const int32_t userId); - void OnConfigChanged(int32_t userId, const std::string &key); - void OnPackageAdded(int32_t userId, const std::string &bundleName); - int32_t GetImeEnablePattern(int32_t userId, const std::string &bundleName, EnabledStatus &status); - static constexpr const char *ENABLE_IME = "settings.inputmethod.enable_ime"; - static constexpr const char *ENABLE_KEYBOARD = "settings.inputmethod.enable_keyboard"; - static constexpr const char *TEMP_IME = "settings.inputmethod.temp_ime"; - -private: - EnableImeDataParser() = default; - ~EnableImeDataParser(); - int32_t UpdateEnableData(int32_t userId, const std::string &key); - void CoverGlobalEnableTable(const std::string &valueStr); - std::string GetUserEnableTable(int32_t userId); - std::string GetEanbleIme(int32_t userId, const std::string &globalStr); - std::string GetGlobalTableUserId(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); - bool ParseTempIme(const std::string &valueStr, int32_t userId, std::vector &tempVector); - bool CheckTargetEnableName(const std::string &key, const std::string &targetName, std::string &nextIme, - const int32_t userId); - std::shared_ptr GetDefaultIme(); - void OnBackgroundPackageAdded(int32_t userId, const std::string &bundleName, const std::string &globalContent); - void OnForegroundPackageAdded(int32_t userId, const std::string &bundleName, const std::string &globalContent); - int32_t AddToUserEnableTable(int32_t userId, const std::string &bundleName, std::string &userContent); - int32_t AddToGlobalEnableTable(int32_t userId, const std::string &bundleName, std::string &globalContent); - int32_t AddToEnableTable( - int32_t userId, const std::string &bundleName, const std::string &uriProxy, std::string &tableContent); - int32_t CoverUserEnableTable(int32_t userId, const std::string &userContent); - -private: - static std::mutex instanceMutex_; - static sptr instance_; - std::mutex listMutex_; - std::unordered_map> enableList_; - std::mutex defaultImeMutex_; - std::shared_ptr defaultImeInfo_{ nullptr }; - int32_t currentUserId_ = 0; - bool isEnableImeInit_{ false }; - std::mutex userIdLock_; -}; -} // namespace MiscServices -} // namespace OHOS - -#endif // ENABLE_IME_DATA_PARSER_H diff --git a/services/adapter/settings_data_provider/include/ime_enabled_info_manager.h b/services/adapter/settings_data_provider/include/ime_enabled_info_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..869e8641aa8252cb7ad928c5dffaf475022f23cc --- /dev/null +++ b/services/adapter/settings_data_provider/include/ime_enabled_info_manager.h @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IME_ENABLED_INFO_MANAGER_H +#define IME_ENABLED_INFO_MANAGER_H + +#include "event_handler.h" +#include "input_method_property.h" +#include "input_method_status.h" +#include "serializable.h" +#include "settings_data_utils.h" + +namespace OHOS { +namespace MiscServices { +// old ime enabled cfg +struct EnabledImeCfg : public Serializable { + UserImeConfig userImeCfg; + bool Unmarshal(cJSON *node) override + { + return GetValue(node, GET_NAME(enableImeList), userImeCfg); + } + bool Marshal(cJSON *node) const override + { + return SetValue(node, GET_NAME(enableImeList), userImeCfg); + } +}; +// old ime full experience cfg +struct SecurityModeCfg : public Serializable { + UserImeConfig userImeCfg; + bool Unmarshal(cJSON *node) override + { + return GetValue(node, GET_NAME(fullExperienceList), userImeCfg); + } +}; + +struct ImeEnabledInfo : public Serializable { + std::string bundleName; + std::string extensionName; + EnabledStatus enabledStatus{ EnabledStatus::DISABLED }; + std::string stateUpdateTime; // user trigger + std::string installTime; + bool Unmarshal(cJSON *node) override + { + auto ret = GetValue(node, GET_NAME(bundleName), bundleName); + ret = GetValue(node, GET_NAME(extensionName), extensionName) && ret; + int32_t enabledStatusTmp = 0; + ret = GetValue(node, GET_NAME(enabledStatus), enabledStatusTmp) && ret; + enabledStatus = static_cast(enabledStatusTmp); + ret = GetValue(node, GET_NAME(stateUpdateTime), stateUpdateTime) && ret; + return GetValue(node, GET_NAME(installTime), installTime) && ret; + } + bool Marshal(cJSON *node) const override + { + auto ret = SetValue(node, GET_NAME(bundleName), bundleName); + ret = SetValue(node, GET_NAME(extensionName), extensionName) && ret; + auto enabledStatusTmp = static_cast(enabledStatus); + ret = SetValue(node, GET_NAME(enabledStatus), enabledStatusTmp) && ret; + ret = SetValue(node, GET_NAME(stateUpdateTime), stateUpdateTime) && ret; + return SetValue(node, GET_NAME(installTime), installTime) && ret; + } + bool operator==(const ImeEnabledInfo &enabledInfo) const + { + return bundleName == enabledInfo.bundleName && extensionName == enabledInfo.extensionName + && enabledStatus == enabledInfo.enabledStatus && installTime == enabledInfo.installTime; + } +}; +struct ImeEnabledCfg : public Serializable { + std::string version; + std::vector enabledInfos; + bool Unmarshal(cJSON *node) override + { + auto ret = GetValue(node, GET_NAME(version), version); + return GetValue(node, GET_NAME(inputmethods), enabledInfos) && ret; + } + bool Marshal(cJSON *node) const override + { + auto ret = SetValue(node, GET_NAME(version), version); + return SetValue(node, GET_NAME(inputmethods), enabledInfos) && ret; + } + bool operator==(const ImeEnabledCfg &enabledCfg) const + { + return version == enabledCfg.version && enabledInfos == enabledCfg.enabledInfos; + } +}; +using EnableChangedHandler = + std::function; +class ImeEnabledInfoManager { +public: + static constexpr const char *ENABLE_IME = "settings.inputmethod.enable_ime"; + static constexpr const char *SECURITY_MODE = "settings.inputmethod.full_experience"; + static ImeEnabledInfoManager &GetInstance(); + void SetEnableChangedHandler(EnableChangedHandler handler); + void SetEventHandler(const std::shared_ptr &eventHandler); + int32_t Init(const std::map> &fullImeInfos); + int32_t Add(int32_t userId, const std::vector &imeInfos); + int32_t Delete(int32_t userId); + int32_t Add(int32_t userId, const FullImeInfo &imeInfo); + int32_t Delete(int32_t userId, const std::string &bundleName); + int32_t Update( + int32_t userId, const std::string &bundleName, const std::string &extensionName, EnabledStatus status); + int32_t GetEnabledState(int32_t userId, const std::string &bundleName, EnabledStatus &status); + int32_t GetEnabledStates(int32_t userId, std::vector &props); // props not has sysSpecialIme + bool IsDefaultFullMode(int32_t userId, const std::string &bundleName); + void OnFullExperienceTableChanged(int32_t userId); // add for compatibility + +private: + ImeEnabledInfoManager() = default; + ~ImeEnabledInfoManager(); + int32_t CorrectAdd(int32_t userId, const std::vector &imeInfos = {}); + int32_t AddUser(int32_t userId, const std::vector &imeInfos = {}); + int32_t GetEnabledStateInner(int32_t userId, const std::string &bundleName, EnabledStatus &status); + int32_t GetEnabledStateInner(int32_t userId, std::vector &props); + int32_t GetEnabledCfg( + int32_t userId, ImeEnabledCfg &cfg, bool isCorrectByBmg, const std::vector &imeInfos = {}); + int32_t GetEnabledTableCfg(int32_t userId, ImeEnabledCfg &cfg); + int32_t GetOldEnabledTableCfg(int32_t userId, const std::string &content, ImeEnabledCfg &cfg); + int32_t GetNewEnabledTableCfg(int32_t userId, const std::string &content, ImeEnabledCfg &cfg); + int32_t MergeFullExperienceTableCfg(int32_t userId, ImeEnabledCfg &cfg); + int32_t ParseFullExperienceTableCfg(int32_t userId, const std::string &content, std::set &bundleNames); + int32_t CorrectByBundleMgr( + int32_t userId, std::vector &enabledInfos, const std::vector &imeInfos); + void CorrectBySysEnabledSwitch(ImeEnabledInfo &info); + void CorrectBySysEnabledSwitch(std::vector &infos); + void CorrectBySysIme(ImeEnabledInfo &info); + void CorrectBySysIme(std::vector &infos); + int32_t SetEnabledCfg(int32_t userId, const ImeEnabledCfg &cfg); + int32_t GetEnabledCfgFromCache(int32_t userId, ImeEnabledCfg &enabledCfg); + int32_t GetEnabledCfgFromCacheWithCorrect(int32_t userId, ImeEnabledCfg &enabledCfg); + void PostCorrectAddTask(int32_t userId); + void NotifyEnableChange(int32_t userId, const std::string &bundleName, EnabledStatus oldStatus); + std::pair CheckUpdate( + int32_t userId, const std::string &bundleName, const std::string &extensionName, EnabledStatus status); + bool HasEnabledSwitch(); + bool IsExpired(const std::string &expirationTime); + void ModGlobalEnabledTable(int32_t userId, const ImeEnabledCfg &newEnabledCfg); // add for compatibility + std::string GetGlobalTableUserId(const std::string &valueStr); // add for compatibility + std::mutex imeEnabledCfgLock_; + std::map imeEnabledCfg_; + EnableChangedHandler enableChangedHandler_; + std::shared_ptr serviceHandler_{ nullptr }; + std::mutex settingOperateLock_; + int32_t currentUserId_{ -1 }; +}; +} // namespace MiscServices +} // namespace OHOS + +#endif // IME_ENABLED_INFO_MANAGER_H diff --git a/services/adapter/settings_data_provider/include/security_mode_parser.h b/services/adapter/settings_data_provider/include/security_mode_parser.h deleted file mode 100644 index b1b9c3b4854a0f25d5d723620fa896fcd402e657..0000000000000000000000000000000000000000 --- a/services/adapter/settings_data_provider/include/security_mode_parser.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SECURITY_MODE_PARSER_H -#define SECURITY_MODE_PARSER_H - -#include "input_method_utils.h" -#include "settings_data_utils.h" - -namespace OHOS { -namespace MiscServices { -struct SecModeCfg : public Serializable { - UserImeConfig userImeCfg; - bool Unmarshal(cJSON *node) override - { - GetValue(node, GET_NAME(fullExperienceList), userImeCfg); - return true; - } -}; -class SecurityModeParser : public RefBase { -public: - static sptr GetInstance(); - int32_t Initialize(const int32_t userId); - SecurityMode GetSecurityMode(const std::string &bundleName, int32_t userId); - int32_t UpdateFullModeList(int32_t userId); - bool IsDefaultFullMode(const std::string &bundleName, int32_t userId); - bool IsSecurityMode(int32_t userId, const std::string &bundleName); - static constexpr const char *SECURITY_MODE = "settings.inputmethod.full_experience"; - -private: - SecurityModeParser() = default; - ~SecurityModeParser(); - - bool ParseSecurityMode(const std::string &valueStr, const int32_t userId); - bool IsFullMode(const std::string bundleName); - bool IsExpired(const std::string &expirationTime); - static std::mutex instanceMutex_; - static sptr instance_; - std::mutex listMutex_; - std::vector fullModeList_; - std::mutex initLock_; - bool initialized_{ false }; -}; -} // namespace MiscServices -} // namespace OHOS - -#endif // SECURITY_MODE_PARSER_H \ No newline at end of file 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 deleted file mode 100644 index 68b8bb7276d5b19345c1e00f23fc82bd8ece2ea8..0000000000000000000000000000000000000000 --- a/services/adapter/settings_data_provider/src/enable_ime_data_parser.cpp +++ /dev/null @@ -1,567 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "enable_ime_data_parser.h" - -#include - -#include "ime_info_inquirer.h" - -namespace OHOS { -namespace MiscServices { -std::mutex EnableImeDataParser::instanceMutex_; -sptr EnableImeDataParser::instance_ = nullptr; -EnableImeDataParser::~EnableImeDataParser() -{ -} - -sptr EnableImeDataParser::GetInstance() -{ - if (instance_ == nullptr) { - std::lock_guard autoLock(instanceMutex_); - if (instance_ == nullptr) { - IMSA_HILOGI("need to create instance."); - instance_ = new (std::nothrow) EnableImeDataParser(); - if (instance_ == nullptr) { - IMSA_HILOGE("instance is nullptr!"); - return instance_; - } - } - } - return instance_; -} - -int32_t EnableImeDataParser::Initialize(const int32_t userId) -{ - currentUserId_ = userId; - { - std::lock_guard autoLock(listMutex_); - enableList_.insert({ std::string(ENABLE_IME), {} }); - enableList_.insert({ std::string(ENABLE_KEYBOARD), {} }); - } - UpdateEnableData(userId, ENABLE_IME); - UpdateEnableData(userId, ENABLE_KEYBOARD); - GetDefaultIme(); - return ErrorCode::NO_ERROR; -} - -void EnableImeDataParser::OnUserChanged(const int32_t targetUserId) -{ - std::lock_guard lock(userIdLock_); - IMSA_HILOGI("run in %{public}d}.", targetUserId); - currentUserId_ = targetUserId; - UpdateEnableData(targetUserId, ENABLE_IME); - UpdateEnableData(targetUserId, ENABLE_KEYBOARD); -} - -bool EnableImeDataParser::CheckNeedSwitch(const std::string &key, SwitchInfo &switchInfo, const int32_t userId) -{ - IMSA_HILOGD("start, data changed."); - auto currentIme = ImeInfoInquirer::GetInstance().GetCurrentInputMethod(userId); - auto defaultIme = GetDefaultIme(); - if (defaultIme == nullptr) { - IMSA_HILOGE("defaultIme is nullptr!"); - return false; - } - switchInfo.bundleName = defaultIme->name; - switchInfo.subName = ""; - if (currentIme == nullptr) { - IMSA_HILOGE("currentIme is nullptr!"); - return true; - } - if (key == std::string(ENABLE_IME)) { - if (currentIme->name == defaultIme->name) { - std::lock_guard autoLock(listMutex_); - GetEnableData(key, enableList_[key], userId); - IMSA_HILOGD("current ime is default, do not need switch ime."); - return false; - } - return CheckTargetEnableName(key, currentIme->name, switchInfo.bundleName, userId); - } else if (key == std::string(ENABLE_KEYBOARD)) { - if (currentIme->name != defaultIme->name || currentIme->id == defaultIme->id) { - IMSA_HILOGD("current ime is not default or id is default."); - std::lock_guard autoLock(listMutex_); - GetEnableData(key, enableList_[key], userId); - return false; - } - switchInfo.subName = defaultIme->id; - return CheckTargetEnableName(key, currentIme->id, switchInfo.subName, userId); - } - IMSA_HILOGW("invalid key: %{public}s.", key.c_str()); - return false; -} - -bool EnableImeDataParser::CheckNeedSwitch(const SwitchInfo &info, const int32_t userId) -{ - IMSA_HILOGD("current userId: %{public}d, target userId: %{public}d, check bundleName: %{public}s", currentUserId_, - userId, info.bundleName.c_str()); - if (info.bundleName == GetDefaultIme()->name) { - IMSA_HILOGD("default ime, permit to switch"); - return true; - } - IMSA_HILOGD("check ime."); - std::vector tempVec; - int32_t tempRet = GetEnableData(TEMP_IME, tempVec, userId); - if (tempRet != ErrorCode::NO_ERROR || tempVec.empty()) { - IMSA_HILOGD("get tempVec list failed, or tempVec list is empty."); - } else { - auto iter = std::find_if( - tempVec.begin(), tempVec.end(), [&info](const std::string &ime) { return info.bundleName == ime; }); - if (iter != tempVec.end()) { - IMSA_HILOGD("In tempVec list."); - return true; - } - } - std::vector enableVec; - int32_t ret = GetEnableData(ENABLE_IME, enableVec, userId); - if (ret != ErrorCode::NO_ERROR || enableVec.empty()) { - IMSA_HILOGD("get enable list failed, or enable list is empty."); - return false; - } - - auto iter = std::find_if(enableVec.begin(), enableVec.end(), - [&info](const std::string &ime) { return info.bundleName == ime; }); - if (iter != enableVec.end()) { - IMSA_HILOGD("in enable list."); - return true; - } - return false; -} - -bool EnableImeDataParser::CheckTargetEnableName(const std::string &key, const std::string &targetName, - std::string &nextIme, const int32_t userId) -{ - IMSA_HILOGD("start."); - std::vector enableVec; - int32_t ret = GetEnableData(key, enableVec, userId); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("get enable list abnormal."); - return false; - } - - if (enableVec.empty()) { - IMSA_HILOGE("enable empty, switch default ime."); - return true; - } - std::lock_guard autoLock(listMutex_); - auto iter = std::find_if(enableVec.begin(), enableVec.end(), - [&targetName](const std::string &ime) { return ime == targetName; }); - if (iter != enableVec.end()) { - IMSA_HILOGD("enable list has current ime, do not need switch."); - enableList_[key].assign(enableVec.begin(), enableVec.end()); - return false; - } - - auto it = std::find_if(enableList_[key].begin(), enableList_[key].end(), - [&targetName](const std::string &ime) { return ime == targetName; }); - if (it == enableList_[key].end()) { - enableList_[key].assign(enableVec.begin(), enableVec.end()); - return true; - } - - std::rotate(enableList_[key].begin(), it, enableList_[key].end()); - auto result = - std::find_first_of(enableList_[key].begin(), enableList_[key].end(), enableVec.begin(), enableVec.end()); - if (result != enableList_[key].end()) { - IMSA_HILOGD("found the next cached ime in enable ime list."); - nextIme = *result; - } - enableList_[key].assign(enableVec.begin(), enableVec.end()); - return true; -} - -void EnableImeDataParser::CoverGlobalEnableTable(const std::string &valueStr) -{ - SettingsDataUtils::GetInstance()->SetStringValue(SETTING_URI_PROXY, ENABLE_IME, valueStr); -} - -std::string EnableImeDataParser::GetUserEnableTable(int32_t userId) -{ - std::string valueStr; - int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue( - SETTINGS_USER_DATA_URI + std::to_string(userId) + "?Proxy=true", ENABLE_IME, valueStr); - IMSA_HILOGI("get user enable table, userId = %{public}d, ret = %{public}d, valurStr = %{public}s", - userId, ret, valueStr.c_str()); - if (valueStr.empty()) { - auto defaultIme = GetDefaultIme(); - if (defaultIme != nullptr) { - valueStr = - "{\"enableImeList\" : {\"" + std::to_string(userId) + "\" : [\"" + defaultIme->name + "\"]}}"; - } - } - return valueStr; -} - -std::string EnableImeDataParser::GetEanbleIme(int32_t userId, const std::string &globalStr) -{ - std::string enableStr = globalStr; - std::string globaleUserId; - if (enableStr.empty()) { - enableStr = GetUserEnableTable(currentUserId_); - CoverGlobalEnableTable(enableStr); - globaleUserId = std::to_string(currentUserId_); - } else { - globaleUserId = GetGlobalTableUserId(enableStr); - } - SettingsDataUtils::GetInstance()->SetStringValue(SETTINGS_USER_DATA_URI + globaleUserId + "?Proxy=true", - ENABLE_IME, enableStr); - if (globaleUserId != std::to_string(currentUserId_)) { - enableStr = GetUserEnableTable(currentUserId_); - CoverGlobalEnableTable(enableStr); - } - if (currentUserId_ != userId) { - enableStr = GetUserEnableTable(userId); - } - return enableStr; -} - -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(SETTING_URI_PROXY, key, valueStr); - if (ret == ErrorCode::ERROR_KEYWORD_NOT_FOUND) { - IMSA_HILOGW("no keyword exist"); - enableVec.clear(); - return ErrorCode::NO_ERROR; - } - 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) { - valueStr = GetEanbleIme(userId, valueStr); - if (valueStr.empty()) { - IMSA_HILOGW("valueStr is empty, userId = %{public}d", userId); - return ErrorCode::NO_ERROR; - } - parseRet = ParseEnableIme(valueStr, userId, enableVec); - } - if (key == ENABLE_KEYBOARD) { - parseRet = ParseEnableKeyboard(valueStr, userId, enableVec); - } - if (key == TEMP_IME) { - parseRet = ParseTempIme(valueStr, userId, enableVec); - } - return parseRet ? ErrorCode::NO_ERROR : ErrorCode::ERROR_ENABLE_IME; -} - -std::string EnableImeDataParser::GetGlobalTableUserId(const std::string &valueStr) -{ - auto root = cJSON_Parse(valueStr.c_str()); - if (root == nullptr) { - IMSA_HILOGE("valueStr content parse failed!"); - return ""; - } - auto subNode = Serializable::GetSubNode(root, "enableImeList"); - if (subNode == nullptr || !cJSON_IsObject(subNode)) { - IMSA_HILOGW("subNode is null or not object"); - cJSON_Delete(root); - return ""; - } - if (subNode->child == nullptr) { - IMSA_HILOGW("subNode has not child"); - cJSON_Delete(root); - return ""; - } - std::string userId = subNode->child->string; - cJSON_Delete(root); - return userId; -} - -bool EnableImeDataParser::ParseTempIme(const std::string &valueStr, int32_t userId, - std::vector &tempVector) -{ - TempImeCfg tempIme; - tempIme.tempImeList.userId = std::to_string(userId); - auto ret = tempIme.Unmarshall(valueStr); - if (!ret) { - return ret; - } - tempVector = tempIme.tempImeList.identities; - return true; -} - -bool EnableImeDataParser::ParseEnableIme(const std::string &valueStr, int32_t userId, - std::vector &enableVec) -{ - EnableImeCfg enableIme; - enableIme.userImeCfg.userId = std::to_string(userId); - auto ret = enableIme.Unmarshall(valueStr); - if (!ret) { - return ret; - } - enableVec = enableIme.userImeCfg.identities; - return true; -} - -bool EnableImeDataParser::ParseEnableKeyboard(const std::string &valueStr, int32_t userId, - std::vector &enableVec) -{ - EnableKeyBoardCfg enableKeyboard; - enableKeyboard.userImeCfg.userId = std::to_string(userId); - auto ret = enableKeyboard.Unmarshall(valueStr); - if (!ret) { - return ret; - } - enableVec = enableKeyboard.userImeCfg.identities; - return true; -} - -std::shared_ptr EnableImeDataParser::GetDefaultIme() -{ - std::lock_guard lock(defaultImeMutex_); - if (defaultImeInfo_ == nullptr) { - defaultImeInfo_ = std::make_shared(); - } - if (!defaultImeInfo_->name.empty() && !defaultImeInfo_->id.empty()) { - IMSA_HILOGD("defaultImeInfo_ has cached default time: %{public}s.", defaultImeInfo_->name.c_str()); - return defaultImeInfo_; - } - - auto defaultIme = ImeInfoInquirer::GetInstance().GetDefaultImeCfgProp(); - if (defaultIme == nullptr) { - IMSA_HILOGE("defaultIme is nullptr!"); - return defaultImeInfo_; - } - defaultImeInfo_->name = defaultIme->name; - defaultImeInfo_->id = defaultIme->id; - return defaultImeInfo_; -} - -void EnableImeDataParser::OnConfigChanged(int32_t userId, const std::string &key) -{ - UpdateEnableData(userId, key); -} - -void EnableImeDataParser::OnPackageAdded(int32_t userId, const std::string &bundleName) -{ - IMSA_HILOGI("run in:%{public}d,%{public}s.", userId, bundleName.c_str()); - auto initEnabledState = ImeInfoInquirer::GetInstance().GetSystemInitEnabledState(); - if (initEnabledState != EnabledStatus::BASIC_MODE) { - IMSA_HILOGI("init enabled state is %{public}d.", static_cast(initEnabledState)); - return; - } - if (bundleName == ImeInfoInquirer::GetInstance().GetDefaultIme().bundleName) { - IMSA_HILOGI("default ime not deal:%{public}d,%{public}s.", userId, bundleName.c_str()); - return; - } - auto settingInstance = SettingsDataUtils::GetInstance(); - if (settingInstance == nullptr) { - return; - } - std::lock_guard lock(userIdLock_); - std::string globalStr; - int32_t ret = settingInstance->GetStringValue(SETTING_URI_PROXY, ENABLE_IME, globalStr); - if (ret != ErrorCode::NO_ERROR && ret != ErrorCode::ERROR_KEYWORD_NOT_FOUND) { - IMSA_HILOGE("get global table failed:%{public}d.", ret); - return; - } - if (userId == currentUserId_) { - OnForegroundPackageAdded(userId, bundleName, globalStr); - return; - } - OnBackgroundPackageAdded(userId, bundleName, globalStr); -} - -void EnableImeDataParser::OnBackgroundPackageAdded( - int32_t userId, const std::string &bundleName, const std::string &globalContent) -{ - IMSA_HILOGI("run in:%{public}d,%{public}s,%{public}s.", userId, bundleName.c_str(), globalContent.c_str()); - auto globalUserId = GetGlobalTableUserId(globalContent); - if (!globalUserId.empty() && globalUserId == std::to_string(userId)) { - IMSA_HILOGW("background add, but globalUserId same with userId:[%{public}d,%{public}d,%{public}s].", - currentUserId_, userId, bundleName.c_str()); - } - std::string finalUserContent; - AddToUserEnableTable(userId, bundleName, finalUserContent); -} - -void EnableImeDataParser::OnForegroundPackageAdded( - int32_t userId, const std::string &bundleName, const std::string &globalContent) -{ - IMSA_HILOGI("run in:%{public}d,%{public}s,%{public}s.", userId, bundleName.c_str(), globalContent.c_str()); - auto globalUserId = GetGlobalTableUserId(globalContent); - if (globalUserId.empty() || globalUserId == std::to_string(currentUserId_)) { - std::string finalGlobalContent; - auto ret = AddToGlobalEnableTable(userId, bundleName, finalGlobalContent); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("add failed:[%{public}d,%{public}s,%{public}s,%{public}d].", userId, globalUserId.c_str(), - bundleName.c_str(), ret); - return; - } - CoverUserEnableTable(userId, finalGlobalContent); - return; - } - IMSA_HILOGW("globalUserId not same with currentUserId:%{public}d,%{public}s,%{public}s.", userId, - bundleName.c_str(), globalContent.c_str()); - auto ret = CoverUserEnableTable(atoi(globalUserId.c_str()), globalContent); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("cover failed:[%{public}d,%{public}s,%{public}s,%{public}d].", userId, globalUserId.c_str(), - bundleName.c_str(), ret); - return; - } - std::string finalUserContent; - ret = AddToUserEnableTable(userId, bundleName, finalUserContent); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("userId not same, add failed:[%{public}d,%{public}s,%{public}s,%{public}d].", userId, - globalUserId.c_str(), bundleName.c_str(), ret); - return; - } - CoverGlobalEnableTable(finalUserContent); -} - -int32_t EnableImeDataParser::AddToUserEnableTable( - int32_t userId, const std::string &bundleName, std::string &userContent) -{ - return AddToEnableTable( - userId, bundleName, SETTINGS_USER_DATA_URI + std::to_string(userId) + "?Proxy=true", userContent); -} - -int32_t EnableImeDataParser::AddToGlobalEnableTable( - int32_t userId, const std::string &bundleName, std::string &globalContent) -{ - return AddToEnableTable(userId, bundleName, SETTING_URI_PROXY, globalContent); -} - -int32_t EnableImeDataParser::AddToEnableTable( - int32_t userId, const std::string &bundleName, const std::string &uriProxy, std::string &tableContent) -{ - auto settingInstance = SettingsDataUtils::GetInstance(); - if (settingInstance == nullptr) { - return ErrorCode::ERROR_ENABLE_IME; - } - std::string valueStr; - int32_t ret = settingInstance->GetStringValue(uriProxy, ENABLE_IME, valueStr); - if (ret != ErrorCode::NO_ERROR && ret != ErrorCode::ERROR_KEYWORD_NOT_FOUND) { - IMSA_HILOGE("get enable table failed:[%{public}d, %{public}s, %{public}s, %{public}d].", userId, - bundleName.c_str(), uriProxy.c_str(), ret); - return ret; - } - IMSA_HILOGI("start:%{public}d,%{public}s,%{public}s,%{public}s.", userId, bundleName.c_str(), uriProxy.c_str(), - valueStr.c_str()); - EnableImeCfg imeCfg; - imeCfg.userImeCfg.userId = std::to_string(userId); - imeCfg.Unmarshall(valueStr); - auto it = std::find_if(imeCfg.userImeCfg.identities.begin(), imeCfg.userImeCfg.identities.end(), - [&bundleName](const std::string &bundleNameTmp) { return bundleNameTmp == bundleName; }); - if (it == imeCfg.userImeCfg.identities.end()) { - imeCfg.userImeCfg.identities.push_back(bundleName); - } - imeCfg.Marshall(tableContent); - if (tableContent.empty()) { - IMSA_HILOGE( - "Marshall failed:[%{public}d, %{public}s, %{public}s].", userId, bundleName.c_str(), uriProxy.c_str()); - return ErrorCode::ERROR_ENABLE_IME; - } - settingInstance->SetStringValue(uriProxy, ENABLE_IME, tableContent); - IMSA_HILOGI("end:%{public}d,%{public}s,%{public}s,%{public}s.", userId, bundleName.c_str(), uriProxy.c_str(), - tableContent.c_str()); - return ErrorCode::NO_ERROR; -} - -int32_t EnableImeDataParser::CoverUserEnableTable(int32_t userId, const std::string &userContent) -{ - auto settingInstance = SettingsDataUtils::GetInstance(); - if (settingInstance == nullptr) { - return ErrorCode::ERROR_ENABLE_IME; - } - auto ret = settingInstance->SetStringValue( - SETTINGS_USER_DATA_URI + std::to_string(userId) + "?Proxy=true", ENABLE_IME, userContent); - if (!ret) { - return ErrorCode::ERROR_ENABLE_IME; - } - return ErrorCode::NO_ERROR; -} - -int32_t EnableImeDataParser::GetImeEnablePattern(int32_t userId, const std::string &bundleName, EnabledStatus &status) -{ - if (ImeInfoInquirer::GetInstance().GetDefaultIme().bundleName == bundleName) { - status = EnabledStatus::BASIC_MODE; - return ErrorCode::NO_ERROR; - } - std::vector bundleNames; - auto ret = GetEnableIme(userId, bundleNames); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("[%{public}d, %{public}s] GetEnableIme failed:%{public}d.", userId, bundleName.c_str(), ret); - return ErrorCode::ERROR_ENABLE_IME; - } - auto it = std::find_if(bundleNames.begin(), bundleNames.end(), - [&bundleName](const std::string &bundleNameTmp) { return bundleNameTmp == bundleName; }); - if (it == bundleNames.end()) { - status = EnabledStatus::DISABLED; - return ErrorCode::NO_ERROR; - } - status = EnabledStatus::BASIC_MODE; - return ErrorCode::NO_ERROR; -} - -int32_t EnableImeDataParser::UpdateEnableData(int32_t userId, const std::string &key) -{ - std::lock_guard autoLock(listMutex_); - if (key == ENABLE_IME) { - isEnableImeInit_ = false; - } - std::vector enableData; - IMSA_HILOGD("update userId: %{public}d %{public}s", userId, key.c_str()); - auto ret = GetEnableData(key, enableData, userId); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("userId: %{public}d get enable %{public}s list failed!", userId, key.c_str()); - return ret; - } - enableList_.insert_or_assign(key, enableData); - if (key == ENABLE_IME) { - isEnableImeInit_ = true; - } - return ErrorCode::NO_ERROR; -} - -int32_t EnableImeDataParser::GetEnableIme(int32_t userId, std::vector &enableVec) -{ - if (userId != currentUserId_) { - return GetEnableData(ENABLE_IME, enableVec, userId); - } - auto ret = GetEnableImeFromCache(enableVec); - if (ret == ErrorCode::NO_ERROR) { - return ErrorCode::NO_ERROR; - } - ret = UpdateEnableData(userId, ENABLE_IME); - if (ret != ErrorCode::NO_ERROR) { - return ret; - } - return GetEnableImeFromCache(enableVec); -} - -int32_t EnableImeDataParser::GetEnableImeFromCache(std::vector &enableVec) -{ - std::lock_guard autoLock(listMutex_); - if (isEnableImeInit_) { - auto it = enableList_.find(ENABLE_IME); - if (it != enableList_.end()) { - IMSA_HILOGD("GetEnableIme from cache."); - enableVec = it->second; - return ErrorCode::NO_ERROR; - } - } - return ErrorCode::ERROR_ENABLE_IME; -} -} // namespace MiscServices -} // namespace OHOS \ No newline at end of file diff --git a/services/adapter/settings_data_provider/src/ime_enabled_info_manager.cpp b/services/adapter/settings_data_provider/src/ime_enabled_info_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e74a69436b7a4695dfd222ec935e06164ac6de8b --- /dev/null +++ b/services/adapter/settings_data_provider/src/ime_enabled_info_manager.cpp @@ -0,0 +1,836 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ime_enabled_info_manager.h" + +#include +#include +#include +#include +#include + +#include "ime_info_inquirer.h" +#include "parameter.h" +namespace OHOS { +namespace MiscServices { +using namespace std::chrono; +ImeEnabledInfoManager &ImeEnabledInfoManager::GetInstance() +{ + static ImeEnabledInfoManager instance; + return instance; +} + +ImeEnabledInfoManager::~ImeEnabledInfoManager() +{ +} + +void ImeEnabledInfoManager::SetEnableChangedHandler(EnableChangedHandler handler) +{ + if (enableChangedHandler_ != nullptr) { + return; + } + enableChangedHandler_ = std::move(handler); +} + +void ImeEnabledInfoManager::SetEventHandler(const std::shared_ptr &eventHandler) +{ + serviceHandler_ = eventHandler; +} + +int32_t ImeEnabledInfoManager::Init(const std::map> &fullImeInfos) +{ + if (!HasEnabledSwitch()) { + return ErrorCode::NO_ERROR; + } + IMSA_HILOGI("run in, user num:%{public}zu.", fullImeInfos.size()); + for (const auto &fullImeInfo : fullImeInfos) { + AddUser(fullImeInfo.first, fullImeInfo.second); + } + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::Add(int32_t userId, const std::vector &imeInfos) +{ + IMSA_HILOGI("userId:%{public}d", userId); + currentUserId_ = userId; + ImeEnabledCfg cfg; + auto ret = GetEnabledCfgFromCache(userId, cfg); + if (ret == ErrorCode::NO_ERROR) { + ModGlobalEnabledTable(userId, cfg); // user switch, replace global table by user table + return ret; + } + return AddUser(userId, imeInfos); +} + +int32_t ImeEnabledInfoManager::CorrectAdd(int32_t userId, const std::vector &imeInfos) +{ + ImeEnabledCfg cfg; + auto ret = GetEnabledCfgFromCache(userId, cfg); + if (ret == ErrorCode::NO_ERROR) { + return ret; + } + return AddUser(userId, imeInfos); +} + +int32_t ImeEnabledInfoManager::AddUser(int32_t userId, const std::vector &imeInfos) +{ + if (!HasEnabledSwitch()) { + return ErrorCode::NO_ERROR; + } + IMSA_HILOGI("run in, userId:%{public}d", userId); + ImeEnabledCfg cfg; + auto ret = GetEnabledCfg(userId, cfg, true, imeInfos); + if (ret != ErrorCode::NO_ERROR) { + return ret; + } + return SetEnabledCfg(userId, cfg); +} + +int32_t ImeEnabledInfoManager::Delete(int32_t userId) +{ + if (!HasEnabledSwitch()) { + return ErrorCode::NO_ERROR; + } + std::lock_guard lock(imeEnabledCfgLock_); + imeEnabledCfg_.erase(userId); + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::Add(int32_t userId, const FullImeInfo &imeInfo) +{ + if (!HasEnabledSwitch()) { + return ErrorCode::NO_ERROR; + } + IMSA_HILOGI("userId:%{public}d, bundleName:%{public}s", userId, imeInfo.prop.name.c_str()); + if (imeInfo.prop.name == ImeInfoInquirer::GetInstance().GetDefaultIme().bundleName) { + IMSA_HILOGI("[%{public}d,%{public}s] is sys ime, deal in init or user add.", userId, imeInfo.prop.name.c_str()); + return ErrorCode::NO_ERROR; + } + ImeEnabledCfg enabledCfg; + auto ret = GetEnabledCfgFromCacheWithCorrect(userId, enabledCfg); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("user:%{public}d get enabledCfg failed:%{public}d.", userId, ret); + return ret; + } + auto iter = std::find_if(enabledCfg.enabledInfos.begin(), enabledCfg.enabledInfos.end(), + [&imeInfo](const ImeEnabledInfo &info) { return imeInfo.prop.name == info.bundleName; }); + if (iter != enabledCfg.enabledInfos.end()) { + enabledCfg.enabledInfos.erase(iter); + } + ImeEnabledInfo infoTmp; + infoTmp.bundleName = imeInfo.prop.name; + infoTmp.extensionName = imeInfo.prop.id; + infoTmp.enabledStatus = ImeInfoInquirer::GetInstance().GetSystemConfig().initEnabledState; + infoTmp.installTime = imeInfo.installTime; + CorrectBySysEnabledSwitch(infoTmp); + CorrectBySysIme(infoTmp); + enabledCfg.enabledInfos.push_back(infoTmp); + return SetEnabledCfg(userId, enabledCfg); +} + +int32_t ImeEnabledInfoManager::Delete(int32_t userId, const std::string &bundleName) +{ + if (!HasEnabledSwitch()) { + return ErrorCode::NO_ERROR; + } + IMSA_HILOGI("enable Delete run in, userId:%{public}d, bundleName:%{public}s", userId, bundleName.c_str()); + ImeEnabledCfg enabledCfg; + auto ret = GetEnabledCfgFromCacheWithCorrect(userId, enabledCfg); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("user:%{public}d get enabledCfg failed:%{public}d.", userId, ret); + return ret; + } + auto iter = std::find_if(enabledCfg.enabledInfos.begin(), enabledCfg.enabledInfos.end(), + [&bundleName](const ImeEnabledInfo &info) { return info.bundleName == bundleName; }); + if (iter == enabledCfg.enabledInfos.end()) { + return ErrorCode::NO_ERROR; + } + enabledCfg.enabledInfos.erase(iter); + return SetEnabledCfg(userId, enabledCfg); +} + +std::pair ImeEnabledInfoManager::CheckUpdate( + int32_t userId, const std::string &bundleName, const std::string &extensionName, EnabledStatus status) +{ + std::pair checkRet{ ErrorCode::NO_ERROR, false }; // second:isFreshCache + IMSA_HILOGI("update info:[%{public}d, %{public}s, %{public}s, %{public}d].", userId, bundleName.c_str(), + extensionName.c_str(), static_cast(status)); + if (!HasEnabledSwitch()) { + IMSA_HILOGE( + "enabled feature not on, [%{public}d, %{public}s] operation not allow.", userId, bundleName.c_str()); + checkRet.first = ErrorCode::ERROR_ENABLE_IME; + return checkRet; + } + if (bundleName.empty()) { + IMSA_HILOGW("%{public}d bundleName:%{public}s abnormal.", userId, bundleName.c_str()); + checkRet.first = ErrorCode::ERROR_PARAMETER_CHECK_FAILED; + return checkRet; + } + if (ImeInfoInquirer::GetInstance().GetDefaultIme().bundleName == bundleName && status == EnabledStatus::DISABLED) { + IMSA_HILOGW("[%{public}d,%{public}s] is sys ime, do not set DISABLED.", userId, bundleName.c_str()); + checkRet.first = ErrorCode::ERROR_DISABLE_SYS_IME; + return checkRet; + } + ImeEnabledCfg enabledCfg; + auto ret = GetEnabledCfgFromCache(userId, enabledCfg); + if (ret == ErrorCode::NO_ERROR) { + auto iter = std::find_if(enabledCfg.enabledInfos.begin(), enabledCfg.enabledInfos.end(), + [&bundleName](const ImeEnabledInfo &info) { return bundleName == info.bundleName; }); + if (iter != enabledCfg.enabledInfos.end()) { + return checkRet; + } + } + if (!ImeInfoInquirer::GetInstance().IsInputMethod(userId, bundleName)) { + checkRet.first = ErrorCode::ERROR_IME_NOT_FOUND; + return checkRet; + } + checkRet.second = true; + return checkRet; +} + +int32_t ImeEnabledInfoManager::Update( + int32_t userId, const std::string &bundleName, const std::string &extensionName, EnabledStatus status) +{ + auto checkRet = CheckUpdate(userId, bundleName, extensionName, status); + if (checkRet.first != ErrorCode::NO_ERROR) { + return checkRet.first; + } + if (checkRet.second) { + auto ret = AddUser(userId); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("user:%{public}d AddUser failed:%{public}d.", userId, ret); + return ErrorCode::ERROR_ENABLE_IME; + } + } + ImeEnabledCfg enabledCfg; + GetEnabledCfgFromCache(userId, enabledCfg); + auto iter = std::find_if(enabledCfg.enabledInfos.begin(), enabledCfg.enabledInfos.end(), + [&bundleName](const ImeEnabledInfo &info) { return bundleName == info.bundleName; }); + if (iter == enabledCfg.enabledInfos.end()) { + IMSA_HILOGE("not find [%{public}d, %{public}s, %{public}s] in ime install list.", userId, bundleName.c_str(), + extensionName.c_str()); + return ErrorCode::ERROR_IME_NOT_FOUND; + } + auto oldStatus = iter->enabledStatus; + iter->enabledStatus = status; + iter->stateUpdateTime = + std::to_string(duration_cast(system_clock::now().time_since_epoch()).count()); + CorrectBySysEnabledSwitch(*iter); + auto ret = SetEnabledCfg(userId, enabledCfg); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("%{public}d SetEnabledCfg failed.", userId); + return ErrorCode::ERROR_ENABLE_IME; + } + NotifyEnableChange(userId, bundleName, oldStatus); + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::GetEnabledState(int32_t userId, const std::string &bundleName, EnabledStatus &status) +{ + IMSA_HILOGD("[%{public}d, %{public}s] start.", userId, bundleName.c_str()); + if (bundleName.empty()) { + IMSA_HILOGW("%{public}d bundleName is empty.", userId); + return ErrorCode::ERROR_BAD_PARAMETERS; + } + if (!HasEnabledSwitch()) { + status = EnabledStatus::FULL_EXPERIENCE_MODE; + return ErrorCode::NO_ERROR; + } + if (bundleName == ImeInfoInquirer::GetInstance().GetSystemConfig().sysSpecialIme) { + status = EnabledStatus::FULL_EXPERIENCE_MODE; + return ErrorCode::NO_ERROR; + } + auto ret = GetEnabledStateInner(userId, bundleName, status); + if (bundleName == ImeInfoInquirer::GetInstance().GetDefaultIme().bundleName && + (ret != ErrorCode::NO_ERROR || status == EnabledStatus::DISABLED)) { + IMSA_HILOGI("mod sys ime enabledStatus."); + status = EnabledStatus::BASIC_MODE; + return ErrorCode::NO_ERROR; + } + if (ret != ErrorCode::NO_ERROR) { + return ErrorCode::ERROR_ENABLE_IME; + } + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::GetEnabledStates(int32_t userId, std::vector &props) +{ + if (props.empty()) { + return ErrorCode::ERROR_BAD_PARAMETERS; + } + if (!HasEnabledSwitch()) { + for (auto &prop : props) { + prop.status = EnabledStatus::FULL_EXPERIENCE_MODE; + } + return ErrorCode::NO_ERROR; + } + auto ret = GetEnabledStateInner(userId, props); + for (auto &prop : props) { + if (prop.name == ImeInfoInquirer::GetInstance().GetDefaultIme().bundleName && + prop.status == EnabledStatus::DISABLED) { + IMSA_HILOGI("mod sys ime enabledStatus."); + prop.status = EnabledStatus::BASIC_MODE; + break; + } + } + if (ret != ErrorCode::NO_ERROR) { + return ErrorCode::ERROR_ENABLE_IME; + } + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::GetEnabledStateInner(int32_t userId, const std::string &bundleName, EnabledStatus &status) +{ + // get from cache + ImeEnabledCfg cacheCfg; + GetEnabledCfgFromCache(userId, cacheCfg); + auto iter = std::find_if(cacheCfg.enabledInfos.begin(), cacheCfg.enabledInfos.end(), + [&bundleName](const ImeEnabledInfo &info) { return bundleName == info.bundleName; }); + if (iter != cacheCfg.enabledInfos.end()) { + status = iter->enabledStatus; + IMSA_HILOGD("[%{public}d, %{public}s] enabledStatus by cache: %{public}d.", userId, bundleName.c_str(), + static_cast(status)); + return ErrorCode::NO_ERROR; + } + // get directly + ImeEnabledCfg cfg; + auto ret = GetEnabledCfg(userId, cfg, false); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("[%{public}d, %{public}s] GetEnabledCfg failed.", userId, bundleName.c_str()); + return ret; + } + iter = std::find_if(cfg.enabledInfos.begin(), cfg.enabledInfos.end(), + [&bundleName](const ImeEnabledInfo &info) { return info.bundleName == bundleName; }); + if (iter == cfg.enabledInfos.end()) { + IMSA_HILOGE("not find [%{public}d, %{public}s] in enableCfg.", userId, bundleName.c_str()); + return ErrorCode::ERROR_ENABLE_IME; + } + status = iter->enabledStatus; + IMSA_HILOGD("[%{public}d, %{public}s] enabledStatus directly: %{public}d.", userId, bundleName.c_str(), + static_cast(status)); + // post correct task + PostCorrectAddTask(userId); + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::GetEnabledStateInner(int32_t userId, std::vector &props) +{ + // get from cache + ImeEnabledCfg cacheCfg; + auto ret = GetEnabledCfgFromCache(userId, cacheCfg); + if (ret == ErrorCode::NO_ERROR) { + IMSA_HILOGI("%{public}d get from cache start.", userId); + for (auto &prop : props) { + auto iter = std::find_if(cacheCfg.enabledInfos.begin(), cacheCfg.enabledInfos.end(), + [&prop](const ImeEnabledInfo &info) { return prop.name == info.bundleName; }); + if (iter != cacheCfg.enabledInfos.end()) { + prop.status = iter->enabledStatus; + IMSA_HILOGD("get [%{public}d, %{public}s,%{public}d] from cache.", userId, prop.name.c_str(), + static_cast(prop.status)); + continue; + } + } + IMSA_HILOGI("%{public}d get from cache end.", userId); + return ErrorCode::NO_ERROR; + } + IMSA_HILOGI("%{public}d get directly start.", userId); + // get directly + ImeEnabledCfg cfg; + ret = GetEnabledCfg(userId, cfg, false); + if (ret != ErrorCode::NO_ERROR) { + return ret; + } + for (auto &prop : props) { + auto iter = std::find_if(cfg.enabledInfos.begin(), cfg.enabledInfos.end(), + [&prop](const ImeEnabledInfo &info) { return prop.name == info.bundleName; }); + if (iter != cfg.enabledInfos.end()) { + prop.status = iter->enabledStatus; + IMSA_HILOGD("get [%{public}d, %{public}s,%{public}d] directly.", userId, prop.name.c_str(), + static_cast(prop.status)); + } + } + IMSA_HILOGI("%{public}d get directly end.", userId); + // post correct task + PostCorrectAddTask(userId); + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::GetEnabledCfgFromCacheWithCorrect(int32_t userId, ImeEnabledCfg &enabledCfg) +{ + auto ret = GetEnabledCfgFromCache(userId, enabledCfg); + if (ret == ErrorCode::NO_ERROR) { + return ret; + } + ret = CorrectAdd(userId); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("correct %{public}d failed.", userId); + return ret; + } + return GetEnabledCfgFromCache(userId, enabledCfg); +} + +int32_t ImeEnabledInfoManager::GetEnabledCfgFromCache(int32_t userId, ImeEnabledCfg &enabledCfg) +{ + std::lock_guard lock(imeEnabledCfgLock_); + auto it = imeEnabledCfg_.find(userId); + if (it == imeEnabledCfg_.end()) { + IMSA_HILOGE("not find %{public}d in cache.", userId); + return ErrorCode::ERROR_ENABLE_IME; + } + enabledCfg = it->second; + IMSA_HILOGI("num %{public}zu in cache.", enabledCfg.enabledInfos.size()); + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::MergeFullExperienceTableCfg(int32_t userId, ImeEnabledCfg &cfg) +{ + if (!ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + return ErrorCode::NO_ERROR; + } + IMSA_HILOGI("run in: %{public}d.", userId); + std::string content; + int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, SECURITY_MODE, content); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGW("%{public}d get global full experience table failed:%{public}d.", userId, ret); + return ret; + } + std::set bundleNames; + ret = ParseFullExperienceTableCfg(userId, content, bundleNames); + if (ret != ErrorCode::NO_ERROR) { + return ret; + } + for (const auto &bundleName : bundleNames) { + auto iter = std::find_if(cfg.enabledInfos.begin(), cfg.enabledInfos.end(), + [&bundleName](const ImeEnabledInfo &info) { return info.bundleName == bundleName; }); + if (iter != cfg.enabledInfos.end()) { + iter->enabledStatus = EnabledStatus::FULL_EXPERIENCE_MODE; + continue; + } + ImeEnabledInfo tmpInfo; + tmpInfo.bundleName = bundleName; + tmpInfo.enabledStatus = EnabledStatus::FULL_EXPERIENCE_MODE; + cfg.enabledInfos.push_back(tmpInfo); + } + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::GetEnabledCfg( + int32_t userId, ImeEnabledCfg &cfg, bool isCorrectByBmg, const std::vector &imeInfos) +{ + if (!SettingsDataUtils::GetInstance()->IsDataShareReady()) { + IMSA_HILOGE("data share not ready."); + return ErrorCode::ERROR_ENABLE_IME; + } + { + std::lock_guard lock(settingOperateLock_); + auto ret = GetEnabledTableCfg(userId, cfg); + /* ERROR_EX_PARCELABLE is added to prevent the new table can not be write + due to the parsing error of original table */ + /* ERROR_KEYWORD_NOT_FOUND is added to prevent the new table can not be write + due to no original table */ + if (ret != ErrorCode::NO_ERROR && ret != ErrorCode::ERROR_KEYWORD_NOT_FOUND && + ret != ErrorCode::ERROR_EX_PARCELABLE) { + return ret; + } + if (cfg.version.empty()) { + ret = MergeFullExperienceTableCfg(userId, cfg); + if (ret != ErrorCode::NO_ERROR && ret != ErrorCode::ERROR_KEYWORD_NOT_FOUND && + ret != ErrorCode::ERROR_EX_PARCELABLE) { + return ret; + } + } + } + cfg.version = GetDisplayVersion(); + if (isCorrectByBmg) { + auto ret = CorrectByBundleMgr(userId, cfg.enabledInfos, imeInfos); + if (ret != ErrorCode::NO_ERROR) { + return ret; + } + } + CorrectBySysEnabledSwitch(cfg.enabledInfos); + CorrectBySysIme(cfg.enabledInfos); + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::GetEnabledTableCfg(int32_t userId, ImeEnabledCfg &cfg) +{ + std::string userContent; + int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue( + SETTINGS_USER_DATA_URI + std::to_string(userId) + "?Proxy=true", ENABLE_IME, userContent); + if (ret != ErrorCode::NO_ERROR && ret != ErrorCode::ERROR_KEYWORD_NOT_FOUND) { + IMSA_HILOGW("%{public}d get user enabled table failed:%{public}d.", userId, ret); + return ret; + } + bool hasUserEnabledTable = (ret != ErrorCode::ERROR_KEYWORD_NOT_FOUND); + bool isNewUserEnabledTable = (userContent.find("version") != std::string::npos); + if (hasUserEnabledTable && isNewUserEnabledTable) { + return GetNewEnabledTableCfg(userId, userContent, cfg); + } + std::string globalContent; + ret = SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, ENABLE_IME, globalContent); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGW("%{public}d get global enabled table failed:%{public}d.", userId, ret); + return ret; + } + ret = ImeEnabledInfoManager::GetOldEnabledTableCfg(userId, globalContent, cfg); + if (ret == ErrorCode::NO_ERROR || !hasUserEnabledTable) { + return ret; + } + return GetOldEnabledTableCfg(userId, userContent, cfg); +} + +int32_t ImeEnabledInfoManager::GetOldEnabledTableCfg(int32_t userId, const std::string &content, ImeEnabledCfg &cfg) +{ + IMSA_HILOGI("%{public}d run in:%{public}s.", userId, content.c_str()); + EnabledImeCfg oldCfg; + oldCfg.userImeCfg.userId = std::to_string(userId); + if (!oldCfg.Unmarshall(content)) { + IMSA_HILOGE("%{public}d Unmarshall failed:%{public}s.", userId, content.c_str()); + return ErrorCode::ERROR_EX_PARCELABLE; + } + auto bundleNames = std::set(oldCfg.userImeCfg.identities.begin(), oldCfg.userImeCfg.identities.end()); + for (const auto &bundleName : bundleNames) { + ImeEnabledInfo tmpInfo; + tmpInfo.bundleName = bundleName; + tmpInfo.enabledStatus = EnabledStatus::BASIC_MODE; + cfg.enabledInfos.push_back(tmpInfo); + } + return ErrorCode::NOERROR; +} + +int32_t ImeEnabledInfoManager::GetNewEnabledTableCfg(int32_t userId, const std::string &content, ImeEnabledCfg &cfg) +{ + IMSA_HILOGI("%{public}d run in.", userId); + if (!cfg.Unmarshall(content)) { + IMSA_HILOGE("%{public}d Unmarshall failed:%{public}s.", userId, content.c_str()); + return ErrorCode::ERROR_EX_PARCELABLE; + } + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::CorrectByBundleMgr( + int32_t userId, std::vector &enabledInfos, const std::vector &imeInfos) +{ + IMSA_HILOGI("ime size, enabled:%{public}zu, installed:%{public}zu.", enabledInfos.size(), imeInfos.size()); + auto imeTmpInfos = imeInfos; + if (imeTmpInfos.empty()) { + auto ret = ImeInfoInquirer::GetInstance().QueryFullImeInfo(userId, imeTmpInfos); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("%{public}d QueryFullImeInfo failed.", userId); + return ret; + } + } + auto start = + std::remove_if(enabledInfos.begin(), enabledInfos.end(), [&imeTmpInfos](const ImeEnabledInfo &enabledInfoTmp) { + auto iter = + std::find_if(imeTmpInfos.begin(), imeTmpInfos.end(), [&enabledInfoTmp](const FullImeInfo &imeInfoTmp) { + return enabledInfoTmp.bundleName == imeInfoTmp.prop.name; + }); + return iter == imeTmpInfos.end(); + }); + enabledInfos.erase(start, enabledInfos.end()); + for (const auto &info : imeTmpInfos) { + auto iter = std::find_if(enabledInfos.begin(), enabledInfos.end(), + [&info](const ImeEnabledInfo &enabledInfoTmp) { return enabledInfoTmp.bundleName == info.prop.name; }); + if (iter != enabledInfos.end()) { + // maybe uninstall and install when imsa abnormal + if (!iter->installTime.empty() && info.installTime != iter->installTime) { + iter->enabledStatus = ImeInfoInquirer::GetInstance().GetSystemConfig().initEnabledState; + } + iter->installTime = info.installTime; + iter->extensionName = info.prop.id; + continue; + } + IMSA_HILOGW("%{public}s add.", info.prop.name.c_str()); + // maybe install when imsa abnormal or upgrade(install but disabled) or table abnormal + ImeEnabledInfo infoTmp; + infoTmp.bundleName = info.prop.name; + infoTmp.extensionName = info.prop.id; + infoTmp.enabledStatus = ImeInfoInquirer::GetInstance().GetSystemConfig().initEnabledState; + infoTmp.installTime = info.installTime; + enabledInfos.push_back(infoTmp); + } + return ErrorCode::NO_ERROR; +} + +void ImeEnabledInfoManager::CorrectBySysEnabledSwitch(std::vector &infos) +{ + for (auto &info : infos) { + CorrectBySysEnabledSwitch(info); + } +} + +void ImeEnabledInfoManager::CorrectBySysEnabledSwitch(ImeEnabledInfo &info) +{ + auto hasEnableSwitch = ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature; + auto hasFullExperienceSwitch = ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature; + IMSA_HILOGI("enable cfg:[%{public}d, %{public}d].", hasEnableSwitch, hasFullExperienceSwitch); + if (!hasEnableSwitch && !hasFullExperienceSwitch) { + info.enabledStatus = EnabledStatus::FULL_EXPERIENCE_MODE; + } + if (hasEnableSwitch && !hasFullExperienceSwitch) { + if (info.enabledStatus == EnabledStatus::BASIC_MODE) { + info.enabledStatus = EnabledStatus::FULL_EXPERIENCE_MODE; + } + } + if (!hasEnableSwitch && hasFullExperienceSwitch) { + if (info.enabledStatus == EnabledStatus::DISABLED) { + info.enabledStatus = EnabledStatus::BASIC_MODE; + } + } +} + +void ImeEnabledInfoManager::CorrectBySysIme(std::vector &infos) +{ + for (auto &info : infos) { + CorrectBySysIme(info); + } +} + +void ImeEnabledInfoManager::CorrectBySysIme(ImeEnabledInfo &info) +{ + auto sysIme = ImeInfoInquirer::GetInstance().GetDefaultIme(); + if (info.bundleName != sysIme.bundleName || info.enabledStatus != EnabledStatus::DISABLED) { + return; + } + info.enabledStatus = EnabledStatus::BASIC_MODE; +} + +int32_t ImeEnabledInfoManager::ParseFullExperienceTableCfg( + int32_t userId, const std::string &content, std::set &bundleNames) +{ + SecurityModeCfg cfg; + cfg.userImeCfg.userId = std::to_string(userId); + if (!cfg.Unmarshall(content)) { + IMSA_HILOGE("%{public}d Unmarshall failed:%{public}s.", userId, content.c_str()); + return ErrorCode::ERROR_EX_PARCELABLE; + } + bundleNames = std::set(cfg.userImeCfg.identities.begin(), cfg.userImeCfg.identities.end()); + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::SetEnabledCfg(int32_t userId, const ImeEnabledCfg &cfg) +{ + std::lock_guard lock(settingOperateLock_); + std::string content; + if (!cfg.Marshall(content)) { + IMSA_HILOGE("Marshall failed"); + return ErrorCode::ERROR_ENABLE_IME; + } + IMSA_HILOGI("[%{public}d, %{public}s].", userId, content.c_str()); + if (!SettingsDataUtils::GetInstance()->SetStringValue( + SETTINGS_USER_DATA_URI + std::to_string(userId) + "?Proxy=true", ENABLE_IME, content)) { + return ErrorCode::ERROR_ENABLE_IME; + } + { + std::lock_guard cgfLock(imeEnabledCfgLock_); + imeEnabledCfg_.insert_or_assign(userId, cfg); + } + ModGlobalEnabledTable(userId, cfg); + return ErrorCode::NO_ERROR; +} + +void ImeEnabledInfoManager::PostCorrectAddTask(int32_t userId) +{ + auto task = [this, userId]() { CorrectAdd(userId); }; + if (serviceHandler_ == nullptr) { + return; + } + // 60000: ms, the task is time-consuming, prevent it is performed during the device boot + serviceHandler_->PostTask(task, "correctAdd", 60000, AppExecFwk::EventQueue::Priority::IMMEDIATE); +} + +void ImeEnabledInfoManager::NotifyEnableChange(int32_t userId, const std::string &bundleName, EnabledStatus oldStatus) +{ + IMSA_HILOGI( + "notify:[%{public}d,%{public}s,%{public}d].", userId, bundleName.c_str(), static_cast(oldStatus)); + auto notifyTask = [this, userId, bundleName, oldStatus]() { + if (enableChangedHandler_ != nullptr) { + enableChangedHandler_(userId, bundleName, oldStatus); + } + }; + if (serviceHandler_ == nullptr) { + return; + } + serviceHandler_->PostTask(notifyTask, "NotifyEnableChangeTask", 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); +} + +bool ImeEnabledInfoManager::HasEnabledSwitch() +{ + return ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + || ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature; +} + +bool ImeEnabledInfoManager::IsDefaultFullMode(int32_t userId, const std::string &bundleName) +{ + auto defaultIme = ImeInfoInquirer::GetInstance().GetDefaultImeCfgProp(); + if (defaultIme != nullptr && bundleName == defaultIme->name) { + return true; + } + std::string appId; + uint32_t versionCode; + if (!ImeInfoInquirer::GetInstance().GetImeAppId(userId, bundleName, appId) + || !ImeInfoInquirer::GetInstance().GetImeVersionCode(userId, bundleName, versionCode)) { + IMSA_HILOGE("%{public}s failed to get appId and versionCode", bundleName.c_str()); + return false; + } + std::vector defaultFullImeList; + if (!SysCfgParser::ParseDefaultFullIme(defaultFullImeList)) { + IMSA_HILOGE("failed to parse config"); + return false; + } + auto ime = std::find_if(defaultFullImeList.begin(), defaultFullImeList.end(), + [&appId](const auto &ime) { return ime.appId == appId; }); + if (ime == defaultFullImeList.end()) { + IMSA_HILOGD("not default FULL"); + return false; + } + bool isDefaultFull = false; + if (ime->expirationVersionCode > 0) { + isDefaultFull = !IsExpired(ime->expirationTime) || versionCode < ime->expirationVersionCode; + } else { + isDefaultFull = !IsExpired(ime->expirationTime); + } + IMSA_HILOGI("ime: %{public}s, isDefaultFull: %{public}d", bundleName.c_str(), isDefaultFull); + return isDefaultFull; +} + +bool ImeEnabledInfoManager::IsExpired(const std::string &expirationTime) +{ + std::istringstream expirationTimeStr(expirationTime); + std::tm expTime = {}; + expirationTimeStr >> std::get_time(&expTime, "%Y-%m-%d %H:%M:%S"); + if (expirationTimeStr.fail()) { + IMSA_HILOGE("get time error, expirationTime: %{public}s", expirationTime.c_str()); + return false; + } + auto expTimePoint = std::chrono::system_clock::from_time_t(std::mktime(&expTime)); + auto now = std::chrono::system_clock::now(); + return expTimePoint < now; +} + +void ImeEnabledInfoManager::OnFullExperienceTableChanged(int32_t userId) +{ + IMSA_HILOGD("start."); + auto defaultImeBundleName = ImeInfoInquirer::GetInstance().GetDefaultIme().bundleName; + if (defaultImeBundleName.empty()) { + return; + } + EnabledStatus oldStatus; + ImeEnabledInfoManager::GetInstance().GetEnabledState(userId, defaultImeBundleName, oldStatus); + std::string content; + int32_t ret = SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, SECURITY_MODE, content); + if (ret != ErrorCode::NO_ERROR && ret != ErrorCode::ERROR_KEYWORD_NOT_FOUND) { + IMSA_HILOGE("read full experience table failed: %{public}d", ret); + return; + } + EnabledStatus newStatus = EnabledStatus::BASIC_MODE; + if (ret == ErrorCode::NO_ERROR) { + std::set bundleNames; + ParseFullExperienceTableCfg(userId, content, bundleNames); + auto it = std::find_if(bundleNames.begin(), bundleNames.end(), + [&defaultImeBundleName](const std::string &bundleName) { return bundleName == defaultImeBundleName; }); + if (it != bundleNames.end()) { + newStatus = EnabledStatus::FULL_EXPERIENCE_MODE; + } + } + if (newStatus == oldStatus) { + return; + } + Update(userId, defaultImeBundleName, ImeInfoInquirer::GetInstance().GetDefaultIme().extName, newStatus); + IMSA_HILOGD("end."); +} + +void ImeEnabledInfoManager::ModGlobalEnabledTable(int32_t userId, const ImeEnabledCfg &newEnabledCfg) +{ + IMSA_HILOGD("start."); + if (userId != currentUserId_) { + IMSA_HILOGW("[%{public}d,%{public}d] not same.", userId, currentUserId_); + return; + } + std::string oldGlobalContent; + auto ret = SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, ENABLE_IME, oldGlobalContent); + if (ret != ErrorCode::NO_ERROR && ret != ErrorCode::ERROR_KEYWORD_NOT_FOUND) { + IMSA_HILOGW("%{public}d get global enabled table failed:%{public}d.", userId, ret); + return; + } + IMSA_HILOGI("global str:%{public}s.", oldGlobalContent); + EnabledImeCfg cfg; + for (const auto &info : newEnabledCfg.enabledInfos) { + if (info.enabledStatus != EnabledStatus::DISABLED) { + cfg.userImeCfg.identities.push_back(info.bundleName); + } + } + std::string newGlobalContent; + cfg.userImeCfg.userId = std::to_string(userId); + if (!cfg.Marshall(newGlobalContent)) { + IMSA_HILOGE("%{public}d Unmarshall failed:%{public}s.", userId, newGlobalContent.c_str()); + return; + } + if (newGlobalContent == oldGlobalContent) { + IMSA_HILOGD("[%{public}s,%{public}s] same, not deal.", newGlobalContent.c_str(), oldGlobalContent.c_str()); + return; + } + if (ret == ErrorCode::ERROR_KEYWORD_NOT_FOUND) { + SettingsDataUtils::GetInstance()->SetStringValue(SETTING_URI_PROXY, ENABLE_IME, newGlobalContent); + return; + } + auto globalTableUserId = GetGlobalTableUserId(oldGlobalContent); + if (globalTableUserId.empty() || userId == atoi(globalTableUserId.c_str())) { + SettingsDataUtils::GetInstance()->SetStringValue(SETTING_URI_PROXY, ENABLE_IME, newGlobalContent); + return; + } + std::string oldGlobalUserContent; + ret = SettingsDataUtils::GetInstance()->GetStringValue( + SETTINGS_USER_DATA_URI + globalTableUserId + "?Proxy=true", ENABLE_IME, oldGlobalUserContent); + if (ret != ErrorCode::NO_ERROR && ret != ErrorCode::ERROR_KEYWORD_NOT_FOUND) { + IMSA_HILOGW("%{public}d get user enabled table failed:%{public}d.", userId, ret); + return; + } + if (ret == ErrorCode::ERROR_KEYWORD_NOT_FOUND || oldGlobalUserContent.find("version") == std::string::npos) { + if (SettingsDataUtils::GetInstance()->SetStringValue( + SETTINGS_USER_DATA_URI + globalTableUserId + "?Proxy=true", ENABLE_IME, oldGlobalContent)) { + SettingsDataUtils::GetInstance()->SetStringValue(SETTING_URI_PROXY, ENABLE_IME, newGlobalContent); + } + return; + } + SettingsDataUtils::GetInstance()->SetStringValue(SETTING_URI_PROXY, ENABLE_IME, newGlobalContent); + IMSA_HILOGD("end."); +} + +std::string ImeEnabledInfoManager::GetGlobalTableUserId(const std::string &valueStr) +{ + auto root = cJSON_Parse(valueStr.c_str()); + if (root == nullptr) { + IMSA_HILOGE("valueStr content parse failed!"); + return ""; + } + auto subNode = Serializable::GetSubNode(root, "enableImeList"); + if (subNode == nullptr || !cJSON_IsObject(subNode)) { + IMSA_HILOGW("subNode is null or not object"); + cJSON_Delete(root); + return ""; + } + if (subNode->child == nullptr) { + IMSA_HILOGW("subNode has not child"); + cJSON_Delete(root); + return ""; + } + std::string userId = subNode->child->string; + cJSON_Delete(root); + return userId; +} +} // namespace MiscServices +} // namespace OHOS diff --git a/services/adapter/settings_data_provider/src/security_mode_parser.cpp b/services/adapter/settings_data_provider/src/security_mode_parser.cpp deleted file mode 100644 index 686b25d87891fd934e0d97b7bc52e3c04b8b6d95..0000000000000000000000000000000000000000 --- a/services/adapter/settings_data_provider/src/security_mode_parser.cpp +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "security_mode_parser.h" - -#include -#include -#include - -#include "full_ime_info_manager.h" -#include "ime_info_inquirer.h" -#include "sys_cfg_parser.h" -namespace OHOS { -namespace MiscServices { -std::mutex SecurityModeParser::instanceMutex_; -sptr SecurityModeParser::instance_ = nullptr; -constexpr const char *SYSTEM_SPECIAL_IME = ""; -SecurityModeParser::~SecurityModeParser() -{ -} - -sptr SecurityModeParser::GetInstance() -{ - if (instance_ == nullptr) { - std::lock_guard autoLock(instanceMutex_); - if (instance_ == nullptr) { - IMSA_HILOGI("need to create SecurityModeParser."); - instance_ = new (std::nothrow) SecurityModeParser(); - if (instance_ == nullptr) { - IMSA_HILOGE("instance is nullptr."); - return instance_; - } - } - } - return instance_; -} - -int32_t SecurityModeParser::Initialize(const int32_t userId) -{ - return UpdateFullModeList(userId); -} - -int32_t SecurityModeParser::UpdateFullModeList(int32_t userId) -{ - IMSA_HILOGD("key: %{public}s.", SECURITY_MODE); - std::string 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; - } - - if (!ParseSecurityMode(valueStr, userId)) { - IMSA_HILOGE("parse %{public}s failed by %{public}d", valueStr.c_str(), userId); - return ErrorCode::ERROR_ENABLE_SECURITY_MODE; - } - return ErrorCode::NO_ERROR; -} - -bool SecurityModeParser::ParseSecurityMode(const std::string &valueStr, const int32_t userId) -{ - SecModeCfg secModeCfg; - secModeCfg.userImeCfg.userId = std::to_string(userId); - auto ret = secModeCfg.Unmarshall(valueStr); - if (!ret) { - IMSA_HILOGE("unmarshall failed!"); - return ret; - } - std::lock_guard autoLock(listMutex_); - fullModeList_ = secModeCfg.userImeCfg.identities; - return true; -} - -SecurityMode SecurityModeParser::GetSecurityMode(const std::string &bundleName, int32_t userId) -{ - if (bundleName == SYSTEM_SPECIAL_IME) { - return SecurityMode::FULL; - } - if (!initialized_) { - std::lock_guard lock(initLock_); - if (!initialized_) { - UpdateFullModeList(userId); - initialized_ = true; - } - } - if (IsFullMode(bundleName)) { - return SecurityMode::FULL; - } else { - return SecurityMode::BASIC; - } -} - -bool SecurityModeParser::IsFullMode(std::string bundleName) -{ - std::lock_guard autoLock(listMutex_); - auto it = std::find_if(fullModeList_.begin(), fullModeList_.end(), - [&bundleName](const std::string &bundle) { return bundle == bundleName; }); - return it != fullModeList_.end(); -} - -bool SecurityModeParser::IsDefaultFullMode(const std::string &bundleName, int32_t userId) -{ - auto defaultIme = ImeInfoInquirer::GetInstance().GetDefaultImeCfgProp(); - if (defaultIme != nullptr && bundleName == defaultIme->name) { - return true; - } - std::string appId; - uint32_t versionCode; - if (!ImeInfoInquirer::GetInstance().GetImeAppId(userId, bundleName, appId) - || !ImeInfoInquirer::GetInstance().GetImeVersionCode(userId, bundleName, versionCode)) { - IMSA_HILOGE("%{public}s failed to get appId and versionCode", bundleName.c_str()); - return false; - } - std::vector defaultFullImeList; - if (!SysCfgParser::ParseDefaultFullIme(defaultFullImeList)) { - IMSA_HILOGE("failed to parse config"); - return false; - } - auto ime = std::find_if(defaultFullImeList.begin(), defaultFullImeList.end(), - [&appId](const auto &ime) { return ime.appId == appId; }); - if (ime == defaultFullImeList.end()) { - IMSA_HILOGD("not default FULL"); - return false; - } - bool isDefaultFull = false; - if (ime->expirationVersionCode > 0) { - isDefaultFull = !IsExpired(ime->expirationTime) || versionCode < ime->expirationVersionCode; - } else { - isDefaultFull = !IsExpired(ime->expirationTime); - } - IMSA_HILOGI("ime: %{public}s, isDefaultFull: %{public}d", bundleName.c_str(), isDefaultFull); - return isDefaultFull; -} - -bool SecurityModeParser::IsSecurityMode(int32_t userId, const std::string &bundleName) -{ - SecurityMode mode = GetSecurityMode(bundleName, userId); - return mode == SecurityMode::FULL; -} - -bool SecurityModeParser::IsExpired(const std::string &expirationTime) -{ - std::istringstream expirationTimeStr(expirationTime); - std::tm expTime = {}; - expirationTimeStr >> std::get_time(&expTime, "%Y-%m-%d %H:%M:%S"); - if (expirationTimeStr.fail()) { - IMSA_HILOGE("get time error, expirationTime: %{public}s", expirationTime.c_str()); - return false; - } - auto expTimePoint = std::chrono::system_clock::from_time_t(std::mktime(&expTime)); - auto now = std::chrono::system_clock::now(); - return expTimePoint < now; -} -} // namespace MiscServices -} // namespace OHOS \ No newline at end of file diff --git a/services/include/full_ime_info_manager.h b/services/include/full_ime_info_manager.h index 98de108d039955c3b50851db32d2a7de28bf3705..e101f7ea3ea7a58aad81733a0ab517ffb074b6a6 100644 --- a/services/include/full_ime_info_manager.h +++ b/services/include/full_ime_info_manager.h @@ -16,6 +16,7 @@ #ifndef SERVICES_INCLUDE_FULL_IME_INFO_MANAGER_H #define SERVICES_INCLUDE_FULL_IME_INFO_MANAGER_H +#include "event_handler.h" #include "input_method_property.h" #include "timer.h" namespace OHOS { @@ -24,25 +25,31 @@ namespace MiscServices { class FullImeInfoManager { public: static FullImeInfoManager &GetInstance(); - int32_t Init(); // osAccount start/bundle scan finished/regular update - int32_t Add(int32_t userId); // user switched - int32_t Update(); // language change - int32_t Delete(int32_t userId); // user removed - int32_t Add(int32_t userId, const std::string &bundleName); // package added + int32_t Init(); // regular Init/boot complete/data share ready + int32_t Add(int32_t userId); // user switched + int32_t Update(); // language change + int32_t Delete(int32_t userId); // user removed + int32_t Add(int32_t userId, const std::string &bundleName); // package added int32_t Delete(int32_t userId, const std::string &bundleName); // package removed int32_t Update(int32_t userId, const std::string &bundleName); // package changed - std::vector Get(int32_t userId); std::string Get(int32_t userId, uint32_t tokenId); - bool Get(const std::string &bundleName, int32_t userId, FullImeInfo &fullImeInfo); + bool Get(int32_t userId, const std::string &bundleName, FullImeInfo &fullImeInfo); bool Has(int32_t userId, const std::string &bundleName); + int32_t Get(int32_t userId, std::vector &props); + void SetEventHandler(const std::shared_ptr &eventHandler); private: FullImeInfoManager(); ~FullImeInfoManager(); + int32_t AddUser(int32_t userId, std::vector &infos); + int32_t AddPackage(int32_t userId, const std::string &bundleName, FullImeInfo &info); + int32_t DeletePackage(int32_t userId, const std::string &bundleName); + void HandleEnableTask(const std::function &task, const std::string &taskName); std::mutex lock_; std::map> fullImeInfos_; Utils::Timer timer_{ "imeInfoCacheInitTimer" }; uint32_t timerId_{ 0 }; + std::shared_ptr serviceHandler_{ nullptr }; }; } // namespace MiscServices } // namespace OHOS diff --git a/services/include/ime_info_inquirer.h b/services/include/ime_info_inquirer.h index 03e05141b83ab418c92eb486bde04b3aa5a26761..5a49740237659cac11c33518b35806e2c4712ff3 100644 --- a/services/include/ime_info_inquirer.h +++ b/services/include/ime_info_inquirer.h @@ -17,9 +17,9 @@ #define SERVICES_INCLUDE_IME_INFO_ENQUIRER_H #include "bundle_mgr_proxy.h" -#include "enable_ime_data_parser.h" #include "ime_cfg_manager.h" #include "input_method_info.h" +#include "input_method_property.h" #include "resource_manager.h" #include "sys_cfg_parser.h" namespace OHOS { @@ -81,17 +81,16 @@ public: bool GetImeVersionCode(int32_t userId, const std::string &bundleName, uint32_t &versionCode); int32_t GetDefaultInputMethod(const int32_t userId, std::shared_ptr &prop, bool isBrief = false); int32_t GetInputMethodConfig(const int32_t userId, AppExecFwk::ElementName &inputMethodConfig); - int32_t ListInputMethod(int32_t userId, InputMethodStatus status, std::vector &props, bool enableOn); + int32_t ListInputMethod(int32_t userId, InputMethodStatus status, std::vector &props); int32_t ListInputMethodSubtype(int32_t userId, const std::string &bundleName, std::vector &subProps); int32_t ListCurrentInputMethodSubtype(int32_t userId, std::vector &subProps); - int32_t GetSwitchInfoBySwitchCount(SwitchInfo &switchInfo, int32_t userId, bool enableOn, uint32_t cacheCount); - bool IsEnableInputMethod(); - bool IsEnableSecurityMode(); + int32_t GetSwitchInfoBySwitchCount(SwitchInfo &switchInfo, int32_t userId, uint32_t cacheCount); bool IsEnableAppAgent(); bool IsVirtualProxyIme(int32_t callingUid); bool IsSpecialSaUid(int32_t callingUid); EnabledStatus GetSystemInitEnabledState(); void InitSystemConfig(); + SystemConfig GetSystemConfig(); ImeNativeCfg GetDefaultIme(); int32_t QueryFullImeInfo(std::vector>> &imeInfos); int32_t QueryFullImeInfo(int32_t userId, std::vector &imeInfos); @@ -123,8 +122,9 @@ private: bool IsNewExtInfos(const std::vector &extInfos); std::vector ListInputMethodInfo(const int32_t userId); int32_t ListInputMethod(const int32_t userId, std::vector &props); - int32_t ListEnabledInputMethod(const int32_t userId, std::vector &props, bool enableOn); - int32_t ListDisabledInputMethod(const int32_t userId, std::vector &props, bool enableOn); + int32_t ListEnabledInputMethod(const int32_t userId, std::vector &props); + int32_t ListDisabledInputMethod(const int32_t userId, std::vector &props); + int32_t ListAllInputMethod(const int32_t userId, std::vector &props); int32_t ListInputMethodSubtype(const int32_t userId, const std::vector &extInfos, std::vector &subProps); int32_t ListInputMethodSubtype(const int32_t userId, const OHOS::AppExecFwk::ExtensionAbilityInfo &extInfo, diff --git a/services/include/input_method_system_ability.h b/services/include/input_method_system_ability.h index b50213cb386670d9488ed896966e13e5165d95c9..daedb8b8ae94dd3ef18a86a2f96864baa09fa533 100644 --- a/services/include/input_method_system_ability.h +++ b/services/include/input_method_system_ability.h @@ -21,7 +21,6 @@ #include "input_method_system_ability_stub.h" #include "inputmethod_dump.h" #include "inputmethod_trace.h" -#include "security_mode_parser.h" #include "system_ability.h" #include "input_method_types.h" #include "user_session_manager.h" @@ -85,7 +84,7 @@ public: void DumpAllMethod(int fd); ErrCode IsDefaultIme() override; ErrCode IsDefaultImeSet(bool& resultValue) override; - ErrCode EnableIme(const std::string &bundleName, bool& resultValue) override; + ErrCode EnableIme(const std::string &bundleName, const std::string &extensionName, int32_t status) override; ErrCode GetInputMethodState(int32_t &status) override; ErrCode IsSystemApp(bool& resultValue) override; int32_t RegisterProxyIme( @@ -166,13 +165,12 @@ private: int32_t SwitchLanguage(); int32_t SwitchType(); int32_t GenerateClientInfo(int32_t userId, InputClientInfo &clientInfo); - void RegisterEnableImeObserver(); void RegisterSecurityModeObserver(); int32_t CheckInputTypeOption(int32_t userId, InputClientInfo &inputClientInfo); int32_t IsDefaultImeFromTokenId(int32_t userId, uint32_t tokenId); void DealSwitchRequest(); - void DealSecurityChange(); - void OnSecurityModeChange(); + void DealSecurityChange(int32_t userId, const std::string &bundleName, EnabledStatus oldStatus); + void OnSecurityModeChange(int32_t userId, const std::string &bundleName, EnabledStatus oldStatus); bool IsCurrentIme(int32_t userId); int32_t StartInputType(int32_t userId, InputType type); // if switch input type need to switch ime, then no need to hide panel first. @@ -180,7 +178,6 @@ private: bool GetDeviceFunctionKeyState(int32_t functionKey, bool &isEnable); bool ModifyImeCfgWithWrongCaps(); void HandleBundleScanFinished(); - int32_t GetInputMethodState(int32_t userId, const std::string &bundleName, EnabledStatus &status); int32_t StartInputInner( InputClientInfo &inputClientInfo, sptr &agent, std::pair &imeInfo); int32_t ShowInputInner(sptr client, int32_t requestKeyboardReason = 0); @@ -194,13 +191,11 @@ private: bool IsImeInUse(); #endif std::mutex checkMutex_; + void OnImeEnabledStatusChange(int32_t userId, const std::string &bundleName, EnabledStatus oldStatus); void DatashareCallback(const std::string &key); bool IsValidBundleName(const std::string &bundleName); std::string GetRestoreBundleName(MessageParcel &data); int32_t RestoreInputmethod(std::string &bundleName); - - std::atomic enableImeOn_ = false; - std::atomic enableSecurityMode_ = false; std::atomic isBundleScanFinished_ = false; std::atomic isScbEnable_ = false; std::mutex switchImeMutex_; diff --git a/services/include/peruser_session.h b/services/include/peruser_session.h index 6056727e6c5a9d7578208c4f5357730ba47c0266..f74ca575fa662f58ab958fd4d8c19e932e9b7882 100644 --- a/services/include/peruser_session.h +++ b/services/include/peruser_session.h @@ -20,7 +20,6 @@ #include "block_queue.h" #include "client_group.h" -#include "enable_ime_data_parser.h" #include "event_status_manager.h" #include "freeze_manager.h" #include "iinput_method_core.h" diff --git a/services/include/sys_cfg_parser.h b/services/include/sys_cfg_parser.h index b9a5110b84c4dcc612504b8b5eed6a8eb9ab9abc..e8b76ba026906a3163f8e4b9ee0475bea9f0b358 100644 --- a/services/include/sys_cfg_parser.h +++ b/services/include/sys_cfg_parser.h @@ -30,6 +30,7 @@ struct SystemConfig : public Serializable { bool enableAppAgentFeature = false; std::unordered_set proxyImeUidList; std::unordered_set specialSaUidList; + std::string sysSpecialIme; bool Unmarshal(cJSON *node) override { GetValue(node, GET_NAME(systemInputMethodConfigAbility), systemInputMethodConfigAbility); @@ -42,6 +43,7 @@ struct SystemConfig : public Serializable { GetValue(node, GET_NAME(enableAppAgentFeature), enableAppAgentFeature); GetValue(node, GET_NAME(proxyImeUidList), proxyImeUidList); GetValue(node, GET_NAME(specialSaUidList), specialSaUidList); + GetValue(node, GET_NAME(sysSpecialIme), sysSpecialIme); return true; } }; diff --git a/services/src/full_ime_info_manager.cpp b/services/src/full_ime_info_manager.cpp index 5d8ff721d429d8a6b6325d73ded5b4a444aca9e3..5d935d65edd22c31b2c368745e2c42eec07c1651 100644 --- a/services/src/full_ime_info_manager.cpp +++ b/services/src/full_ime_info_manager.cpp @@ -14,8 +14,12 @@ */ #include "full_ime_info_manager.h" + #include "common_timer_errors.h" +#include "ime_enabled_info_manager.h" #include "ime_info_inquirer.h" +#include "inputmethod_message_handler.h" +#include "message.h" namespace OHOS { namespace MiscServices { constexpr uint32_t TIMER_TASK_INTERNAL = 3600000; // updated hourly @@ -33,7 +37,14 @@ FullImeInfoManager::FullImeInfoManager() IMSA_HILOGE("failed to create timer!"); return; } - timerId_ = timer_.Register([this]() { Init(); }, TIMER_TASK_INTERNAL, false); + timerId_ = timer_.Register( + []() { + Message *msg = new (std::nothrow) Message(MessageID::MSG_ID_REGULAR_UPDATE_IME_INFO, nullptr); + if (msg != nullptr) { + MessageHandler::Instance()->SendMessage(msg); + } + }, + TIMER_TASK_INTERNAL, false); } FullImeInfoManager &FullImeInfoManager::GetInstance() @@ -44,30 +55,77 @@ FullImeInfoManager &FullImeInfoManager::GetInstance() int32_t FullImeInfoManager::Init() { + IMSA_HILOGI("run in."); std::vector>> fullImeInfos; auto ret = ImeInfoInquirer::GetInstance().QueryFullImeInfo(fullImeInfos); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGW("failed to QueryFullImeInfo, ret:%{public}d", ret); return ret; } - std::lock_guard lock(lock_); - fullImeInfos_.clear(); - for (const auto &infos : fullImeInfos) { - fullImeInfos_.insert_or_assign(infos.first, infos.second); + std::map> fullImeInfosParam; + { + std::lock_guard lock(lock_); + fullImeInfos_.clear(); + for (const auto &infos : fullImeInfos) { + fullImeInfos_.insert_or_assign(infos.first, infos.second); + } + fullImeInfosParam = fullImeInfos_; } + auto task = [fullImeInfosParam]() { ImeEnabledInfoManager::GetInstance().Init(fullImeInfosParam); }; + HandleEnableTask(task, "enableRegularInit"); return ErrorCode::NO_ERROR; } int32_t FullImeInfoManager::Add(int32_t userId) +{ + std::vector infos; + auto ret = AddUser(userId, infos); + auto task = [userId, infos]() { ImeEnabledInfoManager::GetInstance().Add(userId, infos); }; + HandleEnableTask(task, "enableUserAdd"); + return ret; +} + +int32_t FullImeInfoManager::Delete(int32_t userId) +{ + { + std::lock_guard lock(lock_); + fullImeInfos_.erase(userId); + } + auto task = [userId]() { ImeEnabledInfoManager::GetInstance().Delete(userId); }; + HandleEnableTask(task, "enableUserDelete"); + return ErrorCode::NO_ERROR; +} + +int32_t FullImeInfoManager::Add(int32_t userId, const std::string &bundleName) +{ + FullImeInfo info; + auto ret = AddPackage(userId, bundleName, info); + if (ret != ErrorCode::NO_ERROR) { + return ret; + } + auto task = [userId, info]() { ImeEnabledInfoManager::GetInstance().Add(userId, info); }; + HandleEnableTask(task, "enableBundleAdd"); + return ErrorCode::NO_ERROR; +} + +int32_t FullImeInfoManager::Delete(int32_t userId, const std::string &bundleName) +{ + auto ret = DeletePackage(userId, bundleName); + auto task = [userId, bundleName]() { ImeEnabledInfoManager::GetInstance().Delete(userId, bundleName); }; + HandleEnableTask(task, "enableBundleDelete"); + return ret; +} + +int32_t FullImeInfoManager::AddUser(int32_t userId, std::vector &infos) { { std::lock_guard lock(lock_); auto it = fullImeInfos_.find(userId); if (it != fullImeInfos_.end()) { + infos = it->second; return ErrorCode::NO_ERROR; } } - std::vector infos; auto ret = ImeInfoInquirer::GetInstance().QueryFullImeInfo(userId, infos); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("failed to QueryFullImeInfo, userId:%{public}d, ret:%{public}d", userId, ret); @@ -80,27 +138,50 @@ int32_t FullImeInfoManager::Add(int32_t userId) int32_t FullImeInfoManager::Update() { - auto ret = Init(); - if (ret != ErrorCode::NO_ERROR) { + { std::lock_guard lock(lock_); fullImeInfos_.clear(); } + IMSA_HILOGI("run in."); + std::vector>> fullImeInfos; + auto ret = ImeInfoInquirer::GetInstance().QueryFullImeInfo(fullImeInfos); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGW("failed to QueryFullImeInfo, ret:%{public}d", ret); + return ret; + } + { + std::lock_guard lock(lock_); + for (const auto &infos : fullImeInfos) { + fullImeInfos_.insert_or_assign(infos.first, infos.second); + } + } return ErrorCode::NO_ERROR; } -int32_t FullImeInfoManager::Delete(int32_t userId) +int32_t FullImeInfoManager::AddPackage(int32_t userId, const std::string &bundleName, FullImeInfo &info) { + auto ret = ImeInfoInquirer::GetInstance().GetFullImeInfo(userId, bundleName, info); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("failed to GetFullImeInfo failed, userId:%{public}d, bundleName:%{public}s, ret:%{public}d", + userId, bundleName.c_str(), ret); + return ErrorCode::ERROR_PACKAGE_MANAGER; + } std::lock_guard lock(lock_); - fullImeInfos_.erase(userId); + auto it = fullImeInfos_.find(userId); + if (it == fullImeInfos_.end()) { + fullImeInfos_.insert({ userId, { info } }); + return ErrorCode::NO_ERROR; + } + auto iter = std::find_if(it->second.begin(), it->second.end(), + [&bundleName](const FullImeInfo &info) { return bundleName == info.prop.name; }); + if (iter != it->second.end()) { + it->second.erase(iter); + } + it->second.push_back(info); return ErrorCode::NO_ERROR; } -int32_t FullImeInfoManager::Add(int32_t userId, const std::string &bundleName) -{ - return Update(userId, bundleName); -} - -int32_t FullImeInfoManager::Delete(int32_t userId, const std::string &bundleName) +int32_t FullImeInfoManager::DeletePackage(int32_t userId, const std::string &bundleName) { std::lock_guard lock(lock_); auto it = fullImeInfos_.find(userId); @@ -143,31 +224,48 @@ int32_t FullImeInfoManager::Update(int32_t userId, const std::string &bundleName return ErrorCode::NO_ERROR; } -std::vector FullImeInfoManager::Get(int32_t userId) +int32_t FullImeInfoManager::Get(int32_t userId, std::vector &props) { - std::lock_guard lock(lock_); - auto it = fullImeInfos_.find(userId); - if (it == fullImeInfos_.end()) { - return {}; + { + std::lock_guard lock(lock_); + auto it = fullImeInfos_.find(userId); + if (it == fullImeInfos_.end()) { + return {}; + } + for (auto &fullImeInfo : it->second) { + props.push_back(fullImeInfo.prop); + } } - return it->second; + auto ret = ImeEnabledInfoManager::GetInstance().GetEnabledStates(userId, props); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("get enabled status failed:%{public}d!", ret); + return ErrorCode::ERROR_ENABLE_IME; + } + return ErrorCode::NO_ERROR; } -bool FullImeInfoManager::Get(const std::string &bundleName, int32_t userId, FullImeInfo &fullImeInfo) +bool FullImeInfoManager::Get(int32_t userId, const std::string &bundleName, FullImeInfo &fullImeInfo) { - std::lock_guard lock(lock_); - auto it = fullImeInfos_.find(userId); - if (it == fullImeInfos_.end()) { - IMSA_HILOGD("user %{public}d info", userId); - return false; + { + std::lock_guard lock(lock_); + auto it = fullImeInfos_.find(userId); + if (it == fullImeInfos_.end()) { + IMSA_HILOGD("user %{public}d info", userId); + return false; + } + auto iter = std::find_if(it->second.begin(), it->second.end(), + [&bundleName](const FullImeInfo &info) { return bundleName == info.prop.name; }); + if (iter == it->second.end()) { + IMSA_HILOGD("ime: %{public}s not in cache", bundleName.c_str()); + return false; + } + fullImeInfo = *iter; } - auto iter = std::find_if(it->second.begin(), it->second.end(), - [&bundleName](const FullImeInfo &info) { return bundleName == info.prop.name; }); - if (iter == it->second.end()) { - IMSA_HILOGD("ime: %{public}s not in cache", bundleName.c_str()); - return false; + auto ret = + ImeEnabledInfoManager::GetInstance().GetEnabledState(userId, fullImeInfo.prop.name, fullImeInfo.prop.status); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("[%{public}d, %{public}s] get enabled status failed:%{public}d!", userId, bundleName.c_str(), ret); } - fullImeInfo = *iter; return true; } @@ -197,5 +295,19 @@ std::string FullImeInfoManager::Get(int32_t userId, uint32_t tokenId) } return (*iter).prop.name; } + +void FullImeInfoManager::HandleEnableTask(const std::function &task, const std::string &taskName) +{ + if (serviceHandler_ != nullptr) { + return; + } + serviceHandler_->PostTask(task, taskName, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); +} + +void FullImeInfoManager::SetEventHandler(const std::shared_ptr &eventHandler) +{ + ImeEnabledInfoManager::GetInstance().SetEventHandler(eventHandler); + serviceHandler_ = eventHandler; +} } // namespace MiscServices } // namespace OHOS \ No newline at end of file diff --git a/services/src/ime_info_inquirer.cpp b/services/src/ime_info_inquirer.cpp index a587fa0112e583b830465f1fce6ecbd3beeb0243..7a4763cc4731735cdd34d2ed925ae5f5ca1c7eb7 100644 --- a/services/src/ime_info_inquirer.cpp +++ b/services/src/ime_info_inquirer.cpp @@ -52,19 +52,9 @@ void ImeInfoInquirer::InitSystemConfig() } } -bool ImeInfoInquirer::IsEnableInputMethod() +SystemConfig ImeInfoInquirer::GetSystemConfig() { - return systemConfig_.enableInputMethodFeature; -} - -bool ImeInfoInquirer::IsEnableSecurityMode() -{ - return systemConfig_.enableFullExperienceFeature; -} - -EnabledStatus ImeInfoInquirer::GetSystemInitEnabledState() -{ - return systemConfig_.initEnabledState; + return systemConfig_; } bool ImeInfoInquirer::IsEnableAppAgent() @@ -130,14 +120,12 @@ std::shared_ptr ImeInfoInquirer::GetImeInfo(int32_t userId, const std:: std::shared_ptr ImeInfoInquirer::GetImeInfoFromCache(const int32_t userId, const std::string &bundleName, const std::string &subName) { - auto fullInfo = FullImeInfoManager::GetInstance().Get(userId); - auto it = std::find_if(fullInfo.begin(), fullInfo.end(), - [&bundleName](const FullImeInfo &info) { return info.prop.name == bundleName; }); - if (it == fullInfo.end()) { + FullImeInfo imeInfo; + if (!FullImeInfoManager::GetInstance().Get(userId, bundleName, imeInfo)) { return nullptr; } auto info = std::make_shared(); - auto subProps = it->subProps; + auto subProps = imeInfo.subProps; info->isSpecificSubName = !subName.empty(); if (subName.empty() && !subProps.empty()) { info->subProp = subProps[0]; @@ -150,9 +138,9 @@ std::shared_ptr ImeInfoInquirer::GetImeInfoFromCache(const int32_t user } info->subProp = *iter; } - info->isNewIme = it->isNewIme; - info->subProps = it->subProps; - info->prop = it->prop; + info->isNewIme = imeInfo.isNewIme; + info->subProps = imeInfo.subProps; + info->prop = imeInfo.prop; if (!info->isNewIme) { // old ime, make the id of prop same with the id of subProp. info->prop.id = info->subProp.id; @@ -257,34 +245,38 @@ std::vector ImeInfoInquirer::ListInputMethodInfo(const int32_t return properties; } -int32_t ImeInfoInquirer::ListInputMethod(int32_t userId, InputMethodStatus status, std::vector &props, - bool enableOn) +int32_t ImeInfoInquirer::ListInputMethod(int32_t userId, InputMethodStatus status, std::vector &props) { IMSA_HILOGD("userId: %{public}d, status: %{public}d.", userId, status); if (status == InputMethodStatus::ALL) { - return ListInputMethod(userId, props); + return ListAllInputMethod(userId, props); } if (status == InputMethodStatus::ENABLE) { - return ListEnabledInputMethod(userId, props, enableOn); + return ListEnabledInputMethod(userId, props); } if (status == InputMethodStatus::DISABLE) { - return ListDisabledInputMethod(userId, props, enableOn); + return ListDisabledInputMethod(userId, props); } return ErrorCode::ERROR_BAD_PARAMETERS; } +int32_t ImeInfoInquirer::ListAllInputMethod(const int32_t userId, std::vector &props) +{ + auto ret = ListInputMethod(userId, props); + if (ret == ErrorCode::ERROR_ENABLE_IME) { + return ErrorCode::NO_ERROR; + } + return ret; +} + int32_t ImeInfoInquirer::ListInputMethod(const int32_t userId, std::vector &props) { IMSA_HILOGD("userId: %{public}d.", userId); - auto infos = FullImeInfoManager::GetInstance().Get(userId); - for (const auto &info : infos) { - props.push_back(info.prop); - } + auto ret = FullImeInfoManager::GetInstance().Get(userId, props); if (!props.empty()) { - return ErrorCode::NO_ERROR; + return ret; } - - IMSA_HILOGD("%{public}d get all prop form bms.", userId); + IMSA_HILOGI("%{public}d get all prop form bms.", userId); std::vector extensionInfos; if (!QueryImeExtInfos(userId, extensionInfos)) { IMSA_HILOGE("failed to QueryImeExtInfos!"); @@ -307,10 +299,15 @@ int32_t ImeInfoInquirer::ListInputMethod(const int32_t userId, std::vector &props, bool enableOn) +int32_t ImeInfoInquirer::ListEnabledInputMethod(const int32_t userId, std::vector &props) { IMSA_HILOGD("userId: %{public}d.", userId); int32_t ret = ListInputMethod(userId, props); @@ -318,60 +315,30 @@ int32_t ImeInfoInquirer::ListEnabledInputMethod(const int32_t userId, std::vecto IMSA_HILOGE("userId: %{public}d listInputMethod failed!", userId); return ret; } - if (enableOn) { - IMSA_HILOGD("enable on."); - std::vector enableVec; - ret = EnableImeDataParser::GetInstance()->GetEnableIme(userId, enableVec); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("get enable data failed!"); - return ret; - } - auto info = GetDefaultIme(); - enableVec.insert(enableVec.begin(), info.bundleName); - - auto newEnd = std::remove_if(props.begin(), props.end(), [&enableVec](const auto &prop) { - return std::find(enableVec.begin(), enableVec.end(), prop.name) == enableVec.end(); - }); - props.erase(newEnd, props.end()); - } + auto start = std::remove_if( + props.begin(), props.end(), [](const auto &prop) { return prop.status == EnabledStatus::DISABLED; }); + props.erase(start, props.end()); return ErrorCode::NO_ERROR; } -int32_t ImeInfoInquirer::ListDisabledInputMethod(const int32_t userId, std::vector &props, bool enableOn) +int32_t ImeInfoInquirer::ListDisabledInputMethod(const int32_t userId, std::vector &props) { IMSA_HILOGD("userId: %{public}d.", userId); - if (!enableOn) { - IMSA_HILOGD("enable mode off, get disabled ime."); - return ErrorCode::NO_ERROR; - } - auto ret = ListInputMethod(userId, props); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("userId: %{public}d listInputMethod failed!", userId); return ret; } - - std::vector enableVec; - ret = EnableImeDataParser::GetInstance()->GetEnableIme(userId, enableVec); - if (ret != ErrorCode::NO_ERROR) { - IMSA_HILOGE("get enable data failed!"); - return ret; - } - auto info = GetDefaultIme(); - enableVec.insert(enableVec.begin(), info.bundleName); - - auto newEnd = std::remove_if(props.begin(), props.end(), [&enableVec](const auto &prop) { - return std::find(enableVec.begin(), enableVec.end(), prop.name) != enableVec.end(); - }); - props.erase(newEnd, props.end()); + auto start = std::remove_if( + props.begin(), props.end(), [](const auto &prop) { return prop.status != EnabledStatus::DISABLED; }); + props.erase(start, props.end()); return ErrorCode::NO_ERROR; } -int32_t ImeInfoInquirer::GetSwitchInfoBySwitchCount(SwitchInfo &switchInfo, int32_t userId, bool enableOn, - uint32_t cacheCount) +int32_t ImeInfoInquirer::GetSwitchInfoBySwitchCount(SwitchInfo &switchInfo, int32_t userId, uint32_t cacheCount) { std::vector props; - auto ret = ListEnabledInputMethod(userId, props, enableOn); + auto ret = ListEnabledInputMethod(userId, props); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("userId: %{public}d ListEnabledInputMethod failed!", userId); return ret; @@ -399,11 +366,9 @@ int32_t ImeInfoInquirer::ListInputMethodSubtype(int32_t userId, const std::strin std::vector &subProps) { IMSA_HILOGD("userId: %{public}d, bundleName: %{public}s.", userId, bundleName.c_str()); - auto infos = FullImeInfoManager::GetInstance().Get(userId); - auto it = std::find_if( - infos.begin(), infos.end(), [&bundleName](const FullImeInfo &info) { return info.prop.name == bundleName; }); - if (it != infos.end()) { - subProps = (*it).subProps; + FullImeInfo imeInfo; + if (FullImeInfoManager::GetInstance().Get(userId, bundleName, imeInfo)) { + subProps = imeInfo.subProps; return ErrorCode::NO_ERROR; } @@ -679,6 +644,10 @@ std::shared_ptr ImeInfoInquirer::GetImeProperty( prop.label = GetTargetString(extInfos[0], ImeTargetString::LABEL, userId); prop.labelId = extInfos[0].applicationInfo.labelId; prop.iconId = extInfos[0].applicationInfo.iconId; + ret = FullImeInfoManager::GetInstance().GetEnabledState(userId, prop.name, prop.status); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("get enabled status failed:%{public}d!", ret); + } return std::make_shared(prop); } @@ -686,11 +655,9 @@ std::shared_ptr ImeInfoInquirer::GetCurrentInputMethod(int32_t userId) { auto currentImeCfg = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId); IMSA_HILOGD("currentIme: %{public}s.", currentImeCfg->imeId.c_str()); - auto infos = FullImeInfoManager::GetInstance().Get(userId); - auto it = std::find_if(infos.begin(), infos.end(), - [¤tImeCfg](const FullImeInfo &info) { return info.prop.name == currentImeCfg->bundleName; }); - if (it != infos.end()) { - auto prop = std::make_shared((*it).prop); + FullImeInfo imeInfo; + if (FullImeInfoManager::GetInstance().Get(userId, currentImeCfg->bundleName, imeInfo)) { + auto prop = std::make_shared(imeInfo.prop); prop->id = currentImeCfg->extName; return prop; } @@ -703,18 +670,16 @@ std::shared_ptr ImeInfoInquirer::GetCurrentSubtype(int32_t userId) { auto currentIme = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId); IMSA_HILOGD("currentIme: %{public}s.", currentIme->imeId.c_str()); - auto infos = FullImeInfoManager::GetInstance().Get(userId); - auto it = std::find_if(infos.begin(), infos.end(), - [¤tIme](const FullImeInfo &info) { return info.prop.name == currentIme->bundleName; }); - if (it != infos.end() && !it->subProps.empty()) { - auto iter = std::find_if(it->subProps.begin(), it->subProps.end(), + FullImeInfo imeInfo; + if (FullImeInfoManager::GetInstance().Get(userId, currentIme->bundleName, imeInfo) && !imeInfo.subProps.empty()) { + auto iter = std::find_if(imeInfo.subProps.begin(), imeInfo.subProps.end(), [¤tIme](const SubProperty &subProp) { return subProp.id == currentIme->subName; }); - if (iter != it->subProps.end()) { + if (iter != imeInfo.subProps.end()) { return std::make_shared(*iter); } IMSA_HILOGW("subtype %{public}s not found.", currentIme->subName.c_str()); - ImeCfgManager::GetInstance().ModifyImeCfg({ userId, currentIme->imeId, it->subProps[0].id, false }); - return std::make_shared(it->subProps[0]); + ImeCfgManager::GetInstance().ModifyImeCfg({ userId, currentIme->imeId, imeInfo.subProps[0].id, false }); + return std::make_shared(imeInfo.subProps[0]); } IMSA_HILOGD("%{public}d get [%{public}s, %{public}s] form bms.", userId, currentIme->bundleName.c_str(), @@ -757,8 +722,12 @@ bool ImeInfoInquirer::IsImeInstalled(const int32_t userId, const std::string &bu std::shared_ptr ImeInfoInquirer::GetImeToStart(int32_t userId) { auto currentImeCfg = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId); - IMSA_HILOGD("userId: %{public}d, currentIme: %{public}s.", userId, currentImeCfg->imeId.c_str()); - if (currentImeCfg->imeId.empty() || !IsImeInstalled(userId, currentImeCfg->bundleName, currentImeCfg->extName)) { + EnabledStatus status = EnabledStatus::DISABLED; + FullImeInfoManager::GetInstance().GetEnabledState(userId, currentImeCfg->bundleName, status); + IMSA_HILOGI("[%{public}d, %{public}s] status: %{public}d.", userId, currentImeCfg->imeId.c_str(), + static_cast(status)); + if (currentImeCfg->imeId.empty() || !IsImeInstalled(userId, currentImeCfg->bundleName, currentImeCfg->extName) + || status == EnabledStatus::DISABLED) { auto newIme = GetDefaultIme(); auto info = GetDefaultImeInfo(userId); if (info == nullptr) { @@ -809,11 +778,9 @@ int32_t ImeInfoInquirer::GetDefaultInputMethod(const int32_t userId, std::shared IMSA_HILOGE("abnormal default ime cfg!"); return ErrorCode::ERROR_NULL_POINTER; } - auto infos = FullImeInfoManager::GetInstance().Get(userId); - auto it = std::find_if(infos.begin(), infos.end(), - [&defaultIme](const FullImeInfo &info) { return info.prop.name == defaultIme->name; }); - if (it != infos.end()) { - prop = std::make_shared((*it).prop); + FullImeInfo imeInfo; + if (FullImeInfoManager::GetInstance().Get(userId, defaultIme->name, imeInfo)) { + prop = std::make_shared(imeInfo.prop); prop->id = defaultIme->id; return ErrorCode::NO_ERROR; } @@ -1075,10 +1042,13 @@ int32_t ImeInfoInquirer::GetFullImeInfo( imeInfo.prop.labelId = extInfos[0].applicationInfo.labelId; imeInfo.prop.iconId = extInfos[0].applicationInfo.iconId; BundleInfo bundleInfo; - if (GetBundleInfoByBundleName(userId, imeInfo.prop.name, bundleInfo)) { - imeInfo.appId = bundleInfo.signatureInfo.appIdentifier; - imeInfo.versionCode = bundleInfo.versionCode; + if (!GetBundleInfoByBundleName(userId, imeInfo.prop.name, bundleInfo)) { + IMSA_HILOGE("[%{public}d,%{public}s] GetBundleInfoByBundleName failed!", userId, imeInfo.prop.name.c_str()); + return ErrorCode::ERROR_PACKAGE_MANAGER; } + imeInfo.appId = bundleInfo.signatureInfo.appIdentifier; + imeInfo.versionCode = bundleInfo.versionCode; + imeInfo.installTime = std::to_string(bundleInfo.installTime); return ErrorCode::NO_ERROR; } @@ -1144,7 +1114,7 @@ bool ImeInfoInquirer::IsRunningIme(int32_t userId, const std::string &bundleName bool ImeInfoInquirer::GetImeAppId(int32_t userId, const std::string &bundleName, std::string &appId) { FullImeInfo imeInfo; - if (FullImeInfoManager::GetInstance().Get(bundleName, userId, imeInfo) && !imeInfo.appId.empty()) { + if (FullImeInfoManager::GetInstance().Get(userId, bundleName, imeInfo) && !imeInfo.appId.empty()) { appId = imeInfo.appId; return true; } @@ -1159,7 +1129,7 @@ bool ImeInfoInquirer::GetImeAppId(int32_t userId, const std::string &bundleName, bool ImeInfoInquirer::GetImeVersionCode(int32_t userId, const std::string &bundleName, uint32_t &versionCode) { FullImeInfo imeInfo; - if (FullImeInfoManager::GetInstance().Get(bundleName, userId, imeInfo)) { + if (FullImeInfoManager::GetInstance().Get(userId, bundleName, imeInfo)) { versionCode = imeInfo.versionCode; return true; } diff --git a/services/src/input_method_system_ability.cpp b/services/src/input_method_system_ability.cpp index e6b9b64172a25784874aa8fc967d6f6b03ce8383..ddc7d96c5baf19a805db8ee6d8fcab7397ac2a7b 100644 --- a/services/src/input_method_system_ability.cpp +++ b/services/src/input_method_system_ability.cpp @@ -380,12 +380,6 @@ void InputMethodSystemAbility::UpdateUserInfo(int32_t userId) userId_ = userId; UserSessionManager::GetInstance().AddUserSession(userId_); InputMethodSysEvent::GetInstance().SetUserId(userId_); - if (enableImeOn_.load()) { - EnableImeDataParser::GetInstance()->OnUserChanged(userId_); - } - if (enableSecurityMode_.load()) { - SecurityModeParser::GetInstance()->UpdateFullModeList(userId_); - } } int32_t InputMethodSystemAbility::OnIdle(const SystemAbilityOnDemandReason &idleReason) @@ -405,6 +399,8 @@ void InputMethodSystemAbility::OnStop() { IMSA_HILOGI("OnStop start."); FreezeManager::SetEventHandler(nullptr); + UserSessionManager::GetInstance().SetEventHandler(nullptr); + FullImeInfoManager::GetInstance().SetEventHandler(nullptr); serviceHandler_ = nullptr; state_ = ServiceRunningState::STATE_NOT_START; Memory::MemMgrClient::GetInstance().NotifyProcessStatus(getpid(), 1, 0, INPUT_METHOD_SYSTEM_ABILITY_ID); @@ -420,7 +416,7 @@ void InputMethodSystemAbility::InitServiceHandler() std::shared_ptr runner = AppExecFwk::EventRunner::Create("OS_InputMethodSystemAbility"); serviceHandler_ = std::make_shared(runner); FreezeManager::SetEventHandler(serviceHandler_); - + FullImeInfoManager::GetInstance().SetEventHandler(serviceHandler_); IMSA_HILOGI("InitServiceHandler succeeded."); } @@ -440,6 +436,10 @@ void InputMethodSystemAbility::Initialize() InputMethodSysEvent::GetInstance().SetUserId(userId_); IMSA_HILOGI("start get scene board enable status"); isScbEnable_.store(Rosen::SceneBoardJudgement::IsSceneBoardEnabled()); + FullImeInfoManager::GetInstance().SetEnabledStatusChangedHandler( + [this](int32_t userId, const std::string &bundleName, EnabledStatus oldStatus) { + OnImeEnabledStatusChange(userId, bundleName, oldStatus); + }); IMSA_HILOGI("InputMethodSystemAbility::Initialize end."); } @@ -1139,8 +1139,10 @@ ErrCode InputMethodSystemAbility::SwitchInputMethod(const std::string &bundleNam IMSA_HILOGE("%{public}d session is nullptr!", userId); return ErrorCode::ERROR_NULL_POINTER; } - if (enableImeOn_.load() && !EnableImeDataParser::GetInstance()->CheckNeedSwitch(switchInfo, userId)) { - IMSA_HILOGW("Enable mode off or switch is not enable, stopped!"); + EnabledStatus status = EnabledStatus::DISABLED; + auto ret = FullImeInfoManager::GetInstance().GetEnabledState(userId, bundleName, status); + if (ret != ErrorCode::NO_ERROR || status == EnabledStatus::DISABLED) { + IMSA_HILOGW("ime %{public}s not enable, stopped!", bundleName.c_str()); return ErrorCode::ERROR_ENABLE_IME; } auto currentImeCfg = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId); @@ -1154,15 +1156,24 @@ ErrCode InputMethodSystemAbility::SwitchInputMethod(const std::string &bundleNam : OnSwitchInputMethod(userId, switchInfo, static_cast(trigger)); } -ErrCode InputMethodSystemAbility::EnableIme(const std::string &bundleName, bool& resultValue) +int32_t InputMethodSystemAbility::EnableIme( + const std::string &bundleName, const std::string &extensionName, int32_t status) { - if (CheckEnableAndSwitchPermission() != ErrorCode::NO_ERROR) { - IMSA_HILOGE("Check enable ime value failed!"); - return false; + auto ret = CheckEnableAndSwitchPermission(); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("permission check failed!"); + return ret; } + ret = ErrorCode::ERROR_EX_SERVICE_SPECIFIC; int32_t userId = GetCallingUserId(); - resultValue = SettingsDataUtils::GetInstance()->EnableIme(userId, bundleName); - return ERR_OK; + if (serviceHandler_ == nullptr) { + return ret; + } + auto task = [&userId, &bundleName, &extensionName, &status, &ret]() { + ret = ImeEnabledInfoManager::GetInstance().Update(userId, bundleName, extensionName, status); + }; + serviceHandler_->PostSyncTask(task, "enableUpdate", AppExecFwk::EventQueue::Priority::IMMEDIATE); + return ret; } int32_t InputMethodSystemAbility::OnSwitchInputMethod(int32_t userId, const SwitchInfo &switchInfo, @@ -1427,8 +1438,8 @@ ErrCode InputMethodSystemAbility::GetInputMethodConfig(ElementName &inputMethodC ErrCode InputMethodSystemAbility::ListInputMethod(uint32_t status, std::vector &props) { - return ImeInfoInquirer::GetInstance().ListInputMethod(GetCallingUserId(), - static_cast(status), props, enableImeOn_.load()); + return ImeInfoInquirer::GetInstance().ListInputMethod( + GetCallingUserId(), static_cast(status), props); } ErrCode InputMethodSystemAbility::ListCurrentInputMethodSubtype(std::vector &subProps) @@ -1486,8 +1497,7 @@ void InputMethodSystemAbility::WorkThread() FullImeInfoManager::GetInstance().Update(); break; } - case MSG_ID_BOOT_COMPLETED: - case MSG_ID_OS_ACCOUNT_STARTED: { + case MSG_ID_BOOT_COMPLETED: { FullImeInfoManager::GetInstance().Init(); break; } @@ -1495,6 +1505,10 @@ void InputMethodSystemAbility::WorkThread() OnScreenUnlock(msg); break; } + case MSG_ID_REGULAR_UPDATE_IME_INFO: { + FullImeInfoManager::GetInstance().Init(); + break; + } default: { IMSA_HILOGD("the message is %{public}d.", msg->msgId_); break; @@ -1584,10 +1598,6 @@ int32_t InputMethodSystemAbility::HandlePackageEvent(const Message *msg) return FullImeInfoManager::GetInstance().Update(userId, packageName); } if (msg->msgId_ == MSG_ID_PACKAGE_ADDED) { - auto instance = EnableImeDataParser::GetInstance(); - if (instance != nullptr) { - instance->OnPackageAdded(userId, packageName); - } return FullImeInfoManager::GetInstance().Add(userId, packageName); } if (msg->msgId_ == MSG_ID_PACKAGE_REMOVED) { @@ -1704,7 +1714,8 @@ int32_t InputMethodSystemAbility::SwitchByCombinationKey(uint32_t state) return ErrorCode::ERROR_EX_UNSUPPORTED_OPERATION; } -void InputMethodSystemAbility::DealSecurityChange() +void InputMethodSystemAbility::DealSecurityChange( + int32_t userId, const std::string &bundleName, EnabledStatus oldStatus) { { std::lock_guard lock(modeChangeMutex_); @@ -1717,7 +1728,7 @@ void InputMethodSystemAbility::DealSecurityChange() hasPendingChanges_ = true; } } - auto changeTask = [this]() { + auto changeTask = [this, userId, bundleName, oldStatus]() { pthread_setname_np(pthread_self(), "SecurityChange"); auto checkChangeCount = [this]() { std::lock_guard lock(modeChangeMutex_); @@ -1728,7 +1739,7 @@ void InputMethodSystemAbility::DealSecurityChange() return false; }; do { - OnSecurityModeChange(); + OnSecurityModeChange(userId, bundleName, oldStatus); } while (checkChangeCount()); }; std::thread(changeTask).detach(); @@ -1811,7 +1822,7 @@ int32_t InputMethodSystemAbility::SwitchType() cacheCount = targetSwitchCount_.exchange(0); } int32_t ret = - ImeInfoInquirer::GetInstance().GetSwitchInfoBySwitchCount(switchInfo, userId_, enableImeOn_.load(), cacheCount); + ImeInfoInquirer::GetInstance().GetSwitchInfoBySwitchCount(switchInfo, userId_, cacheCount); if (ret != ErrorCode::NO_ERROR) { IMSA_HILOGE("get next SwitchInfo failed, stop switching ime."); return ret; @@ -1843,17 +1854,22 @@ void InputMethodSystemAbility::InitMonitors() void InputMethodSystemAbility::HandleDataShareReady() { - if (ImeInfoInquirer::GetInstance().IsEnableInputMethod()) { - IMSA_HILOGW("Enter enable mode."); - RegisterEnableImeObserver(); - EnableImeDataParser::GetInstance()->Initialize(userId_); - enableImeOn_.store(true); - } - if (ImeInfoInquirer::GetInstance().IsEnableSecurityMode()) { + IMSA_HILOGI("run in."); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { IMSA_HILOGW("Enter security mode."); RegisterSecurityModeObserver(); - SecurityModeParser::GetInstance()->Initialize(userId_); - enableSecurityMode_.store(true); + } + if (SettingsDataUtils::GetInstance()->IsDataShareReady()) { + return; + } + SettingsDataUtils::GetInstance()->NotifyDataShareReady(); + auto session = UserSessionManager::GetInstance().GetUserSession(userId_); + if (session == nullptr) { + UserSessionManager::GetInstance().AddUserSession(userId_); + } + session = UserSessionManager::GetInstance().GetUserSession(userId_); + if (session != nullptr) { + session->AddRestartIme(); } FullImeInfoManager::GetInstance().Init(); } @@ -1928,86 +1944,132 @@ void InputMethodSystemAbility::InitWindowDisplayChangedMonitor() WindowAdapter::GetInstance().RegisterCallingWindowInfoChangedListener(callBack); } -void InputMethodSystemAbility::RegisterEnableImeObserver() -{ - int32_t ret = SettingsDataUtils::GetInstance()->CreateAndRegisterObserver(EnableImeDataParser::ENABLE_IME, - [this]() { DatashareCallback(EnableImeDataParser::ENABLE_IME); }); - IMSA_HILOGI("register enable ime observer, ret: %{public}d.", ret); - ret = SettingsDataUtils::GetInstance()->CreateAndRegisterObserver(EnableImeDataParser::ENABLE_KEYBOARD, - [this]() { DatashareCallback(EnableImeDataParser::ENABLE_KEYBOARD); }); - IMSA_HILOGI("register enable keyboard observer, ret: %{public}d.", ret); -} - void InputMethodSystemAbility::RegisterSecurityModeObserver() { - int32_t ret = SettingsDataUtils::GetInstance()->CreateAndRegisterObserver(SecurityModeParser::SECURITY_MODE, - [this]() { DatashareCallback(SecurityModeParser::SECURITY_MODE); }); + int32_t ret = SettingsDataUtils::GetInstance()->CreateAndRegisterObserver(SETTING_URI_PROXY, + ImeEnabledInfoManager::SECURITY_MODE, [this]() { DatashareCallback(ImeEnabledInfoManager::SECURITY_MODE); }); IMSA_HILOGI("register security mode observer, ret: %{public}d", ret); } void InputMethodSystemAbility::DatashareCallback(const std::string &key) +{ + if (key != ImeEnabledInfoManager::SECURITY_MODE) { + return; + } + IMSA_HILOGI("%{public}d full experience change.", userId_); + if (serviceHandler_ == nullptr) { + return; + } + auto task = [userId = userId_]() { ImeEnabledInfoManager::GetInstance().OnFullExperienceTableChanged(userId); }; + serviceHandler_->PostTask(task, "OnFullExperienceTableChanged", 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); +} + +void InputMethodSystemAbility::OnImeEnabledStatusChange( + int32_t userId, const std::string &bundleName, EnabledStatus oldStatus) { IMSA_HILOGI("start."); - if (key == EnableImeDataParser::ENABLE_KEYBOARD || key == EnableImeDataParser::ENABLE_IME) { - EnableImeDataParser::GetInstance()->OnConfigChanged(userId_, key); - std::lock_guard autoLock(checkMutex_); + auto session = UserSessionManager::GetInstance().GetUserSession(userId); + if (session == nullptr) { + IMSA_HILOGE("%{public}d session is nullptr!", userId); + return; + } + auto imeData = session->GetImeData(ImeType::IME); + std::string currentBundleName; + if (imeData != nullptr) { + currentBundleName = imeData->ime.first; + } else { + IMSA_HILOGE("%{public}d imeData is nullptr!", userId); + currentBundleName = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId)->bundleName; + } + if (bundleName != currentBundleName) { + IMSA_HILOGD("%{public}d,%{public}s not current ime %{public}s!", userId, bundleName.c_str(), + currentBundleName.c_str()); + return; + } + EnabledStatus newStatus; + auto ret = FullImeInfoManager::GetInstance().GetEnabledState(userId_, currentBundleName, newStatus); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGW( + "[%{public}d,%{public}s] get enabled status failed:%{public}d,!", userId_, currentBundleName.c_str(), ret); + return; + } + if (newStatus == EnabledStatus::DISABLED) { + auto ime = ImeInfoInquirer::GetInstance().GetDefaultIme(); SwitchInfo switchInfo; - if (EnableImeDataParser::GetInstance()->CheckNeedSwitch(key, switchInfo, userId_)) { - auto session = UserSessionManager::GetInstance().GetUserSession(userId_); - if (session == nullptr) { - IMSA_HILOGE("%{public}d session is nullptr!", userId_); - return; - } - switchInfo.timestamp = std::chrono::system_clock::now(); - session->GetSwitchQueue().Push(switchInfo); - OnSwitchInputMethod(userId_, switchInfo, SwitchTrigger::IMSA); - } + switchInfo.bundleName = ime.bundleName; + switchInfo.timestamp = std::chrono::system_clock::now(); + session->GetSwitchQueue().Push(switchInfo); + OnSwitchInputMethod(userId, switchInfo, SwitchTrigger::IMSA); + return; } - if (key == SecurityModeParser::SECURITY_MODE) { - DealSecurityChange(); + if (newStatus != oldStatus) { + DealSecurityChange(userId, bundleName, oldStatus); } } -void InputMethodSystemAbility::OnSecurityModeChange() +void InputMethodSystemAbility::OnSecurityModeChange( + int32_t userId, const std::string &bundleName, EnabledStatus oldStatus) { { std::lock_guard lock(modeChangeMutex_); hasPendingChanges_ = false; } - auto currentIme = ImeCfgManager::GetInstance().GetCurrentImeCfg(userId_); - auto oldMode = SecurityModeParser::GetInstance()->GetSecurityMode(currentIme->bundleName, userId_); - SecurityModeParser::GetInstance()->UpdateFullModeList(userId_); - auto newMode = SecurityModeParser::GetInstance()->GetSecurityMode(currentIme->bundleName, userId_); - if (oldMode == newMode) { - IMSA_HILOGD("current ime mode not changed."); + auto session = UserSessionManager::GetInstance().GetUserSession(userId); + if (session == nullptr) { + IMSA_HILOGE("%{public}d session is nullptr!", userId); return; } - IMSA_HILOGI("ime: %{public}s securityMode change to: %{public}d.", currentIme->bundleName.c_str(), - static_cast(newMode)); - auto session = UserSessionManager::GetInstance().GetUserSession(userId_); - if (session == nullptr) { - IMSA_HILOGE("%{public}d session is nullptr!", userId_); + auto imeData = session->GetImeData(ImeType::IME); + if (imeData == nullptr) { + IMSA_HILOGE("%{public}d imeData is nullptr!", userId); + return; + } + if (bundleName != imeData->ime.first) { + IMSA_HILOGD("%{public}d,%{public}s not current ime %{public}s!", userId, bundleName.c_str(), + imeData->ime.first.c_str()); return; } - session->OnSecurityChange(static_cast(newMode)); + EnabledStatus newStatus = EnabledStatus::DISABLED; + auto ret = FullImeInfoManager::GetInstance().GetEnabledState(userId, imeData->ime.first, newStatus); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGW("[%{public}d, %{public}s] get enabled status failed:%{public}d,!", userId, + imeData->ime.first.c_str(), ret); + return; + } + if (newStatus == EnabledStatus::DISABLED || newStatus == oldStatus) { + return; + } + IMSA_HILOGI("ime: %{public}s securityMode change to: %{public}d.", imeData->ime.first.c_str(), + static_cast(newStatus)); + if (newStatus == EnabledStatus::BASIC_MODE) { + session->OnSecurityChange(0); + } else { + session->OnSecurityChange(1); + } session->AddRestartIme(); } int32_t InputMethodSystemAbility::GetSecurityMode(int32_t &security) { IMSA_HILOGD("InputMethodSystemAbility start."); - if (!enableSecurityMode_.load()) { - security = static_cast(SecurityMode::FULL); - return ErrorCode::NO_ERROR; - } auto userId = GetCallingUserId(); auto bundleName = FullImeInfoManager::GetInstance().Get(userId, IPCSkeleton::GetCallingTokenID()); if (bundleName.empty()) { bundleName = identityChecker_->GetBundleNameByToken(IPCSkeleton::GetCallingTokenID()); - IMSA_HILOGW("%{public}s tokenId not find.", bundleName.c_str()); + if (!ImeInfoInquirer::GetInstance().IsInputMethod(userId, bundleName)) { + IMSA_HILOGE("[%{public}d, %{public}s] not an ime.", userId, bundleName.c_str()); + return ErrorCode::ERROR_NOT_IME; + } + } + security = 0; + EnabledStatus status = EnabledStatus::BASIC_MODE; + auto ret = FullImeInfoManager::GetInstance().GetEnabledState(userId, bundleName, status); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGW("[%{public}d, %{public}s] get enabled status failed:%{public}d,!", userId, bundleName.c_str(), ret); + } + if (status == EnabledStatus::FULL_EXPERIENCE_MODE) { + security = 1; } - SecurityMode mode = SecurityModeParser::GetInstance()->GetSecurityMode(bundleName, userId); - security = static_cast(mode); return ErrorCode::NO_ERROR; } @@ -2039,8 +2101,9 @@ ErrCode InputMethodSystemAbility::UnRegisteredProxyIme(int32_t type, const sptr< int32_t InputMethodSystemAbility::CheckEnableAndSwitchPermission() { - if (!identityChecker_->IsNativeSa(IPCSkeleton::GetCallingFullTokenID())) { - IMSA_HILOGE("not native sa!"); + if (!identityChecker_->IsNativeSa(IPCSkeleton::GetCallingFullTokenID()) + && !identityChecker_->IsSystemApp(IPCSkeleton::GetCallingFullTokenID())) { + IMSA_HILOGE("not native sa or system app!"); return ErrorCode::ERROR_STATUS_SYSTEM_PERMISSION; } if (!identityChecker_->HasPermission(IPCSkeleton::GetCallingTokenID(), @@ -2244,11 +2307,6 @@ void InputMethodSystemAbility::HandleOsAccountStarted() if (userId_ != userId) { UpdateUserInfo(userId); } - Message *msg = new (std::nothrow) Message(MessageID::MSG_ID_OS_ACCOUNT_STARTED, nullptr); - if (msg == nullptr) { - return; - } - MessageHandler::Instance()->SendMessage(msg); } void InputMethodSystemAbility::StopImeInBackground() @@ -2261,6 +2319,9 @@ void InputMethodSystemAbility::StopImeInBackground() } } }; + if (serviceHandler_ == nullptr) { + return; + } serviceHandler_->PostTask(task, "StopImeInBackground", 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); } @@ -2270,6 +2331,7 @@ int32_t InputMethodSystemAbility::GetUserId(int32_t uid) auto userId = OsAccountAdapter::GetOsAccountLocalIdFromUid(uid); // 0 represents user 0 in the system if (userId == 0) { + IMSA_HILOGI("user 0"); IMSA_HILOGI("user 0"); return userId_; } @@ -2427,9 +2489,8 @@ ErrCode InputMethodSystemAbility::GetInputMethodState(int32_t& status) return ErrorCode::ERROR_NOT_IME; } } - EnabledStatus tmpStatus = EnabledStatus::DISABLED; - auto ret = GetInputMethodState(userId, bundleName, tmpStatus); + auto ret = ImeEnabledInfoManager::GetInstance().GetEnabledState(userId, bundleName, tmpStatus); if (ret != ErrorCode::NO_ERROR) { return ret; } @@ -2437,42 +2498,6 @@ ErrCode InputMethodSystemAbility::GetInputMethodState(int32_t& status) return ErrorCode::NO_ERROR; } -int32_t InputMethodSystemAbility::GetInputMethodState( - int32_t userId, const std::string &bundleName, EnabledStatus &status) -{ - auto isDefaultBasicMode = !ImeInfoInquirer::GetInstance().IsEnableInputMethod(); - auto isDefaultFullExperience = !ImeInfoInquirer::GetInstance().IsEnableSecurityMode(); - IMSA_HILOGI("sys cfg:[%{public}d, %{public}d].", isDefaultBasicMode, isDefaultFullExperience); - auto isSecurityMode = SecurityModeParser::GetInstance()->IsSecurityMode(userId, bundleName); - if (isDefaultBasicMode) { - if (isDefaultFullExperience) { - status = EnabledStatus::FULL_EXPERIENCE_MODE; - return ErrorCode::NO_ERROR; - } - status = isSecurityMode ? EnabledStatus::FULL_EXPERIENCE_MODE : EnabledStatus::BASIC_MODE; - return ErrorCode::NO_ERROR; - } - - if (isDefaultFullExperience) { - auto ret = EnableImeDataParser::GetInstance()->GetImeEnablePattern(userId, bundleName, status); - if (ret != ErrorCode::NO_ERROR) { - return ret; - } - if (status == EnabledStatus::BASIC_MODE) { - status = EnabledStatus::FULL_EXPERIENCE_MODE; - } - return ErrorCode::NO_ERROR; - } - - if (isSecurityMode) { - status = EnabledStatus::FULL_EXPERIENCE_MODE; - return ErrorCode::NO_ERROR; - } - - auto ret = EnableImeDataParser::GetInstance()->GetImeEnablePattern(userId, bundleName, status); - return ret; -} - ErrCode InputMethodSystemAbility::ShowCurrentInput(uint32_t type) { auto name = ImfHiSysEventUtil::GetAppName(IPCSkeleton::GetCallingTokenID()); diff --git a/services/src/peruser_session.cpp b/services/src/peruser_session.cpp index 14f2547da01eeec484c108e9994cd83fee6cce76..1a26e81466c71c831846c853f90fb7488487a492 100644 --- a/services/src/peruser_session.cpp +++ b/services/src/peruser_session.cpp @@ -17,6 +17,7 @@ #include "variant" #include "peruser_session.h" #include "ability_manager_client.h" +#include "full_ime_info_manager.h" #include "identity_checker_impl.h" #include "ime_connection.h" #include "ime_info_inquirer.h" @@ -27,7 +28,6 @@ #include "on_demand_start_stop_sa.h" #include "os_account_adapter.h" #include "scene_board_judgement.h" -#include "security_mode_parser.h" #include "system_ability_definition.h" #include "wms_connection_observer.h" #include "dm_common.h" @@ -1086,9 +1086,9 @@ bool PerUserSession::CanStartIme() { return (IsSaReady(MEMORY_MANAGER_SA_ID) && IsWmsReady() && #ifdef IMF_SCREENLOCK_MGR_ENABLE - IsSaReady(SCREENLOCK_SERVICE_ID) && + IsSaReady(SCREENLOCK_SERVICE_ID) && #endif - runningIme_.empty()); + runningIme_.empty() && SettingsDataUtils::GetInstance()->IsDataShareReady()); } int32_t PerUserSession::ChangeToDefaultImeIfNeed( @@ -1121,27 +1121,29 @@ int32_t PerUserSession::ChangeToDefaultImeIfNeed( AAFwk::Want PerUserSession::GetWant(const std::shared_ptr &ime) { - SecurityMode mode; bool isolatedSandBox = true; - if (SecurityModeParser::GetInstance()->IsDefaultFullMode(ime->bundleName, userId_)) { - mode = SecurityMode::FULL; + EnabledStatus status = EnabledStatus::BASIC_MODE; + if (FullImeInfoManager::GetInstance().IsDefaultFullMode(userId_, ime->bundleName)) { + status = EnabledStatus::FULL_EXPERIENCE_MODE; isolatedSandBox = false; - } else if (ImeInfoInquirer::GetInstance().IsEnableSecurityMode()) { - mode = SecurityModeParser::GetInstance()->GetSecurityMode(ime->bundleName, userId_); } else { - mode = SecurityMode::FULL; + auto ret = FullImeInfoManager::GetInstance().GetEnabledState(userId_, ime->bundleName, status); + if (ret != ErrorCode::NO_ERROR) { + IMSA_HILOGE("[%{public}d,%{public}s] GetEnabledState failed.", userId_, ime->imeId.c_str()); + } } AAFwk::Want want; want.SetElementName(ime->bundleName, ime->extName); - want.SetParam(STRICT_MODE, !(mode == SecurityMode::FULL)); + want.SetParam(STRICT_MODE, !(status == EnabledStatus::FULL_EXPERIENCE_MODE)); want.SetParam(ISOLATED_SANDBOX, isolatedSandBox); IMSA_HILOGI("StartInputService userId: %{public}d, ime: %{public}s, mode: %{public}d, isolatedSandbox: %{public}d", - userId_, ime->imeId.c_str(), static_cast(mode), isolatedSandBox); + userId_, ime->imeId.c_str(), static_cast(status), isolatedSandBox); return want; } int32_t PerUserSession::StartInputService(const std::shared_ptr &ime) { + IMSA_HILOGI("run in %{public}s", ime->imeId.c_str()); if (ime == nullptr) { return ErrorCode::ERROR_IMSA_IME_TO_START_NULLPTR; } diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 1eb047d1f1800feb93f943bcfcfb79f05ecbb69b..68745ec49e72580412a3aaa891a3463de5630789 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -21,7 +21,6 @@ group("fuzztest") { "controlchannelstub_fuzzer:fuzztest", "corestub_fuzzer:fuzztest", "datachannelstub_fuzzer:fuzztest", - "enableimedataparse_fuzzer:fuzztest", "imeeventmonitormanager_fuzzer:fuzztest", "imesystemchannel_fuzzer:fuzztest", "imfhisyseventreport_fuzzer:fuzztest", @@ -31,7 +30,6 @@ group("fuzztest") { "inputmethodsystemability_fuzzer:fuzztest", "keyeventconsumer_fuzzer:fuzztest", "perusersession_fuzzer:fuzztest", - "securitymodeparser_fuzzer:fuzztest", "settingsdatautils_fuzzer:fuzztest", "systemabilitystub_fuzzer:fuzztest", "systemabilitystubfuzztest:system_ability_stub_fuzztest", diff --git a/test/fuzztest/enableimedataparse_fuzzer/BUILD.gn b/test/fuzztest/enableimedataparse_fuzzer/BUILD.gn deleted file mode 100644 index 14daf3ed89ad6455c3995a96df1a23dc463928d5..0000000000000000000000000000000000000000 --- a/test/fuzztest/enableimedataparse_fuzzer/BUILD.gn +++ /dev/null @@ -1,76 +0,0 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//base/inputmethod/imf/inputmethod.gni") - -#####################hydra-fuzz################### -import("//build/config/features.gni") -import("//build/ohos.gni") -import("//build/test.gni") - -##############################fuzztest########################################## -ohos_fuzztest("EnableImeDataParseFuzzTest") { - module_out_path = "imf/imf" - - fuzz_config_file = - "${inputmethod_path}/test/fuzztest/enableimedataparse_fuzzer" - - include_dirs = [ - "${inputmethod_path}/services/adapter/settings_data_provider/include", - "${inputmethod_path}/services/adapter/ime_connection_manager/include", - "${inputmethod_path}/services/include", - "${inputmethod_path}/test/common", - ] - - cflags = [ - "-g", - "-O0", - "-Wno-unused-variable", - "-fno-omit-frame-pointer", - ] - - sources = [ - "${inputmethod_path}/services/adapter/settings_data_provider/src/enable_ime_data_parser.cpp", - "enableimedataparse_fuzzer.cpp", - ] - - deps = [ - "${inputmethod_path}/interfaces/inner_api/inputmethod_controller:inputmethod_client_static", - "${inputmethod_path}/services:inputmethod_service_static", - "${inputmethod_path}/test/common:inputmethod_test_common", - ] - - external_deps = [ - "ability_base:zuri", - "ability_runtime:dataobs_manager", - "bundle_framework:appexecfwk_core", - "c_utils:utils", - "data_share:datashare_common", - "data_share:datashare_consumer", - "hilog:libhilog", - "input:libmmi-client", - "ipc:ipc_single", - "resource_management:global_resmgr", - ] -} - -############################################################################### -group("fuzztest") { - testonly = true - deps = [] - deps += [ - # deps file - ":EnableImeDataParseFuzzTest", - ] -} -############################################################################### diff --git a/test/fuzztest/enableimedataparse_fuzzer/corpus/init b/test/fuzztest/enableimedataparse_fuzzer/corpus/init deleted file mode 100644 index e7c3fecd8d4d4816e40088113a2316bb9eb2e13f..0000000000000000000000000000000000000000 --- a/test/fuzztest/enableimedataparse_fuzzer/corpus/init +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright (c) 2024 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -FUZZ \ No newline at end of file diff --git a/test/fuzztest/enableimedataparse_fuzzer/enableimedataparse_fuzzer.cpp b/test/fuzztest/enableimedataparse_fuzzer/enableimedataparse_fuzzer.cpp deleted file mode 100644 index fcc066effa3a459069320ddd548a3c1372ae5fca..0000000000000000000000000000000000000000 --- a/test/fuzztest/enableimedataparse_fuzzer/enableimedataparse_fuzzer.cpp +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define private public -#define protected public -#include "enable_ime_data_parser.h" -#undef private - -#include "enableimedataparse_fuzzer.h" - -using namespace OHOS::MiscServices; -namespace OHOS { -constexpr size_t THRESHOLD = 10; -void FuzzInitialize(const int32_t userId) -{ - EnableImeDataParser::GetInstance()->Initialize(userId); -} - -void FuzzOnUserChanged(const int32_t userId) -{ - EnableImeDataParser::GetInstance()->OnUserChanged(userId); -} - -void FuzzCheckNeedSwitch(const std::string &key, SwitchInfo &switchInfo, const int32_t userId) -{ - EnableImeDataParser::GetInstance()->CheckNeedSwitch(key, switchInfo, userId); - EnableImeDataParser::GetInstance()->CheckNeedSwitch(switchInfo, userId); -} - -void FuzzGetEnableData(const std::string &key, std::vector &enableVec, const int32_t userId) -{ - EnableImeDataParser::GetInstance()->GetEnableData(key, enableVec, userId); -} - -void FuzzParseEnableIme(const std::string &valueStr, int32_t userId, std::vector &enableVec) -{ - EnableImeDataParser::GetInstance()->ParseEnableIme(valueStr, userId, enableVec); -} - -void FuzzParseEnableKeyboard(const std::string &valueStr, int32_t userId, std::vector &enableVec) -{ - EnableImeDataParser::GetInstance()->ParseEnableKeyboard(valueStr, userId, enableVec); -} - -void FuzzCheckTargetEnableName( - const std::string &key, const std::string &targetName, std::string &nextIme, const int32_t userId) -{ - EnableImeDataParser::GetInstance()->CheckTargetEnableName(key, targetName, nextIme, userId); -} -} // namespace OHOS -/* Fuzzer entry point */ -extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) -{ - if (size < OHOS::THRESHOLD) { - return 0; - } - /* Run your code on data */ - const int32_t userId = static_cast(size); - std::string fuzzedString(reinterpret_cast(data), size); - SwitchInfo switchInfo; - std::vector enableVec; - - OHOS::FuzzInitialize(userId); - OHOS::FuzzOnUserChanged(userId); - OHOS::FuzzCheckNeedSwitch(fuzzedString, switchInfo, userId); - OHOS::FuzzGetEnableData(fuzzedString, enableVec, userId); - - OHOS::FuzzParseEnableIme(fuzzedString, userId, enableVec); - OHOS::FuzzParseEnableKeyboard(fuzzedString, userId, enableVec); - OHOS::FuzzCheckTargetEnableName(fuzzedString, fuzzedString, fuzzedString, userId); - return 0; -} \ No newline at end of file diff --git a/test/fuzztest/enableimedataparse_fuzzer/enableimedataparse_fuzzer.h b/test/fuzztest/enableimedataparse_fuzzer/enableimedataparse_fuzzer.h deleted file mode 100644 index 43d74ae709a91e99b5a8b877cfadd806709d137f..0000000000000000000000000000000000000000 --- a/test/fuzztest/enableimedataparse_fuzzer/enableimedataparse_fuzzer.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef TEST_FUZZTEST_ENABLE_IME_DATA_PARSE_FUZZER_H -#define TEST_FUZZTEST_ENABLE_IME_DATA_PARSE_FUZZER_H - -#define FUZZ_PROJECT_NAME "enableimedataparse_fuzzer" - -#endif // TEST_FUZZTEST_ENABLE_IME_DATA_PARSE_FUZZER_H \ No newline at end of file diff --git a/test/fuzztest/enableimedataparse_fuzzer/project.xml b/test/fuzztest/enableimedataparse_fuzzer/project.xml deleted file mode 100644 index 6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec..0000000000000000000000000000000000000000 --- a/test/fuzztest/enableimedataparse_fuzzer/project.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - 1000 - - 300 - - 4096 - - diff --git a/test/fuzztest/securitymodeparser_fuzzer/BUILD.gn b/test/fuzztest/securitymodeparser_fuzzer/BUILD.gn deleted file mode 100644 index 03498c4f2e4bd2dc0f801f44d4cac1b076053ddc..0000000000000000000000000000000000000000 --- a/test/fuzztest/securitymodeparser_fuzzer/BUILD.gn +++ /dev/null @@ -1,74 +0,0 @@ -# Copyright (c) 2022-2002 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import("//base/inputmethod/imf/inputmethod.gni") - -#####################hydra-fuzz################### -import("//build/config/features.gni") -import("//build/ohos.gni") -import("//build/test.gni") - -##############################fuzztest########################################## -ohos_fuzztest("SecurityModeParserFuzzTest") { - module_out_path = "imf/imf" - - fuzz_config_file = - "${inputmethod_path}/test/fuzztest/securitymodeparser_fuzzer" - - include_dirs = [ - "${inputmethod_path}/services/adapter/settings_data_provider/include", - "${inputmethod_path}/services/adapter/ime_connection_manager/include", - "${inputmethod_path}/services/include", - "${inputmethod_path}/test/common", - ] - - cflags = [ - "-g", - "-O0", - "-Wno-unused-variable", - "-fno-omit-frame-pointer", - ] - - sources = [ - "${inputmethod_path}/services/adapter/settings_data_provider/src/security_mode_parser.cpp", - "securitymodeparser_fuzzer.cpp", - ] - - deps = [ - "${inputmethod_path}/interfaces/inner_api/inputmethod_controller:inputmethod_client_static", - "${inputmethod_path}/services:inputmethod_service_static", - "${inputmethod_path}/test/common:inputmethod_test_common", - ] - - external_deps = [ - "ability_runtime:ability_manager", - "bundle_framework:appexecfwk_core", - "c_utils:utils", - "data_share:datashare_common", - "data_share:datashare_consumer", - "hilog:libhilog", - "input:libmmi-client", - "ipc:ipc_single", - ] -} - -############################################################################### -group("fuzztest") { - testonly = true - deps = [] - deps += [ - # deps file - ":SecurityModeParserFuzzTest", - ] -} -############################################################################### diff --git a/test/fuzztest/securitymodeparser_fuzzer/corpus/init b/test/fuzztest/securitymodeparser_fuzzer/corpus/init deleted file mode 100644 index bc977bd9738ee9a70b362067f57a9c63d3adb801..0000000000000000000000000000000000000000 --- a/test/fuzztest/securitymodeparser_fuzzer/corpus/init +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -FUZZ \ No newline at end of file diff --git a/test/fuzztest/securitymodeparser_fuzzer/project.xml b/test/fuzztest/securitymodeparser_fuzzer/project.xml deleted file mode 100644 index 6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec..0000000000000000000000000000000000000000 --- a/test/fuzztest/securitymodeparser_fuzzer/project.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - 1000 - - 300 - - 4096 - - diff --git a/test/fuzztest/securitymodeparser_fuzzer/securitymodeparser_fuzzer.cpp b/test/fuzztest/securitymodeparser_fuzzer/securitymodeparser_fuzzer.cpp deleted file mode 100644 index 6359a728de0136bfbc1718ca3306d65ee5a8f372..0000000000000000000000000000000000000000 --- a/test/fuzztest/securitymodeparser_fuzzer/securitymodeparser_fuzzer.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#define private public -#define protected public -#include "security_mode_parser.h" -#undef private - -#include "securitymodeparser_fuzzer.h" - -using namespace OHOS::MiscServices; -namespace OHOS { -void FuzzInitialize(const int32_t userId) -{ - SecurityModeParser::GetInstance()->Initialize(userId); -} - -void FuzzGetSecurityMode(const std::string &bundleName, const int32_t userId) -{ - SecurityModeParser::GetInstance()->GetSecurityMode(bundleName, userId); -} - -void FuzzUpdateFullModeList(const int32_t userId) -{ - SecurityModeParser::GetInstance()->UpdateFullModeList(userId); -} - -void FuzzParseSecurityMode(const std::string &bundleName, const int32_t userId) -{ - SecurityModeParser::GetInstance()->ParseSecurityMode(bundleName, userId); -} - -void FuzzIsFullMode(const std::string &bundleName) -{ - SecurityModeParser::GetInstance()->IsFullMode(bundleName); -} - -} // namespace OHOS -/* Fuzzer entry point */ -extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) -{ - if (data == nullptr) { - return 0; - } - /* Run your code on data */ - const int32_t userId = static_cast(size); - std::string fuzzedString(reinterpret_cast(data), size); - - OHOS::FuzzInitialize(userId); - OHOS::FuzzGetSecurityMode(fuzzedString, userId); - OHOS::FuzzUpdateFullModeList(userId); - OHOS::FuzzParseSecurityMode(fuzzedString, userId); - OHOS::FuzzIsFullMode(fuzzedString); - return 0; -} diff --git a/test/fuzztest/securitymodeparser_fuzzer/securitymodeparser_fuzzer.h b/test/fuzztest/securitymodeparser_fuzzer/securitymodeparser_fuzzer.h deleted file mode 100644 index 8e1752ec251269ab9857295430bb2e18f7e5f062..0000000000000000000000000000000000000000 --- a/test/fuzztest/securitymodeparser_fuzzer/securitymodeparser_fuzzer.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef TEST_FUZZTEST_SYSTEMABILITYSTUB_FUZZER_SYSTEMABILITYSTUB_FUZZER_H -#define TEST_FUZZTEST_SYSTEMABILITYSTUB_FUZZER_SYSTEMABILITYSTUB_FUZZER_H - -#define FUZZ_PROJECT_NAME "securitymodeparser_fuzzer" - -#endif // TEST_FUZZTEST_SYSTEMABILITYSTUB_FUZZER_SYSTEMABILITYSTUB_FUZZER_H diff --git a/test/fuzztest/settingsdatautils_fuzzer/settingsdatautils_fuzzer.cpp b/test/fuzztest/settingsdatautils_fuzzer/settingsdatautils_fuzzer.cpp index 0ffe563fc9fd2015615980ec760a21769e872ba3..d4e609516f3676b4cfbcaf6f6e4cacc6bb3745d9 100644 --- a/test/fuzztest/settingsdatautils_fuzzer/settingsdatautils_fuzzer.cpp +++ b/test/fuzztest/settingsdatautils_fuzzer/settingsdatautils_fuzzer.cpp @@ -32,21 +32,23 @@ void FuzzDataShareHelper() SettingsDataUtils::GetInstance()->ReleaseDataShareHelper(helper); } -void FuzzCreateAndRegisterObserver(const std::string &key, SettingsDataObserver::CallbackFunc func) +void FuzzCreateAndRegisterObserver( + const std::string &uriProxy, const std::string &key, SettingsDataObserver::CallbackFunc func) { - SettingsDataUtils::GetInstance()->CreateAndRegisterObserver(key, func); + SettingsDataUtils::GetInstance()->CreateAndRegisterObserver(uriProxy, key, func); } -void FuzzRegisterObserver(const std::string &key, SettingsDataObserver::CallbackFunc &func) +void FuzzRegisterObserver(const std::string &uriProxy, const std::string &key, SettingsDataObserver::CallbackFunc &func) { sptr observer = new SettingsDataObserver(key, func); - SettingsDataUtils::GetInstance()->RegisterObserver(observer); + SettingsDataUtils::GetInstance()->RegisterObserver(uriProxy, observer); } -void FuzzUnregisterObserver(const std::string &key, SettingsDataObserver::CallbackFunc &func) +void FuzzUnregisterObserver( + const std::string &uriProxy, const std::string &key, SettingsDataObserver::CallbackFunc &func) { sptr observer = new SettingsDataObserver(key, func); - SettingsDataUtils::GetInstance()->UnregisterObserver(observer); + SettingsDataUtils::GetInstance()->UnregisterObserver(uriProxy, observer); } void FuzzGenerateTargetUri(const std::string &key) @@ -64,9 +66,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) OHOS::FuzzGetToken(); OHOS::FuzzDataShareHelper(); - OHOS::FuzzCreateAndRegisterObserver(fuzzedString, func); - OHOS::FuzzRegisterObserver(fuzzedString, func); - OHOS::FuzzUnregisterObserver(fuzzedString, func); + OHOS::FuzzCreateAndRegisterObserver(fuzzedString, fuzzedString, func); + OHOS::FuzzRegisterObserver(fuzzedString, fuzzedString, func); + OHOS::FuzzUnregisterObserver(fuzzedString, fuzzedString, func); OHOS::FuzzGenerateTargetUri(fuzzedString); return 0; } \ No newline at end of file diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 96095a443b76df8963e8e8a3c975d4dfc098e5a4..1d18b611e7491b34c4096679f5ec1a0b4d2d5c5c 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -20,11 +20,11 @@ group("unittest") { if (!use_libfuzzer) { deps += [ - "cpp_test:EnableImeDataParseTest", "cpp_test:FullImeInfoManagerTest", "cpp_test:ITypesUtilTest", "cpp_test:IdentityCheckerTest", "cpp_test:ImeControllerCpaiTest", + "cpp_test:ImeEnabledInfoManagerTest", "cpp_test:ImeFreezeManagerTest", "cpp_test:ImeProxyTest", "cpp_test:ImeSystemChannelTest", @@ -44,7 +44,6 @@ group("unittest") { "cpp_test:JsonOperateTest", "cpp_test:NewImeSwitchTest", "cpp_test:OnDemandStartStopSaTest", - "cpp_test:SecurityModeParseTest", "cpp_test:TaskManagerTest", "cpp_test:TextListenerInnerApiTest", "cpp_test:VirtualListenerTest", diff --git a/test/unittest/cpp_test/BUILD.gn b/test/unittest/cpp_test/BUILD.gn index e64f0e6ea9869f75cd0c3a1265420e54c57000c2..1ea1257c837a558f577602944875fb01c1a45d31 100644 --- a/test/unittest/cpp_test/BUILD.gn +++ b/test/unittest/cpp_test/BUILD.gn @@ -890,110 +890,6 @@ ohos_unittest("ImeProxyTest") { } } -ohos_unittest("EnableImeDataParseTest") { - branch_protector_ret = "pac_ret" - sanitize = { - cfi = true - cfi_cross_dso = true - debug = false - } - module_out_path = module_output_path - - include_dirs = [ - "${inputmethod_path}/test/unittest/cpp_test/mock", - "${inputmethod_path}/services/adapter/settings_data_provider/include", - "${inputmethod_path}/services/adapter/settings_data_provider/common/include", - "${inputmethod_path}/frameworks/native/inputmethod_controller/include", - ] - - sources = [ - "${inputmethod_path}/services/adapter/settings_data_provider/common/src/settings_data_observer.cpp", - "${inputmethod_path}/services/adapter/settings_data_provider/common/src/settings_data_utils.cpp", - "${inputmethod_path}/services/adapter/settings_data_provider/src/enable_ime_data_parser.cpp", - "mock/datashare_helper.cpp", - "mock/global.cpp", - "mock/ime_info_inquirer.cpp", - "src/enable_ime_data_parse_test.cpp", - ] - - deps = [ - "${inputmethod_path}/services/json:imf_json_static", - "//third_party/googletest:gmock", - "//third_party/googletest:gtest_main", - ] - - external_deps = [ - "ability_base:zuri", - "ability_runtime:dataobs_manager", - "bundle_framework:appexecfwk_core", - "c_utils:utils", - "data_share:datashare_common", - "data_share:datashare_consumer", - "hilog:libhilog", - "ipc:ipc_single", - "samgr:samgr_proxy", - ] -} - -ohos_unittest("SecurityModeParseTest") { - branch_protector_ret = "pac_ret" - sanitize = { - cfi = true - cfi_cross_dso = true - debug = false - } - module_out_path = module_output_path - - include_dirs = [ - "${inputmethod_path}/test/unittest/cpp_test/mock", - "${inputmethod_path}/services/include", - "${inputmethod_path}/services/identity_checker/include", - "${inputmethod_path}/services/adapter/ime_connection_manager/include", - "${inputmethod_path}/services/adapter/settings_data_provider/include", - "${inputmethod_path}/services/adapter/settings_data_provider/common/include", - "${inputmethod_path}/frameworks/native/inputmethod_controller/include", - "${inputmethod_path}/frameworks/native/inputmethod_ability/include", - ] - - sources = [ - "${inputmethod_path}/frameworks/native/inputmethod_controller/src/input_client_info.cpp", - "${inputmethod_path}/frameworks/native/inputmethod_controller/src/input_method_tools.cpp", - "${inputmethod_path}/frameworks/native/inputmethod_controller/src/input_method_utils.cpp", - "${inputmethod_path}/services/adapter/ime_connection_manager/src/ime_connection.cpp", - "${inputmethod_path}/services/adapter/settings_data_provider/common/src/settings_data_observer.cpp", - "${inputmethod_path}/services/adapter/settings_data_provider/common/src/settings_data_utils.cpp", - "${inputmethod_path}/services/adapter/settings_data_provider/src/security_mode_parser.cpp", - "${inputmethod_path}/services/src/input_control_channel_service_impl.cpp", - "mock/datashare_helper.cpp", - "src/security_mode_parser_test.cpp", - ] - - deps = [ - "${inputmethod_path}/interfaces/inner_api/inputmethod_ability:input_control_channel_proxy", - "${inputmethod_path}/services:inputmethod_service_static", - "//third_party/googletest:gmock", - "//third_party/googletest:gtest_main", - ] - - external_deps = [ - "ability_base:zuri", - "ability_runtime:ability_manager", - "ability_runtime:dataobs_manager", - "access_token:libaccesstoken_sdk", - "bundle_framework:appexecfwk_core", - "c_utils:utils", - "data_share:datashare_common", - "data_share:datashare_consumer", - "hilog:libhilog", - "input:libmmi-client", - "ipc:ipc_core", - "ipc:ipc_single", - "safwk:system_ability_fwk", - "samgr:samgr_proxy", - "window_manager:libdm", - ] -} - ohos_unittest("TaskManagerTest") { branch_protector_ret = "pac_ret" sanitize = { @@ -1258,16 +1154,21 @@ ohos_unittest("FullImeInfoManagerTest") { sources = [ "${inputmethod_path}/services/src/full_ime_info_manager.cpp", + "mock/ime_enabled_info_manager.cpp", "mock/ime_info_inquirer.cpp", "src/full_ime_info_manager_test.cpp", ] include_dirs = [ + "${inputmethod_path}/common/include", "${inputmethod_path}/frameworks/native/inputmethod_controller/include", "${inputmethod_path}/test/unittest/cpp_test/mock", ] + deps = [ "${inputmethod_path}/common:inputmethod_common" ] + external_deps = [ "c_utils:utils", + "eventhandler:libeventhandler", "googletest:gtest_main", "hilog:libhilog", "input:libmmi-client", @@ -1435,3 +1336,41 @@ ohos_unittest("WindowAdapterTest") { external_deps += [ "window_manager:libwm" ] } } + +ohos_unittest("ImeEnabledInfoManagerTest") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + } + module_out_path = module_output_path + include_dirs = [ "${inputmethod_path}/common/include" ] + sources = [ "src/ime_enabled_info_manager_test.cpp" ] + + configs = [ ":module_private_config" ] + deps = [ + "${inputmethod_path}/services:inputmethod_service_static", + "${inputmethod_path}/services/adapter/settings_data_provider:settings_data_static", + "${inputmethod_path}/services/json:imf_json_static", + "${inputmethod_path}/test/unittest/cpp_test/common:inputmethod_tdd_util", + ] + + external_deps = [ + "ability_base:want", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "data_share:datashare_common", + "data_share:datashare_consumer", + "googletest:gtest_main", + "hilog:libhilog", + "init:libbeget_proxy", + "init:libbegetutil", + "resource_management:global_resmgr", + ] + if (window_manager_use_sceneboard) { + external_deps += [ "window_manager:libwm_lite" ] + } else { + external_deps += [ "window_manager:libwm" ] + } +} diff --git a/test/unittest/cpp_test/common/include/tdd_util.h b/test/unittest/cpp_test/common/include/tdd_util.h index e67525217eb57bbc69b027ffbd9781838a16fa12..e2c96d6ef9b0f03ca880dc05239d2642abd8e897 100644 --- a/test/unittest/cpp_test/common/include/tdd_util.h +++ b/test/unittest/cpp_test/common/include/tdd_util.h @@ -55,6 +55,10 @@ public: static bool KillImsaProcess(); static void PushEnableImeValue(const std::string &key, const std::string &value); static void GrantNativePermission(); + static void DeleteGlobalTable(const std::string &key); + static void DeleteUserTable(int32_t userId, const std::string &key); + static void GenerateGlobalTable(const std::string &key, const std::string &content); + static void GenerateUserTable(int32_t userId, const std::string &key, const std::string &content); static int32_t GetEnableData(std::string &value); static void InitWindow(bool isShow); static void DestroyWindow(); @@ -79,6 +83,8 @@ public: private: static sptr GetBundleMgr(); static int GetUserIdByBundleName(const std::string &bundleName, const int currentUserId); + static void DeleteTable(const std::string &key, const std::string &uriProxy); + static void GenerateTable(const std::string &key, const std::string &uriProxy, const std::string &content); static uint64_t selfTokenID_; static uint64_t testTokenID_; static int32_t userID_; diff --git a/test/unittest/cpp_test/common/src/tdd_util.cpp b/test/unittest/cpp_test/common/src/tdd_util.cpp index 0780046b11fa049411e76e03182e461d44e944ea..38863f3d2b788b0ddf5671a3064c4d5a967e7fed 100644 --- a/test/unittest/cpp_test/common/src/tdd_util.cpp +++ b/test/unittest/cpp_test/common/src/tdd_util.cpp @@ -14,7 +14,6 @@ */ #define private public #define protected public -#include "enable_ime_data_parser.h" #include "full_ime_info_manager.h" #include "settings_data_utils.h" #include "user_session_manager.h" @@ -66,6 +65,7 @@ std::shared_ptr> FocusChangedListenerTestImpl::isFocused_ = std::make_shared>(MAX_TIMEOUT_WAIT_FOCUS, false); std::shared_ptr> FocusChangedListenerTestImpl::unFocused_ = std::make_shared>(MAX_TIMEOUT_WAIT_FOCUS, false); +static constexpr const char *ENABLE_IME = "settings.inputmethod.enable_ime"; void FocusChangedListenerTestImpl::OnFocused(const sptr &focusChangeInfo) { IMSA_HILOGI("get onFocus information from window manager."); @@ -322,10 +322,55 @@ void TddUtil::PushEnableImeValue(const std::string &key, const std::string &valu IMSA_HILOGI("ReleaseDataShareHelper isSuccess: %{public}d", ret); } +void TddUtil::DeleteGlobalTable(const std::string &key) +{ + IMSA_HILOGI("key: %{public}s", key.c_str()); + std::string uriProxy = SETTING_URI_PROXY; + DeleteTable(key, uriProxy); +} + +void TddUtil::DeleteUserTable(int32_t userId, const std::string &key) +{ + IMSA_HILOGI("userId:%{public}d, key:%{public}s", userId, key.c_str()); + std::string uriProxy = SETTINGS_USER_DATA_URI + std::to_string(userId) + "?Proxy=true"; + DeleteTable(key, uriProxy); +} + +void TddUtil::DeleteTable(const std::string &key, const std::string &uriProxy) +{ + auto helper = SettingsDataUtils::GetInstance()->CreateDataShareHelper(uriProxy); + if (helper == nullptr) { + IMSA_HILOGE("helper is nullptr."); + return; + } + DataShare::DataSharePredicates predicates; + predicates.EqualTo(SETTING_COLUMN_KEYWORD, key); + Uri uri(SettingsDataUtils::GetInstance()->GenerateTargetUri(uriProxy, key)); + auto result = helper->DeleteEx(uri, predicates); + IMSA_HILOGI("Delete ret: [%{public}d, %{public}d]", result.first, result.second); + bool ret = SettingsDataUtils::GetInstance()->ReleaseDataShareHelper(helper); + IMSA_HILOGI("ReleaseDataShareHelper isSuccess: %{public}d", ret); +} + +void TddUtil::GenerateGlobalTable(const std::string &key, const std::string &content) +{ + std::string uriProxy = SETTING_URI_PROXY; + GenerateTable(key, uriProxy, content); +} +void TddUtil::GenerateUserTable(int32_t userId, const std::string &key, const std::string &content) +{ + std::string uriProxy = SETTINGS_USER_DATA_URI + std::to_string(userId) + "?Proxy=true"; + GenerateTable(key, uriProxy, content); +} + +void TddUtil::GenerateTable(const std::string &key, const std::string &uriProxy, const std::string &content) +{ + SettingsDataUtils::GetInstance()->SetStringValue(uriProxy, key, content); +} + int32_t TddUtil::GetEnableData(std::string &value) { - auto ret = - SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, EnableImeDataParser::ENABLE_IME, value); + auto ret = SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, ENABLE_IME, value); if (ret == ErrorCode::NO_ERROR) { IMSA_HILOGI("success, value: %{public}s", value.c_str()); } diff --git a/test/unittest/cpp_test/mock/full_ime_info_manager.h b/test/unittest/cpp_test/mock/full_ime_info_manager.h index e6cfb9c3c8cd05fc5f8b42c8e2aa25d68b0c248c..3539b318db7a0324ec366cbd335543311010ce23 100644 --- a/test/unittest/cpp_test/mock/full_ime_info_manager.h +++ b/test/unittest/cpp_test/mock/full_ime_info_manager.h @@ -16,10 +16,13 @@ #ifndef SERVICES_INCLUDE_FULL_IME_INFO_MANAGER_H #define SERVICES_INCLUDE_FULL_IME_INFO_MANAGER_H +#include #include +#include #include -#include "global.h" +#include "event_handler.h" +#include "ime_enabled_info_manager.h" #include "input_method_property.h" #include "timer.h" namespace OHOS { @@ -28,25 +31,36 @@ namespace MiscServices { class FullImeInfoManager { public: static FullImeInfoManager &GetInstance(); - int32_t Init(); // osAccount start/bundle scan finished/regular update - int32_t Add(int32_t userId); // user switched - int32_t Update(); // language change - int32_t Delete(int32_t userId); // user removed - int32_t Add(int32_t userId, const std::string &bundleName); // package added + int32_t Init(); // regular Init/boot complete/data share ready + int32_t Add(int32_t userId); // user switched + int32_t Update(); // language change + int32_t Delete(int32_t userId); // user removed + int32_t Add(int32_t userId, const std::string &bundleName); // package added int32_t Delete(int32_t userId, const std::string &bundleName); // package removed int32_t Update(int32_t userId, const std::string &bundleName); // package changed - std::vector Get(int32_t userId); + int32_t UpdateEnabledStatus( + int32_t userId, const std::string &bundleName, const std::string &extensionName, EnabledStatus status); + int32_t GetEnabledState(int32_t userId, const std::string &bundleName, EnabledStatus &status); + int32_t GetEnabledStates(int32_t userId, std::vector &props); + void SetEnabledStatusChangedHandler(EnabledStatusChangedHandler handler); + bool IsDefaultFullMode(int32_t userId, const std::string &bundleName); + void SetEventHandler(const std::shared_ptr &eventHandler); + std::vector GetWithOutEnabledStatus(int32_t userId); std::string Get(int32_t userId, uint32_t tokenId); - bool Get(const std::string &bundleName, int32_t userId, FullImeInfo &fullImeInfo); + bool Get(int32_t userId, const std::string &bundleName, FullImeInfo &fullImeInfo); bool Has(int32_t userId, const std::string &bundleName); private: FullImeInfoManager(); ~FullImeInfoManager(); + int32_t AddUser(int32_t userId, std::vector &infos); + int32_t AddPackage(int32_t userId, const std::string &bundleName, FullImeInfo &info); + int32_t DeletePackage(int32_t userId, const std::string &bundleName); std::mutex lock_; std::map> fullImeInfos_; - Utils::Timer timer_ { "imeInfoCacheInitTimer" }; - uint32_t timerId_ { 0 }; + Utils::Timer timer_{ "imeInfoCacheInitTimer" }; + uint32_t timerId_{ 0 }; + std::shared_ptr eventHandler_{ nullptr }; }; } // namespace MiscServices } // namespace OHOS diff --git a/test/unittest/cpp_test/mock/global.cpp b/test/unittest/cpp_test/mock/global.cpp deleted file mode 100644 index d5c9410a4a183b00f39705cf79c95e1976ddc5d9..0000000000000000000000000000000000000000 --- a/test/unittest/cpp_test/mock/global.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "global.h" - -#include -#include - -namespace OHOS { -namespace MiscServices { -void LogTimeStamp() -{ - struct timeval tv; - gettimeofday(&tv, nullptr); - struct tm nowTime; - localtime_r(&tv.tv_sec, &nowTime); - int32_t millSec = 1000; - printf("%02d-%02d %02d:%02d:%02d.%03d\t", nowTime.tm_mon, nowTime.tm_mday, nowTime.tm_hour, nowTime.tm_min, - nowTime.tm_sec, static_cast(tv.tv_usec) / millSec); -} -} // namespace MiscServices -} // namespace OHOS diff --git a/test/unittest/cpp_test/mock/global.h b/test/unittest/cpp_test/mock/global.h deleted file mode 100644 index ebd8be5412c74d7034eb638d51272352c9310c1f..0000000000000000000000000000000000000000 --- a/test/unittest/cpp_test/mock/global.h +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (C) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef SERVICES_INCLUDE_GLOBAL_H -#define SERVICES_INCLUDE_GLOBAL_H - -#include - -#include -#include - -#include "hilog/log.h" -#include "ipc_object_stub.h" -#include "iremote_broker.h" -#include "peer_holder.h" -#include "refbase.h" - -namespace OHOS { -namespace MiscServices { -using BRemoteObject = IPCObjectStub; - -#define LOG_INFO(fmt, args...) \ - LogTimeStamp(); \ - printf("I %s:%d %s - " fmt, basename(__FILE__), __LINE__, __FUNCTION__, ##args) - -#define LOG_ERROR(fmt, args...) \ - LogTimeStamp(); \ - printf("E %s:%d %s - " fmt, basename(__FILE__), __LINE__, __FUNCTION__, ##args) - -#define LOG_WARNING(fmt, args...) \ - LogTimeStamp(); \ - printf("W %s:%d %s - " fmt, basename(__FILE__), __LINE__, __FUNCTION__, ##args) - -#if DEBUG -#define LOG_DEBUG(fmt, args...) \ - LogTimeStamp(); \ - printf("D %s:%d %s - " fmt, basename(__FILE__), __LINE__, __FUNCTION__, ##args) -#else -#define LOG_DEBUG(fmt, args...) -#endif - -void LogTimeStamp(); - -// Error Code -namespace ErrorCode { -// Error Code definition in the input method management system -constexpr int32_t NO_ERROR = 0; - -// system service error -constexpr int32_t ERROR_NULL_POINTER = 1; -constexpr int32_t ERROR_PACKAGE_MANAGER = 12; -constexpr int32_t ERROR_KEYWORD_NOT_FOUND = 26; -constexpr int32_t ERROR_ENABLE_IME = 27; - -constexpr int32_t ERROR_ENABLE_SECURITY_MODE = 32; -}; // namespace ErrorCode - -constexpr HiviewDFX::HiLogLabel g_SMALL_SERVICES_LABEL = { LOG_CORE, 0xD001C10, "ImsaKit" }; - -#define IMSA_HILOGD(fmt, ...) \ - (void)HILOG_IMPL(LOG_CORE, LOG_DEBUG, OHOS::MiscServices::g_SMALL_SERVICES_LABEL.domain, \ - OHOS::MiscServices::g_SMALL_SERVICES_LABEL.tag, "line: %{public}d, function: %{public}s," fmt, __LINE__, \ - __FUNCTION__, ##__VA_ARGS__) -#define IMSA_HILOGE(fmt, ...) \ - (void)HILOG_IMPL(LOG_CORE, LOG_ERROR, OHOS::MiscServices::g_SMALL_SERVICES_LABEL.domain, \ - OHOS::MiscServices::g_SMALL_SERVICES_LABEL.tag, "line: %{public}d, function: %{public}s," fmt, __LINE__, \ - __FUNCTION__, ##__VA_ARGS__) -#define IMSA_HILOGF(fmt, ...) \ - (void)HILOG_IMPL(LOG_CORE, LOG_FATAL, OHOS::MiscServices::g_SMALL_SERVICES_LABEL.domain, \ - OHOS::MiscServices::g_SMALL_SERVICES_LABEL.tag, "line: %{public}d, function: %{public}s," fmt, __LINE__, \ - __FUNCTION__, ##__VA_ARGS__) -#define IMSA_HILOGI(fmt, ...) \ - (void)HILOG_IMPL(LOG_CORE, LOG_INFO, OHOS::MiscServices::g_SMALL_SERVICES_LABEL.domain, \ - OHOS::MiscServices::g_SMALL_SERVICES_LABEL.tag, "line: %{public}d, function: %{public}s," fmt, __LINE__, \ - __FUNCTION__, ##__VA_ARGS__) -#define IMSA_HILOGW(fmt, ...) \ - (void)HILOG_IMPL(LOG_CORE, LOG_WARN, OHOS::MiscServices::g_SMALL_SERVICES_LABEL.domain, \ - OHOS::MiscServices::g_SMALL_SERVICES_LABEL.tag, "line: %{public}d, function: %{public}s," fmt, __LINE__, \ - __FUNCTION__, ##__VA_ARGS__) -} // namespace MiscServices -} // namespace OHOS -#endif // SERVICES_INCLUDE_GLOBAL_H diff --git a/test/unittest/cpp_test/mock/ime_enabled_info_manager.cpp b/test/unittest/cpp_test/mock/ime_enabled_info_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2fb08d4f0491a175e7e8a7b019facc46d2b02b4d --- /dev/null +++ b/test/unittest/cpp_test/mock/ime_enabled_info_manager.cpp @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ime_enabled_info_manager.h" + +#include +#include +#include +#include +#include + +#include "global.h" + +namespace OHOS { +namespace MiscServices { +using namespace std::chrono; +ImeEnabledInfoManager &ImeEnabledInfoManager::GetInstance() +{ + static ImeEnabledInfoManager instance; + return instance; +} + +ImeEnabledInfoManager::~ImeEnabledInfoManager() +{ +} + +void ImeEnabledInfoManager::SetEnabledStatusChangedHandler(EnabledStatusChangedHandler handler) +{ +} + +void ImeEnabledInfoManager::SetEventHandler(const std::shared_ptr &eventHandler) +{ +} + +int32_t ImeEnabledInfoManager::Init(const std::map> &fullImeInfos) +{ + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::Delete(int32_t userId) +{ + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::Add(int32_t userId, const std::vector &imeInfos) +{ + return ErrorCode::NO_ERROR; +} +int32_t ImeEnabledInfoManager::Add(int32_t userId, const FullImeInfo &imeInfo) +{ + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::Delete(int32_t userId, const std::string &bundleName) +{ + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::Update( + int32_t userId, const std::string &bundleName, const std::string &extensionName, EnabledStatus status) +{ + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::GetEnabledState(int32_t userId, const std::string &bundleName, EnabledStatus &status) +{ + return ErrorCode::NO_ERROR; +} + +int32_t ImeEnabledInfoManager::GetEnabledStates(int32_t userId, std::vector &props) +{ + return ErrorCode::NO_ERROR; +} + +bool ImeEnabledInfoManager::IsDefaultFullMode(int32_t userId, const std::string &bundleName) +{ + return true; +} + +void ImeEnabledInfoManager::OnFullExperienceTableChanged(int32_t userId) +{ +} +} // namespace MiscServices +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/cpp_test/mock/ime_enabled_info_manager.h b/test/unittest/cpp_test/mock/ime_enabled_info_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..80a2451cc30ba26629401d5e5734b19f868fe135 --- /dev/null +++ b/test/unittest/cpp_test/mock/ime_enabled_info_manager.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IME_ENABLED_INFO_MANAGER_H +#define IME_ENABLED_INFO_MANAGER_H + +#include +#include +#include +#include +#include +#include +#include + +#include "event_handler.h" +#include "input_method_property.h" +#include "input_method_status.h" + +namespace OHOS { +namespace MiscServices { +// old ime enabled cfg +struct EnabledImeCfg { +}; +// old ime full experience cfg +struct SecurityModeCfg { +}; + +struct ImeEnabledInfo { +}; +struct ImeEnabledCfg { +}; +using EnabledStatusChangedHandler = + std::function; +class ImeEnabledInfoManager { +public: + static constexpr const char *ENABLE_IME = "settings.inputmethod.enable_ime"; + static constexpr const char *SECURITY_MODE = "settings.inputmethod.full_experience"; + static ImeEnabledInfoManager &GetInstance(); + void SetEnabledStatusChangedHandler(EnabledStatusChangedHandler handler); + void SetEventHandler(const std::shared_ptr &eventHandler); + int32_t Init(const std::map> &fullImeInfos); + int32_t Add(int32_t userId, const std::vector &imeInfos); + int32_t Delete(int32_t userId); + int32_t Add(int32_t userId, const FullImeInfo &imeInfo); + int32_t Delete(int32_t userId, const std::string &bundleName); + int32_t Update( + int32_t userId, const std::string &bundleName, const std::string &extensionName, EnabledStatus status); + int32_t GetEnabledState(int32_t userId, const std::string &bundleName, EnabledStatus &status); + int32_t GetEnabledStates(int32_t userId, std::vector &props); + bool IsDefaultFullMode(int32_t userId, const std::string &bundleName); + void OnFullExperienceTableChanged(int32_t userId); // add for compatibility +private: + ImeEnabledInfoManager() = default; + ~ImeEnabledInfoManager(); +}; +} // namespace MiscServices +} // namespace OHOS + +#endif // IME_ENABLED_INFO_MANAGER_H \ No newline at end of file diff --git a/test/unittest/cpp_test/src/enable_ime_data_parse_test.cpp b/test/unittest/cpp_test/src/enable_ime_data_parse_test.cpp deleted file mode 100644 index 89a007a14c7bf8b4113b93b59183d5bcda24b1a8..0000000000000000000000000000000000000000 --- a/test/unittest/cpp_test/src/enable_ime_data_parse_test.cpp +++ /dev/null @@ -1,428 +0,0 @@ -/* - * Copyright (C) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define private public -#define protected public -#include "enable_ime_data_parser.h" -#include "ime_info_inquirer.h" - -#undef private - -#include - -using namespace testing::ext; -using namespace OHOS::DataShare; -namespace OHOS { -namespace MiscServices { -const std::string IME_KEY = "settings.inputmethod.enable_ime"; -class EnableImeDataParseTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); - static std::shared_ptr helper_; - static std::shared_ptr resultSet_; - static constexpr int32_t USER_ID = 100; -}; -std::shared_ptr EnableImeDataParseTest::helper_; -std::shared_ptr EnableImeDataParseTest::resultSet_; -constexpr uint32_t USER_100_TOTAL_COUNT = 3; -constexpr uint32_t USER_101_TOTAL_COUNT = 1; -void EnableImeDataParseTest::SetUpTestCase(void) -{ - std::vector columns = { "VALUE" }; - helper_ = DataShare::DataShareHelper::Creator(nullptr, "tsetUri", "tsetUri"); - DataSharePredicates predicates; - Uri uri("tsetUri"); - resultSet_ = helper_->Query(uri, predicates, columns); - EnableImeDataParser::GetInstance()->Initialize(USER_ID); - ImeInfoInquirer::currentIme_ = std::make_shared(); - ImeInfoInquirer::defaultImeProperty_ = std::make_shared(); - ImeInfoInquirer::defaultIme_ = std::make_shared(); - ImeInfoInquirer::GetInstance().GetDefaultImeCfgProp()->name = "defaultImeName"; - ImeInfoInquirer::GetInstance().GetDefaultImeCfgProp()->id = "defaultImeId"; -} - -void EnableImeDataParseTest::TearDownTestCase(void) { } - -void EnableImeDataParseTest::SetUp() -{ - resultSet_->strValue_ = "{\"enableImeList\" : {\"100\" : [ \"xiaoyiIme\", \"baiduIme\", " - "\"sougouIme\"],\"101\" : [\"sougouIme\"]}}"; - - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->name = "defaultImeName"; - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->id = "defaultImeId"; - EnableImeDataParser::GetInstance()->enableList_.clear(); -} - -void EnableImeDataParseTest::TearDown() { } - -/** - * @tc.name: testGetEnableData_001 - * @tc.desc: Get 100 user enable ime - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testGetEnableData_001, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testGetEnableData_001 START"); - std::vector enableVec; - int32_t ret = - EnableImeDataParser::GetInstance()->GetEnableData(IME_KEY, enableVec, EnableImeDataParseTest::USER_ID); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(enableVec.size(), USER_100_TOTAL_COUNT); - if (enableVec.size() == USER_100_TOTAL_COUNT) { - EXPECT_EQ(enableVec[0], "xiaoyiIme"); - EXPECT_EQ(enableVec[1], "baiduIme"); - EXPECT_EQ(enableVec[2], "sougouIme"); - } -} - -/** - * @tc.name: testGetEnableData_002 - * @tc.desc: Get 101 user enable ime - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testGetEnableData_002, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testGetEnableData_002 START"); - std::vector enableVec; - int32_t ret = EnableImeDataParser::GetInstance()->GetEnableData(IME_KEY, enableVec, 101); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(enableVec.size(), USER_101_TOTAL_COUNT); - if (enableVec.size() == USER_101_TOTAL_COUNT) { - EXPECT_EQ(enableVec[0], "sougouIme"); - } -} - -/** - * @tc.name: testGetEnableData_003 - * @tc.desc: Get 100 user enable ime while enable list is empty. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testGetEnableData_003, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testGetEnableData_003 START"); - EnableImeDataParseTest::resultSet_->strValue_ = "{\"enableImeList\" : {\"100\" : []}}"; - std::vector enableVec; - int32_t ret = - EnableImeDataParser::GetInstance()->GetEnableData(IME_KEY, enableVec, EnableImeDataParseTest::USER_ID); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_TRUE(enableVec.empty()); -} - -/** - * @tc.name: testCheckNeedSwitch_001 - * @tc.desc: Check need switch for enable list change, current ime is default - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testCheckNeedSwitch_001, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testCheckNeedSwitch_001 START"); - SwitchInfo switchInfo; - EnableImeDataParser::GetInstance()->enableList_.clear(); - bool ret = - EnableImeDataParser::GetInstance()->CheckNeedSwitch(IME_KEY, switchInfo, EnableImeDataParseTest::USER_ID); - EXPECT_FALSE(ret); -} - -/** - * @tc.name: testCheckNeedSwitch_002 - * @tc.desc: Check need switch for enable list change - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testCheckNeedSwitch_002, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testCheckNeedSwitch_002 START"); - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->name = "xiaoyiIme"; - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->id = "xiaoyiImeId"; - EnableImeDataParseTest::resultSet_->strValue_ = "{\"enableImeList\" : {\"100\" : [\"baiduIme\", " - "\"sougouIme\"],\"101\" : [\"sougouIme\"]}}"; - SwitchInfo switchInfo; - EnableImeDataParser::GetInstance()->enableList_[IME_KEY].push_back("xiaoyiIm" - "e"); - bool ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch(IME_KEY, switchInfo, USER_ID); - EXPECT_TRUE(ret); - EXPECT_EQ(switchInfo.bundleName, EnableImeDataParser::GetInstance()->defaultImeInfo_->name); -} - -/** - * @tc.name: testCheckNeedSwitch_003 - * @tc.desc: Check need switch for enable list change - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testCheckNeedSwitch_003, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testCheckNeedSwitch_003 START"); - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->name = "xiaoyiIme"; - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->id = "xiaoyiImeId"; - EnableImeDataParseTest::resultSet_->strValue_ = "{\"enableImeList\" : {\"100\" : [ \"xiaoyiIme\", \"baiduIme\", " - "\"sougouIme\"],\"101\" : " - "[\"sougouIme\"]}}"; - SwitchInfo switchInfo; - bool ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch(IME_KEY, switchInfo, USER_ID); - EXPECT_FALSE(ret); -} - -/** - * @tc.name: testCheckNeedSwitch_004 - * @tc.desc: Check need switch for enable list change - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testCheckNeedSwitch_004, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testCheckNeedSwitch_004 START"); - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->name = "xiaoyiIme"; - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->id = "xiaoyiImeId"; - EnableImeDataParseTest::resultSet_->strValue_ = "{\"enableImeList\" : {\"100\" : [\"baiduIme\", " - "\"sougouIme\"],\"101\" : [\"sougouIme\"]}}"; - SwitchInfo switchInfo; - EnableImeDataParser::GetInstance()->enableList_[IME_KEY].push_back("xiaoyiIme"); - EnableImeDataParser::GetInstance()->enableList_[IME_KEY].push_back("baiduIme"); - bool ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch(IME_KEY, switchInfo, USER_ID); - EXPECT_TRUE(ret); - EXPECT_EQ(switchInfo.bundleName, "baiduIme"); -} - -/** - * @tc.name: testCheckNeedSwitch_005 - * @tc.desc: Check need switch for enable list change - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testCheckNeedSwitch_005, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testCheckNeedSwitch_005 START"); - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->name = "xiaoyiIme"; - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->id = "xiaoyiImeId"; - EnableImeDataParseTest::resultSet_->strValue_ = "{\"enableImeList\" : {\"100\" : [ \"sougouIme\"],\"101\" : " - "[\"sougouIme\"]}}"; - SwitchInfo switchInfo; - EnableImeDataParser::GetInstance()->enableList_[IME_KEY].push_back("xiaoyiIme"); - EnableImeDataParser::GetInstance()->enableList_[IME_KEY].push_back("baiduIme"); - EnableImeDataParser::GetInstance()->enableList_[IME_KEY].push_back("sougouIme"); - bool ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch(IME_KEY, switchInfo, USER_ID); - EXPECT_TRUE(ret); - EXPECT_EQ(switchInfo.bundleName, "sougouIme"); -} - -/** - * @tc.name: testCheckNeedSwitch_006 - * @tc.desc: Check need switch for enable list change - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testCheckNeedSwitch_006, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testCheckNeedSwitch_006 START"); - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->name = "xiaoyiIme"; - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->id = "xiaoyiImeId"; - SwitchInfo switchInfo; - bool ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch( - "settings.inputmethod.enable_keyboard", switchInfo, USER_ID); - EXPECT_FALSE(ret); -} - -/** - * @tc.name: testCheckNeedSwitch_007 - * @tc.desc: Check need switch for enable list change - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testCheckNeedSwitch_007, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testCheckNeedSwitch_007 START"); - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->name = "defaultImeName"; - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->id = "defaultImeId"; - SwitchInfo switchInfo; - bool ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch( - "settings.inputmethod.enable_keyboard", switchInfo, USER_ID); - EXPECT_FALSE(ret); -} - -/** - * @tc.name: testCheckNeedSwitch_008 - * @tc.desc: Check need switch for switch target ime - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testCheckNeedSwitch_008, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testCheckNeedSwitch_008 START"); - SwitchInfo switchInfo; - switchInfo.bundleName = "defaultImeName"; - switchInfo.subName = "defaultImeId"; - bool ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch(switchInfo, USER_ID); - EXPECT_TRUE(ret); -} - -/** - * @tc.name: testCheckNeedSwitch_009 - * @tc.desc: Check need switch for switch target ime - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testCheckNeedSwitch_009, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testCheckNeedSwitch_009 START"); - SwitchInfo switchInfo; - switchInfo.bundleName = "xiaoyiIme"; - bool ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch(switchInfo, USER_ID); - EXPECT_TRUE(ret); -} - -/** - * @tc.name: testCheckNeedSwitch_010 - * @tc.desc: Check need switch for switch target ime - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testCheckNeedSwitch_010, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testCheckNeedSwitch_010 START"); - EnableImeDataParseTest::resultSet_->strValue_ = "{\"enableImeList\" : {\"100\" : [ \"sougouIme\"], \"101\" : " - "[\"sougouIme\"]}}"; - SwitchInfo switchInfo; - switchInfo.bundleName = "xiaoyiIme"; - bool ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch(switchInfo, USER_ID); - EXPECT_FALSE(ret); -} - -/** - * @tc.name: testCheckNeedSwitch_011 - * @tc.desc: Check need switch for enable list change - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testCheckNeedSwitch_011, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testCheckNeedSwitch_011 START"); - EnableImeDataParseTest::resultSet_->strValue_ = ""; - SwitchInfo switchInfo; - bool ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch(IME_KEY, switchInfo, USER_ID); - EXPECT_FALSE(ret); - EXPECT_EQ(switchInfo.bundleName, EnableImeDataParser::GetInstance()->defaultImeInfo_->name); -} - -/** - * @tc.name: testCheckNeedSwitch_012 - * @tc.desc: Check need switch for enable list change - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testCheckNeedSwitch_012, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testCheckNeedSwitch_012 START"); - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->name = "xiaoyiIme"; - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->id = "xiaoyiImeId"; - EnableImeDataParseTest::resultSet_->strValue_ = "{\"enableImeList\" : {\"100\" : []}}"; - SwitchInfo switchInfo; - EnableImeDataParser::GetInstance()->enableList_[IME_KEY].push_back("xiaoyiIme"); - EnableImeDataParser::GetInstance()->enableList_[IME_KEY].push_back("baiduIme"); - EnableImeDataParser::GetInstance()->enableList_[IME_KEY].push_back("sougouIme"); - bool ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch(IME_KEY, switchInfo, USER_ID); - EXPECT_TRUE(ret); - EXPECT_EQ(switchInfo.bundleName, EnableImeDataParser::GetInstance()->defaultImeInfo_->name); -} - -/** - * @tc.name: testCheckNeedSwitch_013 - * @tc.desc: Check need switch for enable list change - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testCheckNeedSwitch_013, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testCheckNeedSwitch_013 START"); - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->name = "xiaoyiIme"; - ImeInfoInquirer::GetInstance().GetCurrentInputMethod(USER_ID)->id = "xiaoyiImeId"; - SwitchInfo switchInfo; - bool ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch(IME_KEY, switchInfo, USER_ID); - EXPECT_FALSE(ret); - - EXPECT_EQ(EnableImeDataParser::GetInstance()->enableList_[IME_KEY].size(), USER_100_TOTAL_COUNT); - if (EnableImeDataParser::GetInstance()->enableList_[IME_KEY].size() == USER_100_TOTAL_COUNT) { - EXPECT_EQ(EnableImeDataParser::GetInstance()->enableList_[IME_KEY][0], "xiaoyiIme"); - EXPECT_EQ(EnableImeDataParser::GetInstance()->enableList_[IME_KEY][1], "baiduIme"); - EXPECT_EQ(EnableImeDataParser::GetInstance()->enableList_[IME_KEY][2], "sougouIme"); - } - resultSet_->strValue_ = "{\"enableImeList\" : {\"100\" : [ \"xiaoyiIme\"], \"101\" : [\"sougouIme\"]}}"; - - ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch(IME_KEY, switchInfo, USER_ID); - - EXPECT_FALSE(ret); - EXPECT_EQ(EnableImeDataParser::GetInstance()->enableList_[IME_KEY].size(), USER_101_TOTAL_COUNT); - if (EnableImeDataParser::GetInstance()->enableList_[IME_KEY].size() == USER_101_TOTAL_COUNT) { - EXPECT_EQ(EnableImeDataParser::GetInstance()->enableList_[IME_KEY][0], "xiaoyiIme"); - } - - ImeInfoInquirer::currentIme_ = nullptr; - EXPECT_NE(ImeInfoInquirer::defaultImeProperty_, nullptr); - ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch(IME_KEY, switchInfo, USER_ID); - EXPECT_TRUE(ret); - EXPECT_EQ(switchInfo.bundleName, ImeInfoInquirer::GetInstance().GetDefaultImeCfgProp()->name); - - ImeInfoInquirer::currentIme_ = std::make_shared(); - ImeInfoInquirer::defaultImeProperty_ = nullptr; - ret = EnableImeDataParser::GetInstance()->CheckNeedSwitch(IME_KEY, switchInfo, USER_ID); - EXPECT_TRUE(ret); -} - -/** - * @tc.name: testOnUserChanged_001 - * @tc.desc: Test local enable list cache change when user changed. - * @tc.type: FUNC - * @tc.require: - * @tc.author: mashaoyin - */ -HWTEST_F(EnableImeDataParseTest, testOnUserChanged_001, TestSize.Level1) -{ - IMSA_HILOGI("EnableImeDataParseTest testOnUserChanged_001 START"); - int32_t ret = EnableImeDataParser::GetInstance()->GetEnableData( - IME_KEY, EnableImeDataParser::GetInstance()->enableList_[IME_KEY], USER_ID); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(EnableImeDataParser::GetInstance()->enableList_[IME_KEY].size(), USER_100_TOTAL_COUNT); - // 101 means new user id - EnableImeDataParser::GetInstance()->OnUserChanged(101); - EXPECT_EQ(EnableImeDataParser::GetInstance()->enableList_[IME_KEY].size(), USER_101_TOTAL_COUNT); - if (EnableImeDataParser::GetInstance()->enableList_[IME_KEY].size() == USER_101_TOTAL_COUNT) { - EXPECT_EQ(EnableImeDataParser::GetInstance()->enableList_[IME_KEY][0], "sougouIme"); - } -} -} // namespace MiscServices -} // namespace OHOS diff --git a/test/unittest/cpp_test/src/full_ime_info_manager_test.cpp b/test/unittest/cpp_test/src/full_ime_info_manager_test.cpp index a3d6332de4a7e75cddfd00e70548a952fc4e3cf4..510ffd0a42ac09e6de3a59cccbb00dd985fcf7b8 100644 --- a/test/unittest/cpp_test/src/full_ime_info_manager_test.cpp +++ b/test/unittest/cpp_test/src/full_ime_info_manager_test.cpp @@ -89,8 +89,9 @@ HWTEST_F(FullImeInfoManagerTest, test_Init_002, TestSize.Level0) uint32_t tokenId = 2; std::string appId = "appId"; uint32_t versionCode = 11; + std::string installTime = "12345"; std::vector imeInfos; - imeInfos.push_back({ isNewIme, tokenId, appId, versionCode, prop }); + imeInfos.push_back({ isNewIme, tokenId, appId, versionCode, installTime, prop }); std::vector>> fullImeInfos; fullImeInfos.emplace_back(std::make_pair(userId, imeInfos)); ImeInfoInquirer::GetInstance().SetFullImeInfo(true, fullImeInfos); @@ -157,11 +158,12 @@ HWTEST_F(FullImeInfoManagerTest, test_Add_003, TestSize.Level0) std::string appId = "appId"; uint32_t versionCode = 11; prop.name = "bundleName"; - FullImeInfo imeInfo { isNewIme, tokenId, appId, versionCode, prop }; + std::string installTime = "12345"; + FullImeInfo imeInfo { isNewIme, tokenId, appId, versionCode, installTime, prop }; uint32_t tokenId1 = 2; std::string appId1 = "appId1"; prop1.name = "bundleName1"; - FullImeInfo imeInfo1 { isNewIme, tokenId1, appId1, versionCode, prop1 }; + FullImeInfo imeInfo1 { isNewIme, tokenId1, appId1, versionCode, installTime, prop1 }; imeInfos.push_back(imeInfo); imeInfos.push_back(imeInfo1); ImeInfoInquirer::GetInstance().SetFullImeInfo(true, imeInfos); @@ -297,7 +299,7 @@ HWTEST_F(FullImeInfoManagerTest, test_Update_001, TestSize.Level0) std::vector>> fullImeInfos; ImeInfoInquirer::GetInstance().SetFullImeInfo(false, fullImeInfos); auto ret = FullImeInfoManager::GetInstance().Update(); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(ret, ErrorCode::ERROR_PACKAGE_MANAGER); EXPECT_TRUE(FullImeInfoManager::GetInstance().fullImeInfos_.empty()); } @@ -476,8 +478,9 @@ HWTEST_F(FullImeInfoManagerTest, test_Get_001, TestSize.Level0) FullImeInfoManager::GetInstance().fullImeInfos_.insert_or_assign(userId, imeInfos); int32_t userId1 = 101; - auto ret = FullImeInfoManager::GetInstance().Get(userId1); - EXPECT_TRUE(ret.empty()); + std::vector props; + auto ret = FullImeInfoManager::GetInstance().Get(userId1, props); + EXPECT_TRUE(props.empty()); } /** @@ -494,8 +497,9 @@ HWTEST_F(FullImeInfoManagerTest, test_Get_002, TestSize.Level0) imeInfos.push_back(info); FullImeInfoManager::GetInstance().fullImeInfos_.insert_or_assign(userId, imeInfos); - auto ret = FullImeInfoManager::GetInstance().Get(userId); - EXPECT_EQ(ret.size(), 1); + std::vector props; + auto ret = FullImeInfoManager::GetInstance().Get(userId, props); + EXPECT_EQ(props.size(), 1); } /** @@ -513,7 +517,7 @@ HWTEST_F(FullImeInfoManagerTest, test_Get_003, TestSize.Level0) int32_t userId1 = 101; std::string bundleName = "bundleName"; FullImeInfo info; - auto ret = FullImeInfoManager::GetInstance().Get(bundleName, userId1, info); + auto ret = FullImeInfoManager::GetInstance().Get(userId1, bundleName, info); EXPECT_FALSE(ret); } @@ -535,7 +539,7 @@ HWTEST_F(FullImeInfoManagerTest, test_Get_004, TestSize.Level0) int32_t userId1 = 100; std::string bundleName = "bundleName"; FullImeInfo infoRet; - auto ret = FullImeInfoManager::GetInstance().Get(bundleName, userId1, infoRet); + auto ret = FullImeInfoManager::GetInstance().Get(userId1, bundleName, infoRet); EXPECT_FALSE(ret); } @@ -557,7 +561,7 @@ HWTEST_F(FullImeInfoManagerTest, test_Get_005, TestSize.Level0) int32_t userId1 = 100; std::string bundleName = "bundleName1"; FullImeInfo infoRet; - auto ret = FullImeInfoManager::GetInstance().Get(bundleName, userId1, infoRet); + auto ret = FullImeInfoManager::GetInstance().Get(userId1, bundleName, infoRet); EXPECT_TRUE(ret); EXPECT_EQ(infoRet.prop.name, info.prop.name); } diff --git a/test/unittest/cpp_test/src/ime_enabled_info_manager_test.cpp b/test/unittest/cpp_test/src/ime_enabled_info_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0d8d724a1ffabcf396100340c1798beb6d520c73 --- /dev/null +++ b/test/unittest/cpp_test/src/ime_enabled_info_manager_test.cpp @@ -0,0 +1,1178 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define private public +#define protected public +#include "ime_enabled_info_manager.h" + +#include "settings_data_utils.h" +#undef private + +#include + +#include +#include + +#include "ime_info_inquirer.h" +#include "parameter.h" +#include "tdd_util.h" + +using namespace testing::ext; +using namespace OHOS::DataShare; +namespace OHOS { +namespace MiscServices { +struct ImeEnabledNecessaryInfo { + std::string bundleName; + std::string extensionName; + std::string installTime; +}; + +struct UserImeConfigTest : public Serializable { + std::map> cfgs; + bool Marshal(cJSON *node) const override + { + bool ret = true; + for (const auto &cfg : cfgs) { + ret = SetValue(node, cfg.first, cfg.second) && ret; + } + return ret; + } +}; + +struct EnabledImeCfgTest : public Serializable { + UserImeConfigTest userImeCfg; + bool Marshal(cJSON *node) const override + { + return SetValue(node, GET_NAME(enableImeList), userImeCfg); + } +}; + +struct SecurityModeCfgTest : public Serializable { + UserImeConfigTest userImeCfg; + bool Marshal(cJSON *node) const override + { + return SetValue(node, GET_NAME(fullExperienceList), userImeCfg); + } +}; +class ImeEnabledInfoManagerTest : public testing::Test { +public: + static constexpr const char *ENABLE_IME = "settings.inputmethod.enable_ime"; + static constexpr const char *SECURITY_MODE = "settings.inputmethod.full_experience"; + static constexpr const char *DEFAULT_IME_KEY = "default_ime_key"; + static constexpr const char *DEFAULT_IME_INSTALL_TIME = "789456"; + static constexpr const char *IME_KEY1 = "key1"; + static constexpr const char *IME_KEY2 = "key2"; + static constexpr const char *BUNDLE_NAME1 = "bundleName1"; + static constexpr const char *BUNDLE_NAME2 = "bundleName2"; + static constexpr const char *EXT_NAME1 = "extName1"; + static constexpr const char *EXT_NAME2 = "extName2"; + static constexpr const char *INSTALL_TIME1 = "123456"; + static constexpr const char *INSTALL_TIME2 = "456789"; + static constexpr const char *RE_INSTALL_TIME = "33333"; + static constexpr int32_t USER_ID1 = 1; + static constexpr int32_t WAIT_DATA_SHARE_CB_TIMEOUT = 300; + static ImeNativeCfg defaultImeProp_; + static int32_t currentUserId_; + static void SetUpTestCase(); + static void TearDownTestCase(); + static void DataShareCallback(); + void SetUp(); + void TearDown(); + static bool WaitDataShareCallback(const std::map &enabledCfg); + static std::map> GenerateFullImeInfos( + std::map> &easyInfos); + static std::vector GenerateFullImeInfos(std::vector &imeKeys); + static FullImeInfo GenerateFullImeInfo(const std::string &imeKey); + static std::map GenerateAllEnabledCfg( + std::map>> &easyEnabledInfos); + static ImeEnabledCfg GenerateAllEnabledCfg(std::vector> &easyEnabledInfos); + +private: + static std::string originalGlobalEnableImeStr_; + static std::string originalGlobalFullExperienceStr_; + static std::string currentUserEnableImeStr_; + static std::map enabledCfg_; + static std::mutex dataShareCbCvMutex_; + static std::condition_variable dataShareCbCv_; + static std::unordered_map imeEnabledNecessaryInfoMapping_; +}; + +void ImeEnabledInfoManagerTest::SetUpTestCase() +{ + TddUtil::GrantNativePermission(); + ImeInfoInquirer::GetInstance().InitSystemConfig(); + IMSA_HILOGI("sys cfg:[%{public}d,%{public}d,%{public}d].", + ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature, + ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature, + ImeInfoInquirer::GetInstance().GetSystemConfig().initEnabledState); + currentUserId_ = TddUtil::GetCurrentUserId(); + std::string uriProxy = SETTINGS_USER_DATA_URI + std::to_string(currentUserId_) + "?Proxy=true"; + auto ret = SettingsDataUtils::GetInstance()->CreateAndRegisterObserver( + uriProxy, ENABLE_IME, []() { DataShareCallback(); }); + IMSA_HILOGI("CreateAndRegisterObserver ret:%{public}d.", ret); + + ret = SettingsDataUtils::GetInstance()->GetStringValue( + SETTING_URI_PROXY, SECURITY_MODE, originalGlobalFullExperienceStr_); + IMSA_HILOGI("GetStringValue SECURITY_MODE ret:%{public}d.", ret); + ret = SettingsDataUtils::GetInstance()->GetStringValue(SETTING_URI_PROXY, ENABLE_IME, originalGlobalEnableImeStr_); + IMSA_HILOGI("GetStringValue ENABLE_IME ret:%{public}d.", ret); + ret = SettingsDataUtils::GetInstance()->GetStringValue( + SETTINGS_USER_DATA_URI + std::to_string(currentUserId_) + "?Proxy=true", ENABLE_IME, currentUserEnableImeStr_); + IMSA_HILOGI("user GetStringValue ENABLE_IME ret:%{public}d.", ret); + + SettingsDataUtils::GetInstance()->isDataShareReady_ = true; + defaultImeProp_ = ImeInfoInquirer::GetInstance().GetDefaultIme(); + imeEnabledNecessaryInfoMapping_ = { + { IME_KEY1, { BUNDLE_NAME1, EXT_NAME1, INSTALL_TIME1 } }, + { IME_KEY2, { BUNDLE_NAME2, EXT_NAME2, INSTALL_TIME2 } }, + { DEFAULT_IME_KEY, + { + defaultImeProp_.bundleName, + defaultImeProp_.extName, + DEFAULT_IME_INSTALL_TIME, + } }, + }; +} + +void ImeEnabledInfoManagerTest::TearDownTestCase() +{ + if (!originalGlobalFullExperienceStr_.empty()) { + auto ret = SettingsDataUtils::GetInstance()->SetStringValue( + SETTING_URI_PROXY, SECURITY_MODE, originalGlobalFullExperienceStr_); + IMSA_HILOGI("SetStringValue SECURITY_MODE ret:%{public}d.", ret); + } + if (!originalGlobalEnableImeStr_.empty()) { + auto ret = SettingsDataUtils::GetInstance()->SetStringValue( + SETTING_URI_PROXY, ENABLE_IME, originalGlobalEnableImeStr_); + IMSA_HILOGI("SetStringValue ENABLE_IME ret:%{public}d.", ret); + } + if (!currentUserEnableImeStr_.empty()) { + SettingsDataUtils::GetInstance()->SetStringValue( + SETTINGS_USER_DATA_URI + std::to_string(currentUserId_) + "?Proxy=true", ENABLE_IME, + currentUserEnableImeStr_); + } +} + +void ImeEnabledInfoManagerTest::SetUp() +{ + TddUtil::DeleteGlobalTable(ENABLE_IME); + TddUtil::DeleteGlobalTable(SECURITY_MODE); + TddUtil::DeleteUserTable(currentUserId_, ENABLE_IME); +} + +void ImeEnabledInfoManagerTest::TearDown() +{ + enabledCfg_.clear(); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.clear(); +} + +void ImeEnabledInfoManagerTest::DataShareCallback() +{ + IMSA_HILOGI("start."); + std::unique_lock lock(dataShareCbCvMutex_); + std::string content1; + ImeEnabledCfg enabledCfg1; + auto ret = SettingsDataUtils::GetInstance()->GetStringValue( + SETTINGS_USER_DATA_URI + std::to_string(currentUserId_) + "?Proxy=true", ENABLE_IME, content1); + if (!enabledCfg1.Unmarshall(content1)) { + IMSA_HILOGE("%{public}d Unmarshall failed:%{public}d.", currentUserId_, ret); + } else { + enabledCfg_.insert_or_assign(currentUserId_, enabledCfg1); + } + IMSA_HILOGI("notify."); + dataShareCbCv_.notify_one(); +} + +bool ImeEnabledInfoManagerTest::WaitDataShareCallback(const std::map &enabledCfg) +{ + std::unique_lock lock(dataShareCbCvMutex_); + dataShareCbCv_.wait_for(lock, std::chrono::milliseconds(WAIT_DATA_SHARE_CB_TIMEOUT), [&enabledCfg]() { + return enabledCfg == enabledCfg_ && enabledCfg_ == ImeEnabledInfoManager::GetInstance().imeEnabledCfg_; + }); + for (const auto &cfg : enabledCfg) { + IMSA_HILOGI("enabledCfg base info:[%{public}d, %{public}s].", cfg.first, cfg.second.version.c_str()); + for (const auto &info : cfg.second.enabledInfos) { + IMSA_HILOGI("enabledCfg info:[%{public}s,%{public}s,%{public}s,%{public}d].", + info.bundleName.c_str(), info.extensionName.c_str(), info.installTime.c_str(), info.enabledStatus); + } + } + for (const auto &cfg : enabledCfg_) { + IMSA_HILOGI("enabledCfg_ base info:[%{public}d, %{public}s].", cfg.first, cfg.second.version.c_str()); + for (const auto &info : cfg.second.enabledInfos) { + IMSA_HILOGI("enabledCfg_ info:[%{public}s,%{public}s,%{public}s,%{public}d].", + info.bundleName.c_str(), info.extensionName.c_str(), info.installTime.c_str(), info.enabledStatus); + } + } + for (const auto &cfg : ImeEnabledInfoManager::GetInstance().imeEnabledCfg_) { + IMSA_HILOGI("cache base info:[%{public}d, %{public}s].", cfg.first, cfg.second.version.c_str()); + for (const auto &info : cfg.second.enabledInfos) { + IMSA_HILOGI("cache info:[%{public}s,%{public}s,%{public}s,%{public}d].", info.bundleName.c_str(), + info.extensionName.c_str(), info.installTime.c_str(), info.enabledStatus); + } + } + if (enabledCfg != enabledCfg_) { + IMSA_HILOGI("enabledCfg not same enabledCfg_."); + } + if (ImeEnabledInfoManager::GetInstance().imeEnabledCfg_ != enabledCfg) { + IMSA_HILOGI("enabledCfg not same ImeEnabledInfoManager::GetInstance().imeEnabledCfg_."); + } + return enabledCfg == enabledCfg_ && enabledCfg_ == ImeEnabledInfoManager::GetInstance().imeEnabledCfg_; +} + +std::map> ImeEnabledInfoManagerTest::GenerateFullImeInfos( + std::map> &easyInfos) +{ + std::map> fullImeInfos; + for (const auto &easyInfo : easyInfos) { + std::vector imeInfos; + for (const auto &key : easyInfo.second) { + FullImeInfo imeInfo; + auto it = imeEnabledNecessaryInfoMapping_.find(key); + if (it != imeEnabledNecessaryInfoMapping_.end()) { + imeInfo.installTime = it->second.installTime; + imeInfo.prop.name = it->second.bundleName; + imeInfo.prop.id = it->second.extensionName; + } + imeInfos.push_back(imeInfo); + } + fullImeInfos.insert({ easyInfo.first, imeInfos }); + } + return fullImeInfos; +} + +std::vector ImeEnabledInfoManagerTest::GenerateFullImeInfos(std::vector &imeKeys) +{ + std::vector imeInfos; + for (const auto &key : imeKeys) { + FullImeInfo imeInfo; + auto it = imeEnabledNecessaryInfoMapping_.find(key); + if (it != imeEnabledNecessaryInfoMapping_.end()) { + imeInfo.installTime = it->second.installTime; + imeInfo.prop.name = it->second.bundleName; + imeInfo.prop.id = it->second.extensionName; + } + imeInfos.push_back(imeInfo); + } + return imeInfos; +} + +FullImeInfo ImeEnabledInfoManagerTest::GenerateFullImeInfo(const std::string &imeKey) +{ + FullImeInfo imeInfo; + auto it = imeEnabledNecessaryInfoMapping_.find(imeKey); + if (it != imeEnabledNecessaryInfoMapping_.end()) { + imeInfo.installTime = it->second.installTime; + imeInfo.prop.name = it->second.bundleName; + imeInfo.prop.id = it->second.extensionName; + } + return imeInfo; +} + +std::map ImeEnabledInfoManagerTest::GenerateAllEnabledCfg( + std::map>> &easyEnabledInfos) +{ + std::map allEnabledCfg; + for (const auto &easyInfo : easyEnabledInfos) { + ImeEnabledCfg enabledCfg; + enabledCfg.version = GetDisplayVersion(); + std::vector imeEnabledInfos; + for (const auto &info : easyInfo.second) { + ImeEnabledInfo imeEnabledInfo; + auto it = imeEnabledNecessaryInfoMapping_.find(info.first); + if (it != imeEnabledNecessaryInfoMapping_.end()) { + imeEnabledInfo.installTime = it->second.installTime; + imeEnabledInfo.bundleName = it->second.bundleName; + imeEnabledInfo.extensionName = it->second.extensionName; + } + imeEnabledInfo.enabledStatus = info.second; + imeEnabledInfos.push_back(imeEnabledInfo); + } + enabledCfg.enabledInfos = imeEnabledInfos; + allEnabledCfg.insert({ easyInfo.first, enabledCfg }); + } + return allEnabledCfg; +} + +ImeEnabledCfg ImeEnabledInfoManagerTest::GenerateAllEnabledCfg( + std::vector> &easyEnabledInfos) +{ + ImeEnabledCfg enabledCfg; + enabledCfg.version = GetDisplayVersion(); + std::vector imeEnabledInfos; + for (const auto &info : easyEnabledInfos) { + ImeEnabledInfo imeEnabledInfo; + auto it = imeEnabledNecessaryInfoMapping_.find(info.first); + if (it != imeEnabledNecessaryInfoMapping_.end()) { + imeEnabledInfo.installTime = it->second.installTime; + imeEnabledInfo.bundleName = it->second.bundleName; + imeEnabledInfo.extensionName = it->second.extensionName; + } + imeEnabledInfo.enabledStatus = info.second; + imeEnabledInfos.push_back(imeEnabledInfo); + } + enabledCfg.enabledInfos = imeEnabledInfos; + return enabledCfg; +} + +std::string ImeEnabledInfoManagerTest::originalGlobalEnableImeStr_; +std::string ImeEnabledInfoManagerTest::originalGlobalFullExperienceStr_; +std::string ImeEnabledInfoManagerTest::currentUserEnableImeStr_; +std::map ImeEnabledInfoManagerTest::enabledCfg_; +std::mutex ImeEnabledInfoManagerTest::dataShareCbCvMutex_; +std::condition_variable ImeEnabledInfoManagerTest::dataShareCbCv_; +ImeNativeCfg ImeEnabledInfoManagerTest::defaultImeProp_; +int32_t ImeEnabledInfoManagerTest::currentUserId_; +std::unordered_map ImeEnabledInfoManagerTest::imeEnabledNecessaryInfoMapping_; + +/** + * @tc.name: testInit_001 + * @tc.desc: Init:first startup after version burning,no table + * has sys ime + * test:sys ime default BASIC_MODE + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testInit_001, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testInit_001 START"); + std::map> easyInfos; + easyInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, { ImeEnabledInfoManagerTest::DEFAULT_IME_KEY } }); + auto ret = ImeEnabledInfoManager::GetInstance().Init(ImeEnabledInfoManagerTest::GenerateFullImeInfos(easyInfos)); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos))); + EnabledStatus status = EnabledStatus::DISABLED; + ret = ImeEnabledInfoManager::GetInstance().GetEnabledState( + ImeEnabledInfoManagerTest::currentUserId_, ImeEnabledInfoManagerTest::defaultImeProp_.bundleName, status); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(status, EnabledStatus::BASIC_MODE); + } +} + +/** + * @tc.name: testInit_002 + * @tc.desc: Init:upgrade only with global table(enable and full experience), + * has sys ime and BUNDLE_NAME1 + * BUNDLE_NAME1 in full experience table + * BUNDLE_NAME2 in enable table + * test1:BUNDLE_NAME1 in full experience table, not in enable table, status is FULL_EXPERIENCE_MODE + * test2:BUNDLE_NAME2 in enable table, but not installed, will not add to new enable table + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testInit_002, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testInit_002 START"); + EnabledImeCfgTest enableCfg; + enableCfg.userImeCfg.cfgs.insert( + { std::to_string(ImeEnabledInfoManagerTest::currentUserId_), { ImeEnabledInfoManagerTest::BUNDLE_NAME2 } }); + std::string enabledStr; + enableCfg.Marshall(enabledStr); + TddUtil::GenerateGlobalTable(ImeEnabledInfoManagerTest::ENABLE_IME, enabledStr); + + SecurityModeCfgTest SecurityModeCfg; + SecurityModeCfg.userImeCfg.cfgs.insert( + { std::to_string(ImeEnabledInfoManagerTest::currentUserId_), { ImeEnabledInfoManagerTest::BUNDLE_NAME1 } }); + std::string fullExperienceStr; + SecurityModeCfg.Marshall(fullExperienceStr); + TddUtil::GenerateGlobalTable(ImeEnabledInfoManagerTest::SECURITY_MODE, fullExperienceStr); + + std::map> easyInfos; + easyInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, { + ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, + ImeEnabledInfoManagerTest::IME_KEY1, + } }); + auto ret = ImeEnabledInfoManager::GetInstance().Init(ImeEnabledInfoManagerTest::GenerateFullImeInfos(easyInfos)); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY1, EnabledStatus::FULL_EXPERIENCE_MODE), + std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos))); + EnabledStatus status = EnabledStatus::DISABLED; + ret = ImeEnabledInfoManager::GetInstance().GetEnabledState( + ImeEnabledInfoManagerTest::currentUserId_, ImeEnabledInfoManagerTest::BUNDLE_NAME1, status); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(status, EnabledStatus::FULL_EXPERIENCE_MODE); + } +} + +/** + * @tc.name: testInit_003 + * @tc.desc: Init:upgrade only with global enable table + * has sys ime、BUNDLE_NAME1 and BUNDLE_NAME2 + * BUNDLE_NAME1 in enable table + * test1:BUNDLE_NAME1 in enable table, status is BASIC_MODE + * test2:BUNDLE_NAME2 not in enable table and full experience table, + * status is decided by initEnabledState + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testInit_003, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testInit_003 START"); + EnabledImeCfgTest enableCfg; + enableCfg.userImeCfg.cfgs.insert( + { std::to_string(ImeEnabledInfoManagerTest::currentUserId_), { ImeEnabledInfoManagerTest::BUNDLE_NAME1 } }); + std::string enabledStr; + enableCfg.Marshall(enabledStr); + TddUtil::GenerateGlobalTable(ImeEnabledInfoManagerTest::ENABLE_IME, enabledStr); + + std::map> easyInfos; + easyInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, { + ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, + ImeEnabledInfoManagerTest::IME_KEY1, + ImeEnabledInfoManagerTest::IME_KEY2, + } }); + auto ret = ImeEnabledInfoManager::GetInstance().Init(ImeEnabledInfoManagerTest::GenerateFullImeInfos(easyInfos)); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + EnabledStatus imeEnabledStatus2 = ImeInfoInquirer::GetInstance().GetSystemConfig().initEnabledState; + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::IME_KEY1, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, imeEnabledStatus2) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos))); + EnabledStatus status = EnabledStatus::DISABLED; + ret = ImeEnabledInfoManager::GetInstance().GetEnabledState( + ImeEnabledInfoManagerTest::currentUserId_, ImeEnabledInfoManagerTest::BUNDLE_NAME2, status); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(status, imeEnabledStatus2); + } +} + +/** + * @tc.name: testInit_004 + * @tc.desc: Init:upgrade only with global full experience table + * has sys ime、 BUNDLE_NAME2 + * BUNDLE_NAME2 in full experience table + * full experience table parse abnormal + * test:BUNDLE_NAME2 in full experience table, not in enable table, + * full experience table parse abnormal, status is decided by initEnabledState + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testInit_004, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testInit_004 START"); + SecurityModeCfgTest SecurityModeCfg; + SecurityModeCfg.userImeCfg.cfgs.insert({ "", { ImeEnabledInfoManagerTest::BUNDLE_NAME2 } }); + std::string fullExperienceStr; + SecurityModeCfg.Marshall(fullExperienceStr); + TddUtil::GenerateGlobalTable(ImeEnabledInfoManagerTest::SECURITY_MODE, fullExperienceStr); + + std::map> easyInfos; + easyInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, { + ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, + ImeEnabledInfoManagerTest::IME_KEY2, + } }); + auto ret = ImeEnabledInfoManager::GetInstance().Init(ImeEnabledInfoManagerTest::GenerateFullImeInfos(easyInfos)); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + EnabledStatus imeEnabledStatus2 = ImeInfoInquirer::GetInstance().GetSystemConfig().initEnabledState; + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, imeEnabledStatus2) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos))); + EnabledStatus status = EnabledStatus::DISABLED; + ret = ImeEnabledInfoManager::GetInstance().GetEnabledState( + ImeEnabledInfoManagerTest::currentUserId_, ImeEnabledInfoManagerTest::BUNDLE_NAME2, status); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(status, imeEnabledStatus2); + } +} + +/** + * @tc.name: testInit_005 + * @tc.desc: Init:upgrade only with global enable table + * has sys ime、 BUNDLE_NAME1 + * BUNDLE_NAME1 in enable table + * enable table parse abnormal + * test:BUNDLE_NAME1 in in enable table, not in full experience table, + * enable table parse abnormal, status is decided by initEnabledState + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testInit_005, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testInit_004 START"); + EnabledImeCfgTest enableCfg; + enableCfg.userImeCfg.cfgs.insert({ "", { ImeEnabledInfoManagerTest::BUNDLE_NAME1 } }); + std::string enabledStr; + enableCfg.Marshall(enabledStr); + TddUtil::GenerateGlobalTable(ImeEnabledInfoManagerTest::ENABLE_IME, enabledStr); + + std::map> easyInfos; + easyInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, { + ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, + ImeEnabledInfoManagerTest::IME_KEY1, + } }); + auto ret = ImeEnabledInfoManager::GetInstance().Init(ImeEnabledInfoManagerTest::GenerateFullImeInfos(easyInfos)); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + EnabledStatus imeEnabledStatus1 = ImeInfoInquirer::GetInstance().GetSystemConfig().initEnabledState; + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { + std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY1, imeEnabledStatus1), + } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos))); + EnabledStatus status = EnabledStatus::DISABLED; + ret = ImeEnabledInfoManager::GetInstance().GetEnabledState( + ImeEnabledInfoManagerTest::currentUserId_, ImeEnabledInfoManagerTest::BUNDLE_NAME1, status); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(status, imeEnabledStatus1); + } +} + +/** + * @tc.name: testInit_006 + * @tc.desc: Init:upgrade with global table(enable and full experience) and user enable table + * has sys ime、BUNDLE_NAME1 and BUNDLE_NAME2 + * sys ime in global enable table and in user enable table + * BUNDLE_NAME1 in global enable table、 + * BUNDLE_NAME2 in global enable table and in user enable table + * test:has user enable table, but global enable table has normal enable info of this user, + * It's up to global enable table + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testInit_006, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testInit_006 START"); + EnabledImeCfgTest enableCfg; + enableCfg.userImeCfg.cfgs.insert({ std::to_string(ImeEnabledInfoManagerTest::currentUserId_), + { ImeEnabledInfoManagerTest::BUNDLE_NAME1, ImeEnabledInfoManagerTest::BUNDLE_NAME2, + ImeEnabledInfoManagerTest::defaultImeProp_.bundleName } }); + std::string enabledStr; + enableCfg.Marshall(enabledStr); + TddUtil::GenerateGlobalTable(ImeEnabledInfoManagerTest::ENABLE_IME, enabledStr); + + SecurityModeCfgTest SecurityModeCfg; + SecurityModeCfg.userImeCfg.cfgs.insert({ std::to_string(ImeEnabledInfoManagerTest::currentUserId_), {} }); + std::string fullExperienceStr; + SecurityModeCfg.Marshall(fullExperienceStr); + TddUtil::GenerateGlobalTable(ImeEnabledInfoManagerTest::SECURITY_MODE, fullExperienceStr); + + EnabledImeCfgTest enableCfg1; + enableCfg1.userImeCfg.cfgs.insert({ std::to_string(ImeEnabledInfoManagerTest::currentUserId_), + { ImeEnabledInfoManagerTest::BUNDLE_NAME2, ImeEnabledInfoManagerTest::defaultImeProp_.bundleName } }); + std::string userEnabledStr; + enableCfg1.Marshall(userEnabledStr); + TddUtil::GenerateUserTable(currentUserId_, ImeEnabledInfoManagerTest::ENABLE_IME, userEnabledStr); + + std::map> easyInfos; + easyInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, { + ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, + ImeEnabledInfoManagerTest::IME_KEY1, + ImeEnabledInfoManagerTest::IME_KEY2, + } }); + + auto ret = ImeEnabledInfoManager::GetInstance().Init(ImeEnabledInfoManagerTest::GenerateFullImeInfos(easyInfos)); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::IME_KEY1, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos))); + } +} + +/** + * @tc.name: testInit_007 + * @tc.desc: Init:upgrade with global table(enable and full experience) and user enable table + * has sys ime and BUNDLE_NAME1 + * sys ime in user enable table + * BUNDLE_NAME1 in user enable table + * test:has user enable table, but global enable table not has enable info of this user, + * It's up to user enable table + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testInit_007, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testInit_007 START"); + EnabledImeCfgTest enableCfg; + enableCfg.userImeCfg.cfgs.insert({ std::to_string(ImeEnabledInfoManagerTest::USER_ID1), + { ImeEnabledInfoManagerTest::BUNDLE_NAME1, ImeEnabledInfoManagerTest::BUNDLE_NAME2, + ImeEnabledInfoManagerTest::defaultImeProp_.bundleName } }); + std::string enabledStr; + enableCfg.Marshall(enabledStr); + TddUtil::GenerateGlobalTable(ImeEnabledInfoManagerTest::ENABLE_IME, enabledStr); + + SecurityModeCfgTest SecurityModeCfg; + SecurityModeCfg.userImeCfg.cfgs.insert({ std::to_string(ImeEnabledInfoManagerTest::currentUserId_), {} }); + std::string fullExperienceStr; + SecurityModeCfg.Marshall(fullExperienceStr); + TddUtil::GenerateGlobalTable(ImeEnabledInfoManagerTest::SECURITY_MODE, fullExperienceStr); + + EnabledImeCfgTest enableCfg1; + enableCfg1.userImeCfg.cfgs.insert({ std::to_string(ImeEnabledInfoManagerTest::currentUserId_), + { ImeEnabledInfoManagerTest::BUNDLE_NAME1, ImeEnabledInfoManagerTest::defaultImeProp_.bundleName } }); + std::string userEnabledStr; + enableCfg1.Marshall(userEnabledStr); + TddUtil::GenerateUserTable(currentUserId_, ImeEnabledInfoManagerTest::ENABLE_IME, userEnabledStr); + + std::map> easyInfos; + easyInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, { + ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, + ImeEnabledInfoManagerTest::IME_KEY1, + } }); + auto ret = ImeEnabledInfoManager::GetInstance().Init(ImeEnabledInfoManagerTest::GenerateFullImeInfos(easyInfos)); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY1, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos))); + } +} + +/** + * @tc.name: testInit_008 + * @tc.desc: Init:upgrade with only with user enable table + * has sys ime、BUNDLE_NAME2 + * sys ime、BUNDLE_NAME2 in user enable table + * test:has user enable table, not has global enable table, It's up to user enable table + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testInit_008, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testInit_008 START"); + EnabledImeCfgTest enableCfg; + enableCfg.userImeCfg.cfgs.insert({ std::to_string(ImeEnabledInfoManagerTest::currentUserId_), + { ImeEnabledInfoManagerTest::BUNDLE_NAME2, ImeEnabledInfoManagerTest::defaultImeProp_.bundleName } }); + std::string userEnabledStr; + enableCfg.Marshall(userEnabledStr); + TddUtil::GenerateUserTable(currentUserId_, ImeEnabledInfoManagerTest::ENABLE_IME, userEnabledStr); + + std::map> easyInfos; + easyInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, { + ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, + ImeEnabledInfoManagerTest::IME_KEY2, + } }); + auto ret = ImeEnabledInfoManager::GetInstance().Init(ImeEnabledInfoManagerTest::GenerateFullImeInfos(easyInfos)); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos))); + } +} + +/** + * @tc.name: testInit_009 + * @tc.desc: Init:upgrade with global table(enable and full experience) and user enable table + * has sys ime、BUNDLE_NAME2 + * sys ime、BUNDLE_NAME2 in global enable table + * sys ime、BUNDLE_NAME2 in user enable table + * BUNDLE_NAME2 in full experience + * test:in enable table, in full experience, the status is full experience + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testInit_009, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testInit_009 START"); + EnabledImeCfgTest enableCfg; + enableCfg.userImeCfg.cfgs.insert({ std::to_string(ImeEnabledInfoManagerTest::currentUserId_), + { ImeEnabledInfoManagerTest::BUNDLE_NAME2, ImeEnabledInfoManagerTest::defaultImeProp_.bundleName } }); + std::string enabledStr; + enableCfg.Marshall(enabledStr); + TddUtil::GenerateGlobalTable(ImeEnabledInfoManagerTest::ENABLE_IME, enabledStr); + + SecurityModeCfgTest SecurityModeCfg; + SecurityModeCfg.userImeCfg.cfgs.insert( + { std::to_string(ImeEnabledInfoManagerTest::currentUserId_), { ImeEnabledInfoManagerTest::BUNDLE_NAME2 } }); + std::string fullExperienceStr; + SecurityModeCfg.Marshall(fullExperienceStr); + TddUtil::GenerateGlobalTable(ImeEnabledInfoManagerTest::SECURITY_MODE, fullExperienceStr); + + EnabledImeCfgTest enableCfg1; + enableCfg1.userImeCfg.cfgs.insert({ std::to_string(ImeEnabledInfoManagerTest::currentUserId_), + { ImeEnabledInfoManagerTest::BUNDLE_NAME2, ImeEnabledInfoManagerTest::defaultImeProp_.bundleName } }); + std::string userEnabledStr; + enableCfg1.Marshall(userEnabledStr); + TddUtil::GenerateUserTable(currentUserId_, ImeEnabledInfoManagerTest::ENABLE_IME, userEnabledStr); + + std::map> easyInfos; + easyInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, { + ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, + ImeEnabledInfoManagerTest::IME_KEY2, + } }); + auto ret = ImeEnabledInfoManager::GetInstance().Init(ImeEnabledInfoManagerTest::GenerateFullImeInfos(easyInfos)); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, EnabledStatus::FULL_EXPERIENCE_MODE), + std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos))); + } +} + +/** + * @tc.name: testInit_010 + * @tc.desc: Init:device reboot with new user enable table + * has sys ime、BUNDLE_NAME2 + * sys ime、BUNDLE_NAME2 in global enable table + * sys ime、BUNDLE_NAME2 in new user enable table, all BASIC_MODE + * BUNDLE_NAME2 in full experience table + * test:has new user table, It's up to new user enable table + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testInit_010, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testInit_010 START"); + EnabledImeCfgTest enableCfg; + enableCfg.userImeCfg.cfgs.insert({ std::to_string(ImeEnabledInfoManagerTest::currentUserId_), + { ImeEnabledInfoManagerTest::BUNDLE_NAME2, ImeEnabledInfoManagerTest::defaultImeProp_.bundleName } }); + std::string enabledStr; + enableCfg.Marshall(enabledStr); + TddUtil::GenerateGlobalTable(ImeEnabledInfoManagerTest::ENABLE_IME, enabledStr); + + SecurityModeCfgTest securityModeCfg; + securityModeCfg.userImeCfg.cfgs.insert( + { std::to_string(ImeEnabledInfoManagerTest::currentUserId_), { ImeEnabledInfoManagerTest::BUNDLE_NAME2 } }); + std::string fullExperienceStr; + securityModeCfg.Marshall(fullExperienceStr); + TddUtil::GenerateGlobalTable(ImeEnabledInfoManagerTest::SECURITY_MODE, fullExperienceStr); + + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, EnabledStatus::DISABLED) } }); + auto enabledCfg = ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos); + std::string userEnabledStr; + enabledCfg[ImeEnabledInfoManagerTest::currentUserId_].Marshall(userEnabledStr); + TddUtil::GenerateUserTable(currentUserId_, ImeEnabledInfoManagerTest::ENABLE_IME, userEnabledStr); + + std::map> easyInfos; + easyInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, { + ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, + ImeEnabledInfoManagerTest::IME_KEY2, + } }); + auto ret = ImeEnabledInfoManager::GetInstance().Init(ImeEnabledInfoManagerTest::GenerateFullImeInfos(easyInfos)); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback(enabledCfg)); + } +} + +/** + * @tc.name: testInit_011 + * @tc.desc: Init:imsa reboot with new user enable table + * has sys ime、BUNDLE_NAME2, BUNDLE_NAME2 install after uninstall when imsa died + * sys ime、BUNDLE_NAME2 in new user enable table, sys ime is BASIC_MODE, + * BUNDLE_NAME2 is FULL_EXPERIENCE_MODE before imsa died + * test:bundle install after uninstall when imsa died, + * the status is DISABLED or BASIC_MODE decided by initEnabledState + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testInit_011, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testInit_011 START"); + std::vector> easyEnabledInfos = { + std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, EnabledStatus::FULL_EXPERIENCE_MODE) + }; + auto enabledCfg = ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos); + std::string userEnabledStr; + enabledCfg.Marshall(userEnabledStr); + TddUtil::GenerateUserTable(currentUserId_, ImeEnabledInfoManagerTest::ENABLE_IME, userEnabledStr); + + std::vector imeKeys = { ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, + ImeEnabledInfoManagerTest::IME_KEY2 }; + auto imeInfos = ImeEnabledInfoManagerTest::GenerateFullImeInfos(imeKeys); + auto it = std::find_if(imeInfos.begin(), imeInfos.end(), + [](const FullImeInfo &info) { return info.prop.name == ImeEnabledInfoManagerTest::BUNDLE_NAME2; }); + if (it != imeInfos.end()) { + it->installTime = ImeEnabledInfoManagerTest::RE_INSTALL_TIME; + } + auto ret = ImeEnabledInfoManager::GetInstance().Init({ { ImeEnabledInfoManagerTest::currentUserId_, imeInfos } }); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + auto iter = std::find_if(enabledCfg.enabledInfos.begin(), enabledCfg.enabledInfos.end(), + [](const ImeEnabledInfo &info) { return info.bundleName == ImeEnabledInfoManagerTest::BUNDLE_NAME2; }); + if (iter != enabledCfg.enabledInfos.end()) { + iter->enabledStatus = ImeInfoInquirer::GetInstance().GetSystemConfig().initEnabledState; + iter->installTime = ImeEnabledInfoManagerTest::RE_INSTALL_TIME; + } + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + { { ImeEnabledInfoManagerTest::currentUserId_, enabledCfg } })); + } +} + +/** + * @tc.name: testInit_012 + * @tc.desc: Init:upgrade with no table + * has BUNDLE_NAME2 + * test:enabledStatus is decided by sys enabled cfg and sys initEnabledState + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testInit_012, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testInit_012 START"); + std::map> easyInfos; + easyInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, { ImeEnabledInfoManagerTest::IME_KEY2 } }); + auto ret = ImeEnabledInfoManager::GetInstance().Init(ImeEnabledInfoManagerTest::GenerateFullImeInfos(easyInfos)); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (!ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && !ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + EnabledStatus status = EnabledStatus::DISABLED; + ret = ImeEnabledInfoManager::GetInstance().GetEnabledState( + ImeEnabledInfoManagerTest::currentUserId_, ImeEnabledInfoManagerTest::BUNDLE_NAME2, status); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(status, EnabledStatus::FULL_EXPERIENCE_MODE); + } else if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && !ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + auto enabledStatus1 = EnabledStatus::DISABLED; + if (ImeInfoInquirer::GetInstance().GetSystemConfig().initEnabledState != EnabledStatus::DISABLED) { + enabledStatus1 = EnabledStatus::FULL_EXPERIENCE_MODE; + } + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, enabledStatus1) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos))); + + } else if (!ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, EnabledStatus::BASIC_MODE) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos))); + } else { + auto enabledStatus1 = ImeInfoInquirer::GetInstance().GetSystemConfig().initEnabledState; + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, enabledStatus1) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos))); + } +} + +/** + * @tc.name: testUserAdd_001 + * @tc.desc: has cache + * test:user enable table will not be mod if has cache + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testUserAdd_001, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testUserAdd_001 START"); + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, EnabledStatus::FULL_EXPERIENCE_MODE) } }); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_ = + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos); + + std::vector imeKeys = { + ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, + ImeEnabledInfoManagerTest::IME_KEY2, + }; + auto imeInfos = ImeEnabledInfoManagerTest::GenerateFullImeInfos(imeKeys); + auto ret = ImeEnabledInfoManager::GetInstance().Add(ImeEnabledInfoManagerTest::currentUserId_, imeInfos); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeEnabledInfoManager::GetInstance().HasEnabledSwitch()) { + EXPECT_FALSE( + ImeEnabledInfoManagerTest::WaitDataShareCallback(ImeEnabledInfoManager::GetInstance().imeEnabledCfg_)); + } +} + +/** + * @tc.name: testUserDelete_001 + * @tc.desc: has cache + * test:cache will be deleted when user deleted + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testUserDelete_001, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testUserDelete_001 START"); + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, EnabledStatus::FULL_EXPERIENCE_MODE) } }); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_ = + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos); + auto ret = ImeEnabledInfoManager::GetInstance().Delete(ImeEnabledInfoManagerTest::currentUserId_); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeEnabledInfoManager::GetInstance().HasEnabledSwitch()) { + EXPECT_TRUE(ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.empty()); + } +} + +/** + * @tc.name: testBundleAdd_001 + * @tc.desc: BUNDLE_NAME1 is added + * has cache, BUNDLE_NAME1 not in cache + * test1: user enabled table will be mod when bundle add + * test2:enabledStatus of BUNDLE_NAME1 is decided by sys enabled cfg and sys initEnabledState + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testBundleAdd_001, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testBundleAdd_001 START"); + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE) } }); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_ = + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos); + auto imeInfo = ImeEnabledInfoManagerTest::GenerateFullImeInfo(ImeEnabledInfoManagerTest::IME_KEY1); + auto ret = ImeEnabledInfoManager::GetInstance().Add(ImeEnabledInfoManagerTest::currentUserId_, imeInfo); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + auto enabledStatus1 = ImeInfoInquirer::GetInstance().GetSystemConfig().initEnabledState; + std::map>> easyEnabledInfos1; + easyEnabledInfos1.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY1, enabledStatus1) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos1))); + } else if (!ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + std::map>> easyEnabledInfos1; + easyEnabledInfos1.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY1, EnabledStatus::BASIC_MODE) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos1))); + } else if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && !ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + auto enabledStatus1 = EnabledStatus::DISABLED; + if (ImeInfoInquirer::GetInstance().GetSystemConfig().initEnabledState != EnabledStatus::DISABLED) { + enabledStatus1 = EnabledStatus::FULL_EXPERIENCE_MODE; + } + std::map>> easyEnabledInfos1; + easyEnabledInfos1.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY1, enabledStatus1) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos1))); + } else { + EnabledStatus status = EnabledStatus::DISABLED; + ret = ImeEnabledInfoManager::GetInstance().GetEnabledState( + ImeEnabledInfoManagerTest::currentUserId_, ImeEnabledInfoManagerTest::BUNDLE_NAME1, status); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + EXPECT_EQ(status, EnabledStatus::FULL_EXPERIENCE_MODE); + } +} + +/** + * @tc.name: testBundleAdd_002 + * @tc.desc: BUNDLE_NAME2 is added + * has cache, BUNDLE_NAME2 in cache + * test1: user enabled table will be mod when bundle add + * test2:enabledStatus of BUNDLE_NAME1 is decided by sys enabled cfg and sys initEnabledState + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testBundleAdd_002, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testBundleAdd_002 START"); + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, EnabledStatus::FULL_EXPERIENCE_MODE) } }); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_ = + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos); + + auto imeInfo = ImeEnabledInfoManagerTest::GenerateFullImeInfo(ImeEnabledInfoManagerTest::IME_KEY2); + auto ret = ImeEnabledInfoManager::GetInstance().Add(ImeEnabledInfoManagerTest::currentUserId_, imeInfo); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + auto enabledStatus1 = ImeInfoInquirer::GetInstance().GetSystemConfig().initEnabledState; + std::map>> easyEnabledInfos1; + easyEnabledInfos1.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { + std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, enabledStatus1), + } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos1))); + } +} + +/** + * @tc.name: testBundleDelete_001 + * @tc.desc: has cache, bundle deleted in cache + * test:user enabled table will be mod when bundle delete + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testBundleDelete_001, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testBundleDelete_001 START"); + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, EnabledStatus::FULL_EXPERIENCE_MODE) } }); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_ = + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos); + + auto ret = ImeEnabledInfoManager::GetInstance().Delete( + ImeEnabledInfoManagerTest::currentUserId_, ImeEnabledInfoManagerTest::BUNDLE_NAME2); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeEnabledInfoManager::GetInstance().HasEnabledSwitch()) { + std::map>> easyEnabledInfos1; + easyEnabledInfos1.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos1))); + } +} + +/** + * @tc.name: testBundleDelete_002 + * @tc.desc: has cache, bundle deleted not in cache + * test:user enabled table will not be mod if bundle deleted not in cache + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testBundleDelete_002, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testBundleDelete_002 START"); + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, EnabledStatus::FULL_EXPERIENCE_MODE) } }); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_ = + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos); + auto ret = ImeEnabledInfoManager::GetInstance().Delete( + ImeEnabledInfoManagerTest::currentUserId_, ImeEnabledInfoManagerTest::BUNDLE_NAME1); + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + if (ImeEnabledInfoManager::GetInstance().HasEnabledSwitch()) { + EXPECT_FALSE( + ImeEnabledInfoManagerTest::WaitDataShareCallback(ImeEnabledInfoManager::GetInstance().imeEnabledCfg_)); + } +} + +/** + * @tc.name: testBundleEnabledStatusUpdate_001 + * @tc.desc: has cache + * test:user enabled table will be mod when bundle enabledStatus changed + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testBundleEnabledStatusUpdate_001, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testBundleEnabledStatusUpdate_001 START"); + std::map>> easyEnabledInfos; + easyEnabledInfos.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, EnabledStatus::FULL_EXPERIENCE_MODE) } }); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_ = + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos); + + auto ret = ImeEnabledInfoManager::GetInstance().Update(ImeEnabledInfoManagerTest::currentUserId_, + ImeEnabledInfoManagerTest::BUNDLE_NAME2, "", EnabledStatus::DISABLED); + if (ImeEnabledInfoManager::GetInstance().HasEnabledSwitch()) { + EXPECT_EQ(ret, ErrorCode::NO_ERROR); + } else { + EXPECT_EQ(ret, ErrorCode::ERROR_ENABLE_IME); + } + if (ImeInfoInquirer::GetInstance().GetSystemConfig().enableInputMethodFeature + && ImeInfoInquirer::GetInstance().GetSystemConfig().enableFullExperienceFeature) { + std::map>> easyEnabledInfos1; + easyEnabledInfos1.insert({ ImeEnabledInfoManagerTest::currentUserId_, + { std::make_pair(ImeEnabledInfoManagerTest::DEFAULT_IME_KEY, EnabledStatus::BASIC_MODE), + std::make_pair(ImeEnabledInfoManagerTest::IME_KEY2, EnabledStatus::DISABLED) } }); + EXPECT_TRUE(ImeEnabledInfoManagerTest::WaitDataShareCallback( + ImeEnabledInfoManagerTest::GenerateAllEnabledCfg(easyEnabledInfos1))); + } +} + +/** + * @tc.name: testBundleEnabledStatusUpdate_002 + * @tc.desc: test:default ime not allow DISABLED + * @tc.type: FUNC + * @tc.require: + * @tc.author: chenyu + */ +HWTEST_F(ImeEnabledInfoManagerTest, testBundleEnabledStatusUpdate_002, TestSize.Level0) +{ + IMSA_HILOGI("ImeEnabledInfoManagerTest testBundleEnabledStatusUpdate_002 START"); + auto ret = ImeEnabledInfoManager::GetInstance().Update(ImeEnabledInfoManagerTest::currentUserId_, + ImeEnabledInfoManagerTest::defaultImeProp_.bundleName, "", EnabledStatus::DISABLED); + EXPECT_EQ(ret, ErrorCode::ERROR_DISABLE_SYS_IME); +} +} // namespace MiscServices +} // namespace OHOS diff --git a/test/unittest/cpp_test/src/imf_hisysevent_report_test.cpp b/test/unittest/cpp_test/src/imf_hisysevent_report_test.cpp index 7df81f57a2c9471b397a2dad8dcce9ae6428f5fc..2fbd05f56b3794997759ef569c8ac15edaf19704 100644 --- a/test/unittest/cpp_test/src/imf_hisysevent_report_test.cpp +++ b/test/unittest/cpp_test/src/imf_hisysevent_report_test.cpp @@ -507,6 +507,7 @@ void ImfHiSysEventReporterTest::TestStartInput(const uint8_t *data, size_t size) .SetIsShowKeyboard(paramIsShowkeyboard) .SetEventCode(paramIEventCode) .SetErrCode(errCode) + .SetImeCbTime(rand()) .Build(); ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::IME_START_INPUT, *info); } @@ -527,6 +528,7 @@ void ImfHiSysEventReporterTest::TestBaseTextOperation(const uint8_t *data, size_ .SetClientType(PARAM_CLIENT_TYPE) .SetEventCode(paramIEventCode) .SetErrCode(errCode) + .SetBaseTextOperatorTime(rand()) .Build(); ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::BASE_TEXT_OPERATOR, *info); } diff --git a/test/unittest/cpp_test/src/input_method_controller_test.cpp b/test/unittest/cpp_test/src/input_method_controller_test.cpp index 9cdd6893d1a0c0ef7dfc5710e043b9094ddede62..620a9bced561350f862e87a9c225136ea13ad519 100644 --- a/test/unittest/cpp_test/src/input_method_controller_test.cpp +++ b/test/unittest/cpp_test/src/input_method_controller_test.cpp @@ -16,13 +16,12 @@ #define protected public #include "input_method_controller.h" -#include "enable_ime_data_parser.h" +#include "ime_enabled_info_manager.h" +#include "ime_info_inquirer.h" #include "input_data_channel_stub.h" #include "input_method_ability.h" #include "input_method_system_ability.h" -#include "ime_info_inquirer.h" #include "task_manager.h" -#include "security_mode_parser.h" #undef private #include @@ -1662,7 +1661,6 @@ HWTEST_F(InputMethodControllerTest, testIMCReset, TestSize.Level0) EXPECT_EQ(inputMethodController_->abilityManager_, nullptr); inputMethodController_->abilityManager_ = imsaProxy_; } - /** * @tc.name: testGetInputMethodState_001 * @tc.desc: IMA @@ -1672,14 +1670,19 @@ HWTEST_F(InputMethodControllerTest, testIMCReset, TestSize.Level0) HWTEST_F(InputMethodControllerTest, testGetInputMethodState_001, TestSize.Level0) { IMSA_HILOGI("InputMethodControllerTest GetInputMethodState_001 Test START"); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.clear(); + ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = true; + ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = true; + ImeEnabledInfo info; + info.bundleName = TddUtil::currentBundleNameMock_; + info.enabledStatus = EnabledStatus::FULL_EXPERIENCE_MODE; + ImeEnabledCfg cfg; + cfg.enabledInfos.push_back(info); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.insert({ imsa_->userId_, cfg }); EnabledStatus status = EnabledStatus::DISABLED; - SecurityModeParser::GetInstance()->fullModeList_.push_back(TddUtil::currentBundleNameMock_); - EnableImeDataParser::GetInstance()->enableList_[IME_KEY].push_back(TddUtil::currentBundleNameMock_); auto ret = inputMethodController_->GetInputMethodState(status); EXPECT_EQ(ret, ErrorCode::NO_ERROR); EXPECT_TRUE(status == EnabledStatus::FULL_EXPERIENCE_MODE); - SecurityModeParser::GetInstance()->fullModeList_.clear(); - EnableImeDataParser::GetInstance()->enableList_.clear(); } /** @@ -1691,16 +1694,19 @@ HWTEST_F(InputMethodControllerTest, testGetInputMethodState_001, TestSize.Level0 HWTEST_F(InputMethodControllerTest, testGetInputMethodState_002, TestSize.Level0) { IMSA_HILOGI("InputMethodControllerTest GetInputMethodState_002 Test START"); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.clear(); + ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = false; + ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = false; + ImeEnabledInfo info; + info.bundleName = TddUtil::currentBundleNameMock_; + info.enabledStatus = EnabledStatus::BASIC_MODE; + ImeEnabledCfg cfg; + cfg.enabledInfos.push_back(info); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.insert({ imsa_->userId_, cfg }); EnabledStatus status = EnabledStatus::DISABLED; - ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = true; - ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = true; - EnableImeDataParser::GetInstance()->enableList_.clear(); - SecurityModeParser::GetInstance()->fullModeList_.clear(); auto ret = inputMethodController_->GetInputMethodState(status); EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_TRUE(status != EnabledStatus::FULL_EXPERIENCE_MODE); - ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = false; - ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = false; + EXPECT_TRUE(status == EnabledStatus::FULL_EXPERIENCE_MODE); } /** @@ -1712,16 +1718,19 @@ HWTEST_F(InputMethodControllerTest, testGetInputMethodState_002, TestSize.Level0 HWTEST_F(InputMethodControllerTest, testGetInputMethodState_003, TestSize.Level0) { IMSA_HILOGI("InputMethodControllerTest GetInputMethodState_003 Test START"); - EnabledStatus status = EnabledStatus::DISABLED; + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.clear(); ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = true; - ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = true; - EnableImeDataParser::GetInstance()->enableList_[IME_KEY].push_back(TddUtil::currentBundleNameMock_); + ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = false; + ImeEnabledInfo info; + info.bundleName = TddUtil::currentBundleNameMock_; + info.enabledStatus = EnabledStatus::BASIC_MODE; + ImeEnabledCfg cfg; + cfg.enabledInfos.push_back(info); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.insert({ imsa_->userId_, cfg }); + EnabledStatus status = EnabledStatus::DISABLED; auto ret = inputMethodController_->GetInputMethodState(status); EXPECT_EQ(ret, ErrorCode::NO_ERROR); EXPECT_TRUE(status == EnabledStatus::BASIC_MODE); - ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = false; - ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = false; - EnableImeDataParser::GetInstance()->enableList_.clear(); } /** @@ -1733,13 +1742,19 @@ HWTEST_F(InputMethodControllerTest, testGetInputMethodState_003, TestSize.Level0 HWTEST_F(InputMethodControllerTest, testGetInputMethodState_004, TestSize.Level0) { IMSA_HILOGI("InputMethodControllerTest GetInputMethodState_004 Test START"); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.clear(); + ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = false; + ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = true; + ImeEnabledInfo info; + info.bundleName = TddUtil::currentBundleNameMock_; + info.enabledStatus = EnabledStatus::BASIC_MODE; + ImeEnabledCfg cfg; + cfg.enabledInfos.push_back(info); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.insert({ imsa_->userId_, cfg }); EnabledStatus status = EnabledStatus::DISABLED; - ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = true; - ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = false; auto ret = inputMethodController_->GetInputMethodState(status); EXPECT_EQ(ret, ErrorCode::NO_ERROR); EXPECT_TRUE(status == EnabledStatus::BASIC_MODE); - ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = false; } /** @@ -1750,10 +1765,18 @@ HWTEST_F(InputMethodControllerTest, testGetInputMethodState_004, TestSize.Level0 */ HWTEST_F(InputMethodControllerTest, testGetInputMethodState_005, TestSize.Level0) { - IMSA_HILOGI("InputMethodControllerTest GetInputMethodState_005 Test START"); + IMSA_HILOGI("InputMethodControllerTest testGetInputMethodState_005 Test START"); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.clear(); + ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = true; + ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = true; + ImeInfoInquirer::GetInstance().systemConfig_.sysSpecialIme = TddUtil::currentBundleNameMock_; + ImeEnabledInfo info; + info.bundleName = TddUtil::currentBundleNameMock_; + info.enabledStatus = EnabledStatus::BASIC_MODE; + ImeEnabledCfg cfg; + cfg.enabledInfos.push_back(info); + ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.insert({ imsa_->userId_, cfg }); EnabledStatus status = EnabledStatus::DISABLED; - ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = false; - ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = false; auto ret = inputMethodController_->GetInputMethodState(status); EXPECT_EQ(ret, ErrorCode::NO_ERROR); EXPECT_TRUE(status == EnabledStatus::FULL_EXPERIENCE_MODE); @@ -1770,9 +1793,9 @@ HWTEST_F(InputMethodControllerTest, testIsDefaultImeSetAndEnableIme, TestSize.Le IMSA_HILOGI("IMC testIsDefaultImeSetAndEnableIme Test START"); auto ret = inputMethodController_->IsDefaultImeSet(); EXPECT_FALSE(ret); - const std::string bundleName = ""; - ret = inputMethodController_->EnableIme(bundleName); - EXPECT_FALSE(ret); + const std::string bundleName; + auto enableRet = inputMethodController_->EnableIme(bundleName); + EXPECT_NE(enableRet, ErrorCode::NO_ERROR); } /** diff --git a/test/unittest/cpp_test/src/input_method_private_member_test.cpp b/test/unittest/cpp_test/src/input_method_private_member_test.cpp index 83bf8cfccf55b833af9ff0797009d49396fa2f60..0f4026e463a130368d59437ba9931a36b2695bc6 100644 --- a/test/unittest/cpp_test/src/input_method_private_member_test.cpp +++ b/test/unittest/cpp_test/src/input_method_private_member_test.cpp @@ -637,7 +637,7 @@ HWTEST_F(InputMethodPrivateMemberTest, III_TestListEnabledInputMethod_001, TestS // currentIme is empty std::vector props; auto currentUserId = TddUtil::GetCurrentUserId(); - auto ret = ImeInfoInquirer::GetInstance().ListEnabledInputMethod(currentUserId, props, false); + auto ret = ImeInfoInquirer::GetInstance().ListEnabledInputMethod(currentUserId, props); EXPECT_EQ(ret, ErrorCode::NO_ERROR); } @@ -669,7 +669,7 @@ HWTEST_F(InputMethodPrivateMemberTest, III_TestListInputMethod_001, TestSize.Lev { IMSA_HILOGI("InputMethodPrivateMemberTest III_TestListInputMethod_001 TEST START"); std::vector props; - auto ret = ImeInfoInquirer::GetInstance().ListInputMethod(60, InputMethodStatus(10), props, false); + auto ret = ImeInfoInquirer::GetInstance().ListInputMethod(60, InputMethodStatus(10), props); EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS); } @@ -1303,15 +1303,15 @@ HWTEST_F(InputMethodPrivateMemberTest, BranchCoverage003, TestSize.Level0) EXPECT_TRUE(ret3 == ""); std::vector props = {}; - ret = inquirer.ListEnabledInputMethod(INVALID_USER_ID, props, false); + ret = inquirer.ListEnabledInputMethod(INVALID_USER_ID, props); EXPECT_NE(ret, ErrorCode::NO_ERROR); - ret = inquirer.ListDisabledInputMethod(INVALID_USER_ID, props, false); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); + ret = inquirer.ListDisabledInputMethod(INVALID_USER_ID, props); + EXPECT_NE(ret, ErrorCode::NO_ERROR); SwitchInfo switchInfo; uint32_t cacheCount = -1; - ret = inquirer.GetSwitchInfoBySwitchCount(switchInfo, INVALID_USER_ID, false, cacheCount); + ret = inquirer.GetSwitchInfoBySwitchCount(switchInfo, INVALID_USER_ID, cacheCount); EXPECT_NE(ret, ErrorCode::NO_ERROR); } @@ -1326,9 +1326,9 @@ HWTEST_F(InputMethodPrivateMemberTest, BranchCoverage004, TestSize.Level0) auto userSession = std::make_shared(MAIN_USER_ID); sptr observer; std::shared_ptr helper; - std::string invaildString = ""; + std::string invaildString; pid_t pid { -1 }; - auto ret = SettingsDataUtils::GetInstance()->RegisterObserver(observer); + auto ret = SettingsDataUtils::GetInstance()->RegisterObserver(invaildString, observer); EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER); ret = SettingsDataUtils::GetInstance()->GetStringValue(invaildString, invaildString, invaildString); EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER); diff --git a/test/unittest/cpp_test/src/json_operate_test.cpp b/test/unittest/cpp_test/src/json_operate_test.cpp index f47520e354ce2bf86e773de4174921fb912895cf..d24a1cb8e5f6ef4c0c8c6c27b9d026bc47287a72 100644 --- a/test/unittest/cpp_test/src/json_operate_test.cpp +++ b/test/unittest/cpp_test/src/json_operate_test.cpp @@ -15,10 +15,8 @@ #define private public #define protected public -#include "enable_ime_data_parser.h" #include "ime_cfg_manager.h" #include "ime_info_inquirer.h" -#include "security_mode_parser.h" #include "sys_cfg_parser.h" #undef private @@ -80,103 +78,6 @@ public: void TearDown() { } }; -/** - * @tc.name: testParseEnableIme001 - * @tc.desc: parse enableIme - * @tc.type: FUNC - * @tc.require: - * @tc.author: chenyu - */ -HWTEST_F(JsonOperateTest, testParseEnableIme001, TestSize.Level1) -{ - IMSA_HILOGI("JsonOperateTest testParseEnableIme001 START"); - std::vector enableVec; - auto ret = EnableImeDataParser::GetInstance()->ParseEnableIme(ENABLE_IME, 100, enableVec); - ASSERT_TRUE(ret); - ASSERT_EQ(enableVec.size(), 3); - EXPECT_EQ(enableVec[0], "testIme"); - EXPECT_EQ(enableVec[1], "testIme1"); - EXPECT_EQ(enableVec[2], "testIme2"); - - std::vector enableVec1; - ret = EnableImeDataParser::GetInstance()->ParseEnableIme(ENABLE_IME, 101, enableVec1); - ASSERT_TRUE(ret); - ASSERT_EQ(enableVec1.size(), 1); - EXPECT_EQ(enableVec1[0], "testIme3"); - - std::vector enableVec2; - ret = EnableImeDataParser::GetInstance()->ParseEnableIme(ENABLE_IME, 102, enableVec2); - EXPECT_TRUE(ret); - EXPECT_TRUE(enableVec2.empty()); - - std::vector enableVec3; - ret = EnableImeDataParser::GetInstance()->ParseEnableIme(ENABLE_IME, 104, enableVec3); - EXPECT_TRUE(ret); - EXPECT_TRUE(enableVec3.empty()); -} -/** - * @tc.name: testParseEnableKeyboard001 - * @tc.desc: parse enableKeyboard - * @tc.type: FUNC - * @tc.require: - * @tc.author: chenyu - */ -HWTEST_F(JsonOperateTest, testParseEnableKeyboard001, TestSize.Level1) -{ - IMSA_HILOGI("JsonOperateTest testParseEnableKeyboard001 START"); - std::vector enableVec; - auto ret = EnableImeDataParser::GetInstance()->ParseEnableKeyboard(ENABLE_KEYBOARD, 100, enableVec); - ASSERT_TRUE(ret); - ASSERT_EQ(enableVec.size(), 2); - EXPECT_EQ(enableVec[0], "testKeyboard"); - EXPECT_EQ(enableVec[1], "testKeyboard1"); - - std::vector enableVec1; - ret = EnableImeDataParser::GetInstance()->ParseEnableKeyboard(ENABLE_KEYBOARD, 101, enableVec1); - ASSERT_TRUE(ret); - ASSERT_EQ(enableVec1.size(), 1); - EXPECT_EQ(enableVec1[0], "testKeyboard2"); - - std::vector enableVec2; - ret = EnableImeDataParser::GetInstance()->ParseEnableKeyboard(ENABLE_KEYBOARD, 105, enableVec2); - EXPECT_TRUE(ret); - EXPECT_TRUE(enableVec2.empty()); - - std::vector enableVec3; - ret = EnableImeDataParser::GetInstance()->ParseEnableKeyboard(ENABLE_KEYBOARD, 104, enableVec3); - EXPECT_TRUE(ret); - EXPECT_TRUE(enableVec3.empty()); -} - -/** - * @tc.name: testParseSecurityMode001 - * @tc.desc: parse securityMode - * @tc.type: FUNC - * @tc.require: - * @tc.author: chenyu - */ -HWTEST_F(JsonOperateTest, testParseSecurityMode001, TestSize.Level1) -{ - IMSA_HILOGI("JsonOperateTest testParseSecurityMode001 START"); - SecurityModeParser::GetInstance()->fullModeList_.clear(); - auto ret = SecurityModeParser::GetInstance()->ParseSecurityMode(JsonOperateTest::SECURITY_MODE, 100); - ASSERT_TRUE(ret); - auto secMode = SecurityModeParser::GetInstance()->fullModeList_; - ASSERT_EQ(secMode.size(), 2); - EXPECT_EQ(secMode[0], "testIme"); - EXPECT_EQ(secMode[1], "testIme3"); - - SecurityModeParser::GetInstance()->fullModeList_.clear(); - ret = SecurityModeParser::GetInstance()->ParseSecurityMode(JsonOperateTest::SECURITY_MODE, 102); - EXPECT_TRUE(ret); - EXPECT_TRUE(SecurityModeParser::GetInstance()->fullModeList_.empty()); - - SecurityModeParser::GetInstance()->fullModeList_.clear(); - ret = SecurityModeParser::GetInstance()->ParseSecurityMode(JsonOperateTest::SECURITY_MODE, 105); - EXPECT_TRUE(ret); - EXPECT_TRUE(SecurityModeParser::GetInstance()->fullModeList_.empty()); -} - /** * @tc.name: testParseImePersistCfg001 * @tc.desc: parse imePersistCfg @@ -364,11 +265,9 @@ HWTEST_F(JsonOperateTest, testListDisabledInputMethod, TestSize.Level1) IMSA_HILOGI("JsonOperateTest testListDisabledInputMethod START"); int32_t userId = 1234567890; std::vector props; - bool enableOn = false; - auto ret = ImeInfoInquirer::GetInstance().ListDisabledInputMethod(userId, props, enableOn); - ASSERT_EQ(ret, ErrorCode::NO_ERROR); - enableOn = true; - ret = ImeInfoInquirer::GetInstance().ListDisabledInputMethod(userId, props, enableOn); + auto ret = ImeInfoInquirer::GetInstance().ListDisabledInputMethod(userId, props); + ASSERT_NE(ret, ErrorCode::NO_ERROR); + ret = ImeInfoInquirer::GetInstance().ListDisabledInputMethod(userId, props); ASSERT_NE(ret, ErrorCode::NO_ERROR); } @@ -384,8 +283,7 @@ HWTEST_F(JsonOperateTest, testGetSwitchInfoBySwitchCount, TestSize.Level1) SwitchInfo switchInfo; uint32_t cacheCount = 987654321; int32_t userId = 1234567890; - bool enableOn = false; - auto ret = ImeInfoInquirer::GetInstance().GetSwitchInfoBySwitchCount(switchInfo, userId, enableOn, cacheCount); + auto ret = ImeInfoInquirer::GetInstance().GetSwitchInfoBySwitchCount(switchInfo, userId, cacheCount); ASSERT_NE(ret, ErrorCode::NO_ERROR); } diff --git a/test/unittest/cpp_test/src/security_mode_parser_test.cpp b/test/unittest/cpp_test/src/security_mode_parser_test.cpp deleted file mode 100644 index 2871e5444f7cf599d0b6c47ab36de733a453d62f..0000000000000000000000000000000000000000 --- a/test/unittest/cpp_test/src/security_mode_parser_test.cpp +++ /dev/null @@ -1,272 +0,0 @@ -/* - * Copyright (C) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define private public -#define protected public - -#include "input_method_system_ability.h" -#include "security_mode_parser.h" -#include "settings_data_utils.h" - -#undef private - -#include - -using namespace testing::ext; -using namespace OHOS::DataShare; -namespace OHOS { -namespace MiscServices { -const std::string SECURITY_KEY = "settings.inputmethod.full_experience"; -constexpr uint32_t USER_100_TOTAL_COUNT = 3; -constexpr uint32_t USER_101_TOTAL_COUNT = 1; -class SecurityModeParserTest : public testing::Test { -public: - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); - static std::shared_ptr helper_; - static std::shared_ptr resultSet_; - static sptr service_; - static constexpr int32_t USER_ID = 100; -}; -std::shared_ptr SecurityModeParserTest::helper_; -std::shared_ptr SecurityModeParserTest::resultSet_; -sptr SecurityModeParserTest::service_ { nullptr }; -void SecurityModeParserTest::SetUpTestCase(void) -{ - IMSA_HILOGI("SecurityModeParserTest::SetUpTestCase"); - std::vector columns = { "VALUE" }; - helper_ = DataShare::DataShareHelper::Creator(nullptr, "tsetUri", "tsetUri"); - DataSharePredicates predicates; - Uri uri("tsetUri"); - resultSet_ = helper_->Query(uri, predicates, columns); - SecurityModeParser::GetInstance()->Initialize(USER_ID); - - service_ = new (std::nothrow) InputMethodSystemAbility(); - if (service_ == nullptr) { - IMSA_HILOGE("failed to new service"); - return; - } - service_->OnStart(); - service_->userId_ = USER_ID; -} - -void SecurityModeParserTest::TearDownTestCase(void) -{ - service_->OnStop(); - IMSA_HILOGI("SecurityModeParserTest::TearDownTestCase"); -} - -void SecurityModeParserTest::SetUp() -{ - IMSA_HILOGI("SecurityModeParserTest::SetUp"); - resultSet_->strValue_ = "{\"fullExperienceList\" : {\"100\" : [ \"xiaoyiIme\", \"baiduIme\", " - "\"sougouIme\"],\"101\" : [\"sougouIme\"]}}"; - - SecurityModeParser::GetInstance()->fullModeList_.clear(); -} - -void SecurityModeParserTest::TearDown() -{ - IMSA_HILOGI("SecurityModeParserTest::TearDown"); -} - -/** - * @tc.name: testGetFullModeList_001 - * @tc.desc: Get 101 user fullModeList - * @tc.type: FUNC - * @tc.require: - * @tc.author: guojin - */ -HWTEST_F(SecurityModeParserTest, testGetFullModeList_001, TestSize.Level1) -{ - IMSA_HILOGI("SecurityModeParserTest testGetFullModeList_001 START"); - int32_t ret = SecurityModeParser::GetInstance()->UpdateFullModeList(101); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(SecurityModeParser::GetInstance()->fullModeList_.size(), USER_101_TOTAL_COUNT); - if (SecurityModeParser::GetInstance()->fullModeList_.size() == USER_101_TOTAL_COUNT) { - EXPECT_EQ(SecurityModeParser::GetInstance()->fullModeList_[0], "sougouIme"); - } -} - -/** - * @tc.name: testGetFullModeList_002 - * @tc.desc: Get 100 user fullModeList - * @tc.type: FUNC - * @tc.require: - * @tc.author: guojin - */ -HWTEST_F(SecurityModeParserTest, testGetFullModeList_002, TestSize.Level1) -{ - IMSA_HILOGI("SecurityModeParserTest testGetFullModeList_002 START"); - int32_t ret = SecurityModeParser::GetInstance()->UpdateFullModeList(SecurityModeParserTest::USER_ID); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(SecurityModeParser::GetInstance()->fullModeList_.size(), USER_100_TOTAL_COUNT); - if (SecurityModeParser::GetInstance()->fullModeList_.size() == USER_100_TOTAL_COUNT) { - EXPECT_EQ(SecurityModeParser::GetInstance()->fullModeList_[0], "xiaoyiIme"); - EXPECT_EQ(SecurityModeParser::GetInstance()->fullModeList_[1], "baiduIme"); - EXPECT_EQ(SecurityModeParser::GetInstance()->fullModeList_[2], "sougouIme"); - } -} - -/** - * @tc.name: testGetSecurityMode_001 - * @tc.desc: Get 100 user security mode - * @tc.type: FUNC - * @tc.require: - * @tc.author: guojin - */ -HWTEST_F(SecurityModeParserTest, testGetSecurityMode_001, TestSize.Level1) -{ - IMSA_HILOGI("SecurityModeParserTest testGetSecurityMode_001 START"); - int32_t ret = SecurityModeParser::GetInstance()->UpdateFullModeList(SecurityModeParserTest::USER_ID); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - SecurityMode security = - SecurityModeParser::GetInstance()->GetSecurityMode("xiaoyiIme", SecurityModeParserTest::USER_ID); - EXPECT_EQ(static_cast(security), 1); -} - -/** - * @tc.name: testGetSecurityMode_002 - * @tc.desc: Get 100 user security mode - * @tc.type: FUNC - * @tc.require: - * @tc.author: guojin - */ -HWTEST_F(SecurityModeParserTest, testGetSecurityMode_002, TestSize.Level1) -{ - IMSA_HILOGI("SecurityModeParserTest testGetSecurityMode_002 START"); - int32_t ret = SecurityModeParser::GetInstance()->UpdateFullModeList(SecurityModeParserTest::USER_ID); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - SecurityMode security = SecurityModeParser::GetInstance()->GetSecurityMode("test", SecurityModeParserTest::USER_ID); - EXPECT_EQ(static_cast(security), 0); -} - -/** - * @tc.name: testGetSecurityMode_003 - * @tc.desc: Get 100 user security mode - * @tc.type: FUNC - * @tc.require: - * @tc.author: guojin - */ -HWTEST_F(SecurityModeParserTest, testGetSecurityMode_003, TestSize.Level1) -{ - IMSA_HILOGI("SecurityModeParserTest testGetSecurityMode_003 START"); - service_->enableSecurityMode_ = false; - int32_t securityMode; - auto ret = service_->GetSecurityMode(securityMode); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(securityMode, 1); -} - -/** - * @tc.name: testGetSecurityMode_004 - * @tc.desc: Get 100 user security mode - * @tc.type: FUNC - * @tc.require: - * @tc.author: guojin - */ -HWTEST_F(SecurityModeParserTest, testGetSecurityMode_004, TestSize.Level1) -{ - IMSA_HILOGI("SecurityModeParserTest testGetSecurityMode_004 START"); - service_->enableSecurityMode_ = true; - int32_t securityMode; - auto ret = service_->GetSecurityMode(securityMode); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); - EXPECT_EQ(securityMode, 1); -} - -/** - * @tc.name: testInitialize - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecurityModeParserTest, testInitialize, TestSize.Level1) -{ - IMSA_HILOGI("SecurityModeParserTest testInitialize START"); - auto ret = SecurityModeParser::GetInstance()->Initialize(SecurityModeParserTest::USER_ID); - EXPECT_EQ(ret, ErrorCode::NO_ERROR); -} - -/** - * @tc.name: testIsExpired - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecurityModeParserTest, testIsExpired, TestSize.Level1) -{ - IMSA_HILOGI("SecurityModeParserTest testIsExpired START"); - auto ret = SecurityModeParser::GetInstance()->IsExpired("test"); - EXPECT_FALSE(ret); -} - -/** - * @tc.name: testRegisterObserver - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecurityModeParserTest, testRegisterObserver, TestSize.Level1) -{ - IMSA_HILOGI("SecurityModeParserTest testRegisterObserver START"); - sptr observer = nullptr; - auto ret = SettingsDataUtils::GetInstance()->RegisterObserver(observer); - EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER); -} - -/** - * @tc.name: testReleaseDataShareHelper - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecurityModeParserTest, testReleaseDataShareHelper, TestSize.Level1) -{ - IMSA_HILOGI("SecurityModeParserTest testReleaseDataShareHelper START"); - std::shared_ptr helper; - auto ret = SettingsDataUtils::GetInstance()->ReleaseDataShareHelper(helper); - EXPECT_TRUE(ret); - helper = nullptr; - ret = SettingsDataUtils::GetInstance()->ReleaseDataShareHelper(helper); - EXPECT_TRUE(ret); -} - -/** - * @tc.name: testParseSecurityMode_001 - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecurityModeParserTest, testParseSecurityMode_001, TestSize.Level1) -{ - IMSA_HILOGI("SecurityModeParserTest testParseSecurityMode_001 START"); - auto ret = SecurityModeParser::GetInstance()->ParseSecurityMode("", SecurityModeParserTest::USER_ID); - EXPECT_FALSE(ret); -} - -/** - * @tc.name: testParseSecurityMode_002 - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecurityModeParserTest, testParseSecurityMode_002, TestSize.Level1) -{ - IMSA_HILOGI("SecurityModeParserTest testParseSecurityMode_002 START"); - auto ret = SecurityModeParser::GetInstance()->ParseSecurityMode( - "{\"fullExperienceList\" : {\"100\" : [" - "\"xiaoyiIme\", \"baiduIme\", \"sougouIme\"],\"101\" : [\"sougouIme\"]}}", - SecurityModeParserTest::USER_ID); - EXPECT_TRUE(ret); -} -} // namespace MiscServices -} // namespace OHOS \ No newline at end of file