1// Copyright (c) 2012 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/system_tray_delegate_win.h"
6
7#include <string>
8
9#include "ash/shell.h"
10#include "ash/shell_delegate.h"
11#include "ash/system/tray/system_tray.h"
12#include "ash/system/tray/system_tray_delegate.h"
13#include "ash/system/tray/system_tray_notifier.h"
14#include "ash/volume_control_delegate.h"
15#include "base/logging.h"
16#include "base/time/time.h"
17#include "chrome/browser/chrome_notification_types.h"
18#include "chrome/browser/lifetime/application_lifetime.h"
19#include "chrome/browser/profiles/profile_manager.h"
20#include "chrome/browser/ui/chrome_pages.h"
21#include "chrome/browser/ui/host_desktop.h"
22#include "chrome/browser/upgrade_detector.h"
23#include "chrome/grit/locale_settings.h"
24#include "content/public/browser/notification_observer.h"
25#include "content/public/browser/notification_service.h"
26#include "ui/base/l10n/l10n_util.h"
27
28namespace {
29
30class SystemTrayDelegateWin : public ash::SystemTrayDelegate,
31                              public content::NotificationObserver {
32 public:
33  SystemTrayDelegateWin()
34      : clock_type_(base::GetHourClockType()) {
35    // Register notifications on construction so that events such as
36    // PROFILE_CREATED do not get missed if they happen before Initialize().
37    registrar_.reset(new content::NotificationRegistrar);
38    registrar_->Add(this,
39                    chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
40                    content::NotificationService::AllSources());
41  }
42
43  virtual ~SystemTrayDelegateWin() {
44    registrar_.reset();
45  }
46
47  // Overridden from ash::SystemTrayDelegate:
48  virtual void Initialize() OVERRIDE {
49    UpdateClockType();
50  }
51
52  virtual void Shutdown() OVERRIDE {
53  }
54
55  virtual bool GetTrayVisibilityOnStartup() OVERRIDE {
56    return true;
57  }
58
59  virtual ash::user::LoginStatus GetUserLoginStatus() const OVERRIDE {
60    return ash::user::LOGGED_IN_OWNER;
61  }
62
63  virtual void ChangeProfilePicture() OVERRIDE {
64  }
65
66  virtual const std::string GetEnterpriseDomain() const OVERRIDE {
67    return std::string();
68  }
69
70  virtual const base::string16 GetEnterpriseMessage() const OVERRIDE {
71    return base::string16();
72  }
73
74  virtual const std::string GetSupervisedUserManager() const OVERRIDE {
75    return std::string();
76  }
77
78  virtual const base::string16 GetSupervisedUserManagerName() const OVERRIDE {
79    return base::string16();
80  }
81
82  virtual const base::string16 GetSupervisedUserMessage() const OVERRIDE {
83    return base::string16();
84  }
85
86  virtual bool IsUserSupervised() const OVERRIDE {
87    return false;
88  }
89
90  virtual bool SystemShouldUpgrade() const OVERRIDE {
91    return UpgradeDetector::GetInstance()->notify_upgrade();
92  }
93
94  virtual base::HourClockType GetHourClockType() const OVERRIDE {
95    return clock_type_;
96  }
97
98  virtual void ShowSettings() OVERRIDE {
99  }
100
101  virtual bool ShouldShowSettings() OVERRIDE {
102    return true;
103  }
104
105  virtual void ShowDateSettings() OVERRIDE {
106  }
107
108  virtual void ShowSetTimeDialog() OVERRIDE {
109  }
110
111  virtual void ShowNetworkSettings(const std::string& service_path) OVERRIDE {
112  }
113
114  virtual void ShowBluetoothSettings() OVERRIDE {
115  }
116
117  virtual void ShowDisplaySettings() OVERRIDE {
118  }
119
120  virtual void ShowChromeSlow() OVERRIDE {
121  }
122
123  virtual bool ShouldShowDisplayNotification() OVERRIDE {
124    return false;
125  }
126
127  virtual void ShowIMESettings() OVERRIDE {
128  }
129
130  virtual void ShowHelp() OVERRIDE {
131    chrome::ShowHelpForProfile(
132        ProfileManager::GetLastUsedProfile(),
133        chrome::HOST_DESKTOP_TYPE_ASH,
134        chrome::HELP_SOURCE_MENU);
135  }
136
137  virtual void ShowAccessibilityHelp() OVERRIDE {
138  }
139
140  virtual void ShowAccessibilitySettings() OVERRIDE {
141  }
142
143  virtual void ShowPublicAccountInfo() OVERRIDE {
144  }
145
146  virtual void ShowSupervisedUserInfo() OVERRIDE {
147  }
148
149  virtual void ShowEnterpriseInfo() OVERRIDE {
150  }
151
152  virtual void ShowUserLogin() OVERRIDE {
153  }
154
155  virtual bool ShowSpringChargerReplacementDialog() OVERRIDE {
156    return false;
157  }
158
159  virtual bool IsSpringChargerReplacementDialogVisible() OVERRIDE {
160    return false;
161  }
162
163  virtual bool HasUserConfirmedSafeSpringCharger() OVERRIDE {
164    return false;
165  }
166
167  virtual void ShutDown() OVERRIDE {
168  }
169
170  virtual void SignOut() OVERRIDE {
171  }
172
173  virtual void RequestLockScreen() OVERRIDE {
174  }
175
176  virtual void RequestRestartForUpdate() OVERRIDE {
177    chrome::AttemptRestart();
178  }
179
180  virtual void GetAvailableBluetoothDevices(
181      ash::BluetoothDeviceList* list) OVERRIDE {
182  }
183
184  virtual void BluetoothStartDiscovering() OVERRIDE {
185  }
186
187  virtual void BluetoothStopDiscovering() OVERRIDE {
188  }
189
190  virtual void ConnectToBluetoothDevice(const std::string& address) OVERRIDE {
191  }
192
193  virtual bool IsBluetoothDiscovering() OVERRIDE {
194    return false;
195  }
196
197  virtual void GetCurrentIME(ash::IMEInfo* info) OVERRIDE {
198  }
199
200  virtual void GetAvailableIMEList(ash::IMEInfoList* list) OVERRIDE {
201  }
202
203  virtual void GetCurrentIMEProperties(
204      ash::IMEPropertyInfoList* list) OVERRIDE {
205  }
206
207  virtual void SwitchIME(const std::string& ime_id) OVERRIDE {
208  }
209
210  virtual void ActivateIMEProperty(const std::string& key) OVERRIDE {
211  }
212
213  virtual void ShowNetworkConfigure(const std::string& network_id) OVERRIDE {
214  }
215
216  virtual bool EnrollNetwork(const std::string& network_id) OVERRIDE {
217    return true;
218  }
219
220  virtual void ManageBluetoothDevices() OVERRIDE {
221  }
222
223  virtual void ToggleBluetooth() OVERRIDE {
224  }
225
226  virtual void ShowMobileSimDialog() OVERRIDE {
227  }
228
229  virtual void ShowMobileSetupDialog(const std::string& service_path) OVERRIDE {
230  }
231
232  virtual void ShowOtherNetworkDialog(const std::string& type) OVERRIDE {
233  }
234
235  virtual bool GetBluetoothAvailable() OVERRIDE {
236    return false;
237  }
238
239  virtual bool GetBluetoothEnabled() OVERRIDE {
240    return false;
241  }
242
243  virtual bool GetBluetoothDiscovering() OVERRIDE {
244    return false;
245  }
246
247  virtual void ChangeProxySettings() OVERRIDE {
248  }
249
250  virtual ash::VolumeControlDelegate*
251  GetVolumeControlDelegate() const OVERRIDE {
252    return NULL;
253  }
254
255  virtual void SetVolumeControlDelegate(
256      scoped_ptr<ash::VolumeControlDelegate> delegate) OVERRIDE {
257  }
258
259  virtual bool GetSessionStartTime(
260      base::TimeTicks* session_start_time) OVERRIDE {
261    return false;
262  }
263
264  virtual bool GetSessionLengthLimit(
265      base::TimeDelta* session_length_limit) OVERRIDE {
266    return false;
267  }
268
269  virtual int GetSystemTrayMenuWidth() OVERRIDE {
270    return l10n_util::GetLocalizedContentsWidthInPixels(
271        IDS_SYSTEM_TRAY_MENU_BUBBLE_WIDTH_PIXELS);
272  }
273
274  virtual void ActiveUserWasChanged() OVERRIDE {
275  }
276
277  virtual bool IsSearchKeyMappedToCapsLock() OVERRIDE {
278    return false;
279  }
280
281  virtual ash::tray::UserAccountsDelegate* GetUserAccountsDelegate(
282      const std::string& user_id) OVERRIDE {
283    return NULL;
284  }
285
286 private:
287  ash::SystemTrayNotifier* GetSystemTrayNotifier() {
288    return ash::Shell::GetInstance()->system_tray_notifier();
289  }
290
291  void UpdateClockType() {
292    clock_type_ = (base::GetHourClockType() == base::k24HourClock) ?
293        base::k24HourClock : base::k12HourClock;
294    GetSystemTrayNotifier()->NotifyDateFormatChanged();
295  }
296
297  // content::NotificationObserver implementation.
298  virtual void Observe(int type,
299                       const content::NotificationSource& source,
300                       const content::NotificationDetails& details) OVERRIDE {
301    if (type == chrome::NOTIFICATION_UPGRADE_RECOMMENDED) {
302        UpgradeDetector* detector =
303            content::Source<UpgradeDetector>(source).ptr();
304      ash::UpdateObserver::UpdateSeverity severity =
305          ash::UpdateObserver::UPDATE_NORMAL;
306      switch (detector->upgrade_notification_stage()) {
307        case UpgradeDetector::UPGRADE_ANNOYANCE_CRITICAL:
308        case UpgradeDetector::UPGRADE_ANNOYANCE_SEVERE:
309          severity = ash::UpdateObserver::UPDATE_SEVERE_RED;
310          break;
311        case UpgradeDetector::UPGRADE_ANNOYANCE_HIGH:
312          severity = ash::UpdateObserver::UPDATE_HIGH_ORANGE;
313          break;
314        case UpgradeDetector::UPGRADE_ANNOYANCE_ELEVATED:
315          severity = ash::UpdateObserver::UPDATE_LOW_GREEN;
316          break;
317        case UpgradeDetector::UPGRADE_ANNOYANCE_LOW:
318        case UpgradeDetector::UPGRADE_ANNOYANCE_NONE:
319          severity = ash::UpdateObserver::UPDATE_NORMAL;
320          break;
321      }
322      GetSystemTrayNotifier()->NotifyUpdateRecommended(severity);
323    } else {
324      NOTREACHED();
325    }
326  }
327
328  scoped_ptr<content::NotificationRegistrar> registrar_;
329  base::HourClockType clock_type_;
330
331  DISALLOW_COPY_AND_ASSIGN(SystemTrayDelegateWin);
332};
333
334}  // namespace
335
336
337ash::SystemTrayDelegate* CreateWindowsSystemTrayDelegate() {
338  return new SystemTrayDelegateWin();
339}
340