blob: 570275e391ba59c8452c3de6d6861f7f4ead8e04 [file] [log] [blame]
[email protected]049ed6d2012-01-25 11:40:421// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ceff8402011-06-12 23:27:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/upgrade_detector_impl.h"
6
7#include <string>
8
[email protected]6198f7982011-11-17 21:24:379#include "base/bind.h"
[email protected]ceff8402011-06-12 23:27:1210#include "base/command_line.h"
[email protected]4b473742011-11-08 11:58:4211#include "base/file_path.h"
[email protected]ceff8402011-06-12 23:27:1212#include "base/memory/scoped_ptr.h"
13#include "base/memory/singleton.h"
[email protected]4b473742011-11-08 11:58:4214#include "base/path_service.h"
[email protected]ceff8402011-06-12 23:27:1215#include "base/string_number_conversions.h"
16#include "base/string_util.h"
[email protected]ceff8402011-06-12 23:27:1217#include "base/time.h"
18#include "base/utf_string_conversions.h"
[email protected]ceff8402011-06-12 23:27:1219#include "chrome/common/chrome_switches.h"
20#include "chrome/common/chrome_version_info.h"
21#include "chrome/installer/util/browser_distribution.h"
[email protected]c38831a12011-10-28 12:44:4922#include "content/public/browser/browser_thread.h"
[email protected]ceff8402011-06-12 23:27:1223#include "ui/base/resource/resource_bundle.h"
24
25#if defined(OS_WIN)
26#include "chrome/installer/util/install_util.h"
27#elif defined(OS_MACOSX)
[email protected]5950f5712011-06-20 22:15:5228#include "chrome/browser/mac/keystone_glue.h"
[email protected]ceff8402011-06-12 23:27:1229#elif defined(OS_POSIX)
30#include "base/process_util.h"
31#include "base/version.h"
32#endif
33
[email protected]631bb742011-11-02 11:29:3934using content::BrowserThread;
35
[email protected]ceff8402011-06-12 23:27:1236namespace {
37
38// How long (in milliseconds) to wait (each cycle) before checking whether
39// Chrome's been upgraded behind our back.
40const int kCheckForUpgradeMs = 2 * 60 * 60 * 1000; // 2 hours.
41
42// 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.
44const int kNotifyCycleTimeMs = 20 * 60 * 1000; // 20 minutes.
45
46// Same as kNotifyCycleTimeMs but only used during testing.
47const int kNotifyCycleTimeForTestingMs = 500; // Half a second.
48
49std::string CmdLineInterval() {
50 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
51 return cmd_line.GetSwitchValueASCII(switches::kCheckForUpdateIntervalSec);
52}
53
[email protected]049ed6d2012-01-25 11:40:4254bool IsTesting() {
55 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
56 return cmd_line.HasSwitch(switches::kSimulateUpgrade) ||
57 cmd_line.HasSwitch(switches::kCheckForUpdateIntervalSec);
58}
59
[email protected]ceff8402011-06-12 23:27:1260// How often to check for an upgrade.
61int GetCheckForUpgradeEveryMs() {
62 // Check for a value passed via the command line.
63 int interval_ms;
64 std::string interval = CmdLineInterval();
65 if (!interval.empty() && base::StringToInt(interval, &interval_ms))
66 return interval_ms * 1000; // Command line value is in seconds.
67
68 return kCheckForUpgradeMs;
69}
70
71// This task checks the currently running version of Chrome against the
72// installed version. If the installed version is newer, it runs the passed
73// callback task. Otherwise it just deletes the task.
[email protected]689949452011-12-08 02:36:3974void DetectUpgradeTask(const base::Closure& upgrade_detected_task,
75 bool* is_unstable_channel,
76 bool* is_critical_upgrade) {
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
[email protected]ceff8402011-06-12 23:27:1278
[email protected]12126d372012-07-11 18:40:5379 Version installed_version;
80 Version critical_update;
[email protected]ceff8402011-06-12 23:27:1281
82#if defined(OS_WIN)
[email protected]689949452011-12-08 02:36:3983 // Get the version of the currently *installed* instance of Chrome,
84 // which might be newer than the *running* instance if we have been
85 // upgraded in the background.
86 FilePath exe_path;
87 if (!PathService::Get(base::DIR_EXE, &exe_path)) {
88 NOTREACHED() << "Failed to find executable path";
89 return;
[email protected]ceff8402011-06-12 23:27:1290 }
91
[email protected]689949452011-12-08 02:36:3992 bool system_install =
93 !InstallUtil::IsPerUserInstall(exe_path.value().c_str());
94
95 // TODO(tommi): Check if using the default distribution is always the right
96 // thing to do.
97 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
[email protected]12126d372012-07-11 18:40:5398 InstallUtil::GetChromeVersion(dist, system_install, &installed_version);
[email protected]689949452011-12-08 02:36:3999
[email protected]12126d372012-07-11 18:40:53100 if (installed_version.IsValid()) {
101 InstallUtil::GetCriticalUpdateVersion(dist, system_install,
102 &critical_update);
[email protected]689949452011-12-08 02:36:39103 }
104#elif defined(OS_MACOSX)
[email protected]12126d372012-07-11 18:40:53105 installed_version =
106 Version(UTF16ToASCII(keystone_glue::CurrentlyInstalledVersion()));
[email protected]689949452011-12-08 02:36:39107#elif defined(OS_POSIX)
108 // POSIX but not Mac OS X: Linux, etc.
109 CommandLine command_line(*CommandLine::ForCurrentProcess());
110 command_line.AppendSwitch(switches::kProductVersion);
111 std::string reply;
112 if (!base::GetAppOutput(command_line, &reply)) {
113 DLOG(ERROR) << "Failed to get current file version";
114 return;
115 }
116
[email protected]12126d372012-07-11 18:40:53117 installed_version = Version(reply);
[email protected]689949452011-12-08 02:36:39118#endif
119
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.
125 chrome::VersionInfo version_info;
126 if (!version_info.is_valid()) {
127 NOTREACHED() << "Failed to get current file version";
128 return;
129 }
[email protected]12126d372012-07-11 18:40:53130 Version running_version(version_info.Version());
131 if (!running_version.IsValid()) {
132 NOTREACHED();
[email protected]689949452011-12-08 02:36:39133 return;
134 }
135
136 // |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
138 // restart in this case as well. See http://crbug.com/46547
[email protected]12126d372012-07-11 18:40:53139 if (!installed_version.IsValid() ||
140 (installed_version.CompareTo(running_version) > 0)) {
[email protected]689949452011-12-08 02:36:39141 // If a more recent version is available, it might be that we are lacking
142 // a critical update, such as a zero-day fix.
143 *is_critical_upgrade =
[email protected]12126d372012-07-11 18:40:53144 critical_update.IsValid() &&
145 (critical_update.CompareTo(running_version) > 0);
[email protected]689949452011-12-08 02:36:39146
147 // Fire off the upgrade detected task.
148 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
149 upgrade_detected_task);
150 }
151}
[email protected]ceff8402011-06-12 23:27:12152
153} // namespace
154
155UpgradeDetectorImpl::UpgradeDetectorImpl()
[email protected]6198f7982011-11-17 21:24:37156 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
[email protected]ceff8402011-06-12 23:27:12157 is_unstable_channel_(false) {
158 CommandLine command_line(*CommandLine::ForCurrentProcess());
159 if (command_line.HasSwitch(switches::kDisableBackgroundNetworking))
160 return;
[email protected]049ed6d2012-01-25 11:40:42161 if (command_line.HasSwitch(switches::kSimulateUpgrade)) {
162 UpgradeDetected();
163 return;
164 }
[email protected]ceff8402011-06-12 23:27:12165 // 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 {
[email protected]d323a172011-09-02 18:23:02173 detect_upgrade_timer_.Start(FROM_HERE,
[email protected]ceff8402011-06-12 23:27:12174 base::TimeDelta::FromMilliseconds(GetCheckForUpgradeEveryMs()),
175 this, &UpgradeDetectorImpl::CheckForUpgrade);
176 }
177#endif
178}
179
180UpgradeDetectorImpl::~UpgradeDetectorImpl() {
181}
182
183void UpgradeDetectorImpl::CheckForUpgrade() {
[email protected]6198f7982011-11-17 21:24:37184 weak_factory_.InvalidateWeakPtrs();
185 base::Closure callback_task =
186 base::Bind(&UpgradeDetectorImpl::UpgradeDetected,
187 weak_factory_.GetWeakPtr());
[email protected]ceff8402011-06-12 23:27:12188 // 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
190 // while launching a background process and reading its output; on the Mac and
191 // on Windows checking for an upgrade requires reading a file.
192 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
[email protected]689949452011-12-08 02:36:39193 base::Bind(&DetectUpgradeTask,
194 callback_task,
195 &is_unstable_channel_,
196 &is_critical_upgrade_));
[email protected]ceff8402011-06-12 23:27:12197}
198
199void UpgradeDetectorImpl::UpgradeDetected() {
200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
201
202 // Stop the recurring timer (that is checking for changes).
203 detect_upgrade_timer_.Stop();
204
205 NotifyUpgradeDetected();
206
207 // 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
209 // and stop the timer.
[email protected]049ed6d2012-01-25 11:40:42210 int cycle_time = IsTesting() ?
211 kNotifyCycleTimeForTestingMs : kNotifyCycleTimeMs;
[email protected]d323a172011-09-02 18:23:02212 upgrade_notification_timer_.Start(FROM_HERE,
[email protected]ceff8402011-06-12 23:27:12213 base::TimeDelta::FromMilliseconds(cycle_time),
214 this, &UpgradeDetectorImpl::NotifyOnUpgrade);
215}
216
217void UpgradeDetectorImpl::NotifyOnUpgrade() {
218 base::TimeDelta delta = base::Time::Now() - upgrade_detected_time();
[email protected]ceff8402011-06-12 23:27:12219
[email protected]049ed6d2012-01-25 11:40:42220 // We'll make testing more convenient by switching to seconds of waiting
221 // instead of days between flipping severity.
222 bool is_testing = IsTesting();
223 int64 time_passed = is_testing ? delta.InSeconds() : delta.InHours();
[email protected]ceff8402011-06-12 23:27:12224
225 if (is_unstable_channel_) {
226 // 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
228 // minute.
229 const int kUnstableThreshold = 1;
230
[email protected]f1c76b9f2011-10-13 13:43:44231 if (is_critical_upgrade_)
232 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_CRITICAL);
233 else if (time_passed >= kUnstableThreshold) {
[email protected]ceff8402011-06-12 23:27:12234 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW);
235
236 // That's as high as it goes.
237 upgrade_notification_timer_.Stop();
238 } else {
239 return; // Not ready to recommend upgrade.
240 }
241 } else {
[email protected]049ed6d2012-01-25 11:40:42242 const int kMultiplier = is_testing ? 1 : 24;
[email protected]ceff8402011-06-12 23:27:12243 // 14 days when not testing, otherwise 14 seconds.
244 const int kSevereThreshold = 14 * kMultiplier;
245 const int kHighThreshold = 7 * kMultiplier;
246 const int kElevatedThreshold = 4 * kMultiplier;
247 const int kLowThreshold = 2 * kMultiplier;
248
249 // These if statements must be sorted (highest interval first).
[email protected]f1c76b9f2011-10-13 13:43:44250 if (time_passed >= kSevereThreshold || is_critical_upgrade_) {
251 set_upgrade_notification_stage(
252 is_critical_upgrade_ ? UPGRADE_ANNOYANCE_CRITICAL :
253 UPGRADE_ANNOYANCE_SEVERE);
[email protected]ceff8402011-06-12 23:27:12254
255 // We can't get any higher, baby.
256 upgrade_notification_timer_.Stop();
257 } else if (time_passed >= kHighThreshold) {
258 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH);
259 } else if (time_passed >= kElevatedThreshold) {
260 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED);
261 } else if (time_passed >= kLowThreshold) {
262 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW);
263 } else {
264 return; // Not ready to recommend upgrade.
265 }
266 }
267
268 NotifyUpgradeRecommended();
269}
270
271// static
272UpgradeDetectorImpl* UpgradeDetectorImpl::GetInstance() {
273 return Singleton<UpgradeDetectorImpl>::get();
274}
275
276// static
277UpgradeDetector* UpgradeDetector::GetInstance() {
278 return UpgradeDetectorImpl::GetInstance();
279}