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

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

Powered by Google App Engine
This is Rietveld 408576698