blob: 23db83d8f6c2b38bd064ebe6a57d3268ec0ee0e8 [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"
estade1e949edc2016-02-08 21:19:4617#include "ui/gfx/color_palette.h"
18#include "ui/gfx/paint_vector_icon.h"
19#include "ui/gfx/vector_icons_public.h"
[email protected]8fcec3c72010-06-03 00:17:2220
[email protected]f1c76b9f2011-10-13 13:43:4421// How long to wait between checks for whether the user has been idle.
22static const int kIdleRepeatingTimerWait = 10; // Minutes (seconds if testing).
23
24// How much idle time (since last input even was detected) must have passed
25// until we notify that a critical update has occurred.
26static const int kIdleAmount = 2; // Hours (or seconds, if testing).
27
28bool UseTestingIntervals() {
29 // If a command line parameter specifying how long the upgrade check should
30 // be, we assume it is for testing and switch to using seconds instead of
31 // hours.
avi556c05022014-12-22 23:31:4332 const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess();
[email protected]f1c76b9f2011-10-13 13:43:4433 return !cmd_line.GetSwitchValueASCII(
34 switches::kCheckForUpdateIntervalSec).empty();
35}
36
[email protected]8fcec3c72010-06-03 00:17:2237// static
[email protected]b1de2c72013-02-06 02:45:4738void UpgradeDetector::RegisterPrefs(PrefRegistrySimple* registry) {
[email protected]d50dd9d2014-03-28 21:46:2439 registry->RegisterBooleanPref(prefs::kAttemptedToEnableAutoupdate, false);
[email protected]8fcec3c72010-06-03 00:17:2240}
41
estade1e949edc2016-02-08 21:19:4642gfx::Image UpgradeDetector::GetIcon() {
43 SkColor color = gfx::kPlaceholderColor;
[email protected]2735ec62013-04-23 05:37:1144 switch (upgrade_notification_stage_) {
45 case UPGRADE_ANNOYANCE_NONE:
estade1e949edc2016-02-08 21:19:4646 return gfx::Image();
[email protected]2735ec62013-04-23 05:37:1147 case UPGRADE_ANNOYANCE_LOW:
estade1e949edc2016-02-08 21:19:4648 color = gfx::kGoogleGreen700;
49 break;
[email protected]2735ec62013-04-23 05:37:1150 case UPGRADE_ANNOYANCE_ELEVATED:
estade1e949edc2016-02-08 21:19:4651 color = gfx::kGoogleYellow700;
52 break;
[email protected]2735ec62013-04-23 05:37:1153 case UPGRADE_ANNOYANCE_HIGH:
[email protected]2735ec62013-04-23 05:37:1154 case UPGRADE_ANNOYANCE_SEVERE:
[email protected]2735ec62013-04-23 05:37:1155 case UPGRADE_ANNOYANCE_CRITICAL:
estade1e949edc2016-02-08 21:19:4656 color = gfx::kGoogleRed700;
57 break;
[email protected]2735ec62013-04-23 05:37:1158 }
estade1e949edc2016-02-08 21:19:4659 DCHECK_NE(gfx::kPlaceholderColor, color);
60
61 return gfx::Image(
62 gfx::CreateVectorIcon(gfx::VectorIconId::UPGRADE_MENU_ITEM, 16, color));
[email protected]21788142011-05-03 10:38:3563}
64
[email protected]8fcec3c72010-06-03 00:17:2265UpgradeDetector::UpgradeDetector()
[email protected]2a54f2d2013-02-15 05:54:3666 : upgrade_available_(UPGRADE_AVAILABLE_NONE),
[email protected]dd46dd52014-08-07 21:57:1467 best_effort_experiment_updates_available_(false),
68 critical_experiment_updates_available_(false),
[email protected]f1c76b9f2011-10-13 13:43:4469 critical_update_acknowledged_(false),
ygorshenin9903cf42014-10-07 06:47:1170 is_factory_reset_required_(false),
[email protected]f1c76b9f2011-10-13 13:43:4471 upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE),
[email protected]8fcec3c72010-06-03 00:17:2272 notify_upgrade_(false) {
[email protected]b1b73942010-05-26 20:11:5473}
74
75UpgradeDetector::~UpgradeDetector() {
76}
77
[email protected]ceff8402011-06-12 23:27:1278void UpgradeDetector::NotifyUpgradeRecommended() {
[email protected]b1b73942010-05-26 20:11:5479 notify_upgrade_ = true;
80
[email protected]dd46dd52014-08-07 21:57:1481 TriggerNotification(chrome::NOTIFICATION_UPGRADE_RECOMMENDED);
82 if (upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL) {
83 TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL);
84 } else if (upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL_NO_AU) {
85 TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL_NO_AU);
86 } else if (upgrade_available_ == UPGRADE_AVAILABLE_CRITICAL ||
87 critical_experiment_updates_available_) {
88 TriggerCriticalUpdate();
[email protected]f1c76b9f2011-10-13 13:43:4489 }
90}
91
[email protected]dd46dd52014-08-07 21:57:1492void UpgradeDetector::TriggerCriticalUpdate() {
93 const base::TimeDelta idle_timer = UseTestingIntervals() ?
94 base::TimeDelta::FromSeconds(kIdleRepeatingTimerWait) :
95 base::TimeDelta::FromMinutes(kIdleRepeatingTimerWait);
96 idle_check_timer_.Start(FROM_HERE, idle_timer, this,
97 &UpgradeDetector::CheckIdle);
98}
99
[email protected]f1c76b9f2011-10-13 13:43:44100void UpgradeDetector::CheckIdle() {
101 // CalculateIdleState expects an interval in seconds.
102 int idle_time_allowed = UseTestingIntervals() ? kIdleAmount :
103 kIdleAmount * 60 * 60;
104
105 CalculateIdleState(
106 idle_time_allowed, base::Bind(&UpgradeDetector::IdleCallback,
107 base::Unretained(this)));
108}
109
[email protected]dd46dd52014-08-07 21:57:14110void UpgradeDetector::TriggerNotification(chrome::NotificationType type) {
111 content::NotificationService::current()->Notify(
112 type,
113 content::Source<UpgradeDetector>(this),
114 content::NotificationService::NoDetails());
115}
116
derat49b2fd52015-01-16 15:32:12117void UpgradeDetector::IdleCallback(ui::IdleState state) {
[email protected]914ace62012-10-22 17:16:02118 // Don't proceed while an incognito window is open. The timer will still
119 // keep firing, so this function will get a chance to re-evaluate this.
120 if (chrome::IsOffTheRecordSessionActive())
121 return;
122
[email protected]f1c76b9f2011-10-13 13:43:44123 switch (state) {
derat49b2fd52015-01-16 15:32:12124 case ui::IDLE_STATE_LOCKED:
[email protected]f1c76b9f2011-10-13 13:43:44125 // Computer is locked, auto-restart.
126 idle_check_timer_.Stop();
[email protected]0c98ab652013-02-18 00:39:37127 chrome::AttemptRestart();
[email protected]f1c76b9f2011-10-13 13:43:44128 break;
derat49b2fd52015-01-16 15:32:12129 case ui::IDLE_STATE_IDLE:
[email protected]f1c76b9f2011-10-13 13:43:44130 // Computer has been idle for long enough, show warning.
131 idle_check_timer_.Stop();
[email protected]dd46dd52014-08-07 21:57:14132 TriggerNotification(chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED);
[email protected]f1c76b9f2011-10-13 13:43:44133 break;
derat49b2fd52015-01-16 15:32:12134 case ui::IDLE_STATE_ACTIVE:
135 case ui::IDLE_STATE_UNKNOWN:
[email protected]f1c76b9f2011-10-13 13:43:44136 break;
137 default:
138 NOTREACHED(); // Need to add any new value above (either providing
139 // automatic restart or show notification to user).
140 break;
141 }
[email protected]b1b73942010-05-26 20:11:54142}