OLD | NEW |
| (Empty) |
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 #include "chrome/browser/browser_accessibility_manager.h" | |
6 | |
7 #include "base/scoped_comptr_win.h" | |
8 #include "chrome/browser/browser_accessibility.h" | |
9 #include "chrome/browser/renderer_host/render_process_host.h" | |
10 #include "chrome/browser/renderer_host/render_view_host.h" | |
11 #include "chrome/common/render_messages.h" | |
12 | |
13 using webkit_glue::WebAccessibility; | |
14 | |
15 // Factory method to create an instance of BrowserAccessibility | |
16 BrowserAccessibility* BrowserAccessibilityFactory::Create() { | |
17 CComObject<BrowserAccessibility>* instance; | |
18 HRESULT hr = CComObject<BrowserAccessibility>::CreateInstance(&instance); | |
19 DCHECK(SUCCEEDED(hr)); | |
20 return instance->NewReference(); | |
21 } | |
22 | |
23 // static | |
24 // Start child IDs at -1 and decrement each time, because clients use | |
25 // child IDs of 1, 2, 3, ... to access the children of an object by | |
26 // index, so we use negative IDs to clearly distinguish between indices | |
27 // and unique IDs. | |
28 LONG BrowserAccessibilityManager::next_child_id_ = -1; | |
29 | |
30 BrowserAccessibilityManager::BrowserAccessibilityManager( | |
31 HWND parent_hwnd, | |
32 const webkit_glue::WebAccessibility& src, | |
33 BrowserAccessibilityDelegate* delegate, | |
34 BrowserAccessibilityFactory* factory) | |
35 : parent_hwnd_(parent_hwnd), | |
36 delegate_(delegate), | |
37 factory_(factory), | |
38 focus_(NULL) { | |
39 HRESULT hr = ::CreateStdAccessibleObject( | |
40 parent_hwnd_, OBJID_WINDOW, IID_IAccessible, | |
41 reinterpret_cast<void **>(&window_iaccessible_)); | |
42 DCHECK(SUCCEEDED(hr)); | |
43 root_ = CreateAccessibilityTree(NULL, src, 0); | |
44 if (!focus_) | |
45 focus_ = root_; | |
46 } | |
47 | |
48 BrowserAccessibilityManager::~BrowserAccessibilityManager() { | |
49 // Clients could still hold references to some nodes of the tree, so | |
50 // calling Inactivate will make sure that as many nodes as possible are | |
51 // released now, and remaining nodes are marked as inactive so that | |
52 // calls to any methods on them will return E_FAIL; | |
53 root_->InactivateTree(); | |
54 root_->Release(); | |
55 } | |
56 | |
57 BrowserAccessibility* BrowserAccessibilityManager::GetRoot() { | |
58 return root_; | |
59 } | |
60 | |
61 BrowserAccessibility* BrowserAccessibilityManager::GetFromChildID( | |
62 LONG child_id) { | |
63 base::hash_map<LONG, BrowserAccessibility*>::iterator iter = | |
64 child_id_map_.find(child_id); | |
65 if (iter != child_id_map_.end()) { | |
66 return iter->second; | |
67 } else { | |
68 return NULL; | |
69 } | |
70 } | |
71 | |
72 IAccessible* BrowserAccessibilityManager::GetParentWindowIAccessible() { | |
73 return window_iaccessible_; | |
74 } | |
75 | |
76 HWND BrowserAccessibilityManager::GetParentHWND() { | |
77 return parent_hwnd_; | |
78 } | |
79 | |
80 BrowserAccessibility* BrowserAccessibilityManager::GetFocus( | |
81 BrowserAccessibility* root) { | |
82 if (focus_ && (!root || focus_->IsDescendantOf(root))) | |
83 return focus_; | |
84 | |
85 return NULL; | |
86 } | |
87 | |
88 void BrowserAccessibilityManager::SetFocus(const BrowserAccessibility& node) { | |
89 if (delegate_) | |
90 delegate_->SetAccessibilityFocus(node.renderer_id()); | |
91 } | |
92 | |
93 void BrowserAccessibilityManager::DoDefaultAction( | |
94 const BrowserAccessibility& node) { | |
95 if (delegate_) | |
96 delegate_->AccessibilityDoDefaultAction(node.renderer_id()); | |
97 } | |
98 | |
99 void BrowserAccessibilityManager::OnAccessibilityFocusChange(int renderer_id) { | |
100 base::hash_map<int, LONG>::iterator iter = | |
101 renderer_id_to_child_id_map_.find(renderer_id); | |
102 if (iter == renderer_id_to_child_id_map_.end()) | |
103 return; | |
104 | |
105 LONG child_id = iter->second; | |
106 base::hash_map<LONG, BrowserAccessibility*>::iterator uniq_iter = | |
107 child_id_map_.find(child_id); | |
108 if (uniq_iter != child_id_map_.end()) | |
109 focus_ = uniq_iter->second; | |
110 ::NotifyWinEvent(EVENT_OBJECT_FOCUS, parent_hwnd_, OBJID_CLIENT, child_id); | |
111 } | |
112 | |
113 void BrowserAccessibilityManager::OnAccessibilityObjectStateChange( | |
114 int renderer_id) { | |
115 base::hash_map<int, LONG>::iterator iter = | |
116 renderer_id_to_child_id_map_.find(renderer_id); | |
117 if (iter == renderer_id_to_child_id_map_.end()) | |
118 return; | |
119 | |
120 LONG child_id = iter->second; | |
121 ::NotifyWinEvent(EVENT_OBJECT_FOCUS, parent_hwnd_, OBJID_CLIENT, child_id); | |
122 } | |
123 | |
124 BrowserAccessibility* BrowserAccessibilityManager::CreateAccessibilityTree( | |
125 BrowserAccessibility* parent, | |
126 const webkit_glue::WebAccessibility& src, | |
127 int index_in_parent) { | |
128 BrowserAccessibility* instance = factory_->Create(); | |
129 | |
130 // Get the next child ID, and wrap around when we get near the end | |
131 // of a 32-bit integer range. It's okay to wrap around; we just want | |
132 // to avoid it as long as possible because clients may cache the ID of | |
133 // an object for a while to determine if they've seen it before. | |
134 LONG child_id = next_child_id_; | |
135 next_child_id_--; | |
136 if (next_child_id_ == -2000000000) | |
137 next_child_id_ = -1; | |
138 | |
139 instance->Initialize(this, parent, child_id, index_in_parent, src); | |
140 child_id_map_[child_id] = instance; | |
141 renderer_id_to_child_id_map_[src.id] = child_id; | |
142 if ((src.state >> WebAccessibility::STATE_FOCUSED) & 1) | |
143 focus_ = instance; | |
144 for (int i = 0; i < static_cast<int>(src.children.size()); ++i) { | |
145 BrowserAccessibility* child = CreateAccessibilityTree( | |
146 instance, src.children[i], i); | |
147 instance->AddChild(child); | |
148 } | |
149 | |
150 return instance; | |
151 } | |
OLD | NEW |