Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / content / browser / accessibility / one_shot_accessibility_tree_search.h
blobf1c8012cefaadd3cfc9392a920ddc0221c1835eb
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.
5 #ifndef CONTENT_BROWSER_ACCESSIBILITY_ACCESSIBILITY_TREE_SEARCH_H_
6 #define CONTENT_BROWSER_ACCESSIBILITY_ACCESSIBILITY_TREE_SEARCH_H_
8 #include <string>
9 #include <vector>
11 #include "base/macros.h"
12 #include "content/common/content_export.h"
14 namespace content {
16 class BrowserAccessibility;
17 class BrowserAccessibilityManager;
19 // A function that returns whether or not a given node matches, given the
20 // start element of the search as an optional comparator.
21 typedef bool (*AccessibilityMatchPredicate)(
22 BrowserAccessibility* start_element,
23 BrowserAccessibility* this_element);
25 // This class provides an interface for searching the accessibility tree from
26 // a given starting node, with a few built-in options and allowing an arbitrary
27 // number of predicates that can be used to restrict the search.
29 // This class is meant to perform one search. Initialize it, then iterate
30 // over the matches, and then delete it.
32 // This class stores raw pointers to the matches in the tree! Don't keep this
33 // object around if the tree is mutating.
34 class CONTENT_EXPORT OneShotAccessibilityTreeSearch {
35 public:
36 enum Direction { FORWARDS, BACKWARDS };
38 const int UNLIMITED_RESULTS = -1;
40 OneShotAccessibilityTreeSearch(BrowserAccessibilityManager* tree);
41 virtual ~OneShotAccessibilityTreeSearch();
44 // Search parameters. All of these are optional.
47 // Sets the node where the search starts. The first potential match will
48 // be the one immediately following this one. This node will be used as
49 // the first arguement to any predicates.
50 void SetStartNode(BrowserAccessibility* start_node);
52 // Search forwards or backwards in an in-order traversal of the tree.
53 void SetDirection(Direction direction);
55 // Set the maximum number of results, or UNLIMITED_RESULTS
56 // for no limit (default).
57 void SetResultLimit(int result_limit);
59 // If true, only searches children of |start_node| and doesn't
60 // recurse.
61 void SetImmediateDescendantsOnly(bool immediate_descendants_only);
63 // If true, only considers nodes that aren't invisible or offscreen.
64 void SetVisibleOnly(bool visible_only);
66 // Restricts the matches to only nodes where |text| is found as a
67 // substring of any of that node's accessible text, including its
68 // name, description, or value. Case-insensitive.
69 void SetSearchText(const std::string& text);
71 // Restricts the matches to only those that satisfy all predicates.
72 void AddPredicate(AccessibilityMatchPredicate predicate);
75 // Calling either of these executes the search.
78 size_t CountMatches();
79 BrowserAccessibility* GetMatchAtIndex(size_t index);
81 private:
82 void Search();
83 void SearchByWalkingTree();
84 void SearchByIteratingOverChildren();
85 bool Matches(BrowserAccessibility* node);
87 BrowserAccessibilityManager* tree_;
88 BrowserAccessibility* start_node_;
89 Direction direction_;
90 int result_limit_;
91 bool immediate_descendants_only_;
92 bool visible_only_;
93 std::string search_text_;
95 std::vector<AccessibilityMatchPredicate> predicates_;
96 std::vector<BrowserAccessibility*> matches_;
98 bool did_search_;
100 DISALLOW_COPY_AND_ASSIGN(OneShotAccessibilityTreeSearch);
103 } // namespace content
105 #endif // CONTENT_BROWSER_ACCESSIBILITY_ACCESSIBILITY_TREE_SEARCH_H_