[email protected] | 049ed6d | 2012-01-25 11:40:42 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [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_impl.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
[email protected] | 6198f798 | 2011-11-17 21:24:37 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 10 | #include "base/command_line.h" |
[email protected] | 4b47374 | 2011-11-08 11:58:42 | [diff] [blame] | 11 | #include "base/file_path.h" |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
| 13 | #include "base/memory/singleton.h" |
[email protected] | 4b47374 | 2011-11-08 11:58:42 | [diff] [blame] | 14 | #include "base/path_service.h" |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 15 | #include "base/string_number_conversions.h" |
| 16 | #include "base/string_util.h" |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 17 | #include "base/time.h" |
| 18 | #include "base/utf_string_conversions.h" |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 19 | #include "chrome/common/chrome_switches.h" |
| 20 | #include "chrome/common/chrome_version_info.h" |
| 21 | #include "chrome/installer/util/browser_distribution.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 22 | #include "content/public/browser/browser_thread.h" |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 23 | #include "ui/base/resource/resource_bundle.h" |
| 24 | |
| 25 | #if defined(OS_WIN) |
| 26 | #include "chrome/installer/util/install_util.h" |
| 27 | #elif defined(OS_MACOSX) |
[email protected] | 5950f571 | 2011-06-20 22:15:52 | [diff] [blame] | 28 | #include "chrome/browser/mac/keystone_glue.h" |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 29 | #elif defined(OS_POSIX) |
| 30 | #include "base/process_util.h" |
| 31 | #include "base/version.h" |
| 32 | #endif |
| 33 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 34 | using content::BrowserThread; |
| 35 | |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | // How long (in milliseconds) to wait (each cycle) before checking whether |
| 39 | // Chrome's been upgraded behind our back. |
| 40 | const int kCheckForUpgradeMs = 2 * 60 * 60 * 1000; // 2 hours. |
| 41 | |
| 42 | // How long to wait (each cycle) before checking which severity level we should |
| 43 | // be at. Once we reach the highest severity, the timer will stop. |
| 44 | const int kNotifyCycleTimeMs = 20 * 60 * 1000; // 20 minutes. |
| 45 | |
| 46 | // Same as kNotifyCycleTimeMs but only used during testing. |
| 47 | const int kNotifyCycleTimeForTestingMs = 500; // Half a second. |
| 48 | |
| 49 | std::string CmdLineInterval() { |
| 50 | const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); |
| 51 | return cmd_line.GetSwitchValueASCII(switches::kCheckForUpdateIntervalSec); |
| 52 | } |
| 53 | |
[email protected] | 049ed6d | 2012-01-25 11:40:42 | [diff] [blame] | 54 | bool IsTesting() { |
| 55 | const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); |
| 56 | return cmd_line.HasSwitch(switches::kSimulateUpgrade) || |
| 57 | cmd_line.HasSwitch(switches::kCheckForUpdateIntervalSec); |
| 58 | } |
| 59 | |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 60 | // How often to check for an upgrade. |
| 61 | int GetCheckForUpgradeEveryMs() { |
| 62 | // Check for a value passed via the command line. |
| 63 | int interval_ms; |
| 64 | std::string interval = CmdLineInterval(); |
| 65 | if (!interval.empty() && base::StringToInt(interval, &interval_ms)) |
| 66 | return interval_ms * 1000; // Command line value is in seconds. |
| 67 | |
| 68 | return kCheckForUpgradeMs; |
| 69 | } |
| 70 | |
| 71 | // This task checks the currently running version of Chrome against the |
| 72 | // installed version. If the installed version is newer, it runs the passed |
| 73 | // callback task. Otherwise it just deletes the task. |
[email protected] | 68994945 | 2011-12-08 02:36:39 | [diff] [blame] | 74 | void DetectUpgradeTask(const base::Closure& upgrade_detected_task, |
| 75 | bool* is_unstable_channel, |
| 76 | bool* is_critical_upgrade) { |
| 77 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 78 | |
[email protected] | 12126d37 | 2012-07-11 18:40:53 | [diff] [blame] | 79 | Version installed_version; |
| 80 | Version critical_update; |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 81 | |
| 82 | #if defined(OS_WIN) |
[email protected] | 68994945 | 2011-12-08 02:36:39 | [diff] [blame] | 83 | // Get the version of the currently *installed* instance of Chrome, |
| 84 | // which might be newer than the *running* instance if we have been |
| 85 | // upgraded in the background. |
| 86 | FilePath exe_path; |
| 87 | if (!PathService::Get(base::DIR_EXE, &exe_path)) { |
| 88 | NOTREACHED() << "Failed to find executable path"; |
| 89 | return; |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | 68994945 | 2011-12-08 02:36:39 | [diff] [blame] | 92 | bool system_install = |
| 93 | !InstallUtil::IsPerUserInstall(exe_path.value().c_str()); |
| 94 | |
| 95 | // TODO(tommi): Check if using the default distribution is always the right |
| 96 | // thing to do. |
| 97 | BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
[email protected] | 12126d37 | 2012-07-11 18:40:53 | [diff] [blame] | 98 | InstallUtil::GetChromeVersion(dist, system_install, &installed_version); |
[email protected] | 68994945 | 2011-12-08 02:36:39 | [diff] [blame] | 99 | |
[email protected] | 12126d37 | 2012-07-11 18:40:53 | [diff] [blame] | 100 | if (installed_version.IsValid()) { |
| 101 | InstallUtil::GetCriticalUpdateVersion(dist, system_install, |
| 102 | &critical_update); |
[email protected] | 68994945 | 2011-12-08 02:36:39 | [diff] [blame] | 103 | } |
| 104 | #elif defined(OS_MACOSX) |
[email protected] | 12126d37 | 2012-07-11 18:40:53 | [diff] [blame] | 105 | installed_version = |
| 106 | Version(UTF16ToASCII(keystone_glue::CurrentlyInstalledVersion())); |
[email protected] | 68994945 | 2011-12-08 02:36:39 | [diff] [blame] | 107 | #elif defined(OS_POSIX) |
| 108 | // POSIX but not Mac OS X: Linux, etc. |
| 109 | CommandLine command_line(*CommandLine::ForCurrentProcess()); |
| 110 | command_line.AppendSwitch(switches::kProductVersion); |
| 111 | std::string reply; |
| 112 | if (!base::GetAppOutput(command_line, &reply)) { |
| 113 | DLOG(ERROR) << "Failed to get current file version"; |
| 114 | return; |
| 115 | } |
| 116 | |
[email protected] | 12126d37 | 2012-07-11 18:40:53 | [diff] [blame] | 117 | installed_version = Version(reply); |
[email protected] | 68994945 | 2011-12-08 02:36:39 | [diff] [blame] | 118 | #endif |
| 119 | |
| 120 | chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); |
| 121 | *is_unstable_channel = channel == chrome::VersionInfo::CHANNEL_DEV || |
| 122 | channel == chrome::VersionInfo::CHANNEL_CANARY; |
| 123 | |
| 124 | // Get the version of the currently *running* instance of Chrome. |
| 125 | chrome::VersionInfo version_info; |
| 126 | if (!version_info.is_valid()) { |
| 127 | NOTREACHED() << "Failed to get current file version"; |
| 128 | return; |
| 129 | } |
[email protected] | 12126d37 | 2012-07-11 18:40:53 | [diff] [blame] | 130 | Version running_version(version_info.Version()); |
| 131 | if (!running_version.IsValid()) { |
| 132 | NOTREACHED(); |
[email protected] | 68994945 | 2011-12-08 02:36:39 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |
| 136 | // |installed_version| may be NULL when the user downgrades on Linux (by |
| 137 | // switching from dev to beta channel, for example). The user needs a |
| 138 | // restart in this case as well. See http://crbug.com/46547 |
[email protected] | 12126d37 | 2012-07-11 18:40:53 | [diff] [blame] | 139 | if (!installed_version.IsValid() || |
| 140 | (installed_version.CompareTo(running_version) > 0)) { |
[email protected] | 68994945 | 2011-12-08 02:36:39 | [diff] [blame] | 141 | // If a more recent version is available, it might be that we are lacking |
| 142 | // a critical update, such as a zero-day fix. |
| 143 | *is_critical_upgrade = |
[email protected] | 12126d37 | 2012-07-11 18:40:53 | [diff] [blame] | 144 | critical_update.IsValid() && |
| 145 | (critical_update.CompareTo(running_version) > 0); |
[email protected] | 68994945 | 2011-12-08 02:36:39 | [diff] [blame] | 146 | |
| 147 | // Fire off the upgrade detected task. |
| 148 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 149 | upgrade_detected_task); |
| 150 | } |
| 151 | } |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 152 | |
| 153 | } // namespace |
| 154 | |
| 155 | UpgradeDetectorImpl::UpgradeDetectorImpl() |
[email protected] | 6198f798 | 2011-11-17 21:24:37 | [diff] [blame] | 156 | : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 157 | is_unstable_channel_(false) { |
| 158 | CommandLine command_line(*CommandLine::ForCurrentProcess()); |
| 159 | if (command_line.HasSwitch(switches::kDisableBackgroundNetworking)) |
| 160 | return; |
[email protected] | 049ed6d | 2012-01-25 11:40:42 | [diff] [blame] | 161 | if (command_line.HasSwitch(switches::kSimulateUpgrade)) { |
| 162 | UpgradeDetected(); |
| 163 | return; |
| 164 | } |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 165 | // Windows: only enable upgrade notifications for official builds. |
| 166 | // Mac: only enable them if the updater (Keystone) is present. |
| 167 | // Linux (and other POSIX): always enable regardless of branding. |
| 168 | #if (defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)) || defined(OS_POSIX) |
| 169 | #if defined(OS_MACOSX) |
| 170 | if (keystone_glue::KeystoneEnabled()) |
| 171 | #endif |
| 172 | { |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 173 | detect_upgrade_timer_.Start(FROM_HERE, |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 174 | base::TimeDelta::FromMilliseconds(GetCheckForUpgradeEveryMs()), |
| 175 | this, &UpgradeDetectorImpl::CheckForUpgrade); |
| 176 | } |
| 177 | #endif |
| 178 | } |
| 179 | |
| 180 | UpgradeDetectorImpl::~UpgradeDetectorImpl() { |
| 181 | } |
| 182 | |
| 183 | void UpgradeDetectorImpl::CheckForUpgrade() { |
[email protected] | 6198f798 | 2011-11-17 21:24:37 | [diff] [blame] | 184 | weak_factory_.InvalidateWeakPtrs(); |
| 185 | base::Closure callback_task = |
| 186 | base::Bind(&UpgradeDetectorImpl::UpgradeDetected, |
| 187 | weak_factory_.GetWeakPtr()); |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 188 | // We use FILE as the thread to run the upgrade detection code on all |
| 189 | // platforms. For Linux, this is because we don't want to block the UI thread |
| 190 | // while launching a background process and reading its output; on the Mac and |
| 191 | // on Windows checking for an upgrade requires reading a file. |
| 192 | BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
[email protected] | 68994945 | 2011-12-08 02:36:39 | [diff] [blame] | 193 | base::Bind(&DetectUpgradeTask, |
| 194 | callback_task, |
| 195 | &is_unstable_channel_, |
| 196 | &is_critical_upgrade_)); |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void UpgradeDetectorImpl::UpgradeDetected() { |
| 200 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 201 | |
| 202 | // Stop the recurring timer (that is checking for changes). |
| 203 | detect_upgrade_timer_.Stop(); |
| 204 | |
| 205 | NotifyUpgradeDetected(); |
| 206 | |
| 207 | // Start the repeating timer for notifying the user after a certain period. |
| 208 | // The called function will eventually figure out that enough time has passed |
| 209 | // and stop the timer. |
[email protected] | 049ed6d | 2012-01-25 11:40:42 | [diff] [blame] | 210 | int cycle_time = IsTesting() ? |
| 211 | kNotifyCycleTimeForTestingMs : kNotifyCycleTimeMs; |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 212 | upgrade_notification_timer_.Start(FROM_HERE, |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 213 | base::TimeDelta::FromMilliseconds(cycle_time), |
| 214 | this, &UpgradeDetectorImpl::NotifyOnUpgrade); |
| 215 | } |
| 216 | |
| 217 | void UpgradeDetectorImpl::NotifyOnUpgrade() { |
| 218 | base::TimeDelta delta = base::Time::Now() - upgrade_detected_time(); |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 219 | |
[email protected] | 049ed6d | 2012-01-25 11:40:42 | [diff] [blame] | 220 | // We'll make testing more convenient by switching to seconds of waiting |
| 221 | // instead of days between flipping severity. |
| 222 | bool is_testing = IsTesting(); |
| 223 | int64 time_passed = is_testing ? delta.InSeconds() : delta.InHours(); |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 224 | |
| 225 | if (is_unstable_channel_) { |
| 226 | // There's only one threat level for unstable channels like dev and |
| 227 | // canary, and it hits after one hour. During testing, it hits after one |
| 228 | // minute. |
| 229 | const int kUnstableThreshold = 1; |
| 230 | |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 231 | if (is_critical_upgrade_) |
| 232 | set_upgrade_notification_stage(UPGRADE_ANNOYANCE_CRITICAL); |
| 233 | else if (time_passed >= kUnstableThreshold) { |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 234 | set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); |
| 235 | |
| 236 | // That's as high as it goes. |
| 237 | upgrade_notification_timer_.Stop(); |
| 238 | } else { |
| 239 | return; // Not ready to recommend upgrade. |
| 240 | } |
| 241 | } else { |
[email protected] | 049ed6d | 2012-01-25 11:40:42 | [diff] [blame] | 242 | const int kMultiplier = is_testing ? 1 : 24; |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 243 | // 14 days when not testing, otherwise 14 seconds. |
| 244 | const int kSevereThreshold = 14 * kMultiplier; |
| 245 | const int kHighThreshold = 7 * kMultiplier; |
| 246 | const int kElevatedThreshold = 4 * kMultiplier; |
| 247 | const int kLowThreshold = 2 * kMultiplier; |
| 248 | |
| 249 | // These if statements must be sorted (highest interval first). |
[email protected] | f1c76b9f | 2011-10-13 13:43:44 | [diff] [blame] | 250 | if (time_passed >= kSevereThreshold || is_critical_upgrade_) { |
| 251 | set_upgrade_notification_stage( |
| 252 | is_critical_upgrade_ ? UPGRADE_ANNOYANCE_CRITICAL : |
| 253 | UPGRADE_ANNOYANCE_SEVERE); |
[email protected] | ceff840 | 2011-06-12 23:27:12 | [diff] [blame] | 254 | |
| 255 | // We can't get any higher, baby. |
| 256 | upgrade_notification_timer_.Stop(); |
| 257 | } else if (time_passed >= kHighThreshold) { |
| 258 | set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH); |
| 259 | } else if (time_passed >= kElevatedThreshold) { |
| 260 | set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED); |
| 261 | } else if (time_passed >= kLowThreshold) { |
| 262 | set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); |
| 263 | } else { |
| 264 | return; // Not ready to recommend upgrade. |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | NotifyUpgradeRecommended(); |
| 269 | } |
| 270 | |
| 271 | // static |
| 272 | UpgradeDetectorImpl* UpgradeDetectorImpl::GetInstance() { |
| 273 | return Singleton<UpgradeDetectorImpl>::get(); |
| 274 | } |
| 275 | |
| 276 | // static |
| 277 | UpgradeDetector* UpgradeDetector::GetInstance() { |
| 278 | return UpgradeDetectorImpl::GetInstance(); |
| 279 | } |