blob: eb6dfa51c749b9483ba4117873694ab2a33ddc21 [file] [log] [blame]
[email protected]7f070d42011-03-09 20:25:321// Copyright (c) 2011 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]37858e52010-08-26 00:22:029#include "chrome/browser/prefs/pref_service.h"
[email protected]f1c76b9f2011-10-13 13:43:4410#include "chrome/browser/ui/browser_list.h"
[email protected]432115822011-07-10 15:52:2711#include "chrome/common/chrome_notification_types.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"
[email protected]7f070d42011-03-09 20:25:3214#include "content/common/notification_service.h"
[email protected]21788142011-05-03 10:38:3515#include "grit/theme_resources.h"
[email protected]8fcec3c72010-06-03 00:17:2216
[email protected]f1c76b9f2011-10-13 13:43:4417// How long to wait between checks for whether the user has been idle.
18static const int kIdleRepeatingTimerWait = 10; // Minutes (seconds if testing).
19
20// How much idle time (since last input even was detected) must have passed
21// until we notify that a critical update has occurred.
22static const int kIdleAmount = 2; // Hours (or seconds, if testing).
23
24bool UseTestingIntervals() {
25 // If a command line parameter specifying how long the upgrade check should
26 // be, we assume it is for testing and switch to using seconds instead of
27 // hours.
28 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
29 return !cmd_line.GetSwitchValueASCII(
30 switches::kCheckForUpdateIntervalSec).empty();
31}
32
[email protected]8fcec3c72010-06-03 00:17:2233// static
34void UpgradeDetector::RegisterPrefs(PrefService* prefs) {
35 prefs->RegisterBooleanPref(prefs::kRestartLastSessionOnShutdown, false);
36}
37
[email protected]21788142011-05-03 10:38:3538int UpgradeDetector::GetIconResourceID(UpgradeNotificationIconType type) {
39 bool badge = type == UPGRADE_ICON_TYPE_BADGE;
40 switch (upgrade_notification_stage_) {
[email protected]f1c76b9f2011-10-13 13:43:4441 case UPGRADE_ANNOYANCE_CRITICAL:
42 // The critical annoyance state, somewhat ironically, re-purposes the
43 // icon for the second highest severity state, since that state has the
44 // icon most closely resembling the one requested of this feature and the
45 // critical annoyance is never part of the sliding scale of severity
46 // anyway (always shown on its own).
47 return badge ? IDR_UPDATE_BADGE3 : IDR_UPDATE_MENU3;
[email protected]21788142011-05-03 10:38:3548 case UPGRADE_ANNOYANCE_SEVERE:
49 return badge ? IDR_UPDATE_BADGE4 : IDR_UPDATE_MENU4;
50 case UPGRADE_ANNOYANCE_HIGH:
51 return badge ? IDR_UPDATE_BADGE3 : IDR_UPDATE_MENU3;
52 case UPGRADE_ANNOYANCE_ELEVATED:
53 return badge ? IDR_UPDATE_BADGE2 : IDR_UPDATE_MENU2;
54 case UPGRADE_ANNOYANCE_LOW:
55 return badge ? IDR_UPDATE_BADGE : IDR_UPDATE_MENU;
56 default:
57 return 0;
58 }
59}
60
[email protected]8fcec3c72010-06-03 00:17:2261UpgradeDetector::UpgradeDetector()
[email protected]f1c76b9f2011-10-13 13:43:4462 : is_critical_upgrade_(false),
63 critical_update_acknowledged_(false),
64 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::NotifyUpgradeDetected() {
[email protected]3bed26a2011-04-14 16:05:0472 upgrade_detected_time_ = base::Time::Now();
[email protected]f1c76b9f2011-10-13 13:43:4473 critical_update_acknowledged_ = false;
[email protected]b1b73942010-05-26 20:11:5474}
75
[email protected]ceff8402011-06-12 23:27:1276void UpgradeDetector::NotifyUpgradeRecommended() {
[email protected]b1b73942010-05-26 20:11:5477 notify_upgrade_ = true;
78
79 NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:2780 chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
[email protected]b1b73942010-05-26 20:11:5481 Source<UpgradeDetector>(this),
82 NotificationService::NoDetails());
[email protected]f1c76b9f2011-10-13 13:43:4483
84 if (is_critical_upgrade_) {
85 int idle_timer = UseTestingIntervals() ?
86 kIdleRepeatingTimerWait :
87 kIdleRepeatingTimerWait * 60; // To minutes.
88 idle_check_timer_.Start(FROM_HERE,
89 base::TimeDelta::FromSeconds(idle_timer),
90 this, &UpgradeDetector::CheckIdle);
91 }
92}
93
94void UpgradeDetector::CheckIdle() {
95 // CalculateIdleState expects an interval in seconds.
96 int idle_time_allowed = UseTestingIntervals() ? kIdleAmount :
97 kIdleAmount * 60 * 60;
98
99 CalculateIdleState(
100 idle_time_allowed, base::Bind(&UpgradeDetector::IdleCallback,
101 base::Unretained(this)));
102}
103
104void UpgradeDetector::IdleCallback(IdleState state) {
105 switch (state) {
106 case IDLE_STATE_LOCKED:
107 // Computer is locked, auto-restart.
108 idle_check_timer_.Stop();
109 BrowserList::AttemptRestart();
110 break;
111 case IDLE_STATE_IDLE:
112 // Computer has been idle for long enough, show warning.
113 idle_check_timer_.Stop();
114 NotificationService::current()->Notify(
115 chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED,
116 Source<UpgradeDetector>(this),
117 NotificationService::NoDetails());
118 break;
119 case IDLE_STATE_ACTIVE:
120 case IDLE_STATE_UNKNOWN:
121 break;
122 default:
123 NOTREACHED(); // Need to add any new value above (either providing
124 // automatic restart or show notification to user).
125 break;
126 }
[email protected]b1b73942010-05-26 20:11:54127}