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