Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / ash / chrome_shell_delegate_chromeos.cc
blob9853ac8fda53d94559ef1b398f4b095098acd2de
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/accessibility/accessibility_events.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
16 #include "chrome/browser/chromeos/accessibility/magnification_manager.h"
17 #include "chrome/browser/chromeos/background/ash_user_wallpaper_delegate.h"
18 #include "chrome/browser/chromeos/display/display_configuration_observer.h"
19 #include "chrome/browser/chromeos/display/display_preferences.h"
20 #include "chrome/browser/chromeos/extensions/media_player_api.h"
21 #include "chrome/browser/chromeos/extensions/media_player_event_router.h"
22 #include "chrome/browser/profiles/profile_manager.h"
23 #include "chrome/browser/speech/tts_controller.h"
24 #include "chrome/browser/ui/ash/caps_lock_delegate_chromeos.h"
25 #include "chrome/browser/ui/ash/chrome_new_window_delegate_chromeos.h"
26 #include "chrome/browser/ui/ash/session_state_delegate_chromeos.h"
27 #include "chrome/browser/ui/ash/system_tray_delegate_chromeos.h"
28 #include "chrome/browser/ui/browser.h"
29 #include "chrome/browser/ui/browser_finder.h"
30 #include "chrome/browser/ui/browser_window.h"
31 #include "chrome/common/pref_names.h"
32 #include "chromeos/chromeos_switches.h"
33 #include "chromeos/dbus/dbus_thread_manager.h"
34 #include "chromeos/dbus/power_manager_client.h"
35 #include "chromeos/ime/input_method_manager.h"
36 #include "content/public/browser/notification_service.h"
37 #include "content/public/browser/user_metrics.h"
38 #include "grit/generated_resources.h"
39 #include "ui/aura/window.h"
40 #include "ui/base/l10n/l10n_util.h"
42 namespace {
44 // This function is used for restoring focus after the user session is started.
45 // It's needed because some windows can be opened in background while login UI
46 // is still active because we currently restore browser windows before login UI
47 // is deleted.
48 void RestoreFocus() {
49 ash::MruWindowTracker::WindowList mru_list =
50 ash::Shell::GetInstance()->mru_window_tracker()->BuildMruWindowList();
51 if (!mru_list.empty())
52 mru_list.front()->Focus();
55 class AccessibilityDelegateImpl : public ash::AccessibilityDelegate {
56 public:
57 AccessibilityDelegateImpl() {}
58 virtual ~AccessibilityDelegateImpl() {}
60 virtual void ToggleHighContrast() OVERRIDE {
61 DCHECK(chromeos::AccessibilityManager::Get());
62 chromeos::AccessibilityManager::Get()->EnableHighContrast(
63 !chromeos::AccessibilityManager::Get()->IsHighContrastEnabled());
66 virtual bool IsSpokenFeedbackEnabled() const OVERRIDE {
67 DCHECK(chromeos::AccessibilityManager::Get());
68 return chromeos::AccessibilityManager::Get()->IsSpokenFeedbackEnabled();
71 virtual void ToggleSpokenFeedback(
72 ash::AccessibilityNotificationVisibility notify) OVERRIDE {
73 DCHECK(chromeos::AccessibilityManager::Get());
74 chromeos::AccessibilityManager::Get()->ToggleSpokenFeedback(notify);
77 virtual bool IsHighContrastEnabled() const OVERRIDE {
78 DCHECK(chromeos::AccessibilityManager::Get());
79 return chromeos::AccessibilityManager::Get()->IsHighContrastEnabled();
82 virtual void SetMagnifierEnabled(bool enabled) OVERRIDE {
83 DCHECK(chromeos::MagnificationManager::Get());
84 return chromeos::MagnificationManager::Get()->SetMagnifierEnabled(enabled);
87 virtual void SetMagnifierType(ash::MagnifierType type) OVERRIDE {
88 DCHECK(chromeos::MagnificationManager::Get());
89 return chromeos::MagnificationManager::Get()->SetMagnifierType(type);
92 virtual bool IsMagnifierEnabled() const OVERRIDE {
93 DCHECK(chromeos::MagnificationManager::Get());
94 return chromeos::MagnificationManager::Get()->IsMagnifierEnabled();
97 virtual ash::MagnifierType GetMagnifierType() const OVERRIDE {
98 DCHECK(chromeos::MagnificationManager::Get());
99 return chromeos::MagnificationManager::Get()->GetMagnifierType();
102 virtual void SetLargeCursorEnabled(bool enabled) OVERRIDE {
103 DCHECK(chromeos::AccessibilityManager::Get());
104 return chromeos::AccessibilityManager::Get()->EnableLargeCursor(enabled);
107 virtual bool IsLargeCursorEnabled() const OVERRIDE {
108 DCHECK(chromeos::AccessibilityManager::Get());
109 return chromeos::AccessibilityManager::Get()->IsLargeCursorEnabled();
112 virtual void SetAutoclickEnabled(bool enabled) OVERRIDE {
113 DCHECK(chromeos::AccessibilityManager::Get());
114 return chromeos::AccessibilityManager::Get()->EnableAutoclick(enabled);
117 virtual bool IsAutoclickEnabled() const OVERRIDE {
118 DCHECK(chromeos::AccessibilityManager::Get());
119 return chromeos::AccessibilityManager::Get()->IsAutoclickEnabled();
122 virtual bool ShouldShowAccessibilityMenu() const OVERRIDE {
123 DCHECK(chromeos::AccessibilityManager::Get());
124 return chromeos::AccessibilityManager::Get()->
125 ShouldShowAccessibilityMenu();
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();
145 virtual void TriggerAccessibilityAlert(
146 ash::AccessibilityAlert alert) OVERRIDE {
147 Profile* profile = ProfileManager::GetActiveUserProfile();
148 if (profile) {
149 switch (alert) {
150 case ash::A11Y_ALERT_WINDOW_NEEDED: {
151 AccessibilityAlertInfo event(
152 profile, l10n_util::GetStringUTF8(IDS_A11Y_ALERT_WINDOW_NEEDED));
153 SendControlAccessibilityNotification(
154 ui::AccessibilityTypes::EVENT_ALERT, &event);
155 break;
157 case ash::A11Y_ALERT_NONE:
158 break;
163 virtual ash::AccessibilityAlert GetLastAccessibilityAlert() OVERRIDE {
164 return ash::A11Y_ALERT_NONE;
167 virtual base::TimeDelta PlayShutdownSound() const OVERRIDE {
168 return chromeos::AccessibilityManager::Get()->PlayShutdownSound();
171 private:
172 DISALLOW_COPY_AND_ASSIGN(AccessibilityDelegateImpl);
175 class MediaDelegateImpl : public ash::MediaDelegate {
176 public:
177 MediaDelegateImpl() {}
178 virtual ~MediaDelegateImpl() {}
180 virtual void HandleMediaNextTrack() OVERRIDE {
181 extensions::MediaPlayerAPI::Get(
182 ProfileManager::GetActiveUserProfile())->
183 media_player_event_router()->NotifyNextTrack();
186 virtual void HandleMediaPlayPause() OVERRIDE {
187 extensions::MediaPlayerAPI::Get(
188 ProfileManager::GetActiveUserProfile())->
189 media_player_event_router()->NotifyTogglePlayState();
192 virtual void HandleMediaPrevTrack() OVERRIDE {
193 extensions::MediaPlayerAPI::Get(
194 ProfileManager::GetActiveUserProfile())->
195 media_player_event_router()->NotifyPrevTrack();
198 private:
199 DISALLOW_COPY_AND_ASSIGN(MediaDelegateImpl);
202 } // anonymous namespace
204 bool ChromeShellDelegate::IsFirstRunAfterBoot() const {
205 return CommandLine::ForCurrentProcess()->HasSwitch(
206 chromeos::switches::kFirstExecAfterBoot);
209 void ChromeShellDelegate::PreInit() {
210 chromeos::LoadDisplayPreferences(IsFirstRunAfterBoot());
211 // Set the observer now so that we can save the initial state
212 // in Shell::Init.
213 display_configuration_observer_.reset(
214 new chromeos::DisplayConfigurationObserver());
217 void ChromeShellDelegate::Shutdown() {
218 content::RecordAction(base::UserMetricsAction("Shutdown"));
219 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
220 RequestShutdown();
223 ash::CapsLockDelegate* ChromeShellDelegate::CreateCapsLockDelegate() {
224 chromeos::input_method::XKeyboard* xkeyboard =
225 chromeos::input_method::InputMethodManager::Get()->GetXKeyboard();
226 return new CapsLockDelegate(xkeyboard);
229 ash::SessionStateDelegate* ChromeShellDelegate::CreateSessionStateDelegate() {
230 return new SessionStateDelegateChromeos;
233 ash::AccessibilityDelegate* ChromeShellDelegate::CreateAccessibilityDelegate() {
234 return new AccessibilityDelegateImpl;
237 ash::NewWindowDelegate* ChromeShellDelegate::CreateNewWindowDelegate() {
238 return new ChromeNewWindowDelegateChromeos;
241 ash::MediaDelegate* ChromeShellDelegate::CreateMediaDelegate() {
242 return new MediaDelegateImpl;
245 ash::SystemTrayDelegate* ChromeShellDelegate::CreateSystemTrayDelegate() {
246 return chromeos::CreateSystemTrayDelegate();
249 ash::UserWallpaperDelegate* ChromeShellDelegate::CreateUserWallpaperDelegate() {
250 return chromeos::CreateUserWallpaperDelegate();
253 void ChromeShellDelegate::Observe(int type,
254 const content::NotificationSource& source,
255 const content::NotificationDetails& details) {
256 switch (type) {
257 case chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED:
258 ash::Shell::GetInstance()->OnLoginUserProfilePrepared();
259 break;
260 case chrome::NOTIFICATION_SESSION_STARTED:
261 RestoreFocus();
262 ash::Shell::GetInstance()->ShowShelf();
263 break;
264 case chrome::NOTIFICATION_APP_TERMINATING:
265 // Let classes unregister themselves as observers of the
266 // ash::Shell singleton before the shell is destroyed.
267 display_configuration_observer_.reset();
268 break;
269 default:
270 NOTREACHED() << "Unexpected notification " << type;
274 void ChromeShellDelegate::PlatformInit() {
275 registrar_.Add(this,
276 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
277 content::NotificationService::AllSources());
278 registrar_.Add(this,
279 chrome::NOTIFICATION_SESSION_STARTED,
280 content::NotificationService::AllSources());
281 registrar_.Add(this,
282 chrome::NOTIFICATION_APP_TERMINATING,
283 content::NotificationService::AllSources());