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.
5 #include "chrome/browser/upgrade_detector.h"
8 #include "base/command_line.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/lifetime/application_lifetime.h"
12 #include "chrome/browser/ui/browser_otr_state.h"
13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/common/pref_names.h"
15 #include "content/public/browser/notification_service.h"
16 #include "grit/theme_resources.h"
18 // How long to wait between checks for whether the user has been idle.
19 static const int kIdleRepeatingTimerWait
= 10; // Minutes (seconds if testing).
21 // How much idle time (since last input even was detected) must have passed
22 // until we notify that a critical update has occurred.
23 static const int kIdleAmount
= 2; // Hours (or seconds, if testing).
25 bool UseTestingIntervals() {
26 // If a command line parameter specifying how long the upgrade check should
27 // be, we assume it is for testing and switch to using seconds instead of
29 const base::CommandLine
& cmd_line
= *base::CommandLine::ForCurrentProcess();
30 return !cmd_line
.GetSwitchValueASCII(
31 switches::kCheckForUpdateIntervalSec
).empty();
35 void UpgradeDetector::RegisterPrefs(PrefRegistrySimple
* registry
) {
36 registry
->RegisterBooleanPref(prefs::kRestartLastSessionOnShutdown
, false);
37 registry
->RegisterBooleanPref(prefs::kWasRestarted
, false);
38 registry
->RegisterBooleanPref(prefs::kAttemptedToEnableAutoupdate
, false);
41 int UpgradeDetector::GetIconResourceID() {
42 switch (upgrade_notification_stage_
) {
43 case UPGRADE_ANNOYANCE_NONE
:
45 case UPGRADE_ANNOYANCE_LOW
:
46 return IDR_UPDATE_MENU_SEVERITY_LOW
;
47 case UPGRADE_ANNOYANCE_ELEVATED
:
48 return IDR_UPDATE_MENU_SEVERITY_MEDIUM
;
49 case UPGRADE_ANNOYANCE_HIGH
:
50 return IDR_UPDATE_MENU_SEVERITY_HIGH
;
51 case UPGRADE_ANNOYANCE_SEVERE
:
52 return IDR_UPDATE_MENU_SEVERITY_HIGH
;
53 case UPGRADE_ANNOYANCE_CRITICAL
:
54 return IDR_UPDATE_MENU_SEVERITY_HIGH
;
60 UpgradeDetector::UpgradeDetector()
61 : upgrade_available_(UPGRADE_AVAILABLE_NONE
),
62 best_effort_experiment_updates_available_(false),
63 critical_experiment_updates_available_(false),
64 critical_update_acknowledged_(false),
65 is_factory_reset_required_(false),
66 upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE
),
67 notify_upgrade_(false) {
70 UpgradeDetector::~UpgradeDetector() {
73 void UpgradeDetector::NotifyUpgradeRecommended() {
74 notify_upgrade_
= true;
76 TriggerNotification(chrome::NOTIFICATION_UPGRADE_RECOMMENDED
);
77 if (upgrade_available_
== UPGRADE_NEEDED_OUTDATED_INSTALL
) {
78 TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL
);
79 } else if (upgrade_available_
== UPGRADE_NEEDED_OUTDATED_INSTALL_NO_AU
) {
80 TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL_NO_AU
);
81 } else if (upgrade_available_
== UPGRADE_AVAILABLE_CRITICAL
||
82 critical_experiment_updates_available_
) {
83 TriggerCriticalUpdate();
87 void UpgradeDetector::TriggerCriticalUpdate() {
88 const base::TimeDelta idle_timer
= UseTestingIntervals() ?
89 base::TimeDelta::FromSeconds(kIdleRepeatingTimerWait
) :
90 base::TimeDelta::FromMinutes(kIdleRepeatingTimerWait
);
91 idle_check_timer_
.Start(FROM_HERE
, idle_timer
, this,
92 &UpgradeDetector::CheckIdle
);
95 void UpgradeDetector::CheckIdle() {
96 // CalculateIdleState expects an interval in seconds.
97 int idle_time_allowed
= UseTestingIntervals() ? kIdleAmount
:
98 kIdleAmount
* 60 * 60;
101 idle_time_allowed
, base::Bind(&UpgradeDetector::IdleCallback
,
102 base::Unretained(this)));
105 void UpgradeDetector::TriggerNotification(chrome::NotificationType type
) {
106 content::NotificationService::current()->Notify(
108 content::Source
<UpgradeDetector
>(this),
109 content::NotificationService::NoDetails());
112 void UpgradeDetector::IdleCallback(ui::IdleState state
) {
113 // Don't proceed while an incognito window is open. The timer will still
114 // keep firing, so this function will get a chance to re-evaluate this.
115 if (chrome::IsOffTheRecordSessionActive())
119 case ui::IDLE_STATE_LOCKED
:
120 // Computer is locked, auto-restart.
121 idle_check_timer_
.Stop();
122 chrome::AttemptRestart();
124 case ui::IDLE_STATE_IDLE
:
125 // Computer has been idle for long enough, show warning.
126 idle_check_timer_
.Stop();
127 TriggerNotification(chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED
);
129 case ui::IDLE_STATE_ACTIVE
:
130 case ui::IDLE_STATE_UNKNOWN
:
133 NOTREACHED(); // Need to add any new value above (either providing
134 // automatic restart or show notification to user).