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 #include "accessibility_def.h"
18
19 #include <charconv>
20 #include <cmath>
21 #include <iomanip>
22 #include <regex>
23 #include <sstream>
24 #include <vector>
25
26 #include "hilog_wrapper.h"
27 #include "napi/native_api.h"
28 #include "napi/native_node_api.h"
29
30 namespace OHOS {
31 namespace AccessibilityNapi {
32 namespace {
33 const uint32_t COLOR_TRANSPARENT = 0x00000000;
34 const uint32_t COLOR_WHITE = 0xffffffff;
35 const uint32_t COLOR_BLACK = 0xff000000;
36 const uint32_t COLOR_RED = 0xffff0000;
37 const uint32_t COLOR_GREEN = 0xff00ff00;
38 const uint32_t COLOR_BLUE = 0xff0000ff;
39 const uint32_t COLOR_GRAY = 0xffc0c0c0;
40
41 constexpr uint32_t COLOR_STRING_SIZE_STANDARD = 8;
42 constexpr uint32_t COLOR_STRING_SIZE_4 = 4;
43 constexpr uint32_t COLOR_STRING_SIZE_5 = 5;
44 constexpr uint32_t COLOR_STRING_SIZE_7 = 7;
45 constexpr uint32_t COLOR_STRING_SIZE_9 = 9;
46 constexpr uint32_t COLOR_STRING_BASE = 16;
47 constexpr uint32_t COLOR_ALPHA_MASK = 0xff000000;
48
49 constexpr int32_t RGB_LENGTH = 6;
50 constexpr int32_t ALPHA_LENGTH = 2;
51 constexpr int32_t ALPHA_MOVE = 24;
52 constexpr int32_t COLOR_MOVE = 8;
53 const char UNICODE_BODY = '0';
54 const std::string HALF_VALUE = "0";
55 const std::string FULL_VALUE = "1";
56 const std::string NUMBER_VALID_CHARS = "0123456789ABCDEFabcdef";
57 napi_status status = napi_ok;
58 } // namespace
59 using namespace OHOS::Accessibility;
60 using namespace OHOS::AccessibilityConfig;
61
ParseResourceIdFromNAPI(napi_env env,napi_value value,uint32_t & idValue)62 napi_status ParseResourceIdFromNAPI(napi_env env, napi_value value, uint32_t &idValue)
63 {
64 bool hasProperty = false;
65 napi_value propertyName = nullptr;
66 status = napi_create_string_utf8(env, "id", NAPI_AUTO_LENGTH, &propertyName);
67 if (status != napi_ok) {
68 HILOG_ERROR("napi create resource id failed");
69 return status;
70 }
71 status = napi_has_property(env, value, propertyName, &hasProperty);
72 if (!hasProperty) {
73 HILOG_ERROR("property is null");
74 return status;
75 }
76 napi_value itemValue = nullptr;
77 napi_get_property(env, value, propertyName, &itemValue);
78 napi_get_value_uint32(env, itemValue, &idValue);
79 HILOG_DEBUG("get resource id is %{public}d", idValue);
80 return status;
81 }
82
ParseResourceBundleNameFromNAPI(napi_env env,napi_value value,std::string & bundleNameValue)83 napi_status ParseResourceBundleNameFromNAPI(napi_env env, napi_value value,
84 std::string &bundleNameValue)
85 {
86 bool hasProperty = false;
87 napi_value propertyName = nullptr;
88 status = napi_create_string_utf8(env, "bundleName", NAPI_AUTO_LENGTH, &propertyName);
89 if (status != napi_ok) {
90 HILOG_ERROR("napi create bundleName failed");
91 return status;
92 }
93 status = napi_has_property(env, value, propertyName, &hasProperty);
94 if (!hasProperty) {
95 HILOG_ERROR("property is null");
96 return status;
97 }
98 napi_value itemValue = nullptr;
99 status = napi_get_property(env, value, propertyName, &itemValue);
100 if (status != napi_ok) {
101 HILOG_ERROR("get bundleName from napi failed");
102 return status;
103 }
104 bundleNameValue = GetStringFromNAPI(env, itemValue);
105 HILOG_DEBUG("get resource bundleName is %{public}s", bundleNameValue.c_str());
106 return status;
107 }
108
ParseResourceModuleNameFromNAPI(napi_env env,napi_value value,std::string & moduleNameValue)109 napi_status ParseResourceModuleNameFromNAPI(napi_env env, napi_value value,
110 std::string &moduleNameValue)
111 {
112 bool hasProperty = false;
113 napi_value propertyName = nullptr;
114 status = napi_create_string_utf8(env, "moduleName", NAPI_AUTO_LENGTH, &propertyName);
115 if (status != napi_ok) {
116 HILOG_ERROR("napi create moduleName failed");
117 return status;
118 }
119 status = napi_has_property(env, value, propertyName, &hasProperty);
120 if (!hasProperty) {
121 HILOG_ERROR("property is null");
122 return status;
123 }
124 napi_value itemValue = nullptr;
125 status = napi_get_property(env, value, propertyName, &itemValue);
126 if (status != napi_ok) {
127 HILOG_ERROR("get moduleName from napi failed");
128 return status;
129 }
130 moduleNameValue = GetStringFromNAPI(env, itemValue);
131 HILOG_DEBUG("get resource moduleName is %{public}s", moduleNameValue.c_str());
132 return status;
133 }
134
ParseResourceParamsFromNAPI(napi_env env,napi_value value,std::vector<std::tuple<int32_t,std::string>> & resourceParamsValue)135 napi_status ParseResourceParamsFromNAPI(napi_env env, napi_value value,
136 std::vector<std::tuple<int32_t, std::string>> &resourceParamsValue)
137 {
138 bool hasProperty = false;
139 napi_value propertyName = nullptr;
140 napi_valuetype valueType = napi_undefined;
141 status = napi_create_string_utf8(env, "params", NAPI_AUTO_LENGTH, &propertyName);
142 if (status != napi_ok) {
143 HILOG_ERROR("napi create params failed");
144 return status;
145 }
146 status = napi_has_property(env, value, propertyName, &hasProperty);
147 if (!hasProperty) {
148 HILOG_ERROR("property is null");
149 return status;
150 }
151 napi_value paramsValue = nullptr;
152 status = napi_get_property(env, value, propertyName, ¶msValue);
153 if (status != napi_ok) {
154 HILOG_ERROR("get params from napi failed");
155 return status;
156 }
157 uint32_t arrayLength = 0;
158 napi_get_array_length(env, paramsValue, &arrayLength);
159 HILOG_DEBUG("resource params size is %{public}d", arrayLength);
160 for (uint32_t i = 0; i < arrayLength; i++) {
161 napi_value indexValue = nullptr;
162 napi_get_element(env, paramsValue, i, &indexValue);
163 napi_typeof(env, indexValue, &valueType);
164 if (valueType == napi_string) {
165 std::string str = "";
166 str = GetStringFromNAPI(env, indexValue);
167 resourceParamsValue.emplace_back(std::make_tuple(1, str));
168 } else if (valueType == napi_number) {
169 int32_t num;
170 napi_get_value_int32(env, indexValue, &num);
171 resourceParamsValue.emplace_back(std::make_tuple(0, std::to_string(num)));
172 }
173 }
174 return status;
175 }
176
GetStringFromNAPI(napi_env env,napi_value value)177 std::string GetStringFromNAPI(napi_env env, napi_value value)
178 {
179 std::string result;
180 size_t size = 0;
181
182 if (napi_get_value_string_utf8(env, value, nullptr, 0, &size) != napi_ok) {
183 HILOG_ERROR("can not get string size");
184 return "";
185 }
186 result.reserve(size + 1);
187 result.resize(size);
188 if (napi_get_value_string_utf8(env, value, result.data(), (size + 1), &size) != napi_ok) {
189 HILOG_ERROR("can not get string value");
190 return "";
191 }
192 return result;
193 }
194
ParseBool(napi_env env,bool & param,napi_value args)195 bool ParseBool(napi_env env, bool& param, napi_value args)
196 {
197 napi_status status;
198 napi_valuetype valuetype = napi_null;
199 status = napi_typeof(env, args, &valuetype);
200 if (status != napi_ok) {
201 HILOG_ERROR("napi_typeof error and status is %{public}d", status);
202 return false;
203 }
204
205 if (valuetype != napi_boolean) {
206 HILOG_ERROR("Wrong argument type. Boolean expected.");
207 return false;
208 }
209
210 napi_get_value_bool(env, args, ¶m);
211 return true;
212 }
213
ParseString(napi_env env,std::string & param,napi_value args)214 bool ParseString(napi_env env, std::string& param, napi_value args)
215 {
216 napi_status status;
217 napi_valuetype valuetype = napi_null;
218 status = napi_typeof(env, args, &valuetype);
219 if (status != napi_ok) {
220 HILOG_ERROR("napi_typeof error and status is %{public}d", status);
221 return false;
222 }
223
224 if (valuetype != napi_string) {
225 HILOG_ERROR("Wrong argument type. String expected.");
226 return false;
227 }
228
229 param = GetStringFromNAPI(env, args);
230 HILOG_DEBUG("param=%{public}s.", param.c_str());
231 return true;
232 }
233
ParseNumber(napi_env env,napi_value args)234 bool ParseNumber(napi_env env, napi_value args)
235 {
236 napi_status status;
237 napi_valuetype valuetype = napi_null;
238 status = napi_typeof(env, args, &valuetype);
239 if (status != napi_ok) {
240 HILOG_ERROR("napi_typeof error and status is %{public}d", status);
241 return false;
242 }
243
244 if (valuetype != napi_number) {
245 HILOG_ERROR("Wrong argument type. uint32 expected.");
246 return false;
247 }
248
249 HILOG_DEBUG("The type of args is number.");
250 return true;
251 }
252
ParseInt32(napi_env env,int32_t & param,napi_value args)253 bool ParseInt32(napi_env env, int32_t& param, napi_value args)
254 {
255 if (!ParseNumber(env, args)) {
256 return false;
257 }
258
259 napi_get_value_int32(env, args, ¶m);
260 return true;
261 }
262
ParseInt64(napi_env env,int64_t & param,napi_value args)263 bool ParseInt64(napi_env env, int64_t& param, napi_value args)
264 {
265 if (!ParseNumber(env, args)) {
266 return false;
267 }
268
269 napi_get_value_int64(env, args, ¶m);
270 return true;
271 }
272
ParseDouble(napi_env env,double & param,napi_value args)273 bool ParseDouble(napi_env env, double& param, napi_value args)
274 {
275 if (!ParseNumber(env, args)) {
276 return false;
277 }
278
279 napi_get_value_double(env, args, ¶m);
280 return true;
281 }
282
CheckJsFunction(napi_env env,napi_value args)283 bool CheckJsFunction(napi_env env, napi_value args)
284 {
285 napi_status status;
286 napi_valuetype valuetype = napi_null;
287 status = napi_typeof(env, args, &valuetype);
288 if (status != napi_ok) {
289 HILOG_ERROR("napi_typeof error and status is %{public}d", status);
290 return false;
291 }
292
293 if (valuetype != napi_function) {
294 HILOG_DEBUG("Wrong argument type. function expected.");
295 return false;
296 }
297
298 return true;
299 }
300
QueryRetMsg(OHOS::Accessibility::RetError errorCode)301 NAccessibilityErrMsg QueryRetMsg(OHOS::Accessibility::RetError errorCode)
302 {
303 switch (errorCode) {
304 case OHOS::Accessibility::RetError::RET_OK:
305 return { NAccessibilityErrorCode::ACCESSIBILITY_OK, "" };
306 case OHOS::Accessibility::RetError::RET_ERR_FAILED:
307 case OHOS::Accessibility::RetError::RET_ERR_NULLPTR:
308 case OHOS::Accessibility::RetError::RET_ERR_IPC_FAILED:
309 case OHOS::Accessibility::RetError::RET_ERR_SAMGR:
310 case OHOS::Accessibility::RetError::RET_ERR_TIME_OUT:
311 case OHOS::Accessibility::RetError::RET_ERR_REGISTER_EXIST:
312 case OHOS::Accessibility::RetError::RET_ERR_NO_REGISTER:
313 case OHOS::Accessibility::RetError::RET_ERR_NO_CONNECTION:
314 case OHOS::Accessibility::RetError::RET_ERR_NO_WINDOW_CONNECTION:
315 case OHOS::Accessibility::RetError::RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE:
316 case OHOS::Accessibility::RetError::RET_ERR_PERFORM_ACTION_FAILED_BY_ACE:
317 case OHOS::Accessibility::RetError::RET_ERR_NO_INJECTOR:
318 return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_SYSTEM_ABNORMALITY,
319 ERROR_MESSAGE_SYSTEM_ABNORMALITY };
320 case OHOS::Accessibility::RetError::RET_ERR_INVALID_PARAM:
321 return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_INVALID_PARAM, ERROR_MESSAGE_PARAMETER_ERROR };
322 case OHOS::Accessibility::RetError::RET_ERR_NO_PERMISSION:
323 return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_NO_PERMISSION, ERROR_MESSAGE_NO_PERMISSION };
324 case OHOS::Accessibility::RetError::RET_ERR_CONNECTION_EXIST:
325 return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_TARGET_ABILITY_ALREADY_ENABLED,
326 ERROR_MESSAGE_TARGET_ABILITY_ALREADY_ENABLED };
327 case OHOS::Accessibility::RetError::RET_ERR_NO_CAPABILITY:
328 return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_NO_RIGHT, ERROR_MESSAGE_NO_RIGHT };
329 case OHOS::Accessibility::RetError::RET_ERR_NOT_INSTALLED:
330 case OHOS::Accessibility::RetError::RET_ERR_NOT_ENABLED:
331 return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_ERROR_EXTENSION_NAME,
332 ERROR_MESSAGE_INVALID_BUNDLE_NAME_OR_ABILITY_NAME};
333 case OHOS::Accessibility::RetError::RET_ERR_PROPERTY_NOT_EXIST:
334 return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_PROPERTY_NOT_EXIST,
335 ERROR_MESSAGE_PROPERTY_NOT_EXIST };
336 case OHOS::Accessibility::RetError::RET_ERR_ACTION_NOT_SUPPORT:
337 return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_ACTION_NOT_SUPPORT,
338 ERROR_MESSAGE_ACTION_NOT_SUPPORT };
339 case OHOS::Accessibility::RetError::RET_ERR_NOT_SYSTEM_APP:
340 return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_NOT_SYSTEM_APP,
341 ERROR_MESSAGE_NOT_SYSTEM_APP };
342 default:
343 return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_SYSTEM_ABNORMALITY,
344 ERROR_MESSAGE_SYSTEM_ABNORMALITY };
345 }
346 }
347
CreateBusinessError(napi_env env,OHOS::Accessibility::RetError errCode)348 napi_value CreateBusinessError(napi_env env, OHOS::Accessibility::RetError errCode)
349 {
350 napi_value result = nullptr;
351 if (errCode == OHOS::Accessibility::RetError::RET_OK) {
352 napi_get_undefined(env, &result);
353 } else {
354 NAccessibilityErrMsg errMsg = QueryRetMsg(errCode);
355 napi_value eCode = nullptr;
356 napi_create_int32(env, static_cast<int32_t>(errMsg.errCode), &eCode);
357 napi_value eMsg = nullptr;
358 napi_create_string_utf8(env, errMsg.message.c_str(), NAPI_AUTO_LENGTH, &eMsg);
359 napi_create_error(env, nullptr, eMsg, &result);
360 napi_set_named_property(env, result, "code", eCode);
361 }
362 return result;
363 }
364
GetErrorValue(napi_env env,int errCode)365 napi_value GetErrorValue(napi_env env, int errCode)
366 {
367 napi_value result = nullptr;
368 napi_value eCode = nullptr;
369 NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
370 NAPI_CALL(env, napi_create_object(env, &result));
371 NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode));
372 return result;
373 }
374
CheckObserverEqual(napi_env env,napi_value observer,napi_env iterEnv,napi_ref iterRef)375 bool CheckObserverEqual(napi_env env, napi_value observer, napi_env iterEnv, napi_ref iterRef)
376 {
377 HILOG_DEBUG();
378 if (env != iterEnv) {
379 return false;
380 }
381 HILOG_DEBUG("Same env, begin check observer equal");
382 napi_value item = nullptr;
383 bool equalFlag = false;
384 napi_get_reference_value(iterEnv, iterRef, &item);
385 napi_status status = napi_strict_equals(iterEnv, item, observer, &equalFlag);
386 if (status == napi_ok && equalFlag) {
387 HILOG_DEBUG("Observer exist");
388 return true;
389 }
390 return false;
391 }
392
393 /**********************************************************
394 * Convert native object to js object
395 *********************************************************/
ConvertRectToJS(napi_env env,napi_value result,const Accessibility::Rect & rect)396 void ConvertRectToJS(napi_env env, napi_value result, const Accessibility::Rect& rect)
397 {
398 napi_value nLeftTopX = nullptr;
399 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, rect.GetLeftTopXScreenPostion(), &nLeftTopX));
400 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "left", nLeftTopX));
401
402 napi_value nLeftTopY = nullptr;
403 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, rect.GetLeftTopYScreenPostion(), &nLeftTopY));
404 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "top", nLeftTopY));
405
406 napi_value nWidth = nullptr;
407 int32_t width = rect.GetRightBottomXScreenPostion() - rect.GetLeftTopXScreenPostion();
408 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, width, &nWidth));
409 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "width", nWidth));
410
411 napi_value nHeight = nullptr;
412 int32_t height = rect.GetRightBottomYScreenPostion() - rect.GetLeftTopYScreenPostion();
413 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, height, &nHeight));
414 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "height", nHeight));
415 }
416
ConvertGridItemToJS(napi_env env,napi_value result,const Accessibility::GridItemInfo & gridItem)417 void ConvertGridItemToJS(napi_env env, napi_value result, const Accessibility::GridItemInfo& gridItem)
418 {
419 napi_value rowIndex = nullptr;
420 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, gridItem.GetRowIndex(), &rowIndex));
421 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "rowIndex", rowIndex));
422 napi_value columnIndex = nullptr;
423 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, gridItem.GetColumnIndex(), &columnIndex));
424 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "columnIndex", columnIndex));
425 }
426
ConvertWindowTypeToString(AccessibilityWindowType type)427 std::string ConvertWindowTypeToString(AccessibilityWindowType type)
428 {
429 static const std::map<AccessibilityWindowType, const std::string> windowTypeTable = {
430 {AccessibilityWindowType::TYPE_ACCESSIBILITY_OVERLAY, "accessibilityOverlay"},
431 {AccessibilityWindowType::TYPE_APPLICATION, "application"},
432 {AccessibilityWindowType::TYPE_INPUT_METHOD, "inputMethod"},
433 {AccessibilityWindowType::TYPE_SPLIT_SCREEN_DIVIDER, "screenDivider"},
434 {AccessibilityWindowType::TYPE_SYSTEM, "system"}};
435
436 if (windowTypeTable.find(type) == windowTypeTable.end()) {
437 return "";
438 }
439
440 return windowTypeTable.at(type);
441 }
442
ParseEventTypesToVec(uint32_t eventTypesValue)443 static std::vector<std::string> ParseEventTypesToVec(uint32_t eventTypesValue)
444 {
445 std::vector<std::string> result;
446 static std::map<EventType, std::string> accessibilityEventTable = {
447 {EventType::TYPE_VIEW_CLICKED_EVENT, "click"},
448 {EventType::TYPE_VIEW_LONG_CLICKED_EVENT, "longClick"},
449 {EventType::TYPE_VIEW_SELECTED_EVENT, "select"},
450 {EventType::TYPE_VIEW_FOCUSED_EVENT, "focus"},
451 {EventType::TYPE_VIEW_TEXT_UPDATE_EVENT, "textUpdate"},
452 {EventType::TYPE_VIEW_HOVER_ENTER_EVENT, "hoverEnter"},
453 {EventType::TYPE_VIEW_HOVER_EXIT_EVENT, "hoverExit"},
454 {EventType::TYPE_VIEW_SCROLLED_EVENT, "scroll"},
455 {EventType::TYPE_VIEW_TEXT_SELECTION_UPDATE_EVENT, "textSelectionUpdate"},
456 {EventType::TYPE_VIEW_ACCESSIBILITY_FOCUSED_EVENT, "accessibilityFocus"},
457 {EventType::TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED_EVENT, "accessibilityFocusClear"},
458 {EventType::TYPE_VIEW_REQUEST_FOCUS_FOR_ACCESSIBILITY, "requestFocusForAccessibility"},
459 {EventType::TYPE_VIEW_ANNOUNCE_FOR_ACCESSIBILITY, "announceForAccessibility"},
460 {EventType::TYPE_VIEW_ANNOUNCE_FOR_ACCESSIBILITY_NOT_INTERRUPT, "announceForAccessibilityNotInterrupt"},
461 {EventType::TYPE_VIEW_REQUEST_FOCUS_FOR_ACCESSIBILITY_NOT_INTERRUP,
462 "requestFocusForAccessibilityNotInterrupt"},
463 {EventType::TYPE_VIEW_SCROLLING_EVENT, "scrolling"}};
464
465 for (std::map<EventType, std::string>::iterator itr = accessibilityEventTable.begin();
466 itr != accessibilityEventTable.end(); ++itr) {
467 if (eventTypesValue & itr->first) {
468 result.push_back(itr->second);
469 }
470 }
471
472 return result;
473 }
474
ParseAbilityTypesToVec(uint32_t abilityTypesValue)475 static std::vector<std::string> ParseAbilityTypesToVec(uint32_t abilityTypesValue)
476 {
477 std::vector<std::string> result;
478
479 if (abilityTypesValue & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_SPOKEN) {
480 result.push_back("spoken");
481 }
482 if (abilityTypesValue & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_HAPTIC) {
483 result.push_back("haptic");
484 }
485 if (abilityTypesValue & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_AUDIBLE) {
486 result.push_back("audible");
487 }
488 if (abilityTypesValue & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_VISUAL) {
489 result.push_back("visual");
490 }
491 if (abilityTypesValue & AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_GENERIC) {
492 result.push_back("generic");
493 }
494
495 return result;
496 }
497
ParseCapabilitiesToVec(uint32_t capabilitiesValue)498 static std::vector<std::string> ParseCapabilitiesToVec(uint32_t capabilitiesValue)
499 {
500 std::vector<std::string> result;
501
502 if (capabilitiesValue & Capability::CAPABILITY_RETRIEVE) {
503 result.push_back("retrieve");
504 }
505 if (capabilitiesValue & Capability::CAPABILITY_TOUCH_GUIDE) {
506 result.push_back("touchGuide");
507 }
508 if (capabilitiesValue & Capability::CAPABILITY_KEY_EVENT_OBSERVER) {
509 result.push_back("keyEventObserver");
510 }
511 if (capabilitiesValue & Capability::CAPABILITY_ZOOM) {
512 result.push_back("zoom");
513 }
514 if (capabilitiesValue & Capability::CAPABILITY_GESTURE) {
515 result.push_back("gesture");
516 }
517
518 return result;
519 }
520
ConvertDaltonizationTypeToString(OHOS::AccessibilityConfig::DALTONIZATION_TYPE type)521 std::string ConvertDaltonizationTypeToString(OHOS::AccessibilityConfig::DALTONIZATION_TYPE type)
522 {
523 static const std::map<OHOS::AccessibilityConfig::DALTONIZATION_TYPE, const std::string> typeTable = {
524 {OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Normal, "Normal"},
525 {OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Protanomaly, "Protanomaly"},
526 {OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Deuteranomaly, "Deuteranomaly"},
527 {OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Tritanomaly, "Tritanomaly"}};
528
529 if (typeTable.find(type) == typeTable.end()) {
530 return "";
531 }
532
533 return typeTable.at(type);
534 }
535
ConvertClickResponseTimeTypeToString(OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME type)536 std::string ConvertClickResponseTimeTypeToString(OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME type)
537 {
538 static const std::map<OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME, const std::string> typeTable = {
539 {OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayShort, "Short"},
540 {OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayMedium, "Medium"},
541 {OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayLong, "Long"}};
542
543 if (typeTable.find(type) == typeTable.end()) {
544 return "";
545 }
546
547 return typeTable.at(type);
548 }
549
ConvertIgnoreRepeatClickTimeTypeToString(OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME type)550 std::string ConvertIgnoreRepeatClickTimeTypeToString(OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME type)
551 {
552 static const std::map<OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME, const std::string> typeTable = {
553 {OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutShortest, "Shortest"},
554 {OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutShort, "Short"},
555 {OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutMedium, "Medium"},
556 {OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutLong, "Long"},
557 {OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutLongest, "Longest"}};
558
559 if (typeTable.find(type) == typeTable.end()) {
560 return "";
561 }
562
563 return typeTable.at(type);
564 }
565
ConvertAccessibleAbilityInfoToJS(napi_env env,napi_value & result,OHOS::Accessibility::AccessibilityAbilityInfo & info)566 void ConvertAccessibleAbilityInfoToJS(
567 napi_env env, napi_value& result, OHOS::Accessibility::AccessibilityAbilityInfo& info)
568 {
569 HILOG_DEBUG();
570 ConvertAccessibleAbilityInfoToJSPart1(env, result, info);
571 ConvertAccessibleAbilityInfoToJSPart2(env, result, info);
572 ConvertAccessibleAbilityInfoToJSPart3(env, result, info);
573 }
574
ConvertAccessibleAbilityInfoToJSPart1(napi_env env,napi_value & result,OHOS::Accessibility::AccessibilityAbilityInfo & info)575 void ConvertAccessibleAbilityInfoToJSPart1(
576 napi_env env, napi_value& result, OHOS::Accessibility::AccessibilityAbilityInfo& info)
577 {
578 HILOG_DEBUG();
579 napi_value nId = nullptr;
580 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, info.GetId().c_str(), NAPI_AUTO_LENGTH, &nId));
581 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "id", nId));
582
583 napi_value nName = nullptr;
584 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, info.GetName().c_str(), NAPI_AUTO_LENGTH, &nName));
585 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "name", nName));
586
587 napi_value nBundleName = nullptr;
588 NAPI_CALL_RETURN_VOID(
589 env, napi_create_string_utf8(env, info.GetPackageName().c_str(), NAPI_AUTO_LENGTH, &nBundleName));
590 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "bundleName", nBundleName));
591
592 napi_value nAbilityType = nullptr;
593 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &nAbilityType));
594 uint32_t abilityTypesValue = info.GetAccessibilityAbilityType();
595 std::vector<std::string> abilityTypes = ParseAbilityTypesToVec(abilityTypesValue);
596 for (size_t idxType = 0; idxType < abilityTypes.size(); idxType++) {
597 napi_value nType = nullptr;
598 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, abilityTypes[idxType].c_str(),
599 NAPI_AUTO_LENGTH, &nType));
600 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, nAbilityType, idxType, nType));
601 }
602 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "abilityTypes", nAbilityType));
603 }
604
ConvertAccessibleAbilityInfoToJSPart2(napi_env env,napi_value & result,OHOS::Accessibility::AccessibilityAbilityInfo & info)605 void ConvertAccessibleAbilityInfoToJSPart2(
606 napi_env env, napi_value& result, OHOS::Accessibility::AccessibilityAbilityInfo& info)
607 {
608 HILOG_DEBUG();
609 napi_value nCapabilities = nullptr;
610 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &nCapabilities));
611 uint32_t capabilitiesValue = info.GetStaticCapabilityValues();
612 std::vector<std::string> capabilities = ParseCapabilitiesToVec(capabilitiesValue);
613 for (size_t idxCap = 0; idxCap < capabilities.size(); idxCap++) {
614 napi_value nCap = nullptr;
615 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, capabilities[idxCap].c_str(),
616 NAPI_AUTO_LENGTH, &nCap));
617 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, nCapabilities, idxCap, nCap));
618 }
619 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "capabilities", nCapabilities));
620
621 napi_value description = nullptr;
622 NAPI_CALL_RETURN_VOID(
623 env, napi_create_string_utf8(env, info.GetDescription().c_str(), NAPI_AUTO_LENGTH, &description));
624 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "description", description));
625
626 napi_value nEventTypes = nullptr;
627 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &nEventTypes));
628 uint32_t eventTypesValue = info.GetEventTypes();
629 std::vector<std::string> eventTypes = ParseEventTypesToVec(eventTypesValue);
630 for (size_t idxEve = 0; idxEve < eventTypes.size(); idxEve++) {
631 napi_value nEve = nullptr;
632 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, eventTypes[idxEve].c_str(), NAPI_AUTO_LENGTH, &nEve));
633 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, nEventTypes, idxEve, nEve));
634 }
635 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "eventTypes", nEventTypes));
636
637 napi_value filterBundleNames = nullptr;
638 size_t idx = 0;
639 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &filterBundleNames));
640 std::vector<std::string> strFilterBundleNames = info.GetFilterBundleNames();
641 for (auto &filterBundleName : strFilterBundleNames) {
642 napi_value bundleName = nullptr;
643 NAPI_CALL_RETURN_VOID(
644 env, napi_create_string_utf8(env, filterBundleName.c_str(), NAPI_AUTO_LENGTH, &bundleName));
645 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, filterBundleNames, idx, bundleName));
646 idx++;
647 }
648 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "targetBundleNames", filterBundleNames));
649 }
650
ConvertAccessibleAbilityInfoToJSPart3(napi_env env,napi_value & result,OHOS::Accessibility::AccessibilityAbilityInfo & info)651 void ConvertAccessibleAbilityInfoToJSPart3(
652 napi_env env, napi_value& result, OHOS::Accessibility::AccessibilityAbilityInfo& info)
653 {
654 HILOG_DEBUG();
655 napi_value nNeedHide = nullptr;
656 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, info.NeedHide(), &nNeedHide));
657 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "needHide", nNeedHide));
658 }
659
ConvertAccessibleAbilityInfosToJS(napi_env env,napi_value & result,std::vector<OHOS::Accessibility::AccessibilityAbilityInfo> & accessibleAbilityInfos)660 void ConvertAccessibleAbilityInfosToJS(napi_env env, napi_value& result,
661 std::vector<OHOS::Accessibility::AccessibilityAbilityInfo>& accessibleAbilityInfos)
662 {
663 size_t index = 0;
664
665 if (accessibleAbilityInfos.empty()) {
666 return;
667 }
668
669 for (auto& abilityInfo : accessibleAbilityInfos) {
670 napi_value obj = nullptr;
671 napi_create_object(env, &obj);
672 ConvertAccessibleAbilityInfoToJS(env, obj, abilityInfo);
673 napi_set_element(env, result, index, obj);
674 index++;
675 }
676 }
677
ConvertAccessibilityEventTypeToString(EventType type)678 const std::string ConvertAccessibilityEventTypeToString(EventType type)
679 {
680 static const std::map<EventType, const std::string> a11yEvtTypeTable = {
681 {EventType::TYPE_VIEW_ACCESSIBILITY_FOCUSED_EVENT, "accessibilityFocus"},
682 {EventType::TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED_EVENT, "accessibilityFocusClear"},
683 {EventType::TYPE_VIEW_CLICKED_EVENT, "click"},
684 {EventType::TYPE_VIEW_LONG_CLICKED_EVENT, "longClick"},
685 {EventType::TYPE_VIEW_FOCUSED_EVENT, "focus"},
686 {EventType::TYPE_VIEW_SELECTED_EVENT, "select"},
687 {EventType::TYPE_VIEW_SCROLLED_EVENT, "scroll"},
688 {EventType::TYPE_VIEW_HOVER_ENTER_EVENT, "hoverEnter"},
689 {EventType::TYPE_VIEW_HOVER_EXIT_EVENT, "hoverExit"},
690 {EventType::TYPE_VIEW_TEXT_UPDATE_EVENT, "textUpdate"},
691 {EventType::TYPE_VIEW_TEXT_SELECTION_UPDATE_EVENT, "textSelectionUpdate"},
692 {EventType::TYPE_PAGE_CONTENT_UPDATE, "pageContentUpdate"},
693 {EventType::TYPE_PAGE_STATE_UPDATE, "pageStateUpdate"},
694 {EventType::TYPE_TOUCH_BEGIN, "touchBegin"},
695 {EventType::TYPE_TOUCH_END, "touchEnd"},
696 {EventType::TYPE_VIEW_REQUEST_FOCUS_FOR_ACCESSIBILITY, "requestFocusForAccessibility"},
697 {EventType::TYPE_VIEW_ANNOUNCE_FOR_ACCESSIBILITY, "announceForAccessibility"},
698 {EventType::TYPE_PAGE_OPEN, "pageOpen"},
699 {EventType::TYPE_PAGE_CLOSE, "pageClose"},
700 {EventType::TYPE_ELEMENT_INFO_CHANGE, "elementInfoChange"},
701 {EventType::TYPE_VIEW_ANNOUNCE_FOR_ACCESSIBILITY_NOT_INTERRUPT, "announceForAccessibilityNotInterrupt"},
702 {EventType::TYPE_VIEW_REQUEST_FOCUS_FOR_ACCESSIBILITY_NOT_INTERRUP,
703 "requestFocusForAccessibilityNotInterrupt"},
704 {EventType::TYPE_VIEW_SCROLLING_EVENT, "scrolling"}};
705
706 if (a11yEvtTypeTable.find(type) == a11yEvtTypeTable.end()) {
707 return "";
708 }
709
710 return a11yEvtTypeTable.at(type);
711 }
712
CoverGestureTypeToString(GestureType type)713 std::string CoverGestureTypeToString(GestureType type)
714 {
715 static const std::map<GestureType, const std::string> gestureTypeTable = {
716 {GestureType::GESTURE_SWIPE_LEFT, "left"},
717 {GestureType::GESTURE_SWIPE_LEFT_THEN_RIGHT, "leftThenRight"},
718 {GestureType::GESTURE_SWIPE_LEFT_THEN_UP, "leftThenUp"},
719 {GestureType::GESTURE_SWIPE_LEFT_THEN_DOWN, "leftThenDown"},
720 {GestureType::GESTURE_SWIPE_RIGHT, "right"},
721 {GestureType::GESTURE_SWIPE_RIGHT_THEN_LEFT, "rightThenLeft"},
722 {GestureType::GESTURE_SWIPE_RIGHT_THEN_UP, "rightThenUp"},
723 {GestureType::GESTURE_SWIPE_RIGHT_THEN_DOWN, "rightThenDown"},
724 {GestureType::GESTURE_SWIPE_UP, "up"},
725 {GestureType::GESTURE_SWIPE_UP_THEN_LEFT, "upThenLeft"},
726 {GestureType::GESTURE_SWIPE_UP_THEN_RIGHT, "upThenRight"},
727 {GestureType::GESTURE_SWIPE_UP_THEN_DOWN, "upThenDown"},
728 {GestureType::GESTURE_SWIPE_DOWN, "down"},
729 {GestureType::GESTURE_SWIPE_DOWN_THEN_LEFT, "downThenLeft"},
730 {GestureType::GESTURE_SWIPE_DOWN_THEN_RIGHT, "downThenRight"},
731 {GestureType::GESTURE_SWIPE_DOWN_THEN_UP, "downThenUp"},
732 {GestureType::GESTURE_TWO_FINGER_SINGLE_TAP, "twoFingerSingleTap"},
733 {GestureType::GESTURE_TWO_FINGER_DOUBLE_TAP, "twoFingerDoubleTap"},
734 {GestureType::GESTURE_TWO_FINGER_DOUBLE_TAP_AND_HOLD, "twoFingerDoubleTapAndHold"},
735 {GestureType::GESTURE_TWO_FINGER_TRIPLE_TAP, "twoFingerTripleTap"},
736 {GestureType::GESTURE_TWO_FINGER_TRIPLE_TAP_AND_HOLD, "twoFingerTripleTapAndHold"},
737 {GestureType::GESTURE_THREE_FINGER_SINGLE_TAP, "threeFingerSingleTap"},
738 {GestureType::GESTURE_THREE_FINGER_DOUBLE_TAP, "threeFingerDoubleTap"},
739 {GestureType::GESTURE_THREE_FINGER_DOUBLE_TAP_AND_HOLD, "threeFingerDoubleTapAndHold"},
740 {GestureType::GESTURE_THREE_FINGER_TRIPLE_TAP, "threeFingerTripleTap"},
741 {GestureType::GESTURE_THREE_FINGER_TRIPLE_TAP_AND_HOLD, "threeFingerTripleTapAndHold"},
742 {GestureType::GESTURE_FOUR_FINGER_SINGLE_TAP, "fourFingerSingleTap"},
743 {GestureType::GESTURE_FOUR_FINGER_DOUBLE_TAP, "fourFingerDoubleTap"},
744 {GestureType::GESTURE_FOUR_FINGER_DOUBLE_TAP_AND_HOLD, "fourFingerDoubleTapAndHold"},
745 {GestureType::GESTURE_FOUR_FINGER_TRIPLE_TAP, "fourFingerTripleTap"},
746 {GestureType::GESTURE_FOUR_FINGER_TRIPLE_TAP_AND_HOLD, "fourFingerTripleTapAndHold"},
747 {GestureType::GESTURE_THREE_FINGER_SWIPE_UP, "threeFingerSwipeUp"},
748 {GestureType::GESTURE_THREE_FINGER_SWIPE_DOWN, "threeFingerSwipeDown"},
749 {GestureType::GESTURE_THREE_FINGER_SWIPE_LEFT, "threeFingerSwipeLeft"},
750 {GestureType::GESTURE_THREE_FINGER_SWIPE_RIGHT, "threeFingerSwipeRight"},
751 {GestureType::GESTURE_FOUR_FINGER_SWIPE_UP, "fourFingerSwipeUp"},
752 {GestureType::GESTURE_FOUR_FINGER_SWIPE_DOWN, "fourFingerSwipeDown"},
753 {GestureType::GESTURE_FOUR_FINGER_SWIPE_LEFT, "fourFingerSwipeLeft"},
754 {GestureType::GESTURE_FOUR_FINGER_SWIPE_RIGHT, "fourFingerSwipeRight"}
755 };
756
757 if (gestureTypeTable.find(type) == gestureTypeTable.end()) {
758 return "";
759 }
760
761 return gestureTypeTable.at(type);
762 }
763
ConvertWindowUpdateTypeToString(WindowUpdateType type)764 const std::string ConvertWindowUpdateTypeToString(WindowUpdateType type)
765 {
766 static const std::map<WindowUpdateType, const std::string> windowUpdateTypeTable = {
767 {WindowUpdateType::WINDOW_UPDATE_FOCUSED, "focus"},
768 {WindowUpdateType::WINDOW_UPDATE_ACTIVE, "active"},
769 {WindowUpdateType::WINDOW_UPDATE_ADDED, "add"},
770 {WindowUpdateType::WINDOW_UPDATE_REMOVED, "remove"},
771 {WindowUpdateType::WINDOW_UPDATE_BOUNDS, "bounds"},
772 {WindowUpdateType::WINDOW_UPDATE_PROPERTY, "property"},
773 {WindowUpdateType::WINDOW_UPDATE_LAYER, "layer"}};
774
775 if (windowUpdateTypeTable.find(type) == windowUpdateTypeTable.end()) {
776 return "";
777 }
778
779 return windowUpdateTypeTable.at(type);
780 }
781
ConvertEventTypeToString(const AccessibilityEventInfo & eventInfo,std::string & eventTypeString)782 void ConvertEventTypeToString(const AccessibilityEventInfo &eventInfo, std::string &eventTypeString)
783 {
784 EventType type = eventInfo.GetEventType();
785 switch (type) {
786 case TYPE_GESTURE_EVENT: {
787 GestureType gestureType = eventInfo.GetGestureType();
788 eventTypeString = CoverGestureTypeToString(gestureType);
789 break;
790 }
791 case TYPE_WINDOW_UPDATE: {
792 WindowUpdateType windowUpdateType = eventInfo.GetWindowChangeTypes();
793 eventTypeString = ConvertWindowUpdateTypeToString(windowUpdateType);
794 break;
795 }
796 default:
797 eventTypeString = ConvertAccessibilityEventTypeToString(type);
798 break;
799 }
800 }
801
ConvertOperationTypeToString(ActionType type)802 std::string ConvertOperationTypeToString(ActionType type)
803 {
804 static const std::map<ActionType, const std::string> triggerActionTable = {
805 {ActionType::ACCESSIBILITY_ACTION_FOCUS, "focus"},
806 {ActionType::ACCESSIBILITY_ACTION_CLEAR_FOCUS, "clearFocus"},
807 {ActionType::ACCESSIBILITY_ACTION_SELECT, "select"},
808 {ActionType::ACCESSIBILITY_ACTION_CLEAR_SELECTION, "clearSelection"},
809 {ActionType::ACCESSIBILITY_ACTION_CLICK, "click"},
810 {ActionType::ACCESSIBILITY_ACTION_LONG_CLICK, "longClick"},
811 {ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS, "accessibilityFocus"},
812 {ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS, "clearAccessibilityFocus"},
813 {ActionType::ACCESSIBILITY_ACTION_SCROLL_FORWARD, "scrollForward"},
814 {ActionType::ACCESSIBILITY_ACTION_SCROLL_BACKWARD, "scrollBackward"},
815 {ActionType::ACCESSIBILITY_ACTION_COPY, "copy"},
816 {ActionType::ACCESSIBILITY_ACTION_PASTE, "paste"},
817 {ActionType::ACCESSIBILITY_ACTION_CUT, "cut"},
818 {ActionType::ACCESSIBILITY_ACTION_SET_SELECTION, "setSelection"},
819 {ActionType::ACCESSIBILITY_ACTION_SET_CURSOR_POSITION, "setCursorPosition"},
820 {ActionType::ACCESSIBILITY_ACTION_COMMON, "common"},
821 {ActionType::ACCESSIBILITY_ACTION_SET_TEXT, "setText"},
822 {ActionType::ACCESSIBILITY_ACTION_DELETED, "delete"},
823 {ActionType::ACCESSIBILITY_ACTION_SPAN_CLICK, "spanClick"},
824 {ActionType::ACCESSIBILITY_ACTION_NEXT_HTML_ITEM, "nextHtmlItem"},
825 {ActionType::ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM, "previousHtmlItem"}
826 };
827
828 if (triggerActionTable.find(type) == triggerActionTable.end()) {
829 return "";
830 }
831
832 return triggerActionTable.at(type);
833 }
834
ConvertStringToWindowUpdateTypes(std::string type)835 static WindowUpdateType ConvertStringToWindowUpdateTypes(std::string type)
836 {
837 static const std::map<const std::string, WindowUpdateType> windowsUpdateTypesTable = {
838 {"accessibilityFocus", WindowUpdateType::WINDOW_UPDATE_ACCESSIBILITY_FOCUSED},
839 {"focus", WindowUpdateType::WINDOW_UPDATE_FOCUSED},
840 {"active", WindowUpdateType::WINDOW_UPDATE_ACTIVE},
841 {"add", WindowUpdateType::WINDOW_UPDATE_ADDED},
842 {"remove", WindowUpdateType::WINDOW_UPDATE_REMOVED},
843 {"bounds", WindowUpdateType::WINDOW_UPDATE_BOUNDS},
844 {"title", WindowUpdateType::WINDOW_UPDATE_TITLE},
845 {"layer", WindowUpdateType::WINDOW_UPDATE_LAYER},
846 {"parent", WindowUpdateType::WINDOW_UPDATE_PARENT},
847 {"children", WindowUpdateType::WINDOW_UPDATE_CHILDREN},
848 {"pip", WindowUpdateType::WINDOW_UPDATE_PIP},
849 {"property", WindowUpdateType::WINDOW_UPDATE_PROPERTY}};
850
851 if (windowsUpdateTypesTable.find(type) == windowsUpdateTypesTable.end()) {
852 HILOG_WARN("invalid key[%{public}s]", type.c_str());
853 return WINDOW_UPDATE_INVALID;
854 }
855
856 return windowsUpdateTypesTable.at(type);
857 }
858
ConvertStringToEventInfoTypes(std::string type)859 static EventType ConvertStringToEventInfoTypes(std::string type)
860 {
861 static const std::map<const std::string, EventType> eventInfoTypesTable = {
862 {"click", EventType::TYPE_VIEW_CLICKED_EVENT},
863 {"longClick", EventType::TYPE_VIEW_LONG_CLICKED_EVENT},
864 {"select", EventType::TYPE_VIEW_SELECTED_EVENT},
865 {"focus", EventType::TYPE_VIEW_FOCUSED_EVENT},
866 {"textUpdate", EventType::TYPE_VIEW_TEXT_UPDATE_EVENT},
867 {"hoverEnter", EventType::TYPE_VIEW_HOVER_ENTER_EVENT},
868 {"hoverExit", EventType::TYPE_VIEW_HOVER_EXIT_EVENT},
869 {"scroll", EventType::TYPE_VIEW_SCROLLED_EVENT},
870 {"textSelectionUpdate", EventType::TYPE_VIEW_TEXT_SELECTION_UPDATE_EVENT},
871 {"accessibilityFocus", EventType::TYPE_VIEW_ACCESSIBILITY_FOCUSED_EVENT},
872 {"accessibilityFocusClear", EventType::TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED_EVENT},
873 {"requestFocusForAccessibility", EventType::TYPE_VIEW_REQUEST_FOCUS_FOR_ACCESSIBILITY},
874 {"announceForAccessibility", EventType::TYPE_VIEW_ANNOUNCE_FOR_ACCESSIBILITY},
875 {"announceForAccessibilityNotInterrupt", EventType::TYPE_VIEW_ANNOUNCE_FOR_ACCESSIBILITY_NOT_INTERRUPT},
876 {"requestFocusForAccessibilityNotInterrupt",
877 EventType::TYPE_VIEW_REQUEST_FOCUS_FOR_ACCESSIBILITY_NOT_INTERRUP},
878 {"scrolling", EventType::TYPE_VIEW_SCROLLING_EVENT}};
879
880 if (eventInfoTypesTable.find(type) == eventInfoTypesTable.end()) {
881 HILOG_WARN("invalid key[%{public}s]", type.c_str());
882 return TYPE_VIEW_INVALID;
883 }
884
885 return eventInfoTypesTable.at(type);
886 }
887
ConvertStringToCapability(std::string type)888 static uint32_t ConvertStringToCapability(std::string type)
889 {
890 HILOG_DEBUG();
891 static const std::map<const std::string, uint32_t> capabilitiesTable = {
892 {"retrieve", Capability::CAPABILITY_RETRIEVE},
893 {"touchGuide", Capability::CAPABILITY_TOUCH_GUIDE},
894 {"keyEventObserver", Capability::CAPABILITY_KEY_EVENT_OBSERVER},
895 {"zoom", Capability::CAPABILITY_ZOOM},
896 {"gesture", Capability::CAPABILITY_GESTURE}};
897
898 if (capabilitiesTable.find(type) == capabilitiesTable.end()) {
899 HILOG_WARN("invalid key[%{public}s]", type.c_str());
900 return 0;
901 }
902
903 return capabilitiesTable.at(type);
904 }
905
ConvertStringToAccessibleOperationType(const std::string & type)906 ActionType ConvertStringToAccessibleOperationType(const std::string &type)
907 {
908 std::map<const std::string, ActionType> accessibleOperationTypeTable = {
909 {"focus", ActionType::ACCESSIBILITY_ACTION_FOCUS},
910 {"clearFocus", ActionType::ACCESSIBILITY_ACTION_CLEAR_FOCUS},
911 {"select", ActionType::ACCESSIBILITY_ACTION_SELECT},
912 {"clearSelection", ActionType::ACCESSIBILITY_ACTION_CLEAR_SELECTION},
913 {"click", ActionType::ACCESSIBILITY_ACTION_CLICK},
914 {"longClick", ActionType::ACCESSIBILITY_ACTION_LONG_CLICK},
915 {"accessibilityFocus", ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS},
916 {"clearAccessibilityFocus", ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS},
917 {"scrollForward", ActionType::ACCESSIBILITY_ACTION_SCROLL_FORWARD},
918 {"scrollBackward", ActionType::ACCESSIBILITY_ACTION_SCROLL_BACKWARD},
919 {"copy", ActionType::ACCESSIBILITY_ACTION_COPY},
920 {"paste", ActionType::ACCESSIBILITY_ACTION_PASTE},
921 {"cut", ActionType::ACCESSIBILITY_ACTION_CUT},
922 {"setSelection", ActionType::ACCESSIBILITY_ACTION_SET_SELECTION},
923 {"setCursorPosition", ActionType::ACCESSIBILITY_ACTION_SET_CURSOR_POSITION},
924 {"common", ActionType::ACCESSIBILITY_ACTION_COMMON},
925 {"setText", ActionType::ACCESSIBILITY_ACTION_SET_TEXT},
926 {"delete", ActionType::ACCESSIBILITY_ACTION_DELETED},
927 {"home", ActionType::ACCESSIBILITY_ACTION_HOME},
928 {"back", ActionType::ACCESSIBILITY_ACTION_BACK},
929 {"recentTask", ActionType::ACCESSIBILITY_ACTION_RECENTTASK},
930 {"notificationCenter", ActionType::ACCESSIBILITY_ACTION_NOTIFICATIONCENTER},
931 {"controlCenter", ActionType::ACCESSIBILITY_ACTION_CONTROLCENTER},
932 {"spanClick", ActionType::ACCESSIBILITY_ACTION_SPAN_CLICK},
933 {"nextHtmlItem", ActionType::ACCESSIBILITY_ACTION_NEXT_HTML_ITEM},
934 {"previousHtmlItem", ActionType::ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM}};
935
936 if (accessibleOperationTypeTable.find(type) == accessibleOperationTypeTable.end()) {
937 HILOG_WARN("invalid key[%{public}s]", type.c_str());
938 return ACCESSIBILITY_ACTION_INVALID;
939 }
940
941 return accessibleOperationTypeTable.at(type);
942 }
943
ConvertStringToAccessibilityAbilityTypes(const std::string & type)944 AccessibilityAbilityTypes ConvertStringToAccessibilityAbilityTypes(const std::string &type)
945 {
946 std::map<const std::string, AccessibilityAbilityTypes> accessibilityAbilityTypesTable = {
947 {"spoken", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_SPOKEN},
948 {"haptic", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_HAPTIC},
949 {"audible", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_AUDIBLE},
950 {"visual", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_VISUAL},
951 {"generic", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_GENERIC},
952 {"all", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_ALL},
953 };
954
955 if (accessibilityAbilityTypesTable.find(type) == accessibilityAbilityTypesTable.end()) {
956 HILOG_WARN("invalid key[%{public}s]", type.c_str());
957 return AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_INVALID;
958 }
959
960 return accessibilityAbilityTypesTable.at(type);
961 }
962
ConvertStringToAbilityStateType(const std::string & type)963 AbilityStateType ConvertStringToAbilityStateType(const std::string &type)
964 {
965 std::map<const std::string, AbilityStateType> abilityStateTypeTable = {
966 {"enable", AbilityStateType::ABILITY_STATE_ENABLE},
967 {"disable", AbilityStateType::ABILITY_STATE_DISABLE},
968 {"install", AbilityStateType::ABILITY_STATE_INSTALLED}};
969
970 if (abilityStateTypeTable.find(type) == abilityStateTypeTable.end()) {
971 HILOG_WARN("invalid key[%{public}s]", type.c_str());
972 return ABILITY_STATE_INVALID;
973 }
974
975 return abilityStateTypeTable.at(type);
976 }
977
ConvertStringToDaltonizationTypes(std::string & type)978 OHOS::AccessibilityConfig::DALTONIZATION_TYPE ConvertStringToDaltonizationTypes(std::string& type)
979 {
980 std::map<const std::string, OHOS::AccessibilityConfig::DALTONIZATION_TYPE> daltonizationTTypesTable = {
981 {"Normal", OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Normal},
982 {"Protanomaly", OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Protanomaly},
983 {"Deuteranomaly", OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Deuteranomaly},
984 {"Tritanomaly", OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Tritanomaly},
985 };
986
987 if (daltonizationTTypesTable.find(type) == daltonizationTTypesTable.end()) {
988 HILOG_WARN("invalid key[%{public}s]", type.c_str());
989 return OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Normal;
990 }
991
992 return daltonizationTTypesTable.at(type);
993 }
994
ConvertStringToClickResponseTimeTypes(std::string & type)995 OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME ConvertStringToClickResponseTimeTypes(std::string& type)
996 {
997 std::map<const std::string, OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME> clickResponseTimeTypesTable = {
998 {"Short", OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayShort},
999 {"Medium", OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayMedium},
1000 {"Long", OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayLong},
1001 };
1002
1003 if (clickResponseTimeTypesTable.find(type) == clickResponseTimeTypesTable.end()) {
1004 HILOG_WARN("invalid key[%{public}s]", type.c_str());
1005 return OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayShort;
1006 }
1007
1008 return clickResponseTimeTypesTable.at(type);
1009 }
1010
ConvertStringToIgnoreRepeatClickTimeTypes(std::string & type)1011 OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME ConvertStringToIgnoreRepeatClickTimeTypes(std::string& type)
1012 {
1013 std::map<const std::string, OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME> mapTable = {
1014 {"Shortest", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutShortest},
1015 {"Short", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutShort},
1016 {"Medium", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutMedium},
1017 {"Long", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutLong},
1018 {"Longest", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutLongest},
1019 };
1020
1021 if (mapTable.find(type) == mapTable.end()) {
1022 HILOG_WARN("invalid key[%{public}s]", type.c_str());
1023 return OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutShortest;
1024 }
1025
1026 return mapTable.at(type);
1027 }
1028
ConvertStringToTextMoveUnit(const std::string & type)1029 TextMoveUnit ConvertStringToTextMoveUnit(const std::string &type)
1030 {
1031 static const std::map<const std::string, TextMoveUnit> textMoveUnitTable = {{"char", TextMoveUnit::STEP_CHARACTER},
1032 {"word", TextMoveUnit::STEP_WORD},
1033 {"line", TextMoveUnit::STEP_LINE},
1034 {"page", TextMoveUnit::STEP_PAGE},
1035 {"paragraph", TextMoveUnit::STEP_PARAGRAPH}};
1036
1037 if (textMoveUnitTable.find(type) == textMoveUnitTable.end()) {
1038 HILOG_WARN("invalid key[%{public}s]", type.c_str());
1039 return STEP_INVALID;
1040 }
1041
1042 return textMoveUnitTable.at(type);
1043 }
1044
ConvertTextMoveUnitToString(TextMoveUnit type)1045 std::string ConvertTextMoveUnitToString(TextMoveUnit type)
1046 {
1047 static const std::map<TextMoveUnit, const std::string> textMoveUnitTable = {{TextMoveUnit::STEP_CHARACTER, "char"},
1048 {TextMoveUnit::STEP_WORD, "word"},
1049 {TextMoveUnit::STEP_LINE, "line"},
1050 {TextMoveUnit::STEP_PAGE, "page"},
1051 {TextMoveUnit::STEP_PARAGRAPH, "paragraph"}};
1052
1053 if (textMoveUnitTable.find(type) == textMoveUnitTable.end()) {
1054 HILOG_WARN("invalid key[0x%{public}x]", type);
1055 return "";
1056 }
1057
1058 return textMoveUnitTable.at(type);
1059 }
1060
ConvertActionArgsJSToNAPI(napi_env env,napi_value object,std::map<std::string,std::string> & args,OHOS::Accessibility::ActionType action)1061 void ConvertActionArgsJSToNAPI(
1062 napi_env env, napi_value object, std::map<std::string, std::string>& args, OHOS::Accessibility::ActionType action)
1063 {
1064 napi_value propertyNameValue = nullptr;
1065 bool hasProperty = false;
1066 std::string str = "";
1067 std::map<std::string, std::string> scrollValueMap = { {"halfScreen", HALF_VALUE}, {"fullScreen", FULL_VALUE} };
1068 std::string scrollValue = FULL_VALUE;
1069 bool seleFlag = false;
1070 switch (action) {
1071 case ActionType::ACCESSIBILITY_ACTION_NEXT_HTML_ITEM:
1072 case ActionType::ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM:
1073 napi_create_string_utf8(env, "htmlItem", NAPI_AUTO_LENGTH, &propertyNameValue);
1074 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1075 if (hasProperty) {
1076 args.insert(std::pair<std::string, std::string>("htmlItem", str.c_str()));
1077 }
1078 break;
1079 case ActionType::ACCESSIBILITY_ACTION_NEXT_TEXT:
1080 case ActionType::ACCESSIBILITY_ACTION_PREVIOUS_TEXT:
1081 napi_create_string_utf8(env, "textMoveUnit", NAPI_AUTO_LENGTH, &propertyNameValue);
1082 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1083 if (hasProperty) {
1084 args.insert(std::pair<std::string, std::string>("textMoveUnit", str.c_str()));
1085 }
1086 break;
1087 case ActionType::ACCESSIBILITY_ACTION_SET_SELECTION:
1088 napi_create_string_utf8(env, "selectTextBegin", NAPI_AUTO_LENGTH, &propertyNameValue);
1089 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1090 if (hasProperty) {
1091 args.insert(std::pair<std::string, std::string>("selectTextBegin", str.c_str()));
1092 }
1093 napi_create_string_utf8(env, "selectTextEnd", NAPI_AUTO_LENGTH, &propertyNameValue);
1094 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1095 if (hasProperty) {
1096 args.insert(std::pair<std::string, std::string>("selectTextEnd", str.c_str()));
1097 }
1098 napi_create_string_utf8(env, "selectTextInForWard", NAPI_AUTO_LENGTH, &propertyNameValue);
1099 seleFlag = ConvertBoolJSToNAPI(env, object, propertyNameValue, hasProperty);
1100 if (hasProperty) {
1101 std::string value = seleFlag ? "forWard" : "backWard";
1102 args.insert(std::pair<std::string, std::string>("selectTextInForWard", value.c_str()));
1103 }
1104 break;
1105 case ActionType::ACCESSIBILITY_ACTION_SET_CURSOR_POSITION:
1106 napi_create_string_utf8(env, "offset", NAPI_AUTO_LENGTH, &propertyNameValue);
1107 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1108 if (hasProperty) {
1109 args.insert(std::pair<std::string, std::string>("offset", str.c_str()));
1110 }
1111 break;
1112 case ActionType::ACCESSIBILITY_ACTION_SET_TEXT:
1113 napi_create_string_utf8(env, "setText", NAPI_AUTO_LENGTH, &propertyNameValue);
1114 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1115 if (hasProperty) {
1116 args.insert(std::pair<std::string, std::string>("setText", str.c_str()));
1117 }
1118 break;
1119 case ActionType::ACCESSIBILITY_ACTION_SPAN_CLICK:
1120 napi_create_string_utf8(env, "spanId", NAPI_AUTO_LENGTH, &propertyNameValue);
1121 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1122 if (hasProperty) {
1123 args.insert(std::pair<std::string, std::string>("spanId", str.c_str()));
1124 }
1125 break;
1126 case ActionType::ACCESSIBILITY_ACTION_SCROLL_FORWARD:
1127 napi_create_string_utf8(env, "scrolltype", NAPI_AUTO_LENGTH, &propertyNameValue);
1128 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1129 if (hasProperty) {
1130 if (scrollValueMap.find(str) != scrollValueMap.end()) {
1131 scrollValue = scrollValueMap.find(str)->second;
1132 HILOG_DEBUG("ScrollValue %{public}s", scrollValue.c_str());
1133 } else {
1134 HILOG_DEBUG("Input is empty, output fullScreen, value is 1");
1135 }
1136 args.insert(std::pair<std::string, std::string>("scrolltype", scrollValue.c_str()));
1137 }
1138 break;
1139 case ActionType::ACCESSIBILITY_ACTION_SCROLL_BACKWARD:
1140 napi_create_string_utf8(env, "scrolltype", NAPI_AUTO_LENGTH, &propertyNameValue);
1141 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1142 if (hasProperty) {
1143 if (scrollValueMap.find(str) != scrollValueMap.end()) {
1144 scrollValue = scrollValueMap.find(str)->second;
1145 HILOG_DEBUG("ScrollValue %{public}s", scrollValue.c_str());
1146 } else {
1147 HILOG_DEBUG("Input is empty, output fullScreen, value is 1");
1148 }
1149 args.insert(std::pair<std::string, std::string>("scrolltype", scrollValue.c_str()));
1150 }
1151 break;
1152 default:
1153 break;
1154 }
1155 }
1156
ConvertIntJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty)1157 int32_t ConvertIntJSToNAPI(napi_env env, napi_value object, napi_value propertyNameValue, bool &hasProperty)
1158 {
1159 int32_t dataValue = 0;
1160 napi_has_property(env, object, propertyNameValue, &hasProperty);
1161 if (hasProperty) {
1162 napi_value itemValue = nullptr;
1163 napi_get_property(env, object, propertyNameValue, &itemValue);
1164 napi_get_value_int32(env, itemValue, &dataValue);
1165 }
1166 return dataValue;
1167 }
1168
ConvertBoolJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty)1169 bool ConvertBoolJSToNAPI(napi_env env, napi_value object, napi_value propertyNameValue, bool &hasProperty)
1170 {
1171 bool isBool = false;
1172 napi_has_property(env, object, propertyNameValue, &hasProperty);
1173 if (hasProperty) {
1174 napi_value itemValue = nullptr;
1175 napi_get_property(env, object, propertyNameValue, &itemValue);
1176 napi_get_value_bool(env, itemValue, &isBool);
1177 }
1178 return isBool;
1179 }
1180
ConvertStringJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty)1181 std::string ConvertStringJSToNAPI(napi_env env, napi_value object, napi_value propertyNameValue, bool &hasProperty)
1182 {
1183 std::string str = "";
1184 napi_has_property(env, object, propertyNameValue, &hasProperty);
1185 if (hasProperty) {
1186 napi_value itemValue = nullptr;
1187 napi_get_property(env, object, propertyNameValue, &itemValue);
1188 str = GetStringFromNAPI(env, itemValue);
1189 }
1190 return str;
1191 }
1192
ConvertStringArrayJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty,std::vector<std::string> & stringArray)1193 void ConvertStringArrayJSToNAPI(napi_env env, napi_value object,
1194 napi_value propertyNameValue, bool &hasProperty, std::vector<std::string> &stringArray)
1195 {
1196 napi_has_property(env, object, propertyNameValue, &hasProperty);
1197 if (hasProperty) {
1198 napi_value contentsValue = nullptr;
1199 napi_get_property(env, object, propertyNameValue, &contentsValue);
1200 napi_value data = nullptr;
1201 uint32_t dataLen = 0;
1202 napi_get_array_length(env, contentsValue, &dataLen);
1203 for (uint32_t i = 0; i < dataLen; i++) {
1204 napi_get_element(env, contentsValue, i, &data);
1205 std::string str = GetStringFromNAPI(env, data);
1206 stringArray.push_back(str);
1207 }
1208 }
1209 }
1210
ConvertStringArrayJSToNAPICommon(napi_env env,napi_value object,std::vector<std::string> & stringArray)1211 void ConvertStringArrayJSToNAPICommon(napi_env env, napi_value object, std::vector<std::string> &stringArray)
1212 {
1213 napi_value data = nullptr;
1214 uint32_t dataLen = 0;
1215 napi_get_array_length(env, object, &dataLen);
1216 for (uint32_t i = 0; i < dataLen; i++) {
1217 napi_get_element(env, object, i, &data);
1218 std::string str = GetStringFromNAPI(env, data);
1219 stringArray.push_back(str);
1220 }
1221 }
1222
ConvertSpanToJS(napi_env env,napi_value result,const Accessibility::SpanInfo & span)1223 void ConvertSpanToJS(napi_env env, napi_value result, const Accessibility::SpanInfo &span)
1224 {
1225 napi_value spanId;
1226 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, span.GetSpanId(), &spanId));
1227 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "spanId", spanId));
1228
1229 napi_value spanText;
1230 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, span.GetSpanText().c_str(), NAPI_AUTO_LENGTH, &spanText));
1231 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "spanText", spanText));
1232
1233 napi_value accessibilityText;
1234 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, span.GetAccessibilityText().c_str(),
1235 NAPI_AUTO_LENGTH, &accessibilityText));
1236 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "accessibilityText", accessibilityText));
1237
1238 napi_value accessibilityDescription;
1239 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, span.GetAccessibilityDescription().c_str(),
1240 NAPI_AUTO_LENGTH, &accessibilityDescription));
1241 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "accessibilityDescription",
1242 accessibilityDescription));
1243
1244 napi_value accessibilityLevel;
1245 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, span.GetAccessibilityLevel().c_str(),
1246 NAPI_AUTO_LENGTH, &accessibilityLevel));
1247 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "accessibilityLevel", accessibilityLevel));
1248 }
1249
ConvertEventInfoJSToNAPI(napi_env env,napi_value object,OHOS::Accessibility::AccessibilityEventInfo & eventInfo)1250 bool ConvertEventInfoJSToNAPI(
1251 napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo)
1252 {
1253 HILOG_DEBUG();
1254 bool tmpResult = ConvertEventInfoJSToNAPIPart1(env, object, eventInfo);
1255 if (!tmpResult) {
1256 return false;
1257 }
1258 tmpResult = ConvertEventInfoJSToNAPIPart2(env, object, eventInfo);
1259 if (!tmpResult) {
1260 return false;
1261 }
1262 tmpResult = ConvertEventInfoJSToNAPIPart3(env, object, eventInfo);
1263 if (!tmpResult) {
1264 return false;
1265 }
1266 tmpResult = ConvertEventInfoJSToNAPIPart4(env, object, eventInfo);
1267 if (!tmpResult) {
1268 return false;
1269 }
1270 return true;
1271 }
1272
ConvertEventInfoJSToNAPIPart1(napi_env env,napi_value object,OHOS::Accessibility::AccessibilityEventInfo & eventInfo)1273 bool ConvertEventInfoJSToNAPIPart1(
1274 napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo)
1275 {
1276 bool hasProperty = false;
1277 std::string str = "";
1278 napi_value propertyNameValue = nullptr;
1279 napi_create_string_utf8(env, "type", NAPI_AUTO_LENGTH, &propertyNameValue);
1280 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1281 if (hasProperty) {
1282 EventType eventType = ConvertStringToEventInfoTypes(str);
1283 eventInfo.SetEventType(eventType);
1284 if (eventType == TYPE_VIEW_INVALID) {
1285 return false;
1286 }
1287 } else {
1288 return false;
1289 }
1290
1291 napi_create_string_utf8(env, "windowUpdateType", NAPI_AUTO_LENGTH, &propertyNameValue);
1292 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1293 if (hasProperty) {
1294 eventInfo.SetEventType(TYPE_WINDOW_UPDATE);
1295 eventInfo.SetWindowChangeTypes(ConvertStringToWindowUpdateTypes(str));
1296 }
1297
1298 napi_create_string_utf8(env, "bundleName", NAPI_AUTO_LENGTH, &propertyNameValue);
1299 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1300 if (hasProperty) {
1301 if (str != "") {
1302 eventInfo.SetBundleName(str);
1303 } else {
1304 return false;
1305 }
1306 } else {
1307 return false;
1308 }
1309 return true;
1310 }
1311
ConvertEventInfoJSToNAPIPart2(napi_env env,napi_value object,OHOS::Accessibility::AccessibilityEventInfo & eventInfo)1312 bool ConvertEventInfoJSToNAPIPart2(
1313 napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo)
1314 {
1315 bool hasProperty = false;
1316 int32_t dataValue = 0;
1317 std::string str = "";
1318 napi_value propertyNameValue = nullptr;
1319 napi_create_string_utf8(env, "componentType", NAPI_AUTO_LENGTH, &propertyNameValue);
1320 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1321 if (hasProperty) {
1322 eventInfo.SetComponentType(str);
1323 }
1324
1325 napi_create_string_utf8(env, "pageId", NAPI_AUTO_LENGTH, &propertyNameValue);
1326 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1327 if (hasProperty) {
1328 eventInfo.SetPageId(dataValue);
1329 }
1330
1331 napi_create_string_utf8(env, "description", NAPI_AUTO_LENGTH, &propertyNameValue);
1332 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1333 if (hasProperty) {
1334 eventInfo.SetDescription(str);
1335 }
1336
1337 napi_create_string_utf8(env, "triggerAction", NAPI_AUTO_LENGTH, &propertyNameValue);
1338 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1339 if (hasProperty) {
1340 eventInfo.SetTriggerAction(ConvertStringToAccessibleOperationType(str));
1341 if (eventInfo.GetTriggerAction() == ACCESSIBILITY_ACTION_INVALID) {
1342 return false;
1343 }
1344 } else {
1345 return false;
1346 }
1347
1348 napi_create_string_utf8(env, "textMoveUnit", NAPI_AUTO_LENGTH, &propertyNameValue);
1349 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1350 if (hasProperty) {
1351 eventInfo.SetTextMovementStep(ConvertStringToTextMoveUnit(str));
1352 }
1353
1354 napi_create_string_utf8(env, "elementId", NAPI_AUTO_LENGTH, &propertyNameValue);
1355 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1356 if (hasProperty) {
1357 eventInfo.SetRequestFocusElementId(dataValue);
1358 }
1359 return true;
1360 }
1361
ConvertEventInfoJSToNAPIPart3(napi_env env,napi_value object,OHOS::Accessibility::AccessibilityEventInfo & eventInfo)1362 bool ConvertEventInfoJSToNAPIPart3(
1363 napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo)
1364 {
1365 bool hasProperty = false;
1366 int32_t dataValue = 0;
1367 napi_value propertyNameValue = nullptr;
1368 napi_create_string_utf8(env, "contents", NAPI_AUTO_LENGTH, &propertyNameValue);
1369 std::vector<std::string> stringArray {};
1370 ConvertStringArrayJSToNAPI(env, object, propertyNameValue, hasProperty, stringArray);
1371 if (hasProperty) {
1372 for (auto str : stringArray) {
1373 eventInfo.AddContent(str);
1374 }
1375 }
1376
1377 napi_create_string_utf8(env, "lastContent", NAPI_AUTO_LENGTH, &propertyNameValue);
1378 std::string strNapi = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1379 if (hasProperty) {
1380 eventInfo.SetLatestContent(strNapi);
1381 }
1382
1383 napi_create_string_utf8(env, "beginIndex", NAPI_AUTO_LENGTH, &propertyNameValue);
1384 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1385 if (hasProperty) {
1386 eventInfo.SetBeginIndex(dataValue);
1387 }
1388
1389 napi_create_string_utf8(env, "currentIndex", NAPI_AUTO_LENGTH, &propertyNameValue);
1390 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1391 if (hasProperty) {
1392 eventInfo.SetCurrentIndex(dataValue);
1393 }
1394
1395 napi_create_string_utf8(env, "endIndex", NAPI_AUTO_LENGTH, &propertyNameValue);
1396 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1397 if (hasProperty) {
1398 eventInfo.SetEndIndex(dataValue);
1399 }
1400
1401 napi_create_string_utf8(env, "itemCount", NAPI_AUTO_LENGTH, &propertyNameValue);
1402 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1403 if (hasProperty) {
1404 eventInfo.SetItemCounts(dataValue);
1405 }
1406
1407 napi_create_string_utf8(env, "customId", NAPI_AUTO_LENGTH, &propertyNameValue);
1408 std::string inspectorKey = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1409 if (hasProperty) {
1410 eventInfo.SetInspectorKey(inspectorKey);
1411 }
1412
1413 napi_create_string_utf8(env, "textAnnouncedForAccessibility", NAPI_AUTO_LENGTH, &propertyNameValue);
1414 std::string announceText = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1415 if (hasProperty) {
1416 eventInfo.SetTextAnnouncedForAccessibility(announceText);
1417 }
1418 return true;
1419 }
1420
ConvertEventInfoJSToNAPIPart4(napi_env env,napi_value object,OHOS::Accessibility::AccessibilityEventInfo & eventInfo)1421 bool ConvertEventInfoJSToNAPIPart4(
1422 napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo)
1423 {
1424 bool hasProperty = false;
1425 napi_value propertyNameValue = nullptr;
1426 Accessibility::ResourceInfo resourceInfo;
1427 napi_create_string_utf8(env, "textResourceAnnouncedForAccessibility", NAPI_AUTO_LENGTH, &propertyNameValue);
1428 ConvertResourceJSToNAPI(env, object, propertyNameValue, hasProperty, resourceInfo);
1429 if (hasProperty) {
1430 eventInfo.SetResourceBundleName(resourceInfo.bundleName);
1431 eventInfo.SetResourceModuleName(resourceInfo.moduleName);
1432 eventInfo.SetResourceId(resourceInfo.resourceId);
1433 eventInfo.SetResourceParams(resourceInfo.params);
1434 }
1435 return true;
1436 }
1437
ConvertGesturePointJSToNAPI(napi_env env,napi_value object,AccessibilityGesturePosition & gesturePathPosition)1438 static bool ConvertGesturePointJSToNAPI(
1439 napi_env env, napi_value object, AccessibilityGesturePosition& gesturePathPosition)
1440 {
1441 HILOG_DEBUG();
1442 napi_value propertyNameValue = nullptr;
1443 bool hasProperty = false;
1444 double position = 0;
1445
1446 napi_create_string_utf8(env, "positionX", NAPI_AUTO_LENGTH, &propertyNameValue);
1447 napi_has_property(env, object, propertyNameValue, &hasProperty);
1448 if (hasProperty) {
1449 napi_value valueX = nullptr;
1450 napi_get_property(env, object, propertyNameValue, &valueX);
1451 napi_get_value_double(env, valueX, &position);
1452 gesturePathPosition.positionX_ = static_cast<float>(position);
1453 } else {
1454 return false;
1455 }
1456
1457 napi_create_string_utf8(env, "positionY", NAPI_AUTO_LENGTH, &propertyNameValue);
1458 napi_has_property(env, object, propertyNameValue, &hasProperty);
1459 if (hasProperty) {
1460 napi_value valueY = nullptr;
1461 napi_get_property(env, object, propertyNameValue, &valueY);
1462 napi_get_value_double(env, valueY, &position);
1463 gesturePathPosition.positionY_ = static_cast<float>(position);
1464 } else {
1465 return false;
1466 }
1467 return true;
1468 }
1469
ConvertGesturePathJSToNAPI(napi_env env,napi_value object,std::shared_ptr<AccessibilityGestureInjectPath> & gesturePath)1470 bool ConvertGesturePathJSToNAPI(napi_env env, napi_value object,
1471 std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath)
1472 {
1473 HILOG_DEBUG();
1474 if (!gesturePath) {
1475 HILOG_ERROR("gesturePath is null.");
1476 return false;
1477 }
1478
1479 bool tmpResult = ConvertGesturePathJSToNAPIPart1(env, object, gesturePath);
1480 if (!tmpResult) {
1481 return false;
1482 }
1483 tmpResult = ConvertGesturePathJSToNAPIPart2(env, object, gesturePath);
1484 if (!tmpResult) {
1485 return false;
1486 }
1487 return true;
1488 }
1489
ConvertGesturePathJSToNAPIPart1(napi_env env,napi_value object,std::shared_ptr<AccessibilityGestureInjectPath> & gesturePath)1490 bool ConvertGesturePathJSToNAPIPart1(napi_env env, napi_value object,
1491 std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath)
1492 {
1493 napi_value propertyNameValue = nullptr;
1494 bool hasProperty = false;
1495
1496 napi_create_string_utf8(env, "points", NAPI_AUTO_LENGTH, &propertyNameValue);
1497 napi_has_property(env, object, propertyNameValue, &hasProperty);
1498 if (hasProperty) {
1499 napi_value positionValue = nullptr;
1500 napi_get_property(env, object, propertyNameValue, &positionValue);
1501 napi_value jsValue = nullptr;
1502 bool isArray = false;
1503 uint32_t dataLen = 0;
1504 if (napi_is_array(env, positionValue, &isArray) != napi_ok || isArray == false) {
1505 HILOG_ERROR("object is not an array.");
1506 return false;
1507 }
1508 if (napi_get_array_length(env, positionValue, &dataLen) != napi_ok) {
1509 HILOG_ERROR("get array length failed.");
1510 return false;
1511 }
1512 for (uint32_t i = 0; i < dataLen; i++) {
1513 jsValue = nullptr;
1514 AccessibilityGesturePosition path;
1515 if (napi_get_element(env, positionValue, i, &jsValue) != napi_ok) {
1516 HILOG_ERROR("get element of paths failed and i = %{public}d", i);
1517 return false;
1518 }
1519 bool result = ConvertGesturePointJSToNAPI(env, jsValue, path);
1520 if (result) {
1521 gesturePath->AddPosition(path);
1522 } else {
1523 HILOG_ERROR("Parse gesture point error.");
1524 return false;
1525 }
1526 }
1527 } else {
1528 HILOG_ERROR("No points property.");
1529 return false;
1530 }
1531 return true;
1532 }
1533
ConvertGesturePathJSToNAPIPart2(napi_env env,napi_value object,std::shared_ptr<AccessibilityGestureInjectPath> & gesturePath)1534 bool ConvertGesturePathJSToNAPIPart2(napi_env env, napi_value object,
1535 std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath)
1536 {
1537 napi_value propertyNameValue = nullptr;
1538 bool hasProperty = false;
1539
1540 napi_create_string_utf8(env, "durationTime", NAPI_AUTO_LENGTH, &propertyNameValue);
1541 int64_t durationTime = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1542 napi_has_property(env, object, propertyNameValue, &hasProperty);
1543 if (hasProperty) {
1544 gesturePath->SetDurationTime(durationTime);
1545 return true;
1546 }
1547 return false;
1548 }
1549
TransformKeyActionValue(int32_t keyAction)1550 KeyAction TransformKeyActionValue(int32_t keyAction)
1551 {
1552 HILOG_DEBUG("keyAction:%{public}d", keyAction);
1553
1554 KeyAction action = KeyAction::UNKNOWN;
1555 if (keyAction == OHOS::MMI::KeyEvent::KEY_ACTION_DOWN) {
1556 action = KeyAction::DOWN;
1557 } else if (keyAction == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
1558 action = KeyAction::UP;
1559 } else if (keyAction == OHOS::MMI::KeyEvent::KEY_ACTION_CANCEL) {
1560 action = KeyAction::CANCEL;
1561 } else {
1562 HILOG_DEBUG("key action is invalid");
1563 }
1564 return action;
1565 }
1566
HasKeyCode(const std::vector<int32_t> & pressedKeys,int32_t keyCode)1567 bool HasKeyCode(const std::vector<int32_t>& pressedKeys, int32_t keyCode)
1568 {
1569 HILOG_DEBUG();
1570
1571 return std::find(pressedKeys.begin(), pressedKeys.end(), keyCode) != pressedKeys.end();
1572 }
1573
GetKeyValue(napi_env env,napi_value keyObject,std::optional<MMI::KeyEvent::KeyItem> keyItem)1574 void GetKeyValue(napi_env env, napi_value keyObject, std::optional<MMI::KeyEvent::KeyItem> keyItem)
1575 {
1576 HILOG_DEBUG();
1577
1578 if (!keyItem) {
1579 HILOG_WARN("keyItem is null.");
1580 return;
1581 }
1582
1583 napi_value keyCodeValue = nullptr;
1584 int32_t keyCode = keyItem->GetKeyCode();
1585 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, keyCode, &keyCodeValue));
1586 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, keyObject, "code", keyCodeValue));
1587
1588 napi_value timeValue = nullptr;
1589 int64_t pressedTime = keyItem->GetDownTime();
1590 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, pressedTime, &timeValue));
1591 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, keyObject, "pressedTime", timeValue));
1592
1593 napi_value deviceIdValue = nullptr;
1594 int32_t deviceId = keyItem->GetDeviceId();
1595 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, deviceId, &deviceIdValue));
1596 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, keyObject, "deviceId", deviceIdValue));
1597 }
1598
SetInputEventProperty(napi_env env,napi_value result,const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent)1599 void SetInputEventProperty(napi_env env, napi_value result, const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent)
1600 {
1601 HILOG_DEBUG();
1602
1603 if (!keyEvent) {
1604 HILOG_ERROR("keyEvent is null.");
1605 return;
1606 }
1607 // set id
1608 napi_value idValue = nullptr;
1609 int32_t id = keyEvent->GetId();
1610 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, id, &idValue));
1611 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "id", idValue));
1612
1613 // set deviceId
1614 napi_value deviceIdValue = nullptr;
1615 int32_t deviceId = keyEvent->GetDeviceId();
1616 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, deviceId, &deviceIdValue));
1617 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "deviceId", deviceIdValue));
1618
1619 // set actionTime
1620 napi_value actionTimeValue = nullptr;
1621 int64_t actionTime = keyEvent->GetActionTime();
1622 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, actionTime, &actionTimeValue));
1623 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "actionTime", actionTimeValue));
1624
1625 // set screenId
1626 napi_value screenIdValue = nullptr;
1627 int32_t screenId = keyEvent->GetTargetDisplayId();
1628 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, screenId, &screenIdValue));
1629 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "screenId", screenIdValue));
1630
1631 // set windowId
1632 napi_value windowIdValue = nullptr;
1633 int32_t windowId = keyEvent->GetTargetWindowId();
1634 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, windowId, &windowIdValue));
1635 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "windowId", windowIdValue));
1636 }
1637
ConvertKeyEventToJS(napi_env env,napi_value result,const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent)1638 void ConvertKeyEventToJS(napi_env env, napi_value result, const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent)
1639 {
1640 HILOG_DEBUG();
1641
1642 if (!keyEvent) {
1643 HILOG_ERROR("keyEvent is null.");
1644 return;
1645 }
1646
1647 // set inputEvent
1648 SetInputEventProperty(env, result, keyEvent);
1649
1650 // set action
1651 napi_value keyActionValue = nullptr;
1652 KeyAction keyAction = TransformKeyActionValue(keyEvent->GetKeyAction());
1653 if (keyAction != KeyAction::UNKNOWN) {
1654 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, keyAction, &keyActionValue));
1655 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "action", keyActionValue));
1656 }
1657
1658 // set key
1659 napi_value keyObject = nullptr;
1660 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &keyObject));
1661 std::optional<MMI::KeyEvent::KeyItem> keyItem = keyEvent->GetKeyItem();
1662 GetKeyValue(env, keyObject, keyItem);
1663 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "key", keyObject));
1664
1665 // set unicodeChar
1666 napi_value unicodeCharValue = nullptr;
1667 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, 0, &unicodeCharValue));
1668 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "unicodeChar", unicodeCharValue));
1669
1670 // set keys
1671 SetKeyPropertyPart1(env, result, keyEvent);
1672 SetKeyPropertyPart2(env, result, keyEvent);
1673 }
1674
ConvertResourceJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty,Accessibility::ResourceInfo & resourceInfo)1675 void ConvertResourceJSToNAPI(napi_env env, napi_value object, napi_value propertyNameValue, bool &hasProperty,
1676 Accessibility::ResourceInfo& resourceInfo)
1677 {
1678 napi_has_property(env, object, propertyNameValue, &hasProperty);
1679 if (hasProperty) {
1680 napi_value itemValue = nullptr;
1681 napi_get_property(env, object, propertyNameValue, &itemValue);
1682 ParseResourceIdFromNAPI(env, itemValue, resourceInfo.resourceId);
1683 ParseResourceBundleNameFromNAPI(env, itemValue, resourceInfo.bundleName);
1684 ParseResourceModuleNameFromNAPI(env, itemValue, resourceInfo.moduleName);
1685 ParseResourceParamsFromNAPI(env, itemValue, resourceInfo.params);
1686 }
1687 HILOG_DEBUG("resourceId is %{public}d, bundleName is %{public}s, moduleName is %{public}s",
1688 resourceInfo.resourceId, resourceInfo.bundleName.c_str(), resourceInfo.moduleName.c_str());
1689 }
1690
SetKeyPropertyPart1(napi_env env,napi_value result,const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent)1691 void SetKeyPropertyPart1(napi_env env, napi_value result, const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent)
1692 {
1693 HILOG_DEBUG();
1694 if (!keyEvent) {
1695 HILOG_ERROR("keyEvent is nullptr.");
1696 return;
1697 }
1698 // set keys
1699 napi_value keysAarry = nullptr;
1700 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &keysAarry));
1701 uint32_t index = 0;
1702 std::vector<int32_t> pressedKeys = keyEvent->GetPressedKeys();
1703 for (const auto &pressedKeyCode : pressedKeys) {
1704 napi_value element = nullptr;
1705 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &element));
1706 std::optional<MMI::KeyEvent::KeyItem> pressedKeyItem = keyEvent->GetKeyItem(pressedKeyCode);
1707 GetKeyValue(env, element, pressedKeyItem);
1708 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, keysAarry, index, element));
1709 ++index;
1710 }
1711 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "keys", keysAarry));
1712
1713 // set ctrlKey
1714 bool isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_CTRL_LEFT)
1715 || HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_CTRL_RIGHT);
1716 napi_value ctrlKeyValue = nullptr;
1717 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &ctrlKeyValue));
1718 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "ctrlKey", ctrlKeyValue));
1719
1720 // set altKey
1721 isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_ALT_LEFT)
1722 || HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_ALT_RIGHT);
1723 napi_value altKeyValue = nullptr;
1724 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &altKeyValue));
1725 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "altKey", altKeyValue));
1726
1727 // set shiftKey
1728 isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_SHIFT_LEFT)
1729 || HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_SHIFT_RIGHT);
1730 napi_value shiftKeyValue = nullptr;
1731 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &shiftKeyValue));
1732 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "shiftKey", shiftKeyValue));
1733
1734 // set logoKey
1735 isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_META_LEFT)
1736 || HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_META_RIGHT);
1737 napi_value logoKeyValue = nullptr;
1738 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &logoKeyValue));
1739 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "logoKey", logoKeyValue));
1740
1741 // set fnKey
1742 isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_FN);
1743 napi_value fnKeyValue = nullptr;
1744 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &fnKeyValue));
1745 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fnKey", fnKeyValue));
1746 }
1747
SetKeyPropertyPart2(napi_env env,napi_value result,const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent)1748 void SetKeyPropertyPart2(napi_env env, napi_value result, const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent)
1749 {
1750 HILOG_DEBUG();
1751 // set capsLock
1752 napi_value capsLockValue = nullptr;
1753 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, false, &capsLockValue));
1754 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "capsLock", capsLockValue));
1755
1756 // set numLock
1757 napi_value numLockValue = nullptr;
1758 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, false, &numLockValue));
1759 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "numLock", numLockValue));
1760
1761 // set scrollLock
1762 napi_value scrollLockValue = nullptr;
1763 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, false, &scrollLockValue));
1764 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "scrollLock", scrollLockValue));
1765 }
1766
ConvertCaptionPropertyToJS(napi_env env,napi_value & result,OHOS::AccessibilityConfig::CaptionProperty captionProperty)1767 void ConvertCaptionPropertyToJS(
1768 napi_env env, napi_value& result, OHOS::AccessibilityConfig::CaptionProperty captionProperty)
1769 {
1770 HILOG_DEBUG();
1771
1772 napi_value value = nullptr;
1773
1774 NAPI_CALL_RETURN_VOID(env,
1775 napi_create_string_utf8(env, captionProperty.GetFontFamily().c_str(), NAPI_AUTO_LENGTH, &value));
1776 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fontFamily", value));
1777
1778 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, captionProperty.GetFontScale(), &value));
1779 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fontScale", value));
1780
1781 uint32_t color = captionProperty.GetFontColor();
1782 std::string colorStr = ConvertColorToString(color);
1783 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, colorStr.c_str(), NAPI_AUTO_LENGTH, &value));
1784 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fontColor", value));
1785
1786 NAPI_CALL_RETURN_VOID(env,
1787 napi_create_string_utf8(env, captionProperty.GetFontEdgeType().c_str(), NAPI_AUTO_LENGTH, &value));
1788 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fontEdgeType", value));
1789
1790 color = captionProperty.GetBackgroundColor();
1791 colorStr = ConvertColorToString(color);
1792 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, colorStr.c_str(), NAPI_AUTO_LENGTH, &value));
1793 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "backgroundColor", value));
1794
1795 color = captionProperty.GetWindowColor();
1796 colorStr = ConvertColorToString(color);
1797 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, colorStr.c_str(), NAPI_AUTO_LENGTH, &value));
1798 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "windowColor", value));
1799 }
1800
ConvertColorStringToNumer(std::string colorStr)1801 uint32_t ConvertColorStringToNumer(std::string colorStr)
1802 {
1803 HILOG_DEBUG("colorStr is %{public}s", colorStr.c_str());
1804 uint32_t color = COLOR_TRANSPARENT;
1805 if (colorStr.empty()) {
1806 // Empty string, return transparent
1807 return color;
1808 }
1809 // Remove all " ".
1810 colorStr.erase(std::remove(colorStr.begin(), colorStr.end(), ' '), colorStr.end());
1811
1812 if (ColorRegexMatch(colorStr, color)) {
1813 return color;
1814 }
1815
1816 // Match for special string
1817 static const std::map<std::string, uint32_t> colorTable {
1818 std::make_pair("black", COLOR_BLACK),
1819 std::make_pair("blue", COLOR_BLUE),
1820 std::make_pair("gray", COLOR_GRAY),
1821 std::make_pair("green", COLOR_GREEN),
1822 std::make_pair("red", COLOR_RED),
1823 std::make_pair("white", COLOR_WHITE),
1824 };
1825 auto it = colorTable.find(colorStr.c_str());
1826 if (it != colorTable.end()) {
1827 color = it->second;
1828 }
1829 return color;
1830 }
1831
IsColorWithMagic(const std::string & colorStr)1832 bool IsColorWithMagic(const std::string& colorStr)
1833 {
1834 if (colorStr.size() < 1 || colorStr.substr(0, 1) != "#") {
1835 return false;
1836 }
1837
1838 for (size_t i = 1; i < colorStr.size(); i++) {
1839 if (NUMBER_VALID_CHARS.find(colorStr[i]) == std::string::npos) {
1840 return false;
1841 }
1842 }
1843
1844 return true;
1845 }
1846
ColorRegexMatch(std::string colorStr,uint32_t & color)1847 bool ColorRegexMatch(std::string colorStr, uint32_t &color)
1848 {
1849 // for example #909090 or #90909090. avoid use regex match #[0-9A-Fa-f]{6,8}.
1850 if (IsColorWithMagic(colorStr) &&
1851 (colorStr.size() == COLOR_STRING_SIZE_7 || colorStr.size() == COLOR_STRING_SIZE_9)) {
1852 colorStr.erase(0, 1);
1853 auto colorValue = stoul(colorStr, nullptr, COLOR_STRING_BASE);
1854 if (colorStr.length() < COLOR_STRING_SIZE_STANDARD) {
1855 // No alpha specified, set alpha to 0xff
1856 colorValue |= COLOR_ALPHA_MASK;
1857 } else {
1858 auto alpha = colorValue << ALPHA_MOVE;
1859 auto rgb = colorValue >> COLOR_MOVE;
1860 colorValue = alpha | rgb;
1861 }
1862 color = colorValue;
1863 return true;
1864 }
1865 // for #rgb or #rgba. avoid use regex match #[0-9A-Fa-f]{3,4}.
1866 if (IsColorWithMagic(colorStr) &&
1867 (colorStr.size() == COLOR_STRING_SIZE_4 || colorStr.size() == COLOR_STRING_SIZE_5)) {
1868 colorStr.erase(0, 1);
1869 std::string newColorStr;
1870 // Translate #rgb or #rgba to #rrggbb or #rrggbbaa
1871 for (const auto& c : colorStr) {
1872 newColorStr += c;
1873 newColorStr += c;
1874 }
1875 auto valueMini = stoul(newColorStr, nullptr, COLOR_STRING_BASE);
1876 if (newColorStr.length() < COLOR_STRING_SIZE_STANDARD) {
1877 // No alpha specified, set alpha to 0xff
1878 valueMini |= COLOR_ALPHA_MASK;
1879 } else {
1880 auto alphaMini = valueMini << ALPHA_MOVE;
1881 auto rgbMini = valueMini >> COLOR_MOVE;
1882 valueMini = alphaMini | rgbMini;
1883 }
1884 color = valueMini;
1885 return true;
1886 }
1887 return false;
1888 }
1889
ConvertColorToString(uint32_t color)1890 std::string ConvertColorToString(uint32_t color)
1891 {
1892 HILOG_DEBUG("color is 0X%{public}x", color);
1893 uint32_t rgb = color & (~COLOR_ALPHA_MASK);
1894 uint32_t alpha = (color) >> ALPHA_MOVE;
1895 std::stringstream rgbStream;
1896 rgbStream << std::hex << std::setw(RGB_LENGTH) << std::setfill(UNICODE_BODY) << rgb;
1897 std::stringstream alphaStream;
1898 alphaStream << std::hex << std::setw(ALPHA_LENGTH) << std::setfill(UNICODE_BODY) << alpha;
1899 std::string rgbStr(rgbStream.str());
1900 std::string alphaStr(alphaStream.str());
1901 std::string colorStr = "#" + rgbStr + alphaStr;
1902 HILOG_DEBUG("colorStr is %{public}s", colorStr.c_str());
1903 return colorStr;
1904 }
1905
GetColorValue(napi_env env,napi_value object,napi_value propertyNameValue)1906 uint32_t GetColorValue(napi_env env, napi_value object, napi_value propertyNameValue)
1907 {
1908 uint32_t color = COLOR_TRANSPARENT;
1909 napi_valuetype valueType = napi_undefined;
1910 napi_value value = nullptr;
1911 napi_get_property(env, object, propertyNameValue, &value);
1912 napi_status status = napi_typeof(env, value, &valueType);
1913 if (status != napi_ok) {
1914 HILOG_ERROR("GetColorValue error! status is %{public}d", status);
1915 return color;
1916 }
1917 if (valueType == napi_number) {
1918 napi_get_value_uint32(env, value, &color);
1919 HILOG_DEBUG("valueType number, color is 0x%{public}x", color);
1920 }
1921 if (valueType == napi_string) {
1922 char outBuffer[CHAE_BUFFER_MAX + 1] = {0};
1923 size_t outSize = 0;
1924 napi_get_value_string_utf8(env, value, outBuffer, CHAE_BUFFER_MAX, &outSize);
1925 color = ConvertColorStringToNumer(std::string(outBuffer));
1926 }
1927 HILOG_DEBUG("color is 0x%{public}x", color);
1928 return color;
1929 }
1930
GetColorValue(napi_env env,napi_value value)1931 uint32_t GetColorValue(napi_env env, napi_value value)
1932 {
1933 uint32_t color = COLOR_TRANSPARENT;
1934 napi_valuetype valueType = napi_undefined;
1935 napi_status status = napi_typeof(env, value, &valueType);
1936 if (status != napi_ok) {
1937 HILOG_ERROR("GetColorValue error! status is %{public}d", status);
1938 return color;
1939 }
1940 if (valueType == napi_number) {
1941 HILOG_DEBUG("color type is number");
1942 napi_get_value_uint32(env, value, &color);
1943 }
1944 if (valueType == napi_string) {
1945 char outBuffer[CHAE_BUFFER_MAX + 1] = {0};
1946 size_t outSize = 0;
1947 napi_get_value_string_utf8(env, value, outBuffer, CHAE_BUFFER_MAX, &outSize);
1948 color = ConvertColorStringToNumer(std::string(outBuffer));
1949 }
1950 HILOG_DEBUG("color is 0x%{public}x", color);
1951 return color;
1952 }
1953
ConvertObjToCaptionProperty(napi_env env,napi_value object,OHOS::AccessibilityConfig::CaptionProperty * ptrCaptionProperty)1954 bool ConvertObjToCaptionProperty(
1955 napi_env env, napi_value object, OHOS::AccessibilityConfig::CaptionProperty* ptrCaptionProperty)
1956 {
1957 if (!ptrCaptionProperty) {
1958 HILOG_ERROR("ptrCaptionProperty is null.");
1959 return false;
1960 }
1961
1962 bool tmpResult = ConvertObjToCaptionPropertyPart1(env, object, ptrCaptionProperty);
1963 if (!tmpResult) {
1964 return false;
1965 }
1966 tmpResult = ConvertObjToCaptionPropertyPart2(env, object, ptrCaptionProperty);
1967 if (!tmpResult) {
1968 return false;
1969 }
1970 return true;
1971 }
1972
ConvertObjToCaptionPropertyPart1(napi_env env,napi_value object,OHOS::AccessibilityConfig::CaptionProperty * ptrCaptionProperty)1973 bool ConvertObjToCaptionPropertyPart1(
1974 napi_env env, napi_value object, OHOS::AccessibilityConfig::CaptionProperty* ptrCaptionProperty)
1975 {
1976 napi_value propertyNameValue = nullptr;
1977 bool hasProperty = false;
1978 int32_t num = 100;
1979
1980 napi_create_string_utf8(env, "fontFamily", NAPI_AUTO_LENGTH, &propertyNameValue);
1981 std::string fontFamily = ConvertCaptionPropertyJSToNAPI(env, object, propertyNameValue, hasProperty);
1982 if (hasProperty) {
1983 ptrCaptionProperty->SetFontFamily(fontFamily);
1984 } else {
1985 return false;
1986 }
1987
1988 napi_create_string_utf8(env, "fontScale", NAPI_AUTO_LENGTH, &propertyNameValue);
1989 num = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1990 if (hasProperty) {
1991 ptrCaptionProperty->SetFontScale(num);
1992 } else {
1993 return false;
1994 }
1995
1996 napi_create_string_utf8(env, "fontColor", NAPI_AUTO_LENGTH, &propertyNameValue);
1997 napi_has_property(env, object, propertyNameValue, &hasProperty);
1998 if (hasProperty) {
1999 ptrCaptionProperty->SetFontColor(GetColorValue(env, object, propertyNameValue));
2000 } else {
2001 return false;
2002 }
2003 return true;
2004 }
2005
ConvertObjToCaptionPropertyPart2(napi_env env,napi_value object,OHOS::AccessibilityConfig::CaptionProperty * ptrCaptionProperty)2006 bool ConvertObjToCaptionPropertyPart2(
2007 napi_env env, napi_value object, OHOS::AccessibilityConfig::CaptionProperty* ptrCaptionProperty)
2008 {
2009 napi_value propertyNameValue = nullptr;
2010 bool hasProperty = false;
2011
2012 napi_create_string_utf8(env, "fontEdgeType", NAPI_AUTO_LENGTH, &propertyNameValue);
2013 std::string fontEdgeType = ConvertCaptionPropertyJSToNAPI(env, object, propertyNameValue, hasProperty);
2014 if (hasProperty) {
2015 ptrCaptionProperty->SetFontEdgeType(fontEdgeType);
2016 } else {
2017 return false;
2018 }
2019
2020 napi_create_string_utf8(env, "backgroundColor", NAPI_AUTO_LENGTH, &propertyNameValue);
2021 napi_has_property(env, object, propertyNameValue, &hasProperty);
2022 if (hasProperty) {
2023 ptrCaptionProperty->SetBackgroundColor(GetColorValue(env, object, propertyNameValue));
2024 } else {
2025 return false;
2026 }
2027
2028 napi_create_string_utf8(env, "windowColor", NAPI_AUTO_LENGTH, &propertyNameValue);
2029 napi_has_property(env, object, propertyNameValue, &hasProperty);
2030 if (hasProperty) {
2031 ptrCaptionProperty->SetWindowColor(GetColorValue(env, object, propertyNameValue));
2032 } else {
2033 return false;
2034 }
2035 return true;
2036 }
2037
ConvertCaptionPropertyJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty)2038 std::string ConvertCaptionPropertyJSToNAPI(napi_env env, napi_value object,
2039 napi_value propertyNameValue, bool &hasProperty)
2040 {
2041 char outBuffer[CHAE_BUFFER_MAX + 1] = {0};
2042 napi_has_property(env, object, propertyNameValue, &hasProperty);
2043 if (hasProperty) {
2044 napi_value value = nullptr;
2045 size_t outSize = 0;
2046 napi_get_property(env, object, propertyNameValue, &value);
2047 napi_get_value_string_utf8(env, value, outBuffer, CHAE_BUFFER_MAX, &outSize);
2048 }
2049 return std::string(outBuffer);
2050 }
2051
ConvertJSToStringVec(napi_env env,napi_value arrayValue,std::vector<std::string> & values)2052 bool ConvertJSToStringVec(napi_env env, napi_value arrayValue, std::vector<std::string>& values)
2053 {
2054 HILOG_DEBUG();
2055 values.clear();
2056
2057 bool hasElement = true;
2058 for (int32_t i = 0; hasElement; i++) {
2059 napi_has_element(env, arrayValue, i, &hasElement);
2060 if (hasElement) {
2061 napi_value value = nullptr;
2062 napi_status status = napi_get_element(env, arrayValue, i, &value);
2063 if (status != napi_ok) {
2064 return false;
2065 }
2066
2067 char outBuffer[CHAE_BUFFER_MAX + 1] = {0};
2068 size_t outSize = 0;
2069 status = napi_get_value_string_utf8(env, value, outBuffer, CHAE_BUFFER_MAX, &outSize);
2070 if (status != napi_ok) {
2071 return false;
2072 }
2073
2074 values.push_back(std::string(outBuffer));
2075 }
2076 }
2077 return true;
2078 }
2079
ConvertJSToEventTypes(napi_env env,napi_value arrayValue,uint32_t & eventTypes)2080 void ConvertJSToEventTypes(napi_env env, napi_value arrayValue, uint32_t &eventTypes)
2081 {
2082 HILOG_DEBUG();
2083 eventTypes = TYPE_VIEW_INVALID;
2084 std::vector<std::string> values;
2085 ConvertJSToStringVec(env, arrayValue, values);
2086 for (auto &value : values) {
2087 HILOG_DEBUG("the event type is %{public}s", value.c_str());
2088 EventType eventType = ConvertStringToEventInfoTypes(value);
2089 if (eventType == TYPE_VIEW_INVALID) {
2090 HILOG_ERROR("the event type is invalid");
2091 eventTypes = TYPE_VIEW_INVALID;
2092 return;
2093 }
2094 eventTypes |= eventType;
2095 }
2096 }
2097
ConvertJSToCapabilities(napi_env env,napi_value arrayValue,uint32_t & capabilities)2098 bool ConvertJSToCapabilities(napi_env env, napi_value arrayValue, uint32_t &capabilities)
2099 {
2100 HILOG_DEBUG();
2101 capabilities = 0;
2102 std::vector<std::string> values;
2103 ConvertJSToStringVec(env, arrayValue, values);
2104 for (auto &value : values) {
2105 HILOG_DEBUG("capability is %{public}s", value.c_str());
2106 uint32_t capability = ConvertStringToCapability(value);
2107 if (capability == 0) {
2108 HILOG_ERROR("the capability is invalid");
2109 capabilities = 0;
2110 return false;
2111 }
2112 capabilities |= capability;
2113 }
2114 return true;
2115 }
2116
ConvertStringVecToJS(napi_env env,napi_value & result,std::vector<std::string> values)2117 void ConvertStringVecToJS(napi_env env, napi_value &result, std::vector<std::string> values)
2118 {
2119 HILOG_DEBUG();
2120 size_t index = 0;
2121 for (auto& value : values) {
2122 napi_value str = nullptr;
2123 napi_create_string_utf8(env, value.c_str(), value.size(), &str);
2124 napi_set_element(env, result, index, str);
2125 index++;
2126 }
2127 }
2128
ConvertInt64VecToJS(napi_env env,napi_value & result,std::vector<std::int64_t> values)2129 void ConvertInt64VecToJS(napi_env env, napi_value &result, std::vector<std::int64_t> values)
2130 {
2131 HILOG_DEBUG();
2132 size_t index = 0;
2133 for (auto& value : values) {
2134 napi_value id = nullptr;
2135 napi_create_int64(env, value, &id);
2136 napi_set_element(env, result, index, id);
2137 index++;
2138 }
2139 }
2140
ConvertStringToInt64(std::string & str,int64_t & value)2141 bool ConvertStringToInt64(std::string &str, int64_t &value)
2142 {
2143 auto [ptr, errCode] = std::from_chars(str.data(), str.data() + str.size(), value);
2144 return errCode == std::errc{} && ptr == str.data() + str.size();
2145 }
2146 } // namespace AccessibilityNapi
2147 } // namespace OHOS
2148