• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_ACCESSIBILITY_ACTION_FUNCTION_UTILS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_ACCESSIBILITY_ACTION_FUNCTION_UTILS_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/accessibility/accessibility_utils.h"
21 #include "core/components_ng/base/ui_node.h"
22 
23 namespace OHOS::Accessibility {
24 }
25 
26 namespace OHOS::Ace::NG {
27 class FrameNode;
28 class accessibilityProperty;
29 
30 class AccessibilityFunctionUtils {
31 public:
CheckCurrentHasFunc(ActionNotifyChildAction & ptr,const RefPtr<FrameNode> & node)32     static bool CheckCurrentHasFunc(ActionNotifyChildAction& ptr, const RefPtr<FrameNode>& node)
33     {
34         ptr = nullptr;
35         CHECK_NULL_RETURN(node, false);
36         auto accessibilityProperty = node->GetAccessibilityProperty<NG::AccessibilityProperty>();
37         CHECK_NULL_RETURN(accessibilityProperty, false);
38         ptr = accessibilityProperty->GetNotifyChildActionFunc();
39         CHECK_NULL_RETURN(ptr, false);
40         return true;
41     }
42 
CheckCurrentHasFunc(ActionAccessibilityActionIntercept & ptr,const RefPtr<FrameNode> & node)43     static bool CheckCurrentHasFunc(ActionAccessibilityActionIntercept& ptr, const RefPtr<FrameNode>& node)
44     {
45         ptr = nullptr;
46         CHECK_NULL_RETURN(node, false);
47         auto accessibilityProperty = node->GetAccessibilityProperty<NG::AccessibilityProperty>();
48         CHECK_NULL_RETURN(accessibilityProperty, false);
49         ptr = accessibilityProperty->GetAccessibilityActionInterceptFunc();
50         CHECK_NULL_RETURN(ptr, false);
51         return true;
52     }
53 
CheckCurrentHasFunc(ActionSecurityClickAction & ptr,const RefPtr<FrameNode> & node)54     static bool CheckCurrentHasFunc(ActionSecurityClickAction& ptr, const RefPtr<FrameNode>& node)
55     {
56         ptr = nullptr;
57         CHECK_NULL_RETURN(node, false);
58         auto accessibilityProperty = node->GetAccessibilityProperty<NG::AccessibilityProperty>();
59         CHECK_NULL_RETURN(accessibilityProperty, false);
60         ptr = accessibilityProperty->GetSecurityClickActionFunc();
61         CHECK_NULL_RETURN(ptr, false);
62         return true;
63     }
64 
65     template <typename T>
CheckAncestorHasFunc(T & ptr,const RefPtr<FrameNode> & node,RefPtr<FrameNode> & parentNode)66     static bool CheckAncestorHasFunc(T& ptr, const RefPtr<FrameNode>& node, RefPtr<FrameNode>& parentNode)
67     {
68         ptr = nullptr;
69         CHECK_NULL_RETURN(node, false);
70         parentNode = node->GetParentFrameNode();
71         while (parentNode) {
72             if (AccessibilityFunctionUtils::CheckCurrentHasFunc(ptr, parentNode)) {
73                 return true;
74             }
75             parentNode = parentNode->GetParentFrameNode();
76         }
77         return false;
78     }
79 
HandleNotifyChildAction(const RefPtr<FrameNode> & node,NotifyChildActionType type)80     static AccessibilityActionResult HandleNotifyChildAction(const RefPtr<FrameNode>& node, NotifyChildActionType type)
81     {
82         auto result = AccessibilityActionResult::ACTION_OK;
83         CHECK_NULL_RETURN(node, result);
84         RefPtr<FrameNode> parentNode = nullptr;
85         RefPtr<FrameNode> childNode = node;
86         ActionNotifyChildAction func = nullptr;
87         while (CheckAncestorHasFunc(func, childNode, parentNode)) {
88             if (func) {
89                 result = func(node, type);
90             }
91             if (result != AccessibilityActionResult::ACTION_RISE) {
92                 break;
93             }
94             childNode = parentNode;
95         }
96         return result;
97     }
98 
HandleAccessibilityActionIntercept(const RefPtr<FrameNode> & node,AccessibilityInterfaceAction action)99     static AccessibilityActionInterceptResult HandleAccessibilityActionIntercept(const RefPtr<FrameNode>& node,
100         AccessibilityInterfaceAction action)
101     {
102         auto result = AccessibilityActionInterceptResult::ACTION_CONTINUE;
103         CHECK_NULL_RETURN(node, result);
104 
105         ActionAccessibilityActionIntercept func = nullptr;
106         auto currentHasFunc = CheckCurrentHasFunc(func, node);
107         CHECK_EQUAL_RETURN(currentHasFunc, false, result);
108         CHECK_NULL_RETURN(func, result);
109         result = func(action);
110         if (result != AccessibilityActionInterceptResult::ACTION_RISE) {
111             return result;
112         }
113 
114         RefPtr<FrameNode> parentNode = nullptr;
115         if (CheckAncestorHasFunc(func, node, parentNode)) {
116             return HandleAccessibilityActionIntercept(parentNode, action);
117         } else {
118             return AccessibilityActionInterceptResult::ACTION_CONTINUE;
119         }
120     }
121 
HandleClickBySecComponent(const RefPtr<FrameNode> & node,const SecCompEnhanceEvent & secEvent)122     static bool HandleClickBySecComponent(const RefPtr<FrameNode>& node, const SecCompEnhanceEvent& secEvent)
123     {
124         CHECK_NULL_RETURN(node, false);
125         ActionSecurityClickAction func = nullptr;
126         if (!CheckCurrentHasFunc(func, node)) {
127             return false;
128         }
129         CHECK_NULL_RETURN(func, false);
130         func(secEvent);
131         return true;
132     }
133 };
134 } // namespace OHOS::Ace::NG
135 
136 #endif // FOUNDATION_ACE_FRAMEWORKS_COMPONENTS_NG_PROPERTIES_ACCESSIBILITY_ACTION_FUNCTION_UTILS_H
137