AccessibilityManager must be deleted after ash Shell, but before InputMethodManager.
[chromium-blink-merge.git] / chrome / browser / ui / ash / chrome_shell_delegate_chromeos.cc
blob79e85d1cb49ac7919688c08aa4aea7d698fac820
1 // Copyright 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/ui/ash/chrome_shell_delegate.h"
7 #include "ash/accelerators/magnifier_key_scroller.h"
8 #include "ash/accelerators/spoken_feedback_toggler.h"
9 #include "ash/accessibility_delegate.h"
10 #include "ash/wm/mru_window_tracker.h"
11 #include "ash/wm/window_util.h"
12 #include "base/command_line.h"
13 #include "base/prefs/pref_service.h"
14 #include "chrome/browser/app_mode/app_mode_utils.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
17 #include "chrome/browser/chromeos/accessibility/magnification_manager.h"
18 #include "chrome/browser/chromeos/background/ash_user_wallpaper_delegate.h"
19 #include "chrome/browser/chromeos/display/display_configuration_observer.h"
20 #include "chrome/browser/chromeos/display/display_preferences.h"
21 #include "chrome/browser/chromeos/profiles/profile_helper.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/profiles/profile_manager.h"
24 #include "chrome/browser/signin/signin_error_notifier_factory_ash.h"
25 #include "chrome/browser/speech/tts_controller.h"
26 #include "chrome/browser/sync/sync_error_notifier_factory_ash.h"
27 #include "chrome/browser/ui/ash/chrome_new_window_delegate_chromeos.h"
28 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
29 #include "chrome/browser/ui/ash/media_delegate_chromeos.h"
30 #include "chrome/browser/ui/ash/session_state_delegate_chromeos.h"
31 #include "chrome/browser/ui/ash/system_tray_delegate_chromeos.h"
32 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h"
33 #include "chrome/browser/ui/browser.h"
34 #include "chrome/browser/ui/browser_finder.h"
35 #include "chrome/browser/ui/browser_window.h"
36 #include "chrome/grit/generated_resources.h"
37 #include "chromeos/chromeos_switches.h"
38 #include "content/public/browser/notification_service.h"
39 #include "content/public/browser/user_metrics.h"
40 #include "ui/aura/window.h"
41 #include "ui/base/ime/chromeos/input_method_manager.h"
42 #include "ui/base/l10n/l10n_util.h"
44 namespace {
46 void InitAfterFirstSessionStart() {
47 // Restore focus after the user session is started. It's needed because some
48 // windows can be opened in background while login UI is still active because
49 // we currently restore browser windows before login UI is deleted.
50 ash::Shell* shell = ash::Shell::GetInstance();
51 ash::MruWindowTracker::WindowList mru_list =
52 shell->mru_window_tracker()->BuildMruWindowList();
53 if (!mru_list.empty())
54 mru_list.front()->Focus();
56 // Enable magnifier scroll keys as there may be no mouse cursor in kiosk mode.
57 ash::MagnifierKeyScroller::SetEnabled(chrome::IsRunningInForcedAppMode());
59 // Enable long press action to toggle spoken feedback with hotrod
60 // remote which can't handle shortcut.
61 ash::SpokenFeedbackToggler::SetEnabled(chrome::IsRunningInForcedAppMode());
64 class AccessibilityDelegateImpl : public ash::AccessibilityDelegate {
65 public:
66 AccessibilityDelegateImpl() {
67 ash::Shell::GetInstance()->AddShellObserver(
68 chromeos::AccessibilityManager::Get());
70 ~AccessibilityDelegateImpl() override {
71 ash::Shell::GetInstance()->RemoveShellObserver(
72 chromeos::AccessibilityManager::Get());
75 void ToggleHighContrast() override {
76 DCHECK(chromeos::AccessibilityManager::Get());
77 chromeos::AccessibilityManager::Get()->EnableHighContrast(
78 !chromeos::AccessibilityManager::Get()->IsHighContrastEnabled());
81 bool IsSpokenFeedbackEnabled() const override {
82 DCHECK(chromeos::AccessibilityManager::Get());
83 return chromeos::AccessibilityManager::Get()->IsSpokenFeedbackEnabled();
86 void ToggleSpokenFeedback(
87 ui::AccessibilityNotificationVisibility notify) override {
88 DCHECK(chromeos::AccessibilityManager::Get());
89 chromeos::AccessibilityManager::Get()->ToggleSpokenFeedback(notify);
92 bool IsHighContrastEnabled() const override {
93 DCHECK(chromeos::AccessibilityManager::Get());
94 return chromeos::AccessibilityManager::Get()->IsHighContrastEnabled();
97 void SetMagnifierEnabled(bool enabled) override {
98 DCHECK(chromeos::MagnificationManager::Get());
99 return chromeos::MagnificationManager::Get()->SetMagnifierEnabled(enabled);
102 void SetMagnifierType(ui::MagnifierType type) override {
103 DCHECK(chromeos::MagnificationManager::Get());
104 return chromeos::MagnificationManager::Get()->SetMagnifierType(type);
107 bool IsMagnifierEnabled() const override {
108 DCHECK(chromeos::MagnificationManager::Get());
109 return chromeos::MagnificationManager::Get()->IsMagnifierEnabled();
112 ui::MagnifierType GetMagnifierType() const override {
113 DCHECK(chromeos::MagnificationManager::Get());
114 return chromeos::MagnificationManager::Get()->GetMagnifierType();
117 void SetLargeCursorEnabled(bool enabled) override {
118 DCHECK(chromeos::AccessibilityManager::Get());
119 return chromeos::AccessibilityManager::Get()->EnableLargeCursor(enabled);
122 bool IsLargeCursorEnabled() const override {
123 DCHECK(chromeos::AccessibilityManager::Get());
124 return chromeos::AccessibilityManager::Get()->IsLargeCursorEnabled();
127 void SetAutoclickEnabled(bool enabled) override {
128 DCHECK(chromeos::AccessibilityManager::Get());
129 return chromeos::AccessibilityManager::Get()->EnableAutoclick(enabled);
132 bool IsAutoclickEnabled() const override {
133 DCHECK(chromeos::AccessibilityManager::Get());
134 return chromeos::AccessibilityManager::Get()->IsAutoclickEnabled();
137 void SetVirtualKeyboardEnabled(bool enabled) override {
138 DCHECK(chromeos::AccessibilityManager::Get());
139 return chromeos::AccessibilityManager::Get()->
140 EnableVirtualKeyboard(enabled);
143 bool IsVirtualKeyboardEnabled() const override {
144 DCHECK(chromeos::AccessibilityManager::Get());
145 return chromeos::AccessibilityManager::Get()->IsVirtualKeyboardEnabled();
148 bool ShouldShowAccessibilityMenu() const override {
149 DCHECK(chromeos::AccessibilityManager::Get());
150 return chromeos::AccessibilityManager::Get()->
151 ShouldShowAccessibilityMenu();
154 bool IsBrailleDisplayConnected() const override {
155 DCHECK(chromeos::AccessibilityManager::Get());
156 return chromeos::AccessibilityManager::Get()->IsBrailleDisplayConnected();
159 void SilenceSpokenFeedback() const override {
160 TtsController::GetInstance()->Stop();
163 void SaveScreenMagnifierScale(double scale) override {
164 if (chromeos::MagnificationManager::Get())
165 chromeos::MagnificationManager::Get()->SaveScreenMagnifierScale(scale);
168 double GetSavedScreenMagnifierScale() override {
169 if (chromeos::MagnificationManager::Get()) {
170 return chromeos::MagnificationManager::Get()->
171 GetSavedScreenMagnifierScale();
173 return std::numeric_limits<double>::min();
176 void TriggerAccessibilityAlert(ui::AccessibilityAlert alert) override {
177 Profile* profile = ProfileManager::GetActiveUserProfile();
178 if (profile) {
179 switch (alert) {
180 case ui::A11Y_ALERT_WINDOW_NEEDED: {
181 AutomationManagerAura::GetInstance()->HandleAlert(
182 profile, l10n_util::GetStringUTF8(IDS_A11Y_ALERT_WINDOW_NEEDED));
183 break;
185 case ui::A11Y_ALERT_WINDOW_OVERVIEW_MODE_ENTERED: {
186 AutomationManagerAura::GetInstance()->HandleAlert(
187 profile, l10n_util::GetStringUTF8(
188 IDS_A11Y_ALERT_WINDOW_OVERVIEW_MODE_ENTERED));
189 break;
191 case ui::A11Y_ALERT_NONE:
192 break;
197 ui::AccessibilityAlert GetLastAccessibilityAlert() override {
198 return ui::A11Y_ALERT_NONE;
201 void PlayEarcon(int sound_key) override {
202 DCHECK(chromeos::AccessibilityManager::Get());
203 return chromeos::AccessibilityManager::Get()->PlayEarcon(sound_key);
206 base::TimeDelta PlayShutdownSound() const override {
207 return chromeos::AccessibilityManager::Get()->PlayShutdownSound();
210 private:
211 DISALLOW_COPY_AND_ASSIGN(AccessibilityDelegateImpl);
214 } // anonymous namespace
216 bool ChromeShellDelegate::IsFirstRunAfterBoot() const {
217 return base::CommandLine::ForCurrentProcess()->HasSwitch(
218 chromeos::switches::kFirstExecAfterBoot);
221 void ChromeShellDelegate::PreInit() {
222 chromeos::LoadDisplayPreferences(IsFirstRunAfterBoot());
223 // Set the observer now so that we can save the initial state
224 // in Shell::Init.
225 display_configuration_observer_.reset(
226 new chromeos::DisplayConfigurationObserver());
228 chrome_user_metrics_recorder_.reset(new ChromeUserMetricsRecorder);
231 void ChromeShellDelegate::PreShutdown() {
232 display_configuration_observer_.reset();
233 chrome_user_metrics_recorder_.reset();
236 ash::SessionStateDelegate* ChromeShellDelegate::CreateSessionStateDelegate() {
237 return new SessionStateDelegateChromeos;
240 ash::AccessibilityDelegate* ChromeShellDelegate::CreateAccessibilityDelegate() {
241 return new AccessibilityDelegateImpl;
244 ash::NewWindowDelegate* ChromeShellDelegate::CreateNewWindowDelegate() {
245 return new ChromeNewWindowDelegateChromeos;
248 ash::MediaDelegate* ChromeShellDelegate::CreateMediaDelegate() {
249 return new MediaDelegateChromeOS;
252 ash::SystemTrayDelegate* ChromeShellDelegate::CreateSystemTrayDelegate() {
253 return chromeos::CreateSystemTrayDelegate();
256 ash::UserWallpaperDelegate* ChromeShellDelegate::CreateUserWallpaperDelegate() {
257 return chromeos::CreateUserWallpaperDelegate();
260 void ChromeShellDelegate::Observe(int type,
261 const content::NotificationSource& source,
262 const content::NotificationDetails& details) {
263 switch (type) {
264 case chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED: {
265 Profile* profile = content::Details<Profile>(details).ptr();
266 if (!chromeos::ProfileHelper::IsSigninProfile(profile) &&
267 !profile->IsGuestSession() && !profile->IsSupervised()) {
268 // Start the error notifier services to show auth/sync notifications.
269 SigninErrorNotifierFactory::GetForProfile(profile);
270 SyncErrorNotifierFactory::GetForProfile(profile);
272 // Do not use chrome::NOTIFICATION_PROFILE_ADDED because the
273 // profile is not fully initialized by user_manager. Use
274 // chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED instead.
275 if (shelf_delegate_)
276 shelf_delegate_->OnUserProfileReadyToSwitch(profile);
277 ash::Shell::GetInstance()->OnLoginUserProfilePrepared();
278 break;
280 case chrome::NOTIFICATION_SESSION_STARTED:
281 // InitAfterFirstSessionStart() should only be called once upon system
282 // start.
283 if (user_manager::UserManager::Get()->GetLoggedInUsers().size() < 2)
284 InitAfterFirstSessionStart();
285 ash::Shell::GetInstance()->ShowShelf();
286 break;
287 default:
288 NOTREACHED() << "Unexpected notification " << type;
292 void ChromeShellDelegate::PlatformInit() {
293 registrar_.Add(this,
294 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
295 content::NotificationService::AllSources());
296 registrar_.Add(this,
297 chrome::NOTIFICATION_SESSION_STARTED,
298 content::NotificationService::AllSources());