Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / ui / ash / chrome_shell_delegate_chromeos.cc
blob09d11edfd0fd0fafa0cdb9b7174e6475eac38741
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/accessibility/accessibility_events.h"
15 #include "chrome/browser/app_mode/app_mode_utils.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
18 #include "chrome/browser/chromeos/accessibility/magnification_manager.h"
19 #include "chrome/browser/chromeos/background/ash_user_wallpaper_delegate.h"
20 #include "chrome/browser/chromeos/display/display_configuration_observer.h"
21 #include "chrome/browser/chromeos/display/display_preferences.h"
22 #include "chrome/browser/chromeos/profiles/profile_helper.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/profiles/profile_manager.h"
25 #include "chrome/browser/signin/signin_error_notifier_factory_ash.h"
26 #include "chrome/browser/speech/tts_controller.h"
27 #include "chrome/browser/sync/sync_error_notifier_factory_ash.h"
28 #include "chrome/browser/ui/ash/chrome_new_window_delegate_chromeos.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/browser.h"
33 #include "chrome/browser/ui/browser_finder.h"
34 #include "chrome/browser/ui/browser_window.h"
35 #include "chrome/common/pref_names.h"
36 #include "chrome/grit/generated_resources.h"
37 #include "chromeos/chromeos_switches.h"
38 #include "chromeos/ime/input_method_manager.h"
39 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/user_metrics.h"
41 #include "ui/aura/window.h"
42 #include "ui/base/l10n/l10n_util.h"
44 namespace {
46 void InitAfterSessionStart() {
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 #if defined(USE_X11)
57 // Enable magnifier scroll keys as there may be no mouse cursor in kiosk mode.
58 ash::MagnifierKeyScroller::SetEnabled(chrome::IsRunningInForcedAppMode());
60 // Enable long press action to toggle spoken feedback with hotrod
61 // remote which can't handle shortcut.
62 ash::SpokenFeedbackToggler::SetEnabled(chrome::IsRunningInForcedAppMode());
63 #endif
66 class AccessibilityDelegateImpl : public ash::AccessibilityDelegate {
67 public:
68 AccessibilityDelegateImpl() {}
69 virtual ~AccessibilityDelegateImpl() {}
71 virtual void ToggleHighContrast() override {
72 DCHECK(chromeos::AccessibilityManager::Get());
73 chromeos::AccessibilityManager::Get()->EnableHighContrast(
74 !chromeos::AccessibilityManager::Get()->IsHighContrastEnabled());
77 virtual bool IsSpokenFeedbackEnabled() const override {
78 DCHECK(chromeos::AccessibilityManager::Get());
79 return chromeos::AccessibilityManager::Get()->IsSpokenFeedbackEnabled();
82 virtual void ToggleSpokenFeedback(
83 ash::AccessibilityNotificationVisibility notify) override {
84 DCHECK(chromeos::AccessibilityManager::Get());
85 chromeos::AccessibilityManager::Get()->ToggleSpokenFeedback(notify);
88 virtual bool IsHighContrastEnabled() const override {
89 DCHECK(chromeos::AccessibilityManager::Get());
90 return chromeos::AccessibilityManager::Get()->IsHighContrastEnabled();
93 virtual void SetMagnifierEnabled(bool enabled) override {
94 DCHECK(chromeos::MagnificationManager::Get());
95 return chromeos::MagnificationManager::Get()->SetMagnifierEnabled(enabled);
98 virtual void SetMagnifierType(ash::MagnifierType type) override {
99 DCHECK(chromeos::MagnificationManager::Get());
100 return chromeos::MagnificationManager::Get()->SetMagnifierType(type);
103 virtual bool IsMagnifierEnabled() const override {
104 DCHECK(chromeos::MagnificationManager::Get());
105 return chromeos::MagnificationManager::Get()->IsMagnifierEnabled();
108 virtual ash::MagnifierType GetMagnifierType() const override {
109 DCHECK(chromeos::MagnificationManager::Get());
110 return chromeos::MagnificationManager::Get()->GetMagnifierType();
113 virtual void SetLargeCursorEnabled(bool enabled) override {
114 DCHECK(chromeos::AccessibilityManager::Get());
115 return chromeos::AccessibilityManager::Get()->EnableLargeCursor(enabled);
118 virtual bool IsLargeCursorEnabled() const override {
119 DCHECK(chromeos::AccessibilityManager::Get());
120 return chromeos::AccessibilityManager::Get()->IsLargeCursorEnabled();
123 virtual void SetAutoclickEnabled(bool enabled) override {
124 DCHECK(chromeos::AccessibilityManager::Get());
125 return chromeos::AccessibilityManager::Get()->EnableAutoclick(enabled);
128 virtual bool IsAutoclickEnabled() const override {
129 DCHECK(chromeos::AccessibilityManager::Get());
130 return chromeos::AccessibilityManager::Get()->IsAutoclickEnabled();
133 virtual void SetVirtualKeyboardEnabled(bool enabled) override {
134 DCHECK(chromeos::AccessibilityManager::Get());
135 return chromeos::AccessibilityManager::Get()->
136 EnableVirtualKeyboard(enabled);
139 virtual bool IsVirtualKeyboardEnabled() const override {
140 DCHECK(chromeos::AccessibilityManager::Get());
141 return chromeos::AccessibilityManager::Get()->IsVirtualKeyboardEnabled();
144 virtual bool ShouldShowAccessibilityMenu() const override {
145 DCHECK(chromeos::AccessibilityManager::Get());
146 return chromeos::AccessibilityManager::Get()->
147 ShouldShowAccessibilityMenu();
150 virtual bool IsBrailleDisplayConnected() const override {
151 DCHECK(chromeos::AccessibilityManager::Get());
152 return chromeos::AccessibilityManager::Get()->IsBrailleDisplayConnected();
155 virtual void SilenceSpokenFeedback() const override {
156 TtsController::GetInstance()->Stop();
159 virtual void SaveScreenMagnifierScale(double scale) override {
160 if (chromeos::MagnificationManager::Get())
161 chromeos::MagnificationManager::Get()->SaveScreenMagnifierScale(scale);
164 virtual double GetSavedScreenMagnifierScale() override {
165 if (chromeos::MagnificationManager::Get()) {
166 return chromeos::MagnificationManager::Get()->
167 GetSavedScreenMagnifierScale();
169 return std::numeric_limits<double>::min();
172 virtual void TriggerAccessibilityAlert(
173 ash::AccessibilityAlert alert) override {
174 Profile* profile = ProfileManager::GetActiveUserProfile();
175 if (profile) {
176 switch (alert) {
177 case ash::A11Y_ALERT_WINDOW_NEEDED: {
178 AccessibilityAlertInfo event(
179 profile, l10n_util::GetStringUTF8(IDS_A11Y_ALERT_WINDOW_NEEDED));
180 SendControlAccessibilityNotification(
181 ui::AX_EVENT_ALERT, &event);
182 break;
184 case ash::A11Y_ALERT_WINDOW_OVERVIEW_MODE_ENTERED: {
185 AccessibilityAlertInfo event(
186 profile, l10n_util::GetStringUTF8(
187 IDS_A11Y_ALERT_WINDOW_OVERVIEW_MODE_ENTERED));
188 SendControlAccessibilityNotification(
189 ui::AX_EVENT_ALERT, &event);
190 break;
192 case ash::A11Y_ALERT_NONE:
193 break;
198 virtual ash::AccessibilityAlert GetLastAccessibilityAlert() override {
199 return ash::A11Y_ALERT_NONE;
202 virtual void PlayEarcon(int sound_key) override {
203 DCHECK(chromeos::AccessibilityManager::Get());
204 return chromeos::AccessibilityManager::Get()->PlayEarcon(sound_key);
207 virtual base::TimeDelta PlayShutdownSound() const override {
208 return chromeos::AccessibilityManager::Get()->PlayShutdownSound();
211 private:
212 DISALLOW_COPY_AND_ASSIGN(AccessibilityDelegateImpl);
215 } // anonymous namespace
217 bool ChromeShellDelegate::IsFirstRunAfterBoot() const {
218 return CommandLine::ForCurrentProcess()->HasSwitch(
219 chromeos::switches::kFirstExecAfterBoot);
222 void ChromeShellDelegate::PreInit() {
223 chromeos::LoadDisplayPreferences(IsFirstRunAfterBoot());
224 // Set the observer now so that we can save the initial state
225 // in Shell::Init.
226 display_configuration_observer_.reset(
227 new chromeos::DisplayConfigurationObserver());
230 void ChromeShellDelegate::PreShutdown() {
231 display_configuration_observer_.reset();
234 ash::SessionStateDelegate* ChromeShellDelegate::CreateSessionStateDelegate() {
235 return new SessionStateDelegateChromeos;
238 ash::AccessibilityDelegate* ChromeShellDelegate::CreateAccessibilityDelegate() {
239 return new AccessibilityDelegateImpl;
242 ash::NewWindowDelegate* ChromeShellDelegate::CreateNewWindowDelegate() {
243 return new ChromeNewWindowDelegateChromeos;
246 ash::MediaDelegate* ChromeShellDelegate::CreateMediaDelegate() {
247 return new MediaDelegateChromeOS;
250 ash::SystemTrayDelegate* ChromeShellDelegate::CreateSystemTrayDelegate() {
251 return chromeos::CreateSystemTrayDelegate();
254 ash::UserWallpaperDelegate* ChromeShellDelegate::CreateUserWallpaperDelegate() {
255 return chromeos::CreateUserWallpaperDelegate();
258 void ChromeShellDelegate::Observe(int type,
259 const content::NotificationSource& source,
260 const content::NotificationDetails& details) {
261 switch (type) {
262 case chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED: {
263 Profile* profile = content::Details<Profile>(details).ptr();
264 if (!chromeos::ProfileHelper::IsSigninProfile(profile) &&
265 !profile->IsGuestSession() && !profile->IsSupervised()) {
266 // Start the error notifier services to show auth/sync notifications.
267 SigninErrorNotifierFactory::GetForProfile(profile);
268 SyncErrorNotifierFactory::GetForProfile(profile);
270 ash::Shell::GetInstance()->OnLoginUserProfilePrepared();
271 break;
273 case chrome::NOTIFICATION_SESSION_STARTED:
274 InitAfterSessionStart();
275 ash::Shell::GetInstance()->ShowShelf();
276 break;
277 default:
278 NOTREACHED() << "Unexpected notification " << type;
282 void ChromeShellDelegate::PlatformInit() {
283 registrar_.Add(this,
284 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
285 content::NotificationService::AllSources());
286 registrar_.Add(this,
287 chrome::NOTIFICATION_SESSION_STARTED,
288 content::NotificationService::AllSources());