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 #if defined(OS_WIN) | |
| 86 bool IsSystemInstall() { | |
| 87 FilePath exe_path; | |
| 88 if (!PathService::Get(base::DIR_EXE, &exe_path)) { | |
| 89 NOTREACHED() << "Failed to find executable path"; | |
| 90 return false; | |
| 91 } | |
| 92 | |
| 93 return !InstallUtil::IsPerUserInstall(exe_path.value().c_str()); | |
| 94 } | |
| 95 | |
| 96 // This task checks the update policy and calls back the task only if automatic | |
| 97 // updates are allowed. | |
| 98 void DetectUpdatability(bool* is_auto_update_allowed) { | |
| 99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 100 | |
| 101 string16 app_guid = installer::GetAppGuidForUpdates(IsSystemInstall()); | |
| 102 DCHECK(!app_guid.empty()); | |
| 103 *is_auto_update_allowed = GoogleUpdateSettings::AUTOMATIC_UPDATES == | |
| 104 GoogleUpdateSettings::GetAppUpdatePolicy(app_guid, NULL); | |
| 105 } | |
| 106 #endif // defined(OS_WIN) | |
| 107 | |
| 108 } // namespace | |
| 109 | |
| 110 UpgradeDetectorImpl::UpgradeDetectorImpl() | |
| 111 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | |
| 112 is_unstable_channel_(false), | |
| 113 is_outdated_reinstall_allowed_(false), | |
| 114 build_date_(base::GetBuildTime()) { | |
| 115 CommandLine command_line(*CommandLine::ForCurrentProcess()); | |
| 116 // The different command line switches that affect testing can't be used | |
| 117 // simultaneously, if they do, here's the precedence order, based on the order | |
| 118 // of the if statements below: | |
| 119 // - kDisableBackgroundNetworking prevents any of the other tests to work. | |
|
Finnur
2013/02/01 17:09:15
nit: You prevent something _from_ working, but you
MAD
2013/02/02 03:55:34
Done.
| |
| 120 // - kSimulateUpgrade will not test critical or outdated upgrade tests. | |
| 121 // - kSimulateCriticalUpdate has precedence over kSimulateOutdated. | |
| 122 // - kSimulateOutdated can work on it's own, or with a specified date. | |
| 123 if (command_line.HasSwitch(switches::kDisableBackgroundNetworking)) | |
| 124 return; | |
| 125 if (command_line.HasSwitch(switches::kSimulateUpgrade)) { | |
| 126 UpgradeDetected(false, is_unstable_channel_); | |
| 127 return; | |
| 128 } | |
| 129 if (command_line.HasSwitch(switches::kSimulateCriticalUpdate)) { | |
| 130 UpgradeDetected(true, is_unstable_channel_); | |
| 131 return; | |
| 132 } | |
| 133 if (command_line.HasSwitch(switches::kSimulateOutdated)) { | |
| 134 // The outdated simulation can work without a value, which means outdated | |
| 135 // now, or with a value that must be a well formed date/time string that | |
| 136 // overrides the build date. | |
| 137 is_outdated_reinstall_allowed_ = true; | |
| 138 std::string build_date = command_line.GetSwitchValueASCII( | |
| 139 switches::kSimulateOutdated); | |
| 140 base::Time maybe_build_time; | |
| 141 bool result = base::Time::FromString(build_date.c_str(), &maybe_build_time); | |
| 142 if (result && !maybe_build_time.is_null()) { | |
| 143 // We got a valid build date simulation so use it and check for upgrades. | |
| 144 build_date_ = maybe_build_time; | |
| 145 detect_upgrade_timer_.Start(FROM_HERE, | |
| 146 base::TimeDelta::FromMilliseconds(GetCheckForUpgradeEveryMs()), | |
| 147 this, &UpgradeDetectorImpl::CheckForUpgrade); | |
| 148 } else { | |
| 149 // Without a valid date, we simulate that we are already outdated... | |
| 150 upgrade_required_ = UPGRADE_REQUIRED_OUTDATED; | |
| 151 UpgradeDetected(false, is_unstable_channel_); | |
| 152 } | |
| 153 return; | |
| 154 } | |
| 155 // Windows: only enable upgrade notifications for official builds. | |
| 156 // Mac: only enable them if the updater (Keystone) is present. | |
| 157 // Linux (and other POSIX): always enable regardless of branding. | |
| 158 #if (defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)) || defined(OS_POSIX) | |
| 159 #if defined(OS_MACOSX) | |
| 160 is_outdated_reinstall_allowed_ = keystone_glue::KeystoneEnabled(); | |
| 161 if (is_outdated_reinstall_allowed_) | |
| 162 #endif | |
| 163 { | |
| 164 detect_upgrade_timer_.Start(FROM_HERE, | |
| 165 base::TimeDelta::FromMilliseconds(GetCheckForUpgradeEveryMs()), | |
| 166 this, &UpgradeDetectorImpl::CheckForUpgrade); | |
| 167 } | |
| 168 #endif | |
| 169 | |
| 170 // Only enable the outdated install check if we are running the trial for it. | |
| 171 if (base::FieldTrialList::FindFullName(kOutdatedInstallCheckTrialName) != | |
| 172 kOutdatedInstallCheck12WeeksGroupName) { | |
| 173 is_outdated_reinstall_allowed_ = false; | |
| 174 return; | |
| 175 } | |
| 176 | |
| 177 // On Windows, there might be a policy preventing updates, so validate | |
| 178 // updatability, and then set |is_outdated_reinstall_allowed_| appropriately. | |
| 179 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD) | |
| 180 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | |
| 181 base::Bind(&DetectUpdatability, | |
|
Finnur
2013/02/01 17:09:15
I believe we have a race condition here for the Wi
MAD
2013/02/02 03:55:34
OK, I agree that things need to be cleaned up, but
| |
| 182 &is_outdated_reinstall_allowed_)); | |
| 183 #elif defined(OS_POSIX) && !(defined(OS_MACOSX) || defined(OS_CHROME)) | |
| 184 // All other POSIX devices are always allowed, except OS_MACOSX which sets it | |
| 185 // above, and Chrome OS which doesn't support the reinstall bubble. | |
| 186 is_outdated_reinstall_allowed_ = true; | |
| 187 #endif | |
| 188 } | |
| 189 | |
| 190 UpgradeDetectorImpl::~UpgradeDetectorImpl() { | |
| 191 } | |
| 192 | |
| 193 // Static | |
| 71 // This task checks the currently running version of Chrome against the | 194 // This task checks the currently running version of Chrome against the |
| 72 // installed version. If the installed version is newer, it runs the passed | 195 // installed version. If the installed version is newer, it calls back |
| 73 // callback task. Otherwise it just deletes the task. | 196 // UpgradeDetectorImpl::UpgradeDetected. |
| 74 void DetectUpgradeTask(const base::Closure& upgrade_detected_task, | 197 void UpgradeDetectorImpl::DetectUpgradeTask( |
| 75 bool* is_unstable_channel, | 198 base::WeakPtr<UpgradeDetectorImpl> upgrade_detector) { |
| 76 bool* is_critical_upgrade) { | |
| 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 78 | 200 |
| 79 Version installed_version; | 201 Version installed_version; |
| 80 Version critical_update; | 202 Version critical_update; |
| 81 | 203 |
| 82 #if defined(OS_WIN) | 204 #if defined(OS_WIN) |
| 83 // Get the version of the currently *installed* instance of Chrome, | 205 // Get the version of the currently *installed* instance of Chrome, |
| 84 // which might be newer than the *running* instance if we have been | 206 // which might be newer than the *running* instance if we have been |
| 85 // upgraded in the background. | 207 // upgraded in the background. |
| 86 FilePath exe_path; | 208 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 | 209 |
| 95 // TODO(tommi): Check if using the default distribution is always the right | 210 // TODO(tommi): Check if using the default distribution is always the right |
| 96 // thing to do. | 211 // thing to do. |
| 97 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); | 212 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); |
| 98 InstallUtil::GetChromeVersion(dist, system_install, &installed_version); | 213 InstallUtil::GetChromeVersion(dist, system_install, &installed_version); |
| 99 | 214 |
| 100 if (installed_version.IsValid()) { | 215 if (installed_version.IsValid()) { |
| 101 InstallUtil::GetCriticalUpdateVersion(dist, system_install, | 216 InstallUtil::GetCriticalUpdateVersion(dist, system_install, |
| 102 &critical_update); | 217 &critical_update); |
| 103 } | 218 } |
| 104 #elif defined(OS_MACOSX) | 219 #elif defined(OS_MACOSX) |
| 105 installed_version = | 220 installed_version = |
| 106 Version(UTF16ToASCII(keystone_glue::CurrentlyInstalledVersion())); | 221 Version(UTF16ToASCII(keystone_glue::CurrentlyInstalledVersion())); |
| 107 #elif defined(OS_POSIX) | 222 #elif defined(OS_POSIX) |
| 108 // POSIX but not Mac OS X: Linux, etc. | 223 // POSIX but not Mac OS X: Linux, etc. |
| 109 CommandLine command_line(*CommandLine::ForCurrentProcess()); | 224 CommandLine command_line(*CommandLine::ForCurrentProcess()); |
| 110 command_line.AppendSwitch(switches::kProductVersion); | 225 command_line.AppendSwitch(switches::kProductVersion); |
| 111 std::string reply; | 226 std::string reply; |
| 112 if (!base::GetAppOutput(command_line, &reply)) { | 227 if (!base::GetAppOutput(command_line, &reply)) { |
| 113 DLOG(ERROR) << "Failed to get current file version"; | 228 DLOG(ERROR) << "Failed to get current file version"; |
| 114 return; | 229 return; |
| 115 } | 230 } |
| 116 | 231 |
| 117 installed_version = Version(reply); | 232 installed_version = Version(reply); |
| 118 #endif | 233 #endif |
| 119 | 234 |
| 120 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); | 235 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); |
| 121 *is_unstable_channel = channel == chrome::VersionInfo::CHANNEL_DEV || | 236 bool is_unstable_channel = channel == chrome::VersionInfo::CHANNEL_DEV || |
| 122 channel == chrome::VersionInfo::CHANNEL_CANARY; | 237 channel == chrome::VersionInfo::CHANNEL_CANARY; |
| 123 | 238 |
| 124 // Get the version of the currently *running* instance of Chrome. | 239 // Get the version of the currently *running* instance of Chrome. |
| 125 chrome::VersionInfo version_info; | 240 chrome::VersionInfo version_info; |
| 126 if (!version_info.is_valid()) { | 241 if (!version_info.is_valid()) { |
| 127 NOTREACHED() << "Failed to get current file version"; | 242 NOTREACHED() << "Failed to get current file version"; |
| 128 return; | 243 return; |
| 129 } | 244 } |
| 130 Version running_version(version_info.Version()); | 245 Version running_version(version_info.Version()); |
| 131 if (!running_version.IsValid()) { | 246 if (!running_version.IsValid()) { |
| 132 NOTREACHED(); | 247 NOTREACHED(); |
| 133 return; | 248 return; |
| 134 } | 249 } |
| 135 | 250 |
| 136 // |installed_version| may be NULL when the user downgrades on Linux (by | 251 // |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 | 252 // switching from dev to beta channel, for example). The user needs a |
| 138 // restart in this case as well. See http://crbug.com/46547 | 253 // restart in this case as well. See http://crbug.com/46547 |
| 139 if (!installed_version.IsValid() || | 254 if (!installed_version.IsValid() || |
| 140 (installed_version.CompareTo(running_version) > 0)) { | 255 (installed_version.CompareTo(running_version) > 0)) { |
| 141 // If a more recent version is available, it might be that we are lacking | 256 // If a more recent version is available, it might be that we are lacking |
| 142 // a critical update, such as a zero-day fix. | 257 // a critical update, such as a zero-day fix. |
| 143 *is_critical_upgrade = | 258 bool critical = critical_update.IsValid() && |
| 144 critical_update.IsValid() && | 259 critical_update.CompareTo(running_version) > 0; |
| 145 (critical_update.CompareTo(running_version) > 0); | |
| 146 | 260 |
| 147 // Fire off the upgrade detected task. | 261 // Fire off the upgrade detected task. |
| 148 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 262 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 149 upgrade_detected_task); | 263 base::Bind(&UpgradeDetectorImpl::UpgradeDetected, |
| 264 upgrade_detector, critical, is_unstable_channel)); | |
| 150 } | 265 } |
| 151 } | 266 } |
| 152 | 267 |
| 153 } // namespace | 268 void UpgradeDetectorImpl::CheckForUpgrade() { |
| 269 // Only check for outdated install when allowed and when no upgrade has been | |
| 270 // detected. | |
| 271 if (is_outdated_reinstall_allowed_ && | |
|
Finnur
2013/02/01 17:09:15
This would be a field trial check instead (accordi
MAD
2013/02/02 03:55:34
Done.
| |
| 272 upgrade_required_ == UPGRADE_REQUIRED_NONE) { | |
| 273 CompareBuildTimeToSaneTime(); | |
| 274 } | |
| 154 | 275 |
| 155 UpgradeDetectorImpl::UpgradeDetectorImpl() | 276 // If we are simulating an outdated install, no need to detect an upgrade. |
| 156 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 277 // Since upgrade has precedence over outdated installs, it would prevent |
| 157 is_unstable_channel_(false) { | 278 // testing. And [critical] upgrade simulations should never get here, they go |
| 158 CommandLine command_line(*CommandLine::ForCurrentProcess()); | 279 // straight to UpgtradeDetected. |
| 159 if (command_line.HasSwitch(switches::kDisableBackgroundNetworking)) | 280 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); |
| 281 DCHECK(!cmd_line.HasSwitch(switches::kSimulateCriticalUpdate)); | |
| 282 DCHECK(!cmd_line.HasSwitch(switches::kSimulateUpgrade)); | |
| 283 if (cmd_line.HasSwitch(switches::kSimulateOutdated)) | |
| 160 return; | 284 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 | 285 |
| 180 UpgradeDetectorImpl::~UpgradeDetectorImpl() { | |
| 181 } | |
| 182 | |
| 183 void UpgradeDetectorImpl::CheckForUpgrade() { | |
| 184 weak_factory_.InvalidateWeakPtrs(); | 286 weak_factory_.InvalidateWeakPtrs(); |
| 185 base::Closure callback_task = | |
| 186 base::Bind(&UpgradeDetectorImpl::UpgradeDetected, | |
| 187 weak_factory_.GetWeakPtr()); | |
| 188 // We use FILE as the thread to run the upgrade detection code on all | 287 // 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 | 288 // 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 | 289 // while launching a background process and reading its output; on the Mac and |
| 191 // on Windows checking for an upgrade requires reading a file. | 290 // on Windows checking for an upgrade requires reading a file. |
| 192 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 291 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 193 base::Bind(&DetectUpgradeTask, | 292 base::Bind(&UpgradeDetectorImpl::DetectUpgradeTask, |
| 194 callback_task, | 293 weak_factory_.GetWeakPtr())); |
| 195 &is_unstable_channel_, | |
| 196 &is_critical_upgrade_)); | |
| 197 } | 294 } |
| 198 | 295 |
| 199 void UpgradeDetectorImpl::UpgradeDetected() { | 296 void UpgradeDetectorImpl::CompareBuildTimeToSaneTime() { |
| 297 DCHECK(is_outdated_reinstall_allowed_); | |
| 298 // Check again, in case we got posted more than once by CheckForUpgrade. | |
| 299 if (upgrade_required_ != UPGRADE_REQUIRED_NONE) | |
| 300 return; | |
| 301 | |
| 302 base::Time network_time; | |
| 303 // TODO(mad): Uncomment when variations service NetworkTime CL get committed. | |
| 304 // if (!g_browser_process->variations_service()->GetNetworkTime(&network_time)) | |
| 305 // return; | |
|
Finnur
2013/02/01 17:09:15
I likey.
MAD
2013/02/02 03:55:34
B-)
| |
| 306 | |
|
Finnur
2013/02/01 17:09:15
I think you should add a check, just to be sure. S
MAD
2013/02/02 03:55:34
Done.
| |
| 307 DCHECK(!network_time.is_null()); | |
|
Finnur
2013/02/01 17:09:15
You could pull that into the if above and check th
MAD
2013/02/02 03:55:34
Done.
| |
| 308 if (network_time - build_date_ > | |
| 309 base::TimeDelta::FromDays(kOutdatedBuildAgeInDays)) { | |
| 310 upgrade_required_ = UPGRADE_REQUIRED_OUTDATED; | |
| 311 UpgradeDetected(false, is_unstable_channel_); | |
| 312 } | |
| 313 } | |
| 314 | |
| 315 void UpgradeDetectorImpl::UpgradeDetected(bool critical, | |
| 316 bool is_unstable_channel) { | |
| 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 318 is_unstable_channel_ = is_unstable_channel; | |
| 319 if (critical) | |
| 320 upgrade_required_ = UPGRADE_REQUIRED_CRITICAL; | |
| 321 else if (upgrade_required_ == UPGRADE_REQUIRED_NONE) | |
|
Finnur
2013/02/01 17:09:15
I'm sure that a few months down the line we're goi
MAD
2013/02/02 03:55:34
Refactoring took care of this.
| |
| 322 upgrade_required_ = UPGRADE_REQUIRED_REGULAR; | |
| 201 | 323 |
| 202 // Stop the recurring timer (that is checking for changes). | 324 // Stop the recurring timer (that is checking for changes). |
| 203 detect_upgrade_timer_.Stop(); | 325 detect_upgrade_timer_.Stop(); |
| 204 | 326 |
| 205 NotifyUpgradeDetected(); | 327 NotifyUpgradeDetected(); |
| 206 | 328 |
| 207 // Start the repeating timer for notifying the user after a certain period. | 329 // 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 | 330 // The called function will eventually figure out that enough time has passed |
| 209 // and stop the timer. | 331 // and stop the timer. |
| 210 int cycle_time = IsTesting() ? | 332 int cycle_time = IsTesting() ? |
| 211 kNotifyCycleTimeForTestingMs : kNotifyCycleTimeMs; | 333 kNotifyCycleTimeForTestingMs : kNotifyCycleTimeMs; |
| 212 upgrade_notification_timer_.Start(FROM_HERE, | 334 upgrade_notification_timer_.Start(FROM_HERE, |
| 213 base::TimeDelta::FromMilliseconds(cycle_time), | 335 base::TimeDelta::FromMilliseconds(cycle_time), |
| 214 this, &UpgradeDetectorImpl::NotifyOnUpgrade); | 336 this, &UpgradeDetectorImpl::NotifyOnUpgrade); |
| 215 } | 337 } |
| 216 | 338 |
| 217 void UpgradeDetectorImpl::NotifyOnUpgrade() { | 339 void UpgradeDetectorImpl::NotifyOnUpgrade() { |
| 218 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time(); | 340 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time(); |
| 219 | 341 |
| 220 // We'll make testing more convenient by switching to seconds of waiting | 342 // We'll make testing more convenient by switching to seconds of waiting |
| 221 // instead of days between flipping severity. | 343 // instead of days between flipping severity. |
| 222 bool is_testing = IsTesting(); | 344 bool is_testing = IsTesting(); |
| 223 int64 time_passed = is_testing ? delta.InSeconds() : delta.InHours(); | 345 int64 time_passed = is_testing ? delta.InSeconds() : delta.InHours(); |
| 224 | 346 |
| 347 bool is_critical_or_outdated = upgrade_required_ > UPGRADE_REQUIRED_REGULAR; | |
| 225 if (is_unstable_channel_) { | 348 if (is_unstable_channel_) { |
| 226 // There's only one threat level for unstable channels like dev and | 349 // 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 | 350 // canary, and it hits after one hour. During testing, it hits after one |
| 228 // minute. | 351 // minute. |
| 229 const int kUnstableThreshold = 1; | 352 const int kUnstableThreshold = 1; |
| 230 | 353 |
| 231 if (is_critical_upgrade_) | 354 if (is_critical_or_outdated) |
| 232 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_CRITICAL); | 355 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_CRITICAL); |
| 233 else if (time_passed >= kUnstableThreshold) { | 356 else if (time_passed >= kUnstableThreshold) { |
| 234 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); | 357 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); |
| 235 | 358 |
| 236 // That's as high as it goes. | 359 // That's as high as it goes. |
| 237 upgrade_notification_timer_.Stop(); | 360 upgrade_notification_timer_.Stop(); |
| 238 } else { | 361 } else { |
| 239 return; // Not ready to recommend upgrade. | 362 return; // Not ready to recommend upgrade. |
| 240 } | 363 } |
| 241 } else { | 364 } else { |
| 242 const int kMultiplier = is_testing ? 1 : 24; | 365 const int kMultiplier = is_testing ? 1 : 24; |
| 243 // 14 days when not testing, otherwise 14 seconds. | 366 // 14 days when not testing, otherwise 14 seconds. |
| 244 const int kSevereThreshold = 14 * kMultiplier; | 367 const int kSevereThreshold = 14 * kMultiplier; |
| 245 const int kHighThreshold = 7 * kMultiplier; | 368 const int kHighThreshold = 7 * kMultiplier; |
| 246 const int kElevatedThreshold = 4 * kMultiplier; | 369 const int kElevatedThreshold = 4 * kMultiplier; |
| 247 const int kLowThreshold = 2 * kMultiplier; | 370 const int kLowThreshold = 2 * kMultiplier; |
| 248 | 371 |
| 249 // These if statements must be sorted (highest interval first). | 372 // These if statements must be sorted (highest interval first). |
| 250 if (time_passed >= kSevereThreshold || is_critical_upgrade_) { | 373 if (time_passed >= kSevereThreshold || is_critical_or_outdated) { |
| 251 set_upgrade_notification_stage( | 374 set_upgrade_notification_stage( |
| 252 is_critical_upgrade_ ? UPGRADE_ANNOYANCE_CRITICAL : | 375 is_critical_or_outdated ? UPGRADE_ANNOYANCE_CRITICAL : |
| 253 UPGRADE_ANNOYANCE_SEVERE); | 376 UPGRADE_ANNOYANCE_SEVERE); |
| 254 | 377 |
| 255 // We can't get any higher, baby. | 378 // We can't get any higher, baby. |
| 256 upgrade_notification_timer_.Stop(); | 379 upgrade_notification_timer_.Stop(); |
| 257 } else if (time_passed >= kHighThreshold) { | 380 } else if (time_passed >= kHighThreshold) { |
| 258 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH); | 381 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH); |
| 259 } else if (time_passed >= kElevatedThreshold) { | 382 } else if (time_passed >= kElevatedThreshold) { |
| 260 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED); | 383 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED); |
| 261 } else if (time_passed >= kLowThreshold) { | 384 } else if (time_passed >= kLowThreshold) { |
| 262 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); | 385 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); |
| 263 } else { | 386 } else { |
| 264 return; // Not ready to recommend upgrade. | 387 return; // Not ready to recommend upgrade. |
| 265 } | 388 } |
| 266 } | 389 } |
| 267 | 390 |
| 268 NotifyUpgradeRecommended(); | 391 NotifyUpgradeRecommended(); |
| 269 } | 392 } |
| 270 | 393 |
| 271 // static | 394 // static |
| 272 UpgradeDetectorImpl* UpgradeDetectorImpl::GetInstance() { | 395 UpgradeDetectorImpl* UpgradeDetectorImpl::GetInstance() { |
| 273 return Singleton<UpgradeDetectorImpl>::get(); | 396 return Singleton<UpgradeDetectorImpl>::get(); |
| 274 } | 397 } |
| 275 | 398 |
| 276 // static | 399 // static |
| 277 UpgradeDetector* UpgradeDetector::GetInstance() { | 400 UpgradeDetector* UpgradeDetector::GetInstance() { |
| 278 return UpgradeDetectorImpl::GetInstance(); | 401 return UpgradeDetectorImpl::GetInstance(); |
| 279 } | 402 } |
| OLD | NEW |