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