• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "cj_accessibility_utils.h"
16 #include <cstdint>
17 #include "cj_common_ffi.h"
18 #include "cj_accessibility_ffi.h"
19 #include "accessibility_system_ability_client.h"
20 #include "accessibility_def.h"
21 #include "accessibility_utils.h"
22 #include "securec.h"
23 #include "native/ffi_remote_data.h"
24 
25 namespace OHOS {
26 namespace Accessibility {
27 namespace Utils {
MallocCString(const std::string & origin,RetError & errCode)28 char *MallocCString(const std::string &origin, RetError &errCode)
29 {
30     if (origin.empty() || errCode == RET_ERR_FAILED) {
31         return nullptr;
32     }
33     auto len = origin.length() + 1;
34     char *res = static_cast<char *>(malloc(sizeof(char) * len));
35     if (res == nullptr) {
36         HILOG_ERROR("MallocCString malloc failed");
37         errCode = RET_ERR_FAILED;
38         return nullptr;
39     }
40     return std::char_traits<char>::copy(res, origin.c_str(), len);
41 }
42 
VectorToCArrString(std::vector<std::string> & vec,RetError & errCode)43 CArrString VectorToCArrString(std::vector<std::string> &vec, RetError &errCode)
44 {
45     if (vec.size() == 0 || errCode == RET_ERR_FAILED) {
46         return { nullptr, 0 };
47     }
48     char **result = new char *[vec.size()];
49     if (result == nullptr) {
50         HILOG_ERROR("VectorToCArrString malloc failed");
51         errCode = RET_ERR_NULLPTR;
52         return { nullptr, 0 };
53     }
54     size_t temp = 0;
55     for (size_t i = 0; i < vec.size(); i++) {
56         result[i] = new char[vec[i].length() + 1];
57         if (result[i] == nullptr) {
58             break;
59         }
60         auto res = strcpy_s(result[i], vec[i].length() + 1, vec[i].c_str());
61         if (res != EOK) {
62             errCode = RET_ERR_FAILED;
63             HILOG_ERROR("failed to strcpy_s.");
64         }
65         temp++;
66     }
67 
68     if (temp != vec.size()) {
69         for (size_t j = temp; j > 0; j--) {
70             delete[] result[j - 1];
71             result[j - 1] = nullptr;
72         }
73         delete[] result;
74         errCode = RET_ERR_FAILED;
75         return { nullptr, 0 };
76     }
77     return { result, vec.size() };
78 }
79 
CArrStringToVector(CArrString cArrStr)80 std::vector<std::string> CArrStringToVector(CArrString cArrStr)
81 {
82     std::vector<std::string> vec;
83     if (cArrStr.head == nullptr || cArrStr.size <= 0) {
84         return vec;
85     }
86     for (size_t i = 0; i < static_cast<size_t>(cArrStr.size); i++) {
87         if (cArrStr.head[i] != nullptr) {
88             vec.emplace_back(cArrStr.head[i]);
89         }
90     }
91     return vec;
92 }
93 
GetAbilityTypesStr(uint32_t abilityTypes,RetError & errCode)94 CArrString GetAbilityTypesStr(uint32_t abilityTypes, RetError &errCode)
95 {
96     std::vector<std::string> abilityTypesStr;
97     for (auto [key, val] : abilityTypeMap) {
98         if (abilityTypes & key) {
99             abilityTypesStr.push_back(val);
100         }
101     }
102     return VectorToCArrString(abilityTypesStr, errCode);
103 }
104 
GetCapabilityStr(uint32_t capabilities,RetError & errCode)105 CArrString GetCapabilityStr(uint32_t capabilities, RetError &errCode)
106 {
107     std::vector<std::string> capabilityStr;
108     for (auto [key, val] : capabilityMap) {
109         if (capabilities & key) {
110             capabilityStr.push_back(val);
111         }
112     }
113     return VectorToCArrString(capabilityStr, errCode);
114 }
115 
GetEventTypeStr(uint32_t eventType,RetError & errCode)116 CArrString GetEventTypeStr(uint32_t eventType, RetError &errCode)
117 {
118     std::vector<std::string> eventTypeStr;
119     if ((eventType < EventType::TYPE_VIEW_CLICKED_EVENT) ||
120         ((eventType >= EventType::TYPE_MAX_NUM) && (eventType != EventType::TYPES_ALL_MASK))) {
121         errCode = RET_ERR_FAILED;
122         return VectorToCArrString(eventTypeStr, errCode);
123     }
124     for (auto [key, val] : eventTypeMap) {
125         if (eventType & key) {
126             eventTypeStr.push_back(val);
127         }
128     }
129     return VectorToCArrString(eventTypeStr, errCode);
130 }
131 
CheckAbilityType(const std::string & abilityType)132 bool CheckAbilityType(const std::string &abilityType)
133 {
134     if (std::strcmp(abilityType.c_str(), "audible") == 0 || std::strcmp(abilityType.c_str(), "generic") == 0 ||
135         std::strcmp(abilityType.c_str(), "haptic") == 0 || std::strcmp(abilityType.c_str(), "spoken") == 0 ||
136         std::strcmp(abilityType.c_str(), "visual") == 0 || std::strcmp(abilityType.c_str(), "all") == 0) {
137         return true;
138     } else {
139         return false;
140     }
141 }
142 
CheckStateType(const std::string & stateType)143 bool CheckStateType(const std::string &stateType)
144 {
145     if (std::strcmp(stateType.c_str(), "enable") == 0 || std::strcmp(stateType.c_str(), "disable") == 0 ||
146         std::strcmp(stateType.c_str(), "install") == 0) {
147         return true;
148     } else {
149         return false;
150     }
151 }
152 
ConvertAccAbilityInfo2C(AccessibilityAbilityInfo & abilityInfo,RetError & errCode)153 CAccessibilityAbilityInfo ConvertAccAbilityInfo2C(AccessibilityAbilityInfo &abilityInfo, RetError &errCode)
154 {
155     CAccessibilityAbilityInfo cAbility;
156     cAbility.id_ = MallocCString(abilityInfo.GetId(), errCode);
157     cAbility.name_ = MallocCString(abilityInfo.GetName(), errCode);
158     cAbility.bundleName_ = MallocCString(abilityInfo.GetPackageName(), errCode);
159     std::vector<std::string> bundleNames(abilityInfo.GetFilterBundleNames());
160     cAbility.targetBundleNames_ = VectorToCArrString(bundleNames, errCode);
161     cAbility.abilityTypes_ = GetAbilityTypesStr(abilityInfo.GetAccessibilityAbilityType(), errCode);
162     cAbility.capabilities_ = GetCapabilityStr(abilityInfo.GetCapabilityValues(), errCode);
163     cAbility.description_ = MallocCString(abilityInfo.GetDescription(), errCode);
164     cAbility.eventTypes_ = GetEventTypeStr(abilityInfo.GetEventTypes(), errCode);
165     cAbility.needHide_ = abilityInfo.NeedHide();
166     cAbility.label_ = MallocCString(abilityInfo.GetLabel(), errCode);
167     return cAbility;
168 }
169 
FreecAbility(CAccessibilityAbilityInfo * cAbility)170 void FreecAbility(CAccessibilityAbilityInfo *cAbility)
171 {
172     free(cAbility->id_);
173     cAbility->id_ = nullptr;
174     free(cAbility->name_);
175     cAbility->name_ = nullptr;
176     free(cAbility->bundleName_);
177     cAbility->bundleName_ = nullptr;
178     free(cAbility->description_);
179     cAbility->description_ = nullptr;
180     free(cAbility->label_);
181     cAbility->label_ = nullptr;
182     for (auto i = 0; i < cAbility->targetBundleNames_.size; i++) {
183         free(cAbility->targetBundleNames_.head[i]);
184     }
185     free(cAbility->targetBundleNames_.head);
186     cAbility->targetBundleNames_.head = nullptr;
187 
188     for (auto i = 0; i < cAbility->abilityTypes_.size; i++) {
189         free(cAbility->abilityTypes_.head[i]);
190     }
191     free(cAbility->abilityTypes_.head);
192     cAbility->abilityTypes_.head = nullptr;
193 
194     for (auto i = 0; i < cAbility->capabilities_.size; i++) {
195         free(cAbility->capabilities_.head[i]);
196     }
197     free(cAbility->capabilities_.head);
198     cAbility->capabilities_.head = nullptr;
199 
200     for (auto i = 0; i < cAbility->eventTypes_.size; i++) {
201         free(cAbility->eventTypes_.head[i]);
202     }
203     free(cAbility->eventTypes_.head);
204     cAbility->eventTypes_.head = nullptr;
205 }
206 
ConvertArrAccAbilityInfo2CArr(std::vector<AccessibilityAbilityInfo> & abilityList,RetError & errCode)207 CArrAccessibilityAbilityInfo ConvertArrAccAbilityInfo2CArr(std::vector<AccessibilityAbilityInfo> &abilityList,
208     RetError &errCode)
209 {
210     CArrAccessibilityAbilityInfo cArrAbility;
211     cArrAbility.size = static_cast<int64_t>(abilityList.size());
212     if (cArrAbility.size == 0) {
213         return cArrAbility;
214     }
215     int64_t mallocSize = static_cast<int64_t>(sizeof(CAccessibilityAbilityInfo)) * cArrAbility.size;
216     CAccessibilityAbilityInfo *cAbility = static_cast<CAccessibilityAbilityInfo *>(malloc(mallocSize));
217     if (cAbility == nullptr) {
218         errCode = RET_ERR_NULLPTR;
219         return cArrAbility;
220     }
221     memset_s(cAbility, mallocSize, 0, mallocSize);
222     for (auto i = 0; i < cArrAbility.size; ++i) {
223         cAbility[i] = ConvertAccAbilityInfo2C(abilityList[i], errCode);
224         if (errCode != RET_OK) {
225             for (auto j = 0; j < i; j++) {
226                 FreecAbility(&cAbility[j]);
227             }
228             free(cAbility);
229             cAbility = nullptr;
230             HILOG_ERROR("ConvertAccAbilityInfo2C failed.");
231             return cArrAbility;
232         }
233     }
234     cArrAbility.head = cAbility;
235     return cArrAbility;
236 }
237 
ConvertEventInfo2C(const AccessibilityEventInfo & eventInfo,RetError & errCode)238 CEventInfo ConvertEventInfo2C(const AccessibilityEventInfo &eventInfo, RetError &errCode)
239 {
240     CEventInfo cEventInfo;
241     auto eventTypeStr = GetStrFromVal(Utils::eventTypeMap, eventInfo.GetEventType(), errCode, "accessibilityFocus");
242     cEventInfo.type_ = MallocCString(eventTypeStr, errCode);
243     auto windowTypeStr = GetStrFromVal(Utils::windowUpdateTypeMap, eventInfo.GetWindowChangeTypes(), errCode, "add");
244     cEventInfo.windowUpdateType_ = MallocCString(windowTypeStr, errCode);
245     cEventInfo.bundleName_ = MallocCString(eventInfo.GetBundleName(), errCode);
246     cEventInfo.componentType_ = MallocCString(eventInfo.GetComponentType(), errCode);
247     cEventInfo.pageId_ = eventInfo.GetPageId();
248     cEventInfo.description_ = MallocCString(eventInfo.GetDescription(), errCode);
249     auto actionStr = GetStrFromVal(Utils::actionMap, eventInfo.GetTriggerAction(), errCode, "click");
250     cEventInfo.triggerAction_ = MallocCString(actionStr, errCode);
251     auto textMoveStr = GetStrFromVal(Utils::textMoveUnitMap, eventInfo.GetTextMovementStep(), errCode, "char");
252     cEventInfo.textMoveUnit_ = MallocCString(textMoveStr, errCode);
253     auto contVec = eventInfo.GetContentList();
254     cEventInfo.contents_ = VectorToCArrString(contVec, errCode);
255     cEventInfo.lastContent_ = MallocCString(eventInfo.GetLatestContent(), errCode);
256     cEventInfo.beginIndex_ = eventInfo.GetBeginIndex();
257     cEventInfo.currentIndex_ = eventInfo.GetCurrentIndex();
258     cEventInfo.endIndex_ = eventInfo.GetEndIndex();
259     cEventInfo.itemCount_ = eventInfo.GetItemCounts();
260     cEventInfo.elementId_ = eventInfo.GetAccessibilityId();
261     cEventInfo.textAnnouncedForAccessibility_ = MallocCString(eventInfo.GetTextAnnouncedForAccessibility(), errCode);
262     cEventInfo.customId_ = MallocCString(eventInfo.GetInspectorKey(), errCode);
263     return cEventInfo;
264 }
265 
ConvertEventInfo(const CEventInfo & cEventInfo,RetError & errCode)266 AccessibilityEventInfo ConvertEventInfo(const CEventInfo &cEventInfo, RetError &errCode)
267 {
268     AccessibilityEventInfo eventInfo;
269     std::string typeStr(cEventInfo.type_);
270     eventInfo.SetEventType(Utils::GetValueFromStr(rEventTypeMap, typeStr, errCode, TYPE_VIEW_INVALID));
271     std::string windowTypeStr(cEventInfo.windowUpdateType_);
272     eventInfo.SetWindowChangeTypes(
273         Utils::GetValueFromStr(rWindowUpdateTypeMap, windowTypeStr, errCode, WINDOW_UPDATE_ADDED));
274     std::string bundleStr(cEventInfo.bundleName_);
275     eventInfo.SetBundleName(bundleStr);
276     std::string compTypeStr(cEventInfo.componentType_);
277     eventInfo.SetComponentType(compTypeStr);
278     eventInfo.SetPageId(cEventInfo.pageId_);
279     std::string descStr(cEventInfo.description_);
280     eventInfo.SetDescription(descStr);
281     std::string actStr(cEventInfo.triggerAction_);
282     eventInfo.SetTriggerAction(Utils::GetValueFromStr(rActionMap, actStr, errCode, ACCESSIBILITY_ACTION_CLICK));
283     std::string textStr(cEventInfo.textMoveUnit_);
284     eventInfo.SetTextMovementStep(Utils::GetValueFromStr(rTextMoveUnitMap, textStr, errCode, STEP_CHARACTER));
285     std::vector<std::string> contStr = CArrStringToVector(cEventInfo.contents_);
286     for (const auto &cont : contStr) {
287         eventInfo.AddContent(cont);
288     }
289     std::string lastStr(cEventInfo.lastContent_);
290     eventInfo.SetLatestContent(lastStr);
291     eventInfo.SetBeginIndex(cEventInfo.beginIndex_);
292     eventInfo.SetCurrentIndex(cEventInfo.currentIndex_);
293     eventInfo.SetEndIndex(cEventInfo.endIndex_);
294     eventInfo.SetItemCounts(cEventInfo.itemCount_);
295     eventInfo.SetSource(cEventInfo.elementId_);
296     std::string textForStr(cEventInfo.textAnnouncedForAccessibility_);
297     eventInfo.SetTextAnnouncedForAccessibility(textForStr);
298     std::string custStr(cEventInfo.customId_);
299     eventInfo.SetInspectorKey(custStr);
300     return eventInfo;
301 }
302 } // namespace Utils
303 } // namespace Accessibility
304 } // namespace OHOS