[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] | fdf40f3e | 2013-07-11 23:55:46 | [diff] [blame] | 9 | #include "chrome/browser/chrome_notification_types.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] | 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" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 14 | #include "components/prefs/pref_registry_simple.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. |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 29 | const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess(); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 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) { |
[email protected] | d50dd9d | 2014-03-28 21:46:24 | [diff] [blame] | 36 | registry->RegisterBooleanPref(prefs::kAttemptedToEnableAutoupdate, false); |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 37 | } |
| 38 | |
[email protected] | 767e1fa6 | 2014-07-22 00:53:43 | [diff] [blame] | 39 | int UpgradeDetector::GetIconResourceID() { |
[email protected] | 2735ec6 | 2013-04-23 05:37:11 | [diff] [blame] | 40 | switch (upgrade_notification_stage_) { |
| 41 | case UPGRADE_ANNOYANCE_NONE: |
| 42 | return 0; |
| 43 | case UPGRADE_ANNOYANCE_LOW: |
| 44 | return IDR_UPDATE_MENU_SEVERITY_LOW; |
| 45 | case UPGRADE_ANNOYANCE_ELEVATED: |
| 46 | return IDR_UPDATE_MENU_SEVERITY_MEDIUM; |
| 47 | case UPGRADE_ANNOYANCE_HIGH: |
| 48 | return IDR_UPDATE_MENU_SEVERITY_HIGH; |
| 49 | case UPGRADE_ANNOYANCE_SEVERE: |
| 50 | return IDR_UPDATE_MENU_SEVERITY_HIGH; |
| 51 | case UPGRADE_ANNOYANCE_CRITICAL: |
| 52 | return IDR_UPDATE_MENU_SEVERITY_HIGH; |
| 53 | } |
| 54 | NOTREACHED(); |
| 55 | return 0; |
[email protected] | 2178814 | 2011-05-03 10:38:35 | [diff] [blame] | 56 | } |
| 57 | |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 58 | UpgradeDetector::UpgradeDetector() |
[email protected] | 2a54f2d | 2013-02-15 05:54:36 | [diff] [blame] | 59 | : upgrade_available_(UPGRADE_AVAILABLE_NONE), |
[email protected] | dd46dd5 | 2014-08-07 21:57:14 | [diff] [blame] | 60 | best_effort_experiment_updates_available_(false), |
| 61 | critical_experiment_updates_available_(false), |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 62 | critical_update_acknowledged_(false), |
ygorshenin | 9903cf4 | 2014-10-07 06:47:11 | [diff] [blame] | 63 | is_factory_reset_required_(false), |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 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::NotifyUpgradeRecommended() { |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 72 | notify_upgrade_ = true; |
| 73 | |
[email protected] | dd46dd5 | 2014-08-07 21:57:14 | [diff] [blame] | 74 | TriggerNotification(chrome::NOTIFICATION_UPGRADE_RECOMMENDED); |
| 75 | if (upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL) { |
| 76 | TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL); |
| 77 | } else if (upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL_NO_AU) { |
| 78 | TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL_NO_AU); |
| 79 | } else if (upgrade_available_ == UPGRADE_AVAILABLE_CRITICAL || |
| 80 | critical_experiment_updates_available_) { |
| 81 | TriggerCriticalUpdate(); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
[email protected] | dd46dd5 | 2014-08-07 21:57:14 | [diff] [blame] | 85 | void UpgradeDetector::TriggerCriticalUpdate() { |
| 86 | const base::TimeDelta idle_timer = UseTestingIntervals() ? |
| 87 | base::TimeDelta::FromSeconds(kIdleRepeatingTimerWait) : |
| 88 | base::TimeDelta::FromMinutes(kIdleRepeatingTimerWait); |
| 89 | idle_check_timer_.Start(FROM_HERE, idle_timer, this, |
| 90 | &UpgradeDetector::CheckIdle); |
| 91 | } |
| 92 | |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 93 | void UpgradeDetector::CheckIdle() { |
| 94 | // CalculateIdleState expects an interval in seconds. |
| 95 | int idle_time_allowed = UseTestingIntervals() ? kIdleAmount : |
| 96 | kIdleAmount * 60 * 60; |
| 97 | |
| 98 | CalculateIdleState( |
| 99 | idle_time_allowed, base::Bind(&UpgradeDetector::IdleCallback, |
| 100 | base::Unretained(this))); |
| 101 | } |
| 102 | |
[email protected] | dd46dd5 | 2014-08-07 21:57:14 | [diff] [blame] | 103 | void UpgradeDetector::TriggerNotification(chrome::NotificationType type) { |
| 104 | content::NotificationService::current()->Notify( |
| 105 | type, |
| 106 | content::Source<UpgradeDetector>(this), |
| 107 | content::NotificationService::NoDetails()); |
| 108 | } |
| 109 | |
derat | 49b2fd5 | 2015-01-16 15:32:12 | [diff] [blame] | 110 | void UpgradeDetector::IdleCallback(ui::IdleState state) { |
[email protected] | 914ace6 | 2012-10-22 17:16:02 | [diff] [blame] | 111 | // Don't proceed while an incognito window is open. The timer will still |
| 112 | // keep firing, so this function will get a chance to re-evaluate this. |
| 113 | if (chrome::IsOffTheRecordSessionActive()) |
| 114 | return; |
| 115 | |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 116 | switch (state) { |
derat | 49b2fd5 | 2015-01-16 15:32:12 | [diff] [blame] | 117 | case ui::IDLE_STATE_LOCKED: |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 118 | // Computer is locked, auto-restart. |
| 119 | idle_check_timer_.Stop(); |
[email protected] | 0c98ab65 | 2013-02-18 00:39:37 | [diff] [blame] | 120 | chrome::AttemptRestart(); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 121 | break; |
derat | 49b2fd5 | 2015-01-16 15:32:12 | [diff] [blame] | 122 | case ui::IDLE_STATE_IDLE: |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 123 | // Computer has been idle for long enough, show warning. |
| 124 | idle_check_timer_.Stop(); |
[email protected] | dd46dd5 | 2014-08-07 21:57:14 | [diff] [blame] | 125 | TriggerNotification(chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 126 | break; |
derat | 49b2fd5 | 2015-01-16 15:32:12 | [diff] [blame] | 127 | case ui::IDLE_STATE_ACTIVE: |
| 128 | case ui::IDLE_STATE_UNKNOWN: |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 129 | break; |
| 130 | default: |
| 131 | NOTREACHED(); // Need to add any new value above (either providing |
| 132 | // automatic restart or show notification to user). |
| 133 | break; |
| 134 | } |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 135 | } |