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, ¶m);
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, ¶m);
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, ¶m);
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, ¶m);
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 };
658
659 if (triggerActionTable.find(type) == triggerActionTable.end()) {
660 return "";
661 }
662
663 return triggerActionTable.at(type);
664 }
665
ConvertStringToWindowUpdateTypes(std::string type)666 static WindowUpdateType ConvertStringToWindowUpdateTypes(std::string type)
667 {
668 static const std::map<const std::string, WindowUpdateType> windowsUpdateTypesTable = {
669 {"accessibilityFocus", WindowUpdateType::WINDOW_UPDATE_ACCESSIBILITY_FOCUSED},
670 {"focus", WindowUpdateType::WINDOW_UPDATE_FOCUSED},
671 {"active", WindowUpdateType::WINDOW_UPDATE_ACTIVE},
672 {"add", WindowUpdateType::WINDOW_UPDATE_ADDED},
673 {"remove", WindowUpdateType::WINDOW_UPDATE_REMOVED},
674 {"bounds", WindowUpdateType::WINDOW_UPDATE_BOUNDS},
675 {"title", WindowUpdateType::WINDOW_UPDATE_TITLE},
676 {"layer", WindowUpdateType::WINDOW_UPDATE_LAYER},
677 {"parent", WindowUpdateType::WINDOW_UPDATE_PARENT},
678 {"children", WindowUpdateType::WINDOW_UPDATE_CHILDREN},
679 {"pip", WindowUpdateType::WINDOW_UPDATE_PIP},
680 {"property", WindowUpdateType::WINDOW_UPDATE_PROPERTY}};
681
682 if (windowsUpdateTypesTable.find(type) == windowsUpdateTypesTable.end()) {
683 HILOG_WARN("invalid key[%{public}s]", type.c_str());
684 return WINDOW_UPDATE_INVALID;
685 }
686
687 return windowsUpdateTypesTable.at(type);
688 }
689
ConvertStringToEventInfoTypes(std::string type)690 static EventType ConvertStringToEventInfoTypes(std::string type)
691 {
692 static const std::map<const std::string, EventType> eventInfoTypesTable = {
693 {"click", EventType::TYPE_VIEW_CLICKED_EVENT},
694 {"longClick", EventType::TYPE_VIEW_LONG_CLICKED_EVENT},
695 {"select", EventType::TYPE_VIEW_SELECTED_EVENT},
696 {"focus", EventType::TYPE_VIEW_FOCUSED_EVENT},
697 {"textUpdate", EventType::TYPE_VIEW_TEXT_UPDATE_EVENT},
698 {"hoverEnter", EventType::TYPE_VIEW_HOVER_ENTER_EVENT},
699 {"hoverExit", EventType::TYPE_VIEW_HOVER_EXIT_EVENT},
700 {"scroll", EventType::TYPE_VIEW_SCROLLED_EVENT},
701 {"textSelectionUpdate", EventType::TYPE_VIEW_TEXT_SELECTION_UPDATE_EVENT},
702 {"accessibilityFocus", EventType::TYPE_VIEW_ACCESSIBILITY_FOCUSED_EVENT},
703 {"accessibilityFocusClear", EventType::TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED_EVENT},
704 {"requestFocusForAccessibility", EventType::TYPE_VIEW_REQUEST_FOCUS_FOR_ACCESSIBILITY},
705 {"announceForAccessibility", EventType::TYPE_VIEW_ANNOUNCE_FOR_ACCESSIBILITY}};
706
707 if (eventInfoTypesTable.find(type) == eventInfoTypesTable.end()) {
708 HILOG_WARN("invalid key[%{public}s]", type.c_str());
709 return TYPE_VIEW_INVALID;
710 }
711
712 return eventInfoTypesTable.at(type);
713 }
714
ConvertStringToCapability(std::string type)715 static uint32_t ConvertStringToCapability(std::string type)
716 {
717 HILOG_DEBUG();
718 static const std::map<const std::string, uint32_t> capabilitiesTable = {
719 {"retrieve", Capability::CAPABILITY_RETRIEVE},
720 {"touchGuide", Capability::CAPABILITY_TOUCH_GUIDE},
721 {"keyEventObserver", Capability::CAPABILITY_KEY_EVENT_OBSERVER},
722 {"zoom", Capability::CAPABILITY_ZOOM},
723 {"gesture", Capability::CAPABILITY_GESTURE}};
724
725 if (capabilitiesTable.find(type) == capabilitiesTable.end()) {
726 HILOG_WARN("invalid key[%{public}s]", type.c_str());
727 return 0;
728 }
729
730 return capabilitiesTable.at(type);
731 }
732
ConvertStringToAccessibleOperationType(const std::string & type)733 ActionType ConvertStringToAccessibleOperationType(const std::string &type)
734 {
735 std::map<const std::string, ActionType> accessibleOperationTypeTable = {
736 {"focus", ActionType::ACCESSIBILITY_ACTION_FOCUS},
737 {"clearFocus", ActionType::ACCESSIBILITY_ACTION_CLEAR_FOCUS},
738 {"select", ActionType::ACCESSIBILITY_ACTION_SELECT},
739 {"clearSelection", ActionType::ACCESSIBILITY_ACTION_CLEAR_SELECTION},
740 {"click", ActionType::ACCESSIBILITY_ACTION_CLICK},
741 {"longClick", ActionType::ACCESSIBILITY_ACTION_LONG_CLICK},
742 {"accessibilityFocus", ActionType::ACCESSIBILITY_ACTION_ACCESSIBILITY_FOCUS},
743 {"clearAccessibilityFocus", ActionType::ACCESSIBILITY_ACTION_CLEAR_ACCESSIBILITY_FOCUS},
744 {"scrollForward", ActionType::ACCESSIBILITY_ACTION_SCROLL_FORWARD},
745 {"scrollBackward", ActionType::ACCESSIBILITY_ACTION_SCROLL_BACKWARD},
746 {"copy", ActionType::ACCESSIBILITY_ACTION_COPY},
747 {"paste", ActionType::ACCESSIBILITY_ACTION_PASTE},
748 {"cut", ActionType::ACCESSIBILITY_ACTION_CUT},
749 {"setSelection", ActionType::ACCESSIBILITY_ACTION_SET_SELECTION},
750 {"setCursorPosition", ActionType::ACCESSIBILITY_ACTION_SET_CURSOR_POSITION},
751 {"common", ActionType::ACCESSIBILITY_ACTION_COMMON},
752 {"setText", ActionType::ACCESSIBILITY_ACTION_SET_TEXT},
753 {"delete", ActionType::ACCESSIBILITY_ACTION_DELETED},
754 {"home", ActionType::ACCESSIBILITY_ACTION_HOME},
755 {"back", ActionType::ACCESSIBILITY_ACTION_BACK},
756 {"recentTask", ActionType::ACCESSIBILITY_ACTION_RECENTTASK},
757 {"notificationCenter", ActionType::ACCESSIBILITY_ACTION_NOTIFICATIONCENTER},
758 {"controlCenter", ActionType::ACCESSIBILITY_ACTION_CONTROLCENTER},
759 {"spanClick", ActionType::ACCESSIBILITY_ACTION_SPAN_CLICK}};
760
761 if (accessibleOperationTypeTable.find(type) == accessibleOperationTypeTable.end()) {
762 HILOG_WARN("invalid key[%{public}s]", type.c_str());
763 return ACCESSIBILITY_ACTION_INVALID;
764 }
765
766 return accessibleOperationTypeTable.at(type);
767 }
768
ConvertStringToAccessibilityAbilityTypes(const std::string & type)769 AccessibilityAbilityTypes ConvertStringToAccessibilityAbilityTypes(const std::string &type)
770 {
771 std::map<const std::string, AccessibilityAbilityTypes> accessibilityAbilityTypesTable = {
772 {"spoken", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_SPOKEN},
773 {"haptic", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_HAPTIC},
774 {"audible", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_AUDIBLE},
775 {"visual", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_VISUAL},
776 {"generic", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_GENERIC},
777 {"all", AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_ALL},
778 };
779
780 if (accessibilityAbilityTypesTable.find(type) == accessibilityAbilityTypesTable.end()) {
781 HILOG_WARN("invalid key[%{public}s]", type.c_str());
782 return AccessibilityAbilityTypes::ACCESSIBILITY_ABILITY_TYPE_INVALID;
783 }
784
785 return accessibilityAbilityTypesTable.at(type);
786 }
787
ConvertStringToAbilityStateType(const std::string & type)788 AbilityStateType ConvertStringToAbilityStateType(const std::string &type)
789 {
790 std::map<const std::string, AbilityStateType> abilityStateTypeTable = {
791 {"enable", AbilityStateType::ABILITY_STATE_ENABLE},
792 {"disable", AbilityStateType::ABILITY_STATE_DISABLE},
793 {"install", AbilityStateType::ABILITY_STATE_INSTALLED}};
794
795 if (abilityStateTypeTable.find(type) == abilityStateTypeTable.end()) {
796 HILOG_WARN("invalid key[%{public}s]", type.c_str());
797 return ABILITY_STATE_INVALID;
798 }
799
800 return abilityStateTypeTable.at(type);
801 }
802
ConvertStringToDaltonizationTypes(std::string & type)803 OHOS::AccessibilityConfig::DALTONIZATION_TYPE ConvertStringToDaltonizationTypes(std::string& type)
804 {
805 std::map<const std::string, OHOS::AccessibilityConfig::DALTONIZATION_TYPE> daltonizationTTypesTable = {
806 {"Normal", OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Normal},
807 {"Protanomaly", OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Protanomaly},
808 {"Deuteranomaly", OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Deuteranomaly},
809 {"Tritanomaly", OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Tritanomaly},
810 };
811
812 if (daltonizationTTypesTable.find(type) == daltonizationTTypesTable.end()) {
813 HILOG_WARN("invalid key[%{public}s]", type.c_str());
814 return OHOS::AccessibilityConfig::DALTONIZATION_TYPE::Normal;
815 }
816
817 return daltonizationTTypesTable.at(type);
818 }
819
ConvertStringToClickResponseTimeTypes(std::string & type)820 OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME ConvertStringToClickResponseTimeTypes(std::string& type)
821 {
822 std::map<const std::string, OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME> clickResponseTimeTypesTable = {
823 {"Short", OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayShort},
824 {"Medium", OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayMedium},
825 {"Long", OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayLong},
826 };
827
828 if (clickResponseTimeTypesTable.find(type) == clickResponseTimeTypesTable.end()) {
829 HILOG_WARN("invalid key[%{public}s]", type.c_str());
830 return OHOS::AccessibilityConfig::CLICK_RESPONSE_TIME::ResponseDelayShort;
831 }
832
833 return clickResponseTimeTypesTable.at(type);
834 }
835
ConvertStringToIgnoreRepeatClickTimeTypes(std::string & type)836 OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME ConvertStringToIgnoreRepeatClickTimeTypes(std::string& type)
837 {
838 std::map<const std::string, OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME> mapTable = {
839 {"Shortest", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutShortest},
840 {"Short", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutShort},
841 {"Medium", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutMedium},
842 {"Long", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutLong},
843 {"Longest", OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutLongest},
844 };
845
846 if (mapTable.find(type) == mapTable.end()) {
847 HILOG_WARN("invalid key[%{public}s]", type.c_str());
848 return OHOS::AccessibilityConfig::IGNORE_REPEAT_CLICK_TIME::RepeatClickTimeoutShortest;
849 }
850
851 return mapTable.at(type);
852 }
853
ConvertStringToTextMoveUnit(const std::string & type)854 TextMoveUnit ConvertStringToTextMoveUnit(const std::string &type)
855 {
856 static const std::map<const std::string, TextMoveUnit> textMoveUnitTable = {{"char", TextMoveUnit::STEP_CHARACTER},
857 {"word", TextMoveUnit::STEP_WORD},
858 {"line", TextMoveUnit::STEP_LINE},
859 {"page", TextMoveUnit::STEP_PAGE},
860 {"paragraph", TextMoveUnit::STEP_PARAGRAPH}};
861
862 if (textMoveUnitTable.find(type) == textMoveUnitTable.end()) {
863 HILOG_WARN("invalid key[%{public}s]", type.c_str());
864 return STEP_INVALID;
865 }
866
867 return textMoveUnitTable.at(type);
868 }
869
ConvertTextMoveUnitToString(TextMoveUnit type)870 std::string ConvertTextMoveUnitToString(TextMoveUnit type)
871 {
872 static const std::map<TextMoveUnit, const std::string> textMoveUnitTable = {{TextMoveUnit::STEP_CHARACTER, "char"},
873 {TextMoveUnit::STEP_WORD, "word"},
874 {TextMoveUnit::STEP_LINE, "line"},
875 {TextMoveUnit::STEP_PAGE, "page"},
876 {TextMoveUnit::STEP_PARAGRAPH, "paragraph"}};
877
878 if (textMoveUnitTable.find(type) == textMoveUnitTable.end()) {
879 HILOG_WARN("invalid key[0x%{public}x]", type);
880 return "";
881 }
882
883 return textMoveUnitTable.at(type);
884 }
885
ConvertActionArgsJSToNAPI(napi_env env,napi_value object,std::map<std::string,std::string> & args,OHOS::Accessibility::ActionType action)886 void ConvertActionArgsJSToNAPI(
887 napi_env env, napi_value object, std::map<std::string, std::string>& args, OHOS::Accessibility::ActionType action)
888 {
889 napi_value propertyNameValue = nullptr;
890 bool hasProperty = false;
891 std::string str = "";
892 std::map<std::string, std::string> scrollValueMap = { {"halfScreen", HALF_VALUE}, {"fullScreen", FULL_VALUE} };
893 std::string scrollValue = FULL_VALUE;
894 bool seleFlag = false;
895 switch (action) {
896 case ActionType::ACCESSIBILITY_ACTION_NEXT_HTML_ITEM:
897 case ActionType::ACCESSIBILITY_ACTION_PREVIOUS_HTML_ITEM:
898 napi_create_string_utf8(env, "htmlItem", NAPI_AUTO_LENGTH, &propertyNameValue);
899 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
900 if (hasProperty) {
901 args.insert(std::pair<std::string, std::string>("htmlItem", str.c_str()));
902 }
903 break;
904 case ActionType::ACCESSIBILITY_ACTION_NEXT_TEXT:
905 case ActionType::ACCESSIBILITY_ACTION_PREVIOUS_TEXT:
906 napi_create_string_utf8(env, "textMoveUnit", NAPI_AUTO_LENGTH, &propertyNameValue);
907 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
908 if (hasProperty) {
909 args.insert(std::pair<std::string, std::string>("textMoveUnit", str.c_str()));
910 }
911 break;
912 case ActionType::ACCESSIBILITY_ACTION_SET_SELECTION:
913 napi_create_string_utf8(env, "selectTextBegin", NAPI_AUTO_LENGTH, &propertyNameValue);
914 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
915 if (hasProperty) {
916 args.insert(std::pair<std::string, std::string>("selectTextBegin", str.c_str()));
917 }
918 napi_create_string_utf8(env, "selectTextEnd", NAPI_AUTO_LENGTH, &propertyNameValue);
919 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
920 if (hasProperty) {
921 args.insert(std::pair<std::string, std::string>("selectTextEnd", str.c_str()));
922 }
923 napi_create_string_utf8(env, "selectTextInForWard", NAPI_AUTO_LENGTH, &propertyNameValue);
924 seleFlag = ConvertBoolJSToNAPI(env, object, propertyNameValue, hasProperty);
925 if (hasProperty) {
926 std::string value = seleFlag ? "forWard" : "backWard";
927 args.insert(std::pair<std::string, std::string>("selectTextInForWard", value.c_str()));
928 }
929 break;
930 case ActionType::ACCESSIBILITY_ACTION_SET_CURSOR_POSITION:
931 napi_create_string_utf8(env, "offset", NAPI_AUTO_LENGTH, &propertyNameValue);
932 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
933 if (hasProperty) {
934 args.insert(std::pair<std::string, std::string>("offset", str.c_str()));
935 }
936 break;
937 case ActionType::ACCESSIBILITY_ACTION_SET_TEXT:
938 napi_create_string_utf8(env, "setText", NAPI_AUTO_LENGTH, &propertyNameValue);
939 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
940 if (hasProperty) {
941 args.insert(std::pair<std::string, std::string>("setText", str.c_str()));
942 }
943 break;
944 case ActionType::ACCESSIBILITY_ACTION_SPAN_CLICK:
945 napi_create_string_utf8(env, "spanId", NAPI_AUTO_LENGTH, &propertyNameValue);
946 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
947 if (hasProperty) {
948 args.insert(std::pair<std::string, std::string>("spanId", str.c_str()));
949 }
950 break;
951 case ActionType::ACCESSIBILITY_ACTION_SCROLL_FORWARD:
952 napi_create_string_utf8(env, "scrolltype", NAPI_AUTO_LENGTH, &propertyNameValue);
953 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
954 if (hasProperty) {
955 if (scrollValueMap.find(str) != scrollValueMap.end()) {
956 scrollValue = scrollValueMap.find(str)->second;
957 HILOG_DEBUG("ScrollValue %{public}s", scrollValue.c_str());
958 } else {
959 HILOG_DEBUG("Input is empty, output fullScreen, value is 1");
960 }
961 args.insert(std::pair<std::string, std::string>("scrolltype", scrollValue.c_str()));
962 }
963 break;
964 case ActionType::ACCESSIBILITY_ACTION_SCROLL_BACKWARD:
965 napi_create_string_utf8(env, "scrolltype", NAPI_AUTO_LENGTH, &propertyNameValue);
966 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
967 if (hasProperty) {
968 if (scrollValueMap.find(str) != scrollValueMap.end()) {
969 scrollValue = scrollValueMap.find(str)->second;
970 HILOG_DEBUG("ScrollValue %{public}s", scrollValue.c_str());
971 } else {
972 HILOG_DEBUG("Input is empty, output fullScreen, value is 1");
973 }
974 args.insert(std::pair<std::string, std::string>("scrolltype", scrollValue.c_str()));
975 }
976 break;
977 default:
978 break;
979 }
980 }
981
ConvertIntJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty)982 int32_t ConvertIntJSToNAPI(napi_env env, napi_value object, napi_value propertyNameValue, bool &hasProperty)
983 {
984 int32_t dataValue = 0;
985 napi_has_property(env, object, propertyNameValue, &hasProperty);
986 if (hasProperty) {
987 napi_value itemValue = nullptr;
988 napi_get_property(env, object, propertyNameValue, &itemValue);
989 napi_get_value_int32(env, itemValue, &dataValue);
990 }
991 return dataValue;
992 }
993
ConvertBoolJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty)994 bool ConvertBoolJSToNAPI(napi_env env, napi_value object, napi_value propertyNameValue, bool &hasProperty)
995 {
996 bool isBool = false;
997 napi_has_property(env, object, propertyNameValue, &hasProperty);
998 if (hasProperty) {
999 napi_value itemValue = nullptr;
1000 napi_get_property(env, object, propertyNameValue, &itemValue);
1001 napi_get_value_bool(env, itemValue, &isBool);
1002 }
1003 return isBool;
1004 }
1005
ConvertStringJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty)1006 std::string ConvertStringJSToNAPI(napi_env env, napi_value object, napi_value propertyNameValue, bool &hasProperty)
1007 {
1008 std::string str = "";
1009 napi_has_property(env, object, propertyNameValue, &hasProperty);
1010 if (hasProperty) {
1011 napi_value itemValue = nullptr;
1012 napi_get_property(env, object, propertyNameValue, &itemValue);
1013 str = GetStringFromNAPI(env, itemValue);
1014 }
1015 return str;
1016 }
1017
ConvertStringArrayJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty,std::vector<std::string> & stringArray)1018 void ConvertStringArrayJSToNAPI(napi_env env, napi_value object,
1019 napi_value propertyNameValue, bool &hasProperty, std::vector<std::string> &stringArray)
1020 {
1021 napi_has_property(env, object, propertyNameValue, &hasProperty);
1022 if (hasProperty) {
1023 napi_value contentsValue = nullptr;
1024 napi_get_property(env, object, propertyNameValue, &contentsValue);
1025 napi_value data = nullptr;
1026 uint32_t dataLen = 0;
1027 napi_get_array_length(env, contentsValue, &dataLen);
1028 for (uint32_t i = 0; i < dataLen; i++) {
1029 napi_get_element(env, contentsValue, i, &data);
1030 std::string str = GetStringFromNAPI(env, data);
1031 stringArray.push_back(str);
1032 }
1033 }
1034 }
1035
ConvertStringArrayJSToNAPICommon(napi_env env,napi_value object,std::vector<std::string> & stringArray)1036 void ConvertStringArrayJSToNAPICommon(napi_env env, napi_value object, std::vector<std::string> &stringArray)
1037 {
1038 napi_value data = nullptr;
1039 uint32_t dataLen = 0;
1040 napi_get_array_length(env, object, &dataLen);
1041 for (uint32_t i = 0; i < dataLen; i++) {
1042 napi_get_element(env, object, i, &data);
1043 std::string str = GetStringFromNAPI(env, data);
1044 stringArray.push_back(str);
1045 }
1046 }
1047
ConvertSpanToJS(napi_env env,napi_value result,const Accessibility::SpanInfo & span)1048 void ConvertSpanToJS(napi_env env, napi_value result, const Accessibility::SpanInfo &span)
1049 {
1050 napi_value spanId;
1051 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, span.GetSpanId(), &spanId));
1052 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "spanId", spanId));
1053
1054 napi_value spanText;
1055 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, span.GetSpanText().c_str(), NAPI_AUTO_LENGTH, &spanText));
1056 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "spanText", spanText));
1057
1058 napi_value accessibilityText;
1059 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, span.GetAccessibilityText().c_str(),
1060 NAPI_AUTO_LENGTH, &accessibilityText));
1061 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "accessibilityText", accessibilityText));
1062
1063 napi_value accessibilityDescription;
1064 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, span.GetAccessibilityDescription().c_str(),
1065 NAPI_AUTO_LENGTH, &accessibilityDescription));
1066 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "accessibilityDescription",
1067 accessibilityDescription));
1068
1069 napi_value accessibilityLevel;
1070 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, span.GetAccessibilityLevel().c_str(),
1071 NAPI_AUTO_LENGTH, &accessibilityLevel));
1072 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "accessibilityLevel", accessibilityLevel));
1073 }
1074
ConvertEventInfoJSToNAPI(napi_env env,napi_value object,OHOS::Accessibility::AccessibilityEventInfo & eventInfo)1075 bool ConvertEventInfoJSToNAPI(
1076 napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo)
1077 {
1078 HILOG_DEBUG();
1079 bool tmpResult = ConvertEventInfoJSToNAPIPart1(env, object, eventInfo);
1080 if (!tmpResult) {
1081 return false;
1082 }
1083 tmpResult = ConvertEventInfoJSToNAPIPart2(env, object, eventInfo);
1084 if (!tmpResult) {
1085 return false;
1086 }
1087 tmpResult = ConvertEventInfoJSToNAPIPart3(env, object, eventInfo);
1088 if (!tmpResult) {
1089 return false;
1090 }
1091 return true;
1092 }
1093
ConvertEventInfoJSToNAPIPart1(napi_env env,napi_value object,OHOS::Accessibility::AccessibilityEventInfo & eventInfo)1094 bool ConvertEventInfoJSToNAPIPart1(
1095 napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo)
1096 {
1097 bool hasProperty = false;
1098 std::string str = "";
1099 napi_value propertyNameValue = nullptr;
1100 napi_create_string_utf8(env, "type", NAPI_AUTO_LENGTH, &propertyNameValue);
1101 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1102 if (hasProperty) {
1103 EventType eventType = ConvertStringToEventInfoTypes(str);
1104 eventInfo.SetEventType(eventType);
1105 if (eventType == TYPE_VIEW_INVALID) {
1106 return false;
1107 }
1108 } else {
1109 return false;
1110 }
1111
1112 napi_create_string_utf8(env, "windowUpdateType", NAPI_AUTO_LENGTH, &propertyNameValue);
1113 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1114 if (hasProperty) {
1115 eventInfo.SetEventType(TYPE_WINDOW_UPDATE);
1116 eventInfo.SetWindowChangeTypes(ConvertStringToWindowUpdateTypes(str));
1117 }
1118
1119 napi_create_string_utf8(env, "bundleName", NAPI_AUTO_LENGTH, &propertyNameValue);
1120 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1121 if (hasProperty) {
1122 if (str != "") {
1123 eventInfo.SetBundleName(str);
1124 } else {
1125 return false;
1126 }
1127 } else {
1128 return false;
1129 }
1130 return true;
1131 }
1132
ConvertEventInfoJSToNAPIPart2(napi_env env,napi_value object,OHOS::Accessibility::AccessibilityEventInfo & eventInfo)1133 bool ConvertEventInfoJSToNAPIPart2(
1134 napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo)
1135 {
1136 bool hasProperty = false;
1137 int32_t dataValue = 0;
1138 std::string str = "";
1139 napi_value propertyNameValue = nullptr;
1140 napi_create_string_utf8(env, "componentType", NAPI_AUTO_LENGTH, &propertyNameValue);
1141 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1142 if (hasProperty) {
1143 eventInfo.SetComponentType(str);
1144 }
1145
1146 napi_create_string_utf8(env, "pageId", NAPI_AUTO_LENGTH, &propertyNameValue);
1147 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1148 if (hasProperty) {
1149 eventInfo.SetPageId(dataValue);
1150 }
1151
1152 napi_create_string_utf8(env, "description", NAPI_AUTO_LENGTH, &propertyNameValue);
1153 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1154 if (hasProperty) {
1155 eventInfo.SetDescription(str);
1156 }
1157
1158 napi_create_string_utf8(env, "triggerAction", NAPI_AUTO_LENGTH, &propertyNameValue);
1159 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1160 if (hasProperty) {
1161 eventInfo.SetTriggerAction(ConvertStringToAccessibleOperationType(str));
1162 if (eventInfo.GetTriggerAction() == ACCESSIBILITY_ACTION_INVALID) {
1163 return false;
1164 }
1165 } else {
1166 return false;
1167 }
1168
1169 napi_create_string_utf8(env, "textMoveUnit", NAPI_AUTO_LENGTH, &propertyNameValue);
1170 str = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1171 if (hasProperty) {
1172 eventInfo.SetTextMovementStep(ConvertStringToTextMoveUnit(str));
1173 }
1174
1175 napi_create_string_utf8(env, "elementId", NAPI_AUTO_LENGTH, &propertyNameValue);
1176 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1177 if (hasProperty) {
1178 eventInfo.SetRequestFocusElementId(dataValue);
1179 }
1180 return true;
1181 }
1182
ConvertEventInfoJSToNAPIPart3(napi_env env,napi_value object,OHOS::Accessibility::AccessibilityEventInfo & eventInfo)1183 bool ConvertEventInfoJSToNAPIPart3(
1184 napi_env env, napi_value object, OHOS::Accessibility::AccessibilityEventInfo& eventInfo)
1185 {
1186 bool hasProperty = false;
1187 int32_t dataValue = 0;
1188 napi_value propertyNameValue = nullptr;
1189 napi_create_string_utf8(env, "contents", NAPI_AUTO_LENGTH, &propertyNameValue);
1190 std::vector<std::string> stringArray {};
1191 ConvertStringArrayJSToNAPI(env, object, propertyNameValue, hasProperty, stringArray);
1192 if (hasProperty) {
1193 for (auto str : stringArray) {
1194 eventInfo.AddContent(str);
1195 }
1196 }
1197
1198 napi_create_string_utf8(env, "lastContent", NAPI_AUTO_LENGTH, &propertyNameValue);
1199 std::string strNapi = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1200 if (hasProperty) {
1201 eventInfo.SetLatestContent(strNapi);
1202 }
1203
1204 napi_create_string_utf8(env, "beginIndex", NAPI_AUTO_LENGTH, &propertyNameValue);
1205 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1206 if (hasProperty) {
1207 eventInfo.SetBeginIndex(dataValue);
1208 }
1209
1210 napi_create_string_utf8(env, "currentIndex", NAPI_AUTO_LENGTH, &propertyNameValue);
1211 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1212 if (hasProperty) {
1213 eventInfo.SetCurrentIndex(dataValue);
1214 }
1215
1216 napi_create_string_utf8(env, "endIndex", NAPI_AUTO_LENGTH, &propertyNameValue);
1217 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1218 if (hasProperty) {
1219 eventInfo.SetEndIndex(dataValue);
1220 }
1221
1222 napi_create_string_utf8(env, "itemCount", NAPI_AUTO_LENGTH, &propertyNameValue);
1223 dataValue = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1224 if (hasProperty) {
1225 eventInfo.SetItemCounts(dataValue);
1226 }
1227
1228 napi_create_string_utf8(env, "customId", NAPI_AUTO_LENGTH, &propertyNameValue);
1229 std::string inspectorKey = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1230 if (hasProperty) {
1231 eventInfo.SetInspectorKey(inspectorKey);
1232 }
1233
1234 napi_create_string_utf8(env, "textAnnouncedForAccessibility", NAPI_AUTO_LENGTH, &propertyNameValue);
1235 std::string announceText = ConvertStringJSToNAPI(env, object, propertyNameValue, hasProperty);
1236 if (hasProperty) {
1237 eventInfo.SetTextAnnouncedForAccessibility(announceText);
1238 }
1239 return true;
1240 }
1241
ConvertGesturePointJSToNAPI(napi_env env,napi_value object,AccessibilityGesturePosition & gesturePathPosition)1242 static bool ConvertGesturePointJSToNAPI(
1243 napi_env env, napi_value object, AccessibilityGesturePosition& gesturePathPosition)
1244 {
1245 HILOG_DEBUG();
1246 napi_value propertyNameValue = nullptr;
1247 bool hasProperty = false;
1248 double position = 0;
1249
1250 napi_create_string_utf8(env, "positionX", NAPI_AUTO_LENGTH, &propertyNameValue);
1251 napi_has_property(env, object, propertyNameValue, &hasProperty);
1252 if (hasProperty) {
1253 napi_value valueX = nullptr;
1254 napi_get_property(env, object, propertyNameValue, &valueX);
1255 napi_get_value_double(env, valueX, &position);
1256 gesturePathPosition.positionX_ = static_cast<float>(position);
1257 } else {
1258 return false;
1259 }
1260
1261 napi_create_string_utf8(env, "positionY", NAPI_AUTO_LENGTH, &propertyNameValue);
1262 napi_has_property(env, object, propertyNameValue, &hasProperty);
1263 if (hasProperty) {
1264 napi_value valueY = nullptr;
1265 napi_get_property(env, object, propertyNameValue, &valueY);
1266 napi_get_value_double(env, valueY, &position);
1267 gesturePathPosition.positionY_ = static_cast<float>(position);
1268 } else {
1269 return false;
1270 }
1271 return true;
1272 }
1273
ConvertGesturePathJSToNAPI(napi_env env,napi_value object,std::shared_ptr<AccessibilityGestureInjectPath> & gesturePath)1274 bool ConvertGesturePathJSToNAPI(napi_env env, napi_value object,
1275 std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath)
1276 {
1277 HILOG_DEBUG();
1278 if (!gesturePath) {
1279 HILOG_ERROR("gesturePath is null.");
1280 return false;
1281 }
1282
1283 bool tmpResult = ConvertGesturePathJSToNAPIPart1(env, object, gesturePath);
1284 if (!tmpResult) {
1285 return false;
1286 }
1287 tmpResult = ConvertGesturePathJSToNAPIPart2(env, object, gesturePath);
1288 if (!tmpResult) {
1289 return false;
1290 }
1291 return true;
1292 }
1293
ConvertGesturePathJSToNAPIPart1(napi_env env,napi_value object,std::shared_ptr<AccessibilityGestureInjectPath> & gesturePath)1294 bool ConvertGesturePathJSToNAPIPart1(napi_env env, napi_value object,
1295 std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath)
1296 {
1297 napi_value propertyNameValue = nullptr;
1298 bool hasProperty = false;
1299
1300 napi_create_string_utf8(env, "points", NAPI_AUTO_LENGTH, &propertyNameValue);
1301 napi_has_property(env, object, propertyNameValue, &hasProperty);
1302 if (hasProperty) {
1303 napi_value positionValue = nullptr;
1304 napi_get_property(env, object, propertyNameValue, &positionValue);
1305 napi_value jsValue = nullptr;
1306 bool isArray = false;
1307 uint32_t dataLen = 0;
1308 if (napi_is_array(env, positionValue, &isArray) != napi_ok || isArray == false) {
1309 HILOG_ERROR("object is not an array.");
1310 return false;
1311 }
1312 if (napi_get_array_length(env, positionValue, &dataLen) != napi_ok) {
1313 HILOG_ERROR("get array length failed.");
1314 return false;
1315 }
1316 for (uint32_t i = 0; i < dataLen; i++) {
1317 jsValue = nullptr;
1318 AccessibilityGesturePosition path;
1319 if (napi_get_element(env, positionValue, i, &jsValue) != napi_ok) {
1320 HILOG_ERROR("get element of paths failed and i = %{public}d", i);
1321 return false;
1322 }
1323 bool result = ConvertGesturePointJSToNAPI(env, jsValue, path);
1324 if (result) {
1325 gesturePath->AddPosition(path);
1326 } else {
1327 HILOG_ERROR("Parse gesture point error.");
1328 return false;
1329 }
1330 }
1331 } else {
1332 HILOG_ERROR("No points property.");
1333 return false;
1334 }
1335 return true;
1336 }
1337
ConvertGesturePathJSToNAPIPart2(napi_env env,napi_value object,std::shared_ptr<AccessibilityGestureInjectPath> & gesturePath)1338 bool ConvertGesturePathJSToNAPIPart2(napi_env env, napi_value object,
1339 std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath)
1340 {
1341 napi_value propertyNameValue = nullptr;
1342 bool hasProperty = false;
1343
1344 napi_create_string_utf8(env, "durationTime", NAPI_AUTO_LENGTH, &propertyNameValue);
1345 int64_t durationTime = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1346 napi_has_property(env, object, propertyNameValue, &hasProperty);
1347 if (hasProperty) {
1348 gesturePath->SetDurationTime(durationTime);
1349 return true;
1350 }
1351 return false;
1352 }
1353
TransformKeyActionValue(int32_t keyAction)1354 KeyAction TransformKeyActionValue(int32_t keyAction)
1355 {
1356 HILOG_DEBUG("keyAction:%{public}d", keyAction);
1357
1358 KeyAction action = KeyAction::UNKNOWN;
1359 if (keyAction == OHOS::MMI::KeyEvent::KEY_ACTION_DOWN) {
1360 action = KeyAction::DOWN;
1361 } else if (keyAction == OHOS::MMI::KeyEvent::KEY_ACTION_UP) {
1362 action = KeyAction::UP;
1363 } else if (keyAction == OHOS::MMI::KeyEvent::KEY_ACTION_CANCEL) {
1364 action = KeyAction::CANCEL;
1365 } else {
1366 HILOG_DEBUG("key action is invalid");
1367 }
1368 return action;
1369 }
1370
HasKeyCode(const std::vector<int32_t> & pressedKeys,int32_t keyCode)1371 bool HasKeyCode(const std::vector<int32_t>& pressedKeys, int32_t keyCode)
1372 {
1373 HILOG_DEBUG();
1374
1375 return std::find(pressedKeys.begin(), pressedKeys.end(), keyCode) != pressedKeys.end();
1376 }
1377
GetKeyValue(napi_env env,napi_value keyObject,std::optional<MMI::KeyEvent::KeyItem> keyItem)1378 void GetKeyValue(napi_env env, napi_value keyObject, std::optional<MMI::KeyEvent::KeyItem> keyItem)
1379 {
1380 HILOG_DEBUG();
1381
1382 if (!keyItem) {
1383 HILOG_WARN("keyItem is null.");
1384 return;
1385 }
1386
1387 napi_value keyCodeValue = nullptr;
1388 int32_t keyCode = keyItem->GetKeyCode();
1389 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, keyCode, &keyCodeValue));
1390 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, keyObject, "code", keyCodeValue));
1391
1392 napi_value timeValue = nullptr;
1393 int64_t pressedTime = keyItem->GetDownTime();
1394 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, pressedTime, &timeValue));
1395 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, keyObject, "pressedTime", timeValue));
1396
1397 napi_value deviceIdValue = nullptr;
1398 int32_t deviceId = keyItem->GetDeviceId();
1399 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, deviceId, &deviceIdValue));
1400 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, keyObject, "deviceId", deviceIdValue));
1401 }
1402
SetInputEventProperty(napi_env env,napi_value result,const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent)1403 void SetInputEventProperty(napi_env env, napi_value result, const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent)
1404 {
1405 HILOG_DEBUG();
1406
1407 if (!keyEvent) {
1408 HILOG_ERROR("keyEvent is null.");
1409 return;
1410 }
1411 // set id
1412 napi_value idValue = nullptr;
1413 int32_t id = keyEvent->GetId();
1414 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, id, &idValue));
1415 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "id", idValue));
1416
1417 // set deviceId
1418 napi_value deviceIdValue = nullptr;
1419 int32_t deviceId = keyEvent->GetDeviceId();
1420 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, deviceId, &deviceIdValue));
1421 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "deviceId", deviceIdValue));
1422
1423 // set actionTime
1424 napi_value actionTimeValue = nullptr;
1425 int64_t actionTime = keyEvent->GetActionTime();
1426 NAPI_CALL_RETURN_VOID(env, napi_create_int64(env, actionTime, &actionTimeValue));
1427 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "actionTime", actionTimeValue));
1428
1429 // set screenId
1430 napi_value screenIdValue = nullptr;
1431 int32_t screenId = keyEvent->GetTargetDisplayId();
1432 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, screenId, &screenIdValue));
1433 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "screenId", screenIdValue));
1434
1435 // set windowId
1436 napi_value windowIdValue = nullptr;
1437 int32_t windowId = keyEvent->GetTargetWindowId();
1438 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, windowId, &windowIdValue));
1439 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "windowId", windowIdValue));
1440 }
1441
ConvertKeyEventToJS(napi_env env,napi_value result,const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent)1442 void ConvertKeyEventToJS(napi_env env, napi_value result, const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent)
1443 {
1444 HILOG_DEBUG();
1445
1446 if (!keyEvent) {
1447 HILOG_ERROR("keyEvent is null.");
1448 return;
1449 }
1450
1451 // set inputEvent
1452 SetInputEventProperty(env, result, keyEvent);
1453
1454 // set action
1455 napi_value keyActionValue = nullptr;
1456 KeyAction keyAction = TransformKeyActionValue(keyEvent->GetKeyAction());
1457 if (keyAction != KeyAction::UNKNOWN) {
1458 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, keyAction, &keyActionValue));
1459 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "action", keyActionValue));
1460 }
1461
1462 // set key
1463 napi_value keyObject = nullptr;
1464 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &keyObject));
1465 std::optional<MMI::KeyEvent::KeyItem> keyItem = keyEvent->GetKeyItem();
1466 GetKeyValue(env, keyObject, keyItem);
1467 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "key", keyObject));
1468
1469 // set unicodeChar
1470 napi_value unicodeCharValue = nullptr;
1471 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, 0, &unicodeCharValue));
1472 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "unicodeChar", unicodeCharValue));
1473
1474 // set keys
1475 SetKeyPropertyPart1(env, result, keyEvent);
1476 SetKeyPropertyPart2(env, result, keyEvent);
1477 }
1478
SetKeyPropertyPart1(napi_env env,napi_value result,const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent)1479 void SetKeyPropertyPart1(napi_env env, napi_value result, const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent)
1480 {
1481 HILOG_DEBUG();
1482 if (!keyEvent) {
1483 HILOG_ERROR("keyEvent is nullptr.");
1484 return;
1485 }
1486 // set keys
1487 napi_value keysAarry = nullptr;
1488 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &keysAarry));
1489 uint32_t index = 0;
1490 std::vector<int32_t> pressedKeys = keyEvent->GetPressedKeys();
1491 for (const auto &pressedKeyCode : pressedKeys) {
1492 napi_value element = nullptr;
1493 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &element));
1494 std::optional<MMI::KeyEvent::KeyItem> pressedKeyItem = keyEvent->GetKeyItem(pressedKeyCode);
1495 GetKeyValue(env, element, pressedKeyItem);
1496 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, keysAarry, index, element));
1497 ++index;
1498 }
1499 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "keys", keysAarry));
1500
1501 // set ctrlKey
1502 bool isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_CTRL_LEFT)
1503 || HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_CTRL_RIGHT);
1504 napi_value ctrlKeyValue = nullptr;
1505 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &ctrlKeyValue));
1506 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "ctrlKey", ctrlKeyValue));
1507
1508 // set altKey
1509 isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_ALT_LEFT)
1510 || HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_ALT_RIGHT);
1511 napi_value altKeyValue = nullptr;
1512 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &altKeyValue));
1513 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "altKey", altKeyValue));
1514
1515 // set shiftKey
1516 isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_SHIFT_LEFT)
1517 || HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_SHIFT_RIGHT);
1518 napi_value shiftKeyValue = nullptr;
1519 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &shiftKeyValue));
1520 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "shiftKey", shiftKeyValue));
1521
1522 // set logoKey
1523 isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_META_LEFT)
1524 || HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_META_RIGHT);
1525 napi_value logoKeyValue = nullptr;
1526 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &logoKeyValue));
1527 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "logoKey", logoKeyValue));
1528
1529 // set fnKey
1530 isPressed = HasKeyCode(pressedKeys, OHOS::MMI::KeyEvent::KEYCODE_FN);
1531 napi_value fnKeyValue = nullptr;
1532 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, isPressed, &fnKeyValue));
1533 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fnKey", fnKeyValue));
1534 }
1535
SetKeyPropertyPart2(napi_env env,napi_value result,const std::shared_ptr<OHOS::MMI::KeyEvent> & keyEvent)1536 void SetKeyPropertyPart2(napi_env env, napi_value result, const std::shared_ptr<OHOS::MMI::KeyEvent> &keyEvent)
1537 {
1538 HILOG_DEBUG();
1539 // set capsLock
1540 napi_value capsLockValue = nullptr;
1541 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, false, &capsLockValue));
1542 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "capsLock", capsLockValue));
1543
1544 // set numLock
1545 napi_value numLockValue = nullptr;
1546 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, false, &numLockValue));
1547 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "numLock", numLockValue));
1548
1549 // set scrollLock
1550 napi_value scrollLockValue = nullptr;
1551 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, false, &scrollLockValue));
1552 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "scrollLock", scrollLockValue));
1553 }
1554
ConvertCaptionPropertyToJS(napi_env env,napi_value & result,OHOS::AccessibilityConfig::CaptionProperty captionProperty)1555 void ConvertCaptionPropertyToJS(
1556 napi_env env, napi_value& result, OHOS::AccessibilityConfig::CaptionProperty captionProperty)
1557 {
1558 HILOG_DEBUG();
1559
1560 napi_value value = nullptr;
1561
1562 NAPI_CALL_RETURN_VOID(env,
1563 napi_create_string_utf8(env, captionProperty.GetFontFamily().c_str(), NAPI_AUTO_LENGTH, &value));
1564 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fontFamily", value));
1565
1566 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, captionProperty.GetFontScale(), &value));
1567 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fontScale", value));
1568
1569 uint32_t color = captionProperty.GetFontColor();
1570 std::string colorStr = ConvertColorToString(color);
1571 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, colorStr.c_str(), NAPI_AUTO_LENGTH, &value));
1572 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fontColor", value));
1573
1574 NAPI_CALL_RETURN_VOID(env,
1575 napi_create_string_utf8(env, captionProperty.GetFontEdgeType().c_str(), NAPI_AUTO_LENGTH, &value));
1576 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "fontEdgeType", value));
1577
1578 color = captionProperty.GetBackgroundColor();
1579 colorStr = ConvertColorToString(color);
1580 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, colorStr.c_str(), NAPI_AUTO_LENGTH, &value));
1581 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "backgroundColor", value));
1582
1583 color = captionProperty.GetWindowColor();
1584 colorStr = ConvertColorToString(color);
1585 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, colorStr.c_str(), NAPI_AUTO_LENGTH, &value));
1586 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "windowColor", value));
1587 }
1588
ConvertColorStringToNumer(std::string colorStr)1589 uint32_t ConvertColorStringToNumer(std::string colorStr)
1590 {
1591 HILOG_DEBUG("colorStr is %{public}s", colorStr.c_str());
1592 uint32_t color = COLOR_TRANSPARENT;
1593 if (colorStr.empty()) {
1594 // Empty string, return transparent
1595 return color;
1596 }
1597 // Remove all " ".
1598 colorStr.erase(std::remove(colorStr.begin(), colorStr.end(), ' '), colorStr.end());
1599
1600 if (ColorRegexMatch(colorStr, color)) {
1601 return color;
1602 }
1603
1604 // Match for special string
1605 static const std::map<std::string, uint32_t> colorTable {
1606 std::make_pair("black", COLOR_BLACK),
1607 std::make_pair("blue", COLOR_BLUE),
1608 std::make_pair("gray", COLOR_GRAY),
1609 std::make_pair("green", COLOR_GREEN),
1610 std::make_pair("red", COLOR_RED),
1611 std::make_pair("white", COLOR_WHITE),
1612 };
1613 auto it = colorTable.find(colorStr.c_str());
1614 if (it != colorTable.end()) {
1615 color = it->second;
1616 }
1617 return color;
1618 }
1619
ColorRegexMatch(std::string colorStr,uint32_t & color)1620 bool ColorRegexMatch(std::string colorStr, uint32_t &color)
1621 {
1622 // Regex match for #909090 or #90909090.
1623 if (std::regex_match(colorStr, COLOR_WITH_MAGIC)) {
1624 colorStr.erase(0, 1);
1625 auto colorValue = stoul(colorStr, nullptr, COLOR_STRING_BASE);
1626 if (colorStr.length() < COLOR_STRING_SIZE_STANDARD) {
1627 // No alpha specified, set alpha to 0xff
1628 colorValue |= COLOR_ALPHA_MASK;
1629 } else {
1630 auto alpha = colorValue << ALPHA_MOVE;
1631 auto rgb = colorValue >> COLOR_MOVE;
1632 colorValue = alpha | rgb;
1633 }
1634 color = colorValue;
1635 return true;
1636 }
1637 // Regex match for #rgb or #rgba.
1638 if (std::regex_match(colorStr, COLOR_WITH_MAGIC_MINI)) {
1639 colorStr.erase(0, 1);
1640 std::string newColorStr;
1641 // Translate #rgb or #rgba to #rrggbb or #rrggbbaa
1642 for (const auto& c : colorStr) {
1643 newColorStr += c;
1644 newColorStr += c;
1645 }
1646 auto valueMini = stoul(newColorStr, nullptr, COLOR_STRING_BASE);
1647 if (newColorStr.length() < COLOR_STRING_SIZE_STANDARD) {
1648 // No alpha specified, set alpha to 0xff
1649 valueMini |= COLOR_ALPHA_MASK;
1650 } else {
1651 auto alphaMini = valueMini << ALPHA_MOVE;
1652 auto rgbMini = valueMini >> COLOR_MOVE;
1653 valueMini = alphaMini | rgbMini;
1654 }
1655 color = valueMini;
1656 return true;
1657 }
1658 return false;
1659 }
1660
ConvertColorToString(uint32_t color)1661 std::string ConvertColorToString(uint32_t color)
1662 {
1663 HILOG_DEBUG("color is 0X%{public}x", color);
1664 uint32_t rgb = color & (~COLOR_ALPHA_MASK);
1665 uint32_t alpha = (color) >> ALPHA_MOVE;
1666 std::stringstream rgbStream;
1667 rgbStream << std::hex << std::setw(RGB_LENGTH) << std::setfill(UNICODE_BODY) << rgb;
1668 std::stringstream alphaStream;
1669 alphaStream << std::hex << std::setw(ALPHA_LENGTH) << std::setfill(UNICODE_BODY) << alpha;
1670 std::string rgbStr(rgbStream.str());
1671 std::string alphaStr(alphaStream.str());
1672 std::string colorStr = "#" + rgbStr + alphaStr;
1673 HILOG_DEBUG("colorStr is %{public}s", colorStr.c_str());
1674 return colorStr;
1675 }
1676
GetColorValue(napi_env env,napi_value object,napi_value propertyNameValue)1677 uint32_t GetColorValue(napi_env env, napi_value object, napi_value propertyNameValue)
1678 {
1679 uint32_t color = COLOR_TRANSPARENT;
1680 napi_valuetype valueType = napi_undefined;
1681 napi_value value = nullptr;
1682 napi_get_property(env, object, propertyNameValue, &value);
1683 napi_status status = napi_typeof(env, value, &valueType);
1684 if (status != napi_ok) {
1685 HILOG_ERROR("GetColorValue error! status is %{public}d", status);
1686 return color;
1687 }
1688 if (valueType == napi_number) {
1689 napi_get_value_uint32(env, value, &color);
1690 HILOG_DEBUG("valueType number, color is 0x%{public}x", color);
1691 }
1692 if (valueType == napi_string) {
1693 char outBuffer[CHAE_BUFFER_MAX + 1] = {0};
1694 size_t outSize = 0;
1695 napi_get_value_string_utf8(env, value, outBuffer, CHAE_BUFFER_MAX, &outSize);
1696 color = ConvertColorStringToNumer(std::string(outBuffer));
1697 }
1698 HILOG_DEBUG("color is 0x%{public}x", color);
1699 return color;
1700 }
1701
GetColorValue(napi_env env,napi_value value)1702 uint32_t GetColorValue(napi_env env, napi_value value)
1703 {
1704 uint32_t color = COLOR_TRANSPARENT;
1705 napi_valuetype valueType = napi_undefined;
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 HILOG_DEBUG("color type is number");
1713 napi_get_value_uint32(env, value, &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
ConvertObjToCaptionProperty(napi_env env,napi_value object,OHOS::AccessibilityConfig::CaptionProperty * ptrCaptionProperty)1725 bool ConvertObjToCaptionProperty(
1726 napi_env env, napi_value object, OHOS::AccessibilityConfig::CaptionProperty* ptrCaptionProperty)
1727 {
1728 if (!ptrCaptionProperty) {
1729 HILOG_ERROR("ptrCaptionProperty is null.");
1730 return false;
1731 }
1732
1733 bool tmpResult = ConvertObjToCaptionPropertyPart1(env, object, ptrCaptionProperty);
1734 if (!tmpResult) {
1735 return false;
1736 }
1737 tmpResult = ConvertObjToCaptionPropertyPart2(env, object, ptrCaptionProperty);
1738 if (!tmpResult) {
1739 return false;
1740 }
1741 return true;
1742 }
1743
ConvertObjToCaptionPropertyPart1(napi_env env,napi_value object,OHOS::AccessibilityConfig::CaptionProperty * ptrCaptionProperty)1744 bool ConvertObjToCaptionPropertyPart1(
1745 napi_env env, napi_value object, OHOS::AccessibilityConfig::CaptionProperty* ptrCaptionProperty)
1746 {
1747 napi_value propertyNameValue = nullptr;
1748 bool hasProperty = false;
1749 int32_t num = 100;
1750
1751 napi_create_string_utf8(env, "fontFamily", NAPI_AUTO_LENGTH, &propertyNameValue);
1752 std::string fontFamily = ConvertCaptionPropertyJSToNAPI(env, object, propertyNameValue, hasProperty);
1753 if (hasProperty) {
1754 ptrCaptionProperty->SetFontFamily(fontFamily);
1755 } else {
1756 return false;
1757 }
1758
1759 napi_create_string_utf8(env, "fontScale", NAPI_AUTO_LENGTH, &propertyNameValue);
1760 num = ConvertIntJSToNAPI(env, object, propertyNameValue, hasProperty);
1761 if (hasProperty) {
1762 ptrCaptionProperty->SetFontScale(num);
1763 } else {
1764 return false;
1765 }
1766
1767 napi_create_string_utf8(env, "fontColor", NAPI_AUTO_LENGTH, &propertyNameValue);
1768 napi_has_property(env, object, propertyNameValue, &hasProperty);
1769 if (hasProperty) {
1770 ptrCaptionProperty->SetFontColor(GetColorValue(env, object, propertyNameValue));
1771 } else {
1772 return false;
1773 }
1774 return true;
1775 }
1776
ConvertObjToCaptionPropertyPart2(napi_env env,napi_value object,OHOS::AccessibilityConfig::CaptionProperty * ptrCaptionProperty)1777 bool ConvertObjToCaptionPropertyPart2(
1778 napi_env env, napi_value object, OHOS::AccessibilityConfig::CaptionProperty* ptrCaptionProperty)
1779 {
1780 napi_value propertyNameValue = nullptr;
1781 bool hasProperty = false;
1782
1783 napi_create_string_utf8(env, "fontEdgeType", NAPI_AUTO_LENGTH, &propertyNameValue);
1784 std::string fontEdgeType = ConvertCaptionPropertyJSToNAPI(env, object, propertyNameValue, hasProperty);
1785 if (hasProperty) {
1786 ptrCaptionProperty->SetFontEdgeType(fontEdgeType);
1787 } else {
1788 return false;
1789 }
1790
1791 napi_create_string_utf8(env, "backgroundColor", NAPI_AUTO_LENGTH, &propertyNameValue);
1792 napi_has_property(env, object, propertyNameValue, &hasProperty);
1793 if (hasProperty) {
1794 ptrCaptionProperty->SetBackgroundColor(GetColorValue(env, object, propertyNameValue));
1795 } else {
1796 return false;
1797 }
1798
1799 napi_create_string_utf8(env, "windowColor", NAPI_AUTO_LENGTH, &propertyNameValue);
1800 napi_has_property(env, object, propertyNameValue, &hasProperty);
1801 if (hasProperty) {
1802 ptrCaptionProperty->SetWindowColor(GetColorValue(env, object, propertyNameValue));
1803 } else {
1804 return false;
1805 }
1806 return true;
1807 }
1808
ConvertCaptionPropertyJSToNAPI(napi_env env,napi_value object,napi_value propertyNameValue,bool & hasProperty)1809 std::string ConvertCaptionPropertyJSToNAPI(napi_env env, napi_value object,
1810 napi_value propertyNameValue, bool &hasProperty)
1811 {
1812 char outBuffer[CHAE_BUFFER_MAX + 1] = {0};
1813 napi_has_property(env, object, propertyNameValue, &hasProperty);
1814 if (hasProperty) {
1815 napi_value value = nullptr;
1816 size_t outSize = 0;
1817 napi_get_property(env, object, propertyNameValue, &value);
1818 napi_get_value_string_utf8(env, value, outBuffer, CHAE_BUFFER_MAX, &outSize);
1819 }
1820 return std::string(outBuffer);
1821 }
1822
ConvertJSToStringVec(napi_env env,napi_value arrayValue,std::vector<std::string> & values)1823 bool ConvertJSToStringVec(napi_env env, napi_value arrayValue, std::vector<std::string>& values)
1824 {
1825 HILOG_DEBUG();
1826 values.clear();
1827
1828 bool hasElement = true;
1829 for (int32_t i = 0; hasElement; i++) {
1830 napi_has_element(env, arrayValue, i, &hasElement);
1831 if (hasElement) {
1832 napi_value value = nullptr;
1833 napi_status status = napi_get_element(env, arrayValue, i, &value);
1834 if (status != napi_ok) {
1835 return false;
1836 }
1837
1838 char outBuffer[CHAE_BUFFER_MAX + 1] = {0};
1839 size_t outSize = 0;
1840 status = napi_get_value_string_utf8(env, value, outBuffer, CHAE_BUFFER_MAX, &outSize);
1841 if (status != napi_ok) {
1842 return false;
1843 }
1844
1845 values.push_back(std::string(outBuffer));
1846 }
1847 }
1848 return true;
1849 }
1850
ConvertJSToEventTypes(napi_env env,napi_value arrayValue,uint32_t & eventTypes)1851 void ConvertJSToEventTypes(napi_env env, napi_value arrayValue, uint32_t &eventTypes)
1852 {
1853 HILOG_DEBUG();
1854 eventTypes = TYPE_VIEW_INVALID;
1855 std::vector<std::string> values;
1856 ConvertJSToStringVec(env, arrayValue, values);
1857 for (auto &value : values) {
1858 HILOG_DEBUG("the event type is %{public}s", value.c_str());
1859 EventType eventType = ConvertStringToEventInfoTypes(value);
1860 if (eventType == TYPE_VIEW_INVALID) {
1861 HILOG_ERROR("the event type is invalid");
1862 eventTypes = TYPE_VIEW_INVALID;
1863 return;
1864 }
1865 eventTypes |= eventType;
1866 }
1867 }
1868
ConvertJSToCapabilities(napi_env env,napi_value arrayValue,uint32_t & capabilities)1869 bool ConvertJSToCapabilities(napi_env env, napi_value arrayValue, uint32_t &capabilities)
1870 {
1871 HILOG_DEBUG();
1872 capabilities = 0;
1873 std::vector<std::string> values;
1874 ConvertJSToStringVec(env, arrayValue, values);
1875 for (auto &value : values) {
1876 HILOG_DEBUG("capability is %{public}s", value.c_str());
1877 uint32_t capability = ConvertStringToCapability(value);
1878 if (capability == 0) {
1879 HILOG_ERROR("the capability is invalid");
1880 capabilities = 0;
1881 return false;
1882 }
1883 capabilities |= capability;
1884 }
1885 return true;
1886 }
1887
ConvertStringVecToJS(napi_env env,napi_value & result,std::vector<std::string> values)1888 void ConvertStringVecToJS(napi_env env, napi_value &result, std::vector<std::string> values)
1889 {
1890 HILOG_DEBUG();
1891 size_t index = 0;
1892 for (auto& value : values) {
1893 napi_value str = nullptr;
1894 napi_create_string_utf8(env, value.c_str(), value.size(), &str);
1895 napi_set_element(env, result, index, str);
1896 index++;
1897 }
1898 }
1899 } // namespace AccessibilityNapi
1900 } // namespace OHOS
1901