1 // Copyright 2018 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_AX_TREE_ID_H_
6 #define UI_ACCESSIBILITY_AX_TREE_ID_H_
7 
8 #include <string>
9 
10 #include "base/no_destructor.h"
11 #include "base/unguessable_token.h"
12 #include "ui/accessibility/ax_base_export.h"
13 #include "ui/accessibility/ax_enums.mojom-forward.h"
14 
15 namespace mojo {
16 template <typename DataViewType, typename T>
17 struct UnionTraits;
18 }
19 
20 namespace ax {
21 namespace mojom {
22 class AXTreeIDDataView;
23 }
24 }  // namespace ax
25 
26 namespace ui {
27 
28 // A unique ID representing an accessibility tree.
29 class AX_BASE_EXPORT AXTreeID {
30  public:
31   // Create an Unknown AXTreeID.
32   AXTreeID();
33 
34   // Copy constructor.
35   AXTreeID(const AXTreeID& other);
36 
37   // Create a new unique AXTreeID.
38   static AXTreeID CreateNewAXTreeID();
39 
40   // Unserialize an AXTreeID from a string. This is used so that tree IDs
41   // can be stored compactly as a string attribute in an AXNodeData, and
42   // so that AXTreeIDs can be passed to JavaScript bindings in the
43   // automation API.
44   static AXTreeID FromString(const std::string& string);
45 
46   // Convenience method to unserialize an AXTreeID from an UnguessableToken.
47   static AXTreeID FromToken(const base::UnguessableToken& token);
48 
49   AXTreeID& operator=(const AXTreeID& other);
50 
51   std::string ToString() const;
52 
type()53   ax::mojom::AXTreeIDType type() const { return type_; }
token()54   const base::Optional<base::UnguessableToken>& token() const { return token_; }
55 
56   bool operator==(const AXTreeID& rhs) const;
57   bool operator!=(const AXTreeID& rhs) const;
58   bool operator<(const AXTreeID& rhs) const;
59   bool operator<=(const AXTreeID& rhs) const;
60   bool operator>(const AXTreeID& rhs) const;
61   bool operator>=(const AXTreeID& rhs) const;
62 
63  private:
64   explicit AXTreeID(ax::mojom::AXTreeIDType type);
65   explicit AXTreeID(const std::string& string);
66 
67   friend struct mojo::UnionTraits<ax::mojom::AXTreeIDDataView, ui::AXTreeID>;
68   friend class base::NoDestructor<AXTreeID>;
69   friend void swap(AXTreeID& first, AXTreeID& second);
70 
71   ax::mojom::AXTreeIDType type_;
72   base::Optional<base::UnguessableToken> token_ = base::nullopt;
73 };
74 
75 // For use in std::unordered_map.
76 struct AX_BASE_EXPORT AXTreeIDHash {
77   size_t operator()(const ui::AXTreeID& tree_id) const;
78 };
79 
80 AX_BASE_EXPORT std::ostream& operator<<(std::ostream& stream,
81                                         const AXTreeID& value);
82 
83 // The value to use when an AXTreeID is unknown.
84 AX_BASE_EXPORT extern const AXTreeID& AXTreeIDUnknown();
85 
86 }  // namespace ui
87 
88 #endif  // UI_ACCESSIBILITY_AX_TREE_ID_H_
89