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/accessibility_delegate.h"
8 #include "ash/media_delegate.h"
9 #include "ash/wm/mru_window_tracker.h"
10 #include "ash/wm/window_util.h"
11 #include "base/command_line.h"
12 #include "base/prefs/pref_service.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
15 #include "chrome/browser/chromeos/accessibility/magnification_manager.h"
16 #include "chrome/browser/chromeos/background/ash_user_wallpaper_delegate.h"
17 #include "chrome/browser/chromeos/display/display_preferences.h"
18 #include "chrome/browser/chromeos/extensions/media_player_api.h"
19 #include "chrome/browser/chromeos/extensions/media_player_event_router.h"
20 #include "chrome/browser/chromeos/system/ash_system_tray_delegate.h"
21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/browser/speech/tts_controller.h"
23 #include "chrome/browser/ui/ash/caps_lock_delegate_chromeos.h"
24 #include "chrome/browser/ui/ash/chrome_new_window_delegate_chromeos.h"
25 #include "chrome/browser/ui/ash/session_state_delegate_chromeos.h"
26 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_finder.h"
28 #include "chrome/browser/ui/browser_window.h"
29 #include "chrome/common/pref_names.h"
30 #include "chromeos/chromeos_switches.h"
31 #include "chromeos/dbus/dbus_thread_manager.h"
32 #include "chromeos/dbus/power_manager_client.h"
33 #include "chromeos/ime/input_method_manager.h"
34 #include "content/public/browser/notification_service.h"
35 #include "content/public/browser/user_metrics.h"
36 #include "ui/aura/window.h"
40 // This function is used for restoring focus after the user session is started.
41 // It's needed because some windows can be opened in background while login UI
42 // is still active because we currently restore browser windows before login UI
45 ash::MruWindowTracker::WindowList mru_list =
46 ash::Shell::GetInstance()->mru_window_tracker()->BuildMruWindowList();
47 if (!mru_list.empty())
48 mru_list.front()->Focus();
51 class AccessibilityDelegateImpl : public ash::AccessibilityDelegate {
53 AccessibilityDelegateImpl() {}
54 virtual ~AccessibilityDelegateImpl() {}
56 virtual void ToggleHighContrast() OVERRIDE {
57 DCHECK(chromeos::AccessibilityManager::Get());
58 chromeos::AccessibilityManager::Get()->EnableHighContrast(
59 !chromeos::AccessibilityManager::Get()->IsHighContrastEnabled());
62 virtual bool IsSpokenFeedbackEnabled() const OVERRIDE {
63 DCHECK(chromeos::AccessibilityManager::Get());
64 return chromeos::AccessibilityManager::Get()->IsSpokenFeedbackEnabled();
67 virtual void ToggleSpokenFeedback(
68 ash::AccessibilityNotificationVisibility notify) OVERRIDE {
69 DCHECK(chromeos::AccessibilityManager::Get());
70 chromeos::AccessibilityManager::Get()->ToggleSpokenFeedback(notify);
73 virtual bool IsHighContrastEnabled() const OVERRIDE {
74 DCHECK(chromeos::AccessibilityManager::Get());
75 return chromeos::AccessibilityManager::Get()->IsHighContrastEnabled();
78 virtual void SetMagnifierEnabled(bool enabled) OVERRIDE {
79 DCHECK(chromeos::MagnificationManager::Get());
80 return chromeos::MagnificationManager::Get()->SetMagnifierEnabled(enabled);
83 virtual void SetMagnifierType(ash::MagnifierType type) OVERRIDE {
84 DCHECK(chromeos::MagnificationManager::Get());
85 return chromeos::MagnificationManager::Get()->SetMagnifierType(type);
88 virtual bool IsMagnifierEnabled() const OVERRIDE {
89 DCHECK(chromeos::MagnificationManager::Get());
90 return chromeos::MagnificationManager::Get()->IsMagnifierEnabled();
93 virtual ash::MagnifierType GetMagnifierType() const OVERRIDE {
94 DCHECK(chromeos::MagnificationManager::Get());
95 return chromeos::MagnificationManager::Get()->GetMagnifierType();
98 virtual void SetLargeCursorEnabled(bool enabled) OVERRIDE {
99 DCHECK(chromeos::AccessibilityManager::Get());
100 return chromeos::AccessibilityManager::Get()->EnableLargeCursor(enabled);
103 virtual bool IsLargeCursorEnabled() const OVERRIDE {
104 DCHECK(chromeos::AccessibilityManager::Get());
105 return chromeos::AccessibilityManager::Get()->IsLargeCursorEnabled();
108 virtual void SetAutoclickEnabled(bool enabled) OVERRIDE {
109 DCHECK(chromeos::AccessibilityManager::Get());
110 return chromeos::AccessibilityManager::Get()->EnableAutoclick(enabled);
113 virtual bool IsAutoclickEnabled() const OVERRIDE {
114 DCHECK(chromeos::AccessibilityManager::Get());
115 return chromeos::AccessibilityManager::Get()->IsAutoclickEnabled();
118 virtual bool ShouldAlwaysShowAccessibilityMenu() const OVERRIDE {
119 Profile* profile = ProfileManager::GetDefaultProfile();
123 PrefService* user_pref_service = profile->GetPrefs();
124 return user_pref_service && user_pref_service->GetBoolean(
125 prefs::kShouldAlwaysShowAccessibilityMenu);
128 virtual void SilenceSpokenFeedback() const OVERRIDE {
129 TtsController::GetInstance()->Stop();
132 virtual void SaveScreenMagnifierScale(double scale) OVERRIDE {
133 if (chromeos::MagnificationManager::Get())
134 chromeos::MagnificationManager::Get()->SaveScreenMagnifierScale(scale);
137 virtual double GetSavedScreenMagnifierScale() OVERRIDE {
138 if (chromeos::MagnificationManager::Get()) {
139 return chromeos::MagnificationManager::Get()->
140 GetSavedScreenMagnifierScale();
142 return std::numeric_limits<double>::min();
146 DISALLOW_COPY_AND_ASSIGN(AccessibilityDelegateImpl);
149 class MediaDelegateImpl : public ash::MediaDelegate {
151 MediaDelegateImpl() {}
152 virtual ~MediaDelegateImpl() {}
154 virtual void HandleMediaNextTrack() OVERRIDE {
155 extensions::MediaPlayerAPI::Get(
156 ProfileManager::GetDefaultProfileOrOffTheRecord())->
157 media_player_event_router()->NotifyNextTrack();
160 virtual void HandleMediaPlayPause() OVERRIDE {
161 extensions::MediaPlayerAPI::Get(
162 ProfileManager::GetDefaultProfileOrOffTheRecord())->
163 media_player_event_router()->NotifyTogglePlayState();
166 virtual void HandleMediaPrevTrack() OVERRIDE {
167 extensions::MediaPlayerAPI::Get(
168 ProfileManager::GetDefaultProfileOrOffTheRecord())->
169 media_player_event_router()->NotifyPrevTrack();
173 DISALLOW_COPY_AND_ASSIGN(MediaDelegateImpl);
176 } // anonymous namespace
178 bool ChromeShellDelegate::IsFirstRunAfterBoot() const {
179 return CommandLine::ForCurrentProcess()->HasSwitch(
180 chromeos::switches::kFirstExecAfterBoot);
183 void ChromeShellDelegate::PreInit() {
184 chromeos::LoadDisplayPreferences(IsFirstRunAfterBoot());
187 void ChromeShellDelegate::Shutdown() {
188 content::RecordAction(content::UserMetricsAction("Shutdown"));
189 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
193 ash::CapsLockDelegate* ChromeShellDelegate::CreateCapsLockDelegate() {
194 chromeos::input_method::XKeyboard* xkeyboard =
195 chromeos::input_method::InputMethodManager::Get()->GetXKeyboard();
196 return new CapsLockDelegate(xkeyboard);
199 ash::SessionStateDelegate* ChromeShellDelegate::CreateSessionStateDelegate() {
200 return new SessionStateDelegateChromeos;
203 ash::AccessibilityDelegate* ChromeShellDelegate::CreateAccessibilityDelegate() {
204 return new AccessibilityDelegateImpl;
207 ash::NewWindowDelegate* ChromeShellDelegate::CreateNewWindowDelegate() {
208 return new ChromeNewWindowDelegateChromeos;
211 ash::MediaDelegate* ChromeShellDelegate::CreateMediaDelegate() {
212 return new MediaDelegateImpl;
215 ash::SystemTrayDelegate* ChromeShellDelegate::CreateSystemTrayDelegate() {
216 return chromeos::CreateSystemTrayDelegate();
219 ash::UserWallpaperDelegate* ChromeShellDelegate::CreateUserWallpaperDelegate() {
220 return chromeos::CreateUserWallpaperDelegate();
223 void ChromeShellDelegate::Observe(int type,
224 const content::NotificationSource& source,
225 const content::NotificationDetails& details) {
227 case chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED:
228 ash::Shell::GetInstance()->CreateLauncher();
230 case chrome::NOTIFICATION_SESSION_STARTED:
232 ash::Shell::GetInstance()->ShowLauncher();
235 NOTREACHED() << "Unexpected notification " << type;
239 void ChromeShellDelegate::PlatformInit() {
241 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
242 content::NotificationService::AllSources());
244 chrome::NOTIFICATION_SESSION_STARTED,
245 content::NotificationService::AllSources());