[email protected] | d7a8ef6 | 2012-06-22 16:28:46 | [diff] [blame^] | 1 | // Copyright (c) 2012 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] | 2e6389f | 2012-05-18 19:41:25 | [diff] [blame] | 10 | #include "chrome/browser/lifetime/application_lifetime.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] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 14 | #include "content/public/browser/notification_service.h" |
[email protected] | d7a8ef6 | 2012-06-22 16:28:46 | [diff] [blame^] | 15 | #include "grit/theme_resources_standard.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); |
[email protected] | 88c8878 | 2011-12-05 16:29:58 | [diff] [blame] | 36 | prefs->RegisterBooleanPref(prefs::kWasRestarted, false); |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 37 | } |
| 38 | |
[email protected] | 2178814 | 2011-05-03 10:38:35 | [diff] [blame] | 39 | int UpgradeDetector::GetIconResourceID(UpgradeNotificationIconType type) { |
| 40 | bool badge = type == UPGRADE_ICON_TYPE_BADGE; |
| 41 | switch (upgrade_notification_stage_) { |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 42 | 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] | 2178814 | 2011-05-03 10:38:35 | [diff] [blame] | 49 | 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] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 62 | UpgradeDetector::UpgradeDetector() |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 63 | : is_critical_upgrade_(false), |
| 64 | critical_update_acknowledged_(false), |
| 65 | upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE), |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 66 | notify_upgrade_(false) { |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | UpgradeDetector::~UpgradeDetector() { |
| 70 | } |
| 71 | |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 72 | void UpgradeDetector::NotifyUpgradeDetected() { |
[email protected] | 3bed26a | 2011-04-14 16:05:04 | [diff] [blame] | 73 | upgrade_detected_time_ = base::Time::Now(); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 74 | critical_update_acknowledged_ = false; |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 77 | void UpgradeDetector::NotifyUpgradeRecommended() { |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 78 | notify_upgrade_ = true; |
| 79 | |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 80 | content::NotificationService::current()->Notify( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 81 | chrome::NOTIFICATION_UPGRADE_RECOMMENDED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 82 | content::Source<UpgradeDetector>(this), |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 83 | content::NotificationService::NoDetails()); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 84 | |
| 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 | |
| 95 | void 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 | |
| 105 | void 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] | 2e6389f | 2012-05-18 19:41:25 | [diff] [blame] | 110 | browser::AttemptRestart(); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 111 | break; |
| 112 | case IDLE_STATE_IDLE: |
| 113 | // Computer has been idle for long enough, show warning. |
| 114 | idle_check_timer_.Stop(); |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 115 | content::NotificationService::current()->Notify( |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 116 | chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 117 | content::Source<UpgradeDetector>(this), |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 118 | content::NotificationService::NoDetails()); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 119 | 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] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 128 | } |