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_enums.mojom-forward.h"
13 #include "ui/accessibility/ax_export.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_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   AXTreeID& operator=(const AXTreeID& other);
47 
48   std::string ToString() const;
49 
type()50   ax::mojom::AXTreeIDType type() const { return type_; }
token()51   const base::Optional<base::UnguessableToken>& token() const { return token_; }
52 
53   bool operator==(const AXTreeID& rhs) const;
54   bool operator!=(const AXTreeID& rhs) const;
55   bool operator<(const AXTreeID& rhs) const;
56   bool operator<=(const AXTreeID& rhs) const;
57   bool operator>(const AXTreeID& rhs) const;
58   bool operator>=(const AXTreeID& rhs) const;
59 
60  private:
61   explicit AXTreeID(ax::mojom::AXTreeIDType type);
62   explicit AXTreeID(const std::string& string);
63 
64   friend struct mojo::UnionTraits<ax::mojom::AXTreeIDDataView, ui::AXTreeID>;
65   friend class base::NoDestructor<AXTreeID>;
66   friend void swap(AXTreeID& first, AXTreeID& second);
67 
68   ax::mojom::AXTreeIDType type_;
69   base::Optional<base::UnguessableToken> token_ = base::nullopt;
70 };
71 
72 // For use in std::unordered_map.
73 struct AXTreeIDHash {
74   size_t operator()(const ui::AXTreeID& tree_id) const;
75 };
76 
77 AX_EXPORT std::ostream& operator<<(std::ostream& stream, const AXTreeID& value);
78 
79 // The value to use when an AXTreeID is unknown.
80 AX_EXPORT extern const AXTreeID& AXTreeIDUnknown();
81 
82 }  // namespace ui
83 
84 #endif  // UI_ACCESSIBILITY_AX_TREE_ID_H_
85