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