WebKit/chromium/src/WebAccessibilityCacheImpl.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 
       
    50 static PassRefPtr<AccessibilityObject> toAccessibilityObject(const WebAccessibilityObject& object)
       
    51 {
       
    52     return object;
       
    53 }
       
    54 
       
    55 // WebView ----------------------------------------------------------------
       
    56 
       
    57 WebAccessibilityCache* WebAccessibilityCache::create()
       
    58 {
       
    59     return new WebAccessibilityCacheImpl();
       
    60 }
       
    61 
       
    62 // WeakHandle -------------------------------------------------------------
       
    63 
       
    64 PassRefPtr<WebAccessibilityCacheImpl::WeakHandle> WebAccessibilityCacheImpl::WeakHandle::create(AccessibilityObject* object)
       
    65 {
       
    66     // FIXME: Remove resetting ref-count from AccessibilityObjectWrapper
       
    67     RefPtr<WebAccessibilityCacheImpl::WeakHandle> weakHandle = adoptRef(new WebAccessibilityCacheImpl::WeakHandle(object));
       
    68     weakHandle->m_object->setWrapper(weakHandle.get());
       
    69     
       
    70     return weakHandle.release();
       
    71 }
       
    72 
       
    73 WebAccessibilityCacheImpl::WeakHandle::WeakHandle(AccessibilityObject* object)
       
    74     : AccessibilityObjectWrapper(object)
       
    75 {
       
    76 }
       
    77 
       
    78 // WebAccessibilityCacheImpl ----------------------------------------
       
    79 
       
    80 void WebAccessibilityCacheImpl::WeakHandle::detach()
       
    81 {
       
    82     if (m_object)
       
    83         m_object = 0;
       
    84 }
       
    85 
       
    86 WebAccessibilityCacheImpl::WebAccessibilityCacheImpl()
       
    87     : m_nextNewId(firstObjectId)
       
    88     , m_initialized(false)
       
    89 {
       
    90 }
       
    91 
       
    92 WebAccessibilityCacheImpl::~WebAccessibilityCacheImpl()
       
    93 {
       
    94 }
       
    95 
       
    96 void WebAccessibilityCacheImpl::initialize(WebView* view)
       
    97 {
       
    98     AXObjectCache::enableAccessibility();
       
    99     WebAccessibilityObject root = view->accessibilityObject();
       
   100     if (root.isNull())
       
   101         return;
       
   102 
       
   103     RefPtr<AccessibilityObject> rootObject = toAccessibilityObject(root);
       
   104 
       
   105     // Insert root in hashmaps.
       
   106     m_objectMap.set(m_nextNewId, WeakHandle::create(rootObject.get()));
       
   107     m_idMap.set(rootObject.get(), m_nextNewId++);
       
   108 
       
   109     m_initialized = true;
       
   110 }
       
   111 
       
   112 WebAccessibilityObject WebAccessibilityCacheImpl::getObjectById(int id)
       
   113 {
       
   114     ObjectMap::iterator it = m_objectMap.find(id);
       
   115 
       
   116     if (it == m_objectMap.end() || !it->second)
       
   117         return WebAccessibilityObject();
       
   118 
       
   119     return WebAccessibilityObject(it->second->accessibilityObject());
       
   120 }
       
   121 
       
   122 bool WebAccessibilityCacheImpl::isValidId(int id) const
       
   123 {
       
   124     return id >= firstObjectId;
       
   125 }
       
   126 
       
   127 void WebAccessibilityCacheImpl::remove(int id)
       
   128 {
       
   129     ObjectMap::iterator it = m_objectMap.find(id);
       
   130 
       
   131     if (it == m_objectMap.end())
       
   132         return;
       
   133 
       
   134     if (it->second) {
       
   135         // Erase element from reverse hashmap.
       
   136         IdMap::iterator it2 = m_idMap.find(it->second->accessibilityObject());
       
   137         if (it2 != m_idMap.end())
       
   138             m_idMap.remove(it2);
       
   139     }
       
   140 
       
   141     m_objectMap.remove(it);
       
   142 }
       
   143 
       
   144 void WebAccessibilityCacheImpl::clear()
       
   145 {
       
   146     m_objectMap.clear();
       
   147     m_idMap.clear();
       
   148 }
       
   149 
       
   150 int WebAccessibilityCacheImpl::addOrGetId(const WebAccessibilityObject& object)
       
   151 {
       
   152     if (object.isNull())
       
   153         return invalidObjectId;
       
   154 
       
   155     RefPtr<AccessibilityObject> o = toAccessibilityObject(object);
       
   156 
       
   157     IdMap::iterator it = m_idMap.find(o.get());
       
   158 
       
   159     if (it != m_idMap.end())
       
   160         return it->second;
       
   161 
       
   162     // Insert new accessibility object in hashmaps and return its newly
       
   163     // assigned accessibility object id.
       
   164     m_objectMap.set(m_nextNewId, WeakHandle::create(o.get()));
       
   165     m_idMap.set(o.get(), m_nextNewId);
       
   166 
       
   167     return m_nextNewId++;
       
   168 }
       
   169 
       
   170 }