1/*
2* Copyright (C) 2019 Apple Inc. All rights reserved.
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions
6* are met:
7* 1. Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* 2. Redistributions in binary form must reproduce the above copyright
10* notice, this list of conditions and the following disclaimer in the
11* documentation and/or other materials provided with the distribution.
12*
13* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23* THE POSSIBILITY OF SUCH DAMAGE.
24*/
25
26#pragma once
27
28#if ENABLE(ACCESSIBILITY_ISOLATED_TREE)
29
30#include "AXIsolatedTree.h"
31#include "AccessibilityObjectInterface.h"
32#include "FloatRect.h"
33#include "IntPoint.h"
34#include <wtf/Forward.h>
35#include <wtf/HashMap.h>
36#include <wtf/RefPtr.h>
37#include <wtf/RetainPtr.h>
38#include <wtf/ThreadSafeRefCounted.h>
39#include <wtf/Variant.h>
40#include <wtf/Vector.h>
41#include <wtf/WeakPtr.h>
42
43namespace WebCore {
44
45class AXIsolatedTree;
46class AccessibilityObject;
47
48class AXIsolatedTreeNode final : public AccessibilityObjectInterface, public ThreadSafeRefCounted<AXIsolatedTreeNode>, public CanMakeWeakPtr<AXIsolatedTreeNode> {
49
50public:
51 enum class AXPropertyName : uint8_t {
52 None = 0,
53 HelpText,
54 IsAccessibilityIgnored,
55 IsAttachment,
56 IsFileUploadButton,
57 IsImage,
58 IsImageMapLink,
59 IsLink,
60 IsMediaControlLabel,
61 IsScrollbar,
62 IsTree,
63 IsTreeItem,
64 Description,
65 RelativeFrame,
66 RoleValue,
67 SpeechHint,
68 Title,
69 };
70
71 static Ref<AXIsolatedTreeNode> create(const AccessibilityObject&);
72 virtual ~AXIsolatedTreeNode();
73
74 AXID identifier() const { return m_identifier; }
75
76 void setParent(AXID);
77 AXID parent() const { return m_parent; }
78
79 void appendChild(AXID);
80 const Vector<AXID>& children() const { return m_children; };
81
82 AXIsolatedTree* tree() const;
83 AXIsolatedTreeID treeIdentifier() const { return m_treeIdentifier; }
84 void setTreeIdentifier(AXIsolatedTreeID);
85
86#if PLATFORM(COCOA)
87 AccessibilityObjectWrapper* wrapper() const override { return m_wrapper.get(); }
88 void setWrapper(AccessibilityObjectWrapper* wrapper) { m_wrapper = wrapper; }
89#endif
90
91protected:
92 AXIsolatedTreeNode() = default;
93
94private:
95 AXIsolatedTreeNode(const AccessibilityObject&);
96 void initializeAttributeData(const AccessibilityObject&);
97
98 AccessibilityObjectInterface* accessibilityHitTest(const IntPoint&) const override;
99 void updateChildrenIfNecessary() override { }
100
101 bool isMediaControlLabel() const override { return boolAttributeValue(AXPropertyName::IsMediaControlLabel); }
102 bool isAttachment() const override { return boolAttributeValue(AXPropertyName::IsAttachment); }
103 AccessibilityRole roleValue() const override { return static_cast<AccessibilityRole>(intAttributeValue(AXPropertyName::RoleValue)); }
104 bool isLink() const override { return boolAttributeValue(AXPropertyName::IsLink); }
105 bool isImageMapLink() const override { return boolAttributeValue(AXPropertyName::IsImageMapLink); }
106 bool isImage() const override { return boolAttributeValue(AXPropertyName::IsImage); }
107 bool isFileUploadButton() const override { return boolAttributeValue(AXPropertyName::IsFileUploadButton); }
108 bool accessibilityIsIgnored() const override { return boolAttributeValue(AXPropertyName::IsAccessibilityIgnored); }
109 AccessibilityObjectInterface* parentObjectInterfaceUnignored() const override;
110 bool isTree() const override { return boolAttributeValue(AXPropertyName::IsTree); }
111 bool isTreeItem() const override { return boolAttributeValue(AXPropertyName::IsTreeItem); }
112 bool isScrollbar() const override { return boolAttributeValue(AXPropertyName::IsScrollbar); }
113 FloatRect relativeFrame() const override { return rectAttributeValue(AXPropertyName::RelativeFrame); }
114 String speechHintAttributeValue() const override { return stringAttributeValue(AXPropertyName::SpeechHint); }
115 String descriptionAttributeValue() const override { return stringAttributeValue(AXPropertyName::Description); }
116 String helpTextAttributeValue() const override { return stringAttributeValue(AXPropertyName::HelpText); }
117 String titleAttributeValue() const override { return stringAttributeValue(AXPropertyName::Title); }
118 AccessibilityObjectInterface* focusedUIElement() const override;
119
120 using AttributeValueVariant = Variant<std::nullptr_t, String, bool, int, unsigned, double, Optional<FloatRect>>;
121 void setProperty(AXPropertyName, AttributeValueVariant&&, bool shouldRemove = false);
122
123 bool boolAttributeValue(AXPropertyName) const;
124 const String stringAttributeValue(AXPropertyName) const;
125 int intAttributeValue(AXPropertyName) const;
126 unsigned unsignedAttributeValue(AXPropertyName) const;
127 double doubleAttributeValue(AXPropertyName) const;
128 FloatRect rectAttributeValue(AXPropertyName) const;
129
130 AXID m_parent;
131 AXID m_identifier;
132 bool m_initialized { false };
133 AXIsolatedTreeID m_treeIdentifier;
134 WeakPtr<AXIsolatedTree> m_cachedTree;
135 Vector<AXID> m_children;
136
137#if PLATFORM(COCOA)
138 RetainPtr<WebAccessibilityObjectWrapper> m_wrapper;
139#endif
140
141 HashMap<AXPropertyName, AttributeValueVariant, WTF::IntHash<AXPropertyName>, WTF::StrongEnumHashTraits<AXPropertyName>> m_attributeMap;
142};
143
144} // namespace WebCore
145
146#endif // ENABLE((ACCESSIBILITY_ISOLATED_TREE))
147