1 /*
2 * Copyright (C) 2009 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "WebAccessibilityCacheImpl.h"
33
34 #include "AccessibilityObject.h"
35 #include "AXObjectCache.h"
36 #include "Document.h"
37 #include "Frame.h"
38
39 #include "WebAccessibilityObject.h"
40 #include "WebFrameImpl.h"
41 #include "WebViewImpl.h"
42
43 using namespace WebCore;
44
45 namespace WebKit {
46
47 const int invalidObjectId = -1;
48 const int firstObjectId = 1000;
49
toAccessibilityObject(const WebAccessibilityObject & object)50 static PassRefPtr<AccessibilityObject> toAccessibilityObject(const WebAccessibilityObject& object)
51 {
52 return object;
53 }
54
55 // WebView ----------------------------------------------------------------
56
create()57 WebAccessibilityCache* WebAccessibilityCache::create()
58 {
59 return new WebAccessibilityCacheImpl();
60 }
61
62 // WeakHandle -------------------------------------------------------------
63
create(AccessibilityObject * object)64 PassRefPtr<WebAccessibilityCacheImpl::WeakHandle> WebAccessibilityCacheImpl::WeakHandle::create(AccessibilityObject* object)
65 {
66 // FIXME: Remove resetting ref-count from AccessibilityObjectWrapper
67 // and convert to use adoptRef.
68 return new WebAccessibilityCacheImpl::WeakHandle(object);
69 }
70
WeakHandle(AccessibilityObject * object)71 WebAccessibilityCacheImpl::WeakHandle::WeakHandle(AccessibilityObject* object)
72 : AccessibilityObjectWrapper(object)
73 {
74 m_object->setWrapper(this);
75 }
76
77 // WebAccessibilityCacheImpl ----------------------------------------
78
detach()79 void WebAccessibilityCacheImpl::WeakHandle::detach()
80 {
81 if (m_object)
82 m_object = 0;
83 }
84
WebAccessibilityCacheImpl()85 WebAccessibilityCacheImpl::WebAccessibilityCacheImpl()
86 : m_nextNewId(firstObjectId)
87 , m_initialized(false)
88 {
89 }
90
~WebAccessibilityCacheImpl()91 WebAccessibilityCacheImpl::~WebAccessibilityCacheImpl()
92 {
93 }
94
initialize(WebView * view)95 void WebAccessibilityCacheImpl::initialize(WebView* view)
96 {
97 AXObjectCache::enableAccessibility();
98 WebAccessibilityObject root = view->accessibilityObject();
99 if (root.isNull())
100 return;
101
102 RefPtr<AccessibilityObject> rootObject = toAccessibilityObject(root);
103
104 // Insert root in hashmaps.
105 m_objectMap.set(m_nextNewId, WeakHandle::create(rootObject.get()));
106 m_idMap.set(rootObject.get(), m_nextNewId++);
107
108 m_initialized = true;
109 }
110
getObjectById(int id)111 WebAccessibilityObject WebAccessibilityCacheImpl::getObjectById(int id)
112 {
113 ObjectMap::iterator it = m_objectMap.find(id);
114
115 if (it == m_objectMap.end() || !it->second)
116 return WebAccessibilityObject();
117
118 return WebAccessibilityObject(it->second->accessibilityObject());
119 }
120
isValidId(int id) const121 bool WebAccessibilityCacheImpl::isValidId(int id) const
122 {
123 return id >= firstObjectId;
124 }
125
remove(int id)126 void WebAccessibilityCacheImpl::remove(int id)
127 {
128 ObjectMap::iterator it = m_objectMap.find(id);
129
130 if (it == m_objectMap.end())
131 return;
132
133 if (it->second) {
134 // Erase element from reverse hashmap.
135 IdMap::iterator it2 = m_idMap.find(it->second->accessibilityObject());
136 if (it2 != m_idMap.end())
137 m_idMap.remove(it2);
138 }
139
140 m_objectMap.remove(it);
141 }
142
clear()143 void WebAccessibilityCacheImpl::clear()
144 {
145 m_objectMap.clear();
146 m_idMap.clear();
147 }
148
addOrGetId(const WebAccessibilityObject & object)149 int WebAccessibilityCacheImpl::addOrGetId(const WebAccessibilityObject& object)
150 {
151 if (object.isNull())
152 return invalidObjectId;
153
154 RefPtr<AccessibilityObject> o = toAccessibilityObject(object);
155
156 IdMap::iterator it = m_idMap.find(o.get());
157
158 if (it != m_idMap.end())
159 return it->second;
160
161 // Insert new accessibility object in hashmaps and return its newly
162 // assigned accessibility object id.
163 m_objectMap.set(m_nextNewId, WeakHandle::create(o.get()));
164 m_idMap.set(o.get(), m_nextNewId);
165
166 return m_nextNewId++;
167 }
168
169 }
170