Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/accessibility/browser_accessibility_manager.h" | 5 #include "content/browser/accessibility/browser_accessibility_manager.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "content/browser/accessibility/browser_accessibility.h" | 8 #include "content/browser/accessibility/browser_accessibility.h" |
| 9 #include "content/browser/accessibility/browser_accessibility_state_impl.h" | 9 #include "content/browser/accessibility/browser_accessibility_state_impl.h" |
| 10 #include "content/common/accessibility_messages.h" | 10 #include "content/common/accessibility_messages.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 BrowserAccessibility* BrowserAccessibilityFactory::Create() { | 14 BrowserAccessibility* BrowserAccessibilityFactory::Create() { |
| 15 return BrowserAccessibility::Create(); | 15 return BrowserAccessibility::Create(); |
| 16 } | 16 } |
| 17 | 17 |
| 18 // Start child IDs at -1 and decrement each time, because clients use | |
| 19 // child IDs of 1, 2, 3, ... to access the children of an object by | |
| 20 // index, so we use negative IDs to clearly distinguish between indices | |
| 21 // and unique IDs. | |
| 22 // static | |
| 23 int32 BrowserAccessibilityManager::next_child_id_ = -1; | |
| 24 | |
| 25 #if !defined(OS_MACOSX) && \ | 18 #if !defined(OS_MACOSX) && \ |
| 26 !defined(OS_WIN) && \ | 19 !defined(OS_WIN) && \ |
| 27 !defined(TOOLKIT_GTK) | 20 !defined(TOOLKIT_GTK) |
| 28 // We have subclassess of BrowserAccessibilityManager on Mac, Linux/GTK, | 21 // We have subclassess of BrowserAccessibilityManager on Mac, Linux/GTK, |
| 29 // and Win. For any other platform, instantiate the base class. | 22 // and Win. For any other platform, instantiate the base class. |
| 30 // static | 23 // static |
| 31 BrowserAccessibilityManager* BrowserAccessibilityManager::Create( | 24 BrowserAccessibilityManager* BrowserAccessibilityManager::Create( |
| 32 const AccessibilityNodeData& src, | 25 const AccessibilityNodeData& src, |
| 33 BrowserAccessibilityDelegate* delegate, | 26 BrowserAccessibilityDelegate* delegate, |
| 34 BrowserAccessibilityFactory* factory) { | 27 BrowserAccessibilityFactory* factory) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 46 focus_(NULL), | 39 focus_(NULL), |
| 47 osk_state_(OSK_ALLOWED) { | 40 osk_state_(OSK_ALLOWED) { |
| 48 std::vector<AccessibilityNodeData> nodes; | 41 std::vector<AccessibilityNodeData> nodes; |
| 49 nodes.push_back(src); | 42 nodes.push_back(src); |
| 50 if (!UpdateNodes(nodes)) | 43 if (!UpdateNodes(nodes)) |
| 51 return; | 44 return; |
| 52 if (!focus_) | 45 if (!focus_) |
| 53 SetFocus(root_, false); | 46 SetFocus(root_, false); |
| 54 } | 47 } |
| 55 | 48 |
| 56 // static | |
| 57 int32 BrowserAccessibilityManager::GetNextChildID() { | |
| 58 // Get the next child ID, and wrap around when we get near the end | |
| 59 // of a 32-bit integer range. It's okay to wrap around; we just want | |
| 60 // to avoid it as long as possible because clients may cache the ID of | |
| 61 // an object for a while to determine if they've seen it before. | |
| 62 next_child_id_--; | |
| 63 if (next_child_id_ == -2000000000) | |
| 64 next_child_id_ = -1; | |
| 65 | |
| 66 return next_child_id_; | |
| 67 } | |
| 68 | |
| 69 BrowserAccessibilityManager::~BrowserAccessibilityManager() { | 49 BrowserAccessibilityManager::~BrowserAccessibilityManager() { |
| 70 if (root_) | 50 if (root_) |
| 71 root_->Destroy(); | 51 root_->Destroy(); |
| 72 } | 52 } |
| 73 | 53 |
| 74 BrowserAccessibility* BrowserAccessibilityManager::GetRoot() { | 54 BrowserAccessibility* BrowserAccessibilityManager::GetRoot() { |
| 75 return root_; | 55 return root_; |
| 76 } | 56 } |
| 77 | 57 |
| 78 BrowserAccessibility* BrowserAccessibilityManager::GetFromChildID( | |
| 79 int32 child_id) { | |
| 80 base::hash_map<int32, BrowserAccessibility*>::iterator iter = | |
| 81 child_id_map_.find(child_id); | |
| 82 if (iter != child_id_map_.end()) { | |
| 83 return iter->second; | |
| 84 } else { | |
| 85 return NULL; | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 BrowserAccessibility* BrowserAccessibilityManager::GetFromRendererID( | 58 BrowserAccessibility* BrowserAccessibilityManager::GetFromRendererID( |
| 90 int32 renderer_id) { | 59 int32 renderer_id) { |
| 91 base::hash_map<int32, int32>::iterator iter = | 60 base::hash_map<int32, BrowserAccessibility*>::iterator iter = |
| 92 renderer_id_to_child_id_map_.find(renderer_id); | 61 renderer_id_map_.find(renderer_id); |
| 93 if (iter == renderer_id_to_child_id_map_.end()) | 62 if (iter != renderer_id_map_.end()) |
| 94 return NULL; | 63 return iter->second; |
| 95 | 64 return NULL; |
| 96 int32 child_id = iter->second; | |
| 97 return GetFromChildID(child_id); | |
| 98 } | 65 } |
| 99 | 66 |
| 100 void BrowserAccessibilityManager::GotFocus(bool touch_event_context) { | 67 void BrowserAccessibilityManager::GotFocus(bool touch_event_context) { |
| 101 if (!touch_event_context) | 68 if (!touch_event_context) |
| 102 osk_state_ = OSK_DISALLOWED_BECAUSE_TAB_JUST_APPEARED; | 69 osk_state_ = OSK_DISALLOWED_BECAUSE_TAB_JUST_APPEARED; |
| 103 | 70 |
| 104 if (!focus_) | 71 if (!focus_) |
| 105 return; | 72 return; |
| 106 | 73 |
| 107 NotifyAccessibilityEvent(AccessibilityNotificationFocusChanged, focus_); | 74 NotifyAccessibilityEvent(AccessibilityNotificationFocusChanged, focus_); |
| 108 } | 75 } |
| 109 | 76 |
| 110 void BrowserAccessibilityManager::WasHidden() { | 77 void BrowserAccessibilityManager::WasHidden() { |
| 111 osk_state_ = OSK_DISALLOWED_BECAUSE_TAB_HIDDEN; | 78 osk_state_ = OSK_DISALLOWED_BECAUSE_TAB_HIDDEN; |
| 112 } | 79 } |
| 113 | 80 |
| 114 void BrowserAccessibilityManager::GotMouseDown() { | 81 void BrowserAccessibilityManager::GotMouseDown() { |
| 115 osk_state_ = OSK_ALLOWED_WITHIN_FOCUSED_OBJECT; | 82 osk_state_ = OSK_ALLOWED_WITHIN_FOCUSED_OBJECT; |
| 116 NotifyAccessibilityEvent(AccessibilityNotificationFocusChanged, focus_); | 83 NotifyAccessibilityEvent(AccessibilityNotificationFocusChanged, focus_); |
| 117 } | 84 } |
| 118 | 85 |
| 119 bool BrowserAccessibilityManager::IsOSKAllowed(const gfx::Rect& bounds) { | 86 bool BrowserAccessibilityManager::IsOSKAllowed(const gfx::Rect& bounds) { |
| 120 if (!delegate_ || !delegate_->HasFocus()) | 87 if (!delegate_ || !delegate_->HasFocus()) |
| 121 return false; | 88 return false; |
| 122 | 89 |
| 123 gfx::Point touch_point = delegate_->GetLastTouchEventLocation(); | 90 gfx::Point touch_point = delegate_->GetLastTouchEventLocation(); |
| 124 return bounds.Contains(touch_point); | 91 return bounds.Contains(touch_point); |
| 125 } | 92 } |
| 126 | 93 |
| 127 void BrowserAccessibilityManager::Remove(BrowserAccessibility* node) { | 94 void BrowserAccessibilityManager::RemoveNode(BrowserAccessibility* node) { |
| 128 if (node == focus_) | 95 if (node == focus_) |
| 129 SetFocus(root_, false); | 96 SetFocus(root_, false); |
| 130 int child_id = node->child_id(); | |
| 131 int renderer_id = node->renderer_id(); | 97 int renderer_id = node->renderer_id(); |
| 132 child_id_map_.erase(child_id); | 98 renderer_id_map_.erase(renderer_id); |
| 133 DCHECK(renderer_id_to_child_id_map_[renderer_id] == child_id); | |
| 134 // Make sure we don't overwrite a newer entry (see UpdateNode for a possible | |
| 135 // corner case). | |
| 136 if (renderer_id_to_child_id_map_[renderer_id] == child_id) | |
| 137 renderer_id_to_child_id_map_.erase(renderer_id); | |
| 138 } | 99 } |
| 139 | 100 |
| 140 void BrowserAccessibilityManager::OnAccessibilityNotifications( | 101 void BrowserAccessibilityManager::OnAccessibilityNotifications( |
| 141 const std::vector<AccessibilityHostMsg_NotificationParams>& params) { | 102 const std::vector<AccessibilityHostMsg_NotificationParams>& params) { |
| 142 for (uint32 index = 0; index < params.size(); index++) { | 103 for (uint32 index = 0; index < params.size(); index++) { |
| 143 const AccessibilityHostMsg_NotificationParams& param = params[index]; | 104 const AccessibilityHostMsg_NotificationParams& param = params[index]; |
| 144 | 105 |
| 145 // Update nodes that changed. | 106 // Update nodes that changed. |
| 146 if (!UpdateNodes(param.nodes)) | 107 if (!UpdateNodes(param.nodes)) |
| 147 return; | 108 return; |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 287 CHECK(false); | 248 CHECK(false); |
| 288 } | 249 } |
| 289 | 250 |
| 290 return success; | 251 return success; |
| 291 } | 252 } |
| 292 | 253 |
| 293 BrowserAccessibility* BrowserAccessibilityManager::CreateNode( | 254 BrowserAccessibility* BrowserAccessibilityManager::CreateNode( |
| 294 BrowserAccessibility* parent, | 255 BrowserAccessibility* parent, |
| 295 int32 renderer_id, | 256 int32 renderer_id, |
| 296 int32 index_in_parent) { | 257 int32 index_in_parent) { |
| 297 BrowserAccessibility* instance = factory_->Create(); | 258 BrowserAccessibility* node = factory_->Create(); |
| 298 int32 child_id = GetNextChildID(); | 259 node->InitializeTreeStructure( |
| 299 instance->InitializeTreeStructure( | 260 this, parent, renderer_id, index_in_parent); |
| 300 this, parent, child_id, renderer_id, index_in_parent); | 261 InitializeNode(node); |
| 301 child_id_map_[child_id] = instance; | 262 return node; |
| 302 renderer_id_to_child_id_map_[renderer_id] = child_id; | 263 } |
| 303 return instance; | 264 |
| 265 void BrowserAccessibilityManager::InitializeNode(BrowserAccessibility* node) { | |
|
aboxhall
2013/04/09 09:28:29
This seems like a bit of a confusing name for this
dmazzoni
2013/04/09 19:44:04
Good idea, done.
| |
| 266 renderer_id_map_[node->renderer_id()] = node; | |
| 304 } | 267 } |
| 305 | 268 |
| 306 bool BrowserAccessibilityManager::UpdateNode(const AccessibilityNodeData& src) { | 269 bool BrowserAccessibilityManager::UpdateNode(const AccessibilityNodeData& src) { |
| 307 // This method updates one node in the tree based on serialized data | 270 // This method updates one node in the tree based on serialized data |
| 308 // received from the renderer. | 271 // received from the renderer. |
| 309 | 272 |
| 310 // Create a set of child ids in |src| for fast lookup. If a duplicate id is | 273 // Create a set of child ids in |src| for fast lookup. If a duplicate id is |
| 311 // found, exit now with a fatal error before changing anything else. | 274 // found, exit now with a fatal error before changing anything else. |
| 312 std::set<int32> new_child_ids; | 275 std::set<int32> new_child_ids; |
| 313 for (size_t i = 0; i < src.child_ids.size(); ++i) { | 276 for (size_t i = 0; i < src.child_ids.size(); ++i) { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 } | 352 } |
| 390 | 353 |
| 391 // Keep track of what node is focused. | 354 // Keep track of what node is focused. |
| 392 if ((src.state >> AccessibilityNodeData::STATE_FOCUSED) & 1) | 355 if ((src.state >> AccessibilityNodeData::STATE_FOCUSED) & 1) |
| 393 SetFocus(instance, false); | 356 SetFocus(instance, false); |
| 394 | 357 |
| 395 return success; | 358 return success; |
| 396 } | 359 } |
| 397 | 360 |
| 398 } // namespace content | 361 } // namespace content |
| OLD | NEW |