[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] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 9 | #include "base/prefs/pref_registry_simple.h" |
[email protected] | 2e6389f | 2012-05-18 19:41:25 | [diff] [blame] | 10 | #include "chrome/browser/lifetime/application_lifetime.h" |
[email protected] | 914ace6 | 2012-10-22 17:16:02 | [diff] [blame] | 11 | #include "chrome/browser/ui/browser_otr_state.h" |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 12 | #include "chrome/common/chrome_notification_types.h" |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 13 | #include "chrome/common/chrome_switches.h" |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 14 | #include "chrome/common/pref_names.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 15 | #include "content/public/browser/notification_service.h" |
[email protected] | 2a28133 | 2012-07-11 22:20:23 | [diff] [blame] | 16 | #include "grit/theme_resources.h" |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 17 | |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 18 | // How long to wait between checks for whether the user has been idle. |
| 19 | static 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. |
| 23 | static const int kIdleAmount = 2; // Hours (or seconds, if testing). |
| 24 | |
| 25 | bool 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] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 34 | // static |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 35 | void UpgradeDetector::RegisterPrefs(PrefRegistrySimple* registry) { |
| 36 | registry->RegisterBooleanPref(prefs::kRestartLastSessionOnShutdown, false); |
| 37 | registry->RegisterBooleanPref(prefs::kWasRestarted, false); |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 38 | } |
| 39 | |
[email protected] | 2178814 | 2011-05-03 10:38:35 | [diff] [blame] | 40 | int UpgradeDetector::GetIconResourceID(UpgradeNotificationIconType type) { |
| 41 | bool badge = type == UPGRADE_ICON_TYPE_BADGE; |
| 42 | switch (upgrade_notification_stage_) { |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 43 | 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] | 2178814 | 2011-05-03 10:38:35 | [diff] [blame] | 50 | 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] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 63 | UpgradeDetector::UpgradeDetector() |
[email protected] | 2a54f2d | 2013-02-15 05:54:36 | [diff] [blame] | 64 | : upgrade_available_(UPGRADE_AVAILABLE_NONE), |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 65 | critical_update_acknowledged_(false), |
| 66 | upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE), |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 67 | notify_upgrade_(false) { |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | UpgradeDetector::~UpgradeDetector() { |
| 71 | } |
| 72 | |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 73 | void UpgradeDetector::NotifyUpgradeDetected() { |
[email protected] | 3bed26a | 2011-04-14 16:05:04 | [diff] [blame] | 74 | upgrade_detected_time_ = base::Time::Now(); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 75 | critical_update_acknowledged_ = false; |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 76 | } |
| 77 | |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 78 | void UpgradeDetector::NotifyUpgradeRecommended() { |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 79 | notify_upgrade_ = true; |
| 80 | |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 81 | content::NotificationService::current()->Notify( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 82 | chrome::NOTIFICATION_UPGRADE_RECOMMENDED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 83 | content::Source<UpgradeDetector>(this), |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 84 | content::NotificationService::NoDetails()); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 85 | |
[email protected] | 2a54f2d | 2013-02-15 05:54:36 | [diff] [blame] | 86 | switch (upgrade_available_) { |
| 87 | case UPGRADE_NEEDED_OUTDATED_INSTALL: { |
| 88 | content::NotificationService::current()->Notify( |
| 89 | chrome::NOTIFICATION_OUTDATED_INSTALL, |
| 90 | content::Source<UpgradeDetector>(this), |
| 91 | content::NotificationService::NoDetails()); |
| 92 | break; |
| 93 | } |
| 94 | case UPGRADE_AVAILABLE_CRITICAL: { |
| 95 | int idle_timer = UseTestingIntervals() ? |
| 96 | kIdleRepeatingTimerWait : |
| 97 | kIdleRepeatingTimerWait * 60; // To minutes. |
| 98 | idle_check_timer_.Start(FROM_HERE, |
| 99 | base::TimeDelta::FromSeconds(idle_timer), |
| 100 | this, &UpgradeDetector::CheckIdle); |
| 101 | break; |
| 102 | } |
| 103 | default: |
| 104 | break; |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | void UpgradeDetector::CheckIdle() { |
| 109 | // CalculateIdleState expects an interval in seconds. |
| 110 | int idle_time_allowed = UseTestingIntervals() ? kIdleAmount : |
| 111 | kIdleAmount * 60 * 60; |
| 112 | |
| 113 | CalculateIdleState( |
| 114 | idle_time_allowed, base::Bind(&UpgradeDetector::IdleCallback, |
| 115 | base::Unretained(this))); |
| 116 | } |
| 117 | |
| 118 | void UpgradeDetector::IdleCallback(IdleState state) { |
[email protected] | 914ace6 | 2012-10-22 17:16:02 | [diff] [blame] | 119 | // Don't proceed while an incognito window is open. The timer will still |
| 120 | // keep firing, so this function will get a chance to re-evaluate this. |
| 121 | if (chrome::IsOffTheRecordSessionActive()) |
| 122 | return; |
| 123 | |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 124 | switch (state) { |
| 125 | case IDLE_STATE_LOCKED: |
| 126 | // Computer is locked, auto-restart. |
| 127 | idle_check_timer_.Stop(); |
[email protected] | 0c98ab65 | 2013-02-18 00:39:37 | [diff] [blame^] | 128 | chrome::AttemptRestart(); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 129 | break; |
| 130 | case IDLE_STATE_IDLE: |
| 131 | // Computer has been idle for long enough, show warning. |
| 132 | idle_check_timer_.Stop(); |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 133 | content::NotificationService::current()->Notify( |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 134 | chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 135 | content::Source<UpgradeDetector>(this), |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 136 | content::NotificationService::NoDetails()); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 137 | break; |
| 138 | case IDLE_STATE_ACTIVE: |
| 139 | case IDLE_STATE_UNKNOWN: |
| 140 | break; |
| 141 | default: |
| 142 | NOTREACHED(); // Need to add any new value above (either providing |
| 143 | // automatic restart or show notification to user). |
| 144 | break; |
| 145 | } |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 146 | } |