Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / accessibility / browser_accessibility_state_impl.cc
blob597b13ac0c1e7ce56cb804f4fd709f98d730f884
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/sys_color_change_listener.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 Singleton<BrowserAccessibilityStateImpl,
29 LeakySingletonTraits<BrowserAccessibilityStateImpl> >::get();
32 BrowserAccessibilityStateImpl::BrowserAccessibilityStateImpl()
33 : BrowserAccessibilityState(),
34 accessibility_mode_(AccessibilityModeOff) {
35 ResetAccessibilityModeValue();
36 #if defined(OS_WIN)
37 // On Windows, UpdateHistograms calls some system functions with unknown
38 // runtime, so call it on the file thread to ensure there's no jank.
39 // Everything in that method must be safe to call on another thread.
40 BrowserThread::ID update_histogram_thread = BrowserThread::FILE;
41 #else
42 // On all other platforms, UpdateHistograms should be called on the main
43 // thread.
44 BrowserThread::ID update_histogram_thread = BrowserThread::UI;
45 #endif
47 // We need to AddRef() the leaky singleton so that Bind doesn't
48 // delete it prematurely.
49 AddRef();
50 BrowserThread::PostDelayedTask(
51 update_histogram_thread, FROM_HERE,
52 base::Bind(&BrowserAccessibilityStateImpl::UpdateHistograms, this),
53 base::TimeDelta::FromSeconds(kAccessibilityHistogramDelaySecs));
56 BrowserAccessibilityStateImpl::~BrowserAccessibilityStateImpl() {
59 void BrowserAccessibilityStateImpl::OnScreenReaderDetected() {
60 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
61 switches::kDisableRendererAccessibility)) {
62 return;
64 EnableAccessibility();
67 void BrowserAccessibilityStateImpl::EnableAccessibility() {
68 AddAccessibilityMode(AccessibilityModeComplete);
71 void BrowserAccessibilityStateImpl::DisableAccessibility() {
72 ResetAccessibilityMode();
75 void BrowserAccessibilityStateImpl::ResetAccessibilityModeValue() {
76 accessibility_mode_ = AccessibilityModeOff;
77 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
78 switches::kForceRendererAccessibility)) {
79 accessibility_mode_ = AccessibilityModeComplete;
83 void BrowserAccessibilityStateImpl::ResetAccessibilityMode() {
84 ResetAccessibilityModeValue();
86 std::vector<WebContentsImpl*> web_contents_vector =
87 WebContentsImpl::GetAllWebContents();
88 for (size_t i = 0; i < web_contents_vector.size(); ++i)
89 web_contents_vector[i]->SetAccessibilityMode(accessibility_mode());
92 bool BrowserAccessibilityStateImpl::IsAccessibleBrowser() {
93 return ((accessibility_mode_ & AccessibilityModeComplete) ==
94 AccessibilityModeComplete);
97 void BrowserAccessibilityStateImpl::AddHistogramCallback(
98 base::Closure callback) {
99 histogram_callbacks_.push_back(callback);
102 void BrowserAccessibilityStateImpl::UpdateHistogramsForTesting() {
103 UpdateHistograms();
106 void BrowserAccessibilityStateImpl::UpdateHistograms() {
107 UpdatePlatformSpecificHistograms();
109 for (size_t i = 0; i < histogram_callbacks_.size(); ++i)
110 histogram_callbacks_[i].Run();
112 UMA_HISTOGRAM_BOOLEAN("Accessibility.State", IsAccessibleBrowser());
113 UMA_HISTOGRAM_BOOLEAN("Accessibility.InvertedColors",
114 gfx::IsInvertedColorScheme());
115 UMA_HISTOGRAM_BOOLEAN("Accessibility.ManuallyEnabled",
116 base::CommandLine::ForCurrentProcess()->HasSwitch(
117 switches::kForceRendererAccessibility));
120 #if !defined(OS_WIN)
121 void BrowserAccessibilityStateImpl::UpdatePlatformSpecificHistograms() {
123 #endif
125 void BrowserAccessibilityStateImpl::AddAccessibilityMode(
126 AccessibilityMode mode) {
127 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
128 switches::kDisableRendererAccessibility)) {
129 return;
132 accessibility_mode_ =
133 content::AddAccessibilityModeTo(accessibility_mode_, mode);
135 AddOrRemoveFromAllWebContents(mode, true);
138 void BrowserAccessibilityStateImpl::RemoveAccessibilityMode(
139 AccessibilityMode mode) {
140 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
141 switches::kForceRendererAccessibility) &&
142 mode == AccessibilityModeComplete) {
143 return;
146 accessibility_mode_ =
147 content::RemoveAccessibilityModeFrom(accessibility_mode_, mode);
149 AddOrRemoveFromAllWebContents(mode, false);
152 void BrowserAccessibilityStateImpl::AddOrRemoveFromAllWebContents(
153 AccessibilityMode mode,
154 bool add) {
155 std::vector<WebContentsImpl*> web_contents_vector =
156 WebContentsImpl::GetAllWebContents();
157 for (size_t i = 0; i < web_contents_vector.size(); ++i) {
158 if (add)
159 web_contents_vector[i]->AddAccessibilityMode(mode);
160 else
161 web_contents_vector[i]->RemoveAccessibilityMode(mode);
165 } // namespace content