blob: c3c498bfea0560e52ddb47bb0b64cb6444f67647 [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]37858e52010-08-26 00:22:029#include "chrome/browser/prefs/pref_service.h"
[email protected]2e6389f2012-05-18 19:41:2510#include "chrome/browser/lifetime/application_lifetime.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]ad50def52011-10-19 23:17:0714#include "content/public/browser/notification_service.h"
[email protected]d7a8ef62012-06-22 16:28:4615#include "grit/theme_resources_standard.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);
[email protected]88c88782011-12-05 16:29:5836 prefs->RegisterBooleanPref(prefs::kWasRestarted, false);
[email protected]8fcec3c72010-06-03 00:17:2237}
38
[email protected]21788142011-05-03 10:38:3539int UpgradeDetector::GetIconResourceID(UpgradeNotificationIconType type) {
40 bool badge = type == UPGRADE_ICON_TYPE_BADGE;
41 switch (upgrade_notification_stage_) {
[email protected]f1c76b9f2011-10-13 13:43:4442 case UPGRADE_ANNOYANCE_CRITICAL:
43 // The critical annoyance state, somewhat ironically, re-purposes the
44 // icon for the second highest severity state, since that state has the
45 // icon most closely resembling the one requested of this feature and the
46 // critical annoyance is never part of the sliding scale of severity
47 // anyway (always shown on its own).
48 return badge ? IDR_UPDATE_BADGE3 : IDR_UPDATE_MENU3;
[email protected]21788142011-05-03 10:38:3549 case UPGRADE_ANNOYANCE_SEVERE:
50 return badge ? IDR_UPDATE_BADGE4 : IDR_UPDATE_MENU4;
51 case UPGRADE_ANNOYANCE_HIGH:
52 return badge ? IDR_UPDATE_BADGE3 : IDR_UPDATE_MENU3;
53 case UPGRADE_ANNOYANCE_ELEVATED:
54 return badge ? IDR_UPDATE_BADGE2 : IDR_UPDATE_MENU2;
55 case UPGRADE_ANNOYANCE_LOW:
56 return badge ? IDR_UPDATE_BADGE : IDR_UPDATE_MENU;
57 default:
58 return 0;
59 }
60}
61
[email protected]8fcec3c72010-06-03 00:17:2262UpgradeDetector::UpgradeDetector()
[email protected]f1c76b9f2011-10-13 13:43:4463 : is_critical_upgrade_(false),
64 critical_update_acknowledged_(false),
65 upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE),
[email protected]8fcec3c72010-06-03 00:17:2266 notify_upgrade_(false) {
[email protected]b1b73942010-05-26 20:11:5467}
68
69UpgradeDetector::~UpgradeDetector() {
70}
71
[email protected]ceff8402011-06-12 23:27:1272void UpgradeDetector::NotifyUpgradeDetected() {
[email protected]3bed26a2011-04-14 16:05:0473 upgrade_detected_time_ = base::Time::Now();
[email protected]f1c76b9f2011-10-13 13:43:4474 critical_update_acknowledged_ = false;
[email protected]b1b73942010-05-26 20:11:5475}
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]ad50def52011-10-19 23:17:0780 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:2781 chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
[email protected]6c2381d2011-10-19 02:52:5382 content::Source<UpgradeDetector>(this),
[email protected]ad50def52011-10-19 23:17:0783 content::NotificationService::NoDetails());
[email protected]f1c76b9f2011-10-13 13:43:4484
85 if (is_critical_upgrade_) {
86 int idle_timer = UseTestingIntervals() ?
87 kIdleRepeatingTimerWait :
88 kIdleRepeatingTimerWait * 60; // To minutes.
89 idle_check_timer_.Start(FROM_HERE,
90 base::TimeDelta::FromSeconds(idle_timer),
91 this, &UpgradeDetector::CheckIdle);
92 }
93}
94
95void UpgradeDetector::CheckIdle() {
96 // CalculateIdleState expects an interval in seconds.
97 int idle_time_allowed = UseTestingIntervals() ? kIdleAmount :
98 kIdleAmount * 60 * 60;
99
100 CalculateIdleState(
101 idle_time_allowed, base::Bind(&UpgradeDetector::IdleCallback,
102 base::Unretained(this)));
103}
104
105void UpgradeDetector::IdleCallback(IdleState state) {
106 switch (state) {
107 case IDLE_STATE_LOCKED:
108 // Computer is locked, auto-restart.
109 idle_check_timer_.Stop();
[email protected]2e6389f2012-05-18 19:41:25110 browser::AttemptRestart();
[email protected]f1c76b9f2011-10-13 13:43:44111 break;
112 case IDLE_STATE_IDLE:
113 // Computer has been idle for long enough, show warning.
114 idle_check_timer_.Stop();
[email protected]ad50def52011-10-19 23:17:07115 content::NotificationService::current()->Notify(
[email protected]f1c76b9f2011-10-13 13:43:44116 chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED,
[email protected]6c2381d2011-10-19 02:52:53117 content::Source<UpgradeDetector>(this),
[email protected]ad50def52011-10-19 23:17:07118 content::NotificationService::NoDetails());
[email protected]f1c76b9f2011-10-13 13:43:44119 break;
120 case IDLE_STATE_ACTIVE:
121 case IDLE_STATE_UNKNOWN:
122 break;
123 default:
124 NOTREACHED(); // Need to add any new value above (either providing
125 // automatic restart or show notification to user).
126 break;
127 }
[email protected]b1b73942010-05-26 20:11:54128}