| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/browser_accessibility_manager_win.h" | 5 #include "chrome/browser/accessibility/browser_accessibility_manager_win.h" |
| 6 | 6 |
| 7 #include "chrome/browser/browser_accessibility_win.h" | 7 #include "chrome/browser/accessibility/browser_accessibility_win.h" |
| 8 #include "chrome/browser/renderer_host/render_process_host.h" | 8 #include "chrome/browser/renderer_host/render_process_host.h" |
| 9 #include "chrome/browser/renderer_host/render_view_host.h" | 9 #include "chrome/browser/renderer_host/render_view_host.h" |
| 10 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
| 11 #include "chrome/common/render_messages_params.h" | 11 #include "chrome/common/render_messages_params.h" |
| 12 | 12 |
| 13 using webkit_glue::WebAccessibility; | 13 using webkit_glue::WebAccessibility; |
| 14 | 14 |
| 15 // static |
| 16 BrowserAccessibilityManager* BrowserAccessibilityManager::Create( |
| 17 gfx::NativeWindow parent_window, |
| 18 const webkit_glue::WebAccessibility& src, |
| 19 BrowserAccessibilityDelegate* delegate) { |
| 20 return new BrowserAccessibilityManagerWin( |
| 21 parent_window, src, delegate, new BrowserAccessibilityFactory()); |
| 22 } |
| 23 |
| 15 // Factory method to create an instance of BrowserAccessibility | 24 // Factory method to create an instance of BrowserAccessibility |
| 16 BrowserAccessibility* BrowserAccessibilityFactory::Create() { | 25 BrowserAccessibilityWin* BrowserAccessibilityFactory::Create() { |
| 17 CComObject<BrowserAccessibility>* instance; | 26 CComObject<BrowserAccessibilityWin>* instance; |
| 18 HRESULT hr = CComObject<BrowserAccessibility>::CreateInstance(&instance); | 27 HRESULT hr = CComObject<BrowserAccessibilityWin>::CreateInstance(&instance); |
| 19 DCHECK(SUCCEEDED(hr)); | 28 DCHECK(SUCCEEDED(hr)); |
| 20 return instance->NewReference(); | 29 return instance->NewReference(); |
| 21 } | 30 } |
| 22 | 31 |
| 23 // static | 32 // static |
| 24 // Start child IDs at -1 and decrement each time, because clients use | 33 // 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 | 34 // 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 | 35 // index, so we use negative IDs to clearly distinguish between indices |
| 27 // and unique IDs. | 36 // and unique IDs. |
| 28 LONG BrowserAccessibilityManager::next_child_id_ = -1; | 37 LONG BrowserAccessibilityManagerWin::next_child_id_ = -1; |
| 29 | 38 |
| 30 BrowserAccessibilityManager::BrowserAccessibilityManager( | 39 BrowserAccessibilityManagerWin::BrowserAccessibilityManagerWin( |
| 31 HWND parent_hwnd, | 40 HWND parent_window, |
| 32 const webkit_glue::WebAccessibility& src, | 41 const webkit_glue::WebAccessibility& src, |
| 33 BrowserAccessibilityDelegate* delegate, | 42 BrowserAccessibilityDelegate* delegate, |
| 34 BrowserAccessibilityFactory* factory) | 43 BrowserAccessibilityFactory* factory) |
| 35 : parent_hwnd_(parent_hwnd), | 44 : BrowserAccessibilityManager(parent_window), |
| 36 delegate_(delegate), | 45 delegate_(delegate), |
| 37 factory_(factory), | 46 factory_(factory), |
| 38 focus_(NULL) { | 47 focus_(NULL) { |
| 39 HRESULT hr = ::CreateStdAccessibleObject( | 48 HRESULT hr = ::CreateStdAccessibleObject( |
| 40 parent_hwnd_, OBJID_WINDOW, IID_IAccessible, | 49 parent_window, OBJID_WINDOW, IID_IAccessible, |
| 41 reinterpret_cast<void **>(&window_iaccessible_)); | 50 reinterpret_cast<void **>(&window_iaccessible_)); |
| 42 DCHECK(SUCCEEDED(hr)); | 51 DCHECK(SUCCEEDED(hr)); |
| 43 root_ = CreateAccessibilityTree(NULL, GetNextChildID(), src, 0); | 52 root_ = CreateAccessibilityTree(NULL, GetNextChildID(), src, 0); |
| 44 if (!focus_) | 53 if (!focus_) |
| 45 focus_ = root_; | 54 focus_ = root_; |
| 46 } | 55 } |
| 47 | 56 |
| 48 BrowserAccessibilityManager::~BrowserAccessibilityManager() { | 57 BrowserAccessibilityManagerWin::~BrowserAccessibilityManagerWin() { |
| 49 // Clients could still hold references to some nodes of the tree, so | 58 // 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 | 59 // calling Inactivate will make sure that as many nodes as possible are |
| 51 // released now, and remaining nodes are marked as inactive so that | 60 // released now, and remaining nodes are marked as inactive so that |
| 52 // calls to any methods on them will return E_FAIL; | 61 // calls to any methods on them will return E_FAIL; |
| 53 root_->InactivateTree(); | 62 root_->InactivateTree(); |
| 54 root_->Release(); | 63 root_->Release(); |
| 55 } | 64 } |
| 56 | 65 |
| 57 BrowserAccessibility* BrowserAccessibilityManager::GetRoot() { | 66 BrowserAccessibilityWin* BrowserAccessibilityManagerWin::GetRoot() { |
| 58 return root_; | 67 return root_; |
| 59 } | 68 } |
| 60 | 69 |
| 61 void BrowserAccessibilityManager::Remove(LONG child_id) { | 70 void BrowserAccessibilityManagerWin::Remove(LONG child_id) { |
| 62 child_id_map_.erase(child_id); | 71 child_id_map_.erase(child_id); |
| 63 } | 72 } |
| 64 | 73 |
| 65 BrowserAccessibility* BrowserAccessibilityManager::GetFromChildID( | 74 BrowserAccessibilityWin* BrowserAccessibilityManagerWin::GetFromChildID( |
| 66 LONG child_id) { | 75 LONG child_id) { |
| 67 base::hash_map<LONG, BrowserAccessibility*>::iterator iter = | 76 base::hash_map<LONG, BrowserAccessibilityWin*>::iterator iter = |
| 68 child_id_map_.find(child_id); | 77 child_id_map_.find(child_id); |
| 69 if (iter != child_id_map_.end()) { | 78 if (iter != child_id_map_.end()) { |
| 70 return iter->second; | 79 return iter->second; |
| 71 } else { | 80 } else { |
| 72 return NULL; | 81 return NULL; |
| 73 } | 82 } |
| 74 } | 83 } |
| 75 | 84 |
| 76 IAccessible* BrowserAccessibilityManager::GetParentWindowIAccessible() { | 85 IAccessible* BrowserAccessibilityManagerWin::GetParentWindowIAccessible() { |
| 77 return window_iaccessible_; | 86 return window_iaccessible_; |
| 78 } | 87 } |
| 79 | 88 |
| 80 HWND BrowserAccessibilityManager::GetParentHWND() { | 89 BrowserAccessibilityWin* BrowserAccessibilityManagerWin::GetFocus( |
| 81 return parent_hwnd_; | 90 BrowserAccessibilityWin* root) { |
| 82 } | |
| 83 | |
| 84 BrowserAccessibility* BrowserAccessibilityManager::GetFocus( | |
| 85 BrowserAccessibility* root) { | |
| 86 if (focus_ && (!root || focus_->IsDescendantOf(root))) | 91 if (focus_ && (!root || focus_->IsDescendantOf(root))) |
| 87 return focus_; | 92 return focus_; |
| 88 | 93 |
| 89 return NULL; | 94 return NULL; |
| 90 } | 95 } |
| 91 | 96 |
| 92 void BrowserAccessibilityManager::SetFocus(const BrowserAccessibility& node) { | 97 void BrowserAccessibilityManagerWin::SetFocus( |
| 98 const BrowserAccessibilityWin& node) { |
| 93 if (delegate_) | 99 if (delegate_) |
| 94 delegate_->SetAccessibilityFocus(node.renderer_id()); | 100 delegate_->SetAccessibilityFocus(node.renderer_id()); |
| 95 } | 101 } |
| 96 | 102 |
| 97 void BrowserAccessibilityManager::DoDefaultAction( | 103 void BrowserAccessibilityManagerWin::DoDefaultAction( |
| 98 const BrowserAccessibility& node) { | 104 const BrowserAccessibilityWin& node) { |
| 99 if (delegate_) | 105 if (delegate_) |
| 100 delegate_->AccessibilityDoDefaultAction(node.renderer_id()); | 106 delegate_->AccessibilityDoDefaultAction(node.renderer_id()); |
| 101 } | 107 } |
| 102 | 108 |
| 103 void BrowserAccessibilityManager::OnAccessibilityNotifications( | 109 BrowserAccessibilityWin* BrowserAccessibilityManagerWin::UpdateTree( |
| 104 const std::vector<ViewHostMsg_AccessibilityNotification_Params>& params) { | |
| 105 for (uint32 index = 0; index < params.size(); index++) { | |
| 106 const ViewHostMsg_AccessibilityNotification_Params& param = params[index]; | |
| 107 | |
| 108 switch (param.notification_type) { | |
| 109 case ViewHostMsg_AccessibilityNotification_Params:: | |
| 110 NOTIFICATION_TYPE_CHECK_STATE_CHANGED: | |
| 111 OnAccessibilityObjectStateChange(param.acc_obj); | |
| 112 break; | |
| 113 case ViewHostMsg_AccessibilityNotification_Params:: | |
| 114 NOTIFICATION_TYPE_CHILDREN_CHANGED: | |
| 115 OnAccessibilityObjectChildrenChange(param.acc_obj); | |
| 116 break; | |
| 117 case ViewHostMsg_AccessibilityNotification_Params:: | |
| 118 NOTIFICATION_TYPE_FOCUS_CHANGED: | |
| 119 OnAccessibilityObjectFocusChange(param.acc_obj); | |
| 120 break; | |
| 121 case ViewHostMsg_AccessibilityNotification_Params:: | |
| 122 NOTIFICATION_TYPE_LOAD_COMPLETE: | |
| 123 OnAccessibilityObjectLoadComplete(param.acc_obj); | |
| 124 break; | |
| 125 case ViewHostMsg_AccessibilityNotification_Params:: | |
| 126 NOTIFICATION_TYPE_VALUE_CHANGED: | |
| 127 OnAccessibilityObjectValueChange(param.acc_obj); | |
| 128 break; | |
| 129 default: | |
| 130 DCHECK(0); | |
| 131 break; | |
| 132 } | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 BrowserAccessibility* BrowserAccessibilityManager::UpdateTree( | |
| 137 const webkit_glue::WebAccessibility& acc_obj) { | 110 const webkit_glue::WebAccessibility& acc_obj) { |
| 138 base::hash_map<int, LONG>::iterator iter = | 111 base::hash_map<int, LONG>::iterator iter = |
| 139 renderer_id_to_child_id_map_.find(acc_obj.id); | 112 renderer_id_to_child_id_map_.find(acc_obj.id); |
| 140 if (iter == renderer_id_to_child_id_map_.end()) | 113 if (iter == renderer_id_to_child_id_map_.end()) |
| 141 return NULL; | 114 return NULL; |
| 142 | 115 |
| 143 LONG child_id = iter->second; | 116 LONG child_id = iter->second; |
| 144 BrowserAccessibility* old_browser_acc = GetFromChildID(child_id); | 117 BrowserAccessibilityWin* old_browser_acc = GetFromChildID(child_id); |
| 145 if (!old_browser_acc) | 118 if (!old_browser_acc) |
| 146 return NULL; | 119 return NULL; |
| 147 | 120 |
| 148 if (old_browser_acc->GetChildCount() == 0 && acc_obj.children.size() == 0) { | 121 if (old_browser_acc->GetChildCount() == 0 && acc_obj.children.size() == 0) { |
| 149 // Reinitialize the BrowserAccessibility if there are no children to update. | 122 // Reinitialize the BrowserAccessibilityWin if there are no children to |
| 123 // update. |
| 150 old_browser_acc->Initialize( | 124 old_browser_acc->Initialize( |
| 151 this, | 125 this, |
| 152 old_browser_acc->GetParent(), | 126 old_browser_acc->GetParent(), |
| 153 child_id, | 127 child_id, |
| 154 old_browser_acc->index_in_parent(), | 128 old_browser_acc->index_in_parent(), |
| 155 acc_obj); | 129 acc_obj); |
| 156 | 130 |
| 157 return old_browser_acc; | 131 return old_browser_acc; |
| 158 } else { | 132 } else { |
| 159 BrowserAccessibility* new_browser_acc = CreateAccessibilityTree( | 133 BrowserAccessibilityWin* new_browser_acc = CreateAccessibilityTree( |
| 160 old_browser_acc->GetParent(), | 134 old_browser_acc->GetParent(), |
| 161 child_id, | 135 child_id, |
| 162 acc_obj, | 136 acc_obj, |
| 163 old_browser_acc->index_in_parent()); | 137 old_browser_acc->index_in_parent()); |
| 164 | 138 |
| 165 if (old_browser_acc->GetParent()) { | 139 if (old_browser_acc->GetParent()) { |
| 166 old_browser_acc->GetParent()->ReplaceChild( | 140 old_browser_acc->GetParent()->ReplaceChild( |
| 167 old_browser_acc, | 141 old_browser_acc, |
| 168 new_browser_acc); | 142 new_browser_acc); |
| 169 } else { | 143 } else { |
| 170 DCHECK_EQ(old_browser_acc, root_); | 144 DCHECK_EQ(old_browser_acc, root_); |
| 171 root_ = new_browser_acc; | 145 root_ = new_browser_acc; |
| 172 } | 146 } |
| 173 old_browser_acc->InactivateTree(); | 147 old_browser_acc->InactivateTree(); |
| 174 old_browser_acc->Release(); | 148 old_browser_acc->Release(); |
| 175 child_id_map_[child_id] = new_browser_acc; | 149 child_id_map_[child_id] = new_browser_acc; |
| 176 | 150 |
| 177 return new_browser_acc; | 151 return new_browser_acc; |
| 178 } | 152 } |
| 179 } | 153 } |
| 180 | 154 |
| 181 void BrowserAccessibilityManager::OnAccessibilityObjectStateChange( | 155 IAccessible* BrowserAccessibilityManagerWin::GetRootAccessible() { |
| 156 return root_; |
| 157 } |
| 158 |
| 159 void BrowserAccessibilityManagerWin::OnAccessibilityObjectStateChange( |
| 182 const webkit_glue::WebAccessibility& acc_obj) { | 160 const webkit_glue::WebAccessibility& acc_obj) { |
| 183 BrowserAccessibility* new_browser_acc = UpdateTree(acc_obj); | 161 BrowserAccessibilityWin* new_browser_acc = UpdateTree(acc_obj); |
| 184 if (!new_browser_acc) | 162 if (!new_browser_acc) |
| 185 return; | 163 return; |
| 186 | 164 |
| 187 LONG child_id = new_browser_acc->child_id(); | 165 LONG child_id = new_browser_acc->child_id(); |
| 188 NotifyWinEvent( | 166 NotifyWinEvent( |
| 189 EVENT_OBJECT_STATECHANGE, parent_hwnd_, OBJID_CLIENT, child_id); | 167 EVENT_OBJECT_STATECHANGE, GetParentWindow(), OBJID_CLIENT, child_id); |
| 190 } | 168 } |
| 191 | 169 |
| 192 void BrowserAccessibilityManager::OnAccessibilityObjectChildrenChange( | 170 void BrowserAccessibilityManagerWin::OnAccessibilityObjectChildrenChange( |
| 193 const webkit_glue::WebAccessibility& acc_obj) { | 171 const webkit_glue::WebAccessibility& acc_obj) { |
| 194 BrowserAccessibility* new_browser_acc = UpdateTree(acc_obj); | 172 BrowserAccessibilityWin* new_browser_acc = UpdateTree(acc_obj); |
| 195 if (!new_browser_acc) | 173 if (!new_browser_acc) |
| 196 return; | 174 return; |
| 197 | 175 |
| 198 LONG child_id; | 176 LONG child_id; |
| 199 if (root_ != new_browser_acc) { | 177 if (root_ != new_browser_acc) { |
| 200 child_id = new_browser_acc->GetParent()->child_id(); | 178 child_id = new_browser_acc->GetParent()->child_id(); |
| 201 } else { | 179 } else { |
| 202 child_id = CHILDID_SELF; | 180 child_id = CHILDID_SELF; |
| 203 } | 181 } |
| 204 | 182 |
| 205 NotifyWinEvent(EVENT_OBJECT_REORDER, parent_hwnd_, OBJID_CLIENT, child_id); | 183 NotifyWinEvent( |
| 184 EVENT_OBJECT_REORDER, GetParentWindow(), OBJID_CLIENT, child_id); |
| 206 } | 185 } |
| 207 | 186 |
| 208 void BrowserAccessibilityManager::OnAccessibilityObjectFocusChange( | 187 void BrowserAccessibilityManagerWin::OnAccessibilityObjectFocusChange( |
| 209 const webkit_glue::WebAccessibility& acc_obj) { | 188 const webkit_glue::WebAccessibility& acc_obj) { |
| 210 BrowserAccessibility* new_browser_acc = UpdateTree(acc_obj); | 189 BrowserAccessibilityWin* new_browser_acc = UpdateTree(acc_obj); |
| 211 if (!new_browser_acc) | 190 if (!new_browser_acc) |
| 212 return; | 191 return; |
| 213 | 192 |
| 214 focus_ = new_browser_acc; | 193 focus_ = new_browser_acc; |
| 215 LONG child_id = new_browser_acc->child_id(); | 194 LONG child_id = new_browser_acc->child_id(); |
| 216 NotifyWinEvent(EVENT_OBJECT_FOCUS, parent_hwnd_, OBJID_CLIENT, child_id); | 195 NotifyWinEvent(EVENT_OBJECT_FOCUS, GetParentWindow(), OBJID_CLIENT, child_id); |
| 217 } | 196 } |
| 218 | 197 |
| 219 void BrowserAccessibilityManager::OnAccessibilityObjectLoadComplete( | 198 void BrowserAccessibilityManagerWin::OnAccessibilityObjectLoadComplete( |
| 220 const webkit_glue::WebAccessibility& acc_obj) { | 199 const webkit_glue::WebAccessibility& acc_obj) { |
| 221 root_->InactivateTree(); | 200 root_->InactivateTree(); |
| 222 root_->Release(); | 201 root_->Release(); |
| 223 focus_ = NULL; | 202 focus_ = NULL; |
| 224 | 203 |
| 225 root_ = CreateAccessibilityTree(NULL, GetNextChildID(), acc_obj, 0); | 204 root_ = CreateAccessibilityTree(NULL, GetNextChildID(), acc_obj, 0); |
| 226 if (!focus_) | 205 if (!focus_) |
| 227 focus_ = root_; | 206 focus_ = root_; |
| 228 | 207 |
| 229 LONG root_id = root_->child_id(); | 208 LONG root_id = root_->child_id(); |
| 230 NotifyWinEvent(EVENT_OBJECT_FOCUS, parent_hwnd_, OBJID_CLIENT, root_id); | 209 NotifyWinEvent(EVENT_OBJECT_FOCUS, GetParentWindow(), OBJID_CLIENT, root_id); |
| 231 NotifyWinEvent( | 210 NotifyWinEvent( |
| 232 IA2_EVENT_DOCUMENT_LOAD_COMPLETE, parent_hwnd_, OBJID_CLIENT, root_id); | 211 IA2_EVENT_DOCUMENT_LOAD_COMPLETE, GetParentWindow(), OBJID_CLIENT, root_id); |
| 233 } | 212 } |
| 234 | 213 |
| 235 void BrowserAccessibilityManager::OnAccessibilityObjectValueChange( | 214 void BrowserAccessibilityManagerWin::OnAccessibilityObjectValueChange( |
| 236 const webkit_glue::WebAccessibility& acc_obj) { | 215 const webkit_glue::WebAccessibility& acc_obj) { |
| 237 BrowserAccessibility* new_browser_acc = UpdateTree(acc_obj); | 216 BrowserAccessibilityWin* new_browser_acc = UpdateTree(acc_obj); |
| 238 if (!new_browser_acc) | 217 if (!new_browser_acc) |
| 239 return; | 218 return; |
| 240 | 219 |
| 241 LONG child_id = new_browser_acc->child_id(); | 220 LONG child_id = new_browser_acc->child_id(); |
| 242 NotifyWinEvent( | 221 NotifyWinEvent( |
| 243 EVENT_OBJECT_VALUECHANGE, parent_hwnd_, OBJID_CLIENT, child_id); | 222 EVENT_OBJECT_VALUECHANGE, GetParentWindow(), OBJID_CLIENT, child_id); |
| 244 } | 223 } |
| 245 | 224 |
| 246 LONG BrowserAccessibilityManager::GetNextChildID() { | 225 LONG BrowserAccessibilityManagerWin::GetNextChildID() { |
| 247 // Get the next child ID, and wrap around when we get near the end | 226 // Get the next child ID, and wrap around when we get near the end |
| 248 // of a 32-bit integer range. It's okay to wrap around; we just want | 227 // of a 32-bit integer range. It's okay to wrap around; we just want |
| 249 // to avoid it as long as possible because clients may cache the ID of | 228 // to avoid it as long as possible because clients may cache the ID of |
| 250 // an object for a while to determine if they've seen it before. | 229 // an object for a while to determine if they've seen it before. |
| 251 next_child_id_--; | 230 next_child_id_--; |
| 252 if (next_child_id_ == -2000000000) | 231 if (next_child_id_ == -2000000000) |
| 253 next_child_id_ = -1; | 232 next_child_id_ = -1; |
| 254 | 233 |
| 255 return next_child_id_; | 234 return next_child_id_; |
| 256 } | 235 } |
| 257 | 236 |
| 258 BrowserAccessibility* BrowserAccessibilityManager::CreateAccessibilityTree( | 237 BrowserAccessibilityWin* |
| 259 BrowserAccessibility* parent, | 238 BrowserAccessibilityManagerWin::CreateAccessibilityTree( |
| 239 BrowserAccessibilityWin* parent, |
| 260 int child_id, | 240 int child_id, |
| 261 const webkit_glue::WebAccessibility& src, | 241 const webkit_glue::WebAccessibility& src, |
| 262 int index_in_parent) { | 242 int index_in_parent) { |
| 263 BrowserAccessibility* instance = factory_->Create(); | 243 BrowserAccessibilityWin* instance = factory_->Create(); |
| 264 | 244 |
| 265 instance->Initialize(this, parent, child_id, index_in_parent, src); | 245 instance->Initialize(this, parent, child_id, index_in_parent, src); |
| 266 child_id_map_[child_id] = instance; | 246 child_id_map_[child_id] = instance; |
| 267 renderer_id_to_child_id_map_[src.id] = child_id; | 247 renderer_id_to_child_id_map_[src.id] = child_id; |
| 268 if ((src.state >> WebAccessibility::STATE_FOCUSED) & 1) | 248 if ((src.state >> WebAccessibility::STATE_FOCUSED) & 1) |
| 269 focus_ = instance; | 249 focus_ = instance; |
| 270 for (int i = 0; i < static_cast<int>(src.children.size()); ++i) { | 250 for (int i = 0; i < static_cast<int>(src.children.size()); ++i) { |
| 271 BrowserAccessibility* child = CreateAccessibilityTree( | 251 BrowserAccessibilityWin* child = CreateAccessibilityTree( |
| 272 instance, GetNextChildID(), src.children[i], i); | 252 instance, GetNextChildID(), src.children[i], i); |
| 273 instance->AddChild(child); | 253 instance->AddChild(child); |
| 274 } | 254 } |
| 275 | 255 |
| 276 return instance; | 256 return instance; |
| 277 } | 257 } |
| OLD | NEW |