ax_tree_id.h 2.6 KB

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