- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / chrome_shell_delegate_chromeos.cc
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.
4
5 #include "chrome/browser/ui/ash/chrome_shell_delegate.h"
6
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"
37
38 namespace {
39
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
43 // is deleted.
44 void RestoreFocus() {
45   ash::MruWindowTracker::WindowList mru_list =
46       ash::Shell::GetInstance()->mru_window_tracker()->BuildMruWindowList();
47   if (!mru_list.empty())
48     mru_list.front()->Focus();
49 }
50
51 class AccessibilityDelegateImpl : public ash::AccessibilityDelegate {
52  public:
53   AccessibilityDelegateImpl() {}
54   virtual ~AccessibilityDelegateImpl() {}
55
56   virtual void ToggleHighContrast() OVERRIDE {
57     DCHECK(chromeos::AccessibilityManager::Get());
58     chromeos::AccessibilityManager::Get()->EnableHighContrast(
59         !chromeos::AccessibilityManager::Get()->IsHighContrastEnabled());
60   }
61
62   virtual bool IsSpokenFeedbackEnabled() const OVERRIDE {
63     DCHECK(chromeos::AccessibilityManager::Get());
64     return chromeos::AccessibilityManager::Get()->IsSpokenFeedbackEnabled();
65   }
66
67   virtual void ToggleSpokenFeedback(
68       ash::AccessibilityNotificationVisibility notify) OVERRIDE {
69     DCHECK(chromeos::AccessibilityManager::Get());
70     chromeos::AccessibilityManager::Get()->ToggleSpokenFeedback(notify);
71   }
72
73   virtual bool IsHighContrastEnabled() const OVERRIDE {
74     DCHECK(chromeos::AccessibilityManager::Get());
75     return chromeos::AccessibilityManager::Get()->IsHighContrastEnabled();
76   }
77
78   virtual void SetMagnifierEnabled(bool enabled) OVERRIDE {
79     DCHECK(chromeos::MagnificationManager::Get());
80     return chromeos::MagnificationManager::Get()->SetMagnifierEnabled(enabled);
81   }
82
83   virtual void SetMagnifierType(ash::MagnifierType type) OVERRIDE {
84     DCHECK(chromeos::MagnificationManager::Get());
85     return chromeos::MagnificationManager::Get()->SetMagnifierType(type);
86   }
87
88   virtual bool IsMagnifierEnabled() const OVERRIDE {
89     DCHECK(chromeos::MagnificationManager::Get());
90     return chromeos::MagnificationManager::Get()->IsMagnifierEnabled();
91   }
92
93   virtual ash::MagnifierType GetMagnifierType() const OVERRIDE {
94     DCHECK(chromeos::MagnificationManager::Get());
95     return chromeos::MagnificationManager::Get()->GetMagnifierType();
96   }
97
98   virtual void SetLargeCursorEnabled(bool enabled) OVERRIDE {
99     DCHECK(chromeos::AccessibilityManager::Get());
100     return chromeos::AccessibilityManager::Get()->EnableLargeCursor(enabled);
101   }
102
103   virtual bool IsLargeCursorEnabled() const OVERRIDE {
104     DCHECK(chromeos::AccessibilityManager::Get());
105     return chromeos::AccessibilityManager::Get()->IsLargeCursorEnabled();
106   }
107
108   virtual void SetAutoclickEnabled(bool enabled) OVERRIDE {
109     DCHECK(chromeos::AccessibilityManager::Get());
110     return chromeos::AccessibilityManager::Get()->EnableAutoclick(enabled);
111   }
112
113   virtual bool IsAutoclickEnabled() const OVERRIDE {
114     DCHECK(chromeos::AccessibilityManager::Get());
115     return chromeos::AccessibilityManager::Get()->IsAutoclickEnabled();
116   }
117
118   virtual bool ShouldAlwaysShowAccessibilityMenu() const OVERRIDE {
119     Profile* profile = ProfileManager::GetDefaultProfile();
120     if (!profile)
121       return false;
122
123     PrefService* user_pref_service = profile->GetPrefs();
124     return user_pref_service && user_pref_service->GetBoolean(
125         prefs::kShouldAlwaysShowAccessibilityMenu);
126   }
127
128   virtual void SilenceSpokenFeedback() const OVERRIDE {
129     TtsController::GetInstance()->Stop();
130   }
131
132   virtual void SaveScreenMagnifierScale(double scale) OVERRIDE {
133     if (chromeos::MagnificationManager::Get())
134       chromeos::MagnificationManager::Get()->SaveScreenMagnifierScale(scale);
135   }
136
137   virtual double GetSavedScreenMagnifierScale() OVERRIDE {
138     if (chromeos::MagnificationManager::Get()) {
139       return chromeos::MagnificationManager::Get()->
140           GetSavedScreenMagnifierScale();
141     }
142     return std::numeric_limits<double>::min();
143   }
144
145  private:
146   DISALLOW_COPY_AND_ASSIGN(AccessibilityDelegateImpl);
147 };
148
149 class MediaDelegateImpl : public ash::MediaDelegate {
150  public:
151   MediaDelegateImpl() {}
152   virtual ~MediaDelegateImpl() {}
153
154   virtual void HandleMediaNextTrack() OVERRIDE {
155     extensions::MediaPlayerAPI::Get(
156         ProfileManager::GetDefaultProfileOrOffTheRecord())->
157             media_player_event_router()->NotifyNextTrack();
158   }
159
160   virtual void HandleMediaPlayPause() OVERRIDE {
161     extensions::MediaPlayerAPI::Get(
162         ProfileManager::GetDefaultProfileOrOffTheRecord())->
163             media_player_event_router()->NotifyTogglePlayState();
164   }
165
166   virtual void HandleMediaPrevTrack() OVERRIDE {
167     extensions::MediaPlayerAPI::Get(
168         ProfileManager::GetDefaultProfileOrOffTheRecord())->
169             media_player_event_router()->NotifyPrevTrack();
170   }
171
172  private:
173   DISALLOW_COPY_AND_ASSIGN(MediaDelegateImpl);
174 };
175
176 }  // anonymous namespace
177
178 bool ChromeShellDelegate::IsFirstRunAfterBoot() const {
179   return CommandLine::ForCurrentProcess()->HasSwitch(
180       chromeos::switches::kFirstExecAfterBoot);
181 }
182
183 void ChromeShellDelegate::PreInit() {
184   chromeos::LoadDisplayPreferences(IsFirstRunAfterBoot());
185 }
186
187 void ChromeShellDelegate::Shutdown() {
188   content::RecordAction(content::UserMetricsAction("Shutdown"));
189   chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->
190       RequestShutdown();
191 }
192
193 ash::CapsLockDelegate* ChromeShellDelegate::CreateCapsLockDelegate() {
194   chromeos::input_method::XKeyboard* xkeyboard =
195       chromeos::input_method::InputMethodManager::Get()->GetXKeyboard();
196   return new CapsLockDelegate(xkeyboard);
197 }
198
199 ash::SessionStateDelegate* ChromeShellDelegate::CreateSessionStateDelegate() {
200   return new SessionStateDelegateChromeos;
201 }
202
203 ash::AccessibilityDelegate* ChromeShellDelegate::CreateAccessibilityDelegate() {
204   return new AccessibilityDelegateImpl;
205 }
206
207 ash::NewWindowDelegate* ChromeShellDelegate::CreateNewWindowDelegate() {
208   return new ChromeNewWindowDelegateChromeos;
209 }
210
211 ash::MediaDelegate* ChromeShellDelegate::CreateMediaDelegate() {
212   return new MediaDelegateImpl;
213 }
214
215 ash::SystemTrayDelegate* ChromeShellDelegate::CreateSystemTrayDelegate() {
216   return chromeos::CreateSystemTrayDelegate();
217 }
218
219 ash::UserWallpaperDelegate* ChromeShellDelegate::CreateUserWallpaperDelegate() {
220   return chromeos::CreateUserWallpaperDelegate();
221 }
222
223 void ChromeShellDelegate::Observe(int type,
224                                   const content::NotificationSource& source,
225                                   const content::NotificationDetails& details) {
226   switch (type) {
227     case chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED:
228       ash::Shell::GetInstance()->CreateLauncher();
229       break;
230     case chrome::NOTIFICATION_SESSION_STARTED:
231       RestoreFocus();
232       ash::Shell::GetInstance()->ShowLauncher();
233       break;
234     default:
235       NOTREACHED() << "Unexpected notification " << type;
236   }
237 }
238
239 void ChromeShellDelegate::PlatformInit() {
240   registrar_.Add(this,
241                  chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
242                  content::NotificationService::AllSources());
243   registrar_.Add(this,
244                  chrome::NOTIFICATION_SESSION_STARTED,
245                  content::NotificationService::AllSources());
246 }