accessibility_manager.h revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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.
4
5#ifndef CHROME_BROWSER_CHROMEOS_ACCESSIBILITY_ACCESSIBILITY_MANAGER_H_
6#define CHROME_BROWSER_CHROMEOS_ACCESSIBILITY_ACCESSIBILITY_MANAGER_H_
7
8#include <set>
9
10#include "ash/accessibility_delegate.h"
11#include "ash/session/session_state_observer.h"
12#include "base/callback_list.h"
13#include "base/memory/weak_ptr.h"
14#include "base/prefs/pref_change_registrar.h"
15#include "base/scoped_observer.h"
16#include "base/time/time.h"
17#include "chrome/browser/chromeos/accessibility/accessibility_util.h"
18#include "chrome/browser/extensions/api/braille_display_private/braille_controller.h"
19#include "chromeos/ime/input_method_manager.h"
20#include "content/public/browser/notification_observer.h"
21#include "content/public/browser/notification_registrar.h"
22#include "extensions/browser/event_router.h"
23#include "extensions/browser/extension_system.h"
24
25namespace content {
26class RenderViewHost;
27}
28class Profile;
29
30namespace chromeos {
31
32enum AccessibilityNotificationType {
33  ACCESSIBILITY_MANAGER_SHUTDOWN,
34  ACCESSIBILITY_TOGGLE_HIGH_CONTRAST_MODE,
35  ACCESSIBILITY_TOGGLE_LARGE_CURSOR,
36  ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFIER,
37  ACCESSIBILITY_TOGGLE_SPOKEN_FEEDBACK,
38  ACCESSIBILITY_TOGGLE_VIRTUAL_KEYBOARD,
39  ACCESSIBILITY_BRAILLE_DISPLAY_CONNECTION_STATE_CHANGED
40};
41
42struct AccessibilityStatusEventDetails {
43  AccessibilityStatusEventDetails(
44      AccessibilityNotificationType notification_type,
45      bool enabled,
46      ash::AccessibilityNotificationVisibility notify);
47
48  AccessibilityStatusEventDetails(
49      AccessibilityNotificationType notification_type,
50      bool enabled,
51      ash::MagnifierType magnifier_type,
52      ash::AccessibilityNotificationVisibility notify);
53
54  AccessibilityNotificationType notification_type;
55  bool enabled;
56  ash::MagnifierType magnifier_type;
57  ash::AccessibilityNotificationVisibility notify;
58};
59
60typedef base::Callback<void(const AccessibilityStatusEventDetails&)>
61    AccessibilityStatusCallback;
62
63typedef base::CallbackList<void(const AccessibilityStatusEventDetails&)>
64    AccessibilityStatusCallbackList;
65
66typedef AccessibilityStatusCallbackList::Subscription
67    AccessibilityStatusSubscription;
68
69// AccessibilityManager changes the statuses of accessibility features
70// watching profile notifications and pref-changes.
71// TODO(yoshiki): merge MagnificationManager with AccessibilityManager.
72class AccessibilityManager
73    : public content::NotificationObserver,
74      public extensions::api::braille_display_private::BrailleObserver,
75      public input_method::InputMethodManager::Observer,
76      public ash::SessionStateObserver {
77 public:
78  // Creates an instance of AccessibilityManager, this should be called once,
79  // because only one instance should exist at the same time.
80  static void Initialize();
81  // Deletes the existing instance of AccessibilityManager.
82  static void Shutdown();
83  // Returns the existing instance. If there is no instance, returns NULL.
84  static AccessibilityManager* Get();
85
86  // On a user's first login into a device, any a11y features enabled/disabled
87  // by the user on the login screen are enabled/disabled in the user's profile.
88  // This class watches for profile changes and copies settings into the user's
89  // profile when it detects a login with a newly created profile.
90  class PrefHandler {
91   public:
92    explicit PrefHandler(const char* pref_path);
93    virtual ~PrefHandler();
94
95    // Should be called from AccessibilityManager::SetProfile().
96    void HandleProfileChanged(Profile* previous_profile,
97                              Profile* current_profile);
98
99   private:
100    const char* pref_path_;
101
102    DISALLOW_COPY_AND_ASSIGN(PrefHandler);
103  };
104
105  // Returns true when the accessibility menu should be shown.
106  bool ShouldShowAccessibilityMenu();
107
108  // Returns true when cursor compositing should be enabled.
109  bool ShouldEnableCursorCompositing();
110
111  // Enables or disables the large cursor.
112  void EnableLargeCursor(bool enabled);
113  // Returns true if the large cursor is enabled, or false if not.
114  bool IsLargeCursorEnabled();
115
116  // Enables or disable Sticky Keys.
117  void EnableStickyKeys(bool enabled);
118
119  // Returns true if Incognito mode is allowed, or false if not.
120  bool IsIncognitoAllowed();
121
122  // Returns true if the Sticky Keys is enabled, or false if not.
123  bool IsStickyKeysEnabled();
124
125  // Enables or disables spoken feedback. Enabling spoken feedback installs the
126  // ChromeVox component extension.
127  void EnableSpokenFeedback(bool enabled,
128                            ash::AccessibilityNotificationVisibility notify);
129
130  // Returns true if spoken feedback is enabled, or false if not.
131  bool IsSpokenFeedbackEnabled();
132
133  // Toggles whether Chrome OS spoken feedback is on or off.
134  void ToggleSpokenFeedback(ash::AccessibilityNotificationVisibility notify);
135
136  // Enables or disables the high contrast mode for Chrome.
137  void EnableHighContrast(bool enabled);
138
139  // Returns true if High Contrast is enabled, or false if not.
140  bool IsHighContrastEnabled();
141
142  // Enables or disables autoclick.
143  void EnableAutoclick(bool enabled);
144
145  // Returns true if autoclick is enabled.
146  bool IsAutoclickEnabled();
147
148  // Set the delay for autoclicking after stopping the cursor in milliseconds.
149  void SetAutoclickDelay(int delay_ms);
150
151  // Returns the autoclick delay in milliseconds.
152  int GetAutoclickDelay() const;
153
154  // Enables or disables the virtual keyboard.
155  void EnableVirtualKeyboard(bool enabled);
156  // Returns true if the virtual keyboard is enabled, otherwise false.
157  bool IsVirtualKeyboardEnabled();
158
159  // Returns true if a braille display is connected to the system, otherwise
160  // false.
161  bool IsBrailleDisplayConnected() const;
162
163  // SessionStateObserver overrides:
164  virtual void ActiveUserChanged(const std::string& user_id) OVERRIDE;
165
166  void SetProfileForTest(Profile* profile);
167
168  static void SetBrailleControllerForTest(
169      extensions::api::braille_display_private::BrailleController* controller);
170
171  // Enables/disables system sounds.
172  void EnableSystemSounds(bool system_sounds_enabled);
173
174  // Initiates play of shutdown sound and returns it's duration.
175  base::TimeDelta PlayShutdownSound();
176
177  // Injects ChromeVox scripts into given |render_view_host|.
178  void InjectChromeVox(content::RenderViewHost* render_view_host);
179
180  // Register a callback to be notified when the status of an accessibility
181  // option changes.
182  scoped_ptr<AccessibilityStatusSubscription> RegisterCallback(
183      const AccessibilityStatusCallback& cb);
184
185  // Notify registered callbacks of a status change in an accessibility setting.
186  void NotifyAccessibilityStatusChanged(
187      AccessibilityStatusEventDetails& details);
188
189  // Notify accessibility when locale changes occur.
190  void OnLocaleChanged();
191
192 protected:
193  AccessibilityManager();
194  virtual ~AccessibilityManager();
195
196 private:
197  void LoadChromeVox();
198  void LoadChromeVoxToUserScreen();
199  void LoadChromeVoxToLockScreen();
200  void UnloadChromeVox();
201  void UnloadChromeVoxFromLockScreen();
202  void PostLoadChromeVox(Profile* profile);
203  void PostUnloadChromeVox(Profile* profile);
204
205  void UpdateLargeCursorFromPref();
206  void UpdateStickyKeysFromPref();
207  void UpdateSpokenFeedbackFromPref();
208  void UpdateHighContrastFromPref();
209  void UpdateAutoclickFromPref();
210  void UpdateAutoclickDelayFromPref();
211  void UpdateVirtualKeyboardFromPref();
212
213  void CheckBrailleState();
214  void ReceiveBrailleDisplayState(
215      scoped_ptr<extensions::api::braille_display_private::DisplayState> state);
216
217  void SetProfile(Profile* profile);
218
219  void UpdateChromeOSAccessibilityHistograms();
220
221  // content::NotificationObserver
222  virtual void Observe(int type,
223                       const content::NotificationSource& source,
224                       const content::NotificationDetails& details) OVERRIDE;
225
226  // extensions::api::braille_display_private::BrailleObserver implementation.
227  // Enables spoken feedback if a braille display becomes available.
228  virtual void OnDisplayStateChanged(
229      const extensions::api::braille_display_private::DisplayState&
230          display_state) OVERRIDE;
231
232  // InputMethodManager::Observer
233  virtual void InputMethodChanged(input_method::InputMethodManager* manager,
234                                  bool show_message) OVERRIDE;
235
236
237  // Profile which has the current a11y context.
238  Profile* profile_;
239
240  // Profile which ChromeVox is currently loaded to. If NULL, ChromeVox is not
241  // loaded to any profile.
242  bool chrome_vox_loaded_on_lock_screen_;
243  bool chrome_vox_loaded_on_user_screen_;
244
245  content::NotificationRegistrar notification_registrar_;
246  scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
247  scoped_ptr<PrefChangeRegistrar> local_state_pref_change_registrar_;
248  scoped_ptr<ash::ScopedSessionStateObserver> session_state_observer_;
249
250  PrefHandler large_cursor_pref_handler_;
251  PrefHandler spoken_feedback_pref_handler_;
252  PrefHandler high_contrast_pref_handler_;
253  PrefHandler autoclick_pref_handler_;
254  PrefHandler autoclick_delay_pref_handler_;
255  PrefHandler virtual_keyboard_pref_handler_;
256
257  bool large_cursor_enabled_;
258  bool sticky_keys_enabled_;
259  bool spoken_feedback_enabled_;
260  bool high_contrast_enabled_;
261  bool autoclick_enabled_;
262  int autoclick_delay_ms_;
263  bool virtual_keyboard_enabled_;
264
265  ash::AccessibilityNotificationVisibility spoken_feedback_notification_;
266
267  base::WeakPtrFactory<AccessibilityManager> weak_ptr_factory_;
268
269  bool should_speak_chrome_vox_announcements_on_user_screen_;
270
271  bool system_sounds_enabled_;
272
273  AccessibilityStatusCallbackList callback_list_;
274
275  bool braille_display_connected_;
276  ScopedObserver<extensions::api::braille_display_private::BrailleController,
277                 AccessibilityManager> scoped_braille_observer_;
278
279  DISALLOW_COPY_AND_ASSIGN(AccessibilityManager);
280};
281
282}  // namespace chromeos
283
284#endif  // CHROME_BROWSER_CHROMEOS_ACCESSIBILITY_ACCESSIBILITY_MANAGER_H_
285