OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 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 "ui/views/accessibility/ax_tree_source_views.h" |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "ui/accessibility/ax_view_state.h" |
| 9 #include "ui/views/widget/widget.h" |
| 10 |
| 11 namespace views { |
| 12 |
| 13 // A simple store associating each |View| with a unique id. |
| 14 class AXViewStore { |
| 15 public: |
| 16 AXViewStore() : view_id_(0) {}; |
| 17 |
| 18 int32 GetOrStoreView(View* view) { |
| 19 int32 current_id = RetrieveId(view); |
| 20 if (current_id != -1) |
| 21 return current_id; |
| 22 id_to_view_[++view_id_] = view; |
| 23 view_to_id_[view] = view_id_; |
| 24 return view_id_; |
| 25 } |
| 26 |
| 27 void RemoveView(View* view) { |
| 28 std::map<View*, int32>::iterator view_it = view_to_id_.find(view); |
| 29 if (view_it == view_to_id_.end()) |
| 30 return; |
| 31 int32 view_id = view_it->second; |
| 32 view_to_id_.erase(view_it); |
| 33 std::map<int32, View*>::iterator id_it = id_to_view_.find(view_id); |
| 34 if (id_it == id_to_view_.end()) { |
| 35 NOTREACHED(); |
| 36 return; |
| 37 } |
| 38 id_to_view_.erase(id_it); |
| 39 } |
| 40 |
| 41 View* RetrieveView(int32 id) { |
| 42 std::map<int32, View*>::iterator it = id_to_view_.find(id); |
| 43 if (it != id_to_view_.end()) |
| 44 return it->second; |
| 45 return NULL; |
| 46 } |
| 47 |
| 48 int32 RetrieveId(View* view) { |
| 49 if (!view) |
| 50 return -1; |
| 51 std::map<View*, int32>::iterator it = view_to_id_.find(view); |
| 52 if (it != view_to_id_.end()) |
| 53 return it->second; |
| 54 return -1; |
| 55 } |
| 56 |
| 57 private: |
| 58 // Unique id assigned to a view. |
| 59 int32 view_id_; |
| 60 |
| 61 // Maps from id to |View*| and |View*| to id. |
| 62 std::map<int32, View*> id_to_view_; |
| 63 std::map<View*, int32> view_to_id_; |
| 64 }; |
| 65 |
| 66 AXTreeSourceViews::AXTreeSourceViews(Widget* root) : |
| 67 view_store_(new AXViewStore()), |
| 68 root_(root) { |
| 69 root->AddObserver(this); |
| 70 } |
| 71 |
| 72 AXTreeSourceViews::~AXTreeSourceViews() { |
| 73 if (root_) |
| 74 root_->RemoveObserver(this); |
| 75 } |
| 76 |
| 77 View* AXTreeSourceViews::GetRoot() const { |
| 78 if (!root_) |
| 79 return NULL; |
| 80 return root_->GetRootView(); |
| 81 } |
| 82 |
| 83 View* AXTreeSourceViews::GetFromId(int32 id) const { |
| 84 return view_store_->RetrieveView(id); |
| 85 } |
| 86 |
| 87 int32 AXTreeSourceViews::GetId(View* node) const { |
| 88 return view_store_->GetOrStoreView(node); |
| 89 } |
| 90 |
| 91 void AXTreeSourceViews::GetChildren(View* node, |
| 92 std::vector<View*>* out_children) const { |
| 93 for (int i = 0; i < node->child_count(); ++i) { |
| 94 View* child_view = node->child_at(i); |
| 95 out_children->push_back(child_view); |
| 96 } |
| 97 } |
| 98 |
| 99 View* AXTreeSourceViews::GetParent(View* node) const { |
| 100 return node->parent(); |
| 101 } |
| 102 |
| 103 bool AXTreeSourceViews::IsValid(View* node) const { |
| 104 if (root_ && root_->GetRootView()) |
| 105 return root_->GetRootView()->Contains(node); |
| 106 return false; |
| 107 } |
| 108 |
| 109 bool AXTreeSourceViews::IsEqual(View* node1, |
| 110 View* node2) const { |
| 111 return node1 == node2; |
| 112 } |
| 113 |
| 114 View* AXTreeSourceViews::GetNull() const { |
| 115 return NULL; |
| 116 } |
| 117 |
| 118 void AXTreeSourceViews::SerializeNode( |
| 119 View* node, ui::AXNodeData* out_data) const { |
| 120 ui::AXViewState view_data; |
| 121 const_cast<View*>(node)->GetAccessibleState(&view_data); |
| 122 out_data->id = view_store_->GetOrStoreView(node); |
| 123 out_data->role = view_data.role; |
| 124 out_data->state = view_data.state; |
| 125 out_data->location = node->GetBoundsInScreen(); |
| 126 out_data->AddStringAttribute( |
| 127 ui::AX_ATTR_NAME, base::UTF16ToUTF8(view_data.name)); |
| 128 out_data->AddStringAttribute( |
| 129 ui::AX_ATTR_VALUE, base::UTF16ToUTF8(view_data.value)); |
| 130 } |
| 131 |
| 132 void AXTreeSourceViews::OnViewRemoved(Widget* widget, View* view) { |
| 133 if (widget == root_) |
| 134 view_store_->RemoveView(view); |
| 135 } |
| 136 |
| 137 void AXTreeSourceViews::OnWidgetDestroyed(Widget* widget) { |
| 138 if (widget == root_) |
| 139 root_ = NULL; |
| 140 } |
| 141 |
| 142 } // namespace views |
OLD | NEW |