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
3 // found in the LICENSE file.
5 #include "chrome/browser/upgrade_detector_impl.h"
10 #include "base/command_line.h"
11 #include "base/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/singleton.h"
14 #include "base/path_service.h"
15 #include "base/string_number_conversions.h"
16 #include "base/string_util.h"
17 #include "base/time.h"
18 #include "base/utf_string_conversions.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/chrome_version_info.h"
21 #include "chrome/installer/util/browser_distribution.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "ui/base/resource/resource_bundle.h"
26 #include "chrome/installer/util/install_util.h"
27 #elif defined(OS_MACOSX)
28 #include "chrome/browser/mac/keystone_glue.h"
29 #elif defined(OS_POSIX)
30 #include "base/process_util.h"
31 #include "base/version.h"
34 using content::BrowserThread
;
38 // How long (in milliseconds) to wait (each cycle) before checking whether
39 // Chrome's been upgraded behind our back.
40 const int kCheckForUpgradeMs
= 2 * 60 * 60 * 1000; // 2 hours.
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.
44 const int kNotifyCycleTimeMs
= 20 * 60 * 1000; // 20 minutes.
46 // Same as kNotifyCycleTimeMs but only used during testing.
47 const int kNotifyCycleTimeForTestingMs
= 500; // Half a second.
49 std::string
CmdLineInterval() {
50 const CommandLine
& cmd_line
= *CommandLine::ForCurrentProcess();
51 return cmd_line
.GetSwitchValueASCII(switches::kCheckForUpdateIntervalSec
);
55 const CommandLine
& cmd_line
= *CommandLine::ForCurrentProcess();
56 return cmd_line
.HasSwitch(switches::kSimulateUpgrade
) ||
57 cmd_line
.HasSwitch(switches::kCheckForUpdateIntervalSec
);
60 // How often to check for an upgrade.
61 int GetCheckForUpgradeEveryMs() {
62 // Check for a value passed via the command line.
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.
68 return kCheckForUpgradeMs
;
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.
74 void DetectUpgradeTask(const base::Closure
& upgrade_detected_task
,
75 bool* is_unstable_channel
,
76 bool* is_critical_upgrade
) {
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
79 scoped_ptr
<Version
> installed_version
;
80 scoped_ptr
<Version
> critical_update
;
83 // 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.
87 if (!PathService::Get(base::DIR_EXE
, &exe_path
)) {
88 NOTREACHED() << "Failed to find executable path";
93 !InstallUtil::IsPerUserInstall(exe_path
.value().c_str());
95 // TODO(tommi): Check if using the default distribution is always the right
97 BrowserDistribution
* dist
= BrowserDistribution::GetDistribution();
98 installed_version
.reset(InstallUtil::GetChromeVersion(dist
,
101 if (installed_version
.get()) {
102 critical_update
.reset(
103 InstallUtil::GetCriticalUpdateVersion(dist
, system_install
));
105 #elif defined(OS_MACOSX)
106 installed_version
.reset(
107 Version::GetVersionFromString(UTF16ToASCII(
108 keystone_glue::CurrentlyInstalledVersion())));
109 #elif defined(OS_POSIX)
110 // POSIX but not Mac OS X: Linux, etc.
111 CommandLine
command_line(*CommandLine::ForCurrentProcess());
112 command_line
.AppendSwitch(switches::kProductVersion
);
114 if (!base::GetAppOutput(command_line
, &reply
)) {
115 DLOG(ERROR
) << "Failed to get current file version";
119 installed_version
.reset(Version::GetVersionFromString(reply
));
122 chrome::VersionInfo::Channel channel
= chrome::VersionInfo::GetChannel();
123 *is_unstable_channel
= channel
== chrome::VersionInfo::CHANNEL_DEV
||
124 channel
== chrome::VersionInfo::CHANNEL_CANARY
;
126 // Get the version of the currently *running* instance of Chrome.
127 chrome::VersionInfo version_info
;
128 if (!version_info
.is_valid()) {
129 NOTREACHED() << "Failed to get current file version";
132 scoped_ptr
<Version
> running_version(
133 Version::GetVersionFromString(version_info
.Version()));
134 if (running_version
.get() == NULL
) {
135 NOTREACHED() << "Failed to parse version info";
139 // |installed_version| may be NULL when the user downgrades on Linux (by
140 // switching from dev to beta channel, for example). The user needs a
141 // restart in this case as well. See http://crbug.com/46547
142 if (!installed_version
.get() ||
143 (installed_version
->CompareTo(*running_version
) > 0)) {
144 // If a more recent version is available, it might be that we are lacking
145 // a critical update, such as a zero-day fix.
146 *is_critical_upgrade
=
147 critical_update
.get() &&
148 (critical_update
->CompareTo(*running_version
) > 0);
150 // Fire off the upgrade detected task.
151 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
152 upgrade_detected_task
);
158 UpgradeDetectorImpl::UpgradeDetectorImpl()
159 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
160 is_unstable_channel_(false) {
161 CommandLine
command_line(*CommandLine::ForCurrentProcess());
162 if (command_line
.HasSwitch(switches::kDisableBackgroundNetworking
))
164 if (command_line
.HasSwitch(switches::kSimulateUpgrade
)) {
168 // Windows: only enable upgrade notifications for official builds.
169 // Mac: only enable them if the updater (Keystone) is present.
170 // Linux (and other POSIX): always enable regardless of branding.
171 #if (defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)) || defined(OS_POSIX)
172 #if defined(OS_MACOSX)
173 if (keystone_glue::KeystoneEnabled())
176 detect_upgrade_timer_
.Start(FROM_HERE
,
177 base::TimeDelta::FromMilliseconds(GetCheckForUpgradeEveryMs()),
178 this, &UpgradeDetectorImpl::CheckForUpgrade
);
183 UpgradeDetectorImpl::~UpgradeDetectorImpl() {
186 void UpgradeDetectorImpl::CheckForUpgrade() {
187 weak_factory_
.InvalidateWeakPtrs();
188 base::Closure callback_task
=
189 base::Bind(&UpgradeDetectorImpl::UpgradeDetected
,
190 weak_factory_
.GetWeakPtr());
191 // We use FILE as the thread to run the upgrade detection code on all
192 // platforms. For Linux, this is because we don't want to block the UI thread
193 // while launching a background process and reading its output; on the Mac and
194 // on Windows checking for an upgrade requires reading a file.
195 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE
,
196 base::Bind(&DetectUpgradeTask
,
198 &is_unstable_channel_
,
199 &is_critical_upgrade_
));
202 void UpgradeDetectorImpl::UpgradeDetected() {
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
205 // Stop the recurring timer (that is checking for changes).
206 detect_upgrade_timer_
.Stop();
208 NotifyUpgradeDetected();
210 // Start the repeating timer for notifying the user after a certain period.
211 // The called function will eventually figure out that enough time has passed
212 // and stop the timer.
213 int cycle_time
= IsTesting() ?
214 kNotifyCycleTimeForTestingMs
: kNotifyCycleTimeMs
;
215 upgrade_notification_timer_
.Start(FROM_HERE
,
216 base::TimeDelta::FromMilliseconds(cycle_time
),
217 this, &UpgradeDetectorImpl::NotifyOnUpgrade
);
220 void UpgradeDetectorImpl::NotifyOnUpgrade() {
221 base::TimeDelta delta
= base::Time::Now() - upgrade_detected_time();
223 // We'll make testing more convenient by switching to seconds of waiting
224 // instead of days between flipping severity.
225 bool is_testing
= IsTesting();
226 int64 time_passed
= is_testing
? delta
.InSeconds() : delta
.InHours();
228 if (is_unstable_channel_
) {
229 // There's only one threat level for unstable channels like dev and
230 // canary, and it hits after one hour. During testing, it hits after one
232 const int kUnstableThreshold
= 1;
234 if (is_critical_upgrade_
)
235 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_CRITICAL
);
236 else if (time_passed
>= kUnstableThreshold
) {
237 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW
);
239 // That's as high as it goes.
240 upgrade_notification_timer_
.Stop();
242 return; // Not ready to recommend upgrade.
245 const int kMultiplier
= is_testing
? 1 : 24;
246 // 14 days when not testing, otherwise 14 seconds.
247 const int kSevereThreshold
= 14 * kMultiplier
;
248 const int kHighThreshold
= 7 * kMultiplier
;
249 const int kElevatedThreshold
= 4 * kMultiplier
;
250 const int kLowThreshold
= 2 * kMultiplier
;
252 // These if statements must be sorted (highest interval first).
253 if (time_passed
>= kSevereThreshold
|| is_critical_upgrade_
) {
254 set_upgrade_notification_stage(
255 is_critical_upgrade_
? UPGRADE_ANNOYANCE_CRITICAL
:
256 UPGRADE_ANNOYANCE_SEVERE
);
258 // We can't get any higher, baby.
259 upgrade_notification_timer_
.Stop();
260 } else if (time_passed
>= kHighThreshold
) {
261 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH
);
262 } else if (time_passed
>= kElevatedThreshold
) {
263 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED
);
264 } else if (time_passed
>= kLowThreshold
) {
265 set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW
);
267 return; // Not ready to recommend upgrade.
271 NotifyUpgradeRecommended();
275 UpgradeDetectorImpl
* UpgradeDetectorImpl::GetInstance() {
276 return Singleton
<UpgradeDetectorImpl
>::get();
280 UpgradeDetector
* UpgradeDetector::GetInstance() {
281 return UpgradeDetectorImpl::GetInstance();