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

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: Sync'd 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"
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";
Finnur 2013/02/05 10:52:19 I assume you've filed this an experiment request w
MAD 2013/02/05 16:49:16 Not yet, but I do know about this, I sit with the
Finnur 2013/02/05 22:14:50 Hah! :)
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 #if defined(OS_WIN)
93 bool IsSystemInstall() {
94 FilePath exe_path;
95 if (!PathService::Get(base::DIR_EXE, &exe_path)) {
96 NOTREACHED() << "Failed to find executable path";
97 return false;
98 }
99
100 return !InstallUtil::IsPerUserInstall(exe_path.value().c_str());
101 }
102
103 // This task checks the update policy and calls back the task only if automatic
104 // updates are allowed. It also identifies whether we are running an unstable
105 // version.
Finnur 2013/02/04 21:30:17 s/version/channel/
MAD 2013/02/05 16:49:16 Done.
106 void DetectUpdatability(const base::Closure& callback_task,
107 bool* is_unstable_channel) {
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
109 *is_unstable_channel = IsUnstableChannel();
110
111 string16 app_guid = installer::GetAppGuidForUpdates(IsSystemInstall());
112 DCHECK(!app_guid.empty());
113 if (GoogleUpdateSettings::AUTOMATIC_UPDATES ==
114 GoogleUpdateSettings::GetAppUpdatePolicy(app_guid, NULL)) {
115 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback_task);
Finnur 2013/02/05 10:52:19 I think a direct call to ... CheckForUnstableChan
MAD 2013/02/05 16:49:16 Done.
116 }
117 }
118 #endif // defined(OS_WIN)
119
120 // This task identifies whether we are running an unstable version. And then
121 // it unconditionnally calls back the provided task.
Finnur 2013/02/04 21:30:17 no double n in unconditionally.
MAD 2013/02/05 16:49:16 Done.
122 void CheckForUnstableChannel(const base::Closure& callback_task,
123 bool* is_unstable_channel) {
124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
125 *is_unstable_channel = IsUnstableChannel();
126 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback_task);
127 }
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 std::string build_date = command_line.GetSwitchValueASCII(
159 switches::kSimulateOutdated);
160 base::Time maybe_build_time;
161 bool result = base::Time::FromString(build_date.c_str(), &maybe_build_time);
162 if (result && !maybe_build_time.is_null()) {
163 // We got a valid build date simulation so use it and check for upgrades.
164 build_date_ = maybe_build_time;
165 StartTimerForUpgradeCheck();
166 } else {
167 // Without a valid date, we simulate that we are already outdated...
168 UpgradeDetected(UPGRADE_NEEDED_OUTDATED_INSTALL);
169 }
170 return;
171 }
172
173 // Windows: only enable upgrade notifications for official builds.
174 // Mac: only enable them if the updater (Keystone) is present.
175 // Linux (and other POSIX): always enable regardless of branding.
176 base::Closure start_upgrade_check_timer_task =
177 base::Bind(&UpgradeDetectorImpl::StartTimerForUpgradeCheck,
178 weak_factory_.GetWeakPtr());
179 #if defined(OS_WINDOW) && defined(GOOGLE_CHROME_BUILD)
180 // On Windows, there might be a policy preventing updates, so validate
181 // updatability, and then call StartTimerForUpgradeCheck appropriately.
182 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
183 base::Bind(&DetectUpdatability,
184 start_upgrade_check_timer_task,
185 &is_unstable_channel_));
186 return;
187 #elif defined(OS_WINDOW) && !defined(GOOGLE_CHROME_BUILD)
188 return; // Chromium has no upgrade channel.
189 #elif defined(OS_MACOSX)
190 if (!keystone_glue::KeystoneEnabled())
191 return; // Keystone updater not enabled.
192 #elif !defined(OS_POSIX)
193 return;
194 #endif
195
196 // Check whether the build is an unstable channel before starting the timer.
197 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
198 base::Bind(&CheckForUnstableChannel,
199 start_upgrade_check_timer_task,
200 &is_unstable_channel_));
201 }
202
203 UpgradeDetectorImpl::~UpgradeDetectorImpl() {
204 }
205
206 // Static
71 // This task checks the currently running version of Chrome against the 207 // This task checks the currently running version of Chrome against the
72 // installed version. If the installed version is newer, it runs the passed 208 // installed version. If the installed version is newer, it calls back
73 // callback task. Otherwise it just deletes the task. 209 // UpgradeDetectorImpl::UpgradeDetected using |upgrade_detector|.
74 void DetectUpgradeTask(const base::Closure& upgrade_detected_task, 210 void UpgradeDetectorImpl::DetectUpgradeTask(
75 bool* is_unstable_channel, 211 base::WeakPtr<UpgradeDetectorImpl> upgrade_detector) {
76 bool* is_critical_upgrade) {
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
78 213
79 Version installed_version; 214 Version installed_version;
80 Version critical_update; 215 Version critical_update;
81 216
82 #if defined(OS_WIN) 217 #if defined(OS_WIN)
83 // Get the version of the currently *installed* instance of Chrome, 218 // Get the version of the currently *installed* instance of Chrome,
84 // which might be newer than the *running* instance if we have been 219 // which might be newer than the *running* instance if we have been
85 // upgraded in the background. 220 // upgraded in the background.
86 FilePath exe_path; 221 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 222
95 // TODO(tommi): Check if using the default distribution is always the right 223 // TODO(tommi): Check if using the default distribution is always the right
96 // thing to do. 224 // thing to do.
97 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 225 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
98 InstallUtil::GetChromeVersion(dist, system_install, &installed_version); 226 InstallUtil::GetChromeVersion(dist, system_install, &installed_version);
99 227
100 if (installed_version.IsValid()) { 228 if (installed_version.IsValid()) {
101 InstallUtil::GetCriticalUpdateVersion(dist, system_install, 229 InstallUtil::GetCriticalUpdateVersion(dist, system_install,
102 &critical_update); 230 &critical_update);
103 } 231 }
104 #elif defined(OS_MACOSX) 232 #elif defined(OS_MACOSX)
105 installed_version = 233 installed_version =
106 Version(UTF16ToASCII(keystone_glue::CurrentlyInstalledVersion())); 234 Version(UTF16ToASCII(keystone_glue::CurrentlyInstalledVersion()));
107 #elif defined(OS_POSIX) 235 #elif defined(OS_POSIX)
108 // POSIX but not Mac OS X: Linux, etc. 236 // POSIX but not Mac OS X: Linux, etc.
109 CommandLine command_line(*CommandLine::ForCurrentProcess()); 237 CommandLine command_line(*CommandLine::ForCurrentProcess());
110 command_line.AppendSwitch(switches::kProductVersion); 238 command_line.AppendSwitch(switches::kProductVersion);
111 std::string reply; 239 std::string reply;
112 if (!base::GetAppOutput(command_line, &reply)) { 240 if (!base::GetAppOutput(command_line, &reply)) {
113 DLOG(ERROR) << "Failed to get current file version"; 241 DLOG(ERROR) << "Failed to get current file version";
114 return; 242 return;
115 } 243 }
116 244
117 installed_version = Version(reply); 245 installed_version = Version(reply);
118 #endif 246 #endif
119 247
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. 248 // Get the version of the currently *running* instance of Chrome.
125 chrome::VersionInfo version_info; 249 chrome::VersionInfo version_info;
126 if (!version_info.is_valid()) { 250 if (!version_info.is_valid()) {
127 NOTREACHED() << "Failed to get current file version"; 251 NOTREACHED() << "Failed to get current file version";
128 return; 252 return;
129 } 253 }
130 Version running_version(version_info.Version()); 254 Version running_version(version_info.Version());
131 if (!running_version.IsValid()) { 255 if (!running_version.IsValid()) {
132 NOTREACHED(); 256 NOTREACHED();
133 return; 257 return;
134 } 258 }
135 259
136 // |installed_version| may be NULL when the user downgrades on Linux (by 260 // |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 261 // switching from dev to beta channel, for example). The user needs a
138 // restart in this case as well. See http://crbug.com/46547 262 // restart in this case as well. See http://crbug.com/46547
139 if (!installed_version.IsValid() || 263 if (!installed_version.IsValid() ||
140 (installed_version.CompareTo(running_version) > 0)) { 264 (installed_version.CompareTo(running_version) > 0)) {
141 // If a more recent version is available, it might be that we are lacking 265 // If a more recent version is available, it might be that we are lacking
142 // a critical update, such as a zero-day fix. 266 // a critical update, such as a zero-day fix.
143 *is_critical_upgrade = 267 UpgradeAvailable upgrade_available = UPGRADE_AVAILABLE_REGULAR;
144 critical_update.IsValid() && 268 if (critical_update.IsValid() &&
145 (critical_update.CompareTo(running_version) > 0); 269 critical_update.CompareTo(running_version) > 0) {
270 upgrade_available = UPGRADE_AVAILABLE_CRITICAL;
271 }
146 272
147 // Fire off the upgrade detected task. 273 // Fire off the upgrade detected task.
148 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 274 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
149 upgrade_detected_task); 275 base::Bind(&UpgradeDetectorImpl::UpgradeDetected,
276 upgrade_detector,
277 upgrade_available));
150 } 278 }
151 } 279 }
152 280
153 } // namespace 281 void UpgradeDetectorImpl::StartTimerForUpgradeCheck() {
154 282 detect_upgrade_timer_.Start(FROM_HERE,
155 UpgradeDetectorImpl::UpgradeDetectorImpl() 283 base::TimeDelta::FromMilliseconds(GetCheckForUpgradeEveryMs()),
156 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), 284 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 } 285 }
182 286
183 void UpgradeDetectorImpl::CheckForUpgrade() { 287 void UpgradeDetectorImpl::CheckForUpgrade() {
288 // Interrupt any (unlikely) unfinished business.
Finnur 2013/02/05 10:52:19 Can you expand a bit on this comment (meaning: tha
MAD 2013/02/05 16:49:16 Done.
184 weak_factory_.InvalidateWeakPtrs(); 289 weak_factory_.InvalidateWeakPtrs();
185 base::Closure callback_task = 290
186 base::Bind(&UpgradeDetectorImpl::UpgradeDetected, 291 // No need to look for upgrades if the install is outdated.
187 weak_factory_.GetWeakPtr()); 292 if (CheckForOutdatedInstall())
293 return;
294
188 // We use FILE as the thread to run the upgrade detection code on all 295 // 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 296 // 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 297 // while launching a background process and reading its output; on the Mac and
191 // on Windows checking for an upgrade requires reading a file. 298 // on Windows checking for an upgrade requires reading a file.
192 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, 299 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
193 base::Bind(&DetectUpgradeTask, 300 base::Bind(&UpgradeDetectorImpl::DetectUpgradeTask,
194 callback_task, 301 weak_factory_.GetWeakPtr()));
195 &is_unstable_channel_,
196 &is_critical_upgrade_));
197 } 302 }
198 303
199 void UpgradeDetectorImpl::UpgradeDetected() { 304 bool UpgradeDetectorImpl::CheckForOutdatedInstall() {
305 // Only enable the outdated install check if we are running the trial for it,
306 // unless we are simulating an outdated isntall.
307 static bool simulate_outdated = CommandLine::ForCurrentProcess()->HasSwitch(
308 switches::kSimulateOutdated);
309 if (base::FieldTrialList::FindFullName(kOutdatedInstallCheckTrialName) !=
310 kOutdatedInstallCheck12WeeksGroupName && !simulate_outdated) {
311 return false;
312 }
313
314 base::Time network_time(base::Time::Now());
315 // TODO(mad): Uncomment when variations service NetworkTime CL gets committed.
316 // if (!g_browser_process->variations_service()->GetNetworkTime(&network_time))
317 // return;
318
319 if (network_time.is_null() || build_date_.is_null() ||
320 build_date_ > network_time) {
321 NOTREACHED();
322 return false;
323 }
324
325 if (network_time - build_date_ >
326 base::TimeDelta::FromDays(kOutdatedBuildAgeInDays)) {
327 UpgradeDetected(UPGRADE_NEEDED_OUTDATED_INSTALL);
Finnur 2013/02/05 10:52:19 You can move this line to line 293 without adverse
MAD 2013/02/05 16:49:16 OK, sorry for not being clear, but this won't work
Finnur 2013/02/05 22:14:50 Oh, I see. Good point. I forgot about the simulate
328 return true;
329 }
330 // If we simlated an outdated install with a date, we don't want to keep
Finnur 2013/02/05 10:52:19 I think this is correct, but just to be sure: We w
MAD 2013/02/05 16:49:16 It is possible to get here when we have no current
Finnur 2013/02/05 22:14:50 Yes, my question was more along the lines of "I'm
331 // checking for version upgrades, which happens on non-official builds.
332 return simulate_outdated;
333 }
334
335 void UpgradeDetectorImpl::UpgradeDetected(UpgradeAvailable upgrade_available) {
200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
337 upgrade_available_ = upgrade_available;
201 338
202 // Stop the recurring timer (that is checking for changes). 339 // Stop the recurring timer (that is checking for changes).
203 detect_upgrade_timer_.Stop(); 340 detect_upgrade_timer_.Stop();
204 341
205 NotifyUpgradeDetected(); 342 NotifyUpgradeDetected();
206 343
207 // Start the repeating timer for notifying the user after a certain period. 344 // 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 345 // The called function will eventually figure out that enough time has passed
209 // and stop the timer. 346 // and stop the timer.
210 int cycle_time = IsTesting() ? 347 int cycle_time = IsTesting() ?
211 kNotifyCycleTimeForTestingMs : kNotifyCycleTimeMs; 348 kNotifyCycleTimeForTestingMs : kNotifyCycleTimeMs;
212 upgrade_notification_timer_.Start(FROM_HERE, 349 upgrade_notification_timer_.Start(FROM_HERE,
213 base::TimeDelta::FromMilliseconds(cycle_time), 350 base::TimeDelta::FromMilliseconds(cycle_time),
214 this, &UpgradeDetectorImpl::NotifyOnUpgrade); 351 this, &UpgradeDetectorImpl::NotifyOnUpgrade);
215 } 352 }
216 353
217 void UpgradeDetectorImpl::NotifyOnUpgrade() { 354 void UpgradeDetectorImpl::NotifyOnUpgrade() {
218 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time(); 355 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time();
219 356
220 // We'll make testing more convenient by switching to seconds of waiting 357 // We'll make testing more convenient by switching to seconds of waiting
221 // instead of days between flipping severity. 358 // instead of days between flipping severity.
222 bool is_testing = IsTesting(); 359 bool is_testing = IsTesting();
223 int64 time_passed = is_testing ? delta.InSeconds() : delta.InHours(); 360 int64 time_passed = is_testing ? delta.InSeconds() : delta.InHours();
224 361
362 bool is_critical_or_outdated = upgrade_available_ > UPGRADE_AVAILABLE_REGULAR;
225 if (is_unstable_channel_) { 363 if (is_unstable_channel_) {
226 // There's only one threat level for unstable channels like dev and 364 // 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 365 // canary, and it hits after one hour. During testing, it hits after one
228 // minute. 366 // minute.
229 const int kUnstableThreshold = 1; 367 const int kUnstableThreshold = 1;
230 368
231 if (is_critical_upgrade_) 369 if (is_critical_or_outdated)
232 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_CRITICAL); 370 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_CRITICAL);
233 else if (time_passed >= kUnstableThreshold) { 371 else if (time_passed >= kUnstableThreshold) {
234 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); 372 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW);
235 373
236 // That's as high as it goes. 374 // That's as high as it goes.
237 upgrade_notification_timer_.Stop(); 375 upgrade_notification_timer_.Stop();
238 } else { 376 } else {
239 return; // Not ready to recommend upgrade. 377 return; // Not ready to recommend upgrade.
240 } 378 }
241 } else { 379 } else {
242 const int kMultiplier = is_testing ? 1 : 24; 380 const int kMultiplier = is_testing ? 1 : 24;
243 // 14 days when not testing, otherwise 14 seconds. 381 // 14 days when not testing, otherwise 14 seconds.
244 const int kSevereThreshold = 14 * kMultiplier; 382 const int kSevereThreshold = 14 * kMultiplier;
245 const int kHighThreshold = 7 * kMultiplier; 383 const int kHighThreshold = 7 * kMultiplier;
246 const int kElevatedThreshold = 4 * kMultiplier; 384 const int kElevatedThreshold = 4 * kMultiplier;
247 const int kLowThreshold = 2 * kMultiplier; 385 const int kLowThreshold = 2 * kMultiplier;
248 386
249 // These if statements must be sorted (highest interval first). 387 // These if statements must be sorted (highest interval first).
250 if (time_passed >= kSevereThreshold || is_critical_upgrade_) { 388 if (time_passed >= kSevereThreshold || is_critical_or_outdated) {
251 set_upgrade_notification_stage( 389 set_upgrade_notification_stage(
252 is_critical_upgrade_ ? UPGRADE_ANNOYANCE_CRITICAL : 390 is_critical_or_outdated ? UPGRADE_ANNOYANCE_CRITICAL :
253 UPGRADE_ANNOYANCE_SEVERE); 391 UPGRADE_ANNOYANCE_SEVERE);
254 392
255 // We can't get any higher, baby. 393 // We can't get any higher, baby.
256 upgrade_notification_timer_.Stop(); 394 upgrade_notification_timer_.Stop();
257 } else if (time_passed >= kHighThreshold) { 395 } else if (time_passed >= kHighThreshold) {
258 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH); 396 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH);
259 } else if (time_passed >= kElevatedThreshold) { 397 } else if (time_passed >= kElevatedThreshold) {
260 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED); 398 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED);
261 } else if (time_passed >= kLowThreshold) { 399 } else if (time_passed >= kLowThreshold) {
262 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW); 400 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW);
263 } else { 401 } else {
264 return; // Not ready to recommend upgrade. 402 return; // Not ready to recommend upgrade.
265 } 403 }
266 } 404 }
267 405
268 NotifyUpgradeRecommended(); 406 NotifyUpgradeRecommended();
269 } 407 }
270 408
271 // static 409 // static
272 UpgradeDetectorImpl* UpgradeDetectorImpl::GetInstance() { 410 UpgradeDetectorImpl* UpgradeDetectorImpl::GetInstance() {
273 return Singleton<UpgradeDetectorImpl>::get(); 411 return Singleton<UpgradeDetectorImpl>::get();
274 } 412 }
275 413
276 // static 414 // static
277 UpgradeDetector* UpgradeDetector::GetInstance() { 415 UpgradeDetector* UpgradeDetector::GetInstance() {
278 return UpgradeDetectorImpl::GetInstance(); 416 return UpgradeDetectorImpl::GetInstance();
279 } 417 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698