Home | History | Annotate | Download | only in login
      1 // Copyright (c) 2011 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.
      4 
      5 #include "chrome/browser/chromeos/login/wizard_accessibility_helper.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/stl_util-inl.h"
      9 #include "chrome/browser/browser_process.h"
     10 #include "chrome/browser/extensions/extension_accessibility_api.h"
     11 #include "chrome/browser/prefs/pref_service.h"
     12 #include "chrome/browser/profiles/profile.h"
     13 #include "chrome/browser/profiles/profile_manager.h"
     14 #include "chrome/common/pref_names.h"
     15 #include "content/common/notification_registrar.h"
     16 #include "content/common/notification_service.h"
     17 #include "grit/generated_resources.h"
     18 #include "ui/base/l10n/l10n_util.h"
     19 #include "views/accelerator.h"
     20 #include "views/view.h"
     21 
     22 namespace chromeos {
     23 
     24 scoped_ptr<views::Accelerator> WizardAccessibilityHelper::accelerator_;
     25 
     26 // static
     27 views::Accelerator WizardAccessibilityHelper::GetAccelerator() {
     28   if (!WizardAccessibilityHelper::accelerator_.get())
     29     WizardAccessibilityHelper::accelerator_.reset(
     30         new views::Accelerator(ui::VKEY_Z, false, true, true));
     31   return *(WizardAccessibilityHelper::accelerator_.get());
     32 }
     33 
     34 // static
     35 WizardAccessibilityHelper* WizardAccessibilityHelper::GetInstance() {
     36   return Singleton<WizardAccessibilityHelper>::get();
     37 }
     38 
     39 WizardAccessibilityHelper::WizardAccessibilityHelper() {
     40   accessibility_handler_.reset(new WizardAccessibilityHandler());
     41   profile_ = ProfileManager::GetDefaultProfile();
     42   registered_notifications_ = false;
     43 }
     44 
     45 void WizardAccessibilityHelper::Init() {
     46   if (IsAccessibilityEnabled()) {
     47     if (!registered_notifications_)
     48       RegisterNotifications();
     49     // SetAccessibilityEnabled(true) fully enables accessibility. Init() is
     50     // necessary during startup when the global accessibility pref is set but
     51     // the notifications are not registered.
     52     SetAccessibilityEnabled(true);
     53   }
     54 }
     55 
     56 void WizardAccessibilityHelper::RegisterNotifications() {
     57   registrar_.Add(accessibility_handler_.get(),
     58                  NotificationType::ACCESSIBILITY_CONTROL_FOCUSED,
     59                  NotificationService::AllSources());
     60   registrar_.Add(accessibility_handler_.get(),
     61                  NotificationType::ACCESSIBILITY_CONTROL_ACTION,
     62                  NotificationService::AllSources());
     63   registrar_.Add(accessibility_handler_.get(),
     64                  NotificationType::ACCESSIBILITY_TEXT_CHANGED,
     65                  NotificationService::AllSources());
     66   registrar_.Add(accessibility_handler_.get(),
     67                  NotificationType::ACCESSIBILITY_MENU_OPENED,
     68                  NotificationService::AllSources());
     69   registrar_.Add(accessibility_handler_.get(),
     70                  NotificationType::ACCESSIBILITY_MENU_CLOSED,
     71                  NotificationService::AllSources());
     72   registered_notifications_ = true;
     73 }
     74 
     75 void WizardAccessibilityHelper::UnregisterNotifications() {
     76   if (!registered_notifications_)
     77     return;
     78   registrar_.RemoveAll();
     79   registered_notifications_ = false;
     80 }
     81 
     82 bool WizardAccessibilityHelper::IsAccessibilityEnabled() {
     83   return g_browser_process &&
     84       g_browser_process->local_state()->GetBoolean(
     85       prefs::kAccessibilityEnabled);
     86 }
     87 
     88 void WizardAccessibilityHelper::MaybeSpeak(const char* str, bool queue,
     89     bool interruptible) {
     90   if (IsAccessibilityEnabled()) {
     91     accessibility_handler_->Speak(str, queue, interruptible);
     92   }
     93 }
     94 
     95 void WizardAccessibilityHelper::ToggleAccessibility() {
     96   if (!IsAccessibilityEnabled()) {
     97     VLOG(1) << "Enabling accessibility.";
     98     if (!registered_notifications_)
     99       RegisterNotifications();
    100     SetAccessibilityEnabled(true);
    101   } else {
    102     SetAccessibilityEnabled(false);
    103     if (registered_notifications_)
    104       UnregisterNotifications();
    105   }
    106 }
    107 
    108 void WizardAccessibilityHelper::SetAccessibilityEnabled(bool enabled) {
    109   bool doSpeak = (IsAccessibilityEnabled() != enabled);
    110   if (g_browser_process) {
    111     PrefService* prefService = g_browser_process->local_state();
    112     prefService->SetBoolean(prefs::kAccessibilityEnabled, enabled);
    113     prefService->ScheduleSavePersistentPrefs();
    114   }
    115   ExtensionAccessibilityEventRouter::GetInstance()->
    116       SetAccessibilityEnabled(enabled);
    117   if (doSpeak) {
    118     accessibility_handler_->Speak(enabled ?
    119         l10n_util::GetStringUTF8(IDS_CHROMEOS_ACC_ACCESS_ENABLED).c_str() :
    120         l10n_util::GetStringUTF8(IDS_CHROMEOS_ACC_ACCESS_DISABLED).c_str(),
    121         false, true);
    122   }
    123 }
    124 
    125 }  // namespace chromeos
    126