Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / profiles / gaia_info_update_service.cc
blob9733fe47ffe751340bf81cbb3a38b5d1d5163381
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/profiles/gaia_info_update_service.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/profiles/profile_info_cache.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/sync/profile_sync_service.h"
14 #include "chrome/common/pref_names.h"
15 #include "chrome/common/profile_management_switches.h"
16 #include "content/public/browser/notification_details.h"
17 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "ui/gfx/image/image.h"
20 namespace {
22 // Update the user's GAIA info every 24 hours.
23 const int kUpdateIntervalHours = 24;
25 // If the users's GAIA info is very out of date then wait at least this long
26 // before starting an update. This avoids slowdown during startup.
27 const int kMinUpdateIntervalSeconds = 5;
29 } // namespace
31 GAIAInfoUpdateService::GAIAInfoUpdateService(Profile* profile)
32 : profile_(profile) {
33 PrefService* prefs = profile_->GetPrefs();
34 username_pref_.Init(prefs::kGoogleServicesUsername, prefs,
35 base::Bind(&GAIAInfoUpdateService::OnUsernameChanged,
36 base::Unretained(this)));
38 last_updated_ = base::Time::FromInternalValue(
39 prefs->GetInt64(prefs::kProfileGAIAInfoUpdateTime));
40 ScheduleNextUpdate();
43 GAIAInfoUpdateService::~GAIAInfoUpdateService() {
46 void GAIAInfoUpdateService::Update() {
47 // The user must be logged in.
48 std::string username = profile_->GetPrefs()->GetString(
49 prefs::kGoogleServicesUsername);
50 if (username.empty())
51 return;
53 if (profile_image_downloader_)
54 return;
55 profile_image_downloader_.reset(new ProfileDownloader(this));
56 profile_image_downloader_->Start();
59 // static
60 bool GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(Profile* profile) {
61 #if defined(OS_CHROMEOS)
62 return false;
63 #endif
65 // Sync must be allowed.
66 if (!profile->GetOriginalProfile()->IsSyncAccessible())
67 return false;
69 // To enable this feature for testing pass "--google-profile-info".
70 if (switches::IsGoogleProfileInfo())
71 return true;
73 // This feature is disable by default.
74 return false;
77 bool GAIAInfoUpdateService::NeedsProfilePicture() const {
78 return true;
81 int GAIAInfoUpdateService::GetDesiredImageSideLength() const {
82 return 256;
85 Profile* GAIAInfoUpdateService::GetBrowserProfile() {
86 return profile_;
89 std::string GAIAInfoUpdateService::GetCachedPictureURL() const {
90 return profile_->GetPrefs()->GetString(prefs::kProfileGAIAInfoPictureURL);
93 void GAIAInfoUpdateService::OnProfileDownloadSuccess(
94 ProfileDownloader* downloader) {
95 // Make sure that |ProfileDownloader| gets deleted after return.
96 scoped_ptr<ProfileDownloader> profile_image_downloader(
97 profile_image_downloader_.release());
99 // Save the last updated time.
100 last_updated_ = base::Time::Now();
101 profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime,
102 last_updated_.ToInternalValue());
103 ScheduleNextUpdate();
105 base::string16 full_name = downloader->GetProfileFullName();
106 base::string16 given_name = downloader->GetProfileGivenName();
107 SkBitmap bitmap = downloader->GetProfilePicture();
108 ProfileDownloader::PictureStatus picture_status =
109 downloader->GetProfilePictureStatus();
110 std::string picture_url = downloader->GetProfilePictureURL();
112 ProfileInfoCache& cache =
113 g_browser_process->profile_manager()->GetProfileInfoCache();
114 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
115 if (profile_index == std::string::npos)
116 return;
118 cache.SetGAIANameOfProfileAtIndex(profile_index, full_name);
119 cache.SetGAIAGivenNameOfProfileAtIndex(profile_index, given_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);
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);
150 void GAIAInfoUpdateService::OnProfileDownloadFailure(
151 ProfileDownloader* downloader,
152 ProfileDownloaderDelegate::FailureReason reason) {
153 profile_image_downloader_.reset();
155 // Save the last updated time.
156 last_updated_ = base::Time::Now();
157 profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime,
158 last_updated_.ToInternalValue());
159 ScheduleNextUpdate();
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;
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, base::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();
187 void GAIAInfoUpdateService::ScheduleNextUpdate() {
188 if (timer_.IsRunning())
189 return;
191 const base::TimeDelta desired_delta =
192 base::TimeDelta::FromHours(kUpdateIntervalHours);
193 const base::TimeDelta update_delta = base::Time::Now() - last_updated_;
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;
201 timer_.Start(FROM_HERE, delta, this, &GAIAInfoUpdateService::Update);