Home | History | Annotate | Download | only in ash
      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 <vector>
      8 
      9 #include "ash/accessibility_delegate.h"
     10 #include "ash/magnifier/magnifier_constants.h"
     11 #include "ash/media_delegate.h"
     12 #include "ash/system/tray/default_system_tray_delegate.h"
     13 #include "ash/wm/window_util.h"
     14 #include "base/command_line.h"
     15 #include "chrome/browser/accessibility/accessibility_events.h"
     16 #include "chrome/browser/browser_process.h"
     17 #include "chrome/browser/chrome_notification_types.h"
     18 #include "chrome/browser/prefs/session_startup_pref.h"
     19 #include "chrome/browser/profiles/profile.h"
     20 #include "chrome/browser/profiles/profile_manager.h"
     21 #include "chrome/browser/signin/signin_error_notifier_factory_ash.h"
     22 #include "chrome/browser/sync/sync_error_notifier_factory_ash.h"
     23 #include "chrome/browser/ui/ash/chrome_new_window_delegate.h"
     24 #include "chrome/browser/ui/ash/session_state_delegate_views.h"
     25 #include "chrome/browser/ui/ash/solid_color_user_wallpaper_delegate.h"
     26 #include "chrome/browser/ui/browser.h"
     27 #include "chrome/browser/ui/browser_finder.h"
     28 #include "chrome/browser/ui/browser_list.h"
     29 #include "chrome/browser/ui/browser_tabstrip.h"
     30 #include "chrome/browser/ui/browser_window.h"
     31 #include "chrome/browser/ui/host_desktop.h"
     32 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
     33 #include "chrome/browser/ui/startup/startup_browser_creator_impl.h"
     34 #include "chrome/common/chrome_switches.h"
     35 #include "content/public/browser/notification_service.h"
     36 
     37 #if defined(OS_WIN)
     38 #include "chrome/browser/ui/ash/system_tray_delegate_win.h"
     39 #endif
     40 
     41 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
     42 #include "chrome/browser/ui/ash/system_tray_delegate_linux.h"
     43 #endif
     44 
     45 namespace {
     46 
     47 class NewWindowDelegateImpl : public ChromeNewWindowDelegate {
     48  public:
     49   NewWindowDelegateImpl() {}
     50   virtual ~NewWindowDelegateImpl() {}
     51 
     52   // Overridden from ash::NewWindowDelegate:
     53   virtual void OpenFileManager() OVERRIDE {}
     54   virtual void OpenCrosh() OVERRIDE {}
     55   virtual void ShowKeyboardOverlay() OVERRIDE {}
     56 
     57  private:
     58   DISALLOW_COPY_AND_ASSIGN(NewWindowDelegateImpl);
     59 };
     60 
     61 class MediaDelegateImpl : public ash::MediaDelegate {
     62  public:
     63   MediaDelegateImpl() {}
     64   virtual ~MediaDelegateImpl() {}
     65   virtual void HandleMediaNextTrack() OVERRIDE {}
     66   virtual void HandleMediaPlayPause() OVERRIDE {}
     67   virtual void HandleMediaPrevTrack() OVERRIDE {}
     68   virtual ash::MediaCaptureState GetMediaCaptureState(
     69       content::BrowserContext* context) OVERRIDE {
     70     return ash::MEDIA_CAPTURE_NONE;
     71   }
     72 
     73  private:
     74   DISALLOW_COPY_AND_ASSIGN(MediaDelegateImpl);
     75 };
     76 
     77 class EmptyAccessibilityDelegate : public ash::AccessibilityDelegate {
     78  public:
     79   EmptyAccessibilityDelegate() {}
     80   virtual ~EmptyAccessibilityDelegate() {}
     81 
     82   virtual void ToggleHighContrast() OVERRIDE {
     83   }
     84 
     85   virtual bool IsHighContrastEnabled() const OVERRIDE {
     86     return false;
     87   }
     88 
     89   virtual bool IsSpokenFeedbackEnabled() const OVERRIDE {
     90     return false;
     91   }
     92 
     93   virtual void ToggleSpokenFeedback(
     94       ash::AccessibilityNotificationVisibility notify) OVERRIDE {
     95   }
     96 
     97   virtual void SetLargeCursorEnabled(bool enalbed) OVERRIDE {
     98   }
     99 
    100   virtual bool IsLargeCursorEnabled() const OVERRIDE {
    101     return false;
    102   }
    103 
    104   virtual void SetMagnifierEnabled(bool enabled) OVERRIDE {
    105   }
    106 
    107   virtual void SetMagnifierType(ash::MagnifierType type) OVERRIDE {
    108   }
    109 
    110   virtual bool IsMagnifierEnabled() const OVERRIDE {
    111     return false;
    112   }
    113 
    114   virtual void SetAutoclickEnabled(bool enabled) OVERRIDE {
    115   }
    116 
    117   virtual bool IsAutoclickEnabled() const OVERRIDE {
    118     return false;
    119   }
    120 
    121   virtual ash::MagnifierType GetMagnifierType() const OVERRIDE {
    122     return ash::kDefaultMagnifierType;
    123   }
    124 
    125   virtual void SaveScreenMagnifierScale(double scale) OVERRIDE {
    126   }
    127 
    128   virtual double GetSavedScreenMagnifierScale() OVERRIDE {
    129     return std::numeric_limits<double>::min();
    130   }
    131 
    132   virtual bool ShouldShowAccessibilityMenu() const OVERRIDE {
    133     return false;
    134   }
    135 
    136   virtual bool IsBrailleDisplayConnected() const OVERRIDE { return false; }
    137 
    138   virtual void SilenceSpokenFeedback() const OVERRIDE {
    139   }
    140 
    141   virtual void SetVirtualKeyboardEnabled(bool enabled) OVERRIDE {
    142   }
    143 
    144   virtual bool IsVirtualKeyboardEnabled() const OVERRIDE {
    145     return false;
    146   }
    147 
    148   virtual void TriggerAccessibilityAlert(
    149       ash::AccessibilityAlert alert) OVERRIDE {
    150   }
    151 
    152   virtual ash::AccessibilityAlert GetLastAccessibilityAlert() OVERRIDE {
    153     return ash::A11Y_ALERT_NONE;
    154   }
    155 
    156   virtual base::TimeDelta PlayShutdownSound() const OVERRIDE {
    157     return base::TimeDelta();
    158   }
    159 
    160  private:
    161   DISALLOW_COPY_AND_ASSIGN(EmptyAccessibilityDelegate);
    162 };
    163 
    164 }  // namespace
    165 
    166 bool ChromeShellDelegate::IsFirstRunAfterBoot() const {
    167   return false;
    168 }
    169 
    170 void ChromeShellDelegate::PreInit() {
    171 }
    172 
    173 void ChromeShellDelegate::PreShutdown() {
    174 }
    175 
    176 ash::NewWindowDelegate* ChromeShellDelegate::CreateNewWindowDelegate() {
    177   return new NewWindowDelegateImpl;
    178 }
    179 
    180 ash::MediaDelegate* ChromeShellDelegate::CreateMediaDelegate() {
    181   return new MediaDelegateImpl;
    182 }
    183 
    184 ash::SessionStateDelegate* ChromeShellDelegate::CreateSessionStateDelegate() {
    185   return new SessionStateDelegate;
    186 }
    187 
    188 ash::SystemTrayDelegate* ChromeShellDelegate::CreateSystemTrayDelegate() {
    189 #if defined(OS_WIN)
    190   return CreateWindowsSystemTrayDelegate();
    191 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
    192   return CreateLinuxSystemTrayDelegate();
    193 #else
    194   return new ash::DefaultSystemTrayDelegate;
    195 #endif
    196 }
    197 
    198 ash::AccessibilityDelegate* ChromeShellDelegate::CreateAccessibilityDelegate() {
    199   return new EmptyAccessibilityDelegate;
    200 }
    201 
    202 ash::UserWallpaperDelegate* ChromeShellDelegate::CreateUserWallpaperDelegate() {
    203   return CreateSolidColorUserWallpaperDelegate();
    204 }
    205 
    206 void ChromeShellDelegate::Observe(int type,
    207                                   const content::NotificationSource& source,
    208                                   const content::NotificationDetails& details) {
    209   switch (type) {
    210     case chrome::NOTIFICATION_PROFILE_ADDED: {
    211       // Start the error notifier services to show sync/auth notifications.
    212       Profile* profile = content::Source<Profile>(source).ptr();
    213       SigninErrorNotifierFactory::GetForProfile(profile);
    214       SyncErrorNotifierFactory::GetForProfile(profile);
    215       break;
    216     }
    217     case chrome::NOTIFICATION_ASH_SESSION_STARTED: {
    218       // Start the error notifier services for the already loaded profiles.
    219       const std::vector<Profile*> profiles =
    220           g_browser_process->profile_manager()->GetLoadedProfiles();
    221       for (std::vector<Profile*>::const_iterator it = profiles.begin();
    222            it != profiles.end(); ++it) {
    223         SigninErrorNotifierFactory::GetForProfile(*it);
    224         SyncErrorNotifierFactory::GetForProfile(*it);
    225       }
    226 
    227 #if defined(OS_WIN)
    228       // If we are launched to service a windows 8 search request then let the
    229       // IPC which carries the search string create the browser and initiate
    230       // the navigation.
    231       if (CommandLine::ForCurrentProcess()->HasSwitch(
    232           switches::kWindows8Search))
    233         break;
    234 #endif
    235       // If Chrome ASH is launched when no browser is open in the desktop,
    236       // we should execute the startup code.
    237       // If there are browsers open in the desktop, we create a browser window
    238       // and open a new tab page, if session restore is not on.
    239       BrowserList* desktop_list = BrowserList::GetInstance(
    240           chrome::HOST_DESKTOP_TYPE_NATIVE);
    241       if (desktop_list->empty()) {
    242         // We pass a dummy command line here, because the browser is launched in
    243         // silent-mode by the metro viewer process, which causes the
    244         // StartupBrowserCreatorImpl class to not create any browsers which is
    245         // not the behavior we want.
    246         CommandLine dummy(CommandLine::NO_PROGRAM);
    247         StartupBrowserCreatorImpl startup_impl(
    248             base::FilePath(),
    249             dummy,
    250             chrome::startup::IS_NOT_FIRST_RUN);
    251         startup_impl.Launch(
    252             ProfileManager::GetActiveUserProfile(),
    253             std::vector<GURL>(),
    254             true,
    255             chrome::HOST_DESKTOP_TYPE_ASH);
    256       } else {
    257         Browser* browser =
    258             chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow());
    259         if (browser && browser->is_type_tabbed()) {
    260           chrome::AddTabAt(browser, GURL(), -1, true);
    261           return;
    262         }
    263 
    264         chrome::ScopedTabbedBrowserDisplayer displayer(
    265             ProfileManager::GetActiveUserProfile(),
    266             chrome::HOST_DESKTOP_TYPE_ASH);
    267         chrome::AddTabAt(displayer.browser(), GURL(), -1, true);
    268       }
    269       break;
    270     }
    271     case chrome::NOTIFICATION_ASH_SESSION_ENDED:
    272       break;
    273     default:
    274       NOTREACHED() << "Unexpected notification " << type;
    275   }
    276 }
    277 
    278 void ChromeShellDelegate::PlatformInit() {
    279 #if defined(OS_WIN)
    280   registrar_.Add(this,
    281                  chrome::NOTIFICATION_PROFILE_ADDED,
    282                  content::NotificationService::AllSources());
    283   registrar_.Add(this,
    284                  chrome::NOTIFICATION_ASH_SESSION_STARTED,
    285                  content::NotificationService::AllSources());
    286   registrar_.Add(this,
    287                  chrome::NOTIFICATION_ASH_SESSION_ENDED,
    288                  content::NotificationService::AllSources());
    289 #endif
    290 }
    291