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