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