blob: 2dfd1a91560d353cf9753dfa8654eeb8046c4483 [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"
estade01876af82017-01-09 21:35:439#include "chrome/app/vector_icons/vector_icons.h"
[email protected]fdf40f3e2013-07-11 23:55:4610#include "chrome/browser/chrome_notification_types.h"
[email protected]2e6389f2012-05-18 19:41:2511#include "chrome/browser/lifetime/application_lifetime.h"
[email protected]914ace62012-10-22 17:16:0212#include "chrome/browser/ui/browser_otr_state.h"
[email protected]f1c76b9f2011-10-13 13:43:4413#include "chrome/common/chrome_switches.h"
[email protected]8fcec3c72010-06-03 00:17:2214#include "chrome/common/pref_names.h"
brettwb1fc1b82016-02-02 00:19:0815#include "components/prefs/pref_registry_simple.h"
[email protected]ad50def52011-10-19 23:17:0716#include "content/public/browser/notification_service.h"
estade1e949edc2016-02-08 21:19:4617#include "ui/gfx/color_palette.h"
18#include "ui/gfx/paint_vector_icon.h"
[email protected]8fcec3c72010-06-03 00:17:2219
[email protected]f1c76b9f2011-10-13 13:43:4420// How long to wait between checks for whether the user has been idle.
21static const int kIdleRepeatingTimerWait = 10; // Minutes (seconds if testing).
22
23// How much idle time (since last input even was detected) must have passed
24// until we notify that a critical update has occurred.
25static const int kIdleAmount = 2; // Hours (or seconds, if testing).
26
27bool UseTestingIntervals() {
28 // If a command line parameter specifying how long the upgrade check should
29 // be, we assume it is for testing and switch to using seconds instead of
30 // hours.
avi556c05022014-12-22 23:31:4331 const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess();
[email protected]f1c76b9f2011-10-13 13:43:4432 return !cmd_line.GetSwitchValueASCII(
33 switches::kCheckForUpdateIntervalSec).empty();
34}
35
[email protected]8fcec3c72010-06-03 00:17:2236// static
[email protected]b1de2c72013-02-06 02:45:4737void UpgradeDetector::RegisterPrefs(PrefRegistrySimple* registry) {
[email protected]d50dd9d2014-03-28 21:46:2438 registry->RegisterBooleanPref(prefs::kAttemptedToEnableAutoupdate, false);
[email protected]8fcec3c72010-06-03 00:17:2239}
40
estade1e949edc2016-02-08 21:19:4641gfx::Image UpgradeDetector::GetIcon() {
42 SkColor color = gfx::kPlaceholderColor;
[email protected]2735ec62013-04-23 05:37:1143 switch (upgrade_notification_stage_) {
44 case UPGRADE_ANNOYANCE_NONE:
estade1e949edc2016-02-08 21:19:4645 return gfx::Image();
[email protected]2735ec62013-04-23 05:37:1146 case UPGRADE_ANNOYANCE_LOW:
estade1e949edc2016-02-08 21:19:4647 color = gfx::kGoogleGreen700;
48 break;
[email protected]2735ec62013-04-23 05:37:1149 case UPGRADE_ANNOYANCE_ELEVATED:
estade1e949edc2016-02-08 21:19:4650 color = gfx::kGoogleYellow700;
51 break;
[email protected]2735ec62013-04-23 05:37:1152 case UPGRADE_ANNOYANCE_HIGH:
[email protected]2735ec62013-04-23 05:37:1153 case UPGRADE_ANNOYANCE_SEVERE:
[email protected]2735ec62013-04-23 05:37:1154 case UPGRADE_ANNOYANCE_CRITICAL:
estade1e949edc2016-02-08 21:19:4655 color = gfx::kGoogleRed700;
56 break;
[email protected]2735ec62013-04-23 05:37:1157 }
estade1e949edc2016-02-08 21:19:4658 DCHECK_NE(gfx::kPlaceholderColor, color);
59
estade01876af82017-01-09 21:35:4360 return gfx::Image(gfx::CreateVectorIcon(kBrowserToolsUpdateIcon, color));
[email protected]21788142011-05-03 10:38:3561}
62
[email protected]8fcec3c72010-06-03 00:17:2263UpgradeDetector::UpgradeDetector()
[email protected]2a54f2d2013-02-15 05:54:3664 : upgrade_available_(UPGRADE_AVAILABLE_NONE),
[email protected]dd46dd52014-08-07 21:57:1465 best_effort_experiment_updates_available_(false),
66 critical_experiment_updates_available_(false),
[email protected]f1c76b9f2011-10-13 13:43:4467 critical_update_acknowledged_(false),
ygorshenin9903cf42014-10-07 06:47:1168 is_factory_reset_required_(false),
[email protected]f1c76b9f2011-10-13 13:43:4469 upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE),
[email protected]8fcec3c72010-06-03 00:17:2270 notify_upgrade_(false) {
[email protected]b1b73942010-05-26 20:11:5471}
72
73UpgradeDetector::~UpgradeDetector() {
74}
75
[email protected]ceff8402011-06-12 23:27:1276void UpgradeDetector::NotifyUpgradeRecommended() {
[email protected]b1b73942010-05-26 20:11:5477 notify_upgrade_ = true;
78
[email protected]dd46dd52014-08-07 21:57:1479 TriggerNotification(chrome::NOTIFICATION_UPGRADE_RECOMMENDED);
80 if (upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL) {
81 TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL);
82 } else if (upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL_NO_AU) {
83 TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL_NO_AU);
84 } else if (upgrade_available_ == UPGRADE_AVAILABLE_CRITICAL ||
85 critical_experiment_updates_available_) {
86 TriggerCriticalUpdate();
[email protected]f1c76b9f2011-10-13 13:43:4487 }
88}
89
[email protected]dd46dd52014-08-07 21:57:1490void UpgradeDetector::TriggerCriticalUpdate() {
91 const base::TimeDelta idle_timer = UseTestingIntervals() ?
92 base::TimeDelta::FromSeconds(kIdleRepeatingTimerWait) :
93 base::TimeDelta::FromMinutes(kIdleRepeatingTimerWait);
94 idle_check_timer_.Start(FROM_HERE, idle_timer, this,
95 &UpgradeDetector::CheckIdle);
96}
97
[email protected]f1c76b9f2011-10-13 13:43:4498void UpgradeDetector::CheckIdle() {
99 // CalculateIdleState expects an interval in seconds.
100 int idle_time_allowed = UseTestingIntervals() ? kIdleAmount :
101 kIdleAmount * 60 * 60;
102
103 CalculateIdleState(
104 idle_time_allowed, base::Bind(&UpgradeDetector::IdleCallback,
105 base::Unretained(this)));
106}
107
[email protected]dd46dd52014-08-07 21:57:14108void UpgradeDetector::TriggerNotification(chrome::NotificationType type) {
109 content::NotificationService::current()->Notify(
110 type,
111 content::Source<UpgradeDetector>(this),
112 content::NotificationService::NoDetails());
113}
114
derat49b2fd52015-01-16 15:32:12115void UpgradeDetector::IdleCallback(ui::IdleState state) {
[email protected]914ace62012-10-22 17:16:02116 // Don't proceed while an incognito window is open. The timer will still
117 // keep firing, so this function will get a chance to re-evaluate this.
trizzofofc061d02016-07-07 00:51:21118 if (chrome::IsIncognitoSessionActive())
[email protected]914ace62012-10-22 17:16:02119 return;
120
[email protected]f1c76b9f2011-10-13 13:43:44121 switch (state) {
derat49b2fd52015-01-16 15:32:12122 case ui::IDLE_STATE_LOCKED:
[email protected]f1c76b9f2011-10-13 13:43:44123 // Computer is locked, auto-restart.
124 idle_check_timer_.Stop();
[email protected]0c98ab652013-02-18 00:39:37125 chrome::AttemptRestart();
[email protected]f1c76b9f2011-10-13 13:43:44126 break;
derat49b2fd52015-01-16 15:32:12127 case ui::IDLE_STATE_IDLE:
[email protected]f1c76b9f2011-10-13 13:43:44128 // Computer has been idle for long enough, show warning.
129 idle_check_timer_.Stop();
[email protected]dd46dd52014-08-07 21:57:14130 TriggerNotification(chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED);
[email protected]f1c76b9f2011-10-13 13:43:44131 break;
derat49b2fd52015-01-16 15:32:12132 case ui::IDLE_STATE_ACTIVE:
133 case ui::IDLE_STATE_UNKNOWN:
[email protected]f1c76b9f2011-10-13 13:43:44134 break;
135 default:
136 NOTREACHED(); // Need to add any new value above (either providing
137 // automatic restart or show notification to user).
138 break;
139 }
[email protected]b1b73942010-05-26 20:11:54140}