1 // Copyright 2017 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_MODE_H_
6 #define UI_ACCESSIBILITY_AX_MODE_H_
7 
8 #include <stdint.h>
9 
10 #include <ostream>
11 #include <string>
12 
13 #include "base/logging.h"
14 #include "ui/accessibility/ax_export.h"
15 
16 namespace ui {
17 
18 class AX_EXPORT AXMode {
19  public:
20   static constexpr uint32_t kFirstModeFlag = 1 << 0;
21 
22   // Native accessibility APIs, specific to each platform, are enabled.
23   // When this mode is set that indicates the presence of a third-party
24   // client accessing Chrome via accessibility APIs. However, unless one
25   // of the modes below is set, the contents of web pages will not be
26   // accessible.
27   static constexpr uint32_t kNativeAPIs = 1 << 0;
28 
29   // The renderer process will generate an accessibility tree containing
30   // basic information about all nodes, including role, name, value,
31   // state, and location. This is the minimum mode required in order for
32   // web contents to be accessible, and the remaining modes are meaningless
33   // unless this one is set.
34   //
35   // Note that sometimes this mode will be set when kNativeAPI is not, when the
36   // content layer embedder is providing accessibility support via some other
37   // mechanism other than what's implemented in content/browser.
38   static constexpr uint32_t kWebContents = 1 << 1;
39 
40   // The accessibility tree will contain inline text boxes, which are
41   // necessary to expose information about line breaks and word boundaries.
42   // Without this mode, you can retrieve the plaintext value of a text field
43   // but not the information about how it's broken down into lines.
44   //
45   // Note that when this mode is off it's still possible to request inline
46   // text boxes for a specific node on-demand, asynchronously.
47   static constexpr uint32_t kInlineTextBoxes = 1 << 2;
48 
49   // The accessibility tree will contain extra accessibility
50   // attributes typically only needed by screen readers and other
51   // assistive technology for blind users. Examples include text style
52   // attributes, table cell information, live region properties, range
53   // values, and relationship attributes.
54   static constexpr uint32_t kScreenReader = 1 << 3;
55 
56   // The accessibility tree will contain the HTML tag name and HTML attributes
57   // for all accessibility nodes that come from web content.
58   static constexpr uint32_t kHTML = 1 << 4;
59 
60   // The accessibility tree will contain automatic image annotations.
61   static constexpr uint32_t kLabelImages = 1 << 5;
62 
63   // Update this to include the last supported mode flag. If you add
64   // another, be sure to update the stream insertion operator for
65   // logging and debugging.
66   static constexpr uint32_t kLastModeFlag = 1 << 5;
67 
AXMode()68   constexpr AXMode() : flags_(0) {}
AXMode(uint32_t flags)69   constexpr AXMode(uint32_t flags) : flags_(flags) {}
70 
has_mode(uint32_t flag)71   bool has_mode(uint32_t flag) const { return (flags_ & flag) > 0; }
72 
set_mode(uint32_t flag,bool value)73   void set_mode(uint32_t flag, bool value) {
74     flags_ = value ? (flags_ | flag) : (flags_ & ~flag);
75   }
76 
mode()77   uint32_t mode() const { return flags_; }
78 
79   bool operator==(AXMode rhs) const { return flags_ == rhs.flags_; }
80 
is_mode_off()81   bool is_mode_off() const { return flags_ == 0; }
82 
83   bool operator!=(AXMode rhs) const { return !(*this == rhs); }
84 
85   AXMode& operator|=(const AXMode& rhs) {
86     flags_ |= rhs.flags_;
87     return *this;
88   }
89 
90   std::string ToString() const;
91 
92  private:
93   uint32_t flags_;
94 };
95 
96 static constexpr AXMode kAXModeWebContentsOnly(AXMode::kWebContents |
97                                                AXMode::kInlineTextBoxes |
98                                                AXMode::kScreenReader |
99                                                AXMode::kHTML);
100 
101 static constexpr AXMode kAXModeComplete(AXMode::kNativeAPIs |
102                                         AXMode::kWebContents |
103                                         AXMode::kInlineTextBoxes |
104                                         AXMode::kScreenReader | AXMode::kHTML);
105 
106 // For debugging, test assertions, etc.
107 AX_EXPORT std::ostream& operator<<(std::ostream& stream, const AXMode& mode);
108 
109 }  // namespace ui
110 
111 #endif  // UI_ACCESSIBILITY_AX_MODE_H_
112