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