Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/upgrade_detector_impl.h" | 5 #include "chrome/browser/upgrade_detector_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/build_time.h" | |
| 10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 11 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
| 15 #include "base/metrics/field_trial.h" | |
| 14 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 15 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" |
| 16 #include "base/string_util.h" | 18 #include "base/string_util.h" |
| 17 #include "base/time.h" | 19 #include "base/time.h" |
| 18 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 21 #include "base/version.h" | |
| 22 #include "chrome/browser/browser_process.h" | |
| 19 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/chrome_version_info.h" | 24 #include "chrome/common/chrome_version_info.h" |
| 21 #include "chrome/installer/util/browser_distribution.h" | |
| 22 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 23 #include "ui/base/resource/resource_bundle.h" | 26 #include "ui/base/resource/resource_bundle.h" |
| 24 | 27 |
| 25 #if defined(OS_WIN) | 28 #if defined(OS_WIN) |
| 29 #include "chrome/installer/util/browser_distribution.h" | |
| 30 #include "chrome/installer/util/google_update_settings.h" | |
| 31 #include "chrome/installer/util/helper.h" | |
| 26 #include "chrome/installer/util/install_util.h" | 32 #include "chrome/installer/util/install_util.h" |
| 27 #elif defined(OS_MACOSX) | 33 #elif defined(OS_MACOSX) |
| 28 #include "chrome/browser/mac/keystone_glue.h" | 34 #include "chrome/browser/mac/keystone_glue.h" |
| 29 #elif defined(OS_POSIX) | 35 #elif defined(OS_POSIX) |
| 30 #include "base/process_util.h" | 36 #include "base/process_util.h" |
| 31 #include "base/version.h" | |
| 32 #endif | 37 #endif |
| 33 | 38 |
| 34 using content::BrowserThread; | 39 using content::BrowserThread; |
| 35 | 40 |
| 36 namespace { | 41 namespace { |
| 37 | 42 |
| 38 // How long (in milliseconds) to wait (each cycle) before checking whether | 43 // How long (in milliseconds) to wait (each cycle) before checking whether |
| 39 // Chrome's been upgraded behind our back. | 44 // Chrome's been upgraded behind our back. |
| 40 const int kCheckForUpgradeMs = 2 * 60 * 60 * 1000; // 2 hours. | 45 const int kCheckForUpgradeMs = 2 * 60 * 60 * 1000; // 2 hours. |
| 41 | 46 |
| 42 // How long to wait (each cycle) before checking which severity level we should | 47 // 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. | 48 // be at. Once we reach the highest severity, the timer will stop. |
| 44 const int kNotifyCycleTimeMs = 20 * 60 * 1000; // 20 minutes. | 49 const int kNotifyCycleTimeMs = 20 * 60 * 1000; // 20 minutes. |
| 45 | 50 |
| 46 // Same as kNotifyCycleTimeMs but only used during testing. | 51 // Same as kNotifyCycleTimeMs but only used during testing. |
| 47 const int kNotifyCycleTimeForTestingMs = 500; // Half a second. | 52 const int kNotifyCycleTimeForTestingMs = 500; // Half a second. |
| 48 | 53 |
| 54 // The number of days after which we identify a build/install as outdated. | |
| 55 const uint64 kOutdatedBuildAgeInDays = 12 * 7; | |
| 56 | |
| 57 // Finch Experiment strings to identify if we should check for outdated install. | |
| 58 const char kOutdatedInstallCheckTrialName[] = "OutdatedInstallCheck"; | |
| 59 const char kOutdatedInstallCheck12WeeksGroupName[] = "12WeeksOutdatedIntalls"; | |
| 60 | |
| 49 std::string CmdLineInterval() { | 61 std::string CmdLineInterval() { |
| 50 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); | 62 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); |
| 51 return cmd_line.GetSwitchValueASCII(switches::kCheckForUpdateIntervalSec); | 63 return cmd_line.GetSwitchValueASCII(switches::kCheckForUpdateIntervalSec); |
| 52 } | 64 } |
| 53 | 65 |
| 54 bool IsTesting() { | 66 bool IsTesting() { |
| 55 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); | 67 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); |
| 56 return cmd_line.HasSwitch(switches::kSimulateUpgrade) || | 68 return cmd_line.HasSwitch(switches::kSimulateUpgrade) || |
| 57 cmd_line.HasSwitch(switches::kCheckForUpdateIntervalSec); | 69 cmd_line.HasSwitch(switches::kCheckForUpdateIntervalSec) || |
| 70 cmd_line.HasSwitch(switches::kSimulateCriticalUpdate) || | |
| 71 cmd_line.HasSwitch(switches::kSimulateOutdated); | |
| 58 } | 72 } |
| 59 | 73 |
| 60 // How often to check for an upgrade. | 74 // How often to check for an upgrade. |
| 61 int GetCheckForUpgradeEveryMs() { | 75 int GetCheckForUpgradeEveryMs() { |
| 62 // Check for a value passed via the command line. | 76 // Check for a value passed via the command line. |
| 63 int interval_ms; | 77 int interval_ms; |
| 64 std::string interval = CmdLineInterval(); | 78 std::string interval = CmdLineInterval(); |
| 65 if (!interval.empty() && base::StringToInt(interval, &interval_ms)) | 79 if (!interval.empty() && base::StringToInt(interval, &interval_ms)) |
| 66 return interval_ms * 1000; // Command line value is in seconds. | 80 return interval_ms * 1000; // Command line value is in seconds. |
| 67 | 81 |
| 68 return kCheckForUpgradeMs; | 82 return kCheckForUpgradeMs; |
| 69 } | 83 } |
| 70 | 84 |
| 85 bool IsUnstableChannel() { | |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 87 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); | |
| 88 return channel == chrome::VersionInfo::CHANNEL_DEV || | |
| 89 channel == chrome::VersionInfo::CHANNEL_CANARY; | |
| 90 } | |
| 91 | |
| 92 // This task identifies whether we are running an unstable version. And then | |
| 93 // it unconditionally calls back the provided task. | |
| 94 void CheckForUnstableChannel(const base::Closure& callback_task, | |
| 95 bool* is_unstable_channel) { | |
| 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 97 *is_unstable_channel = IsUnstableChannel(); | |
| 98 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback_task); | |
| 99 } | |
| 100 | |
| 101 #if defined(OS_WIN) | |
| 102 bool IsSystemInstall() { | |
| 103 FilePath exe_path; | |
| 104 if (!PathService::Get(base::DIR_EXE, &exe_path)) { | |
| 105 NOTREACHED() << "Failed to find executable path"; | |
| 106 return false; | |
| 107 } | |
| 108 | |
| 109 return !InstallUtil::IsPerUserInstall(exe_path.value().c_str()); | |
| 110 } | |
| 111 | |
| 112 // This task checks the update policy and calls back the task only if automatic | |
| 113 // updates are allowed. It also identifies whether we are running an unstable | |
| 114 // channel. | |
| 115 void DetectUpdatability(const base::Closure& callback_task, | |
| 116 bool* is_unstable_channel) { | |
| 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 118 | |
| 119 string16 app_guid = installer::GetAppGuidForUpdates(IsSystemInstall()); | |
| 120 DCHECK(!app_guid.empty()); | |
| 121 if (GoogleUpdateSettings::AUTOMATIC_UPDATES == | |
| 122 GoogleUpdateSettings::GetAppUpdatePolicy(app_guid, NULL)) { | |
| 123 CheckForUnstableChannel(callback_task, is_unstable_channel); | |
| 124 } else { | |
| 125 *is_unstable_channel = IsUnstableChannel(); | |
| 126 } | |
|
Finnur
2013/02/05 22:14:50
Why do you need the else for?
If the app update po
MAD
2013/02/06 03:14:28
D'ho!ne.
| |
| 127 } | |
| 128 #endif // defined(OS_WIN) | |
| 129 | |
| 130 } // namespace | |
| 131 | |
| 132 UpgradeDetectorImpl::UpgradeDetectorImpl() | |
| 133 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | |
| 134 is_unstable_channel_(false), | |
| 135 build_date_(base::GetBuildTime()) { | |
| 136 CommandLine command_line(*CommandLine::ForCurrentProcess()); | |
| 137 // The different command line switches that affect testing can't be used | |
| 138 // simultaneously, if they do, here's the precedence order, based on the order | |
| 139 // of the if statements below: | |
| 140 // - kDisableBackgroundNetworking prevents any of the other command line | |
| 141 // switch from being taken into account. | |
| 142 // - kSimulateUpgrade supersedes critical or outdated upgrade switches. | |
| 143 // - kSimulateCriticalUpdate has precedence over kSimulateOutdated. | |
| 144 // - kSimulateOutdated can work on its own, or with a specified date. | |
| 145 if (command_line.HasSwitch(switches::kDisableBackgroundNetworking)) | |
| 146 return; | |
| 147 if (command_line.HasSwitch(switches::kSimulateUpgrade)) { | |
| 148 UpgradeDetected(UPGRADE_AVAILABLE_REGULAR); | |
| 149 return; | |
| 150 } | |
| 151 if (command_line.HasSwitch(switches::kSimulateCriticalUpdate)) { | |
| 152 UpgradeDetected(UPGRADE_AVAILABLE_CRITICAL); | |
| 153 return; | |
| 154 } | |
| 155 if (command_line.HasSwitch(switches::kSimulateOutdated)) { | |
| 156 // The outdated simulation can work without a value, which means outdated | |
| 157 // now, or with a value that must be a well formed date/time string that | |
| 158 // overrides the build date. | |
| 159 std::string build_date = command_line.GetSwitchValueASCII( | |
| 160 switches::kSimulateOutdated); | |
| 161 base::Time maybe_build_time; | |
| 162 bool result = base::Time::FromString(build_date.c_str(), &maybe_build_time); | |
| 163 if (result && !maybe_build_time.is_null()) { | |
| 164 // We got a valid build date simulation so use it and check for upgrades. | |
| 165 build_date_ = maybe_build_time; | |
| 166 StartTimerForUpgradeCheck(); | |
| 167 } else { | |
| 168 // Without a valid date, we simulate that we are already outdated... | |
| 169 UpgradeDetected(UPGRADE_NEEDED_OUTDATED_INSTALL); | |
| 170 } | |
| 171 return; | |
| 172 } | |
| 173 | |
| 174 // Windows: only enable upgrade notifications for official builds. | |
| 175 // Mac: only enable them if the updater (Keystone) is present. | |
| 176 // Linux (and other POSIX): always enable regardless of branding. | |
| 177 base::Closure start_upgrade_check_timer_task = | |
| 178 base::Bind(&UpgradeDetectorImpl::StartTimerForUpgradeCheck, | |
| 179 weak_factory_.GetWeakPtr()); | |
| 180 #if defined(OS_WINDOW) && defined(GOOGLE_CHROME_BUILD) | |
| 181 // On Windows, there might be a policy preventing updates, so validate | |
| 182 // updatability, and then call StartTimerForUpgradeCheck appropriately. | |
| 183 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 184 base::Bind(&DetectUpdatability, | |
| 185 start_upgrade_check_timer_task, | |
| 186 &is_unstable_channel_)); | |
| 187 return; | |
| 188 #elif defined(OS_WINDOW) && !defined(GOOGLE_CHROME_BUILD) | |
| 189 return; // Chromium has no upgrade channel. | |
| 190 #elif defined(OS_MACOSX) | |
| 191 if (!keystone_glue::KeystoneEnabled()) | |
| 192 return; // Keystone updater not enabled. | |
| 193 #elif !defined(OS_POSIX) | |
| 194 return; | |
| 195 #endif | |
| 196 | |
| 197 // Check whether the build is an unstable channel before starting the timer. | |
| 198 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 199 base::Bind(&CheckForUnstableChannel, | |
| 200 start_upgrade_check_timer_task, | |
| 201 &is_unstable_channel_)); | |
| 202 } | |
| 203 | |
| 204 UpgradeDetectorImpl::~UpgradeDetectorImpl() { | |
| 205 } | |
| 206 | |
| 207 // Static | |
| 71 // This task checks the currently running version of Chrome against the | 208 // This task checks the currently running version of Chrome against the |
| 72 // installed version. If the installed version is newer, it runs the passed | 209 // installed version. If the installed version is newer, it calls back |
| 73 // callback task. Otherwise it just deletes the task. | 210 // UpgradeDetectorImpl::UpgradeDetected using |upgrade_detector|. |
| 74 void DetectUpgradeTask(const base::Closure& upgrade_detected_task, | 211 void UpgradeDetectorImpl::DetectUpgradeTask( |
| 75 bool* is_unstable_channel, | 212 base::WeakPtr<UpgradeDetectorImpl> upgrade_detector) { |
| 76 bool* is_critical_upgrade) { | |
| 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 78 | 214 |
| 79 Version installed_version; | 215 Version installed_version; |
| 80 Version critical_update; | 216 Version critical_update; |
| 81 | 217 |
| 82 #if defined(OS_WIN) | 218 #if defined(OS_WIN) |
| 83 // Get the version of the currently *installed* instance of Chrome, | 219 // Get the version of the currently *installed* instance of Chrome, |
| 84 // which might be newer than the *running* instance if we have been | 220 // which might be newer than the *running* instance if we have been |
| 85 // upgraded in the background. | 221 // upgraded in the background. |
| 86 FilePath exe_path; | 222 bool system_install = IsSystemInstall(); |
| 87 if (!PathService::Get(base::DIR_EXE, &exe_path)) { | |
| 88 NOTREACHED() << "Failed to find executable path"; | |
| 89 return; | |
| 90 } | |
| 91 | |
| 92 bool system_install = | |
| 93 !InstallUtil::IsPerUserInstall(exe_path.value().c_str()); | |
| 94 | 223 |
| 95 // TODO(tommi): Check if using the default distribution is always the right | 224 // TODO(tommi): Check if using the default distribution is always the right |
| 96 // thing to do. | 225 // thing to do. |
| 97 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 226 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 98 InstallUtil::GetChromeVersion(dist, system_install, &installed_version); | 227 InstallUtil::GetChromeVersion(dist, system_install, &installed_version); |
| 99 | 228 |
| 100 if (installed_version.IsValid()) { | 229 if (installed_version.IsValid()) { |
| 101 InstallUtil::GetCriticalUpdateVersion(dist, system_install, | 230 InstallUtil::GetCriticalUpdateVersion(dist, system_install, |
| 102 &critical_update); | 231 &critical_update); |
| 103 } | 232 } |
| 104 #elif defined(OS_MACOSX) | 233 #elif defined(OS_MACOSX) |
| 105 installed_version = | 234 installed_version = |
| 106 Version(UTF16ToASCII(keystone_glue::CurrentlyInstalledVersion())); | 235 Version(UTF16ToASCII(keystone_glue::CurrentlyInstalledVersion())); |
| 107 #elif defined(OS_POSIX) | 236 #elif defined(OS_POSIX) |
| 108 // POSIX but not Mac OS X: Linux, etc. | 237 // POSIX but not Mac OS X: Linux, etc. |
| 109 CommandLine command_line(*CommandLine::ForCurrentProcess()); | 238 CommandLine command_line(*CommandLine::ForCurrentProcess()); |
| 110 command_line.AppendSwitch(switches::kProductVersion); | 239 command_line.AppendSwitch(switches::kProductVersion); |
| 111 std::string reply; | 240 std::string reply; |
| 112 if (!base::GetAppOutput(command_line, &reply)) { | 241 if (!base::GetAppOutput(command_line, &reply)) { |
| 113 DLOG(ERROR) << "Failed to get current file version"; | 242 DLOG(ERROR) << "Failed to get current file version"; |
| 114 return; | 243 return; |
| 115 } | 244 } |
| 116 | 245 |
| 117 installed_version = Version(reply); | 246 installed_version = Version(reply); |
| 118 #endif | 247 #endif |
| 119 | 248 |
| 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. | 249 // Get the version of the currently *running* instance of Chrome. |
| 125 chrome::VersionInfo version_info; | 250 chrome::VersionInfo version_info; |
| 126 if (!version_info.is_valid()) { | 251 if (!version_info.is_valid()) { |
| 127 NOTREACHED() << "Failed to get current file version"; | 252 NOTREACHED() << "Failed to get current file version"; |
| 128 return; | 253 return; |
| 129 } | 254 } |
| 130 Version running_version(version_info.Version()); | 255 Version running_version(version_info.Version()); |
| 131 if (!running_version.IsValid()) { | 256 if (!running_version.IsValid()) { |
| 132 NOTREACHED(); | 257 NOTREACHED(); |
| 133 return; | 258 return; |
| 134 } | 259 } |
| 135 | 260 |
| 136 // |installed_version| may be NULL when the user downgrades on Linux (by | 261 // |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 | 262 // switching from dev to beta channel, for example). The user needs a |
| 138 // restart in this case as well. See http://crbug.com/46547 | 263 // restart in this case as well. See http://crbug.com/46547 |
| 139 if (!installed_version.IsValid() || | 264 if (!installed_version.IsValid() || |
| 140 (installed_version.CompareTo(running_version) > 0)) { | 265 (installed_version.CompareTo(running_version) > 0)) { |
| 141 // If a more recent version is available, it might be that we are lacking | 266 // If a more recent version is available, it might be that we are lacking |
| 142 // a critical update, such as a zero-day fix. | 267 // a critical update, such as a zero-day fix. |
| 143 *is_critical_upgrade = | 268 UpgradeAvailable upgrade_available = UPGRADE_AVAILABLE_REGULAR; |
| 144 critical_update.IsValid() && | 269 if (critical_update.IsValid() && |
| 145 (critical_update.CompareTo(running_version) > 0); | 270 critical_update.CompareTo(running_version) > 0) { |
| 271 upgrade_available = UPGRADE_AVAILABLE_CRITICAL; | |
| 272 } | |
| 146 | 273 |
| 147 // Fire off the upgrade detected task. | 274 // Fire off the upgrade detected task. |
| 148 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 275 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 149 upgrade_detected_task); | 276 base::Bind(&UpgradeDetectorImpl::UpgradeDetected, |
| 277 upgrade_detector, | |
| 278 upgrade_available)); | |
| 150 } | 279 } |
| 151 } | 280 } |
| 152 | 281 |
| 153 } // namespace | 282 void UpgradeDetectorImpl::StartTimerForUpgradeCheck() { |
| 154 | 283 detect_upgrade_timer_.Start(FROM_HERE, |
| 155 UpgradeDetectorImpl::UpgradeDetectorImpl() | 284 base::TimeDelta::FromMilliseconds(GetCheckForUpgradeEveryMs()), |
| 156 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 285 this, &UpgradeDetectorImpl::CheckForUpgrade); |
| 157 is_unstable_channel_(false) { | |
| 158 CommandLine command_line(*CommandLine::ForCurrentProcess()); | |
| 159 if (command_line.HasSwitch(switches::kDisableBackgroundNetworking)) | |
| 160 return; | |
| 161 if (command_line.HasSwitch(switches::kSimulateUpgrade)) { | |
| 162 UpgradeDetected(); | |
| 163 return; | |
| 164 } | |
| 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 { | |
| 173 detect_upgrade_timer_.Start(FROM_HERE, | |
| 174 base::TimeDelta::FromMilliseconds(GetCheckForUpgradeEveryMs()), | |
| 175 this, &UpgradeDetectorImpl::CheckForUpgrade); | |
| 176 } | |
| 177 #endif | |
| 178 } | |
| 179 | |
| 180 UpgradeDetectorImpl::~UpgradeDetectorImpl() { | |
| 181 } | 286 } |
| 182 | 287 |
| 183 void UpgradeDetectorImpl::CheckForUpgrade() { | 288 void UpgradeDetectorImpl::CheckForUpgrade() { |
| 289 // Interrupt any (unlikely) unfinished execution of DetectUpgradeTask, or at | |
| 290 // least prevent the callback to be executed, because we will potentially | |
|
Finnur
2013/02/05 22:14:50
s/to/from/
MAD
2013/02/06 03:14:28
Done.
| |
| 291 // call it from within DetectOutdatedInstall() or will post | |
| 292 // DetectUpgradeTask again below anyway. | |
| 184 weak_factory_.InvalidateWeakPtrs(); | 293 weak_factory_.InvalidateWeakPtrs(); |
| 185 base::Closure callback_task = | 294 |
| 186 base::Bind(&UpgradeDetectorImpl::UpgradeDetected, | 295 // No need to look for upgrades if the install is outdated. |
| 187 weak_factory_.GetWeakPtr()); | 296 if (DetectOutdatedInstall()) |
| 297 return; | |
| 298 | |
| 188 // We use FILE as the thread to run the upgrade detection code on all | 299 // 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 | 300 // 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 | 301 // while launching a background process and reading its output; on the Mac and |
| 191 // on Windows checking for an upgrade requires reading a file. | 302 // on Windows checking for an upgrade requires reading a file. |
| 192 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 303 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 193 base::Bind(&DetectUpgradeTask, | 304 base::Bind(&UpgradeDetectorImpl::DetectUpgradeTask, |
| 194 callback_task, | 305 weak_factory_.GetWeakPtr())); |
| 195 &is_unstable_channel_, | |
| 196 &is_critical_upgrade_)); | |
| 197 } | 306 } |
| 198 | 307 |
| 199 void UpgradeDetectorImpl::UpgradeDetected() { | 308 bool UpgradeDetectorImpl::DetectOutdatedInstall() { |
| 309 // Only enable the outdated install check if we are running the trial for it, | |
| 310 // unless we are simulating an outdated isntall. | |
| 311 static bool simulate_outdated = CommandLine::ForCurrentProcess()->HasSwitch( | |
| 312 switches::kSimulateOutdated); | |
| 313 if (base::FieldTrialList::FindFullName(kOutdatedInstallCheckTrialName) != | |
| 314 kOutdatedInstallCheck12WeeksGroupName && !simulate_outdated) { | |
| 315 return false; | |
| 316 } | |
| 317 | |
| 318 base::Time network_time(base::Time::Now()); | |
| 319 // TODO(mad): Uncomment when variations service NetworkTime CL gets committed. | |
| 320 // if (!g_browser_process->variations_service()->GetNetworkTime(&network_time)) | |
| 321 // return; | |
| 322 | |
| 323 if (network_time.is_null() || build_date_.is_null() || | |
| 324 build_date_ > network_time) { | |
| 325 NOTREACHED(); | |
| 326 return false; | |
| 327 } | |
| 328 | |
| 329 if (network_time - build_date_ > | |
| 330 base::TimeDelta::FromDays(kOutdatedBuildAgeInDays)) { | |
| 331 UpgradeDetected(UPGRADE_NEEDED_OUTDATED_INSTALL); | |
| 332 return true; | |
| 333 } | |
| 334 // If we simlated an outdated install with a date, we don't want to keep | |
| 335 // checking for version upgrades, which happens on non-official builds. | |
| 336 return simulate_outdated; | |
| 337 } | |
| 338 | |
| 339 void UpgradeDetectorImpl::UpgradeDetected(UpgradeAvailable upgrade_available) { | |
| 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 341 upgrade_available_ = upgrade_available; | |
| 201 | 342 |
| 202 // Stop the recurring timer (that is checking for changes). | 343 // Stop the recurring timer (that is checking for changes). |
| 203 detect_upgrade_timer_.Stop(); | 344 detect_upgrade_timer_.Stop(); |
| 204 | 345 |
| 205 NotifyUpgradeDetected(); | 346 NotifyUpgradeDetected(); |
| 206 | 347 |
| 207 // Start the repeating timer for notifying the user after a certain period. | 348 // 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 | 349 // The called function will eventually figure out that enough time has passed |
| 209 // and stop the timer. | 350 // and stop the timer. |
| 210 int cycle_time = IsTesting() ? | 351 int cycle_time = IsTesting() ? |
| 211 kNotifyCycleTimeForTestingMs : kNotifyCycleTimeMs; | 352 kNotifyCycleTimeForTestingMs : kNotifyCycleTimeMs; |
| 212 upgrade_notification_timer_.Start(FROM_HERE, | 353 upgrade_notification_timer_.Start(FROM_HERE, |
| 213 base::TimeDelta::FromMilliseconds(cycle_time), | 354 base::TimeDelta::FromMilliseconds(cycle_time), |
| 214 this, &UpgradeDetectorImpl::NotifyOnUpgrade); | 355 this, &UpgradeDetectorImpl::NotifyOnUpgrade); |
| 215 } | 356 } |
| 216 | 357 |
| 217 void UpgradeDetectorImpl::NotifyOnUpgrade() { | 358 void UpgradeDetectorImpl::NotifyOnUpgrade() { |
| 218 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time(); | 359 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time(); |
| 219 | 360 |
| 220 // We'll make testing more convenient by switching to seconds of waiting | 361 // We'll make testing more convenient by switching to seconds of waiting |
| 221 // instead of days between flipping severity. | 362 // instead of days between flipping severity. |
| 222 bool is_testing = IsTesting(); | 363 bool is_testing = IsTesting(); |
| 223 int64 time_passed = is_testing ? delta.InSeconds() : delta.InHours(); | 364 int64 time_passed = is_testing ? delta.InSeconds() : delta.InHours(); |
| 224 | 365 |
| 366 bool is_critical_or_outdated = upgrade_available_ > UPGRADE_AVAILABLE_REGULAR; | |
| 225 if (is_unstable_channel_) { | 367 if (is_unstable_channel_) { |
| 226 // There's only one threat level for unstable channels like dev and | 368 // 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 | 369 // canary, and it hits after one hour. During testing, it hits after one |
| 228 // minute. | 370 // minute. |
| 229 const int kUnstableThreshold = 1; | 371 const int kUnstableThreshold = 1; |
| 230 | 372 |
| 231 if (is_critical_upgrade_) | 373 if (is_critical_or_outdated) |
| 232 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_CRITICAL); | 374 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_CRITICAL); |
| 233 else if (time_passed >= kUnstableThreshold) { | 375 else if (time_passed >= kUnstableThreshold) { |
| 234 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); | 376 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); |
| 235 | 377 |
| 236 // That's as high as it goes. | 378 // That's as high as it goes. |
| 237 upgrade_notification_timer_.Stop(); | 379 upgrade_notification_timer_.Stop(); |
| 238 } else { | 380 } else { |
| 239 return; // Not ready to recommend upgrade. | 381 return; // Not ready to recommend upgrade. |
| 240 } | 382 } |
| 241 } else { | 383 } else { |
| 242 const int kMultiplier = is_testing ? 1 : 24; | 384 const int kMultiplier = is_testing ? 1 : 24; |
| 243 // 14 days when not testing, otherwise 14 seconds. | 385 // 14 days when not testing, otherwise 14 seconds. |
| 244 const int kSevereThreshold = 14 * kMultiplier; | 386 const int kSevereThreshold = 14 * kMultiplier; |
| 245 const int kHighThreshold = 7 * kMultiplier; | 387 const int kHighThreshold = 7 * kMultiplier; |
| 246 const int kElevatedThreshold = 4 * kMultiplier; | 388 const int kElevatedThreshold = 4 * kMultiplier; |
| 247 const int kLowThreshold = 2 * kMultiplier; | 389 const int kLowThreshold = 2 * kMultiplier; |
| 248 | 390 |
| 249 // These if statements must be sorted (highest interval first). | 391 // These if statements must be sorted (highest interval first). |
| 250 if (time_passed >= kSevereThreshold || is_critical_upgrade_) { | 392 if (time_passed >= kSevereThreshold || is_critical_or_outdated) { |
| 251 set_upgrade_notification_stage( | 393 set_upgrade_notification_stage( |
| 252 is_critical_upgrade_ ? UPGRADE_ANNOYANCE_CRITICAL : | 394 is_critical_or_outdated ? UPGRADE_ANNOYANCE_CRITICAL : |
| 253 UPGRADE_ANNOYANCE_SEVERE); | 395 UPGRADE_ANNOYANCE_SEVERE); |
| 254 | 396 |
| 255 // We can't get any higher, baby. | 397 // We can't get any higher, baby. |
| 256 upgrade_notification_timer_.Stop(); | 398 upgrade_notification_timer_.Stop(); |
| 257 } else if (time_passed >= kHighThreshold) { | 399 } else if (time_passed >= kHighThreshold) { |
| 258 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH); | 400 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH); |
| 259 } else if (time_passed >= kElevatedThreshold) { | 401 } else if (time_passed >= kElevatedThreshold) { |
| 260 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED); | 402 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED); |
| 261 } else if (time_passed >= kLowThreshold) { | 403 } else if (time_passed >= kLowThreshold) { |
| 262 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); | 404 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); |
| 263 } else { | 405 } else { |
| 264 return; // Not ready to recommend upgrade. | 406 return; // Not ready to recommend upgrade. |
| 265 } | 407 } |
| 266 } | 408 } |
| 267 | 409 |
| 268 NotifyUpgradeRecommended(); | 410 NotifyUpgradeRecommended(); |
| 269 } | 411 } |
| 270 | 412 |
| 271 // static | 413 // static |
| 272 UpgradeDetectorImpl* UpgradeDetectorImpl::GetInstance() { | 414 UpgradeDetectorImpl* UpgradeDetectorImpl::GetInstance() { |
| 273 return Singleton<UpgradeDetectorImpl>::get(); | 415 return Singleton<UpgradeDetectorImpl>::get(); |
| 274 } | 416 } |
| 275 | 417 |
| 276 // static | 418 // static |
| 277 UpgradeDetector* UpgradeDetector::GetInstance() { | 419 UpgradeDetector* UpgradeDetector::GetInstance() { |
| 278 return UpgradeDetectorImpl::GetInstance(); | 420 return UpgradeDetectorImpl::GetInstance(); |
| 279 } | 421 } |
| OLD | NEW |