Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(56)

Side by Side Diff: chrome/browser/chromeos/accessibility/accessibility_manager.cc

Issue 14200048: Introduce AcccessibilityManager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed comments & minor fix Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/accessibility/accessibility_util.h" 5 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
6
7 #include <queue>
8 6
9 #include "ash/high_contrast/high_contrast_controller.h" 7 #include "ash/high_contrast/high_contrast_controller.h"
10 #include "ash/magnifier/magnification_controller.h"
11 #include "ash/magnifier/partial_magnification_controller.h"
12 #include "ash/shell.h" 8 #include "ash/shell.h"
13 #include "ash/shell_delegate.h" 9 #include "ash/system/tray/system_tray_notifier.h"
14 #include "base/bind.h" 10 #include "base/memory/scoped_ptr.h"
15 #include "base/bind_helpers.h" 11 #include "base/memory/singleton.h"
16 #include "base/logging.h"
17 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/prefs/pref_member.h"
18 #include "base/prefs/pref_service.h" 14 #include "base/prefs/pref_service.h"
19 #include "chrome/browser/accessibility/accessibility_extension_api.h" 15 #include "chrome/browser/accessibility/accessibility_extension_api.h"
20 #include "chrome/browser/browser_process.h" 16 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/chromeos/accessibility/magnification_manager.h" 17 #include "chrome/browser/chromeos/accessibility/magnification_manager.h"
18 #include "chrome/browser/chromeos/login/user_manager.h"
22 #include "chrome/browser/extensions/component_loader.h" 19 #include "chrome/browser/extensions/component_loader.h"
23 #include "chrome/browser/extensions/extension_service.h" 20 #include "chrome/browser/extensions/extension_service.h"
24 #include "chrome/browser/extensions/extension_system.h" 21 #include "chrome/browser/extensions/extension_system.h"
25 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/profiles/profile_manager.h" 23 #include "chrome/browser/profiles/profile_manager.h"
27 #include "chrome/browser/speech/tts_controller.h" 24 #include "chrome/browser/speech/tts_controller.h"
28 #include "chrome/browser/ui/singleton_tabs.h" 25 #include "chrome/common/chrome_notification_types.h"
29 #include "chrome/common/extensions/extension.h" 26 #include "chrome/common/extensions/extension.h"
30 #include "chrome/common/extensions/extension_messages.h" 27 #include "chrome/common/extensions/extension_messages.h"
31 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" 28 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
32 #include "chrome/common/extensions/user_script.h"
33 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
34 #include "chrome/common/url_constants.h"
35 #include "content/public/browser/browser_accessibility_state.h" 30 #include "content/public/browser/browser_accessibility_state.h"
31 #include "content/public/browser/notification_observer.h"
32 #include "content/public/browser/notification_registrar.h"
36 #include "content/public/browser/notification_service.h" 33 #include "content/public/browser/notification_service.h"
37 #include "content/public/browser/render_process_host.h" 34 #include "content/public/browser/render_process_host.h"
38 #include "content/public/browser/render_view_host.h" 35 #include "content/public/browser/render_view_host.h"
39 #include "content/public/browser/web_contents.h" 36 #include "content/public/browser/web_contents.h"
40 #include "content/public/browser/web_ui.h" 37 #include "content/public/browser/web_ui.h"
41 #include "extensions/browser/file_reader.h" 38 #include "extensions/browser/file_reader.h"
42 #include "extensions/common/extension_resource.h" 39 #include "extensions/common/extension_resource.h"
43 #include "googleurl/src/gurl.h"
44 #include "grit/browser_resources.h" 40 #include "grit/browser_resources.h"
45 #include "grit/generated_resources.h" 41 #include "grit/generated_resources.h"
46 #include "ui/base/l10n/l10n_util.h" 42 #include "ui/base/l10n/l10n_util.h"
47 #include "ui/base/resource/resource_bundle.h" 43 #include "ui/base/resource/resource_bundle.h"
48 44
49 using content::RenderViewHost; 45 using content::RenderViewHost;
50 46
51 namespace chromeos { 47 namespace chromeos {
52 namespace accessibility { 48
49 namespace {
50
51 static chromeos::AccessibilityManager* g_accessibility_manager = NULL;
53 52
54 // Helper class that directly loads an extension's content scripts into 53 // Helper class that directly loads an extension's content scripts into
55 // all of the frames corresponding to a given RenderViewHost. 54 // all of the frames corresponding to a given RenderViewHost.
56 class ContentScriptLoader { 55 class ContentScriptLoader {
57 public: 56 public:
58 // Initialize the ContentScriptLoader with the ID of the extension 57 // Initialize the ContentScriptLoader with the ID of the extension
59 // and the RenderViewHost where the scripts should be loaded. 58 // and the RenderViewHost where the scripts should be loaded.
60 ContentScriptLoader(const std::string& extension_id, 59 ContentScriptLoader(const std::string& extension_id,
61 int render_process_id, 60 int render_process_id,
62 int render_view_id) 61 int render_view_id)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 } 105 }
107 Run(); 106 Run();
108 } 107 }
109 108
110 std::string extension_id_; 109 std::string extension_id_;
111 int render_process_id_; 110 int render_process_id_;
112 int render_view_id_; 111 int render_view_id_;
113 std::queue<extensions::ExtensionResource> resources_; 112 std::queue<extensions::ExtensionResource> resources_;
114 }; 113 };
115 114
116 void UpdateChromeOSAccessibilityHistograms() { 115 } // namespace
117 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosSpokenFeedback", 116
118 IsSpokenFeedbackEnabled()); 117 ///////////////////////////////////////////////////////////////////////////////
119 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosHighContrast", 118 // AccessibilityStatusEventDetails
120 IsHighContrastEnabled()); 119
121 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosVirtualKeyboard", 120 AccessibilityStatusEventDetails::AccessibilityStatusEventDetails(
122 IsVirtualKeyboardEnabled()); 121 bool enabled,
123 if (MagnificationManager::Get()) { 122 ash::AccessibilityNotificationVisibility notify)
124 uint32 type = MagnificationManager::Get()->IsMagnifierEnabled() ? 123 : enabled(enabled),
125 MagnificationManager::Get()->GetMagnifierType() : 0; 124 magnifier_type(ash::kDefaultMagnifierType),
126 // '0' means magnifier is disabled. 125 notify(notify) {}
127 UMA_HISTOGRAM_ENUMERATION("Accessibility.CrosScreenMagnifier", 126
128 type, 127 AccessibilityStatusEventDetails::AccessibilityStatusEventDetails(
129 ash::kMaxMagnifierType + 1); 128 bool enabled,
130 } 129 ash::MagnifierType magnifier_type,
130 ash::AccessibilityNotificationVisibility notify)
131 : enabled(enabled),
132 magnifier_type(magnifier_type),
133 notify(notify) {}
134
135 ///////////////////////////////////////////////////////////////////////////////
136 //
137 // AccessibilityManager
138
139 // static
140 void AccessibilityManager::Initialize() {
141 CHECK(g_accessibility_manager == NULL);
142 g_accessibility_manager = new AccessibilityManager();
131 } 143 }
132 144
133 void Initialize() { 145 // static
134 content::BrowserAccessibilityState::GetInstance()->AddHistogramCallback( 146 void AccessibilityManager::Shutdown() {
135 base::Bind(&UpdateChromeOSAccessibilityHistograms)); 147 CHECK(g_accessibility_manager);
148 delete g_accessibility_manager;
149 g_accessibility_manager = NULL;
136 } 150 }
137 151
138 void EnableSpokenFeedback(bool enabled, 152 // static
139 content::WebUI* login_web_ui, 153 AccessibilityManager* AccessibilityManager::Get() {
140 ash::AccessibilityNotificationVisibility notify) { 154 CHECK(g_accessibility_manager);
141 bool spoken_feedback_enabled = g_browser_process && 155 return g_accessibility_manager;
142 g_browser_process->local_state()->GetBoolean( 156 }
143 prefs::kSpokenFeedbackEnabled); 157
144 if (spoken_feedback_enabled == enabled) { 158 AccessibilityManager::AccessibilityManager() : profile_(NULL),
159 spoken_feedback_enabled_(false),
160 high_contrast_enabled_(false) {
161 notification_registrar_.Add(this,
162 chrome::NOTIFICATION_SESSION_STARTED,
163 content::NotificationService::AllSources());
164 notification_registrar_.Add(this,
165 chrome::NOTIFICATION_PROFILE_CREATED,
166 content::NotificationService::AllSources());
167 notification_registrar_.Add(this,
168 chrome::NOTIFICATION_PROFILE_DESTROYED,
169 content::NotificationService::AllSources());
170 notification_registrar_.Add(this,
171 chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE,
172 content::NotificationService::AllSources());
173 }
174
175 AccessibilityManager::~AccessibilityManager() {
176 CHECK(this == g_accessibility_manager);
177 }
178
179 void AccessibilityManager::EnableSpokenFeedback(
180 bool enabled,
181 content::WebUI* login_web_ui,
182 ash::AccessibilityNotificationVisibility notify) {
183 if (!profile_)
184 return;
185
186 if (spoken_feedback_enabled_ == enabled) {
145 DLOG(INFO) << "Spoken feedback is already " << 187 DLOG(INFO) << "Spoken feedback is already " <<
146 (enabled ? "enabled" : "disabled") << ". Going to do nothing."; 188 (enabled ? "enabled" : "disabled") << ". Going to do nothing.";
147 return; 189 return;
148 } 190 }
149 191
150 g_browser_process->local_state()->SetBoolean( 192 spoken_feedback_enabled_ = enabled;
193
194 PrefService* pref_service = profile_->GetPrefs();
195 pref_service->SetBoolean(
151 prefs::kSpokenFeedbackEnabled, enabled); 196 prefs::kSpokenFeedbackEnabled, enabled);
152 g_browser_process->local_state()->CommitPendingWrite(); 197 pref_service->CommitPendingWrite();
153 ExtensionAccessibilityEventRouter::GetInstance()-> 198 ExtensionAccessibilityEventRouter::GetInstance()->
154 SetAccessibilityEnabled(enabled); 199 SetAccessibilityEnabled(enabled);
155 200
156 AccessibilityStatusEventDetails details(enabled, notify); 201 AccessibilityStatusEventDetails details(enabled, notify);
157 content::NotificationService::current()->Notify( 202 content::NotificationService::current()->Notify(
158 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK, 203 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK,
159 content::NotificationService::AllSources(), 204 content::NotificationService::AllSources(),
160 content::Details<AccessibilityStatusEventDetails>(&details)); 205 content::Details<AccessibilityStatusEventDetails>(&details));
161 206
162 Speak(l10n_util::GetStringUTF8( 207 Speak(l10n_util::GetStringUTF8(
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 loader->Run(); // It cleans itself up when done. 257 loader->Run(); // It cleans itself up when done.
213 } 258 }
214 259
215 DLOG(INFO) << "ChromeVox was Loaded."; 260 DLOG(INFO) << "ChromeVox was Loaded.";
216 } else { // Unload ChromeVox 261 } else { // Unload ChromeVox
217 extension_service->component_loader()->Remove(path); 262 extension_service->component_loader()->Remove(path);
218 DLOG(INFO) << "ChromeVox was Unloaded."; 263 DLOG(INFO) << "ChromeVox was Unloaded.";
219 } 264 }
220 } 265 }
221 266
222 void EnableHighContrast(bool enabled) { 267 bool AccessibilityManager::IsSpokenFeedbackEnabled() {
223 PrefService* pref_service = g_browser_process->local_state(); 268 if (!profile_)
224 pref_service->SetBoolean(prefs::kHighContrastEnabled, enabled); 269 return false;
225 pref_service->CommitPendingWrite(); 270 return spoken_feedback_enabled_;
226
227 AccessibilityStatusEventDetails detail(enabled, ash::A11Y_NOTIFICATION_NONE);
228 content::NotificationService::current()->Notify(
229 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE,
230 content::NotificationService::AllSources(),
231 content::Details<AccessibilityStatusEventDetails>(&detail));
232
233 #if defined(USE_ASH)
234 ash::Shell::GetInstance()->high_contrast_controller()->SetEnabled(enabled);
235 #endif
236 } 271 }
237 272
238 void EnableVirtualKeyboard(bool enabled) { 273 void AccessibilityManager::ToggleSpokenFeedback(
239 PrefService* pref_service = g_browser_process->local_state(); 274 content::WebUI* login_web_ui,
240 pref_service->SetBoolean(prefs::kVirtualKeyboardEnabled, enabled); 275 ash::AccessibilityNotificationVisibility notify) {
241 pref_service->CommitPendingWrite(); 276 bool spoken_feedback_enabled = IsSpokenFeedbackEnabled();
277 spoken_feedback_enabled = !spoken_feedback_enabled;
278 EnableSpokenFeedback(spoken_feedback_enabled, login_web_ui, notify);
242 } 279 }
243 280
244 void ToggleSpokenFeedback(content::WebUI* login_web_ui, 281 void AccessibilityManager::Speak(const std::string& text) {
245 ash::AccessibilityNotificationVisibility notify) {
246 bool spoken_feedback_enabled = g_browser_process &&
247 g_browser_process->local_state()->GetBoolean(
248 prefs::kSpokenFeedbackEnabled);
249 spoken_feedback_enabled = !spoken_feedback_enabled;
250 EnableSpokenFeedback(spoken_feedback_enabled, login_web_ui, notify);
251 };
252
253 void Speak(const std::string& text) {
254 UtteranceContinuousParameters params; 282 UtteranceContinuousParameters params;
255 283
256 Profile* profile = ProfileManager::GetDefaultProfile(); 284 Utterance* utterance = new Utterance(profile_);
257 Utterance* utterance = new Utterance(profile);
258 utterance->set_text(text); 285 utterance->set_text(text);
259 utterance->set_lang(g_browser_process->GetApplicationLocale()); 286 utterance->set_lang(g_browser_process->GetApplicationLocale());
260 utterance->set_continuous_parameters(params); 287 utterance->set_continuous_parameters(params);
261 utterance->set_can_enqueue(false); 288 utterance->set_can_enqueue(false);
262 utterance->set_options(new DictionaryValue()); 289 utterance->set_options(new DictionaryValue());
263 290
264 TtsController* controller = TtsController::GetInstance(); 291 TtsController* controller = TtsController::GetInstance();
265 controller->SpeakOrEnqueue(utterance); 292 controller->SpeakOrEnqueue(utterance);
266 } 293 }
267 294
268 bool IsSpokenFeedbackEnabled() { 295 void AccessibilityManager::MaybeSpeak(const std::string& utterance) {
269 if (!g_browser_process) {
270 return false;
271 }
272 PrefService* prefs = g_browser_process->local_state();
273 bool spoken_feedback_enabled = prefs &&
274 prefs->GetBoolean(prefs::kSpokenFeedbackEnabled);
275 return spoken_feedback_enabled;
276 }
277
278 bool IsHighContrastEnabled() {
279 if (!g_browser_process) {
280 return false;
281 }
282 PrefService* prefs = g_browser_process->local_state();
283 bool high_contrast_enabled = prefs &&
284 prefs->GetBoolean(prefs::kHighContrastEnabled);
285 return high_contrast_enabled;
286 }
287
288 bool IsVirtualKeyboardEnabled() {
289 if (!g_browser_process) {
290 return false;
291 }
292 PrefService* prefs = g_browser_process->local_state();
293 bool virtual_keyboard_enabled = prefs &&
294 prefs->GetBoolean(prefs::kVirtualKeyboardEnabled);
295 return virtual_keyboard_enabled;
296 }
297
298 void MaybeSpeak(const std::string& utterance) {
299 if (IsSpokenFeedbackEnabled()) 296 if (IsSpokenFeedbackEnabled())
300 Speak(utterance); 297 Speak(utterance);
301 } 298 }
302 299
303 void ShowAccessibilityHelp(Browser* browser) { 300 void AccessibilityManager::EnableHighContrast(bool enabled) {
304 chrome::ShowSingletonTab(browser, GURL(chrome::kChromeAccessibilityHelpURL)); 301 if (!profile_)
302 return;
303
304 if (high_contrast_enabled_ == enabled)
305 return;
306
307 high_contrast_enabled_ = enabled;
308
309 PrefService* pref_service = profile_->GetPrefs();
310 pref_service->SetBoolean(prefs::kHighContrastEnabled, enabled);
311 pref_service->CommitPendingWrite();
312
313 AccessibilityStatusEventDetails detail(enabled, ash::A11Y_NOTIFICATION_NONE);
314 content::NotificationService::current()->Notify(
315 chrome::NOTIFICATION_CROS_ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE,
316 content::NotificationService::AllSources(),
317 content::Details<AccessibilityStatusEventDetails>(&detail));
318
319 #if defined(USE_ASH)
320 ash::Shell::GetInstance()->high_contrast_controller()->SetEnabled(enabled);
321 #endif
305 } 322 }
306 323
307 } // namespace accessibility 324 bool AccessibilityManager::IsHighContrastEnabled() {
325 if (!profile_)
326 return false;
327 return high_contrast_enabled_;
328 }
329
330 void AccessibilityManager::UpdateSpokenFeedbackStatusFromPref() {
331 PrefService* pref_service = profile_->GetPrefs();
332 bool spoken_feedback_enabled =
333 pref_service->GetBoolean(prefs::kSpokenFeedbackEnabled);
334 EnableSpokenFeedback(
335 spoken_feedback_enabled, NULL, ash::A11Y_NOTIFICATION_NONE);
336 }
337
338 void AccessibilityManager::UpdateHighContrastStatusFromPref() {
339 PrefService* pref_service = profile_->GetPrefs();
340 bool high_contrast_enabled =
341 pref_service->GetBoolean(prefs::kHighContrastEnabled);
342 EnableHighContrast(high_contrast_enabled);
343 }
344
345 void AccessibilityManager::SetProfile(Profile* profile) {
346 pref_change_registrar_.reset();
347
348 if (profile) {
349 pref_change_registrar_.reset(new PrefChangeRegistrar);
350 pref_change_registrar_->Init(profile->GetPrefs());
351 pref_change_registrar_->Add(
352 prefs::kSpokenFeedbackEnabled,
353 base::Bind(&AccessibilityManager::UpdateSpokenFeedbackStatusFromPref,
354 base::Unretained(this)));
355 pref_change_registrar_->Add(
356 prefs::kHighContrastEnabled,
357 base::Bind(&AccessibilityManager::UpdateHighContrastStatusFromPref,
358 base::Unretained(this)));
359
360 content::BrowserAccessibilityState::GetInstance()->AddHistogramCallback(
361 base::Bind(
362 &AccessibilityManager::UpdateChromeOSAccessibilityHistograms,
363 base::Unretained(this)));
364 }
365
366 profile_ = profile;
367 UpdateSpokenFeedbackStatusFromPref();
368 UpdateHighContrastStatusFromPref();
369 }
370
371 void AccessibilityManager::SetProfileForTest(Profile* profile) {
372 SetProfile(profile);
373 }
374
375 void AccessibilityManager::UpdateChromeOSAccessibilityHistograms() {
376 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosSpokenFeedback",
377 IsSpokenFeedbackEnabled());
378 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosHighContrast",
379 IsHighContrastEnabled());
380 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosVirtualKeyboard",
381 accessibility::IsVirtualKeyboardEnabled());
382 if (MagnificationManager::Get()) {
383 uint32 type = MagnificationManager::Get()->IsMagnifierEnabled() ?
384 MagnificationManager::Get()->GetMagnifierType() : 0;
385 // '0' means magnifier is disabled.
386 UMA_HISTOGRAM_ENUMERATION("Accessibility.CrosScreenMagnifier",
387 type,
388 ash::kMaxMagnifierType + 1);
389 }
390 }
391
392 void AccessibilityManager::Observe(
393 int type,
394 const content::NotificationSource& source,
395 const content::NotificationDetails& details) {
396 switch (type) {
397 case chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE:
398 case chrome::NOTIFICATION_SESSION_STARTED: {
399 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
400 SetProfile(profile);
401 break;
402 }
403 case chrome::NOTIFICATION_PROFILE_CREATED: {
404 Profile* profile = content::Source<Profile>(source).ptr();
405 SetProfile(profile);
406 break;
407 }
408 case chrome::NOTIFICATION_PROFILE_DESTROYED: {
409 SetProfile(NULL);
410 break;
411 }
412 }
413 }
414
308 } // namespace chromeos 415 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698