1/*
2 * Copyright (C) 2010 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "AccessibilityMenuList.h"
28
29#include "AXObjectCache.h"
30#include "AccessibilityMenuListPopup.h"
31#include "RenderMenuList.h"
32
33namespace WebCore {
34
35AccessibilityMenuList::AccessibilityMenuList(RenderMenuList* renderer)
36 : AccessibilityRenderObject(renderer)
37{
38}
39
40Ref<AccessibilityMenuList> AccessibilityMenuList::create(RenderMenuList* renderer)
41{
42 return adoptRef(*new AccessibilityMenuList(renderer));
43}
44
45bool AccessibilityMenuList::press()
46{
47#if !PLATFORM(IOS_FAMILY)
48 auto element = this->element();
49 AXObjectCache::AXNotification notification = AXObjectCache::AXPressDidFail;
50 if (element && !element->isDisabledFormControl() && is<RenderMenuList>(renderer())) {
51 RenderMenuList* menuList = downcast<RenderMenuList>(renderer());
52 if (menuList->popupIsVisible())
53 menuList->hidePopup();
54 else
55 menuList->showPopup();
56 notification = AXObjectCache::AXPressDidSucceed;
57 }
58 if (auto cache = axObjectCache())
59 cache->postNotification(element, notification);
60 return true;
61#endif
62 return false;
63}
64
65void AccessibilityMenuList::addChildren()
66{
67 if (!m_renderer)
68 return;
69
70 AXObjectCache* cache = axObjectCache();
71 if (!cache)
72 return;
73
74 AccessibilityObject* list = cache->getOrCreate(AccessibilityRole::MenuListPopup);
75 if (!list)
76 return;
77
78 downcast<AccessibilityMockObject>(*list).setParent(this);
79 if (list->accessibilityIsIgnored()) {
80 cache->remove(list->axObjectID());
81 return;
82 }
83
84 m_haveChildren = true;
85 m_children.append(list);
86
87 list->addChildren();
88}
89
90void AccessibilityMenuList::childrenChanged()
91{
92 if (m_children.isEmpty())
93 return;
94
95 ASSERT(m_children.size() == 1);
96 m_children[0]->childrenChanged();
97}
98
99bool AccessibilityMenuList::isCollapsed() const
100{
101#if !PLATFORM(IOS_FAMILY)
102 return !static_cast<RenderMenuList*>(renderer())->popupIsVisible();
103#else
104 return true;
105#endif
106}
107
108bool AccessibilityMenuList::canSetFocusAttribute() const
109{
110 if (!node())
111 return false;
112
113 return !downcast<Element>(*node()).isDisabledFormControl();
114}
115
116void AccessibilityMenuList::didUpdateActiveOption(int optionIndex)
117{
118 Ref<Document> document(m_renderer->document());
119 AXObjectCache* cache = document->axObjectCache();
120
121 const auto& childObjects = children();
122 if (!childObjects.isEmpty()) {
123 ASSERT(childObjects.size() == 1);
124 ASSERT(is<AccessibilityMenuListPopup>(*childObjects[0]));
125
126 // We might be calling this method in situations where the renderers for list items
127 // associated to the menu list have not been created (e.g. they might be rendered
128 // in the UI process, as it's the case in the GTK+ port, which uses GtkMenuItem).
129 // So, we need to make sure that the accessibility popup object has some children
130 // before asking it to update its active option, or it will read invalid memory.
131 // You can reproduce the issue in the GTK+ port by removing this check and running
132 // accessibility/insert-selected-option-into-select-causes-crash.html (will crash).
133 int popupChildrenSize = static_cast<int>(childObjects[0]->children().size());
134 if (is<AccessibilityMenuListPopup>(*childObjects[0]) && optionIndex >= 0 && optionIndex < popupChildrenSize)
135 downcast<AccessibilityMenuListPopup>(*childObjects[0]).didUpdateActiveOption(optionIndex);
136 }
137
138 cache->postNotification(this, document.ptr(), AXObjectCache::AXMenuListValueChanged, TargetElement, PostSynchronously);
139}
140
141} // namespace WebCore
142