Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / profiles / gaia_info_update_service.cc
blob083e21db4168cd2bae61de94c9afe53917fb4ca3
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 "base/strings/utf_string_conversions.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/profiles/profile_metrics.h"
15 #include "chrome/browser/profiles/profiles_state.h"
16 #include "chrome/browser/signin/signin_manager_factory.h"
17 #include "chrome/common/pref_names.h"
18 #include "components/signin/core/common/profile_management_switches.h"
19 #include "content/public/browser/notification_details.h"
20 #include "third_party/skia/include/core/SkBitmap.h"
21 #include "ui/gfx/image/image.h"
23 namespace {
25 // Update the user's GAIA info every 24 hours.
26 const int kUpdateIntervalHours = 24;
28 // If the users's GAIA info is very out of date then wait at least this long
29 // before starting an update. This avoids slowdown during startup.
30 const int kMinUpdateIntervalSeconds = 5;
32 } // namespace
34 GAIAInfoUpdateService::GAIAInfoUpdateService(Profile* profile)
35 : profile_(profile) {
36 SigninManagerBase* signin_manager =
37 SigninManagerFactory::GetForProfile(profile_);
38 signin_manager->AddObserver(this);
40 PrefService* prefs = profile_->GetPrefs();
41 last_updated_ = base::Time::FromInternalValue(
42 prefs->GetInt64(prefs::kProfileGAIAInfoUpdateTime));
43 ScheduleNextUpdate();
46 GAIAInfoUpdateService::~GAIAInfoUpdateService() {
47 DCHECK(!profile_) << "Shutdown not called before dtor";
50 void GAIAInfoUpdateService::Update() {
51 // UMA Profile Metrics should be logged regularly.
52 ProfileMetrics::LogNumberOfProfiles(g_browser_process->profile_manager());
54 // The user must be logged in.
55 SigninManagerBase* signin_manager =
56 SigninManagerFactory::GetForProfile(profile_);
57 if (!signin_manager->IsAuthenticated())
58 return;
60 if (profile_image_downloader_)
61 return;
62 profile_image_downloader_.reset(new ProfileDownloader(this));
63 profile_image_downloader_->Start();
66 // static
67 bool GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(Profile* profile) {
68 #if defined(OS_CHROMEOS)
69 return false;
70 #endif
72 // To enable this feature for testing pass "--google-profile-info".
73 if (switches::IsGoogleProfileInfo())
74 return true;
76 // This feature is disable by default.
77 return false;
80 bool GAIAInfoUpdateService::NeedsProfilePicture() const {
81 return true;
84 int GAIAInfoUpdateService::GetDesiredImageSideLength() const {
85 return 256;
88 Profile* GAIAInfoUpdateService::GetBrowserProfile() {
89 return profile_;
92 std::string GAIAInfoUpdateService::GetCachedPictureURL() const {
93 return profile_->GetPrefs()->GetString(prefs::kProfileGAIAInfoPictureURL);
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());
102 // Save the last updated time.
103 last_updated_ = base::Time::Now();
104 profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime,
105 last_updated_.ToInternalValue());
106 ScheduleNextUpdate();
108 base::string16 full_name = downloader->GetProfileFullName();
109 base::string16 given_name = downloader->GetProfileGivenName();
110 SkBitmap bitmap = downloader->GetProfilePicture();
111 ProfileDownloader::PictureStatus picture_status =
112 downloader->GetProfilePictureStatus();
113 std::string picture_url = downloader->GetProfilePictureURL();
115 ProfileInfoCache& cache =
116 g_browser_process->profile_manager()->GetProfileInfoCache();
117 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
118 if (profile_index == std::string::npos)
119 return;
121 cache.SetGAIANameOfProfileAtIndex(profile_index, full_name);
122 // The profile index may have changed.
123 profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
124 DCHECK_NE(profile_index, std::string::npos);
126 cache.SetGAIAGivenNameOfProfileAtIndex(profile_index, given_name);
127 // The profile index may have changed.
128 profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
129 DCHECK_NE(profile_index, std::string::npos);
131 if (picture_status == ProfileDownloader::PICTURE_SUCCESS) {
132 profile_->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
133 picture_url);
134 gfx::Image gfx_image = gfx::Image::CreateFrom1xBitmap(bitmap);
135 cache.SetGAIAPictureOfProfileAtIndex(profile_index, &gfx_image);
136 } else if (picture_status == ProfileDownloader::PICTURE_DEFAULT) {
137 cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL);
140 const base::string16 hosted_domain = downloader->GetProfileHostedDomain();
141 profile_->GetPrefs()->SetString(prefs::kGoogleServicesHostedDomain,
142 (hosted_domain.empty() ? Profile::kNoHostedDomainFound :
143 base::UTF16ToUTF8(hosted_domain)));
146 void GAIAInfoUpdateService::OnProfileDownloadFailure(
147 ProfileDownloader* downloader,
148 ProfileDownloaderDelegate::FailureReason reason) {
149 profile_image_downloader_.reset();
151 // Save the last updated time.
152 last_updated_ = base::Time::Now();
153 profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime,
154 last_updated_.ToInternalValue());
155 ScheduleNextUpdate();
158 void GAIAInfoUpdateService::OnUsernameChanged(const std::string& username) {
159 ProfileInfoCache& cache =
160 g_browser_process->profile_manager()->GetProfileInfoCache();
161 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
162 if (profile_index == std::string::npos)
163 return;
165 if (username.empty()) {
166 // Unset the old user's GAIA info.
167 cache.SetGAIANameOfProfileAtIndex(profile_index, base::string16());
168 cache.SetGAIAGivenNameOfProfileAtIndex(profile_index, base::string16());
169 // The profile index may have changed.
170 profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
171 if (profile_index == std::string::npos)
172 return;
173 cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL);
174 // Unset the cached URL.
175 profile_->GetPrefs()->ClearPref(prefs::kProfileGAIAInfoPictureURL);
176 } else {
177 // Update the new user's GAIA info.
178 Update();
182 void GAIAInfoUpdateService::Shutdown() {
183 timer_.Stop();
184 profile_image_downloader_.reset();
185 SigninManagerBase* signin_manager =
186 SigninManagerFactory::GetForProfile(profile_);
187 signin_manager->RemoveObserver(this);
189 // OK to reset |profile_| pointer here because GAIAInfoUpdateService will not
190 // access it again. This pointer is also used to implement the delegate for
191 // |profile_image_downloader_|. However that object was destroyed above.
192 profile_ = NULL;
195 void GAIAInfoUpdateService::ScheduleNextUpdate() {
196 if (timer_.IsRunning())
197 return;
199 const base::TimeDelta desired_delta =
200 base::TimeDelta::FromHours(kUpdateIntervalHours);
201 const base::TimeDelta update_delta = base::Time::Now() - last_updated_;
203 base::TimeDelta delta;
204 if (update_delta < base::TimeDelta() || update_delta > desired_delta)
205 delta = base::TimeDelta::FromSeconds(kMinUpdateIntervalSeconds);
206 else
207 delta = desired_delta - update_delta;
209 timer_.Start(FROM_HERE, delta, this, &GAIAInfoUpdateService::Update);
212 void GAIAInfoUpdateService::GoogleSigninSucceeded(
213 const std::string& account_id,
214 const std::string& username,
215 const std::string& password) {
216 OnUsernameChanged(username);
219 void GAIAInfoUpdateService::GoogleSignedOut(const std::string& account_id,
220 const std::string& username) {
221 OnUsernameChanged(std::string());