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