#include "chrome/browser/upgrade_detector_impl.h"
#include <string>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "base/path_service.h"
#include "base/string_number_conversions.h"
#include "base/string_util.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/installer/util/browser_distribution.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/resource/resource_bundle.h"
#if defined(OS_WIN)
#include "chrome/installer/util/install_util.h"
#elif defined(OS_MACOSX)
#include "chrome/browser/mac/keystone_glue.h"
#elif defined(OS_POSIX)
#include "base/process_util.h"
#include "base/version.h"
#endif
using content::BrowserThread;
namespace {
const int kCheckForUpgradeMs = 2 * 60 * 60 * 1000;
const int kNotifyCycleTimeMs = 20 * 60 * 1000;
const int kNotifyCycleTimeForTestingMs = 500;
std::string CmdLineInterval() {
const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
return cmd_line.GetSwitchValueASCII(switches::kCheckForUpdateIntervalSec);
}
bool IsTesting() {
const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
return cmd_line.HasSwitch(switches::kSimulateUpgrade) ||
cmd_line.HasSwitch(switches::kCheckForUpdateIntervalSec);
}
int GetCheckForUpgradeEveryMs() {
int interval_ms;
std::string interval = CmdLineInterval();
if (!interval.empty() && base::StringToInt(interval, &interval_ms))
return interval_ms * 1000;
return kCheckForUpgradeMs;
}
void DetectUpgradeTask(const base::Closure& upgrade_detected_task,
bool* is_unstable_channel,
bool* is_critical_upgrade) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
Version installed_version;
Version critical_update;
#if defined(OS_WIN)
FilePath exe_path;
if (!PathService::Get(base::DIR_EXE, &exe_path)) {
NOTREACHED() << "Failed to find executable path";
return;
}
bool system_install =
!InstallUtil::IsPerUserInstall(exe_path.value().c_str());
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
InstallUtil::GetChromeVersion(dist, system_install, &installed_version);
if (installed_version.IsValid()) {
InstallUtil::GetCriticalUpdateVersion(dist, system_install,
&critical_update);
}
#elif defined(OS_MACOSX)
installed_version =
Version(UTF16ToASCII(keystone_glue::CurrentlyInstalledVersion()));
#elif defined(OS_POSIX)
CommandLine command_line(*CommandLine::ForCurrentProcess());
command_line.AppendSwitch(switches::kProductVersion);
std::string reply;
if (!base::GetAppOutput(command_line, &reply)) {
DLOG(ERROR) << "Failed to get current file version";
return;
}
installed_version = Version(reply);
#endif
chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
*is_unstable_channel = channel == chrome::VersionInfo::CHANNEL_DEV ||
channel == chrome::VersionInfo::CHANNEL_CANARY;
chrome::VersionInfo version_info;
if (!version_info.is_valid()) {
NOTREACHED() << "Failed to get current file version";
return;
}
Version running_version(version_info.Version());
if (!running_version.IsValid()) {
NOTREACHED();
return;
}
if (!installed_version.IsValid() ||
(installed_version.CompareTo(running_version) > 0)) {
*is_critical_upgrade =
critical_update.IsValid() &&
(critical_update.CompareTo(running_version) > 0);
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
upgrade_detected_task);
}
}
}
UpgradeDetectorImpl::UpgradeDetectorImpl()
: ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
is_unstable_channel_(false) {
CommandLine command_line(*CommandLine::ForCurrentProcess());
if (command_line.HasSwitch(switches::kDisableBackgroundNetworking))
return;
if (command_line.HasSwitch(switches::kSimulateUpgrade)) {
UpgradeDetected();
return;
}
#if (defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)) || defined(OS_POSIX)
#if defined(OS_MACOSX)
if (keystone_glue::KeystoneEnabled())
#endif
{
detect_upgrade_timer_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(GetCheckForUpgradeEveryMs()),
this, &UpgradeDetectorImpl::CheckForUpgrade);
}
#endif
}
UpgradeDetectorImpl::~UpgradeDetectorImpl() {
}
void UpgradeDetectorImpl::CheckForUpgrade() {
weak_factory_.InvalidateWeakPtrs();
base::Closure callback_task =
base::Bind(&UpgradeDetectorImpl::UpgradeDetected,
weak_factory_.GetWeakPtr());
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&DetectUpgradeTask,
callback_task,
&is_unstable_channel_,
&is_critical_upgrade_));
}
void UpgradeDetectorImpl::UpgradeDetected() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
detect_upgrade_timer_.Stop();
NotifyUpgradeDetected();
int cycle_time = IsTesting() ?
kNotifyCycleTimeForTestingMs : kNotifyCycleTimeMs;
upgrade_notification_timer_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(cycle_time),
this, &UpgradeDetectorImpl::NotifyOnUpgrade);
}
void UpgradeDetectorImpl::NotifyOnUpgrade() {
base::TimeDelta delta = base::Time::Now() - upgrade_detected_time();
bool is_testing = IsTesting();
int64 time_passed = is_testing ? delta.InSeconds() : delta.InHours();
if (is_unstable_channel_) {
const int kUnstableThreshold = 1;
if (is_critical_upgrade_)
set_upgrade_notification_stage(UPGRADE_ANNOYANCE_CRITICAL);
else if (time_passed >= kUnstableThreshold) {
set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW);
upgrade_notification_timer_.Stop();
} else {
return;
}
} else {
const int kMultiplier = is_testing ? 1 : 24;
const int kSevereThreshold = 14 * kMultiplier;
const int kHighThreshold = 7 * kMultiplier;
const int kElevatedThreshold = 4 * kMultiplier;
const int kLowThreshold = 2 * kMultiplier;
if (time_passed >= kSevereThreshold || is_critical_upgrade_) {
set_upgrade_notification_stage(
is_critical_upgrade_ ? UPGRADE_ANNOYANCE_CRITICAL :
UPGRADE_ANNOYANCE_SEVERE);
upgrade_notification_timer_.Stop();
} else if (time_passed >= kHighThreshold) {
set_upgrade_notification_stage(UPGRADE_ANNOYANCE_HIGH);
} else if (time_passed >= kElevatedThreshold) {
set_upgrade_notification_stage(UPGRADE_ANNOYANCE_ELEVATED);
} else if (time_passed >= kLowThreshold) {
set_upgrade_notification_stage(UPGRADE_ANNOYANCE_LOW);
} else {
return;
}
}
NotifyUpgradeRecommended();
}
UpgradeDetectorImpl* UpgradeDetectorImpl::GetInstance() {
return Singleton<UpgradeDetectorImpl>::get();
}
UpgradeDetector* UpgradeDetector::GetInstance() {
return UpgradeDetectorImpl::GetInstance();
}