Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: content/browser/accessibility/one_shot_accessibility_tree_search.cc

Issue 1134653003: Implement UIElementsForSearchPredicate accessibility APIs for OS X. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address feedback Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 "content/browser/accessibility/one_shot_accessibility_tree_search.h"
6
7 #include "base/i18n/case_conversion.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/browser/accessibility/browser_accessibility.h"
11 #include "content/browser/accessibility/browser_accessibility_manager.h"
12
13 namespace content {
14
15 // Given a node, populate a vector with all of the strings from that node's
16 // attributes that might be relevant for a text search.
17 void GetNodeStrings(BrowserAccessibility* node,
18 std::vector<base::string16>* strings) {
19 if (node->HasStringAttribute(ui::AX_ATTR_NAME))
20 strings->push_back(node->GetString16Attribute(ui::AX_ATTR_NAME));
21 if (node->HasStringAttribute(ui::AX_ATTR_DESCRIPTION))
22 strings->push_back(node->GetString16Attribute(ui::AX_ATTR_DESCRIPTION));
23 if (node->HasStringAttribute(ui::AX_ATTR_HELP))
24 strings->push_back(node->GetString16Attribute(ui::AX_ATTR_HELP));
25 if (node->HasStringAttribute(ui::AX_ATTR_VALUE))
26 strings->push_back(node->GetString16Attribute(ui::AX_ATTR_VALUE));
27 if (node->HasStringAttribute(ui::AX_ATTR_PLACEHOLDER))
28 strings->push_back(node->GetString16Attribute(ui::AX_ATTR_PLACEHOLDER));
29 }
30
31 OneShotAccessibilityTreeSearch::OneShotAccessibilityTreeSearch(
32 BrowserAccessibilityManager* tree)
33 : tree_(tree),
34 start_node_(nullptr),
35 direction_(OneShotAccessibilityTreeSearch::FORWARDS),
36 result_limit_(UNLIMITED_RESULTS),
37 immediate_descendants_only_(false),
38 visible_only_(false),
39 did_search_(false) {
40 }
41
42 OneShotAccessibilityTreeSearch::~OneShotAccessibilityTreeSearch() {
43 }
44
45 void OneShotAccessibilityTreeSearch::SetStartNode(
46 BrowserAccessibility* start_node) {
47 DCHECK(!did_search_);
48 start_node_ = start_node;
49 }
50
51 void OneShotAccessibilityTreeSearch::SetDirection(Direction direction) {
52 DCHECK(!did_search_);
53 direction_ = direction;
54 }
55
56 void OneShotAccessibilityTreeSearch::SetResultLimit(int result_limit) {
57 DCHECK(!did_search_);
58 result_limit_ = result_limit;
59 }
60
61 void OneShotAccessibilityTreeSearch::SetImmediateDescendantsOnly(
62 bool immediate_descendants_only) {
63 DCHECK(!did_search_);
64 immediate_descendants_only_ = immediate_descendants_only;
65 }
66
67 void OneShotAccessibilityTreeSearch::SetVisibleOnly(bool visible_only) {
68 DCHECK(!did_search_);
69 visible_only_ = visible_only;
70 }
71
72 void OneShotAccessibilityTreeSearch::SetSearchText(const std::string& text) {
73 DCHECK(!did_search_);
74 search_text_ = text;
75 }
76
77 void OneShotAccessibilityTreeSearch::AddPredicate(
78 AccessibilityMatchPredicate predicate) {
79 DCHECK(!did_search_);
80 predicates_.push_back(predicate);
81 }
82
83 size_t OneShotAccessibilityTreeSearch::CountMatches() {
84 if (!did_search_)
85 Search();
86
87 return matches_.size();
88 }
89
90 BrowserAccessibility* OneShotAccessibilityTreeSearch::GetMatchAtIndex(
91 size_t index) {
92 if (!did_search_)
93 Search();
94
95 CHECK(index < matches_.size());
96 return matches_[index];
97 }
98
99 void OneShotAccessibilityTreeSearch::Search()
100 {
101 if (immediate_descendants_only_) {
102 SearchByIteratingOverChildren();
103 } else {
104 SearchByWalkingTree();
105 }
106 }
107
108 void OneShotAccessibilityTreeSearch::SearchByIteratingOverChildren() {
109 if (!start_node_)
110 return;
111
112 for (unsigned i = 0;
113 i < start_node_->PlatformChildCount() &&
114 (result_limit_ == UNLIMITED_RESULTS ||
115 static_cast<int>(matches_.size()) < result_limit_);
116 ++i) {
117 BrowserAccessibility* child = start_node_->PlatformGetChild(i);
118 if (Matches(child))
119 matches_.push_back(child);
120 }
121 }
122
123 void OneShotAccessibilityTreeSearch::SearchByWalkingTree() {
124 BrowserAccessibility* node = nullptr;
125 if (start_node_) {
126 if (direction_ == FORWARDS)
127 node = tree_->NextInTreeOrder(start_node_);
128 else
129 node = tree_->PreviousInTreeOrder(start_node_);
130 } else {
131 start_node_ = tree_->GetRoot();
132 node = start_node_;
133 }
134
135 while (node && (result_limit_ == UNLIMITED_RESULTS ||
136 static_cast<int>(matches_.size()) < result_limit_)) {
137 if (Matches(node))
138 matches_.push_back(node);
139
140 if (direction_ == FORWARDS)
141 node = tree_->NextInTreeOrder(node);
142 else
143 node = tree_->PreviousInTreeOrder(node);
144 }
145 }
146
147 bool OneShotAccessibilityTreeSearch::Matches(BrowserAccessibility* node) {
148 for (size_t i = 0; i < predicates_.size(); ++i) {
149 if (!predicates_[i](start_node_, node))
150 return false;
151 }
152
153 if (visible_only_) {
154 if (node->HasState(ui::AX_STATE_INVISIBLE) ||
155 node->HasState(ui::AX_STATE_OFFSCREEN)) {
156 return false;
157 }
158 }
159
160 if (!search_text_.empty()) {
161 base::string16 search_text_lower =
162 base::i18n::ToLower(base::UTF8ToUTF16(search_text_));
163 std::vector<base::string16> node_strings;
164 GetNodeStrings(node, &node_strings);
165 bool found_text_match = false;
166 for (size_t i = 0; i < node_strings.size(); ++i) {
167 base::string16 node_string_lower =
168 base::i18n::ToLower(node_strings[i]);
169 if (node_string_lower.find(search_text_lower) !=
170 base::string16::npos) {
171 found_text_match = true;
172 break;
173 }
174 }
175 if (!found_text_match)
176 return false;
177 }
178
179 return true;
180 }
181
182 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698