browser_accessibility_manager.h revision 513209b27ff55e2841eac0e4120199c23acce758
1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_MANAGER_H_
6#define CHROME_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_MANAGER_H_
7#pragma once
8
9#include <vector>
10
11#include "base/hash_tables.h"
12#include "base/scoped_ptr.h"
13#include "build/build_config.h"
14#include "chrome/common/render_messages_params.h"
15#include "gfx/native_widget_types.h"
16#include "webkit/glue/webaccessibility.h"
17
18class BrowserAccessibility;
19#if defined(OS_WIN)
20class BrowserAccessibilityManagerWin;
21#endif
22
23using webkit_glue::WebAccessibility;
24
25// Class that can perform actions on behalf of the BrowserAccessibilityManager.
26class BrowserAccessibilityDelegate {
27 public:
28  virtual ~BrowserAccessibilityDelegate() {}
29  virtual void SetAccessibilityFocus(int acc_obj_id) = 0;
30  virtual void AccessibilityDoDefaultAction(int acc_obj_id) = 0;
31  virtual bool HasFocus() = 0;
32  virtual gfx::Rect GetViewBounds() const = 0;
33};
34
35class BrowserAccessibilityFactory {
36 public:
37  virtual ~BrowserAccessibilityFactory() {}
38
39  // Create an instance of BrowserAccessibility and return a new
40  // reference to it.
41  virtual BrowserAccessibility* Create();
42};
43
44// Manages a tree of BrowserAccessibility objects.
45class BrowserAccessibilityManager {
46 public:
47  // Creates the platform specific BrowserAccessibilityManager. Ownership passes
48  // to the caller.
49  static BrowserAccessibilityManager* Create(
50    gfx::NativeView parent_view,
51    const WebAccessibility& src,
52    BrowserAccessibilityDelegate* delegate,
53    BrowserAccessibilityFactory* factory = new BrowserAccessibilityFactory());
54
55  virtual ~BrowserAccessibilityManager();
56
57  virtual void NotifyAccessibilityEvent(
58      ViewHostMsg_AccessibilityNotification_Params::NotificationType n,
59      BrowserAccessibility* node) = 0;
60
61  // Returns the next unique child id.
62  static int32 GetNextChildID();
63
64  // Return a pointer to the root of the tree, does not make a new reference.
65  BrowserAccessibility* GetRoot();
66
67  // Removes the BrowserAccessibility child_id from the manager.
68  void Remove(int32 child_id);
69
70  // Return a pointer to the object corresponding to the given child_id,
71  // does not make a new reference.
72  BrowserAccessibility* GetFromChildID(int32 child_id);
73
74  // Called to notify the accessibility manager that its associated native
75  // view got focused.
76  void GotFocus();
77
78  // Tell the renderer to set focus to this node.
79  void SetFocus(const BrowserAccessibility& node);
80
81  // Tell the renderer to do the default action for this node.
82  void DoDefaultAction(const BrowserAccessibility& node);
83
84  // Retrieve the bounds of the parent View in screen coordinates.
85  gfx::Rect GetViewBounds();
86
87  // Called when the renderer process has notified us of about tree changes.
88  // Send a notification to MSAA clients of the change.
89  void OnAccessibilityNotifications(
90    const std::vector<ViewHostMsg_AccessibilityNotification_Params>& params);
91
92  gfx::NativeView GetParentView();
93
94#if defined(OS_WIN)
95  BrowserAccessibilityManagerWin* toBrowserAccessibilityManagerWin();
96#endif
97
98  // Return the object that has focus, if it's a descandant of the
99  // given root (inclusive). Does not make a new reference.
100  BrowserAccessibility* GetFocus(BrowserAccessibility* root);
101
102 protected:
103  BrowserAccessibilityManager(
104      gfx::NativeView parent_view,
105      const WebAccessibility& src,
106      BrowserAccessibilityDelegate* delegate,
107      BrowserAccessibilityFactory* factory);
108
109 private:
110  void OnAccessibilityObjectStateChange(
111      const WebAccessibility& acc_obj);
112  void OnAccessibilityObjectChildrenChange(
113      const WebAccessibility& acc_obj);
114  void OnAccessibilityObjectFocusChange(
115      const WebAccessibility& acc_obj);
116  void OnAccessibilityObjectLoadComplete(
117      const WebAccessibility& acc_obj);
118  void OnAccessibilityObjectValueChange(
119      const WebAccessibility& acc_obj);
120  void OnAccessibilityObjectTextChange(
121      const WebAccessibility& acc_obj);
122
123  // Recursively compare the IDs of our subtree to a new subtree received
124  // from the renderer and return true if their IDs match exactly.
125  bool CanModifyTreeInPlace(
126      BrowserAccessibility* current_root,
127      const WebAccessibility& new_root);
128
129  // Recursively modify a subtree (by reinitializing) to match a new
130  // subtree received from the renderer process. Should only be called
131  // if CanModifyTreeInPlace returned true.
132  void ModifyTreeInPlace(
133      BrowserAccessibility* current_root,
134      const WebAccessibility& new_root);
135
136  // Update an accessibility node with an updated WebAccessibility node
137  // received from the renderer process. When |include_children| is true
138  // the node's children will also be updated, otherwise only the node
139  // itself is updated. Returns the updated node or NULL if no node was
140  // updated.
141  BrowserAccessibility* UpdateNode(
142      const WebAccessibility& acc_obj,
143      bool include_children);
144
145  // Recursively build a tree of BrowserAccessibility objects from
146  // the WebAccessibility tree received from the renderer process.
147  BrowserAccessibility* CreateAccessibilityTree(
148      BrowserAccessibility* parent,
149      int child_id,
150      const WebAccessibility& src,
151      int index_in_parent);
152
153 protected:
154  // The next unique id for a BrowserAccessibility instance.
155  static int32 next_child_id_;
156
157  // The parent view.
158  gfx::NativeView parent_view_;
159
160  // The object that can perform actions on our behalf.
161  BrowserAccessibilityDelegate* delegate_;
162
163  // Factory to create BrowserAccessibility objects (for dependency injection).
164  scoped_ptr<BrowserAccessibilityFactory> factory_;
165
166  // The root of the tree of IAccessible objects and the element that
167  // currently has focus, if any.
168  BrowserAccessibility* root_;
169  BrowserAccessibility* focus_;
170
171  // A mapping from the IDs of objects in the renderer, to the child IDs
172  // we use internally here.
173  base::hash_map<int32, int32> renderer_id_to_child_id_map_;
174
175  // A mapping from child IDs to BrowserAccessibility objects.
176  base::hash_map<int32, BrowserAccessibility*> child_id_map_;
177
178  DISALLOW_COPY_AND_ASSIGN(BrowserAccessibilityManager);
179};
180
181#endif  // CHROME_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_MANAGER_H_
182