• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     RefPtr<WebAccessibilityCacheImpl::WeakHandle> weakHandle = adoptRef(new WebAccessibilityCacheImpl::WeakHandle(object));
67     weakHandle->m_object->setWrapper(weakHandle.get());
68 
69     return weakHandle.release();
70 }
71 
WeakHandle(AccessibilityObject * object)72 WebAccessibilityCacheImpl::WeakHandle::WeakHandle(AccessibilityObject* object)
73     : AccessibilityObjectWrapper(object)
74 {
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 
remove(int id)121 void WebAccessibilityCacheImpl::remove(int id)
122 {
123     ObjectMap::iterator it = m_objectMap.find(id);
124 
125     if (it == m_objectMap.end())
126         return;
127 
128     if (it->second) {
129         // Erase element from reverse hashmap.
130         IdMap::iterator it2 = m_idMap.find(it->second->accessibilityObject());
131         if (it2 != m_idMap.end())
132             m_idMap.remove(it2);
133     }
134 
135     m_objectMap.remove(it);
136 }
137 
clear()138 void WebAccessibilityCacheImpl::clear()
139 {
140     m_objectMap.clear();
141     m_idMap.clear();
142 }
143 
addOrGetId(const WebAccessibilityObject & object)144 int WebAccessibilityCacheImpl::addOrGetId(const WebAccessibilityObject& object)
145 {
146     if (!object.isValid())
147         return invalidObjectId;
148 
149     RefPtr<AccessibilityObject> o = toAccessibilityObject(object);
150 
151     IdMap::iterator it = m_idMap.find(o.get());
152 
153     if (it != m_idMap.end())
154         return it->second;
155 
156     // Insert new accessibility object in hashmaps and return its newly
157     // assigned accessibility object id.
158     m_objectMap.set(m_nextNewId, WeakHandle::create(o.get()));
159     m_idMap.set(o.get(), m_nextNewId);
160 
161     return m_nextNewId++;
162 }
163 
isCached(const WebAccessibilityObject & object)164 bool WebAccessibilityCacheImpl::isCached(const WebAccessibilityObject& object)
165 {
166     if (!object.isValid())
167         return false;
168 
169     RefPtr<AccessibilityObject> o = toAccessibilityObject(object);
170     IdMap::iterator it = m_idMap.find(o.get());
171     if (it == m_idMap.end())
172         return false;
173 
174     return true;
175 }
176 
177 }
178