blob: ab3a4cfaeb7c2191aaa85668e56a9608f3674510 [file] [log] [blame]
[email protected]d7a8ef62012-06-22 16:28:461// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b1b73942010-05-26 20:11:542// 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/upgrade_detector.h"
6
[email protected]f1c76b9f2011-10-13 13:43:447#include "base/bind.h"
8#include "base/command_line.h"
[email protected]fdf40f3e2013-07-11 23:55:469#include "chrome/browser/chrome_notification_types.h"
[email protected]2e6389f2012-05-18 19:41:2510#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]914ace62012-10-22 17:16:0211#include "chrome/browser/ui/browser_otr_state.h"
[email protected]f1c76b9f2011-10-13 13:43:4412#include "chrome/common/chrome_switches.h"
[email protected]8fcec3c72010-06-03 00:17:2213#include "chrome/common/pref_names.h"
brettwb1fc1b82016-02-02 00:19:0814#include "components/prefs/pref_registry_simple.h"
[email protected]ad50def52011-10-19 23:17:0715#include "content/public/browser/notification_service.h"
[email protected]2a281332012-07-11 22:20:2316#include "grit/theme_resources.h"
[email protected]8fcec3c72010-06-03 00:17:2217
[email protected]f1c76b9f2011-10-13 13:43:4418// How long to wait between checks for whether the user has been idle.
19static const int kIdleRepeatingTimerWait = 10; // Minutes (seconds if testing).
20
21// How much idle time (since last input even was detected) must have passed
22// until we notify that a critical update has occurred.
23static const int kIdleAmount = 2; // Hours (or seconds, if testing).
24
25bool 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
28 // hours.
avi556c05022014-12-22 23:31:4329 const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess();
[email protected]f1c76b9f2011-10-13 13:43:4430 return !cmd_line.GetSwitchValueASCII(
31 switches::kCheckForUpdateIntervalSec).empty();
32}
33
[email protected]8fcec3c72010-06-03 00:17:2234// static
[email protected]b1de2c72013-02-06 02:45:4735void UpgradeDetector::RegisterPrefs(PrefRegistrySimple* registry) {
[email protected]d50dd9d2014-03-28 21:46:2436 registry->RegisterBooleanPref(prefs::kAttemptedToEnableAutoupdate, false);
[email protected]8fcec3c72010-06-03 00:17:2237}
38
[email protected]767e1fa62014-07-22 00:53:4339int UpgradeDetector::GetIconResourceID() {
[email protected]2735ec62013-04-23 05:37:1140 switch (upgrade_notification_stage_) {
41 case UPGRADE_ANNOYANCE_NONE:
42 return 0;
43 case UPGRADE_ANNOYANCE_LOW:
44 return IDR_UPDATE_MENU_SEVERITY_LOW;
45 case UPGRADE_ANNOYANCE_ELEVATED:
46 return IDR_UPDATE_MENU_SEVERITY_MEDIUM;
47 case UPGRADE_ANNOYANCE_HIGH:
48 return IDR_UPDATE_MENU_SEVERITY_HIGH;
49 case UPGRADE_ANNOYANCE_SEVERE:
50 return IDR_UPDATE_MENU_SEVERITY_HIGH;
51 case UPGRADE_ANNOYANCE_CRITICAL:
52 return IDR_UPDATE_MENU_SEVERITY_HIGH;
53 }
54 NOTREACHED();
55 return 0;
[email protected]21788142011-05-03 10:38:3556}
57
[email protected]8fcec3c72010-06-03 00:17:2258UpgradeDetector::UpgradeDetector()
[email protected]2a54f2d2013-02-15 05:54:3659 : upgrade_available_(UPGRADE_AVAILABLE_NONE),
[email protected]dd46dd52014-08-07 21:57:1460 best_effort_experiment_updates_available_(false),
61 critical_experiment_updates_available_(false),
[email protected]f1c76b9f2011-10-13 13:43:4462 critical_update_acknowledged_(false),
ygorshenin9903cf42014-10-07 06:47:1163 is_factory_reset_required_(false),
[email protected]f1c76b9f2011-10-13 13:43:4464 upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE),
[email protected]8fcec3c72010-06-03 00:17:2265 notify_upgrade_(false) {
[email protected]b1b73942010-05-26 20:11:5466}
67
68UpgradeDetector::~UpgradeDetector() {
69}
70
[email protected]ceff8402011-06-12 23:27:1271void UpgradeDetector::NotifyUpgradeRecommended() {
[email protected]b1b73942010-05-26 20:11:5472 notify_upgrade_ = true;
73
[email protected]dd46dd52014-08-07 21:57:1474 TriggerNotification(chrome::NOTIFICATION_UPGRADE_RECOMMENDED);
75 if (upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL) {
76 TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL);
77 } else if (upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL_NO_AU) {
78 TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL_NO_AU);
79 } else if (upgrade_available_ == UPGRADE_AVAILABLE_CRITICAL ||
80 critical_experiment_updates_available_) {
81 TriggerCriticalUpdate();
[email protected]f1c76b9f2011-10-13 13:43:4482 }
83}
84
[email protected]dd46dd52014-08-07 21:57:1485void UpgradeDetector::TriggerCriticalUpdate() {
86 const base::TimeDelta idle_timer = UseTestingIntervals() ?
87 base::TimeDelta::FromSeconds(kIdleRepeatingTimerWait) :
88 base::TimeDelta::FromMinutes(kIdleRepeatingTimerWait);
89 idle_check_timer_.Start(FROM_HERE, idle_timer, this,
90 &UpgradeDetector::CheckIdle);
91}
92
[email protected]f1c76b9f2011-10-13 13:43:4493void UpgradeDetector::CheckIdle() {
94 // CalculateIdleState expects an interval in seconds.
95 int idle_time_allowed = UseTestingIntervals() ? kIdleAmount :
96 kIdleAmount * 60 * 60;
97
98 CalculateIdleState(
99 idle_time_allowed, base::Bind(&UpgradeDetector::IdleCallback,
100 base::Unretained(this)));
101}
102
[email protected]dd46dd52014-08-07 21:57:14103void UpgradeDetector::TriggerNotification(chrome::NotificationType type) {
104 content::NotificationService::current()->Notify(
105 type,
106 content::Source<UpgradeDetector>(this),
107 content::NotificationService::NoDetails());
108}
109
derat49b2fd52015-01-16 15:32:12110void UpgradeDetector::IdleCallback(ui::IdleState state) {
[email protected]914ace62012-10-22 17:16:02111 // Don't proceed while an incognito window is open. The timer will still
112 // keep firing, so this function will get a chance to re-evaluate this.
113 if (chrome::IsOffTheRecordSessionActive())
114 return;
115
[email protected]f1c76b9f2011-10-13 13:43:44116 switch (state) {
derat49b2fd52015-01-16 15:32:12117 case ui::IDLE_STATE_LOCKED:
[email protected]f1c76b9f2011-10-13 13:43:44118 // Computer is locked, auto-restart.
119 idle_check_timer_.Stop();
[email protected]0c98ab652013-02-18 00:39:37120 chrome::AttemptRestart();
[email protected]f1c76b9f2011-10-13 13:43:44121 break;
derat49b2fd52015-01-16 15:32:12122 case ui::IDLE_STATE_IDLE:
[email protected]f1c76b9f2011-10-13 13:43:44123 // Computer has been idle for long enough, show warning.
124 idle_check_timer_.Stop();
[email protected]dd46dd52014-08-07 21:57:14125 TriggerNotification(chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED);
[email protected]f1c76b9f2011-10-13 13:43:44126 break;
derat49b2fd52015-01-16 15:32:12127 case ui::IDLE_STATE_ACTIVE:
128 case ui::IDLE_STATE_UNKNOWN:
[email protected]f1c76b9f2011-10-13 13:43:44129 break;
130 default:
131 NOTREACHED(); // Need to add any new value above (either providing
132 // automatic restart or show notification to user).
133 break;
134 }
[email protected]b1b73942010-05-26 20:11:54135}