[email protected] | 7f070d4 | 2011-03-09 20:25:32 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 2 | // 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] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame^] | 7 | #include "base/bind.h" |
| 8 | #include "base/command_line.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 9 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame^] | 10 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 11 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame^] | 12 | #include "chrome/common/chrome_switches.h" |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 13 | #include "chrome/common/pref_names.h" |
[email protected] | 7f070d4 | 2011-03-09 20:25:32 | [diff] [blame] | 14 | #include "content/common/notification_service.h" |
[email protected] | 2178814 | 2011-05-03 10:38:35 | [diff] [blame] | 15 | #include "grit/theme_resources.h" |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 16 | |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame^] | 17 | // How long to wait between checks for whether the user has been idle. |
| 18 | static 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. |
| 22 | static const int kIdleAmount = 2; // Hours (or seconds, if testing). |
| 23 | |
| 24 | bool 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] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 33 | // static |
| 34 | void UpgradeDetector::RegisterPrefs(PrefService* prefs) { |
| 35 | prefs->RegisterBooleanPref(prefs::kRestartLastSessionOnShutdown, false); |
| 36 | } |
| 37 | |
[email protected] | 2178814 | 2011-05-03 10:38:35 | [diff] [blame] | 38 | int UpgradeDetector::GetIconResourceID(UpgradeNotificationIconType type) { |
| 39 | bool badge = type == UPGRADE_ICON_TYPE_BADGE; |
| 40 | switch (upgrade_notification_stage_) { |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame^] | 41 | 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] | 2178814 | 2011-05-03 10:38:35 | [diff] [blame] | 48 | 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] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 61 | UpgradeDetector::UpgradeDetector() |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame^] | 62 | : is_critical_upgrade_(false), |
| 63 | critical_update_acknowledged_(false), |
| 64 | upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE), |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 65 | notify_upgrade_(false) { |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | UpgradeDetector::~UpgradeDetector() { |
| 69 | } |
| 70 | |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 71 | void UpgradeDetector::NotifyUpgradeDetected() { |
[email protected] | 3bed26a | 2011-04-14 16:05:04 | [diff] [blame] | 72 | upgrade_detected_time_ = base::Time::Now(); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame^] | 73 | critical_update_acknowledged_ = false; |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 76 | void UpgradeDetector::NotifyUpgradeRecommended() { |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 77 | notify_upgrade_ = true; |
| 78 | |
| 79 | NotificationService::current()->Notify( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 80 | chrome::NOTIFICATION_UPGRADE_RECOMMENDED, |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 81 | Source<UpgradeDetector>(this), |
| 82 | NotificationService::NoDetails()); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame^] | 83 | |
| 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 | |
| 94 | void 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 | |
| 104 | void 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] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 127 | } |