1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "utils.h"
16
17 namespace OHOS::MiscServices {
18 const std::map<int32_t, IMFErrorCode> Utils::ERROR_CODE_MAP = {
19 { ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED, EXCEPTION_CONTROLLER },
20 { ErrorCode::ERROR_STATUS_PERMISSION_DENIED, EXCEPTION_PERMISSION },
21 { ErrorCode::ERROR_STATUS_SYSTEM_PERMISSION, EXCEPTION_SYSTEM_PERMISSION },
22 { ErrorCode::ERROR_REMOTE_CLIENT_DIED, EXCEPTION_IMCLIENT },
23 { ErrorCode::ERROR_CLIENT_NOT_FOUND, EXCEPTION_IMCLIENT },
24 { ErrorCode::ERROR_CLIENT_NULL_POINTER, EXCEPTION_IMCLIENT },
25 { ErrorCode::ERROR_CLIENT_NOT_FOCUSED, EXCEPTION_IMCLIENT },
26 { ErrorCode::ERROR_CLIENT_NOT_EDITABLE, EXCEPTION_EDITABLE },
27 { ErrorCode::ERROR_CLIENT_NOT_BOUND, EXCEPTION_DETACHED },
28 { ErrorCode::ERROR_CLIENT_ADD_FAILED, EXCEPTION_IMCLIENT },
29 { ErrorCode::ERROR_NULL_POINTER, EXCEPTION_IMMS },
30 { ErrorCode::ERROR_BAD_PARAMETERS, EXCEPTION_IMMS },
31 { ErrorCode::ERROR_SERVICE_START_FAILED, EXCEPTION_IMMS },
32 { ErrorCode::ERROR_KBD_SHOW_FAILED, EXCEPTION_IMMS },
33 { ErrorCode::ERROR_KBD_HIDE_FAILED, EXCEPTION_IMMS },
34 { ErrorCode::ERROR_IME_NOT_STARTED, EXCEPTION_IMMS },
35 { ErrorCode::ERROR_EX_NULL_POINTER, EXCEPTION_IMMS },
36 { ErrorCode::ERROR_PERSIST_CONFIG, EXCEPTION_CONFPERSIST },
37 { ErrorCode::ERROR_PACKAGE_MANAGER, EXCEPTION_PACKAGEMANAGER },
38 { ErrorCode::ERROR_EX_UNSUPPORTED_OPERATION, EXCEPTION_IMMS },
39 { ErrorCode::ERROR_EX_SERVICE_SPECIFIC, EXCEPTION_IMMS },
40 { ErrorCode::ERROR_EX_PARCELABLE, EXCEPTION_IMMS },
41 { ErrorCode::ERROR_EX_ILLEGAL_ARGUMENT, EXCEPTION_IMMS },
42 { ErrorCode::ERROR_EX_ILLEGAL_STATE, EXCEPTION_IMMS },
43 { ErrorCode::ERROR_IME_START_INPUT_FAILED, EXCEPTION_IMMS },
44 { ErrorCode::ERROR_NOT_IME, EXCEPTION_IME },
45 { ErrorCode::ERROR_IME, EXCEPTION_IMENGINE },
46 { ErrorCode::ERROR_PARAMETER_CHECK_FAILED, EXCEPTION_PARAMCHECK },
47 { ErrorCode::ERROR_NOT_DEFAULT_IME, EXCEPTION_DEFAULTIME },
48 { ErrorCode::ERROR_ENABLE_IME, EXCEPTION_IMMS },
49 { ErrorCode::ERROR_NOT_CURRENT_IME, EXCEPTION_IMMS },
50 { ErrorCode::ERROR_PANEL_NOT_FOUND, EXCEPTION_PANEL_NOT_FOUND },
51 { ErrorCode::ERROR_WINDOW_MANAGER, EXCEPTION_WINDOW_MANAGER },
52 { ErrorCode::ERROR_GET_TEXT_CONFIG, EXCEPTION_IMCLIENT },
53 { ErrorCode::ERROR_INVALID_PRIVATE_COMMAND_SIZE, EXCEPTION_PARAMCHECK },
54 { ErrorCode::ERROR_TEXT_LISTENER_ERROR, EXCEPTION_IMCLIENT },
55 { ErrorCode::ERROR_TEXT_PREVIEW_NOT_SUPPORTED, EXCEPTION_TEXT_PREVIEW_NOT_SUPPORTED },
56 { ErrorCode::ERROR_INVALID_RANGE, EXCEPTION_PARAMCHECK },
57 { ErrorCode::ERROR_SECURITY_MODE_OFF, EXCEPTION_BASIC_MODE },
58 { ErrorCode::ERROR_MSG_HANDLER_NOT_REGIST, EXCEPTION_REQUEST_NOT_ACCEPT },
59 { ErrorCode::ERROR_MESSAGE_HANDLER, EXCEPTION_IMCLIENT },
60 { ErrorCode::ERROR_INVALID_ARRAY_BUFFER_SIZE, EXCEPTION_PARAMCHECK },
61 { ErrorCode::ERROR_INVALID_PANEL_TYPE, EXCEPTION_INVALID_PANEL_TYPE_FLAG },
62 { ErrorCode::ERROR_INVALID_PANEL_FLAG, EXCEPTION_INVALID_PANEL_TYPE_FLAG },
63 { ErrorCode::ERROR_IMA_CHANNEL_NULLPTR, EXCEPTION_IMCLIENT },
64 { ErrorCode::ERROR_IPC_REMOTE_NULLPTR, EXCEPTION_IMMS },
65 { ErrorCode::ERROR_IMA_NULLPTR, EXCEPTION_IMMS },
66 { ErrorCode::ERROR_IMSA_INPUT_TYPE_NOT_FOUND, EXCEPTION_IMMS },
67 { ErrorCode::ERROR_IMSA_DEFAULT_IME_NOT_FOUND, EXCEPTION_IMMS },
68 { ErrorCode::ERROR_IMSA_CLIENT_INPUT_READY_FAILED, EXCEPTION_IMMS },
69 { ErrorCode::ERROR_IMSA_MALLOC_FAILED, EXCEPTION_IMMS },
70 { ErrorCode::ERROR_IMSA_NULLPTR, EXCEPTION_IMMS },
71 { ErrorCode::ERROR_IMSA_USER_SESSION_NOT_FOUND, EXCEPTION_IMMS },
72 { ErrorCode::ERROR_IMSA_GET_IME_INFO_FAILED, EXCEPTION_IMMS },
73 { ErrorCode::ERROR_IMSA_IME_TO_START_NULLPTR, EXCEPTION_IMMS },
74 { ErrorCode::ERROR_IMSA_REBOOT_OLD_IME_NOT_STOP, EXCEPTION_IMMS },
75 { ErrorCode::ERROR_IMSA_IME_EVENT_CONVERT_FAILED, EXCEPTION_IMMS },
76 { ErrorCode::ERROR_IMSA_IME_CONNECT_FAILED, EXCEPTION_IMMS },
77 { ErrorCode::ERROR_IMSA_IME_DISCONNECT_FAILED, EXCEPTION_IMMS },
78 { ErrorCode::ERROR_IMSA_IME_START_TIMEOUT, EXCEPTION_IMMS },
79 { ErrorCode::ERROR_IMSA_IME_START_MORE_THAN_EIGHT_SECOND, EXCEPTION_IMMS },
80 { ErrorCode::ERROR_IMSA_FORCE_STOP_IME_TIMEOUT, EXCEPTION_IMMS },
81 { ErrorCode::ERROR_IMC_NULLPTR, EXCEPTION_IMMS },
82 { ErrorCode::ERROR_DEVICE_UNSUPPORTED, EXCEPTION_UNSUPPORTED },
83 { ErrorCode::ERROR_IME_NOT_FOUND, EXCEPTION_IME_NOT_FOUND },
84 { ErrorCode::ERROR_OPERATE_SYSTEM_IME, EXCEPTION_OPERATE_DEFAULTIME },
85 { ErrorCode::ERROR_SWITCH_IME, EXCEPTION_IMMS },
86 { ErrorCode::ERROR_IMA_INVALID_IMMERSIVE_EFFECT, EXCEPTION_INVALID_IMMERSIVE_EFFECT },
87 { ErrorCode::ERROR_IMA_PRECONDITION_REQUIRED, EXCEPTION_PRECONDITION_REQUIRED },
88 };
89
MallocCString(const std::string & origin)90 char* Utils::MallocCString(const std::string &origin)
91 {
92 if (origin.empty()) {
93 return nullptr;
94 }
95 auto len = origin.length() + 1;
96 char *res = static_cast<char *>(malloc(sizeof(char) * len));
97 if (res == nullptr) {
98 return nullptr;
99 }
100 return std::char_traits<char>::copy(res, origin.c_str(), len);
101 }
102
InputMethodProperty2C(CInputMethodProperty * props,const Property & property)103 void Utils::InputMethodProperty2C(CInputMethodProperty *props, const Property &property)
104 {
105 if (props == nullptr) {
106 IMSA_HILOGE("props is nullptr.");
107 return;
108 }
109 props->name = Utils::MallocCString(property.name);
110 props->id = Utils::MallocCString(property.id);
111 props->label = Utils::MallocCString(property.label);
112 props->labelId = property.labelId;
113 props->icon = Utils::MallocCString(property.icon);
114 props->iconId = property.iconId;
115 }
116
C2InputMethodProperty(CInputMethodProperty props)117 Property Utils::C2InputMethodProperty(CInputMethodProperty props)
118 {
119 Property property;
120 property.name = std::string(props.name);
121 property.id = std::string(props.id);
122 property.label = std::string(props.label);
123 property.labelId = props.labelId;
124 property.icon = std::string(props.icon);
125 property.iconId = props.iconId;
126 return property;
127 }
128
InputMethodSubProperty2C(CInputMethodSubtype * props,const SubProperty & property)129 void Utils::InputMethodSubProperty2C(CInputMethodSubtype *props, const SubProperty &property)
130 {
131 if (props == nullptr) {
132 IMSA_HILOGE("props is nullptr.");
133 return;
134 }
135 props->name = Utils::MallocCString(property.name);
136 props->id = Utils::MallocCString(property.id);
137 props->label = Utils::MallocCString(property.label);
138 props->labelId = property.labelId;
139 props->icon = Utils::MallocCString(property.icon);
140 props->iconId = property.iconId;
141 props->mode = Utils::MallocCString(property.mode);
142 props->locale = Utils::MallocCString(property.locale);
143 props->language = Utils::MallocCString(property.language);
144 }
145
ConvertErrorCode(int32_t code)146 int32_t Utils::ConvertErrorCode(int32_t code)
147 {
148 IMSA_HILOGD("Convert start.");
149 auto iter = ERROR_CODE_MAP.find(code);
150 if (iter != ERROR_CODE_MAP.end()) {
151 IMSA_HILOGD("ErrorCode: %{public}d", iter->second);
152 return iter->second;
153 }
154 IMSA_HILOGD("Convert fail.");
155 return code;
156 }
157 }