1 // Copyright 2020 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 #ifndef UI_ACCESSIBILITY_PLATFORM_INSPECT_PROPERTY_NODE_H_
6 #define UI_ACCESSIBILITY_PLATFORM_INSPECT_PROPERTY_NODE_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "ui/accessibility/ax_export.h"
12 
13 namespace base {
14 template <typename T>
15 class Optional;
16 }
17 
18 namespace ui {
19 
20 struct AXPropertyFilter;
21 
22 // Property node is a tree-like structure, representing a property or collection
23 // of properties and its invocation parameters. A collection of properties is
24 // specified by putting a wildcard into a property name, for exampe, AXRole*
25 // will match both AXRole and AXRoleDescription properties. Parameters of a
26 // property are given in parentheses like a conventional function call, for
27 // example, AXCellForColumnAndRow([0, 0]) will call AXCellForColumnAndRow
28 // parameterized property for column/row 0 indexes.
29 class AX_EXPORT AXPropertyNode final {
30  public:
31   // Parses a property node from a property filter.
32   static AXPropertyNode From(const AXPropertyFilter& filter);
33 
34   AXPropertyNode();
35   AXPropertyNode(AXPropertyNode&&);
36   ~AXPropertyNode();
37 
38   AXPropertyNode& operator=(AXPropertyNode&& other);
39   explicit operator bool() const;
40 
41   // Key name in case of { key: value } dictionary.
42   std::string key;
43 
44   // An object the property should be called for, designated by a line number
45   // in accessible tree the object is located at. For example, :1 indicates
46   // that the property should be called for an object located at first line.
47   std::string target;
48 
49   // Value or a property name, for example 3 or AXLineForIndex
50   std::string name_or_value;
51 
52   // Parameters if it's a property, for example, it is a vector of a single
53   // value 3 in case of AXLineForIndex(3)
54   std::vector<AXPropertyNode> parameters;
55 
56   // Used to store the origianl unparsed property including invocation
57   // parameters if any.
58   std::string original_property;
59 
60   // The list of line indexes of accessible objects the property is allowed to
61   // be called for, used if no property target is provided.
62   std::vector<std::string> line_indexes;
63 
64   bool IsMatching(const std::string& pattern) const;
65 
66   // Argument conversion methods.
67   bool IsArray() const;
68   bool IsDict() const;
69   base::Optional<int> AsInt() const;
70   const AXPropertyNode* FindKey(const char* refkey) const;
71   base::Optional<std::string> FindStringKey(const char* refkey) const;
72   base::Optional<int> FindIntKey(const char* key) const;
73 
74   std::string ToString() const;
75 
76  private:
77   using iterator = std::string::const_iterator;
78 
79   explicit AXPropertyNode(iterator key_begin,
80                           iterator key_end,
81                           const std::string&);
82   AXPropertyNode(iterator begin, iterator end);
83   AXPropertyNode(iterator key_begin,
84                  iterator key_end,
85                  iterator value_begin,
86                  iterator value_end);
87 
88   // Helper to set context and name.
89   void Set(iterator begin, iterator end);
90 
91   // Builds a property node struct for a string of NAME(ARG1, ..., ARGN) format,
92   // where each ARG is a scalar value or a string of the same format.
93   static iterator Parse(AXPropertyNode* node, iterator begin, iterator end);
94 };
95 
96 }  // namespace ui
97 
98 #endif  // UI_ACCESSIBILITY_PLATFORM_INSPECT_PROPERTY_NODE_H_
99