blob: 6f172f199d37793aa5f7aa1fba6d58af9a6c9a97 [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]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]432115822011-07-10 15:52:2712#include "chrome/common/chrome_notification_types.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) {
41 bool badge = type == UPGRADE_ICON_TYPE_BADGE;
42 switch (upgrade_notification_stage_) {
[email protected]f1c76b9f2011-10-13 13:43:4443 case UPGRADE_ANNOYANCE_CRITICAL:
44 // The critical annoyance state, somewhat ironically, re-purposes the
45 // icon for the second highest severity state, since that state has the
46 // icon most closely resembling the one requested of this feature and the
47 // critical annoyance is never part of the sliding scale of severity
48 // anyway (always shown on its own).
49 return badge ? IDR_UPDATE_BADGE3 : IDR_UPDATE_MENU3;
[email protected]21788142011-05-03 10:38:3550 case UPGRADE_ANNOYANCE_SEVERE:
51 return badge ? IDR_UPDATE_BADGE4 : IDR_UPDATE_MENU4;
52 case UPGRADE_ANNOYANCE_HIGH:
53 return badge ? IDR_UPDATE_BADGE3 : IDR_UPDATE_MENU3;
54 case UPGRADE_ANNOYANCE_ELEVATED:
55 return badge ? IDR_UPDATE_BADGE2 : IDR_UPDATE_MENU2;
56 case UPGRADE_ANNOYANCE_LOW:
57 return badge ? IDR_UPDATE_BADGE : IDR_UPDATE_MENU;
58 default:
59 return 0;
60 }
61}
62
[email protected]8fcec3c72010-06-03 00:17:2263UpgradeDetector::UpgradeDetector()
[email protected]f1c76b9f2011-10-13 13:43:4464 : is_critical_upgrade_(false),
65 critical_update_acknowledged_(false),
66 upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE),
[email protected]8fcec3c72010-06-03 00:17:2267 notify_upgrade_(false) {
[email protected]b1b73942010-05-26 20:11:5468}
69
70UpgradeDetector::~UpgradeDetector() {
71}
72
[email protected]ceff8402011-06-12 23:27:1273void UpgradeDetector::NotifyUpgradeDetected() {
[email protected]3bed26a2011-04-14 16:05:0474 upgrade_detected_time_ = base::Time::Now();
[email protected]f1c76b9f2011-10-13 13:43:4475 critical_update_acknowledged_ = false;
[email protected]b1b73942010-05-26 20:11:5476}
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]ad50def52011-10-19 23:17:0781 content::NotificationService::current()->Notify(
[email protected]432115822011-07-10 15:52:2782 chrome::NOTIFICATION_UPGRADE_RECOMMENDED,
[email protected]6c2381d2011-10-19 02:52:5383 content::Source<UpgradeDetector>(this),
[email protected]ad50def52011-10-19 23:17:0784 content::NotificationService::NoDetails());
[email protected]f1c76b9f2011-10-13 13:43:4485
86 if (is_critical_upgrade_) {
87 int idle_timer = UseTestingIntervals() ?
88 kIdleRepeatingTimerWait :
89 kIdleRepeatingTimerWait * 60; // To minutes.
90 idle_check_timer_.Start(FROM_HERE,
91 base::TimeDelta::FromSeconds(idle_timer),
92 this, &UpgradeDetector::CheckIdle);
93 }
94}
95
96void UpgradeDetector::CheckIdle() {
97 // CalculateIdleState expects an interval in seconds.
98 int idle_time_allowed = UseTestingIntervals() ? kIdleAmount :
99 kIdleAmount * 60 * 60;
100
101 CalculateIdleState(
102 idle_time_allowed, base::Bind(&UpgradeDetector::IdleCallback,
103 base::Unretained(this)));
104}
105
106void UpgradeDetector::IdleCallback(IdleState state) {
[email protected]914ace62012-10-22 17:16:02107 // Don't proceed while an incognito window is open. The timer will still
108 // keep firing, so this function will get a chance to re-evaluate this.
109 if (chrome::IsOffTheRecordSessionActive())
110 return;
111
[email protected]f1c76b9f2011-10-13 13:43:44112 switch (state) {
113 case IDLE_STATE_LOCKED:
114 // Computer is locked, auto-restart.
115 idle_check_timer_.Stop();
[email protected]2e6389f2012-05-18 19:41:25116 browser::AttemptRestart();
[email protected]f1c76b9f2011-10-13 13:43:44117 break;
118 case IDLE_STATE_IDLE:
119 // Computer has been idle for long enough, show warning.
120 idle_check_timer_.Stop();
[email protected]ad50def52011-10-19 23:17:07121 content::NotificationService::current()->Notify(
[email protected]f1c76b9f2011-10-13 13:43:44122 chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED,
[email protected]6c2381d2011-10-19 02:52:53123 content::Source<UpgradeDetector>(this),
[email protected]ad50def52011-10-19 23:17:07124 content::NotificationService::NoDetails());
[email protected]f1c76b9f2011-10-13 13:43:44125 break;
126 case IDLE_STATE_ACTIVE:
127 case IDLE_STATE_UNKNOWN:
128 break;
129 default:
130 NOTREACHED(); // Need to add any new value above (either providing
131 // automatic restart or show notification to user).
132 break;
133 }
[email protected]b1b73942010-05-26 20:11:54134}