Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: chrome/browser/upgrade_detector_impl.cc

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

Powered by Google App Engine
This is Rietveld 408576698