1 /*
2 * Copyright (C) 2022 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
16 #include "accessibility_utils.h"
17
18 #include <cmath>
19 #include <iomanip>
20 #include <regex>
21 #include <sstream>
22 #include <vector>
23
24 #include "hilog_wrapper.h"
25 #include "napi/native_api.h"
26 #include "napi/native_node_api.h"
27
28 namespace OHOS {
29 namespace AccessibilityNapi {
30 namespace {
31 const uint32_t COLOR_TRANSPARENT = 0x00000000;
32 const uint32_t COLOR_WHITE = 0xffffffff;
33 const uint32_t COLOR_BLACK = 0xff000000;
34 const uint32_t COLOR_RED = 0xffff0000;
35 const uint32_t COLOR_GREEN = 0xff00ff00;
36 const uint32_t COLOR_BLUE = 0xff0000ff;
37 const uint32_t COLOR_GRAY = 0xffc0c0c0;
38
39 constexpr uint32_t COLOR_STRING_SIZE_STANDARD = 8;
40 constexpr uint32_t COLOR_STRING_BASE = 16;
41 const std::regex COLOR_WITH_MAGIC("#[0-9A-Fa-f]{6,8}");
42 const std::regex COLOR_WITH_MAGIC_MINI("#[0-9A-Fa-f]{3,4}");
43 constexpr uint32_t COLOR_ALPHA_MASK = 0xff000000;
44
45 constexpr int32_t RGB_LENGTH = 6;
46 constexpr int32_t ALPHA_LENGTH = 2;
47 constexpr int32_t ALPHA_MOVE = 24;
48 constexpr int32_t COLOR_MOVE = 8;
49 const char UNICODE_BODY = '0';
50 } // namespace
51 using namespace OHOS::Accessibility;
52 using namespace OHOS::AccessibilityConfig;
53
GetStringFromNAPI(napi_env env,napi_value value)54 std::string GetStringFromNAPI(napi_env env, napi_value value)
55 {
56 std::string result;
57 size_t size = 0;
58
59 if (napi_get_value_string_utf8(env, value, nullptr, 0, &size) != napi_ok) {
60 HILOG_ERROR("can not get string size");
61 return "";
62 }
63 result.reserve(size + 1);
64 result.resize(size);
65 if (napi_get_value_string_utf8(env, value, result.data(), (size + 1), &size) != napi_ok) {
66 HILOG_ERROR("can not get string value");
67 return "";
68 }
69 return result;
70 }
71
ParseBool(napi_env env,bool & param,napi_value args)72 bool ParseBool(napi_env env, bool& param, napi_value args)
73 {
74 napi_status status;
75 napi_valuetype valuetype;
76 status = napi_typeof(env, args, &valuetype);
77 if (status != napi_ok) {
78 HILOG_ERROR("napi_typeof error and status is %{public}d", status);
79 return false;
80 }
81
82 if (valuetype != napi_boolean) {
83 HILOG_ERROR("Wrong argument type. Boolean expected.");
84 return false;
85 }
86
87 napi_get_value_bool(env, args, ¶m);
88 return true;
89 }
90
ParseString(napi_env env,std::string & param,napi_value args)91 bool ParseString(napi_env env, std::string& param, napi_value args)
92 {
93 napi_status status;
94 napi_valuetype valuetype;
95 status = napi_typeof(env, args, &valuetype);
96 if (status != napi_ok) {
97 HILOG_ERROR("napi_typeof error and status is %{public}d", status);
98 return false;
99 }
100
101 if (valuetype != napi_string) {
102 HILOG_ERROR("Wrong argument type. String expected.");
103 return false;
104 }
105
106 param = GetStringFromNAPI(env, args);
107 HILOG_DEBUG("param=%{public}s.", param.c_str());
108 return true;
109 }
110
ParseNumber(napi_env env,napi_value args)111 bool ParseNumber(napi_env env, napi_value args)
112 {
113 napi_status status;
114 napi_valuetype valuetype;
115 status = napi_typeof(env, args, &valuetype);
116 if (status != napi_ok) {
117 HILOG_ERROR("napi_typeof error and status is %{public}d", status);
118 return false;
119 }
120
121 if (valuetype != napi_number) {
122 HILOG_ERROR("Wrong argument type. uint32 expected.");
123 return false;
124 }
125
126 HILOG_DEBUG("The type of args is number.");
127 return true;
128 }
129
ParseInt32(napi_env env,int32_t & param,napi_value args)130 bool ParseInt32(napi_env env, int32_t& param, napi_value args)
131 {
132 if (!ParseNumber(env, args)) {
133 return false;
134 }
135
136 napi_get_value_int32(env, args, ¶m);
137 return true;
138 }
139
ParseDouble(napi_env env,double & param,napi_value args)140 bool ParseDouble(napi_env env, double& param, napi_value args)
141 {
142 if (!ParseNumber(env, args)) {
143 return false;
144 }
145
146 napi_get_value_double(env, args, ¶m);
147 return true;
148 }
149
CheckJsFunction(napi_env env,napi_value args)150 bool CheckJsFunction(napi_env env, napi_value args)
151 {
152 napi_status status;
153 napi_valuetype valuetype;
154 status = napi_typeof(env, args, &valuetype);
155 if (status != napi_ok) {
156 HILOG_ERROR("napi_typeof error and status is %{public}d", status);
157 return false;
158 }
159
160 if (valuetype != napi_function) {
161 HILOG_DEBUG("Wrong argument type. function expected.");
162 return false;
163 }
164
165 return true;
166 }
167
QueryRetMsg(OHOS::Accessibility::RetError errorCode)168 NAccessibilityErrMsg QueryRetMsg(OHOS::Accessibility::RetError errorCode)
169 {
170 auto iter = ACCESSIBILITY_JS_TO_ERROR_CODE_MAP.find(errorCode);
171 if (iter != ACCESSIBILITY_JS_TO_ERROR_CODE_MAP.end()) {
172 return iter->second;
173 } else {
174 return ACCESSIBILITY_JS_TO_ERROR_CODE_MAP.at(OHOS::Accessibility::RetError::RET_ERR_FAILED);
175 }
176 }
177
CreateBusinessError(napi_env env,OHOS::Accessibility::RetError errCode)178 napi_value CreateBusinessError(napi_env env, OHOS::Accessibility::RetError errCode)
179 {
180 napi_value result = nullptr;
181 if (errCode == OHOS::Accessibility::RetError::RET_OK) {
182 napi_get_undefined(env, &result);
183 } else {
184 NAccessibilityErrMsg errMsg = QueryRetMsg(errCode);
185 napi_value eCode = nullptr;
186 napi_create_int32(env, static_cast<int32_t>(errMsg.errCode), &eCode);
187 napi_value eMsg = nullptr;
188 napi_create_string_utf8(env, errMsg.message.c_str(), NAPI_AUTO_LENGTH, &eMsg);
189 napi_create_error(env, nullptr, eMsg, &result);
190 napi_set_named_property(env, result, "code", eCode);
191 }
192 return result;
193 }
194
GetErrorValue(napi_env env,int errCode)195 napi_value GetErrorValue(napi_env env, int errCode)
196 {
197 napi_value result = nullptr;
198 napi_value eCode = nullptr;
199 NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
200 NAPI_CALL(env, napi_create_object(env, &result));
201 NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode));
202 return result;
203 }
204
CheckObserverEqual(napi_env env,napi_value observer,napi_env iterEnv,napi_ref iterRef)205 bool CheckObserverEqual(napi_env env, napi_value observer, napi_env iterEnv, napi_ref iterRef)
206 {
207 HILOG_DEBUG();
208 if (env != iterEnv) {
209 return false;
210 }
211 HILOG_DEBUG("Same env, begin check observer equal");
212 napi_value item = nullptr;
213 bool equalFlag = false;
214 napi_get_reference_value(iterEnv, iterRef, &item);
215 napi_status status = napi_strict_equals(iterEnv, item, observer, &equalFlag);
216 if (status == napi_ok && equalFlag) {
217 HILOG_DEBUG("Observer exist");
218 return true;
219 }
220 return false;
221 }
222
223 /**********************************************************
224 * Convert native object to js object
225 *********************************************************/
ConvertRectToJS(napi_env env,napi_value result,const Accessibility::Rect & rect)226 void ConvertRectToJS(napi_env env, napi_value result, const Accessibility::Rect& rect)
227 {
228 napi_value nLeftTopX;
229 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, rect.GetLeftTopXScreenPostion(), &nLeftTopX));
230 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "left", nLeftTopX));
231
232 napi_value nLeftTopY;
233 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, rect.GetLeftTopYScreenPostion(), &nLeftTopY));
234 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "top", nLeftTopY));
235
236 napi_value nWidth;
237 int32_t width = rect.GetRightBottomXScreenPostion() - rect.GetLeftTopXScreenPostion();
238 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, width, &nWidth));
239 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "width", nWidth));
240
241 napi_value nHeight;
242 int32_t height = rect.GetRightBottomYScreenPostion() - rect.GetLeftTopYScreenPostion();
243 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, height, &nHeight));
244 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "height", nHeight));
245 }
246
ConvertWindowTypeToString(AccessibilityWindowType type)247 std::string ConvertWindowTypeToString(AccessibilityWindowType type)
248 {
249 static const std::map<AccessibilityWindowType, const std::string> windowTypeTable = {
250 {AccessibilityWindowType::TYPE_ACCESSIBILITY_OVERLAY, "accessibilityOverlay"},
251 {AccessibilityWindowType::TYPE_APPLICATION, "application"},
252 {AccessibilityWindowType::TYPE_INPUT_METHOD, "inputMethod"},
253 {AccessibilityWindowType::TYPE_SPLIT_SCREEN_DIVIDER, "screenDivider"},
254 {AccessibilityWindowType::TYPE_SYSTEM, "system"}};
255
256 if (windowTypeTable.find(type) == windowTypeTable.end()) {
257 return "";
258 }
259
260 return windowTypeTable.at(type);
261 }
262
ParseEventTypesToVec(uint32_t eventTypesValue)263 static std::vector<std::string> ParseEventTypesToVec(uint32_t eventTypesValue)
264 {
265 std::vector<std::string> result;
266 static std::map<EventType, std::string> accessibilityEventTable = {
267 {EventType::TYPE_VIEW_CLICKED_EVENT, "click"},
268 {EventType::TYPE_VIEW_LONG_CLICKED_EVENT, "longClick"},
269 {EventType::TYPE_VIEW_SELECTED_EVENT, "select"},
270 {EventType::TYPE_VIEW_FOCUSED_EVENT, "focus"},
271 {EventType::TYPE_VIEW_TEXT_UPDATE_EVENT, "textUpdate"},
272 {EventType::TYPE_VIEW_HOVER_ENTER_EVENT, "hoverEnter"},
273 {EventType::TYPE_VIEW_HOVER_EXIT_EVENT, "hoverExit"},
274 {EventType::TYPE_VIEW_SCROLLED_EVENT, "scroll"},
275 {EventType::TYPE_VIEW_TEXT_SELECTION_UPDATE_EVENT, "textSelectionUpdate"},
276 {EventType::TYPE_VIEW_ACCESSIBILITY_FOCUSED_EVENT, "accessibilityFocus"},
277 {EventType::TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED_EVENT, "accessibilityFocusClear"}};
278
279 for (std::map<EventType, std::string>::iterator itr = accessibilityEventTable.begin();
280 itr != accessibilityEventTable.end(); ++itr) {
281 if (eventTypesValue & itr->first) {
282 result.push_back(itr->second);
283 }
284 }
285
286 return result;
287 }
288
ParseAbilityTypesToVec(uint32_t abilityTypesValue)289 static std::vector<std::string> ParseAbilityTypesToVec(uint32_t abilityTypesValue)
290 {
291 std::vector<std::string> result;
292
293 if (abilityTypesValue & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_SPOKEN) {
294 result.push_back("spoken");
295 }
296 if (abilityTypesValue & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_HAPTIC) {
297 result.push_back("haptic");
298 }
299 if (abilityTypesValue & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_AUDIBLE) {
300 result.push_back("audible");
301 }
302 if (abilityTypesValue & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_VISUAL) {
303 result.push_back("visual");
304 }
305 if (abilityTypesValue & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_GENERIC) {
306 result.push_back("generic");
307 }
308
309 return result;
310 }
311
ParseCapabilitiesToVec(uint32_t capabilitiesValue)312 static std::vector<std::string> ParseCapabilitiesToVec(uint32_t capabilitiesValue)
313 {
314 std::vector<std::string> result;
315
316 if (capabilitiesValue & Capability::CAPABILITY_RETRIEVE) {
317 result.push_back("retrieve");
318 }
319 if (capabilitiesValue & Capability::CAPABILITY_TOUCH_GUIDE) {
320 result.push_back("touchGuide");
321 }
322 if (capabilitiesValue & Capability::CAPABILITY_KEY_EVENT_OBSERVER) {
323 result.push_back("keyEventObserver");
324 }
325 if (capabilitiesValue & Capability::CAPABILITY_ZOOM) {
326 result.push_back("zoom");
327 }
328 if (capabilitiesValue & Capability::CAPABILITY_GESTURE) {
329 result.push_back("gesture");
330 }
331
332 return result;
333 }
334
ConvertDaltonizationTypeToString(OHOS::AccessibilityConfig::DALTONIZATION_TYPE type)335 std::string ConvertDaltonizationTypeToString(OHOS::AccessibilityConfig::DALTONIZATION_TYPE type)
336 {
337 static const std::map<OHOS::AccessibilityConfig::DALTONIZATION_TYPE, const std::string> typeTable = {
338 {OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Normal, "Normal"},
339 {OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Protanomaly, "Protanomaly"},
340 {OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Deuteranomaly, "Deuteranomaly"},
341 {OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Tritanomaly, "Tritanomaly"}};
342
343 if (typeTable.find(type) == typeTable.end()) {
344 return "";
345 }
346
347 return typeTable.at(type);
348 }
349
ConvertClickResponseTimeTypeToString(OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME type)350 std::string ConvertClickResponseTimeTypeToString(OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME type)
351 {
352 static const std::map<OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME, const std::string> typeTable = {
353 {OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayShort, "Short"},
354 {OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayMedium, "Medium"},
355 {OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayLong, "Long"}};
356
357 if (typeTable.find(type) == typeTable.end()) {
358 return "";
359 }
360
361 return typeTable.at(type);
362 }
363
ConvertIgnoreRepeatClickTimeTypeToString(OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME type)364 std::string ConvertIgnoreRepeatClickTimeTypeToString(OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME type)
365 {
366 static const std::map<OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME, const std::string> typeTable = {
367 {OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutShortest, "Shortest"},
368 {OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutShort, "Short"},
369 {OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutMedium, "Medium"},
370 {OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutLong, "Long"},
371 {OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutLongest, "Longest"}};
372
373 if (typeTable.find(type) == typeTable.end()) {
374 return "";
375 }
376
377 return typeTable.at(type);
378 }
379
ConvertAccessibleAbilityInfoToJS(napi_env env,napi_value & result,OHOS::Accessibility::AccessibilityAbilityInfo & info)380 void ConvertAccessibleAbilityInfoToJS(
381 napi_env env, napi_value& result, OHOS::Accessibility::AccessibilityAbilityInfo& info)
382 {
383 HILOG_DEBUG();
384 ConvertAccessibleAbilityInfoToJSPart1(env, result, info);
385 ConvertAccessibleAbilityInfoToJSPart2(env, result, info);
386 }
387
ConvertAccessibleAbilityInfoToJSPart1(napi_env env,napi_value & result,OHOS::Accessibility::AccessibilityAbilityInfo & info)388 void ConvertAccessibleAbilityInfoToJSPart1(
389 napi_env env, napi_value& result, OHOS::Accessibility::AccessibilityAbilityInfo& info)
390 {
391 HILOG_DEBUG();
392 napi_value nId;
393 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, info.GetId().c_str(), NAPI_AUTO_LENGTH, &nId));
394 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "id", nId));
395
396 napi_value nName;
397 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, info.GetName().c_str(), NAPI_AUTO_LENGTH, &nName));
398 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "name", nName));
399
400 napi_value nBundleName;
401 NAPI_CALL_RETURN_VOID(
402 env, napi_create_string_utf8(env, info.GetPackageName().c_str(), NAPI_AUTO_LENGTH, &nBundleName));
403 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "bundleName", nBundleName));
404
405 napi_value nAbilityType;
406 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &nAbilityType));
407 uint32_t abilityTypesValue = info.GetAccessibilityAbilityType();
408 std::vector<std::string> abilityTypes = ParseAbilityTypesToVec(abilityTypesValue);
409 for (size_t idxType = 0; idxType < abilityTypes.size(); idxType++) {
410 napi_value nType;
411 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, abilityTypes[idxType].c_str(),
412 NAPI_AUTO_LENGTH, &nType));
413 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, nAbilityType, idxType, nType));
414 }
415 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "abilityTypes", nAbilityType));
416 }
417
ConvertAccessibleAbilityInfoToJSPart2(napi_env env,napi_value & result,OHOS::Accessibility::AccessibilityAbilityInfo & info)418 void ConvertAccessibleAbilityInfoToJSPart2(
419 napi_env env, napi_value& result, OHOS::Accessibility::AccessibilityAbilityInfo& info)
420 {
421 HILOG_DEBUG();
422 napi_value nCapabilities;
423 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &nCapabilities));
424 uint32_t capabilitiesValue = info.GetStaticCapabilityValues();
425 std::vector<std::string> capabilities = ParseCapabilitiesToVec(capabilitiesValue);
426 for (size_t idxCap = 0; idxCap < capabilities.size(); idxCap++) {
427 napi_value nCap;
428 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, capabilities[idxCap].c_str(),
429 NAPI_AUTO_LENGTH, &nCap));
430 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, nCapabilities, idxCap, nCap));
431 }
432 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "capabilities", nCapabilities));
433
434 napi_value description;
435 NAPI_CALL_RETURN_VOID(
436 env, napi_create_string_utf8(env, info.GetDescription().c_str(), NAPI_AUTO_LENGTH, &description));
437 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "description", description));
438
439 napi_value nEventTypes;
440 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &nEventTypes));
441 uint32_t eventTypesValue = info.GetEventTypes();
442 std::vector<std::string> eventTypes = ParseEventTypesToVec(eventTypesValue);
443 for (size_t idxEve = 0; idxEve < eventTypes.size(); idxEve++) {
444 napi_value nEve;
445 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, eventTypes[idxEve].c_str(), NAPI_AUTO_LENGTH, &nEve));
446 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, nEventTypes, idxEve, nEve));
447 }
448 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "eventTypes", nEventTypes));
449
450 napi_value filterBundleNames;
451 size_t idx = 0;
452 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &filterBundleNames));
453 std::vector<std::string> strFilterBundleNames = info.GetFilterBundleNames();
454 for (auto &filterBundleName : strFilterBundleNames) {
455 napi_value bundleName;
456 NAPI_CALL_RETURN_VOID(
457 env, napi_create_string_utf8(env, filterBundleName.c_str(), NAPI_AUTO_LENGTH, &bundleName));
458 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, filterBundleNames, idx, bundleName));
459 idx++;
460 }
461 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "targetBundleNames", filterBundleNames));
462 }
463
ConvertAccessibleAbilityInfosToJS(napi_env env,napi_value & result,std::vector<OHOS::Accessibility::AccessibilityAbilityInfo> & accessibleAbilityInfos)464 void ConvertAccessibleAbilityInfosToJS(napi_env env, napi_value& result,
465 std::vector<OHOS::Accessibility::AccessibilityAbilityInfo>& accessibleAbilityInfos)
466 {
467 size_t index = 0;
468
469 if (accessibleAbilityInfos.empty()) {
470 return;
471 }
472
473 for (auto& abilityInfo : accessibleAbilityInfos) {
474 napi_value obj = nullptr;
475 napi_create_object(env, &obj);
476 ConvertAccessibleAbilityInfoToJS(env, obj, abilityInfo);
477 napi_set_element(env, result, index, obj);
478 index++;
479 }
480 }
481
ConvertAccessibilityEventTypeToString(EventType type)482 const std::string ConvertAccessibilityEventTypeToString(EventType type)
483 {
484 static const std::map<EventType, const std::string> a11yEvtTypeTable = {
485 {EventType::TYPE_VIEW_ACCESSIBILITY_FOCUSED_EVENT, "accessibilityFocus"},
486 {EventType::TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED_EVENT, "accessibilityFocusClear"},
487 {EventType::TYPE_VIEW_CLICKED_EVENT, "click"},
488 {EventType::TYPE_VIEW_LONG_CLICKED_EVENT, "longClick"},
489 {EventType::TYPE_VIEW_FOCUSED_EVENT, "focus"},
490 {EventType::TYPE_VIEW_SELECTED_EVENT, "select"},
491 {EventType::TYPE_VIEW_SCROLLED_EVENT, "scroll"},
492 {EventType::TYPE_VIEW_HOVER_ENTER_EVENT, "hoverEnter"},
493 {EventType::TYPE_VIEW_HOVER_EXIT_EVENT, "hoverExit"},
494 {EventType::TYPE_VIEW_TEXT_UPDATE_EVENT, "textUpdate"},
495 {EventType::TYPE_VIEW_TEXT_SELECTION_UPDATE_EVENT, "textSelectionUpdate"},
496 {EventType::TYPE_PAGE_CONTENT_UPDATE, "pageContentUpdate"},
497 {EventType::TYPE_PAGE_STATE_UPDATE, "pageStateUpdate"},
498 {EventType::TYPE_TOUCH_BEGIN, "touchBegin"},
499 {EventType::TYPE_TOUCH_END, "touchEnd"}};
500
501 if (a11yEvtTypeTable.find(type) == a11yEvtTypeTable.end()) {
502 return "";
503 }
504
505 return a11yEvtTypeTable.at(type);
506 }
507
CoverGestureTypeToString(GestureType type)508 std::string CoverGestureTypeToString(GestureType type)
509 {
510 static const std::map<GestureType, const std::string> gestureTypeTable = {
511 {GestureType::GESTURE_SWIPE_LEFT, "left"},
512 {GestureType::GESTURE_SWIPE_LEFT_THEN_RIGHT, "leftThenRight"},
513 {GestureType::GESTURE_SWIPE_LEFT_THEN_UP, "leftThenUp"},
514 {GestureType::GESTURE_SWIPE_LEFT_THEN_DOWN, "leftThenDown"},
515 {GestureType::GESTURE_SWIPE_RIGHT, "right"},
516 {GestureType::GESTURE_SWIPE_RIGHT_THEN_LEFT, "rightThenLeft"},
517 {GestureType::GESTURE_SWIPE_RIGHT_THEN_UP, "rightThenUp"},
518 {GestureType::GESTURE_SWIPE_RIGHT_THEN_DOWN, "rightThenDown"},
519 {GestureType::GESTURE_SWIPE_UP, "up"},
520 {GestureType::GESTURE_SWIPE_UP_THEN_LEFT, "upThenLeft"},
521 {GestureType::GESTURE_SWIPE_UP_THEN_RIGHT, "upThenRight"},
522 {GestureType::GESTURE_SWIPE_UP_THEN_DOWN, "upThenDown"},
523 {GestureType::GESTURE_SWIPE_DOWN, "down"},
524 {GestureType::GESTURE_SWIPE_DOWN_THEN_LEFT, "downThenLeft"},
525 {GestureType::GESTURE_SWIPE_DOWN_THEN_RIGHT, "downThenRight"},
526 {GestureType::GESTURE_SWIPE_DOWN_THEN_UP, "downThenUp"},
527 {GestureType::GESTURE_TWO_FINGER_SINGLE_TAP, "twoFingerSingleTap"},
528 {GestureType::GESTURE_TWO_FINGER_DOUBLE_TAP, "twoFingerDoubleTap"},
529 {GestureType::GESTURE_TWO_FINGER_DOUBLE_TAP_AND_HOLD, "twoFingerDoubleTapAndHold"},
530 {GestureType::GESTURE_TWO_FINGER_TRIPLE_TAP, "twoFingerTripleTap"},
531 {GestureType::GESTURE_TWO_FINGER_TRIPLE_TAP_AND_HOLD, "twoFingerTripleTapAndHold"},
532 {GestureType::GESTURE_THREE_FINGER_SINGLE_TAP, "threeFingerSingleTap"},
533 {GestureType::GESTURE_THREE_FINGER_DOUBLE_TAP, "threeFingerDoubleTap"},
534 {GestureType::GESTURE_THREE_FINGER_DOUBLE_TAP_AND_HOLD, "threeFingerDoubleTapAndHold"},
535 {GestureType::GESTURE_THREE_FINGER_TRIPLE_TAP, "threeFingerTripleTap"},
536 {GestureType::GESTURE_THREE_FINGER_TRIPLE_TAP_AND_HOLD, "threeFingerTripleTapAndHold"},
537 {GestureType::GESTURE_FOUR_FINGER_SINGLE_TAP, "fourFingerSingleTap"},
538 {GestureType::GESTURE_FOUR_FINGER_DOUBLE_TAP, "fourFingerDoubleTap"},
539 {GestureType::GESTURE_FOUR_FINGER_DOUBLE_TAP_AND_HOLD, "fourFingerDoubleTapAndHold"},
540 {GestureType::GESTURE_FOUR_FINGER_TRIPLE_TAP, "fourFingerTripleTap"},
541 {GestureType::GESTURE_FOUR_FINGER_TRIPLE_TAP_AND_HOLD, "fourFingerTripleTapAndHold"},
542 {GestureType::GESTURE_THREE_FINGER_SWIPE_UP, "threeFingerSwipeUp"},
543 {GestureType::GESTURE_THREE_FINGER_SWIPE_DOWN, "threeFingerSwipeDown"},
544 {GestureType::GESTURE_THREE_FINGER_SWIPE_LEFT, "threeFingerSwipeLeft"},
545 {GestureType::GESTURE_THREE_FINGER_SWIPE_RIGHT, "threeFingerSwipeRight"},
546 {GestureType::GESTURE_FOUR_FINGER_SWIPE_UP, "fourFingerSwipeUp"},
547 {GestureType::GESTURE_FOUR_FINGER_SWIPE_DOWN, "fourFingerSwipeDown"},
548 {GestureType::GESTURE_FOUR_FINGER_SWIPE_LEFT, "fourFingerSwipeLeft"},
549 {GestureType::GESTURE_FOUR_FINGER_SWIPE_RIGHT, "fourFingerSwipeRight"}
550 };
551
552 if (gestureTypeTable.find(type) == gestureTypeTable.end()) {
553 return "";
554 }
555
556 return gestureTypeTable.at(type);
557 }
558
ConvertWindowUpdateTypeToString(WindowUpdateType type)559 const std::string ConvertWindowUpdateTypeToString(WindowUpdateType type)
560 {
561 static const std::map<WindowUpdateType, const std::string> windowUpdateTypeTable = {
562 {WindowUpdateType::WINDOW_UPDATE_FOCUSED, "focus"},
563 {WindowUpdateType::WINDOW_UPDATE_ACTIVE, "active"},
564 {WindowUpdateType::WINDOW_UPDATE_ADDED, "add"},
565 {WindowUpdateType::WINDOW_UPDATE_REMOVED, "remove"},
566 {WindowUpdateType::WINDOW_UPDATE_BOUNDS, "bounds"},
567 {WindowUpdateType::WINDOW_UPDATE_PROPERTY, "property"}};
568
569 if (windowUpdateTypeTable.find(type) == windowUpdateTypeTable.end()) {
570 return "";
571 }
572
573 return windowUpdateTypeTable.at(type);
574 }
575
ConvertEventTypeToString(const AccessibilityEventInfo & eventInfo,std::string & eventTypeString)576 void ConvertEventTypeToString(const AccessibilityEventInfo &eventInfo, std::string &eventTypeString)
577 {
578 EventType type = eventInfo.GetEventType();
579 switch (type) {
580 case TYPE_GESTURE_EVENT: {
581 GestureType gestureType = eventInfo.GetGestureType();
582 eventTypeString = CoverGestureTypeToString(gestureType);
583 break;
584 }
585 case TYPE_WINDOW_UPDATE: {
586 WindowUpdateType windowUpdateType = eventInfo.GetWindowChangeTypes();
587 eventTypeString = ConvertWindowUpdateTypeToString(windowUpdateType);
588 break;
589 }
590 default:
591 eventTypeString = ConvertAccessibilityEventTypeToString(type);
592 break;
593 }
594 }
595
ConvertOperationTypeToString(ActionType type)596 std::string ConvertOperationTypeToString(ActionType type)
597 {
598 static const std::map<ActionType, const std::string> triggerActionTable = {
599 {ActionType::ACCESSIBILITY_ACTION_FOCUS, "focus"},
600 {ActionType::ACCESSIBILITY_ACTION_CLEAR_FOCUS, "clearFocus"},
601 {ActionType::ACCESSIBILITY_ACTION_SELECT, "select"},
602 {ActionType::ACCESSIBILITY_ACTION_CLEAR_SELECTION, "clearSelection"},
603 {ActionType::ACCESSIBILITY_ACTION_CLICK, "click"},
604 {ActionType::ACCESSIBILITY_ACTION_LONG_CLICK, "longClick"},
605 {ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS, "accessibilityFocus"},
606 {ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS, "clearAccessibilityFocus"},
607 {ActionType::ACCESSIBILITY_ACTION_SCROLL_FORWARD, "scrollForward"},
608 {ActionType::ACCESSIBILITY_ACTION_SCROLL_BACKWARD, "scrollBackward"},
609 {ActionType::ACCESSIBILITY_ACTION_COPY, "copy"},
610 {ActionType::ACCESSIBILITY_ACTION_PASTE, "paste"},
611 {ActionType::ACCESSIBILITY_ACTION_CUT, "cut"},
612 {ActionType::ACCESSIBILITY_ACTION_SET_SELECTION, "setSelection"},
613 {ActionType::ACCESSIBILITY_ACTION_SET_TEXT, "setText"},
614 {ActionType::ACCESSIBILITY_ACTION_DELETED, "delete"},
615 };
616
617 if (triggerActionTable.find(type) == triggerActionTable.end()) {
618 return "";
619 }
620
621 return triggerActionTable.at(type);
622 }
623
ConvertStringToWindowUpdateTypes(std::string type)624 static WindowUpdateType ConvertStringToWindowUpdateTypes(std::string type)
625 {
626 static const std::map<const std::string, WindowUpdateType> windowsUpdateTypesTable = {
627 {"accessibilityFocus", WindowUpdateType::WINDOW_UPDATE_ACCESSIBILITY_FOCUSED},
628 {"focus", WindowUpdateType::WINDOW_UPDATE_FOCUSED},
629 {"active", WindowUpdateType::WINDOW_UPDATE_ACTIVE},
630 {"add", WindowUpdateType::WINDOW_UPDATE_ADDED},
631 {"remove", WindowUpdateType::WINDOW_UPDATE_REMOVED},
632 {"bounds", WindowUpdateType::WINDOW_UPDATE_BOUNDS},
633 {"title", WindowUpdateType::WINDOW_UPDATE_TITLE},
634 {"layer", WindowUpdateType::WINDOW_UPDATE_LAYER},
635 {"parent", WindowUpdateType::WINDOW_UPDATE_PARENT},
636 {"children", WindowUpdateType::WINDOW_UPDATE_CHILDREN},
637 {"pip", WindowUpdateType::WINDOW_UPDATE_PIP},
638 {"property", WindowUpdateType::WINDOW_UPDATE_PROPERTY}};
639
640 if (windowsUpdateTypesTable.find(type) == windowsUpdateTypesTable.end()) {
641 HILOG_WARN("invalid key[%{public}s]", type.c_str());
642 return WINDOW_UPDATE_INVALID;
643 }
644
645 return windowsUpdateTypesTable.at(type);
646 }
647
ConvertStringToEventInfoTypes(std::string type)648 static EventType ConvertStringToEventInfoTypes(std::string type)
649 {
650 static const std::map<const std::string, EventType> eventInfoTypesTable = {
651 {"click", EventType::TYPE_VIEW_CLICKED_EVENT},
652 {"longClick", EventType::TYPE_VIEW_LONG_CLICKED_EVENT},
653 {"select", EventType::TYPE_VIEW_SELECTED_EVENT},
654 {"focus", EventType::TYPE_VIEW_FOCUSED_EVENT},
655 {"textUpdate", EventType::TYPE_VIEW_TEXT_UPDATE_EVENT},
656 {"hoverEnter", EventType::TYPE_VIEW_HOVER_ENTER_EVENT},
657 {"hoverExit", EventType::TYPE_VIEW_HOVER_EXIT_EVENT},
658 {"scroll", EventType::TYPE_VIEW_SCROLLED_EVENT},
659 {"textSelectionUpdate", EventType::TYPE_VIEW_TEXT_SELECTION_UPDATE_EVENT},
660 {"accessibilityFocus", EventType::TYPE_VIEW_ACCESSIBILITY_FOCUSED_EVENT},
661 {"accessibilityFocusClear", EventType::TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED_EVENT}};
662
663 if (eventInfoTypesTable.find(type) == eventInfoTypesTable.end()) {
664 HILOG_WARN("invalid key[%{public}s]", type.c_str());
665 return TYPE_VIEW_INVALID;
666 }
667
668 return eventInfoTypesTable.at(type);
669 }
670
ConvertStringToCapability(std::string type)671 static uint32_t ConvertStringToCapability(std::string type)
672 {
673 HILOG_DEBUG();
674 static const std::map<const std::string, uint32_t> capabilitiesTable = {
675 {"retrieve", Capability::CAPABILITY_RETRIEVE},
676 {"touchGuide", Capability::CAPABILITY_TOUCH_GUIDE},
677 {"keyEventObserver", Capability::CAPABILITY_KEY_EVENT_OBSERVER},
678 {"zoom", Capability::CAPABILITY_ZOOM},
679 {"gesture", Capability::CAPABILITY_GESTURE}};
680
681 if (capabilitiesTable.find(type) == capabilitiesTable.end()) {
682 HILOG_WARN("invalid key[%{public}s]", type.c_str());
683 return 0;
684 }
685
686 return capabilitiesTable.at(type);
687 }
688
ConvertStringToAccessibleOperationType(const std::string & type)689 ActionType ConvertStringToAccessibleOperationType(const std::string &type)
690 {
691 std::map<const std::string, ActionType> accessibleOperationTypeTable = {
692 {"focus", ActionType::ACCESSIBILITY_ACTION_FOCUS},
693 {"clearFocus", ActionType::ACCESSIBILITY_ACTION_CLEAR_FOCUS},
694 {"select", ActionType::ACCESSIBILITY_ACTION_SELECT},
695 {"clearSelection", ActionType::ACCESSIBILITY_ACTION_CLEAR_SELECTION},
696 {"click", ActionType::ACCESSIBILITY_ACTION_CLICK},
697 {"longClick", ActionType::ACCESSIBILITY_ACTION_LONG_CLICK},
698 {"accessibilityFocus", ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS},
699 {"clearAccessibilityFocus", ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS},
700 {"scrollForward", ActionType::ACCESSIBILITY_ACTION_SCROLL_FORWARD},
701 {"scrollBackward", ActionType::ACCESSIBILITY_ACTION_SCROLL_BACKWARD},
702 {"copy", ActionType::ACCESSIBILITY_ACTION_COPY},
703 {"paste", ActionType::ACCESSIBILITY_ACTION_PASTE},
704 {"cut", ActionType::ACCESSIBILITY_ACTION_CUT},
705 {"setSelection", ActionType::ACCESSIBILITY_ACTION_SET_SELECTION},
706 {"setText", ActionType::ACCESSIBILITY_ACTION_SET_TEXT},
707 {"delete", ActionType::ACCESSIBILITY_ACTION_DELETED}};
708
709 if (accessibleOperationTypeTable.find(type) == accessibleOperationTypeTable.end()) {
710 HILOG_WARN("invalid key[%{public}s]", type.c_str());
711 return ACCESSIBILITY_ACTION_INVALID;
712 }
713
714 return accessibleOperationTypeTable.at(type);
715 }
716
ConvertStringToAccessibilityAbilityTypes(const std::string & type)717 AccessibilityAbilityTypes ConvertStringToAccessibilityAbilityTypes(const std::string &type)
718 {
719 std::map<const std::string, AccessibilityAbilityTypes> accessibilityAbilityTypesTable = {
720 {"spoken", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_SPOKEN},
721 {"haptic", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_HAPTIC},
722 {"audible", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_AUDIBLE},
723 {"visual", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_VISUAL},
724 {"generic", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_GENERIC},
725 {"all", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_ALL},
726 };
727
728 if (accessibilityAbilityTypesTable.find(type) == accessibilityAbilityTypesTable.end()) {
729 HILOG_WARN("invalid key[%{public}s]", type.c_str());
730 return AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_INVALID;
731 }
732
733 return accessibilityAbilityTypesTable.at(type);
734 }
735
ConvertStringToAbilityStateType(const std::string & type)736 AbilityStateType ConvertStringToAbilityStateType(const std::string &type)
737 {
738 std::map<const std::string, AbilityStateType> abilityStateTypeTable = {
739 {"enable", AbilityStateType::ABILITY_STATE_ENABLE},
740 {"disable", AbilityStateType::ABILITY_STATE_DISABLE},
741 {"install", AbilityStateType::ABILITY_STATE_INSTALLED}};
742
743 if (abilityStateTypeTable.find(type) == abilityStateTypeTable.end()) {
744 HILOG_WARN("invalid key[%{public}s]", type.c_str());
745 return ABILITY_STATE_INVALID;
746 }
747
748 return abilityStateTypeTable.at(type);
749 }
750
ConvertStringToDaltonizationTypes(std::string & type)751 OHOS::AccessibilityConfig::DALTONIZATION_TYPE ConvertStringToDaltonizationTypes(std::string& type)
752 {
753 std::map<const std::string, OHOS::AccessibilityConfig::DALTONIZATION_TYPE> daltonizationTTypesTable = {
754 {"Normal", OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Normal},
755 {"Protanomaly", OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Protanomaly},
756 {"Deuteranomaly", OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Deuteranomaly},
757 {"Tritanomaly", OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Tritanomaly},
758 };
759
760 if (daltonizationTTypesTable.find(type) == daltonizationTTypesTable.end()) {
761 HILOG_WARN("invalid key[%{public}s]", type.c_str());
762 return OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Normal;
763 }
764
765 return daltonizationTTypesTable.at(type);
766 }
767
ConvertStringToClickResponseTimeTypes(std::string & type)768 OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME ConvertStringToClickResponseTimeTypes(std::string& type)
769 {
770 std::map<const std::string, OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME> clickResponseTimeTypesTable = {
771 {"Short", OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayShort},
772 {"Medium", OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayMedium},
773 {"Long", OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayLong},
774 };
775
776 if (clickResponseTimeTypesTable.find(type) == clickResponseTimeTypesTable.end()) {
777 HILOG_WARN("invalid key[%{public}s]", type.c_str());
778 return OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayShort;
779 }
780
781 return clickResponseTimeTypesTable.at(type);
782 }
783
ConvertStringToIgnoreRepeatClickTimeTypes(std::string & type)784 OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME ConvertStringToIgnoreRepeatClickTimeTypes(std::string& type)
785 {
786 std::map<const std::string, OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME> mapTable = {
787 {"Shortest", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutShortest},
788 {"Short", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutShort},
789 {"Medium", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutMedium},
790 {"Long", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutLong},
791 {"Longest", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutLongest},
792 };
793
794 if (mapTable.find(type) == mapTable.end()) {
795 HILOG_WARN("invalid key[%{public}s]", type.c_str());
796 return OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutShortest;
797 }
798
799 return mapTable.at(type);
800 }
801
ConvertStringToTextMoveUnit(const std::string & type)802 TextMoveUnit ConvertStringToTextMoveUnit(const std::string &type)
803 {
804 static const std::map<const std::string, TextMoveUnit> textMoveUnitTable = {{"char", TextMoveUnit::STEP_CHARACTER},
805 {"word", TextMoveUnit::STEP_WORD},
806 {"line", TextMoveUnit::STEP_LINE},
807 {"page", TextMoveUnit::STEP_PAGE},
808 {"paragraph", TextMoveUnit::STEP_PARAGRAPH}};
809
810 if (textMoveUnitTable.find(type) == textMoveUnitTable.end()) {
811 HILOG_WARN("invalid key[%{public}s]", type.c_str());
812 return STEP_INVALID;
813 }
814
815 return textMoveUnitTable.at(type);
816 }
817
ConvertTextMoveUnitToString(TextMoveUnit type)818 std::string ConvertTextMoveUnitToString(TextMoveUnit type)
819 {
820 static const std::map<TextMoveUnit, const std::string> textMoveUnitTable = {{TextMoveUnit::STEP_CHARACTER, "char"},
821 {TextMoveUnit::STEP_WORD, "word"},
822 {TextMoveUnit::STEP_LINE, "line"},
823 {TextMoveUnit::STEP_PAGE, "page"},
824 {TextMoveUnit::STEP_PARAGRAPH, "paragraph"}};
825
826 if (textMoveUnitTable.find(type) == textMoveUnitTable.end()) {
827 HILOG_WARN("invalid key[0x%{public}x]", type);
828 return "";
829 }
830
831 return textMoveUnitTable.at(type);
832 }
833
ConvertActionArgsJSToNAPI(napi_env env,napi_value object,std::map<std::string,std::string> & args,OHOS::Accessibility::ActionType action)834 void ConvertActionArgsJSToNAPI(
835 napi_env env, napi_value object, std::map<std::string, std::string>& args, OHOS::Accessibility::ActionType action)
836 {
837 napi_value propertyNameValue = nullptr;
838 bool hasProperty = false;
839 std::string str = "";
840 switch (action) {
841 case ActionType::ACCESSIBILITY_ACTION_NEXT_HTML_ITEM:
842 case ActionType::ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM:
843 napi_create_string_utf8(env, "htmlItem", NAPI_AUTO_LENGTH, &propertyNameValue);
844 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
845 if (hasProperty) {
846 args.insert(std::pair<std::string, std::string>("htmlItem", str.c_str()));
847 }
848 break;
849 case ActionType::ACCESSIBILITY_ACTION_NEXT_TEXT:
850 case ActionType::ACCESSIBILITY_ACTION_PREVIOUS_TEXT:
851 napi_create_string_utf8(env, "textMoveUnit", NAPI_AUTO_LENGTH, &propertyNameValue);
852 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
853 if (hasProperty) {
854 args.insert(std::pair<std::string, std::string>("textMoveUnit", str.c_str()));
855 }
856 break;
857 case ActionType::ACCESSIBILITY_ACTION_SET_SELECTION:
858 napi_create_string_utf8(env, "selectTextBegin", NAPI_AUTO_LENGTH, &propertyNameValue);
859 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
860 if (hasProperty) {
861 args.insert(std::pair<std::string, std::string>("selectTextBegin", str.c_str()));
862 }
863 napi_create_string_utf8(env, "selectTextEnd", NAPI_AUTO_LENGTH, &propertyNameValue);
864 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
865 if (hasProperty) {
866 args.insert(std::pair<std::string, std::string>("selectTextEnd", str.c_str()));
867 }
868 break;
869 case ActionType::ACCESSIBILITY_ACTION_SET_TEXT:
870 napi_create_string_utf8(env, "setText", NAPI_AUTO_LENGTH, &propertyNameValue);
871 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
872 if (hasProperty) {
873 args.insert(std::pair<std::string, std::string>("setText", str.c_str()));
874 }
875 break;
876 default:
877 break;
878 }
879 }
880
ConvertIntJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty)881 int32_t ConvertIntJSToNAPI(napi_env env, napi_value object, napi_value propertyNameValue, bool &hasProperty)
882 {
883 int32_t dataValue = 0;
884 napi_has_property(env, object, propertyNameValue, &hasProperty);
885 if (hasProperty) {
886 napi_value itemValue = nullptr;
887 napi_get_property(env, object, propertyNameValue, &itemValue);
888 napi_get_value_int32(env, itemValue, &dataValue);
889 }
890 return dataValue;
891 }
892
ConvertStringJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty)893 std::string ConvertStringJSToNAPI(napi_env env, napi_value object, napi_value propertyNameValue, bool &hasProperty)
894 {
895 std::string str = "";
896 napi_has_property(env, object, propertyNameValue, &hasProperty);
897 if (hasProperty) {
898 napi_value itemValue = nullptr;
899 napi_get_property(env, object, propertyNameValue, &itemValue);
900 str = GetStringFromNAPI(env, itemValue);
901 }
902 return str;
903 }
904
ConvertStringArrayJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty,std::vector<std::string> & stringArray)905 void ConvertStringArrayJSToNAPI(napi_env env, napi_value object,
906 napi_value propertyNameValue, bool &hasProperty, std::vector<std::string> &stringArray)
907 {
908 napi_has_property(env, object, propertyNameValue, &hasProperty);
909 if (hasProperty) {
910 napi_value contentsValue = nullptr;
911 napi_get_property(env, object, propertyNameValue, &contentsValue);
912 napi_value data = nullptr;
913 uint32_t dataLen = 0;
914 napi_get_array_length(env, contentsValue, &dataLen);
915 for (uint32_t i = 0; i < dataLen; i++) {
916 napi_get_element(env, contentsValue, i, &data);
917 std::string str = GetStringFromNAPI(env, data);
918 stringArray.push_back(str);
919 }
920 }
921 }
922
ConvertStringArrayJSToNAPICommon(napi_env env,napi_value object,std::vector<std::string> & stringArray)923 void ConvertStringArrayJSToNAPICommon(napi_env env, napi_value object, std::vector<std::string> &stringArray)
924 {
925 napi_value data = nullptr;
926 uint32_t dataLen = 0;
927 napi_get_array_length(env, object, &dataLen);
928 for (uint32_t i = 0; i < dataLen; i++) {
929 napi_get_element(env, object, i, &data);
930 std::string str = GetStringFromNAPI(env, data);
931 stringArray.push_back(str);
932 }
933 }
934
ConvertEventInfoJSToNAPI(napi_env env,napi_value object,OHOS::Accessibility::AccessibilityEventInfo & eventInfo)935 bool ConvertEventInfoJSToNAPI(
936 napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo)
937 {
938 HILOG_DEBUG();
939 bool tmpResult = ConvertEventInfoJSToNAPIPart1(env, object, eventInfo);
940 if (!tmpResult) {
941 return false;
942 }
943 tmpResult = ConvertEventInfoJSToNAPIPart2(env, object, eventInfo);
944 if (!tmpResult) {
945 return false;
946 }
947 tmpResult = ConvertEventInfoJSToNAPIPart3(env, object, eventInfo);
948 if (!tmpResult) {
949 return false;
950 }
951 return true;
952 }
953
ConvertEventInfoJSToNAPIPart1(napi_env env,napi_value object,OHOS::Accessibility::AccessibilityEventInfo & eventInfo)954 bool ConvertEventInfoJSToNAPIPart1(
955 napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo)
956 {
957 bool hasProperty = false;
958 std::string str = "";
959 napi_value propertyNameValue = nullptr;
960 napi_create_string_utf8(env, "type", NAPI_AUTO_LENGTH, &propertyNameValue);
961 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
962 if (hasProperty) {
963 EventType eventType = ConvertStringToEventInfoTypes(str);
964 eventInfo.SetEventType(eventType);
965 if (eventType == TYPE_VIEW_INVALID) {
966 return false;
967 }
968 } else {
969 return false;
970 }
971
972 napi_create_string_utf8(env, "windowUpdateType", NAPI_AUTO_LENGTH, &propertyNameValue);
973 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
974 if (hasProperty) {
975 eventInfo.SetEventType(TYPE_WINDOW_UPDATE);
976 eventInfo.SetWindowChangeTypes(ConvertStringToWindowUpdateTypes(str));
977 }
978
979 napi_create_string_utf8(env, "bundleName", NAPI_AUTO_LENGTH, &propertyNameValue);
980 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
981 if (hasProperty) {
982 if (str != "") {
983 eventInfo.SetBundleName(str);
984 } else {
985 return false;
986 }
987 } else {
988 return false;
989 }
990 return true;
991 }
992
ConvertEventInfoJSToNAPIPart2(napi_env env,napi_value object,OHOS::Accessibility::AccessibilityEventInfo & eventInfo)993 bool ConvertEventInfoJSToNAPIPart2(
994 napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo)
995 {
996 bool hasProperty = false;
997 int32_t dataValue = 0;
998 std::string str = "";
999 napi_value propertyNameValue = nullptr;
1000 napi_create_string_utf8(env, "componentType", NAPI_AUTO_LENGTH, &propertyNameValue);
1001 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1002 if (hasProperty) {
1003 eventInfo.SetComponentType(str);
1004 }
1005
1006 napi_create_string_utf8(env, "pageId", NAPI_AUTO_LENGTH, &propertyNameValue);
1007 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1008 if (hasProperty) {
1009 eventInfo.SetPageId(dataValue);
1010 }
1011
1012 napi_create_string_utf8(env, "description", NAPI_AUTO_LENGTH, &propertyNameValue);
1013 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1014 if (hasProperty) {
1015 eventInfo.SetDescription(str);
1016 }
1017
1018 napi_create_string_utf8(env, "triggerAction", NAPI_AUTO_LENGTH, &propertyNameValue);
1019 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1020 if (hasProperty) {
1021 eventInfo.SetTriggerAction(ConvertStringToAccessibleOperationType(str));
1022 if (eventInfo.GetTriggerAction() == ACCESSIBILITY_ACTION_INVALID) {
1023 return false;
1024 }
1025 } else {
1026 return false;
1027 }
1028
1029 napi_create_string_utf8(env, "textMoveUnit", NAPI_AUTO_LENGTH, &propertyNameValue);
1030 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1031 if (hasProperty) {
1032 eventInfo.SetTextMovementStep(ConvertStringToTextMoveUnit(str));
1033 }
1034 return true;
1035 }
1036
ConvertEventInfoJSToNAPIPart3(napi_env env,napi_value object,OHOS::Accessibility::AccessibilityEventInfo & eventInfo)1037 bool ConvertEventInfoJSToNAPIPart3(
1038 napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo)
1039 {
1040 bool hasProperty = false;
1041 int32_t dataValue = 0;
1042 napi_value propertyNameValue = nullptr;
1043 napi_create_string_utf8(env, "contents", NAPI_AUTO_LENGTH, &propertyNameValue);
1044 std::vector<std::string> stringArray {};
1045 ConvertStringArrayJSToNAPI(env, object, propertyNameValue, hasProperty, stringArray);
1046 if (hasProperty) {
1047 for (auto str : stringArray) {
1048 eventInfo.AddContent(str);
1049 }
1050 }
1051
1052 napi_create_string_utf8(env, "lastContent", NAPI_AUTO_LENGTH, &propertyNameValue);
1053 std::string strNapi = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1054 if (hasProperty) {
1055 eventInfo.SetLatestContent(strNapi);
1056 }
1057
1058 napi_create_string_utf8(env, "beginIndex", NAPI_AUTO_LENGTH, &propertyNameValue);
1059 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1060 if (hasProperty) {
1061 eventInfo.SetBeginIndex(dataValue);
1062 }
1063
1064 napi_create_string_utf8(env, "currentIndex", NAPI_AUTO_LENGTH, &propertyNameValue);
1065 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1066 if (hasProperty) {
1067 eventInfo.SetCurrentIndex(dataValue);
1068 }
1069
1070 napi_create_string_utf8(env, "endIndex", NAPI_AUTO_LENGTH, &propertyNameValue);
1071 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1072 if (hasProperty) {
1073 eventInfo.SetEndIndex(dataValue);
1074 }
1075
1076 napi_create_string_utf8(env, "itemCount", NAPI_AUTO_LENGTH, &propertyNameValue);
1077 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1078 if (hasProperty) {
1079 eventInfo.SetItemCounts(dataValue);
1080 }
1081 return true;
1082 }
1083
ConvertGesturePointJSToNAPI(napi_env env,napi_value object,AccessibilityGesturePosition & gesturePathPosition)1084 static bool ConvertGesturePointJSToNAPI(
1085 napi_env env, napi_value object, AccessibilityGesturePosition& gesturePathPosition)
1086 {
1087 HILOG_DEBUG();
1088 napi_value propertyNameValue = nullptr;
1089 bool hasProperty = false;
1090 double position = 0;
1091
1092 napi_create_string_utf8(env, "positionX", NAPI_AUTO_LENGTH, &propertyNameValue);
1093 napi_has_property(env, object, propertyNameValue, &hasProperty);
1094 if (hasProperty) {
1095 napi_value valueX = nullptr;
1096 napi_get_property(env, object, propertyNameValue, &valueX);
1097 napi_get_value_double(env, valueX, &position);
1098 gesturePathPosition.positionX_ = static_cast<float>(position);
1099 } else {
1100 return false;
1101 }
1102
1103 napi_create_string_utf8(env, "positionY", NAPI_AUTO_LENGTH, &propertyNameValue);
1104 napi_has_property(env, object, propertyNameValue, &hasProperty);
1105 if (hasProperty) {
1106 napi_value valueY = nullptr;
1107 napi_get_property(env, object, propertyNameValue, &valueY);
1108 napi_get_value_double(env, valueY, &position);
1109 gesturePathPosition.positionY_ = static_cast<float>(position);
1110 } else {
1111 return false;
1112 }
1113 return true;
1114 }
1115
ConvertGesturePathJSToNAPI(napi_env env,napi_value object,std::shared_ptr<AccessibilityGestureInjectPath> & gesturePath)1116 bool ConvertGesturePathJSToNAPI(napi_env env, napi_value object,
1117 std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath)
1118 {
1119 HILOG_DEBUG();
1120 napi_value propertyNameValue = nullptr;
1121 bool hasProperty = false;
1122
1123 napi_create_string_utf8(env, "points", NAPI_AUTO_LENGTH, &propertyNameValue);
1124 napi_has_property(env, object, propertyNameValue, &hasProperty);
1125 if (hasProperty) {
1126 napi_value positionValue = nullptr;
1127 napi_get_property(env, object, propertyNameValue, &positionValue);
1128 napi_value jsValue = nullptr;
1129 bool isArray = false;
1130 uint32_t dataLen = 0;
1131 if (napi_is_array(env, positionValue, &isArray) != napi_ok || isArray == false) {
1132 HILOG_ERROR("object is not an array.");
1133 return false;
1134 }
1135 if (napi_get_array_length(env, positionValue, &dataLen) != napi_ok) {
1136 HILOG_ERROR("get array length failed.");
1137 return false;
1138 }
1139 for (uint32_t i = 0; i < dataLen; i++) {
1140 jsValue = nullptr;
1141 AccessibilityGesturePosition path;
1142 if (napi_get_element(env, positionValue, i, &jsValue) != napi_ok) {
1143 HILOG_ERROR("get element of paths failed and i = %{public}d", i);
1144 return false;
1145 }
1146 bool result = ConvertGesturePointJSToNAPI(env, jsValue, path);
1147 if (result) {
1148 gesturePath->AddPosition(path);
1149 } else {
1150 HILOG_ERROR("Parse gesture point error.");
1151 return false;
1152 }
1153 }
1154 } else {
1155 HILOG_ERROR("No points property.");
1156 return false;
1157 }
1158
1159 napi_create_string_utf8(env, "durationTime", NAPI_AUTO_LENGTH, &propertyNameValue);
1160 int64_t durationTime = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1161 napi_has_property(env, object, propertyNameValue, &hasProperty);
1162 if (hasProperty) {
1163 gesturePath->SetDurationTime(durationTime);
1164 return true;
1165 }
1166 return false;
1167 }
1168
TransformKeyActionValue(int32_t keyAction)1169 KeyAction TransformKeyActionValue(int32_t keyAction)
1170 {
1171 HILOG_DEBUG("keyAction:%{public}d", keyAction);
1172
1173 KeyAction action = KeyAction::UNKNOWN;
1174 if (keyAction == OHOS::MMI::KeyEvent::KEY_ACTION_DOWN) {
1175 action = KeyAction::DOWN;
1176 } else if (keyAction == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
1177 action = KeyAction::UP;
1178 } else if (keyAction == OHOS::MMI::KeyEvent::KEY_ACTION_CANCEL) {
1179 action = KeyAction::CANCEL;
1180 } else {
1181 HILOG_DEBUG("key action is invalid");
1182 }
1183 return action;
1184 }
1185
HasKeyCode(const std::vector<int32_t> & pressedKeys,int32_t keyCode)1186 bool HasKeyCode(const std::vector<int32_t>& pressedKeys, int32_t keyCode)
1187 {
1188 HILOG_DEBUG();
1189
1190 return std::find(pressedKeys.begin(), pressedKeys.end(), keyCode) != pressedKeys.end();
1191 }
1192
GetKeyValue(napi_env env,napi_value keyObject,std::optional<MMI::KeyEvent::KeyItem> keyItem)1193 void GetKeyValue(napi_env env, napi_value keyObject, std::optional<MMI::KeyEvent::KeyItem> keyItem)
1194 {
1195 HILOG_DEBUG();
1196
1197 if (!keyItem) {
1198 HILOG_WARN("keyItem is null.");
1199 return;
1200 }
1201
1202 napi_value keyCodeValue = nullptr;
1203 int32_t keyCode = keyItem->GetKeyCode();
1204 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, keyCode, &keyCodeValue));
1205 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, keyObject, "code", keyCodeValue));
1206
1207 napi_value timeValue = nullptr;
1208 int64_t pressedTime = keyItem->GetDownTime();
1209 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, pressedTime, &timeValue));
1210 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, keyObject, "pressedTime", timeValue));
1211
1212 napi_value deviceIdValue = nullptr;
1213 int32_t deviceId = keyItem->GetDeviceId();
1214 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, deviceId, &deviceIdValue));
1215 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, keyObject, "deviceId", deviceIdValue));
1216 }
1217
SetInputEventProperty(napi_env env,napi_value result,const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent)1218 void SetInputEventProperty(napi_env env, napi_value result, const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent)
1219 {
1220 HILOG_DEBUG();
1221
1222 // set id
1223 napi_value idValue = nullptr;
1224 int32_t id = keyEvent->GetId();
1225 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, id, &idValue));
1226 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "id", idValue));
1227
1228 // set deviceId
1229 napi_value deviceIdValue = nullptr;
1230 int32_t deviceId = keyEvent->GetDeviceId();
1231 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, deviceId, &deviceIdValue));
1232 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "deviceId", deviceIdValue));
1233
1234 // set actionTime
1235 napi_value actionTimeValue = nullptr;
1236 int64_t actionTime = keyEvent->GetActionTime();
1237 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, actionTime, &actionTimeValue));
1238 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "actionTime", actionTimeValue));
1239
1240 // set screenId
1241 napi_value screenIdValue = nullptr;
1242 int32_t screenId = keyEvent->GetTargetDisplayId();
1243 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, screenId, &screenIdValue));
1244 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "screenId", screenIdValue));
1245
1246 // set windowId
1247 napi_value windowIdValue = nullptr;
1248 int32_t windowId = keyEvent->GetTargetWindowId();
1249 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, windowId, &windowIdValue));
1250 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "windowId", windowIdValue));
1251 }
1252
ConvertKeyEventToJS(napi_env env,napi_value result,const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent)1253 void ConvertKeyEventToJS(napi_env env, napi_value result, const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent)
1254 {
1255 HILOG_DEBUG();
1256
1257 if (!keyEvent) {
1258 HILOG_ERROR("keyEvent is null.");
1259 return;
1260 }
1261
1262 // set inputEvent
1263 SetInputEventProperty(env, result, keyEvent);
1264
1265 // set action
1266 napi_value keyActionValue = nullptr;
1267 KeyAction keyAction = TransformKeyActionValue(keyEvent->GetKeyAction());
1268 if (keyAction != KeyAction::UNKNOWN) {
1269 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, keyAction, &keyActionValue));
1270 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "action", keyActionValue));
1271 }
1272
1273 // set key
1274 napi_value keyObject = nullptr;
1275 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &keyObject));
1276 std::optional<MMI::KeyEvent::KeyItem> keyItem = keyEvent->GetKeyItem();
1277 GetKeyValue(env, keyObject, keyItem);
1278 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "key", keyObject));
1279
1280 // set unicodeChar
1281 napi_value unicodeCharValue = nullptr;
1282 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, 0, &unicodeCharValue));
1283 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "unicodeChar", unicodeCharValue));
1284
1285 // set keys
1286 SetKeyPropertyPart1(env, result, keyEvent);
1287 SetKeyPropertyPart2(env, result, keyEvent);
1288 }
1289
SetKeyPropertyPart1(napi_env env,napi_value result,const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent)1290 void SetKeyPropertyPart1(napi_env env, napi_value result, const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent)
1291 {
1292 HILOG_DEBUG();
1293 // set keys
1294 napi_value keysAarry = nullptr;
1295 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &keysAarry));
1296 uint32_t index = 0;
1297 std::vector<int32_t> pressedKeys = keyEvent->GetPressedKeys();
1298 for (const auto &pressedKeyCode : pressedKeys) {
1299 napi_value element = nullptr;
1300 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &element));
1301 std::optional<MMI::KeyEvent::KeyItem> pressedKeyItem = keyEvent->GetKeyItem(pressedKeyCode);
1302 GetKeyValue(env, element, pressedKeyItem);
1303 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, keysAarry, index, element));
1304 ++index;
1305 }
1306 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "keys", keysAarry));
1307
1308 // set ctrlKey
1309 bool isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_CTRL_LEFT)
1310 || HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_CTRL_RIGHT);
1311 napi_value ctrlKeyValue = nullptr;
1312 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &ctrlKeyValue));
1313 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "ctrlKey", ctrlKeyValue));
1314
1315 // set altKey
1316 isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_ALT_LEFT)
1317 || HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_ALT_RIGHT);
1318 napi_value altKeyValue = nullptr;
1319 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &altKeyValue));
1320 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "altKey", altKeyValue));
1321
1322 // set shiftKey
1323 isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_SHIFT_LEFT)
1324 || HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_SHIFT_RIGHT);
1325 napi_value shiftKeyValue = nullptr;
1326 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &shiftKeyValue));
1327 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "shiftKey", shiftKeyValue));
1328
1329 // set logoKey
1330 isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_META_LEFT)
1331 || HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_META_RIGHT);
1332 napi_value logoKeyValue = nullptr;
1333 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &logoKeyValue));
1334 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "logoKey", logoKeyValue));
1335
1336 // set fnKey
1337 isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_FN);
1338 napi_value fnKeyValue = nullptr;
1339 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &fnKeyValue));
1340 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fnKey", fnKeyValue));
1341 }
1342
SetKeyPropertyPart2(napi_env env,napi_value result,const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent)1343 void SetKeyPropertyPart2(napi_env env, napi_value result, const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent)
1344 {
1345 HILOG_DEBUG();
1346 // set capsLock
1347 napi_value capsLockValue = nullptr;
1348 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, false, &capsLockValue));
1349 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "capsLock", capsLockValue));
1350
1351 // set numLock
1352 napi_value numLockValue = nullptr;
1353 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, false, &numLockValue));
1354 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "numLock", numLockValue));
1355
1356 // set scrollLock
1357 napi_value scrollLockValue = nullptr;
1358 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, false, &scrollLockValue));
1359 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "scrollLock", scrollLockValue));
1360 }
1361
ConvertCaptionPropertyToJS(napi_env env,napi_value & result,OHOS::AccessibilityConfig::CaptionProperty captionProperty)1362 void ConvertCaptionPropertyToJS(
1363 napi_env env, napi_value& result, OHOS::AccessibilityConfig::CaptionProperty captionProperty)
1364 {
1365 HILOG_DEBUG();
1366
1367 napi_value value;
1368
1369 NAPI_CALL_RETURN_VOID(env,
1370 napi_create_string_utf8(env, captionProperty.GetFontFamily().c_str(), NAPI_AUTO_LENGTH, &value));
1371 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fontFamily", value));
1372
1373 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, captionProperty.GetFontScale(), &value));
1374 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fontScale", value));
1375
1376 uint32_t color = captionProperty.GetFontColor();
1377 std::string colorStr = ConvertColorToString(color);
1378 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, colorStr.c_str(), NAPI_AUTO_LENGTH, &value));
1379 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fontColor", value));
1380
1381 NAPI_CALL_RETURN_VOID(env,
1382 napi_create_string_utf8(env, captionProperty.GetFontEdgeType().c_str(), NAPI_AUTO_LENGTH, &value));
1383 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fontEdgeType", value));
1384
1385 color = captionProperty.GetBackgroundColor();
1386 colorStr = ConvertColorToString(color);
1387 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, colorStr.c_str(), NAPI_AUTO_LENGTH, &value));
1388 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "backgroundColor", value));
1389
1390 color = captionProperty.GetWindowColor();
1391 colorStr = ConvertColorToString(color);
1392 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, colorStr.c_str(), NAPI_AUTO_LENGTH, &value));
1393 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "windowColor", value));
1394 }
1395
ConvertColorStringToNumer(std::string colorStr)1396 uint32_t ConvertColorStringToNumer(std::string colorStr)
1397 {
1398 HILOG_DEBUG("colorStr is %{public}s", colorStr.c_str());
1399 uint32_t color = COLOR_TRANSPARENT;
1400 if (colorStr.empty()) {
1401 // Empty string, return transparent
1402 return color;
1403 }
1404 // Remove all " ".
1405 colorStr.erase(std::remove(colorStr.begin(), colorStr.end(), ' '), colorStr.end());
1406
1407 if (ColorRegexMatch(colorStr, color)) {
1408 return color;
1409 }
1410
1411 // Match for special string
1412 static const std::map<std::string, uint32_t> colorTable {
1413 std::make_pair("black", COLOR_BLACK),
1414 std::make_pair("blue", COLOR_BLUE),
1415 std::make_pair("gray", COLOR_GRAY),
1416 std::make_pair("green", COLOR_GREEN),
1417 std::make_pair("red", COLOR_RED),
1418 std::make_pair("white", COLOR_WHITE),
1419 };
1420 auto it = colorTable.find(colorStr.c_str());
1421 if (it != colorTable.end()) {
1422 color = it->second;
1423 }
1424 return color;
1425 }
1426
ColorRegexMatch(std::string colorStr,uint32_t & color)1427 bool ColorRegexMatch(std::string colorStr, uint32_t &color)
1428 {
1429 // Regex match for #909090 or #90909090.
1430 if (std::regex_match(colorStr, COLOR_WITH_MAGIC)) {
1431 colorStr.erase(0, 1);
1432 auto colorValue = stoul(colorStr, nullptr, COLOR_STRING_BASE);
1433 if (colorStr.length() < COLOR_STRING_SIZE_STANDARD) {
1434 // No alpha specified, set alpha to 0xff
1435 colorValue |= COLOR_ALPHA_MASK;
1436 } else {
1437 auto alpha = colorValue << ALPHA_MOVE;
1438 auto rgb = colorValue >> COLOR_MOVE;
1439 colorValue = alpha | rgb;
1440 }
1441 color = colorValue;
1442 return true;
1443 }
1444 // Regex match for #rgb or #rgba.
1445 if (std::regex_match(colorStr, COLOR_WITH_MAGIC_MINI)) {
1446 colorStr.erase(0, 1);
1447 std::string newColorStr;
1448 // Translate #rgb or #rgba to #rrggbb or #rrggbbaa
1449 for (const auto& c : colorStr) {
1450 newColorStr += c;
1451 newColorStr += c;
1452 }
1453 auto valueMini = stoul(newColorStr, nullptr, COLOR_STRING_BASE);
1454 if (newColorStr.length() < COLOR_STRING_SIZE_STANDARD) {
1455 // No alpha specified, set alpha to 0xff
1456 valueMini |= COLOR_ALPHA_MASK;
1457 } else {
1458 auto alphaMini = valueMini << ALPHA_MOVE;
1459 auto rgbMini = valueMini >> COLOR_MOVE;
1460 valueMini = alphaMini | rgbMini;
1461 }
1462 color = valueMini;
1463 return true;
1464 }
1465 return false;
1466 }
1467
ConvertColorToString(uint32_t color)1468 std::string ConvertColorToString(uint32_t color)
1469 {
1470 HILOG_DEBUG("color is 0X%{public}x", color);
1471 uint32_t rgb = color & (~COLOR_ALPHA_MASK);
1472 uint32_t alpha = (color) >> ALPHA_MOVE;
1473 std::stringstream rgbStream;
1474 rgbStream << std::hex << std::setw(RGB_LENGTH) << std::setfill(UNICODE_BODY) << rgb;
1475 std::stringstream alphaStream;
1476 alphaStream << std::hex << std::setw(ALPHA_LENGTH) << std::setfill(UNICODE_BODY) << alpha;
1477 std::string rgbStr(rgbStream.str());
1478 std::string alphaStr(alphaStream.str());
1479 std::string colorStr = "#" + rgbStr + alphaStr;
1480 HILOG_DEBUG("colorStr is %{public}s", colorStr.c_str());
1481 return colorStr;
1482 }
1483
GetColorValue(napi_env env,napi_value object,napi_value propertyNameValue)1484 uint32_t GetColorValue(napi_env env, napi_value object, napi_value propertyNameValue)
1485 {
1486 uint32_t color = COLOR_TRANSPARENT;
1487 napi_valuetype valueType = napi_undefined;
1488 napi_value value = nullptr;
1489 napi_get_property(env, object, propertyNameValue, &value);
1490 napi_status status = napi_typeof(env, value, &valueType);
1491 if (status != napi_ok) {
1492 HILOG_ERROR("GetColorValue error! status is %{public}d", status);
1493 return color;
1494 }
1495 if (valueType == napi_number) {
1496 napi_get_value_uint32(env, value, &color);
1497 HILOG_DEBUG("valueType number, color is 0x%{public}x", color);
1498 }
1499 if (valueType == napi_string) {
1500 char outBuffer[CHAE_BUFFER_MAX + 1] = {0};
1501 size_t outSize = 0;
1502 napi_get_value_string_utf8(env, value, outBuffer, CHAE_BUFFER_MAX, &outSize);
1503 color = ConvertColorStringToNumer(std::string(outBuffer));
1504 }
1505 HILOG_DEBUG("color is 0x%{public}x", color);
1506 return color;
1507 }
1508
GetColorValue(napi_env env,napi_value value)1509 uint32_t GetColorValue(napi_env env, napi_value value)
1510 {
1511 uint32_t color = COLOR_TRANSPARENT;
1512 napi_valuetype valueType = napi_undefined;
1513 napi_status status = napi_typeof(env, value, &valueType);
1514 if (status != napi_ok) {
1515 HILOG_ERROR("GetColorValue error! status is %{public}d", status);
1516 return color;
1517 }
1518 if (valueType == napi_number) {
1519 HILOG_DEBUG("color type is number");
1520 napi_get_value_uint32(env, value, &color);
1521 }
1522 if (valueType == napi_string) {
1523 char outBuffer[CHAE_BUFFER_MAX + 1] = {0};
1524 size_t outSize = 0;
1525 napi_get_value_string_utf8(env, value, outBuffer, CHAE_BUFFER_MAX, &outSize);
1526 color = ConvertColorStringToNumer(std::string(outBuffer));
1527 }
1528 HILOG_DEBUG("color is 0x%{public}x", color);
1529 return color;
1530 }
1531
ConvertObjToCaptionProperty(napi_env env,napi_value object,OHOS::AccessibilityConfig::CaptionProperty * ptrCaptionProperty)1532 bool ConvertObjToCaptionProperty(
1533 napi_env env, napi_value object, OHOS::AccessibilityConfig::CaptionProperty* ptrCaptionProperty)
1534 {
1535 napi_value propertyNameValue = nullptr;
1536 bool hasProperty = false;
1537 int32_t num = 100;
1538
1539 napi_create_string_utf8(env, "fontFamily", NAPI_AUTO_LENGTH, &propertyNameValue);
1540 std::string fontFamily = ConvertCaptionPropertyJSToNAPI(env, object, propertyNameValue, hasProperty);
1541 if (hasProperty) {
1542 ptrCaptionProperty->SetFontFamily(fontFamily);
1543 } else {
1544 return false;
1545 }
1546
1547 napi_create_string_utf8(env, "fontScale", NAPI_AUTO_LENGTH, &propertyNameValue);
1548 num = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1549 if (hasProperty) {
1550 ptrCaptionProperty->SetFontScale(num);
1551 } else {
1552 return false;
1553 }
1554
1555 napi_create_string_utf8(env, "fontColor", NAPI_AUTO_LENGTH, &propertyNameValue);
1556 napi_has_property(env, object, propertyNameValue, &hasProperty);
1557 if (hasProperty) {
1558 ptrCaptionProperty->SetFontColor(GetColorValue(env, object, propertyNameValue));
1559 } else {
1560 return false;
1561 }
1562
1563 napi_create_string_utf8(env, "fontEdgeType", NAPI_AUTO_LENGTH, &propertyNameValue);
1564 std::string fontEdgeType = ConvertCaptionPropertyJSToNAPI(env, object, propertyNameValue, hasProperty);
1565 if (hasProperty) {
1566 ptrCaptionProperty->SetFontEdgeType(fontEdgeType);
1567 } else {
1568 return false;
1569 }
1570
1571 napi_create_string_utf8(env, "backgroundColor", NAPI_AUTO_LENGTH, &propertyNameValue);
1572 napi_has_property(env, object, propertyNameValue, &hasProperty);
1573 if (hasProperty) {
1574 ptrCaptionProperty->SetBackgroundColor(GetColorValue(env, object, propertyNameValue));
1575 } else {
1576 return false;
1577 }
1578
1579 napi_create_string_utf8(env, "windowColor", NAPI_AUTO_LENGTH, &propertyNameValue);
1580 napi_has_property(env, object, propertyNameValue, &hasProperty);
1581 if (hasProperty) {
1582 ptrCaptionProperty->SetWindowColor(GetColorValue(env, object, propertyNameValue));
1583 } else {
1584 return false;
1585 }
1586
1587 return true;
1588 }
1589
ConvertCaptionPropertyJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty)1590 std::string ConvertCaptionPropertyJSToNAPI(napi_env env, napi_value object,
1591 napi_value propertyNameValue, bool &hasProperty)
1592 {
1593 char outBuffer[CHAE_BUFFER_MAX + 1] = {0};
1594 napi_has_property(env, object, propertyNameValue, &hasProperty);
1595 if (hasProperty) {
1596 napi_value value = nullptr;
1597 size_t outSize = 0;
1598 napi_get_property(env, object, propertyNameValue, &value);
1599 napi_get_value_string_utf8(env, value, outBuffer, CHAE_BUFFER_MAX, &outSize);
1600 }
1601 return std::string(outBuffer);
1602 }
1603
ConvertJSToStringVec(napi_env env,napi_value arrayValue,std::vector<std::string> & values)1604 bool ConvertJSToStringVec(napi_env env, napi_value arrayValue, std::vector<std::string>& values)
1605 {
1606 HILOG_DEBUG();
1607 values.clear();
1608
1609 bool hasElement = true;
1610 for (int32_t i = 0; hasElement; i++) {
1611 napi_has_element(env, arrayValue, i, &hasElement);
1612 if (hasElement) {
1613 napi_value value = nullptr;
1614 napi_status status = napi_get_element(env, arrayValue, i, &value);
1615 if (status != napi_ok) {
1616 return false;
1617 }
1618
1619 char outBuffer[CHAE_BUFFER_MAX + 1] = {0};
1620 size_t outSize = 0;
1621 status = napi_get_value_string_utf8(env, value, outBuffer, CHAE_BUFFER_MAX, &outSize);
1622 if (status != napi_ok) {
1623 return false;
1624 }
1625
1626 values.push_back(std::string(outBuffer));
1627 }
1628 }
1629 return true;
1630 }
1631
ConvertJSToEventTypes(napi_env env,napi_value arrayValue,uint32_t & eventTypes)1632 void ConvertJSToEventTypes(napi_env env, napi_value arrayValue, uint32_t &eventTypes)
1633 {
1634 HILOG_DEBUG();
1635 eventTypes = TYPE_VIEW_INVALID;
1636 std::vector<std::string> values;
1637 ConvertJSToStringVec(env, arrayValue, values);
1638 for (auto &value : values) {
1639 HILOG_DEBUG("the event type is %{public}s", value.c_str());
1640 EventType eventType = ConvertStringToEventInfoTypes(value);
1641 if (eventType == TYPE_VIEW_INVALID) {
1642 HILOG_ERROR("the event type is invalid");
1643 eventTypes = TYPE_VIEW_INVALID;
1644 return;
1645 }
1646 eventTypes |= eventType;
1647 }
1648 }
1649
ConvertJSToCapabilities(napi_env env,napi_value arrayValue,uint32_t & capabilities)1650 bool ConvertJSToCapabilities(napi_env env, napi_value arrayValue, uint32_t &capabilities)
1651 {
1652 HILOG_DEBUG();
1653 capabilities = 0;
1654 std::vector<std::string> values;
1655 ConvertJSToStringVec(env, arrayValue, values);
1656 for (auto &value : values) {
1657 HILOG_DEBUG("capability is %{public}s", value.c_str());
1658 uint32_t capability = ConvertStringToCapability(value);
1659 if (capability == 0) {
1660 HILOG_ERROR("the capability is invalid");
1661 capabilities = 0;
1662 return false;
1663 }
1664 capabilities |= capability;
1665 }
1666 return true;
1667 }
1668
ConvertStringVecToJS(napi_env env,napi_value & result,std::vector<std::string> values)1669 void ConvertStringVecToJS(napi_env env, napi_value &result, std::vector<std::string> values)
1670 {
1671 HILOG_DEBUG();
1672 size_t index = 0;
1673 for (auto& value : values) {
1674 napi_value str = nullptr;
1675 napi_create_string_utf8(env, value.c_str(), value.size(), &str);
1676 napi_set_element(env, result, index, str);
1677 index++;
1678 }
1679 }
1680 } // namespace AccessibilityNapi
1681 } // namespace OHOS
1682