Home | History | Annotate | Download | only in profiles
      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.
      4 
      5 #include "chrome/browser/profiles/gaia_info_update_service.h"
      6 
      7 #include "base/command_line.h"
      8 #include "base/prefs/pref_service.h"
      9 #include "chrome/browser/browser_process.h"
     10 #include "chrome/browser/chrome_notification_types.h"
     11 #include "chrome/browser/profiles/profile.h"
     12 #include "chrome/browser/profiles/profile_info_cache.h"
     13 #include "chrome/browser/profiles/profile_manager.h"
     14 #include "chrome/browser/sync/profile_sync_service.h"
     15 #include "chrome/common/chrome_switches.h"
     16 #include "chrome/common/pref_names.h"
     17 #include "content/public/browser/notification_details.h"
     18 #include "third_party/skia/include/core/SkBitmap.h"
     19 #include "ui/gfx/image/image.h"
     20 
     21 namespace {
     22 
     23 // Update the user's GAIA info every 24 hours.
     24 const int kUpdateIntervalHours = 24;
     25 
     26 // If the users's GAIA info is very out of date then wait at least this long
     27 // before starting an update. This avoids slowdown during startup.
     28 const int kMinUpdateIntervalSeconds = 5;
     29 
     30 } // namespace
     31 
     32 GAIAInfoUpdateService::GAIAInfoUpdateService(Profile* profile)
     33     : profile_(profile) {
     34   PrefService* prefs = profile_->GetPrefs();
     35   username_pref_.Init(prefs::kGoogleServicesUsername, prefs,
     36                       base::Bind(&GAIAInfoUpdateService::OnUsernameChanged,
     37                                  base::Unretained(this)));
     38 
     39   last_updated_ = base::Time::FromInternalValue(
     40       prefs->GetInt64(prefs::kProfileGAIAInfoUpdateTime));
     41   ScheduleNextUpdate();
     42 }
     43 
     44 GAIAInfoUpdateService::~GAIAInfoUpdateService() {
     45 }
     46 
     47 void GAIAInfoUpdateService::Update() {
     48   // The user must be logged in.
     49   std::string username = profile_->GetPrefs()->GetString(
     50       prefs::kGoogleServicesUsername);
     51   if (username.empty())
     52     return;
     53 
     54   if (profile_image_downloader_)
     55     return;
     56   profile_image_downloader_.reset(new ProfileDownloader(this));
     57   profile_image_downloader_->Start();
     58 }
     59 
     60 // static
     61 bool GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(Profile* profile) {
     62 #if defined(OS_CHROMEOS)
     63   return false;
     64 #endif
     65 
     66   // Sync must be allowed.
     67   if (!profile->GetOriginalProfile()->IsSyncAccessible())
     68     return false;
     69 
     70   // To enable this feature for testing pass "--gaia-profile-info".
     71   if (CommandLine::ForCurrentProcess()->HasSwitch(
     72           switches::kGaiaProfileInfo)) {
     73     return true;
     74   }
     75 
     76   // This feature is disable by default.
     77   return false;
     78 }
     79 
     80 bool GAIAInfoUpdateService::NeedsProfilePicture() const {
     81   return true;
     82 }
     83 
     84 int GAIAInfoUpdateService::GetDesiredImageSideLength() const {
     85   return 256;
     86 }
     87 
     88 Profile* GAIAInfoUpdateService::GetBrowserProfile() {
     89   return profile_;
     90 }
     91 
     92 std::string GAIAInfoUpdateService::GetCachedPictureURL() const {
     93   return profile_->GetPrefs()->GetString(prefs::kProfileGAIAInfoPictureURL);
     94 }
     95 
     96 void GAIAInfoUpdateService::OnProfileDownloadSuccess(
     97     ProfileDownloader* downloader) {
     98   // Make sure that |ProfileDownloader| gets deleted after return.
     99   scoped_ptr<ProfileDownloader> profile_image_downloader(
    100       profile_image_downloader_.release());
    101 
    102   // Save the last updated time.
    103   last_updated_ = base::Time::Now();
    104   profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime,
    105                                  last_updated_.ToInternalValue());
    106   ScheduleNextUpdate();
    107 
    108   string16 full_name = downloader->GetProfileFullName();
    109   SkBitmap bitmap = downloader->GetProfilePicture();
    110   ProfileDownloader::PictureStatus picture_status =
    111       downloader->GetProfilePictureStatus();
    112   std::string picture_url = downloader->GetProfilePictureURL();
    113 
    114   ProfileInfoCache& cache =
    115       g_browser_process->profile_manager()->GetProfileInfoCache();
    116   size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
    117   if (profile_index == std::string::npos)
    118     return;
    119 
    120   cache.SetGAIANameOfProfileAtIndex(profile_index, full_name);
    121   // The profile index may have changed.
    122   profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
    123   if (profile_index == std::string::npos)
    124     return;
    125   if (picture_status == ProfileDownloader::PICTURE_SUCCESS) {
    126     profile_->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
    127                                     picture_url);
    128     gfx::Image gfx_image = gfx::Image::CreateFrom1xBitmap(bitmap);
    129     cache.SetGAIAPictureOfProfileAtIndex(profile_index, &gfx_image);
    130   } else if (picture_status == ProfileDownloader::PICTURE_DEFAULT) {
    131     cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL);
    132   }
    133 
    134   // If this profile hasn't switched to using GAIA information for the profile
    135   // name and picture then switch it now. Once the profile has switched this
    136   // preference guards against clobbering the user's custom settings.
    137   if (!cache.GetHasMigratedToGAIAInfoOfProfileAtIndex(profile_index)) {
    138     cache.SetHasMigratedToGAIAInfoOfProfileAtIndex(profile_index, true);
    139     // Order matters here for shortcut management, like in
    140     // ProfileShortcutManagerWin::OnProfileAdded, as the picture update does not
    141     // allow us to change the target, so we have to apply any renaming first. We
    142     // also need to re-fetch the index, as SetIsUsingGAIANameOfProfileAtIndex
    143     // may alter it.
    144     cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, true);
    145     profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
    146     cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, true);
    147   }
    148 }
    149 
    150 void GAIAInfoUpdateService::OnProfileDownloadFailure(
    151     ProfileDownloader* downloader,
    152     ProfileDownloaderDelegate::FailureReason reason) {
    153   profile_image_downloader_.reset();
    154 
    155   // Save the last updated time.
    156   last_updated_ = base::Time::Now();
    157   profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime,
    158                                  last_updated_.ToInternalValue());
    159   ScheduleNextUpdate();
    160 }
    161 
    162 void GAIAInfoUpdateService::OnUsernameChanged() {
    163   ProfileInfoCache& cache =
    164       g_browser_process->profile_manager()->GetProfileInfoCache();
    165   size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
    166   if (profile_index == std::string::npos)
    167     return;
    168 
    169   std::string username = profile_->GetPrefs()->GetString(
    170       prefs::kGoogleServicesUsername);
    171   if (username.empty()) {
    172     // Unset the old user's GAIA info.
    173     cache.SetGAIANameOfProfileAtIndex(profile_index, string16());
    174     // The profile index may have changed.
    175     profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
    176     if (profile_index == std::string::npos)
    177       return;
    178     cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL);
    179     // Unset the cached URL.
    180     profile_->GetPrefs()->ClearPref(prefs::kProfileGAIAInfoPictureURL);
    181   } else {
    182     // Update the new user's GAIA info.
    183     Update();
    184   }
    185 }
    186 
    187 void GAIAInfoUpdateService::ScheduleNextUpdate() {
    188   if (timer_.IsRunning())
    189     return;
    190 
    191   const base::TimeDelta desired_delta =
    192       base::TimeDelta::FromHours(kUpdateIntervalHours);
    193   const base::TimeDelta update_delta = base::Time::Now() - last_updated_;
    194 
    195   base::TimeDelta delta;
    196   if (update_delta < base::TimeDelta() || update_delta > desired_delta)
    197     delta = base::TimeDelta::FromSeconds(kMinUpdateIntervalSeconds);
    198   else
    199     delta = desired_delta - update_delta;
    200 
    201   timer_.Start(FROM_HERE, delta, this, &GAIAInfoUpdateService::Update);
    202 }
    203