blob: f6b68bc8b875f1490545012cbdf62cd79eacc408 [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]3853a4c2013-02-11 17:15:579#include "base/prefs/pref_registry_simple.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"
[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.
29 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
30 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) {
36 registry->RegisterBooleanPref(prefs::kRestartLastSessionOnShutdown, false);
37 registry->RegisterBooleanPref(prefs::kWasRestarted, false);
[email protected]8fcec3c72010-06-03 00:17:2238}
39
[email protected]21788142011-05-03 10:38:3540int UpgradeDetector::GetIconResourceID(UpgradeNotificationIconType type) {
[email protected]2735ec62013-04-23 05:37:1141 if (type == UPGRADE_ICON_TYPE_BADGE) {
42 // Badges do not exist on Views and Mac OS X.
43#if !defined(TOOLKIT_VIEWS) && !defined(OS_MACOSX)
44 switch (upgrade_notification_stage_) {
45 case UPGRADE_ANNOYANCE_NONE:
46 return 0;
47 case UPGRADE_ANNOYANCE_LOW:
48 return IDR_UPDATE_BADGE;
49 case UPGRADE_ANNOYANCE_ELEVATED:
50 return IDR_UPDATE_BADGE2;
51 case UPGRADE_ANNOYANCE_HIGH:
52 return IDR_UPDATE_BADGE3;
53 case UPGRADE_ANNOYANCE_SEVERE:
54 return IDR_UPDATE_BADGE3;
55 case UPGRADE_ANNOYANCE_CRITICAL:
56 return IDR_UPDATE_BADGE3;
57 }
58#endif
59 NOTREACHED();
60 return 0;
[email protected]21788142011-05-03 10:38:3561 }
[email protected]2735ec62013-04-23 05:37:1162
63 switch (upgrade_notification_stage_) {
64 case UPGRADE_ANNOYANCE_NONE:
65 return 0;
66 case UPGRADE_ANNOYANCE_LOW:
67 return IDR_UPDATE_MENU_SEVERITY_LOW;
68 case UPGRADE_ANNOYANCE_ELEVATED:
69 return IDR_UPDATE_MENU_SEVERITY_MEDIUM;
70 case UPGRADE_ANNOYANCE_HIGH:
71 return IDR_UPDATE_MENU_SEVERITY_HIGH;
72 case UPGRADE_ANNOYANCE_SEVERE:
73 return IDR_UPDATE_MENU_SEVERITY_HIGH;
74 case UPGRADE_ANNOYANCE_CRITICAL:
75 return IDR_UPDATE_MENU_SEVERITY_HIGH;
76 }
77 NOTREACHED();
78 return 0;
[email protected]21788142011-05-03 10:38:3579}
80
[email protected]8fcec3c72010-06-03 00:17:2281UpgradeDetector::UpgradeDetector()
[email protected]2a54f2d2013-02-15 05:54:3682 : upgrade_available_(UPGRADE_AVAILABLE_NONE),
[email protected]f1c76b9f2011-10-13 13:43:4483 critical_update_acknowledged_(false),
84 upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE),
[email protected]8fcec3c72010-06-03 00:17:2285 notify_upgrade_(false) {
[email protected]b1b73942010-05-26 20:11:5486}
87
88UpgradeDetector::~UpgradeDetector() {
89}
90
[email protected]ceff8402011-06-12 23:27:1291void UpgradeDetector::NotifyUpgradeDetected() {
[email protected]3bed26a2011-04-14 16:05:0492 upgrade_detected_time_ = base::Time::Now();
[email protected]f1c76b9f2011-10-13 13:43:4493 critical_update_acknowledged_ = false;
[email protected]b1b73942010-05-26 20:11:5494}
95
[email protected]ceff8402011-06-12 23:27:1296void UpgradeDetector::NotifyUpgradeRecommended() {
[email protected]b1b73942010-05-26 20:11:5497 notify_upgrade_ = true;
98
[email protected]ad50def52011-10-19 23:17:0799 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:27100 chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
[email protected]6c2381d2011-10-19 02:52:53101 content::Source<UpgradeDetector>(this),
[email protected]ad50def52011-10-19 23:17:07102 content::NotificationService::NoDetails());
[email protected]f1c76b9f2011-10-13 13:43:44103
[email protected]2a54f2d2013-02-15 05:54:36104 switch (upgrade_available_) {
105 case UPGRADE_NEEDED_OUTDATED_INSTALL: {
106 content::NotificationService::current()->Notify(
107 chrome::NOTIFICATION_OUTDATED_INSTALL,
108 content::Source<UpgradeDetector>(this),
109 content::NotificationService::NoDetails());
110 break;
111 }
112 case UPGRADE_AVAILABLE_CRITICAL: {
113 int idle_timer = UseTestingIntervals() ?
114 kIdleRepeatingTimerWait :
115 kIdleRepeatingTimerWait * 60; // To minutes.
116 idle_check_timer_.Start(FROM_HERE,
117 base::TimeDelta::FromSeconds(idle_timer),
118 this, &UpgradeDetector::CheckIdle);
119 break;
120 }
121 default:
122 break;
[email protected]f1c76b9f2011-10-13 13:43:44123 }
124}
125
126void UpgradeDetector::CheckIdle() {
127 // CalculateIdleState expects an interval in seconds.
128 int idle_time_allowed = UseTestingIntervals() ? kIdleAmount :
129 kIdleAmount * 60 * 60;
130
131 CalculateIdleState(
132 idle_time_allowed, base::Bind(&UpgradeDetector::IdleCallback,
133 base::Unretained(this)));
134}
135
136void UpgradeDetector::IdleCallback(IdleState state) {
[email protected]914ace62012-10-22 17:16:02137 // Don't proceed while an incognito window is open. The timer will still
138 // keep firing, so this function will get a chance to re-evaluate this.
139 if (chrome::IsOffTheRecordSessionActive())
140 return;
141
[email protected]f1c76b9f2011-10-13 13:43:44142 switch (state) {
143 case IDLE_STATE_LOCKED:
144 // Computer is locked, auto-restart.
145 idle_check_timer_.Stop();
[email protected]0c98ab652013-02-18 00:39:37146 chrome::AttemptRestart();
[email protected]f1c76b9f2011-10-13 13:43:44147 break;
148 case IDLE_STATE_IDLE:
149 // Computer has been idle for long enough, show warning.
150 idle_check_timer_.Stop();
[email protected]ad50def52011-10-19 23:17:07151 content::NotificationService::current()->Notify(
[email protected]f1c76b9f2011-10-13 13:43:44152 chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED,
[email protected]6c2381d2011-10-19 02:52:53153 content::Source<UpgradeDetector>(this),
[email protected]ad50def52011-10-19 23:17:07154 content::NotificationService::NoDetails());
[email protected]f1c76b9f2011-10-13 13:43:44155 break;
156 case IDLE_STATE_ACTIVE:
157 case IDLE_STATE_UNKNOWN:
158 break;
159 default:
160 NOTREACHED(); // Need to add any new value above (either providing
161 // automatic restart or show notification to user).
162 break;
163 }
[email protected]b1b73942010-05-26 20:11:54164}