blob: faf6a7bd412ba3745d4e9a7a84d8965df4ccbb61 [file] [log] [blame]
[email protected]fa4f91682012-02-21 19:53:261// Copyright (c) 2012 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 "content/browser/accessibility/browser_accessibility_state_impl.h"
6
avib7348942015-12-25 20:57:107#include <stddef.h>
8
[email protected]3e3c4522012-04-13 21:16:299#include "base/command_line.h"
asvitkine30330812016-08-30 04:01:0810#include "base/metrics/histogram_macros.h"
avib7348942015-12-25 20:57:1011#include "build/build_config.h"
[email protected]df376972013-05-03 21:45:0312#include "content/browser/renderer_host/render_widget_host_impl.h"
[email protected]95640212014-07-26 18:14:3013#include "content/browser/web_contents/web_contents_impl.h"
[email protected]18df7362012-10-24 01:29:4014#include "content/public/browser/browser_thread.h"
[email protected]3e3c4522012-04-13 21:16:2915#include "content/public/common/content_switches.h"
mlamouri20bb4c4a2015-07-08 14:30:2216#include "ui/gfx/color_utils.h"
[email protected]fa4f91682012-02-21 19:53:2617
[email protected]e6b34872012-10-24 20:51:3218namespace content {
19
dmazzoni368ea132016-12-20 08:22:4220// IMPORTANT!
21// These values are written to logs. Do not renumber or delete
22// existing items; add new entries to the end of the list.
23enum ModeFlagHistogramValue {
dougtcd3dad732017-03-14 03:26:2324 UMA_AX_MODE_NATIVE_APIS = 0,
25 UMA_AX_MODE_WEB_CONTENTS = 1,
26 UMA_AX_MODE_INLINE_TEXT_BOXES = 2,
27 UMA_AX_MODE_SCREEN_READER = 3,
28 UMA_AX_MODE_HTML = 4,
dmazzoni368ea132016-12-20 08:22:4229
30 // This must always be the last enum. It's okay for its value to
31 // increase, but none of the other enum values may change.
dougtcd3dad732017-03-14 03:26:2332 UMA_AX_MODE_MAX
dmazzoni368ea132016-12-20 08:22:4233};
34
35// Record a histograms for an accessibility mode when it's enabled.
36void RecordNewAccessibilityModeFlags(ModeFlagHistogramValue mode_flag) {
dougtcd3dad732017-03-14 03:26:2337 UMA_HISTOGRAM_ENUMERATION("Accessibility.ModeFlag", mode_flag,
38 UMA_AX_MODE_MAX);
dmazzoni368ea132016-12-20 08:22:4239}
40
[email protected]fa4f91682012-02-21 19:53:2641// Update the accessibility histogram 45 seconds after initialization.
dmazzoni368ea132016-12-20 08:22:4242static const int ACCESSIBILITY_HISTOGRAM_DELAY_SECS = 45;
[email protected]fa4f91682012-02-21 19:53:2643
44// static
45BrowserAccessibilityState* BrowserAccessibilityState::GetInstance() {
46 return BrowserAccessibilityStateImpl::GetInstance();
47}
48
49// static
50BrowserAccessibilityStateImpl* BrowserAccessibilityStateImpl::GetInstance() {
olli.raula36aa8be2015-09-10 11:14:2251 return base::Singleton<
52 BrowserAccessibilityStateImpl,
53 base::LeakySingletonTraits<BrowserAccessibilityStateImpl>>::get();
[email protected]fa4f91682012-02-21 19:53:2654}
55
56BrowserAccessibilityStateImpl::BrowserAccessibilityStateImpl()
57 : BrowserAccessibilityState(),
dmazzonidea5ba62015-02-03 19:27:2458 disable_hot_tracking_(false) {
[email protected]1e558fa2014-02-12 23:28:5259 ResetAccessibilityModeValue();
[email protected]882db442012-12-04 16:37:5460#if defined(OS_WIN)
[email protected]30fdb362013-01-09 02:33:2761 // On Windows, UpdateHistograms calls some system functions with unknown
[email protected]882db442012-12-04 16:37:5462 // runtime, so call it on the file thread to ensure there's no jank.
63 // Everything in that method must be safe to call on another thread.
64 BrowserThread::ID update_histogram_thread = BrowserThread::FILE;
65#else
[email protected]30fdb362013-01-09 02:33:2766 // On all other platforms, UpdateHistograms should be called on the main
[email protected]882db442012-12-04 16:37:5467 // thread.
68 BrowserThread::ID update_histogram_thread = BrowserThread::UI;
69#endif
70
71 // We need to AddRef() the leaky singleton so that Bind doesn't
[email protected]3c17ae742012-10-24 17:37:2672 // delete it prematurely.
73 AddRef();
[email protected]e6b34872012-10-24 20:51:3274 BrowserThread::PostDelayedTask(
[email protected]882db442012-12-04 16:37:5475 update_histogram_thread, FROM_HERE,
[email protected]30fdb362013-01-09 02:33:2776 base::Bind(&BrowserAccessibilityStateImpl::UpdateHistograms, this),
dmazzoni368ea132016-12-20 08:22:4277 base::TimeDelta::FromSeconds(ACCESSIBILITY_HISTOGRAM_DELAY_SECS));
[email protected]fa4f91682012-02-21 19:53:2678}
79
80BrowserAccessibilityStateImpl::~BrowserAccessibilityStateImpl() {
81}
82
83void BrowserAccessibilityStateImpl::OnScreenReaderDetected() {
[email protected]479278702014-08-11 20:32:0984 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]bfa71242014-03-27 21:10:2485 switches::kDisableRendererAccessibility)) {
86 return;
87 }
88 EnableAccessibility();
[email protected]fa4f91682012-02-21 19:53:2689}
90
[email protected]df376972013-05-03 21:45:0391void BrowserAccessibilityStateImpl::EnableAccessibility() {
dougtcd3dad732017-03-14 03:26:2392 AddAccessibilityModeFlags(kAccessibilityModeComplete);
[email protected]fa4f91682012-02-21 19:53:2693}
94
[email protected]df376972013-05-03 21:45:0395void BrowserAccessibilityStateImpl::DisableAccessibility() {
[email protected]1e558fa2014-02-12 23:28:5296 ResetAccessibilityMode();
97}
98
99void BrowserAccessibilityStateImpl::ResetAccessibilityModeValue() {
dougtcd3dad732017-03-14 03:26:23100 accessibility_mode_ = AccessibilityMode();
[email protected]479278702014-08-11 20:32:09101 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]1e558fa2014-02-12 23:28:52102 switches::kForceRendererAccessibility)) {
dougtcd3dad732017-03-14 03:26:23103 accessibility_mode_ = kAccessibilityModeComplete;
[email protected]1e558fa2014-02-12 23:28:52104 }
105}
106
107void BrowserAccessibilityStateImpl::ResetAccessibilityMode() {
108 ResetAccessibilityModeValue();
109
[email protected]95640212014-07-26 18:14:30110 std::vector<WebContentsImpl*> web_contents_vector =
111 WebContentsImpl::GetAllWebContents();
112 for (size_t i = 0; i < web_contents_vector.size(); ++i)
113 web_contents_vector[i]->SetAccessibilityMode(accessibility_mode());
[email protected]df376972013-05-03 21:45:03114}
115
[email protected]fa4f91682012-02-21 19:53:26116bool BrowserAccessibilityStateImpl::IsAccessibleBrowser() {
dougtcd3dad732017-03-14 03:26:23117 return accessibility_mode_ == kAccessibilityModeComplete;
[email protected]fa4f91682012-02-21 19:53:26118}
119
[email protected]89430152012-12-03 19:18:56120void BrowserAccessibilityStateImpl::AddHistogramCallback(
121 base::Closure callback) {
122 histogram_callbacks_.push_back(callback);
123}
124
[email protected]30fdb362013-01-09 02:33:27125void BrowserAccessibilityStateImpl::UpdateHistogramsForTesting() {
126 UpdateHistograms();
127}
128
129void BrowserAccessibilityStateImpl::UpdateHistograms() {
[email protected]18df7362012-10-24 01:29:40130 UpdatePlatformSpecificHistograms();
131
[email protected]89430152012-12-03 19:18:56132 for (size_t i = 0; i < histogram_callbacks_.size(); ++i)
133 histogram_callbacks_[i].Run();
134
dmazzoni368ea132016-12-20 08:22:42135 // TODO(dmazzoni): remove this in M59 since Accessibility.ModeFlag
136 // supercedes it. http://crbug.com/672205
[email protected]18df7362012-10-24 01:29:40137 UMA_HISTOGRAM_BOOLEAN("Accessibility.State", IsAccessibleBrowser());
dmazzoni368ea132016-12-20 08:22:42138
[email protected]18df7362012-10-24 01:29:40139 UMA_HISTOGRAM_BOOLEAN("Accessibility.InvertedColors",
mlamouri20bb4c4a2015-07-08 14:30:22140 color_utils::IsInvertedColorScheme());
[email protected]18df7362012-10-24 01:29:40141 UMA_HISTOGRAM_BOOLEAN("Accessibility.ManuallyEnabled",
[email protected]479278702014-08-11 20:32:09142 base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]18df7362012-10-24 01:29:40143 switches::kForceRendererAccessibility));
[email protected]fa4f91682012-02-21 19:53:26144}
[email protected]e2fa1cca42012-08-22 14:07:27145
ellyjonesa577c6cb2016-04-01 22:31:03146#if !defined(OS_WIN) && !defined(OS_MACOSX)
[email protected]18df7362012-10-24 01:29:40147void BrowserAccessibilityStateImpl::UpdatePlatformSpecificHistograms() {
148}
149#endif
150
dmazzonidd3d51a72016-12-14 18:41:01151void BrowserAccessibilityStateImpl::AddAccessibilityModeFlags(
[email protected]e2fa1cca42012-08-22 14:07:27152 AccessibilityMode mode) {
[email protected]479278702014-08-11 20:32:09153 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]1e558fa2014-02-12 23:28:52154 switches::kDisableRendererAccessibility)) {
[email protected]df376972013-05-03 21:45:03155 return;
[email protected]1e558fa2014-02-12 23:28:52156 }
[email protected]df376972013-05-03 21:45:03157
dmazzoni368ea132016-12-20 08:22:42158 AccessibilityMode previous_mode = accessibility_mode_;
dmazzonidd3d51a72016-12-14 18:41:01159 accessibility_mode_ |= mode;
dmazzoni368ea132016-12-20 08:22:42160 if (accessibility_mode_ == previous_mode)
161 return;
162
163 // Retrieve only newly added modes for the purposes of logging.
dougtcd3dad732017-03-14 03:26:23164 int new_mode_flags = mode.mode() & (~previous_mode.mode());
165 if (new_mode_flags & AccessibilityMode::kNativeAPIs)
166 RecordNewAccessibilityModeFlags(UMA_AX_MODE_NATIVE_APIS);
167 if (new_mode_flags & AccessibilityMode::kWebContents)
168 RecordNewAccessibilityModeFlags(UMA_AX_MODE_WEB_CONTENTS);
169 if (new_mode_flags & AccessibilityMode::kInlineTextBoxes)
170 RecordNewAccessibilityModeFlags(UMA_AX_MODE_INLINE_TEXT_BOXES);
171 if (new_mode_flags & AccessibilityMode::kScreenReader)
172 RecordNewAccessibilityModeFlags(UMA_AX_MODE_SCREEN_READER);
173 if (new_mode_flags & AccessibilityMode::kHTML)
174 RecordNewAccessibilityModeFlags(UMA_AX_MODE_HTML);
dmazzoni368ea132016-12-20 08:22:42175
dmazzonidd3d51a72016-12-14 18:41:01176 std::vector<WebContentsImpl*> web_contents_vector =
177 WebContentsImpl::GetAllWebContents();
178 for (size_t i = 0; i < web_contents_vector.size(); ++i)
179 web_contents_vector[i]->AddAccessibilityMode(accessibility_mode_);
[email protected]1e558fa2014-02-12 23:28:52180}
181
dmazzonidd3d51a72016-12-14 18:41:01182void BrowserAccessibilityStateImpl::RemoveAccessibilityModeFlags(
[email protected]1e558fa2014-02-12 23:28:52183 AccessibilityMode mode) {
[email protected]479278702014-08-11 20:32:09184 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]1e558fa2014-02-12 23:28:52185 switches::kForceRendererAccessibility) &&
dougtcd3dad732017-03-14 03:26:23186 mode == kAccessibilityModeComplete) {
[email protected]1e558fa2014-02-12 23:28:52187 return;
188 }
189
dougtcd3dad732017-03-14 03:26:23190 int raw_flags =
191 accessibility_mode_.mode() ^ (mode.mode() & accessibility_mode_.mode());
192 accessibility_mode_ = raw_flags;
193
[email protected]95640212014-07-26 18:14:30194 std::vector<WebContentsImpl*> web_contents_vector =
195 WebContentsImpl::GetAllWebContents();
dmazzonidd3d51a72016-12-14 18:41:01196 for (size_t i = 0; i < web_contents_vector.size(); ++i)
197 web_contents_vector[i]->SetAccessibilityMode(accessibility_mode());
[email protected]e2fa1cca42012-08-22 14:07:27198}
[email protected]e6b34872012-10-24 20:51:32199
200} // namespace content