Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_state_impl.cc
blob31b9aebc034e2e422ca0ad4bc316d801e902f8a3
1 // 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.
5 #include "content/browser/accessibility/browser_accessibility_state_impl.h"
7 #include "base/command_line.h"
8 #include "base/metrics/histogram.h"
9 #include "content/browser/accessibility/accessibility_mode_helper.h"
10 #include "content/browser/renderer_host/render_widget_host_impl.h"
11 #include "content/browser/web_contents/web_contents_impl.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/common/content_switches.h"
14 #include "ui/gfx/color_utils.h"
16 namespace content {
18 // Update the accessibility histogram 45 seconds after initialization.
19 static const int kAccessibilityHistogramDelaySecs = 45;
21 // static
22 BrowserAccessibilityState* BrowserAccessibilityState::GetInstance() {
23 return BrowserAccessibilityStateImpl::GetInstance();
26 // static
27 BrowserAccessibilityStateImpl* BrowserAccessibilityStateImpl::GetInstance() {
28 return base::Singleton<
29 BrowserAccessibilityStateImpl,
30 base::LeakySingletonTraits<BrowserAccessibilityStateImpl>>::get();
33 BrowserAccessibilityStateImpl::BrowserAccessibilityStateImpl()
34 : BrowserAccessibilityState(),
35 accessibility_mode_(AccessibilityModeOff),
36 disable_hot_tracking_(false) {
37 ResetAccessibilityModeValue();
38 #if defined(OS_WIN)
39 // On Windows, UpdateHistograms calls some system functions with unknown
40 // runtime, so call it on the file thread to ensure there's no jank.
41 // Everything in that method must be safe to call on another thread.
42 BrowserThread::ID update_histogram_thread = BrowserThread::FILE;
43 #else
44 // On all other platforms, UpdateHistograms should be called on the main
45 // thread.
46 BrowserThread::ID update_histogram_thread = BrowserThread::UI;
47 #endif
49 // We need to AddRef() the leaky singleton so that Bind doesn't
50 // delete it prematurely.
51 AddRef();
52 BrowserThread::PostDelayedTask(
53 update_histogram_thread, FROM_HERE,
54 base::Bind(&BrowserAccessibilityStateImpl::UpdateHistograms, this),
55 base::TimeDelta::FromSeconds(kAccessibilityHistogramDelaySecs));
58 BrowserAccessibilityStateImpl::~BrowserAccessibilityStateImpl() {
61 void BrowserAccessibilityStateImpl::OnScreenReaderDetected() {
62 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
63 switches::kDisableRendererAccessibility)) {
64 return;
66 EnableAccessibility();
69 void BrowserAccessibilityStateImpl::EnableAccessibility() {
70 AddAccessibilityMode(AccessibilityModeComplete);
73 void BrowserAccessibilityStateImpl::DisableAccessibility() {
74 ResetAccessibilityMode();
77 void BrowserAccessibilityStateImpl::ResetAccessibilityModeValue() {
78 accessibility_mode_ = AccessibilityModeOff;
79 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
80 switches::kForceRendererAccessibility)) {
81 accessibility_mode_ = AccessibilityModeComplete;
85 void BrowserAccessibilityStateImpl::ResetAccessibilityMode() {
86 ResetAccessibilityModeValue();
88 std::vector<WebContentsImpl*> web_contents_vector =
89 WebContentsImpl::GetAllWebContents();
90 for (size_t i = 0; i < web_contents_vector.size(); ++i)
91 web_contents_vector[i]->SetAccessibilityMode(accessibility_mode());
94 bool BrowserAccessibilityStateImpl::IsAccessibleBrowser() {
95 return ((accessibility_mode_ & AccessibilityModeComplete) ==
96 AccessibilityModeComplete);
99 void BrowserAccessibilityStateImpl::AddHistogramCallback(
100 base::Closure callback) {
101 histogram_callbacks_.push_back(callback);
104 void BrowserAccessibilityStateImpl::UpdateHistogramsForTesting() {
105 UpdateHistograms();
108 void BrowserAccessibilityStateImpl::UpdateHistograms() {
109 UpdatePlatformSpecificHistograms();
111 for (size_t i = 0; i < histogram_callbacks_.size(); ++i)
112 histogram_callbacks_[i].Run();
114 UMA_HISTOGRAM_BOOLEAN("Accessibility.State", IsAccessibleBrowser());
115 UMA_HISTOGRAM_BOOLEAN("Accessibility.InvertedColors",
116 color_utils::IsInvertedColorScheme());
117 UMA_HISTOGRAM_BOOLEAN("Accessibility.ManuallyEnabled",
118 base::CommandLine::ForCurrentProcess()->HasSwitch(
119 switches::kForceRendererAccessibility));
122 #if !defined(OS_WIN)
123 void BrowserAccessibilityStateImpl::UpdatePlatformSpecificHistograms() {
125 #endif
127 void BrowserAccessibilityStateImpl::AddAccessibilityMode(
128 AccessibilityMode mode) {
129 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
130 switches::kDisableRendererAccessibility)) {
131 return;
134 accessibility_mode_ =
135 content::AddAccessibilityModeTo(accessibility_mode_, mode);
137 AddOrRemoveFromAllWebContents(mode, true);
140 void BrowserAccessibilityStateImpl::RemoveAccessibilityMode(
141 AccessibilityMode mode) {
142 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
143 switches::kForceRendererAccessibility) &&
144 mode == AccessibilityModeComplete) {
145 return;
148 accessibility_mode_ =
149 content::RemoveAccessibilityModeFrom(accessibility_mode_, mode);
151 AddOrRemoveFromAllWebContents(mode, false);
154 void BrowserAccessibilityStateImpl::AddOrRemoveFromAllWebContents(
155 AccessibilityMode mode,
156 bool add) {
157 std::vector<WebContentsImpl*> web_contents_vector =
158 WebContentsImpl::GetAllWebContents();
159 for (size_t i = 0; i < web_contents_vector.size(); ++i) {
160 if (add)
161 web_contents_vector[i]->AddAccessibilityMode(mode);
162 else
163 web_contents_vector[i]->RemoveAccessibilityMode(mode);
167 } // namespace content