This source file includes following definitions.
- ShouldUseGAIAProfileInfo
- NeedsProfilePicture
- GetDesiredImageSideLength
- GetBrowserProfile
- GetCachedPictureURL
- OnProfileDownloadSuccess
- OnProfileDownloadFailure
- OnUsernameChanged
- Shutdown
- ScheduleNextUpdate
- GoogleSigninSucceeded
- GoogleSignedOut
#include "chrome/browser/profiles/gaia_info_update_service.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_info_cache.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/profile_management_switches.h"
#include "content/public/browser/notification_details.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/image/image.h"
namespace {
const int kUpdateIntervalHours = 24;
const int kMinUpdateIntervalSeconds = 5;
}
GAIAInfoUpdateService::GAIAInfoUpdateService(Profile* profile)
: profile_(profile) {
SigninManagerBase* signin_manager =
SigninManagerFactory::GetForProfile(profile_);
signin_manager->AddObserver(this);
PrefService* prefs = profile_->GetPrefs();
last_updated_ = base::Time::FromInternalValue(
prefs->GetInt64(prefs::kProfileGAIAInfoUpdateTime));
ScheduleNextUpdate();
}
GAIAInfoUpdateService::~GAIAInfoUpdateService() {
DCHECK(!profile_) << "Shutdown not called before dtor";
}
void GAIAInfoUpdateService::Update() {
SigninManagerBase* signin_manager =
SigninManagerFactory::GetForProfile(profile_);
if (signin_manager->GetAuthenticatedAccountId().empty())
return;
if (profile_image_downloader_)
return;
profile_image_downloader_.reset(new ProfileDownloader(this));
profile_image_downloader_->Start();
}
bool GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(Profile* profile) {
#if defined(OS_CHROMEOS)
return false;
#endif
if (!profile->GetOriginalProfile()->IsSyncAccessible())
return false;
if (switches::IsGoogleProfileInfo())
return true;
return false;
}
bool GAIAInfoUpdateService::NeedsProfilePicture() const {
return true;
}
int GAIAInfoUpdateService::GetDesiredImageSideLength() const {
return 256;
}
Profile* GAIAInfoUpdateService::GetBrowserProfile() {
return profile_;
}
std::string GAIAInfoUpdateService::GetCachedPictureURL() const {
return profile_->GetPrefs()->GetString(prefs::kProfileGAIAInfoPictureURL);
}
void GAIAInfoUpdateService::OnProfileDownloadSuccess(
ProfileDownloader* downloader) {
scoped_ptr<ProfileDownloader> profile_image_downloader(
profile_image_downloader_.release());
last_updated_ = base::Time::Now();
profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime,
last_updated_.ToInternalValue());
ScheduleNextUpdate();
base::string16 full_name = downloader->GetProfileFullName();
base::string16 given_name = downloader->GetProfileGivenName();
SkBitmap bitmap = downloader->GetProfilePicture();
ProfileDownloader::PictureStatus picture_status =
downloader->GetProfilePictureStatus();
std::string picture_url = downloader->GetProfilePictureURL();
ProfileInfoCache& cache =
g_browser_process->profile_manager()->GetProfileInfoCache();
size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
if (profile_index == std::string::npos)
return;
cache.SetGAIANameOfProfileAtIndex(profile_index, full_name);
cache.SetGAIAGivenNameOfProfileAtIndex(profile_index, given_name);
profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
if (profile_index == std::string::npos)
return;
if (picture_status == ProfileDownloader::PICTURE_SUCCESS) {
profile_->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
picture_url);
gfx::Image gfx_image = gfx::Image::CreateFrom1xBitmap(bitmap);
cache.SetGAIAPictureOfProfileAtIndex(profile_index, &gfx_image);
} else if (picture_status == ProfileDownloader::PICTURE_DEFAULT) {
cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL);
}
profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, true);
}
void GAIAInfoUpdateService::OnProfileDownloadFailure(
ProfileDownloader* downloader,
ProfileDownloaderDelegate::FailureReason reason) {
profile_image_downloader_.reset();
last_updated_ = base::Time::Now();
profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime,
last_updated_.ToInternalValue());
ScheduleNextUpdate();
}
void GAIAInfoUpdateService::OnUsernameChanged(const std::string& username) {
ProfileInfoCache& cache =
g_browser_process->profile_manager()->GetProfileInfoCache();
size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
if (profile_index == std::string::npos)
return;
if (username.empty()) {
cache.SetGAIANameOfProfileAtIndex(profile_index, base::string16());
profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
if (profile_index == std::string::npos)
return;
cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL);
profile_->GetPrefs()->ClearPref(prefs::kProfileGAIAInfoPictureURL);
} else {
Update();
}
}
void GAIAInfoUpdateService::Shutdown() {
timer_.Stop();
profile_image_downloader_.reset();
SigninManagerBase* signin_manager =
SigninManagerFactory::GetForProfile(profile_);
signin_manager->RemoveObserver(this);
profile_ = NULL;
}
void GAIAInfoUpdateService::ScheduleNextUpdate() {
if (timer_.IsRunning())
return;
const base::TimeDelta desired_delta =
base::TimeDelta::FromHours(kUpdateIntervalHours);
const base::TimeDelta update_delta = base::Time::Now() - last_updated_;
base::TimeDelta delta;
if (update_delta < base::TimeDelta() || update_delta > desired_delta)
delta = base::TimeDelta::FromSeconds(kMinUpdateIntervalSeconds);
else
delta = desired_delta - update_delta;
timer_.Start(FROM_HERE, delta, this, &GAIAInfoUpdateService::Update);
}
void GAIAInfoUpdateService::GoogleSigninSucceeded(
const std::string& username,
const std::string& password) {
OnUsernameChanged(username);
}
void GAIAInfoUpdateService::GoogleSignedOut(const std::string& username) {
OnUsernameChanged(std::string());
}