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