Home | History | Annotate | Download | only in accessibility
      1 /*
      2  * Copyright (C) 2003, 2006, 2007, 2008, 2009, 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 COMPUTER, 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 COMPUTER, 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 #ifndef AXObjectCache_h
     27 #define AXObjectCache_h
     28 
     29 #include "AccessibilityObject.h"
     30 #include "EventHandler.h"
     31 #include "Timer.h"
     32 #include <limits.h>
     33 #include <wtf/HashMap.h>
     34 #include <wtf/HashSet.h>
     35 #include <wtf/RefPtr.h>
     36 
     37 #ifdef __OBJC__
     38 @class WebCoreTextMarker;
     39 #else
     40 class WebCoreTextMarker;
     41 #endif
     42 
     43 namespace WebCore {
     44 
     45 class HTMLAreaElement;
     46 class Node;
     47 class Page;
     48 class RenderObject;
     49 class String;
     50 class VisiblePosition;
     51 
     52 struct TextMarkerData {
     53     AXID axID;
     54     Node* node;
     55     int offset;
     56     EAffinity affinity;
     57 };
     58 
     59 enum PostType { PostSynchronously, PostAsynchronously };
     60 
     61 class AXObjectCache : public Noncopyable {
     62 public:
     63     AXObjectCache();
     64     ~AXObjectCache();
     65 
     66     static AccessibilityObject* focusedUIElementForPage(const Page*);
     67 
     68     // to be used with render objects
     69     AccessibilityObject* getOrCreate(RenderObject*);
     70 
     71     // used for objects without backing elements
     72     AccessibilityObject* getOrCreate(AccessibilityRole);
     73 
     74     // will only return the AccessibilityObject if it already exists
     75     AccessibilityObject* get(RenderObject*);
     76 
     77     void remove(RenderObject*);
     78     void remove(AXID);
     79 
     80     void detachWrapper(AccessibilityObject*);
     81     void attachWrapper(AccessibilityObject*);
     82     void childrenChanged(RenderObject*);
     83     void selectedChildrenChanged(RenderObject*);
     84     // Called by a node when text or a text equivalent (e.g. alt) attribute is changed.
     85     void contentChanged(RenderObject*);
     86 
     87     void handleActiveDescendantChanged(RenderObject*);
     88     void handleAriaRoleChanged(RenderObject*);
     89     void handleFocusedUIElementChanged(RenderObject* oldFocusedRenderer, RenderObject* newFocusedRenderer);
     90     void handleScrolledToAnchor(const Node* anchorNode);
     91 
     92     static void enableAccessibility() { gAccessibilityEnabled = true; }
     93     static void enableEnhancedUserInterfaceAccessibility() { gAccessibilityEnhancedUserInterfaceEnabled = true; }
     94 
     95     static bool accessibilityEnabled() { return gAccessibilityEnabled; }
     96     static bool accessibilityEnhancedUserInterfaceEnabled() { return gAccessibilityEnhancedUserInterfaceEnabled; }
     97 
     98     void removeAXID(AccessibilityObject*);
     99     bool isIDinUse(AXID id) const { return m_idsInUse.contains(id); }
    100     AXID platformGenerateAXID() const;
    101     AccessibilityObject* objectFromAXID(AXID id) const { return m_objects.get(id).get(); }
    102 
    103     // Text marker utilities.
    104     static void textMarkerDataForVisiblePosition(TextMarkerData&, const VisiblePosition&);
    105     static VisiblePosition visiblePositionForTextMarkerData(TextMarkerData&);
    106 
    107     enum AXNotification {
    108         AXActiveDescendantChanged,
    109         AXCheckedStateChanged,
    110         AXFocusedUIElementChanged,
    111         AXLayoutComplete,
    112         AXLoadComplete,
    113         AXSelectedChildrenChanged,
    114         AXSelectedTextChanged,
    115         AXValueChanged,
    116         AXScrolledToAnchor,
    117         AXLiveRegionChanged,
    118         AXMenuListValueChanged,
    119     };
    120 
    121     void postNotification(RenderObject*, AXNotification, bool postToElement, PostType = PostAsynchronously);
    122     void postNotification(AccessibilityObject*, Document*, AXNotification, bool postToElement, PostType = PostAsynchronously);
    123 
    124 protected:
    125     void postPlatformNotification(AccessibilityObject*, AXNotification);
    126 
    127 private:
    128     HashMap<AXID, RefPtr<AccessibilityObject> > m_objects;
    129     HashMap<RenderObject*, AXID> m_renderObjectMapping;
    130     static bool gAccessibilityEnabled;
    131     static bool gAccessibilityEnhancedUserInterfaceEnabled;
    132 
    133     HashSet<AXID> m_idsInUse;
    134 
    135     Timer<AXObjectCache> m_notificationPostTimer;
    136     Vector<pair<RefPtr<AccessibilityObject>, AXNotification> > m_notificationsToPost;
    137     void notificationPostTimerFired(Timer<AXObjectCache>*);
    138 
    139     static AccessibilityObject* focusedImageMapUIElement(HTMLAreaElement*);
    140 
    141     AXID getAXID(AccessibilityObject*);
    142     bool nodeIsAriaType(Node*, String role);
    143 };
    144 
    145 #if !HAVE(ACCESSIBILITY)
    146 inline void AXObjectCache::handleActiveDescendantChanged(RenderObject*) { }
    147 inline void AXObjectCache::handleAriaRoleChanged(RenderObject*) { }
    148 inline void AXObjectCache::detachWrapper(AccessibilityObject*) { }
    149 inline void AXObjectCache::attachWrapper(AccessibilityObject*) { }
    150 inline void AXObjectCache::selectedChildrenChanged(RenderObject*) { }
    151 inline void AXObjectCache::postNotification(RenderObject*, AXNotification, bool postToElement, PostType) { }
    152 inline void AXObjectCache::postPlatformNotification(AccessibilityObject*, AXNotification) { }
    153 inline void AXObjectCache::handleFocusedUIElementChanged(RenderObject*, RenderObject*) { }
    154 inline void AXObjectCache::handleScrolledToAnchor(const Node*) { }
    155 inline void AXObjectCache::contentChanged(RenderObject*) { }
    156 #endif
    157 
    158 }
    159 
    160 #endif
    161