Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / accessibility / accessibility_manager.cc
blob338a86e270637bb349e6482f9cca7edc41e05804
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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
7 #include "ash/audio/sounds.h"
8 #include "ash/autoclick/autoclick_controller.h"
9 #include "ash/high_contrast/high_contrast_controller.h"
10 #include "ash/metrics/user_metrics_recorder.h"
11 #include "ash/session/session_state_delegate.h"
12 #include "ash/shell.h"
13 #include "ash/sticky_keys/sticky_keys_controller.h"
14 #include "ash/system/tray/system_tray_notifier.h"
15 #include "base/callback.h"
16 #include "base/callback_helpers.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/singleton.h"
19 #include "base/metrics/histogram.h"
20 #include "base/path_service.h"
21 #include "base/prefs/pref_member.h"
22 #include "base/prefs/pref_service.h"
23 #include "base/strings/string_split.h"
24 #include "base/strings/string_util.h"
25 #include "base/time/time.h"
26 #include "base/values.h"
27 #include "chrome/browser/accessibility/accessibility_extension_api.h"
28 #include "chrome/browser/browser_process.h"
29 #include "chrome/browser/chrome_notification_types.h"
30 #include "chrome/browser/chromeos/accessibility/magnification_manager.h"
31 #include "chrome/browser/chromeos/login/lock/screen_locker.h"
32 #include "chrome/browser/chromeos/login/ui/login_display_host.h"
33 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
34 #include "chrome/browser/chromeos/login/ui/webui_login_view.h"
35 #include "chrome/browser/chromeos/profiles/profile_helper.h"
36 #include "chrome/browser/chromeos/ui/accessibility_focus_ring_controller.h"
37 #include "chrome/browser/extensions/component_loader.h"
38 #include "chrome/browser/extensions/extension_service.h"
39 #include "chrome/browser/profiles/profile.h"
40 #include "chrome/browser/profiles/profile_manager.h"
41 #include "chrome/common/chrome_paths.h"
42 #include "chrome/common/extensions/api/accessibility_private.h"
43 #include "chrome/common/extensions/extension_constants.h"
44 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
45 #include "chrome/common/pref_names.h"
46 #include "chrome/grit/browser_resources.h"
47 #include "chromeos/audio/chromeos_sounds.h"
48 #include "chromeos/login/login_state.h"
49 #include "components/user_manager/user_manager.h"
50 #include "content/public/browser/browser_accessibility_state.h"
51 #include "content/public/browser/browser_thread.h"
52 #include "content/public/browser/notification_details.h"
53 #include "content/public/browser/notification_service.h"
54 #include "content/public/browser/notification_source.h"
55 #include "content/public/browser/render_process_host.h"
56 #include "content/public/browser/render_view_host.h"
57 #include "content/public/browser/web_contents.h"
58 #include "content/public/browser/web_ui.h"
59 #include "extensions/browser/extension_registry.h"
60 #include "extensions/browser/extension_system.h"
61 #include "extensions/browser/file_reader.h"
62 #include "extensions/common/extension.h"
63 #include "extensions/common/extension_messages.h"
64 #include "extensions/common/extension_resource.h"
65 #include "extensions/common/host_id.h"
66 #include "media/audio/sounds/sounds_manager.h"
67 #include "ui/base/ime/chromeos/input_method_manager.h"
68 #include "ui/base/resource/resource_bundle.h"
69 #include "ui/keyboard/keyboard_controller.h"
70 #include "ui/keyboard/keyboard_util.h"
72 using content::BrowserThread;
73 using content::RenderViewHost;
74 using extensions::api::braille_display_private::BrailleController;
75 using extensions::api::braille_display_private::DisplayState;
76 using extensions::api::braille_display_private::KeyEvent;
78 namespace chromeos {
80 namespace {
82 static chromeos::AccessibilityManager* g_accessibility_manager = NULL;
84 static BrailleController* g_braille_controller_for_test = NULL;
86 BrailleController* GetBrailleController() {
87 return g_braille_controller_for_test
88 ? g_braille_controller_for_test
89 : BrailleController::GetInstance();
92 base::FilePath GetChromeVoxPath() {
93 base::FilePath path;
94 if (!PathService::Get(chrome::DIR_RESOURCES, &path))
95 NOTREACHED();
96 path = path.Append(extension_misc::kChromeVoxExtensionPath);
97 return path;
100 // Helper class that directly loads an extension's content scripts into
101 // all of the frames corresponding to a given RenderViewHost.
102 class ContentScriptLoader {
103 public:
104 // Initialize the ContentScriptLoader with the ID of the extension
105 // and the RenderViewHost where the scripts should be loaded.
106 ContentScriptLoader(const std::string& extension_id,
107 int render_process_id,
108 int render_view_id)
109 : extension_id_(extension_id),
110 render_process_id_(render_process_id),
111 render_view_id_(render_view_id) {}
113 // Call this once with the ExtensionResource corresponding to each
114 // content script to be loaded.
115 void AppendScript(extensions::ExtensionResource resource) {
116 resources_.push(resource);
119 // Finally, call this method once to fetch all of the resources and
120 // load them. This method will delete this object when done.
121 void Run() {
122 if (resources_.empty()) {
123 delete this;
124 return;
127 extensions::ExtensionResource resource = resources_.front();
128 resources_.pop();
129 scoped_refptr<FileReader> reader(new FileReader(resource, base::Bind(
130 &ContentScriptLoader::OnFileLoaded, base::Unretained(this))));
131 reader->Start();
134 private:
135 void OnFileLoaded(bool success, const std::string& data) {
136 if (success) {
137 ExtensionMsg_ExecuteCode_Params params;
138 params.request_id = 0;
139 params.host_id = HostID(HostID::EXTENSIONS, extension_id_);
140 params.is_javascript = true;
141 params.code = data;
142 params.run_at = extensions::UserScript::DOCUMENT_IDLE;
143 params.all_frames = true;
144 params.match_about_blank = false;
145 params.in_main_world = false;
147 RenderViewHost* render_view_host =
148 RenderViewHost::FromID(render_process_id_, render_view_id_);
149 if (render_view_host) {
150 render_view_host->Send(new ExtensionMsg_ExecuteCode(
151 render_view_host->GetRoutingID(), params));
154 Run();
157 std::string extension_id_;
158 int render_process_id_;
159 int render_view_id_;
160 std::queue<extensions::ExtensionResource> resources_;
163 void InjectChromeVoxContentScript(
164 ExtensionService* extension_service,
165 int render_process_id,
166 int render_view_id,
167 const base::Closure& done_cb);
169 void LoadChromeVoxExtension(
170 Profile* profile,
171 RenderViewHost* render_view_host,
172 base::Closure done_cb) {
173 ExtensionService* extension_service =
174 extensions::ExtensionSystem::Get(profile)->extension_service();
175 if (render_view_host) {
176 // Wrap the passed in callback to inject the content script.
177 done_cb = base::Bind(
178 &InjectChromeVoxContentScript,
179 extension_service,
180 render_view_host->GetProcess()->GetID(),
181 render_view_host->GetRoutingID(),
182 done_cb);
184 extension_service->component_loader()->AddChromeVoxExtension(done_cb);
187 void InjectChromeVoxContentScript(
188 ExtensionService* extension_service,
189 int render_process_id,
190 int render_view_id,
191 const base::Closure& done_cb) {
192 // Make sure to always run |done_cb|. ChromeVox was loaded even if we end up
193 // not injecting into this particular render view.
194 base::ScopedClosureRunner done_runner(done_cb);
195 RenderViewHost* render_view_host =
196 RenderViewHost::FromID(render_process_id, render_view_id);
197 if (!render_view_host)
198 return;
199 const extensions::Extension* extension =
200 extensions::ExtensionRegistry::Get(extension_service->profile())
201 ->enabled_extensions()
202 .GetByID(extension_misc::kChromeVoxExtensionId);
204 // Set a flag to tell ChromeVox that it's just been enabled,
205 // so that it won't interrupt our speech feedback enabled message.
206 ExtensionMsg_ExecuteCode_Params params;
207 params.request_id = 0;
208 params.host_id = HostID(HostID::EXTENSIONS, extension->id());
209 params.is_javascript = true;
210 params.code = "window.INJECTED_AFTER_LOAD = true;";
211 params.run_at = extensions::UserScript::DOCUMENT_IDLE;
212 params.all_frames = true;
213 params.match_about_blank = false;
214 params.in_main_world = false;
215 render_view_host->Send(new ExtensionMsg_ExecuteCode(
216 render_view_host->GetRoutingID(), params));
218 // Inject ChromeVox' content scripts.
219 ContentScriptLoader* loader = new ContentScriptLoader(
220 extension->id(), render_view_host->GetProcess()->GetID(),
221 render_view_host->GetRoutingID());
223 const extensions::UserScriptList& content_scripts =
224 extensions::ContentScriptsInfo::GetContentScripts(extension);
225 for (size_t i = 0; i < content_scripts.size(); i++) {
226 const extensions::UserScript& script = content_scripts[i];
227 for (size_t j = 0; j < script.js_scripts().size(); ++j) {
228 const extensions::UserScript::File& file = script.js_scripts()[j];
229 extensions::ExtensionResource resource = extension->GetResource(
230 file.relative_path());
231 loader->AppendScript(resource);
234 loader->Run(); // It cleans itself up when done.
237 void UnloadChromeVoxExtension(Profile* profile) {
238 base::FilePath path = GetChromeVoxPath();
239 ExtensionService* extension_service =
240 extensions::ExtensionSystem::Get(profile)->extension_service();
241 extension_service->component_loader()->Remove(path);
244 } // namespace
246 ///////////////////////////////////////////////////////////////////////////////
247 // AccessibilityStatusEventDetails
249 AccessibilityStatusEventDetails::AccessibilityStatusEventDetails(
250 AccessibilityNotificationType notification_type,
251 bool enabled,
252 ui::AccessibilityNotificationVisibility notify)
253 : notification_type(notification_type),
254 enabled(enabled),
255 magnifier_type(ui::kDefaultMagnifierType),
256 notify(notify) {}
258 AccessibilityStatusEventDetails::AccessibilityStatusEventDetails(
259 AccessibilityNotificationType notification_type,
260 bool enabled,
261 ui::MagnifierType magnifier_type,
262 ui::AccessibilityNotificationVisibility notify)
263 : notification_type(notification_type),
264 enabled(enabled),
265 magnifier_type(magnifier_type),
266 notify(notify) {}
268 ///////////////////////////////////////////////////////////////////////////////
270 // AccessibilityManager::PrefHandler
272 AccessibilityManager::PrefHandler::PrefHandler(const char* pref_path)
273 : pref_path_(pref_path) {}
275 AccessibilityManager::PrefHandler::~PrefHandler() {}
277 void AccessibilityManager::PrefHandler::HandleProfileChanged(
278 Profile* previous_profile, Profile* current_profile) {
279 // Returns if the current profile is null.
280 if (!current_profile)
281 return;
283 // If the user set a pref value on the login screen and is now starting a
284 // session with a new profile, copy the pref value to the profile.
285 if ((previous_profile &&
286 ProfileHelper::IsSigninProfile(previous_profile) &&
287 current_profile->IsNewProfile() &&
288 !ProfileHelper::IsSigninProfile(current_profile)) ||
289 // Special case for Guest mode:
290 // Guest mode launches a guest-mode browser process before session starts,
291 // so the previous profile is null.
292 (!previous_profile &&
293 current_profile->IsGuestSession())) {
294 // Returns if the pref has not been set by the user.
295 const PrefService::Preference* pref = ProfileHelper::GetSigninProfile()->
296 GetPrefs()->FindPreference(pref_path_);
297 if (!pref || !pref->IsUserControlled())
298 return;
300 // Copy the pref value from the signin screen.
301 const base::Value* value_on_login = pref->GetValue();
302 PrefService* user_prefs = current_profile->GetPrefs();
303 user_prefs->Set(pref_path_, *value_on_login);
307 ///////////////////////////////////////////////////////////////////////////////
309 // AccessibilityManager
311 // static
312 void AccessibilityManager::Initialize() {
313 CHECK(g_accessibility_manager == NULL);
314 g_accessibility_manager = new AccessibilityManager();
317 // static
318 void AccessibilityManager::Shutdown() {
319 CHECK(g_accessibility_manager);
320 delete g_accessibility_manager;
321 g_accessibility_manager = NULL;
324 // static
325 AccessibilityManager* AccessibilityManager::Get() {
326 return g_accessibility_manager;
329 AccessibilityManager::AccessibilityManager()
330 : profile_(NULL),
331 chrome_vox_loaded_on_lock_screen_(false),
332 chrome_vox_loaded_on_user_screen_(false),
333 large_cursor_pref_handler_(prefs::kAccessibilityLargeCursorEnabled),
334 spoken_feedback_pref_handler_(prefs::kAccessibilitySpokenFeedbackEnabled),
335 high_contrast_pref_handler_(prefs::kAccessibilityHighContrastEnabled),
336 autoclick_pref_handler_(prefs::kAccessibilityAutoclickEnabled),
337 autoclick_delay_pref_handler_(prefs::kAccessibilityAutoclickDelayMs),
338 virtual_keyboard_pref_handler_(
339 prefs::kAccessibilityVirtualKeyboardEnabled),
340 large_cursor_enabled_(false),
341 sticky_keys_enabled_(false),
342 spoken_feedback_enabled_(false),
343 high_contrast_enabled_(false),
344 autoclick_enabled_(false),
345 autoclick_delay_ms_(ash::AutoclickController::kDefaultAutoclickDelayMs),
346 virtual_keyboard_enabled_(false),
347 spoken_feedback_notification_(ui::A11Y_NOTIFICATION_NONE),
348 should_speak_chrome_vox_announcements_on_user_screen_(true),
349 system_sounds_enabled_(false),
350 braille_display_connected_(false),
351 scoped_braille_observer_(this),
352 braille_ime_current_(false),
353 weak_ptr_factory_(this) {
354 notification_registrar_.Add(this,
355 chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
356 content::NotificationService::AllSources());
357 notification_registrar_.Add(this,
358 chrome::NOTIFICATION_SESSION_STARTED,
359 content::NotificationService::AllSources());
360 notification_registrar_.Add(this,
361 chrome::NOTIFICATION_PROFILE_DESTROYED,
362 content::NotificationService::AllSources());
363 notification_registrar_.Add(this,
364 chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
365 content::NotificationService::AllSources());
367 input_method::InputMethodManager::Get()->AddObserver(this);
369 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
370 media::SoundsManager* manager = media::SoundsManager::Get();
371 manager->Initialize(SOUND_SHUTDOWN,
372 bundle.GetRawDataResource(IDR_SOUND_SHUTDOWN_WAV));
373 manager->Initialize(
374 SOUND_SPOKEN_FEEDBACK_ENABLED,
375 bundle.GetRawDataResource(IDR_SOUND_SPOKEN_FEEDBACK_ENABLED_WAV));
376 manager->Initialize(
377 SOUND_SPOKEN_FEEDBACK_DISABLED,
378 bundle.GetRawDataResource(IDR_SOUND_SPOKEN_FEEDBACK_DISABLED_WAV));
379 manager->Initialize(SOUND_PASSTHROUGH,
380 bundle.GetRawDataResource(IDR_SOUND_PASSTHROUGH_WAV));
381 manager->Initialize(SOUND_EXIT_SCREEN,
382 bundle.GetRawDataResource(IDR_SOUND_EXIT_SCREEN_WAV));
383 manager->Initialize(SOUND_ENTER_SCREEN,
384 bundle.GetRawDataResource(IDR_SOUND_ENTER_SCREEN_WAV));
387 AccessibilityManager::~AccessibilityManager() {
388 CHECK(this == g_accessibility_manager);
389 AccessibilityStatusEventDetails details(
390 ACCESSIBILITY_MANAGER_SHUTDOWN,
391 false,
392 ui::A11Y_NOTIFICATION_NONE);
393 NotifyAccessibilityStatusChanged(details);
394 input_method::InputMethodManager::Get()->RemoveObserver(this);
397 bool AccessibilityManager::ShouldShowAccessibilityMenu() {
398 // If any of the loaded profiles has an accessibility feature turned on - or
399 // enforced to always show the menu - we return true to show the menu.
400 std::vector<Profile*> profiles =
401 g_browser_process->profile_manager()->GetLoadedProfiles();
402 for (std::vector<Profile*>::iterator it = profiles.begin();
403 it != profiles.end();
404 ++it) {
405 PrefService* pref_service = (*it)->GetPrefs();
406 if (pref_service->GetBoolean(prefs::kAccessibilityStickyKeysEnabled) ||
407 pref_service->GetBoolean(prefs::kAccessibilityLargeCursorEnabled) ||
408 pref_service->GetBoolean(prefs::kAccessibilitySpokenFeedbackEnabled) ||
409 pref_service->GetBoolean(prefs::kAccessibilityHighContrastEnabled) ||
410 pref_service->GetBoolean(prefs::kAccessibilityAutoclickEnabled) ||
411 pref_service->GetBoolean(prefs::kShouldAlwaysShowAccessibilityMenu) ||
412 pref_service->GetBoolean(prefs::kAccessibilityScreenMagnifierEnabled) ||
413 pref_service->GetBoolean(prefs::kAccessibilityVirtualKeyboardEnabled))
414 return true;
416 return false;
419 bool AccessibilityManager::ShouldEnableCursorCompositing() {
420 if (!profile_)
421 return false;
422 PrefService* pref_service = profile_->GetPrefs();
423 // Enable cursor compositing when one or more of the listed accessibility
424 // features are turned on.
425 if (pref_service->GetBoolean(prefs::kAccessibilityLargeCursorEnabled) ||
426 pref_service->GetBoolean(prefs::kAccessibilityHighContrastEnabled) ||
427 pref_service->GetBoolean(prefs::kAccessibilityScreenMagnifierEnabled))
428 return true;
429 return false;
432 void AccessibilityManager::EnableLargeCursor(bool enabled) {
433 if (!profile_)
434 return;
436 PrefService* pref_service = profile_->GetPrefs();
437 pref_service->SetBoolean(prefs::kAccessibilityLargeCursorEnabled, enabled);
438 pref_service->CommitPendingWrite();
441 void AccessibilityManager::UpdateLargeCursorFromPref() {
442 if (!profile_)
443 return;
445 const bool enabled =
446 profile_->GetPrefs()->GetBoolean(prefs::kAccessibilityLargeCursorEnabled);
448 if (large_cursor_enabled_ == enabled)
449 return;
451 large_cursor_enabled_ = enabled;
453 AccessibilityStatusEventDetails details(
454 ACCESSIBILITY_TOGGLE_LARGE_CURSOR,
455 enabled,
456 ui::A11Y_NOTIFICATION_NONE);
458 NotifyAccessibilityStatusChanged(details);
460 ash::Shell::GetInstance()->cursor_manager()->SetCursorSet(
461 enabled ? ui::CURSOR_SET_LARGE : ui::CURSOR_SET_NORMAL);
462 ash::Shell::GetInstance()->SetCursorCompositingEnabled(
463 ShouldEnableCursorCompositing());
466 bool AccessibilityManager::IsIncognitoAllowed() {
467 // Supervised users can't create incognito-mode windows.
468 return !user_manager::UserManager::Get()->IsLoggedInAsSupervisedUser() &&
469 !user_manager::UserManager::Get()->IsLoggedInAsChildUser();
472 bool AccessibilityManager::IsLargeCursorEnabled() {
473 return large_cursor_enabled_;
476 void AccessibilityManager::EnableStickyKeys(bool enabled) {
477 if (!profile_)
478 return;
479 PrefService* pref_service = profile_->GetPrefs();
480 pref_service->SetBoolean(prefs::kAccessibilityStickyKeysEnabled, enabled);
481 pref_service->CommitPendingWrite();
484 bool AccessibilityManager::IsStickyKeysEnabled() {
485 return sticky_keys_enabled_;
488 void AccessibilityManager::UpdateStickyKeysFromPref() {
489 if (!profile_)
490 return;
492 const bool enabled =
493 profile_->GetPrefs()->GetBoolean(prefs::kAccessibilityStickyKeysEnabled);
495 if (sticky_keys_enabled_ == enabled)
496 return;
498 sticky_keys_enabled_ = enabled;
499 ash::Shell::GetInstance()->sticky_keys_controller()->Enable(enabled);
502 void AccessibilityManager::EnableSpokenFeedback(
503 bool enabled,
504 ui::AccessibilityNotificationVisibility notify) {
505 if (!profile_)
506 return;
507 ash::Shell::GetInstance()->metrics()->RecordUserMetricsAction(
508 enabled ? ash::UMA_STATUS_AREA_ENABLE_SPOKEN_FEEDBACK
509 : ash::UMA_STATUS_AREA_DISABLE_SPOKEN_FEEDBACK);
511 spoken_feedback_notification_ = notify;
513 PrefService* pref_service = profile_->GetPrefs();
514 pref_service->SetBoolean(prefs::kAccessibilitySpokenFeedbackEnabled, enabled);
515 pref_service->CommitPendingWrite();
517 spoken_feedback_notification_ = ui::A11Y_NOTIFICATION_NONE;
520 void AccessibilityManager::UpdateSpokenFeedbackFromPref() {
521 if (!profile_)
522 return;
524 const bool enabled = profile_->GetPrefs()->GetBoolean(
525 prefs::kAccessibilitySpokenFeedbackEnabled);
527 if (spoken_feedback_enabled_ == enabled)
528 return;
530 spoken_feedback_enabled_ = enabled;
532 AccessibilityStatusEventDetails details(
533 ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK,
534 enabled,
535 spoken_feedback_notification_);
537 NotifyAccessibilityStatusChanged(details);
539 if (enabled) {
540 LoadChromeVox();
541 } else {
542 UnloadChromeVox();
544 UpdateBrailleImeState();
547 void AccessibilityManager::LoadChromeVox() {
548 base::Closure done_cb = base::Bind(&AccessibilityManager::PostLoadChromeVox,
549 weak_ptr_factory_.GetWeakPtr(),
550 profile_);
551 ScreenLocker* screen_locker = ScreenLocker::default_screen_locker();
552 if (screen_locker && screen_locker->locked()) {
553 // If on the lock screen, loads ChromeVox only to the lock screen as for
554 // now. On unlock, it will be loaded to the user screen.
555 // (see. AccessibilityManager::Observe())
556 LoadChromeVoxToLockScreen(done_cb);
557 } else {
558 LoadChromeVoxToUserScreen(done_cb);
562 void AccessibilityManager::LoadChromeVoxToUserScreen(
563 const base::Closure& done_cb) {
564 if (chrome_vox_loaded_on_user_screen_)
565 return;
567 // Determine whether an OOBE screen is currently being shown. If so,
568 // ChromeVox will be injected directly into that screen.
569 content::WebUI* login_web_ui = NULL;
571 if (ProfileHelper::IsSigninProfile(profile_)) {
572 LoginDisplayHost* login_display_host = LoginDisplayHostImpl::default_host();
573 if (login_display_host) {
574 WebUILoginView* web_ui_login_view =
575 login_display_host->GetWebUILoginView();
576 if (web_ui_login_view)
577 login_web_ui = web_ui_login_view->GetWebUI();
580 // Lock screen uses the signin progile.
581 chrome_vox_loaded_on_lock_screen_ = true;
584 chrome_vox_loaded_on_user_screen_ = true;
585 LoadChromeVoxExtension(
586 profile_, login_web_ui ?
587 login_web_ui->GetWebContents()->GetRenderViewHost() : NULL,
588 done_cb);
591 void AccessibilityManager::LoadChromeVoxToLockScreen(
592 const base::Closure& done_cb) {
593 if (chrome_vox_loaded_on_lock_screen_)
594 return;
596 ScreenLocker* screen_locker = ScreenLocker::default_screen_locker();
597 if (screen_locker && screen_locker->locked()) {
598 content::WebUI* lock_web_ui = screen_locker->GetAssociatedWebUI();
599 if (lock_web_ui) {
600 Profile* profile = Profile::FromWebUI(lock_web_ui);
601 chrome_vox_loaded_on_lock_screen_ = true;
602 LoadChromeVoxExtension(
603 profile,
604 lock_web_ui->GetWebContents()->GetRenderViewHost(),
605 done_cb);
610 void AccessibilityManager::UnloadChromeVox() {
611 if (chrome_vox_loaded_on_lock_screen_)
612 UnloadChromeVoxFromLockScreen();
614 if (chrome_vox_loaded_on_user_screen_) {
615 UnloadChromeVoxExtension(profile_);
616 chrome_vox_loaded_on_user_screen_ = false;
619 PostUnloadChromeVox(profile_);
622 void AccessibilityManager::UnloadChromeVoxFromLockScreen() {
623 // Lock screen uses the signin progile.
624 Profile* signin_profile = ProfileHelper::GetSigninProfile();
625 UnloadChromeVoxExtension(signin_profile);
626 chrome_vox_loaded_on_lock_screen_ = false;
629 bool AccessibilityManager::IsSpokenFeedbackEnabled() {
630 return spoken_feedback_enabled_;
633 void AccessibilityManager::ToggleSpokenFeedback(
634 ui::AccessibilityNotificationVisibility notify) {
635 EnableSpokenFeedback(!IsSpokenFeedbackEnabled(), notify);
638 void AccessibilityManager::EnableHighContrast(bool enabled) {
639 if (!profile_)
640 return;
642 PrefService* pref_service = profile_->GetPrefs();
643 pref_service->SetBoolean(prefs::kAccessibilityHighContrastEnabled, enabled);
644 pref_service->CommitPendingWrite();
647 void AccessibilityManager::UpdateHighContrastFromPref() {
648 if (!profile_)
649 return;
651 const bool enabled = profile_->GetPrefs()->GetBoolean(
652 prefs::kAccessibilityHighContrastEnabled);
654 if (high_contrast_enabled_ == enabled)
655 return;
657 high_contrast_enabled_ = enabled;
659 AccessibilityStatusEventDetails details(
660 ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE,
661 enabled,
662 ui::A11Y_NOTIFICATION_NONE);
664 NotifyAccessibilityStatusChanged(details);
666 ash::Shell::GetInstance()->high_contrast_controller()->SetEnabled(enabled);
667 ash::Shell::GetInstance()->SetCursorCompositingEnabled(
668 ShouldEnableCursorCompositing());
671 void AccessibilityManager::OnLocaleChanged() {
672 if (!profile_)
673 return;
675 if (!IsSpokenFeedbackEnabled())
676 return;
678 // If the system locale changes and spoken feedback is enabled,
679 // reload ChromeVox so that it switches its internal translations
680 // to the new language.
681 EnableSpokenFeedback(false, ui::A11Y_NOTIFICATION_NONE);
682 EnableSpokenFeedback(true, ui::A11Y_NOTIFICATION_NONE);
685 void AccessibilityManager::PlayEarcon(int sound_key) {
686 DCHECK(sound_key < chromeos::SOUND_COUNT);
687 ash::PlaySystemSoundIfSpokenFeedback(sound_key);
690 bool AccessibilityManager::IsHighContrastEnabled() {
691 return high_contrast_enabled_;
694 void AccessibilityManager::EnableAutoclick(bool enabled) {
695 if (!profile_)
696 return;
698 PrefService* pref_service = profile_->GetPrefs();
699 pref_service->SetBoolean(prefs::kAccessibilityAutoclickEnabled, enabled);
700 pref_service->CommitPendingWrite();
703 bool AccessibilityManager::IsAutoclickEnabled() {
704 return autoclick_enabled_;
707 void AccessibilityManager::UpdateAutoclickFromPref() {
708 if (!profile_)
709 return;
711 bool enabled =
712 profile_->GetPrefs()->GetBoolean(prefs::kAccessibilityAutoclickEnabled);
714 if (autoclick_enabled_ == enabled)
715 return;
716 autoclick_enabled_ = enabled;
718 ash::Shell::GetInstance()->autoclick_controller()->SetEnabled(enabled);
721 void AccessibilityManager::SetAutoclickDelay(int delay_ms) {
722 if (!profile_)
723 return;
725 PrefService* pref_service = profile_->GetPrefs();
726 pref_service->SetInteger(prefs::kAccessibilityAutoclickDelayMs, delay_ms);
727 pref_service->CommitPendingWrite();
730 int AccessibilityManager::GetAutoclickDelay() const {
731 return autoclick_delay_ms_;
734 void AccessibilityManager::UpdateAutoclickDelayFromPref() {
735 if (!profile_)
736 return;
738 int autoclick_delay_ms =
739 profile_->GetPrefs()->GetInteger(prefs::kAccessibilityAutoclickDelayMs);
741 if (autoclick_delay_ms == autoclick_delay_ms_)
742 return;
743 autoclick_delay_ms_ = autoclick_delay_ms;
745 ash::Shell::GetInstance()->autoclick_controller()->SetAutoclickDelay(
746 autoclick_delay_ms_);
749 void AccessibilityManager::EnableVirtualKeyboard(bool enabled) {
750 if (!profile_)
751 return;
753 PrefService* pref_service = profile_->GetPrefs();
754 pref_service->SetBoolean(prefs::kAccessibilityVirtualKeyboardEnabled,
755 enabled);
756 pref_service->CommitPendingWrite();
759 bool AccessibilityManager::IsVirtualKeyboardEnabled() {
760 return virtual_keyboard_enabled_;
763 void AccessibilityManager::UpdateVirtualKeyboardFromPref() {
764 if (!profile_)
765 return;
767 const bool enabled = profile_->GetPrefs()->GetBoolean(
768 prefs::kAccessibilityVirtualKeyboardEnabled);
770 if (virtual_keyboard_enabled_ == enabled)
771 return;
772 virtual_keyboard_enabled_ = enabled;
774 keyboard::SetAccessibilityKeyboardEnabled(enabled);
775 // Note that there are two versions of the on-screen keyboard. A full layout
776 // is provided for accessibility, which includes sticky modifier keys to
777 // enable typing of hotkeys. A compact version is used in touchview mode
778 // to provide a layout with larger keys to facilitate touch typing. In the
779 // event that the a11y keyboard is being disabled, an on-screen keyboard might
780 // still be enabled and a forced reset is required to pick up the layout
781 // change.
782 if (keyboard::IsKeyboardEnabled())
783 ash::Shell::GetInstance()->CreateKeyboard();
784 else
785 ash::Shell::GetInstance()->DeactivateKeyboard();
787 AccessibilityStatusEventDetails details(
788 ACCESSIBILITY_TOGGLE_VIRTUAL_KEYBOARD,
789 enabled,
790 ui::A11Y_NOTIFICATION_NONE);
791 NotifyAccessibilityStatusChanged(details);
794 bool AccessibilityManager::IsBrailleDisplayConnected() const {
795 return braille_display_connected_;
798 void AccessibilityManager::CheckBrailleState() {
799 BrailleController* braille_controller = GetBrailleController();
800 if (!scoped_braille_observer_.IsObserving(braille_controller))
801 scoped_braille_observer_.Add(braille_controller);
802 BrowserThread::PostTaskAndReplyWithResult(
803 BrowserThread::IO,
804 FROM_HERE,
805 base::Bind(&BrailleController::GetDisplayState,
806 base::Unretained(braille_controller)),
807 base::Bind(&AccessibilityManager::ReceiveBrailleDisplayState,
808 weak_ptr_factory_.GetWeakPtr()));
811 void AccessibilityManager::ReceiveBrailleDisplayState(
812 scoped_ptr<extensions::api::braille_display_private::DisplayState> state) {
813 OnBrailleDisplayStateChanged(*state);
816 void AccessibilityManager::UpdateBrailleImeState() {
817 if (!profile_)
818 return;
819 PrefService* pref_service = profile_->GetPrefs();
820 std::vector<std::string> preload_engines;
821 base::SplitString(pref_service->GetString(prefs::kLanguagePreloadEngines),
822 ',',
823 &preload_engines);
824 std::vector<std::string>::iterator it =
825 std::find(preload_engines.begin(),
826 preload_engines.end(),
827 extension_misc::kBrailleImeEngineId);
828 bool is_enabled = (it != preload_engines.end());
829 bool should_be_enabled =
830 (spoken_feedback_enabled_ && braille_display_connected_);
831 if (is_enabled == should_be_enabled)
832 return;
833 if (should_be_enabled)
834 preload_engines.push_back(extension_misc::kBrailleImeEngineId);
835 else
836 preload_engines.erase(it);
837 pref_service->SetString(prefs::kLanguagePreloadEngines,
838 JoinString(preload_engines, ','));
839 braille_ime_current_ = false;
842 // Overridden from InputMethodManager::Observer.
843 void AccessibilityManager::InputMethodChanged(
844 input_method::InputMethodManager* manager,
845 bool show_message) {
846 // Sticky keys is implemented only in ash.
847 // TODO(dpolukhin): support Athena, crbug.com/408733.
848 ash::Shell::GetInstance()->sticky_keys_controller()->SetModifiersEnabled(
849 manager->IsISOLevel5ShiftUsedByCurrentInputMethod(),
850 manager->IsAltGrUsedByCurrentInputMethod());
851 const chromeos::input_method::InputMethodDescriptor descriptor =
852 manager->GetActiveIMEState()->GetCurrentInputMethod();
853 braille_ime_current_ =
854 (descriptor.id() == extension_misc::kBrailleImeEngineId);
857 void AccessibilityManager::SetProfile(Profile* profile) {
858 pref_change_registrar_.reset();
859 local_state_pref_change_registrar_.reset();
861 if (profile) {
862 // TODO(yoshiki): Move following code to PrefHandler.
863 pref_change_registrar_.reset(new PrefChangeRegistrar);
864 pref_change_registrar_->Init(profile->GetPrefs());
865 pref_change_registrar_->Add(
866 prefs::kAccessibilityLargeCursorEnabled,
867 base::Bind(&AccessibilityManager::UpdateLargeCursorFromPref,
868 base::Unretained(this)));
869 pref_change_registrar_->Add(
870 prefs::kAccessibilityStickyKeysEnabled,
871 base::Bind(&AccessibilityManager::UpdateStickyKeysFromPref,
872 base::Unretained(this)));
873 pref_change_registrar_->Add(
874 prefs::kAccessibilitySpokenFeedbackEnabled,
875 base::Bind(&AccessibilityManager::UpdateSpokenFeedbackFromPref,
876 base::Unretained(this)));
877 pref_change_registrar_->Add(
878 prefs::kAccessibilityHighContrastEnabled,
879 base::Bind(&AccessibilityManager::UpdateHighContrastFromPref,
880 base::Unretained(this)));
881 pref_change_registrar_->Add(
882 prefs::kAccessibilityAutoclickEnabled,
883 base::Bind(&AccessibilityManager::UpdateAutoclickFromPref,
884 base::Unretained(this)));
885 pref_change_registrar_->Add(
886 prefs::kAccessibilityAutoclickDelayMs,
887 base::Bind(&AccessibilityManager::UpdateAutoclickDelayFromPref,
888 base::Unretained(this)));
889 pref_change_registrar_->Add(
890 prefs::kAccessibilityVirtualKeyboardEnabled,
891 base::Bind(&AccessibilityManager::UpdateVirtualKeyboardFromPref,
892 base::Unretained(this)));
894 local_state_pref_change_registrar_.reset(new PrefChangeRegistrar);
895 local_state_pref_change_registrar_->Init(g_browser_process->local_state());
896 local_state_pref_change_registrar_->Add(
897 prefs::kApplicationLocale,
898 base::Bind(&AccessibilityManager::OnLocaleChanged,
899 base::Unretained(this)));
901 content::BrowserAccessibilityState::GetInstance()->AddHistogramCallback(
902 base::Bind(
903 &AccessibilityManager::UpdateChromeOSAccessibilityHistograms,
904 base::Unretained(this)));
907 large_cursor_pref_handler_.HandleProfileChanged(profile_, profile);
908 spoken_feedback_pref_handler_.HandleProfileChanged(profile_, profile);
909 high_contrast_pref_handler_.HandleProfileChanged(profile_, profile);
910 autoclick_pref_handler_.HandleProfileChanged(profile_, profile);
911 autoclick_delay_pref_handler_.HandleProfileChanged(profile_, profile);
912 virtual_keyboard_pref_handler_.HandleProfileChanged(profile_, profile);
914 bool had_profile = (profile_ != NULL);
915 profile_ = profile;
917 if (!had_profile && profile)
918 CheckBrailleState();
919 else
920 UpdateBrailleImeState();
921 UpdateLargeCursorFromPref();
922 UpdateStickyKeysFromPref();
923 UpdateSpokenFeedbackFromPref();
924 UpdateHighContrastFromPref();
925 UpdateAutoclickFromPref();
926 UpdateAutoclickDelayFromPref();
927 UpdateVirtualKeyboardFromPref();
930 void AccessibilityManager::ActiveUserChanged(const std::string& user_id) {
931 SetProfile(ProfileManager::GetActiveUserProfile());
934 void AccessibilityManager::SetProfileForTest(Profile* profile) {
935 SetProfile(profile);
938 void AccessibilityManager::SetBrailleControllerForTest(
939 BrailleController* controller) {
940 g_braille_controller_for_test = controller;
943 void AccessibilityManager::EnableSystemSounds(bool system_sounds_enabled) {
944 system_sounds_enabled_ = system_sounds_enabled;
947 base::TimeDelta AccessibilityManager::PlayShutdownSound() {
948 if (!system_sounds_enabled_)
949 return base::TimeDelta();
950 system_sounds_enabled_ = false;
951 if (!ash::PlaySystemSoundIfSpokenFeedback(SOUND_SHUTDOWN))
952 return base::TimeDelta();
953 return media::SoundsManager::Get()->GetDuration(SOUND_SHUTDOWN);
956 void AccessibilityManager::InjectChromeVox(RenderViewHost* render_view_host) {
957 LoadChromeVoxExtension(profile_, render_view_host, base::Closure());
960 scoped_ptr<AccessibilityStatusSubscription>
961 AccessibilityManager::RegisterCallback(
962 const AccessibilityStatusCallback& cb) {
963 return callback_list_.Add(cb);
966 void AccessibilityManager::NotifyAccessibilityStatusChanged(
967 AccessibilityStatusEventDetails& details) {
968 callback_list_.Notify(details);
971 void AccessibilityManager::UpdateChromeOSAccessibilityHistograms() {
972 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosSpokenFeedback",
973 IsSpokenFeedbackEnabled());
974 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosHighContrast",
975 IsHighContrastEnabled());
976 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosVirtualKeyboard",
977 IsVirtualKeyboardEnabled());
978 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosStickyKeys", IsStickyKeysEnabled());
979 if (MagnificationManager::Get()) {
980 uint32 type = MagnificationManager::Get()->IsMagnifierEnabled() ?
981 MagnificationManager::Get()->GetMagnifierType() : 0;
982 // '0' means magnifier is disabled.
983 UMA_HISTOGRAM_ENUMERATION("Accessibility.CrosScreenMagnifier",
984 type,
985 ui::kMaxMagnifierType + 1);
987 if (profile_) {
988 const PrefService* const prefs = profile_->GetPrefs();
989 UMA_HISTOGRAM_BOOLEAN(
990 "Accessibility.CrosLargeCursor",
991 prefs->GetBoolean(prefs::kAccessibilityLargeCursorEnabled));
992 UMA_HISTOGRAM_BOOLEAN(
993 "Accessibility.CrosAlwaysShowA11yMenu",
994 prefs->GetBoolean(prefs::kShouldAlwaysShowAccessibilityMenu));
996 bool autoclick_enabled =
997 prefs->GetBoolean(prefs::kAccessibilityAutoclickEnabled);
998 UMA_HISTOGRAM_BOOLEAN("Accessibility.CrosAutoclick", autoclick_enabled);
999 if (autoclick_enabled) {
1000 // We only want to log the autoclick delay if the user has actually
1001 // enabled autoclick.
1002 UMA_HISTOGRAM_CUSTOM_TIMES(
1003 "Accessibility.CrosAutoclickDelay",
1004 base::TimeDelta::FromMilliseconds(
1005 prefs->GetInteger(prefs::kAccessibilityAutoclickDelayMs)),
1006 base::TimeDelta::FromMilliseconds(1),
1007 base::TimeDelta::FromMilliseconds(3000),
1008 50);
1013 void AccessibilityManager::Observe(
1014 int type,
1015 const content::NotificationSource& source,
1016 const content::NotificationDetails& details) {
1017 switch (type) {
1018 case chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE: {
1019 // Update |profile_| when entering the login screen.
1020 Profile* profile = ProfileManager::GetActiveUserProfile();
1021 if (ProfileHelper::IsSigninProfile(profile))
1022 SetProfile(profile);
1023 break;
1025 case chrome::NOTIFICATION_SESSION_STARTED:
1026 // Update |profile_| when entering a session.
1027 SetProfile(ProfileManager::GetActiveUserProfile());
1029 // Ensure ChromeVox makes announcements at the start of new sessions.
1030 should_speak_chrome_vox_announcements_on_user_screen_ = true;
1032 // Add a session state observer to be able to monitor session changes.
1033 if (!session_state_observer_.get() && ash::Shell::HasInstance())
1034 session_state_observer_.reset(
1035 new ash::ScopedSessionStateObserver(this));
1036 break;
1037 case chrome::NOTIFICATION_PROFILE_DESTROYED: {
1038 // Update |profile_| when exiting a session or shutting down.
1039 Profile* profile = content::Source<Profile>(source).ptr();
1040 if (profile_ == profile)
1041 SetProfile(NULL);
1042 break;
1044 case chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED: {
1045 bool is_screen_locked = *content::Details<bool>(details).ptr();
1046 if (spoken_feedback_enabled_) {
1047 if (is_screen_locked)
1048 LoadChromeVoxToLockScreen(base::Closure());
1049 // If spoken feedback was enabled, make sure it is also enabled on
1050 // the user screen.
1051 // The status tray gets verbalized by user screen ChromeVox, so we need
1052 // to load it on the user screen even if the screen is locked.
1053 LoadChromeVoxToUserScreen(base::Closure());
1055 break;
1060 void AccessibilityManager::OnBrailleDisplayStateChanged(
1061 const DisplayState& display_state) {
1062 braille_display_connected_ = display_state.available;
1063 if (braille_display_connected_) {
1064 EnableSpokenFeedback(true, ui::A11Y_NOTIFICATION_SHOW);
1066 UpdateBrailleImeState();
1068 AccessibilityStatusEventDetails details(
1069 ACCESSIBILITY_BRAILLE_DISPLAY_CONNECTION_STATE_CHANGED,
1070 braille_display_connected_,
1071 ui::A11Y_NOTIFICATION_SHOW);
1072 NotifyAccessibilityStatusChanged(details);
1075 void AccessibilityManager::OnBrailleKeyEvent(const KeyEvent& event) {
1076 // Ensure the braille IME is active on braille keyboard (dots) input.
1077 if ((event.command ==
1078 extensions::api::braille_display_private::KEY_COMMAND_DOTS) &&
1079 !braille_ime_current_) {
1080 input_method::InputMethodManager::Get()
1081 ->GetActiveIMEState()
1082 ->ChangeInputMethod(extension_misc::kBrailleImeEngineId,
1083 false /* show_message */);
1087 void AccessibilityManager::PostLoadChromeVox(Profile* profile) {
1088 // Do any setup work needed immediately after ChromeVox actually loads.
1089 if (system_sounds_enabled_)
1090 ash::PlaySystemSoundAlways(SOUND_SPOKEN_FEEDBACK_ENABLED);
1092 if (chrome_vox_loaded_on_lock_screen_ ||
1093 should_speak_chrome_vox_announcements_on_user_screen_) {
1094 extensions::ExtensionSystem* system =
1095 extensions::ExtensionSystem::Get(profile);
1096 CHECK(system);
1098 scoped_ptr<base::ListValue> event_args(new base::ListValue());
1099 scoped_ptr<extensions::Event> event(
1100 new extensions::Event(extensions::api::accessibility_private::
1101 OnIntroduceChromeVox::kEventName,
1102 event_args.Pass()));
1103 system->event_router()->DispatchEventWithLazyListener(
1104 extension_misc::kChromeVoxExtensionId, event.Pass());
1107 should_speak_chrome_vox_announcements_on_user_screen_ =
1108 chrome_vox_loaded_on_lock_screen_;
1111 void AccessibilityManager::PostUnloadChromeVox(Profile* profile) {
1112 // Do any teardown work needed immediately after ChromeVox actually unloads.
1113 if (system_sounds_enabled_)
1114 ash::PlaySystemSoundAlways(SOUND_SPOKEN_FEEDBACK_DISABLED);
1115 // Clear the accessibility focus ring.
1116 AccessibilityFocusRingController::GetInstance()->SetFocusRing(
1117 std::vector<gfx::Rect>());
1120 } // namespace chromeos