1// Copyright (c) 2011 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#ifndef CHROME_BROWSER_UPGRADE_DETECTOR_H_
6#define CHROME_BROWSER_UPGRADE_DETECTOR_H_
7
8#include "base/timer/timer.h"
9#include "chrome/browser/chrome_notification_types.h"
10#include "chrome/browser/idle.h"
11#include "ui/gfx/image/image.h"
12
13class PrefRegistrySimple;
14
15///////////////////////////////////////////////////////////////////////////////
16// UpgradeDetector
17//
18// This class is a singleton class that monitors when an upgrade happens in the
19// background. We basically ask Omaha what it thinks the latest version is and
20// if our version is lower we send out a notification upon:
21//   a) Detecting an upgrade and...
22//   b) When we think the user should be notified about the upgrade.
23// The latter happens much later, since we don't want to be too annoying.
24//
25class UpgradeDetector {
26 public:
27  // The Homeland Security Upgrade Advisory System.
28  enum UpgradeNotificationAnnoyanceLevel {
29    UPGRADE_ANNOYANCE_NONE = 0,  // What? Me worry?
30    UPGRADE_ANNOYANCE_LOW,       // Green.
31    UPGRADE_ANNOYANCE_ELEVATED,  // Yellow.
32    UPGRADE_ANNOYANCE_HIGH,      // Red.
33    UPGRADE_ANNOYANCE_SEVERE,    // Orange.
34    UPGRADE_ANNOYANCE_CRITICAL,  // Red exclamation mark.
35  };
36
37  // Returns the singleton implementation instance.
38  static UpgradeDetector* GetInstance();
39
40  virtual ~UpgradeDetector();
41
42  static void RegisterPrefs(PrefRegistrySimple* registry);
43
44  // Whether the user should be notified about an upgrade.
45  bool notify_upgrade() const { return notify_upgrade_; }
46
47  // Whether the upgrade recommendation is due to Chrome being outdated.
48  bool is_outdated_install() const {
49    return upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL;
50  }
51
52  // Whether the upgrade recommendation is due to Chrome being outdated AND
53  // auto-update is turned off.
54  bool is_outdated_install_no_au() const {
55    return upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL_NO_AU;
56  }
57
58  // Notifify this object that the user has acknowledged the critical update
59  // so we don't need to complain about it for now.
60  void acknowledge_critical_update() {
61    critical_update_acknowledged_ = true;
62  }
63
64  // Whether the user has acknowledged the critical update.
65  bool critical_update_acknowledged() const {
66    return critical_update_acknowledged_;
67  }
68
69  // Retrieves the right icon ID based on the degree of severity (see
70  // UpgradeNotificationAnnoyanceLevel, each level has an an accompanying icon
71  // to go with it) to display within the wrench menu.
72  int GetIconResourceID();
73
74  UpgradeNotificationAnnoyanceLevel upgrade_notification_stage() const {
75    return upgrade_notification_stage_;
76  }
77
78 protected:
79  enum UpgradeAvailable {
80    // If no update is available and current install is recent enough.
81    UPGRADE_AVAILABLE_NONE,
82    // If a regular update is available.
83    UPGRADE_AVAILABLE_REGULAR,
84    // If a critical update to Chrome has been installed, such as a zero-day
85    // fix.
86    UPGRADE_AVAILABLE_CRITICAL,
87    // If no update to Chrome has been installed for more than the recommended
88    // time.
89    UPGRADE_NEEDED_OUTDATED_INSTALL,
90    // If no update to Chrome has been installed for more than the recommended
91    // time AND auto-update is turned off.
92    UPGRADE_NEEDED_OUTDATED_INSTALL_NO_AU,
93  };
94
95  UpgradeDetector();
96
97  // Sends out UPGRADE_RECOMMENDED notification and set notify_upgrade_.
98  void NotifyUpgradeRecommended();
99
100  // Triggers a critical update, which starts a timer that checks the machine
101  // idle state. Protected and virtual so that it could be overridden by tests.
102  virtual void TriggerCriticalUpdate();
103
104  UpgradeAvailable upgrade_available() const { return upgrade_available_; }
105  void set_upgrade_available(UpgradeAvailable available) {
106    upgrade_available_ = available;
107  }
108
109  void set_best_effort_experiment_updates_available(bool available) {
110    best_effort_experiment_updates_available_ = available;
111  }
112
113  bool critical_experiment_updates_available() const {
114    return critical_experiment_updates_available_;
115  }
116  void set_critical_experiment_updates_available(bool available) {
117    critical_experiment_updates_available_ = available;
118  }
119
120  void set_critical_update_acknowledged(bool acknowledged) {
121    critical_update_acknowledged_ = acknowledged;
122  }
123
124  void set_upgrade_notification_stage(UpgradeNotificationAnnoyanceLevel stage) {
125    upgrade_notification_stage_ = stage;
126  }
127
128 private:
129  // Initiates an Idle check. See IdleCallback below.
130  void CheckIdle();
131
132  // The callback for the IdleCheck. Tells us whether Chrome has received any
133  // input events since the specified time.
134  void IdleCallback(IdleState state);
135
136  // Triggers a global notification of the specified |type|.
137  void TriggerNotification(chrome::NotificationType type);
138
139  // Whether any software updates are available (experiment updates are tracked
140  // separately via additional member variables below).
141  UpgradeAvailable upgrade_available_;
142
143  // Whether "best effort" experiment updates are available.
144  bool best_effort_experiment_updates_available_;
145
146  // Whether "critical" experiment updates are available.
147  bool critical_experiment_updates_available_;
148
149  // Whether the user has acknowledged the critical update.
150  bool critical_update_acknowledged_;
151
152  // A timer to check to see if we've been idle for long enough to show the
153  // critical warning. Should only be set if |upgrade_available_| is
154  // UPGRADE_AVAILABLE_CRITICAL.
155  base::RepeatingTimer<UpgradeDetector> idle_check_timer_;
156
157  // The stage at which the annoyance level for upgrade notifications is at.
158  UpgradeNotificationAnnoyanceLevel upgrade_notification_stage_;
159
160  // Whether we have waited long enough after detecting an upgrade (to see
161  // is we should start nagging about upgrading).
162  bool notify_upgrade_;
163
164  DISALLOW_COPY_AND_ASSIGN(UpgradeDetector);
165};
166
167#endif  // CHROME_BROWSER_UPGRADE_DETECTOR_H_
168