This source file includes following definitions.
- CmdLineInterval
- SimulatingOutdated
- IsTesting
- GetCheckForUpgradeEveryMs
- IsUnstableChannel
- CheckForUnstableChannel
- IsSystemInstall
- DetectUpdatability
- build_date_
- DetectUpgradeTask
- StartTimerForUpgradeCheck
- CheckForUpgrade
- DetectOutdatedInstall
- UpgradeDetected
- NotifyOnUpgrade
- GetInstance
- GetInstance
#include "chrome/browser/upgrade_detector_impl.h"
#include <string>
#include "base/bind.h"
#include "base/build_time.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "base/path_service.h"
#include "base/prefs/pref_service.h"
#include "base/process/launch.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/version.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/google/google_util.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/resource/resource_bundle.h"
#if defined(OS_WIN)
#include "base/win/win_util.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/google_update_settings.h"
#include "chrome/installer/util/helper.h"
#include "chrome/installer/util/install_util.h"
#elif defined(OS_MACOSX)
#include "chrome/browser/mac/keystone_glue.h"
#endif
using content::BrowserThread;
namespace {
const int kCheckForUpgradeMs = 2 * 60 * 60 * 1000;
const int kNotifyCycleTimeMs = 20 * 60 * 1000;
const int kNotifyCycleTimeForTestingMs = 500;
const uint64 kOutdatedBuildAgeInDays = 12 * 7;
std::string CmdLineInterval() {
const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
return cmd_line.GetSwitchValueASCII(switches::kCheckForUpdateIntervalSec);
}
bool SimulatingOutdated() {
const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
return cmd_line.HasSwitch(switches::kSimulateOutdated) ||
cmd_line.HasSwitch(switches::kSimulateOutdatedNoAU);
}
bool IsTesting() {
const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
return cmd_line.HasSwitch(switches::kSimulateUpgrade) ||
cmd_line.HasSwitch(switches::kCheckForUpdateIntervalSec) ||
cmd_line.HasSwitch(switches::kSimulateCriticalUpdate) ||
SimulatingOutdated();
}
int GetCheckForUpgradeEveryMs() {
int interval_ms;
std::string interval = CmdLineInterval();
if (!interval.empty() && base::StringToInt(interval, &interval_ms))
return interval_ms * 1000;
return kCheckForUpgradeMs;
}
bool IsUnstableChannel() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
return channel == chrome::VersionInfo::CHANNEL_DEV ||
channel == chrome::VersionInfo::CHANNEL_CANARY;
}
void CheckForUnstableChannel(const base::Closure& callback_task,
bool* is_unstable_channel) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
*is_unstable_channel = IsUnstableChannel();
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback_task);
}
#if defined(OS_WIN)
bool IsSystemInstall() {
base::FilePath exe_path;
if (!PathService::Get(base::DIR_EXE, &exe_path)) {
NOTREACHED() << "Failed to find executable path";
return false;
}
return !InstallUtil::IsPerUserInstall(exe_path.value().c_str());
}
void DetectUpdatability(const base::Closure& callback_task,
bool* is_unstable_channel,
bool* is_auto_update_enabled) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
base::string16 app_guid = installer::GetAppGuidForUpdates(IsSystemInstall());
DCHECK(!app_guid.empty());
if (is_auto_update_enabled) {
*is_auto_update_enabled =
GoogleUpdateSettings::AreAutoupdatesEnabled(app_guid);
}
if (!base::win::IsEnrolledToDomain())
CheckForUnstableChannel(callback_task, is_unstable_channel);
}
#endif
}
UpgradeDetectorImpl::UpgradeDetectorImpl()
: weak_factory_(this),
is_unstable_channel_(false),
is_auto_update_enabled_(true),
build_date_(base::GetBuildTime()) {
CommandLine command_line(*CommandLine::ForCurrentProcess());
if (command_line.HasSwitch(switches::kDisableBackgroundNetworking))
return;
if (command_line.HasSwitch(switches::kSimulateUpgrade)) {
UpgradeDetected(UPGRADE_AVAILABLE_REGULAR);
return;
}
if (command_line.HasSwitch(switches::kSimulateCriticalUpdate)) {
UpgradeDetected(UPGRADE_AVAILABLE_CRITICAL);
return;
}
if (SimulatingOutdated()) {
std::string switch_name;
if (command_line.HasSwitch(switches::kSimulateOutdatedNoAU)) {
is_auto_update_enabled_ = false;
switch_name = switches::kSimulateOutdatedNoAU;
} else {
switch_name = switches::kSimulateOutdated;
}
std::string build_date = command_line.GetSwitchValueASCII(switch_name);
base::Time maybe_build_time;
bool result = base::Time::FromString(build_date.c_str(), &maybe_build_time);
if (result && !maybe_build_time.is_null()) {
build_date_ = maybe_build_time;
StartTimerForUpgradeCheck();
} else {
UpgradeDetected(
is_auto_update_enabled_ ? UPGRADE_NEEDED_OUTDATED_INSTALL
: UPGRADE_NEEDED_OUTDATED_INSTALL_NO_AU);
}
return;
}
base::Closure start_upgrade_check_timer_task =
base::Bind(&UpgradeDetectorImpl::StartTimerForUpgradeCheck,
weak_factory_.GetWeakPtr());
#if defined(OS_WIN)
#if defined(GOOGLE_CHROME_BUILD)
bool attempted_enabling_autoupdate = g_browser_process->local_state() &&
g_browser_process->local_state()->GetBoolean(
prefs::kAttemptedToEnableAutoupdate);
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&DetectUpdatability,
start_upgrade_check_timer_task,
&is_unstable_channel_,
attempted_enabling_autoupdate ?
NULL : &is_auto_update_enabled_));
#endif
#else
#if defined(OS_MACOSX)
if (!keystone_glue::KeystoneEnabled()) {
is_auto_update_enabled_ = false;
return;
}
#elif defined(OS_POSIX)
#else
return;
#endif
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&CheckForUnstableChannel,
start_upgrade_check_timer_task,
&is_unstable_channel_));
#endif
network_time_tracker_.Start();
}
UpgradeDetectorImpl::~UpgradeDetectorImpl() {
}
void UpgradeDetectorImpl::DetectUpgradeTask(
base::WeakPtr<UpgradeDetectorImpl> upgrade_detector) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
Version installed_version;
Version critical_update;
#if defined(OS_WIN)
bool system_install = IsSystemInstall();
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(base::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 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)) {
UpgradeAvailable upgrade_available = UPGRADE_AVAILABLE_REGULAR;
if (critical_update.IsValid() &&
critical_update.CompareTo(running_version) > 0) {
upgrade_available = UPGRADE_AVAILABLE_CRITICAL;
}
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&UpgradeDetectorImpl::UpgradeDetected,
upgrade_detector,
upgrade_available));
}
}
void UpgradeDetectorImpl::StartTimerForUpgradeCheck() {
detect_upgrade_timer_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(GetCheckForUpgradeEveryMs()),
this, &UpgradeDetectorImpl::CheckForUpgrade);
}
void UpgradeDetectorImpl::CheckForUpgrade() {
weak_factory_.InvalidateWeakPtrs();
if (DetectOutdatedInstall())
return;
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&UpgradeDetectorImpl::DetectUpgradeTask,
weak_factory_.GetWeakPtr()));
}
bool UpgradeDetectorImpl::DetectOutdatedInstall() {
static bool simulate_outdated = SimulatingOutdated();
if (!simulate_outdated) {
std::string brand;
if (google_util::GetBrand(&brand) && !google_util::IsOrganic(brand))
return false;
}
base::Time network_time;
base::TimeDelta uncertainty;
if (!network_time_tracker_.GetNetworkTime(base::TimeTicks::Now(),
&network_time,
&uncertainty)) {
network_time = base::Time::Now();
}
if (network_time.is_null() || build_date_.is_null() ||
build_date_ > network_time) {
NOTREACHED();
return false;
}
if (network_time - build_date_ >
base::TimeDelta::FromDays(kOutdatedBuildAgeInDays)) {
UpgradeDetected(is_auto_update_enabled_ ?
UPGRADE_NEEDED_OUTDATED_INSTALL :
UPGRADE_NEEDED_OUTDATED_INSTALL_NO_AU);
return true;
}
return simulate_outdated;
}
void UpgradeDetectorImpl::UpgradeDetected(UpgradeAvailable upgrade_available) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
upgrade_available_ = upgrade_available;
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();
bool is_critical_or_outdated = upgrade_available_ > UPGRADE_AVAILABLE_REGULAR;
if (is_unstable_channel_) {
const int kUnstableThreshold = 1;
if (is_critical_or_outdated)
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 ? 10 : 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_or_outdated) {
set_upgrade_notification_stage(
is_critical_or_outdated ? 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();
}