[email protected] | c5c2a67 | 2010-10-01 23:28:04 | [diff] [blame^] | 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_WIN_H_ |
| 6 | #define CHROME_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_MANAGER_WIN_H_ |
| 7 | #pragma once |
| 8 | |
| 9 | #include <atlbase.h> |
| 10 | #include <atlcom.h> |
| 11 | #include <oleacc.h> |
| 12 | |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "chrome/browser/accessibility/browser_accessibility_manager.h" |
| 16 | #include "base/hash_tables.h" |
| 17 | #include "base/scoped_comptr_win.h" |
| 18 | #include "base/scoped_ptr.h" |
| 19 | #include "webkit/glue/webaccessibility.h" |
| 20 | |
| 21 | class BrowserAccessibilityWin; |
| 22 | struct ViewHostMsg_AccessibilityNotification_Params; |
| 23 | |
| 24 | class BrowserAccessibilityWinFactory { |
| 25 | public: |
| 26 | virtual ~BrowserAccessibilityWinFactory() {} |
| 27 | |
| 28 | // Create an instance of BrowserAccessibilityWin and return a new |
| 29 | // reference to it. |
| 30 | virtual BrowserAccessibilityWin* Create(); |
| 31 | }; |
| 32 | |
| 33 | // Manages a tree of BrowserAccessibilityWin objects. |
| 34 | class BrowserAccessibilityManagerWin : public BrowserAccessibilityManager { |
| 35 | public: |
| 36 | BrowserAccessibilityManagerWin( |
| 37 | HWND parent_window, |
| 38 | const webkit_glue::WebAccessibility& src, |
| 39 | BrowserAccessibilityDelegate* delegate, |
| 40 | BrowserAccessibilityWinFactory* factory = |
| 41 | new BrowserAccessibilityWinFactory()); |
| 42 | |
| 43 | virtual ~BrowserAccessibilityManagerWin(); |
| 44 | |
| 45 | // Return a pointer to the root of the tree, does not make a new reference. |
| 46 | BrowserAccessibilityWin* GetRoot(); |
| 47 | |
| 48 | // Removes the BrowserAccessibilityWin child_id from the manager. |
| 49 | void Remove(LONG child_id); |
| 50 | |
| 51 | // Return a pointer to the object corresponding to the given child_id, |
| 52 | // does not make a new reference. |
| 53 | BrowserAccessibilityWin* GetFromChildID(LONG child_id); |
| 54 | |
| 55 | // Get a the default IAccessible for the parent window, does not make a |
| 56 | // new reference. |
| 57 | IAccessible* GetParentWindowIAccessible(); |
| 58 | |
| 59 | // Return the object that has focus, if it's a descandant of the |
| 60 | // given root (inclusive). Does not make a new reference. |
| 61 | BrowserAccessibilityWin* GetFocus(BrowserAccessibilityWin* root); |
| 62 | |
| 63 | // Tell the renderer to set focus to this node. |
| 64 | void SetFocus(const BrowserAccessibilityWin& node); |
| 65 | |
| 66 | // Tell the renderer to do the default action for this node. |
| 67 | void DoDefaultAction(const BrowserAccessibilityWin& node); |
| 68 | |
| 69 | // BrowserAccessibilityManager Methods |
| 70 | virtual IAccessible* GetRootAccessible(); |
| 71 | virtual void OnAccessibilityObjectStateChange( |
| 72 | const webkit_glue::WebAccessibility& acc_obj); |
| 73 | virtual void OnAccessibilityObjectChildrenChange( |
| 74 | const webkit_glue::WebAccessibility& acc_obj); |
| 75 | virtual void OnAccessibilityObjectFocusChange( |
| 76 | const webkit_glue::WebAccessibility& acc_obj); |
| 77 | virtual void OnAccessibilityObjectLoadComplete( |
| 78 | const webkit_glue::WebAccessibility& acc_obj); |
| 79 | virtual void OnAccessibilityObjectValueChange( |
| 80 | const webkit_glue::WebAccessibility& acc_obj); |
| 81 | virtual void OnAccessibilityObjectTextChange( |
| 82 | const webkit_glue::WebAccessibility& acc_obj); |
| 83 | |
| 84 | private: |
| 85 | // Recursively compare the IDs of our subtree to a new subtree received |
| 86 | // from the renderer and return true if their IDs match exactly. |
| 87 | bool CanModifyTreeInPlace( |
| 88 | BrowserAccessibilityWin* current_root, |
| 89 | const webkit_glue::WebAccessibility& new_root); |
| 90 | |
| 91 | // Recursively modify a subtree (by reinitializing) to match a new |
| 92 | // subtree received from the renderer process. Should only be called |
| 93 | // if CanModifyTreeInPlace returned true. |
| 94 | void ModifyTreeInPlace( |
| 95 | BrowserAccessibilityWin* current_root, |
| 96 | const webkit_glue::WebAccessibility& new_root); |
| 97 | |
| 98 | // Update the accessibility tree with an updated WebAccessibility tree or |
| 99 | // subtree received from the renderer process. First attempts to modify |
| 100 | // the tree in place, and if that fails, replaces the entire subtree. |
| 101 | // Returns the updated node or NULL if no node was updated. |
| 102 | BrowserAccessibilityWin* UpdateTree( |
| 103 | const webkit_glue::WebAccessibility& acc_obj); |
| 104 | |
| 105 | // Returns the next MSAA child id. |
| 106 | static LONG GetNextChildID(); |
| 107 | |
| 108 | // Recursively build a tree of BrowserAccessibilityWin objects from |
| 109 | // the WebAccessibility tree received from the renderer process. |
| 110 | BrowserAccessibilityWin* CreateAccessibilityTree( |
| 111 | BrowserAccessibilityWin* parent, |
| 112 | int child_id, |
| 113 | const webkit_glue::WebAccessibility& src, |
| 114 | int index_in_parent); |
| 115 | |
| 116 | // The object that can perform actions on our behalf. |
| 117 | BrowserAccessibilityDelegate* delegate_; |
| 118 | |
| 119 | // Factory to create BrowserAccessibility objects (for dependency injection). |
| 120 | scoped_ptr<BrowserAccessibilityWinFactory> factory_; |
| 121 | |
| 122 | // A default IAccessible instance for the parent window. |
| 123 | ScopedComPtr<IAccessible> window_iaccessible_; |
| 124 | |
| 125 | // The root of the tree of IAccessible objects and the element that |
| 126 | // currently has focus, if any. |
| 127 | BrowserAccessibilityWin* root_; |
| 128 | BrowserAccessibilityWin* focus_; |
| 129 | |
| 130 | // A mapping from the IDs of objects in the renderer, to the child IDs |
| 131 | // we use internally here. |
| 132 | base::hash_map<int, LONG> renderer_id_to_child_id_map_; |
| 133 | |
| 134 | // A mapping from child IDs to BrowserAccessibilityWin objects. |
| 135 | base::hash_map<LONG, BrowserAccessibilityWin*> child_id_map_; |
| 136 | |
| 137 | // The next child ID to use; static so that they're global to the process. |
| 138 | // Screen readers cache these IDs to see if they've seen the same object |
| 139 | // before so we should avoid reusing them within the same project. |
| 140 | static LONG next_child_id_; |
| 141 | |
| 142 | DISALLOW_COPY_AND_ASSIGN(BrowserAccessibilityManagerWin); |
| 143 | }; |
| 144 | |
| 145 | #endif // CHROME_BROWSER_ACCESSIBILITY_BROWSER_ACCESSIBILITY_MANAGER_WIN_H_ |