blob: 67e3f3b3fc7585313fc6d8e8148c5189519e32c6 [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"
estade1e949edc2016-02-08 21:19:4616#include "ui/gfx/color_palette.h"
17#include "ui/gfx/paint_vector_icon.h"
vitaliii0c2a3a292017-01-09 08:52:0718#include "ui/gfx/vector_icons_public.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
vitaliii0c2a3a292017-01-09 08:52:0760 return gfx::Image(
61 gfx::CreateVectorIcon(gfx::VectorIconId::BROWSER_TOOLS_UPDATE, color));
[email protected]21788142011-05-03 10:38:3562}
63
[email protected]8fcec3c72010-06-03 00:17:2264UpgradeDetector::UpgradeDetector()
[email protected]2a54f2d2013-02-15 05:54:3665 : upgrade_available_(UPGRADE_AVAILABLE_NONE),
[email protected]dd46dd52014-08-07 21:57:1466 best_effort_experiment_updates_available_(false),
67 critical_experiment_updates_available_(false),
[email protected]f1c76b9f2011-10-13 13:43:4468 critical_update_acknowledged_(false),
ygorshenin9903cf42014-10-07 06:47:1169 is_factory_reset_required_(false),
[email protected]f1c76b9f2011-10-13 13:43:4470 upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE),
[email protected]8fcec3c72010-06-03 00:17:2271 notify_upgrade_(false) {
[email protected]b1b73942010-05-26 20:11:5472}
73
74UpgradeDetector::~UpgradeDetector() {
75}
76
[email protected]ceff8402011-06-12 23:27:1277void UpgradeDetector::NotifyUpgradeRecommended() {
[email protected]b1b73942010-05-26 20:11:5478 notify_upgrade_ = true;
79
[email protected]dd46dd52014-08-07 21:57:1480 TriggerNotification(chrome::NOTIFICATION_UPGRADE_RECOMMENDED);
81 if (upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL) {
82 TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL);
83 } else if (upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL_NO_AU) {
84 TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL_NO_AU);
85 } else if (upgrade_available_ == UPGRADE_AVAILABLE_CRITICAL ||
86 critical_experiment_updates_available_) {
87 TriggerCriticalUpdate();
[email protected]f1c76b9f2011-10-13 13:43:4488 }
89}
90
[email protected]dd46dd52014-08-07 21:57:1491void UpgradeDetector::TriggerCriticalUpdate() {
92 const base::TimeDelta idle_timer = UseTestingIntervals() ?
93 base::TimeDelta::FromSeconds(kIdleRepeatingTimerWait) :
94 base::TimeDelta::FromMinutes(kIdleRepeatingTimerWait);
95 idle_check_timer_.Start(FROM_HERE, idle_timer, this,
96 &UpgradeDetector::CheckIdle);
97}
98
[email protected]f1c76b9f2011-10-13 13:43:4499void UpgradeDetector::CheckIdle() {
100 // CalculateIdleState expects an interval in seconds.
101 int idle_time_allowed = UseTestingIntervals() ? kIdleAmount :
102 kIdleAmount * 60 * 60;
103
104 CalculateIdleState(
105 idle_time_allowed, base::Bind(&UpgradeDetector::IdleCallback,
106 base::Unretained(this)));
107}
108
[email protected]dd46dd52014-08-07 21:57:14109void UpgradeDetector::TriggerNotification(chrome::NotificationType type) {
110 content::NotificationService::current()->Notify(
111 type,
112 content::Source<UpgradeDetector>(this),
113 content::NotificationService::NoDetails());
114}
115
derat49b2fd52015-01-16 15:32:12116void UpgradeDetector::IdleCallback(ui::IdleState state) {
[email protected]914ace62012-10-22 17:16:02117 // Don't proceed while an incognito window is open. The timer will still
118 // keep firing, so this function will get a chance to re-evaluate this.
trizzofofc061d02016-07-07 00:51:21119 if (chrome::IsIncognitoSessionActive())
[email protected]914ace62012-10-22 17:16:02120 return;
121
[email protected]f1c76b9f2011-10-13 13:43:44122 switch (state) {
derat49b2fd52015-01-16 15:32:12123 case ui::IDLE_STATE_LOCKED:
[email protected]f1c76b9f2011-10-13 13:43:44124 // Computer is locked, auto-restart.
125 idle_check_timer_.Stop();
[email protected]0c98ab652013-02-18 00:39:37126 chrome::AttemptRestart();
[email protected]f1c76b9f2011-10-13 13:43:44127 break;
derat49b2fd52015-01-16 15:32:12128 case ui::IDLE_STATE_IDLE:
[email protected]f1c76b9f2011-10-13 13:43:44129 // Computer has been idle for long enough, show warning.
130 idle_check_timer_.Stop();
[email protected]dd46dd52014-08-07 21:57:14131 TriggerNotification(chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED);
[email protected]f1c76b9f2011-10-13 13:43:44132 break;
derat49b2fd52015-01-16 15:32:12133 case ui::IDLE_STATE_ACTIVE:
134 case ui::IDLE_STATE_UNKNOWN:
[email protected]f1c76b9f2011-10-13 13:43:44135 break;
136 default:
137 NOTREACHED(); // Need to add any new value above (either providing
138 // automatic restart or show notification to user).
139 break;
140 }
[email protected]b1b73942010-05-26 20:11:54141}