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"
18 // Update the accessibility histogram 45 seconds after initialization.
19 static const int kAccessibilityHistogramDelaySecs
= 45;
22 BrowserAccessibilityState
* BrowserAccessibilityState::GetInstance() {
23 return BrowserAccessibilityStateImpl::GetInstance();
27 BrowserAccessibilityStateImpl
* BrowserAccessibilityStateImpl::GetInstance() {
28 return Singleton
<BrowserAccessibilityStateImpl
,
29 LeakySingletonTraits
<BrowserAccessibilityStateImpl
> >::get();
32 BrowserAccessibilityStateImpl::BrowserAccessibilityStateImpl()
33 : BrowserAccessibilityState(),
34 accessibility_mode_(AccessibilityModeOff
),
35 disable_hot_tracking_(false) {
36 ResetAccessibilityModeValue();
38 // On Windows, UpdateHistograms calls some system functions with unknown
39 // runtime, so call it on the file thread to ensure there's no jank.
40 // Everything in that method must be safe to call on another thread.
41 BrowserThread::ID update_histogram_thread
= BrowserThread::FILE;
43 // On all other platforms, UpdateHistograms should be called on the main
45 BrowserThread::ID update_histogram_thread
= BrowserThread::UI
;
48 // We need to AddRef() the leaky singleton so that Bind doesn't
49 // delete it prematurely.
51 BrowserThread::PostDelayedTask(
52 update_histogram_thread
, FROM_HERE
,
53 base::Bind(&BrowserAccessibilityStateImpl::UpdateHistograms
, this),
54 base::TimeDelta::FromSeconds(kAccessibilityHistogramDelaySecs
));
57 BrowserAccessibilityStateImpl::~BrowserAccessibilityStateImpl() {
60 void BrowserAccessibilityStateImpl::OnScreenReaderDetected() {
61 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
62 switches::kDisableRendererAccessibility
)) {
65 EnableAccessibility();
68 void BrowserAccessibilityStateImpl::EnableAccessibility() {
69 AddAccessibilityMode(AccessibilityModeComplete
);
72 void BrowserAccessibilityStateImpl::DisableAccessibility() {
73 ResetAccessibilityMode();
76 void BrowserAccessibilityStateImpl::ResetAccessibilityModeValue() {
77 accessibility_mode_
= AccessibilityModeOff
;
78 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
79 switches::kForceRendererAccessibility
)) {
80 accessibility_mode_
= AccessibilityModeComplete
;
84 void BrowserAccessibilityStateImpl::ResetAccessibilityMode() {
85 ResetAccessibilityModeValue();
87 std::vector
<WebContentsImpl
*> web_contents_vector
=
88 WebContentsImpl::GetAllWebContents();
89 for (size_t i
= 0; i
< web_contents_vector
.size(); ++i
)
90 web_contents_vector
[i
]->SetAccessibilityMode(accessibility_mode());
93 bool BrowserAccessibilityStateImpl::IsAccessibleBrowser() {
94 return ((accessibility_mode_
& AccessibilityModeComplete
) ==
95 AccessibilityModeComplete
);
98 void BrowserAccessibilityStateImpl::AddHistogramCallback(
99 base::Closure callback
) {
100 histogram_callbacks_
.push_back(callback
);
103 void BrowserAccessibilityStateImpl::UpdateHistogramsForTesting() {
107 void BrowserAccessibilityStateImpl::UpdateHistograms() {
108 UpdatePlatformSpecificHistograms();
110 for (size_t i
= 0; i
< histogram_callbacks_
.size(); ++i
)
111 histogram_callbacks_
[i
].Run();
113 UMA_HISTOGRAM_BOOLEAN("Accessibility.State", IsAccessibleBrowser());
114 UMA_HISTOGRAM_BOOLEAN("Accessibility.InvertedColors",
115 gfx::IsInvertedColorScheme());
116 UMA_HISTOGRAM_BOOLEAN("Accessibility.ManuallyEnabled",
117 base::CommandLine::ForCurrentProcess()->HasSwitch(
118 switches::kForceRendererAccessibility
));
122 void BrowserAccessibilityStateImpl::UpdatePlatformSpecificHistograms() {
126 void BrowserAccessibilityStateImpl::AddAccessibilityMode(
127 AccessibilityMode mode
) {
128 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
129 switches::kDisableRendererAccessibility
)) {
133 accessibility_mode_
=
134 content::AddAccessibilityModeTo(accessibility_mode_
, mode
);
136 AddOrRemoveFromAllWebContents(mode
, true);
139 void BrowserAccessibilityStateImpl::RemoveAccessibilityMode(
140 AccessibilityMode mode
) {
141 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
142 switches::kForceRendererAccessibility
) &&
143 mode
== AccessibilityModeComplete
) {
147 accessibility_mode_
=
148 content::RemoveAccessibilityModeFrom(accessibility_mode_
, mode
);
150 AddOrRemoveFromAllWebContents(mode
, false);
153 void BrowserAccessibilityStateImpl::AddOrRemoveFromAllWebContents(
154 AccessibilityMode mode
,
156 std::vector
<WebContentsImpl
*> web_contents_vector
=
157 WebContentsImpl::GetAllWebContents();
158 for (size_t i
= 0; i
< web_contents_vector
.size(); ++i
) {
160 web_contents_vector
[i
]->AddAccessibilityMode(mode
);
162 web_contents_vector
[i
]->RemoveAccessibilityMode(mode
);
166 } // namespace content