• 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()) {
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) {
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 
ConvertArrAccAbilityInfo2CArr(std::vector<AccessibilityAbilityInfo> & abilityList,RetError & errCode)170 CArrAccessibilityAbilityInfo ConvertArrAccAbilityInfo2CArr(std::vector<AccessibilityAbilityInfo> &abilityList,
171     RetError &errCode)
172 {
173     CArrAccessibilityAbilityInfo cArrAbility;
174     cArrAbility.size = static_cast<int64_t>(abilityList.size());
175     if (cArrAbility.size == 0) {
176         return cArrAbility;
177     }
178     int64_t mallocSize = static_cast<int64_t>(sizeof(CAccessibilityAbilityInfo)) * cArrAbility.size;
179     CAccessibilityAbilityInfo *cAbility = static_cast<CAccessibilityAbilityInfo *>(malloc(mallocSize));
180     if (cAbility == nullptr) {
181         errCode = RET_ERR_NULLPTR;
182         return cArrAbility;
183     }
184     if (memset_s(cAbility, mallocSize, 0, mallocSize) != EOK) {
185         errCode = RET_ERR_FAILED;
186         return cArrAbility;
187     }
188     for (auto i = 0; i < cArrAbility.size; ++i) {
189         cAbility[i] = ConvertAccAbilityInfo2C(abilityList[i], errCode);
190         if (errCode != RET_OK) {
191             HILOG_ERROR("ConvertAccAbilityInfo2C failed.");
192             return cArrAbility;
193         }
194     }
195     cArrAbility.head = cAbility;
196     return cArrAbility;
197 }
198 
ConvertEventInfo2C(const AccessibilityEventInfo & eventInfo,RetError & errCode)199 CEventInfo ConvertEventInfo2C(const AccessibilityEventInfo &eventInfo, RetError &errCode)
200 {
201     CEventInfo cEventInfo;
202     auto eventTypeStr = GetStrFromVal(Utils::eventTypeMap, eventInfo.GetEventType(), errCode, "accessibilityFocus");
203     cEventInfo.type_ = MallocCString(eventTypeStr, errCode);
204     auto windowTypeStr = GetStrFromVal(Utils::windowUpdateTypeMap, eventInfo.GetWindowChangeTypes(), errCode, "add");
205     cEventInfo.windowUpdateType_ = MallocCString(windowTypeStr, errCode);
206     cEventInfo.bundleName_ = MallocCString(eventInfo.GetBundleName(), errCode);
207     cEventInfo.componentType_ = MallocCString(eventInfo.GetComponentType(), errCode);
208     cEventInfo.pageId_ = eventInfo.GetPageId();
209     cEventInfo.description_ = MallocCString(eventInfo.GetDescription(), errCode);
210     auto actionStr = GetStrFromVal(Utils::actionMap, eventInfo.GetTriggerAction(), errCode, "click");
211     cEventInfo.triggerAction_ = MallocCString(actionStr, errCode);
212     auto textMoveStr = GetStrFromVal(Utils::textMoveUnitMap, eventInfo.GetTextMovementStep(), errCode, "char");
213     cEventInfo.textMoveUnit_ = MallocCString(textMoveStr, errCode);
214     auto contVec = eventInfo.GetContentList();
215     cEventInfo.contents_ = VectorToCArrString(contVec, errCode);
216     cEventInfo.lastContent_ = MallocCString(eventInfo.GetLatestContent(), errCode);
217     cEventInfo.beginIndex_ = eventInfo.GetBeginIndex();
218     cEventInfo.currentIndex_ = eventInfo.GetCurrentIndex();
219     cEventInfo.endIndex_ = eventInfo.GetEndIndex();
220     cEventInfo.itemCount_ = eventInfo.GetItemCounts();
221     cEventInfo.elementId_ = eventInfo.GetAccessibilityId();
222     cEventInfo.textAnnouncedForAccessibility_ = MallocCString(eventInfo.GetTextAnnouncedForAccessibility(), errCode);
223     cEventInfo.customId_ = MallocCString(eventInfo.GetInspectorKey(), errCode);
224     return cEventInfo;
225 }
226 
ConvertEventInfo(const CEventInfo & cEventInfo,RetError & errCode)227 AccessibilityEventInfo ConvertEventInfo(const CEventInfo &cEventInfo, RetError &errCode)
228 {
229     AccessibilityEventInfo eventInfo;
230     std::string typeStr(cEventInfo.type_);
231     eventInfo.SetEventType(Utils::GetValueFromStr(rEventTypeMap, typeStr, errCode, TYPE_VIEW_INVALID));
232     std::string windowTypeStr(cEventInfo.windowUpdateType_);
233     eventInfo.SetWindowChangeTypes(
234         Utils::GetValueFromStr(rWindowUpdateTypeMap, windowTypeStr, errCode, WINDOW_UPDATE_ADDED));
235     std::string bundleStr(cEventInfo.bundleName_);
236     eventInfo.SetBundleName(bundleStr);
237     std::string compTypeStr(cEventInfo.componentType_);
238     eventInfo.SetComponentType(compTypeStr);
239     eventInfo.SetPageId(cEventInfo.pageId_);
240     std::string descStr(cEventInfo.description_);
241     eventInfo.SetDescription(descStr);
242     std::string actStr(cEventInfo.triggerAction_);
243     eventInfo.SetTriggerAction(Utils::GetValueFromStr(rActionMap, actStr, errCode, ACCESSIBILITY_ACTION_CLICK));
244     std::string textStr(cEventInfo.textMoveUnit_);
245     eventInfo.SetTextMovementStep(Utils::GetValueFromStr(rTextMoveUnitMap, textStr, errCode, STEP_CHARACTER));
246     std::vector<std::string> contStr = CArrStringToVector(cEventInfo.contents_);
247     for (const auto &cont : contStr) {
248         eventInfo.AddContent(cont);
249     }
250     std::string lastStr(cEventInfo.lastContent_);
251     eventInfo.SetLatestContent(lastStr);
252     eventInfo.SetBeginIndex(cEventInfo.beginIndex_);
253     eventInfo.SetCurrentIndex(cEventInfo.currentIndex_);
254     eventInfo.SetEndIndex(cEventInfo.endIndex_);
255     eventInfo.SetItemCounts(cEventInfo.itemCount_);
256     eventInfo.SetSource(cEventInfo.elementId_);
257     std::string textForStr(cEventInfo.textAnnouncedForAccessibility_);
258     eventInfo.SetTextAnnouncedForAccessibility(textForStr);
259     std::string custStr(cEventInfo.customId_);
260     eventInfo.SetInspectorKey(custStr);
261     return eventInfo;
262 }
263 } // namespace Utils
264 } // namespace Accessibility
265 } // namespace OHOS