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.
4 
5 #include "ui/accessibility/ax_tree_data.h"
6 
7 #include <set>
8 
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "ui/accessibility/ax_enum_util.h"
14 #include "ui/accessibility/ax_enums.mojom.h"
15 
16 namespace ui {
17 
AXTreeData()18 AXTreeData::AXTreeData()
19     : sel_anchor_affinity(ax::mojom::TextAffinity::kDownstream),
20       sel_focus_affinity(ax::mojom::TextAffinity::kDownstream) {}
21 
22 AXTreeData::AXTreeData(const AXTreeData& other) = default;
23 AXTreeData::~AXTreeData() = default;
24 
25 // Note that this includes an initial space character if nonempty, but
26 // that works fine because this is normally printed by AXTree::ToString.
ToString() const27 std::string AXTreeData::ToString() const {
28   std::string result;
29 
30   if (tree_id != AXTreeIDUnknown())
31     result += " tree_id=" + tree_id.ToString().substr(0, 8);
32   if (parent_tree_id != AXTreeIDUnknown())
33     result += " parent_tree_id=" + parent_tree_id.ToString().substr(0, 8);
34   if (focused_tree_id != AXTreeIDUnknown())
35     result += " focused_tree_id=" + focused_tree_id.ToString().substr(0, 8);
36 
37   if (!doctype.empty())
38     result += " doctype=" + doctype;
39   if (loaded)
40     result += " loaded=true";
41   if (loading_progress != 0.0f)
42     result += " loading_progress=" + base::NumberToString(loading_progress);
43   if (!mimetype.empty())
44     result += " mimetype=" + mimetype;
45   if (!url.empty())
46     result += " url=" + url;
47   if (!title.empty())
48     result += " title=" + title;
49 
50   if (focus_id != AXNode::kInvalidAXID)
51     result += " focus_id=" + base::NumberToString(focus_id);
52 
53   if (sel_anchor_object_id != AXNode::kInvalidAXID) {
54     result +=
55         (sel_is_backward ? " sel_is_backward=true" : " sel_is_backward=false");
56     result +=
57         " sel_anchor_object_id=" + base::NumberToString(sel_anchor_object_id);
58     result += " sel_anchor_offset=" + base::NumberToString(sel_anchor_offset);
59     result += " sel_anchor_affinity=";
60     result += ui::ToString(sel_anchor_affinity);
61   }
62   if (sel_focus_object_id != AXNode::kInvalidAXID) {
63     result +=
64         " sel_focus_object_id=" + base::NumberToString(sel_focus_object_id);
65     result += " sel_focus_offset=" + base::NumberToString(sel_focus_offset);
66     result += " sel_focus_affinity=";
67     result += ui::ToString(sel_focus_affinity);
68   }
69 
70   return result;
71 }
72 
operator ==(const AXTreeData & lhs,const AXTreeData & rhs)73 bool operator==(const AXTreeData& lhs, const AXTreeData& rhs) {
74   return (lhs.tree_id == rhs.tree_id &&
75           lhs.parent_tree_id == rhs.parent_tree_id &&
76           lhs.focused_tree_id == rhs.focused_tree_id &&
77           lhs.doctype == rhs.doctype && lhs.loaded == rhs.loaded &&
78           lhs.loading_progress == rhs.loading_progress &&
79           lhs.mimetype == rhs.mimetype && lhs.title == rhs.title &&
80           lhs.url == rhs.url && lhs.focus_id == rhs.focus_id &&
81           lhs.sel_anchor_object_id == rhs.sel_anchor_object_id &&
82           lhs.sel_anchor_offset == rhs.sel_anchor_offset &&
83           lhs.sel_anchor_affinity == rhs.sel_anchor_affinity &&
84           lhs.sel_focus_object_id == rhs.sel_focus_object_id &&
85           lhs.sel_focus_offset == rhs.sel_focus_offset &&
86           lhs.sel_focus_affinity == rhs.sel_focus_affinity);
87 }
88 
operator !=(const AXTreeData & lhs,const AXTreeData & rhs)89 bool operator!=(const AXTreeData& lhs, const AXTreeData& rhs) {
90   return !(lhs == rhs);
91 }
92 
93 }  // namespace ui
94