[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" |
estade | 01876af8 | 2017-01-09 21:35:43 | [diff] [blame] | 9 | #include "chrome/app/vector_icons/vector_icons.h" |
[email protected] | fdf40f3e | 2013-07-11 23:55:46 | [diff] [blame] | 10 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 2e6389f | 2012-05-18 19:41:25 | [diff] [blame] | 11 | #include "chrome/browser/lifetime/application_lifetime.h" |
[email protected] | 914ace6 | 2012-10-22 17:16:02 | [diff] [blame] | 12 | #include "chrome/browser/ui/browser_otr_state.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" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 15 | #include "components/prefs/pref_registry_simple.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 16 | #include "content/public/browser/notification_service.h" |
estade | 1e949edc | 2016-02-08 21:19:46 | [diff] [blame] | 17 | #include "ui/gfx/color_palette.h" |
| 18 | #include "ui/gfx/paint_vector_icon.h" |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 19 | |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 20 | // How long to wait between checks for whether the user has been idle. |
| 21 | static const int kIdleRepeatingTimerWait = 10; // Minutes (seconds if testing). |
| 22 | |
| 23 | // How much idle time (since last input even was detected) must have passed |
| 24 | // until we notify that a critical update has occurred. |
| 25 | static const int kIdleAmount = 2; // Hours (or seconds, if testing). |
| 26 | |
| 27 | bool UseTestingIntervals() { |
| 28 | // If a command line parameter specifying how long the upgrade check should |
| 29 | // be, we assume it is for testing and switch to using seconds instead of |
| 30 | // hours. |
avi | 556c0502 | 2014-12-22 23:31:43 | [diff] [blame] | 31 | const base::CommandLine& cmd_line = *base::CommandLine::ForCurrentProcess(); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 32 | return !cmd_line.GetSwitchValueASCII( |
| 33 | switches::kCheckForUpdateIntervalSec).empty(); |
| 34 | } |
| 35 | |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 36 | // static |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 37 | void UpgradeDetector::RegisterPrefs(PrefRegistrySimple* registry) { |
[email protected] | d50dd9d | 2014-03-28 21:46:24 | [diff] [blame] | 38 | registry->RegisterBooleanPref(prefs::kAttemptedToEnableAutoupdate, false); |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 39 | } |
| 40 | |
estade | 1e949edc | 2016-02-08 21:19:46 | [diff] [blame] | 41 | gfx::Image UpgradeDetector::GetIcon() { |
| 42 | SkColor color = gfx::kPlaceholderColor; |
[email protected] | 2735ec6 | 2013-04-23 05:37:11 | [diff] [blame] | 43 | switch (upgrade_notification_stage_) { |
| 44 | case UPGRADE_ANNOYANCE_NONE: |
estade | 1e949edc | 2016-02-08 21:19:46 | [diff] [blame] | 45 | return gfx::Image(); |
[email protected] | 2735ec6 | 2013-04-23 05:37:11 | [diff] [blame] | 46 | case UPGRADE_ANNOYANCE_LOW: |
estade | 1e949edc | 2016-02-08 21:19:46 | [diff] [blame] | 47 | color = gfx::kGoogleGreen700; |
| 48 | break; |
[email protected] | 2735ec6 | 2013-04-23 05:37:11 | [diff] [blame] | 49 | case UPGRADE_ANNOYANCE_ELEVATED: |
estade | 1e949edc | 2016-02-08 21:19:46 | [diff] [blame] | 50 | color = gfx::kGoogleYellow700; |
| 51 | break; |
[email protected] | 2735ec6 | 2013-04-23 05:37:11 | [diff] [blame] | 52 | case UPGRADE_ANNOYANCE_HIGH: |
[email protected] | 2735ec6 | 2013-04-23 05:37:11 | [diff] [blame] | 53 | case UPGRADE_ANNOYANCE_SEVERE: |
[email protected] | 2735ec6 | 2013-04-23 05:37:11 | [diff] [blame] | 54 | case UPGRADE_ANNOYANCE_CRITICAL: |
estade | 1e949edc | 2016-02-08 21:19:46 | [diff] [blame] | 55 | color = gfx::kGoogleRed700; |
| 56 | break; |
[email protected] | 2735ec6 | 2013-04-23 05:37:11 | [diff] [blame] | 57 | } |
estade | 1e949edc | 2016-02-08 21:19:46 | [diff] [blame] | 58 | DCHECK_NE(gfx::kPlaceholderColor, color); |
| 59 | |
estade | 01876af8 | 2017-01-09 21:35:43 | [diff] [blame] | 60 | return gfx::Image(gfx::CreateVectorIcon(kBrowserToolsUpdateIcon, color)); |
[email protected] | 2178814 | 2011-05-03 10:38:35 | [diff] [blame] | 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] | dd46dd5 | 2014-08-07 21:57:14 | [diff] [blame] | 65 | best_effort_experiment_updates_available_(false), |
| 66 | critical_experiment_updates_available_(false), |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 67 | critical_update_acknowledged_(false), |
ygorshenin | 9903cf4 | 2014-10-07 06:47:11 | [diff] [blame] | 68 | is_factory_reset_required_(false), |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 69 | upgrade_notification_stage_(UPGRADE_ANNOYANCE_NONE), |
[email protected] | 8fcec3c7 | 2010-06-03 00:17:22 | [diff] [blame] | 70 | notify_upgrade_(false) { |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | UpgradeDetector::~UpgradeDetector() { |
| 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 | |
[email protected] | dd46dd5 | 2014-08-07 21:57:14 | [diff] [blame] | 79 | TriggerNotification(chrome::NOTIFICATION_UPGRADE_RECOMMENDED); |
| 80 | if (upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL) { |
| 81 | TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL); |
| 82 | } else if (upgrade_available_ == UPGRADE_NEEDED_OUTDATED_INSTALL_NO_AU) { |
| 83 | TriggerNotification(chrome::NOTIFICATION_OUTDATED_INSTALL_NO_AU); |
| 84 | } else if (upgrade_available_ == UPGRADE_AVAILABLE_CRITICAL || |
| 85 | critical_experiment_updates_available_) { |
| 86 | TriggerCriticalUpdate(); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
[email protected] | dd46dd5 | 2014-08-07 21:57:14 | [diff] [blame] | 90 | void UpgradeDetector::TriggerCriticalUpdate() { |
| 91 | const base::TimeDelta idle_timer = UseTestingIntervals() ? |
| 92 | base::TimeDelta::FromSeconds(kIdleRepeatingTimerWait) : |
| 93 | base::TimeDelta::FromMinutes(kIdleRepeatingTimerWait); |
| 94 | idle_check_timer_.Start(FROM_HERE, idle_timer, this, |
| 95 | &UpgradeDetector::CheckIdle); |
| 96 | } |
| 97 | |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 98 | void UpgradeDetector::CheckIdle() { |
| 99 | // CalculateIdleState expects an interval in seconds. |
| 100 | int idle_time_allowed = UseTestingIntervals() ? kIdleAmount : |
| 101 | kIdleAmount * 60 * 60; |
| 102 | |
| 103 | CalculateIdleState( |
| 104 | idle_time_allowed, base::Bind(&UpgradeDetector::IdleCallback, |
| 105 | base::Unretained(this))); |
| 106 | } |
| 107 | |
[email protected] | dd46dd5 | 2014-08-07 21:57:14 | [diff] [blame] | 108 | void UpgradeDetector::TriggerNotification(chrome::NotificationType type) { |
| 109 | content::NotificationService::current()->Notify( |
| 110 | type, |
| 111 | content::Source<UpgradeDetector>(this), |
| 112 | content::NotificationService::NoDetails()); |
| 113 | } |
| 114 | |
derat | 49b2fd5 | 2015-01-16 15:32:12 | [diff] [blame] | 115 | void UpgradeDetector::IdleCallback(ui::IdleState state) { |
[email protected] | 914ace6 | 2012-10-22 17:16:02 | [diff] [blame] | 116 | // Don't proceed while an incognito window is open. The timer will still |
| 117 | // keep firing, so this function will get a chance to re-evaluate this. |
trizzofo | fc061d0 | 2016-07-07 00:51:21 | [diff] [blame] | 118 | if (chrome::IsIncognitoSessionActive()) |
[email protected] | 914ace6 | 2012-10-22 17:16:02 | [diff] [blame] | 119 | return; |
| 120 | |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 121 | switch (state) { |
derat | 49b2fd5 | 2015-01-16 15:32:12 | [diff] [blame] | 122 | case ui::IDLE_STATE_LOCKED: |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 123 | // Computer is locked, auto-restart. |
| 124 | idle_check_timer_.Stop(); |
[email protected] | 0c98ab65 | 2013-02-18 00:39:37 | [diff] [blame] | 125 | chrome::AttemptRestart(); |
[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_IDLE: |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 128 | // Computer has been idle for long enough, show warning. |
| 129 | idle_check_timer_.Stop(); |
[email protected] | dd46dd5 | 2014-08-07 21:57:14 | [diff] [blame] | 130 | TriggerNotification(chrome::NOTIFICATION_CRITICAL_UPGRADE_INSTALLED); |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 131 | break; |
derat | 49b2fd5 | 2015-01-16 15:32:12 | [diff] [blame] | 132 | case ui::IDLE_STATE_ACTIVE: |
| 133 | case ui::IDLE_STATE_UNKNOWN: |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 134 | break; |
| 135 | default: |
| 136 | NOTREACHED(); // Need to add any new value above (either providing |
| 137 | // automatic restart or show notification to user). |
| 138 | break; |
| 139 | } |
[email protected] | b1b7394 | 2010-05-26 20:11:54 | [diff] [blame] | 140 | } |