| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/profiles/gaia_info_update_service.h" | 5 #include "chrome/browser/profiles/gaia_info_update_service.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/browser_process.h" | 11 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/profiles/profile_info_cache.h" | 14 #include "chrome/browser/profiles/profile_attributes_entry.h" |
| 15 #include "chrome/browser/profiles/profile_attributes_storage.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/browser/profiles/profile_metrics.h" | 17 #include "chrome/browser/profiles/profile_metrics.h" |
| 17 #include "chrome/browser/profiles/profiles_state.h" | 18 #include "chrome/browser/profiles/profiles_state.h" |
| 18 #include "chrome/browser/signin/signin_manager_factory.h" | 19 #include "chrome/browser/signin/signin_manager_factory.h" |
| 19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 20 #include "components/prefs/pref_service.h" | 21 #include "components/prefs/pref_service.h" |
| 21 #include "components/signin/core/common/profile_management_switches.h" | 22 #include "components/signin/core/common/profile_management_switches.h" |
| 22 #include "components/signin/core/common/signin_pref_names.h" | 23 #include "components/signin/core/common/signin_pref_names.h" |
| 23 #include "content/public/browser/notification_details.h" | 24 #include "content/public/browser/notification_details.h" |
| 24 #include "third_party/skia/include/core/SkBitmap.h" | 25 #include "third_party/skia/include/core/SkBitmap.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 last_updated_.ToInternalValue()); | 114 last_updated_.ToInternalValue()); |
| 114 ScheduleNextUpdate(); | 115 ScheduleNextUpdate(); |
| 115 | 116 |
| 116 base::string16 full_name = downloader->GetProfileFullName(); | 117 base::string16 full_name = downloader->GetProfileFullName(); |
| 117 base::string16 given_name = downloader->GetProfileGivenName(); | 118 base::string16 given_name = downloader->GetProfileGivenName(); |
| 118 SkBitmap bitmap = downloader->GetProfilePicture(); | 119 SkBitmap bitmap = downloader->GetProfilePicture(); |
| 119 ProfileDownloader::PictureStatus picture_status = | 120 ProfileDownloader::PictureStatus picture_status = |
| 120 downloader->GetProfilePictureStatus(); | 121 downloader->GetProfilePictureStatus(); |
| 121 std::string picture_url = downloader->GetProfilePictureURL(); | 122 std::string picture_url = downloader->GetProfilePictureURL(); |
| 122 | 123 |
| 123 ProfileInfoCache& cache = | 124 ProfileAttributesEntry* entry; |
| 124 g_browser_process->profile_manager()->GetProfileInfoCache(); | 125 if (!g_browser_process->profile_manager()->GetProfileAttributesStorage(). |
| 125 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | 126 GetProfileAttributesWithPath(profile_->GetPath(), &entry)) { |
| 126 if (profile_index == std::string::npos) | |
| 127 return; | 127 return; |
| 128 } |
| 128 | 129 |
| 129 cache.SetGAIANameOfProfileAtIndex(profile_index, full_name); | 130 entry->SetGAIAName(full_name); |
| 130 // The profile index may have changed. | 131 entry->SetGAIAGivenName(given_name); |
| 131 profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | |
| 132 DCHECK_NE(profile_index, std::string::npos); | |
| 133 | |
| 134 cache.SetGAIAGivenNameOfProfileAtIndex(profile_index, given_name); | |
| 135 // The profile index may have changed. | |
| 136 profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | |
| 137 DCHECK_NE(profile_index, std::string::npos); | |
| 138 | 132 |
| 139 if (picture_status == ProfileDownloader::PICTURE_SUCCESS) { | 133 if (picture_status == ProfileDownloader::PICTURE_SUCCESS) { |
| 140 profile_->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL, | 134 profile_->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL, |
| 141 picture_url); | 135 picture_url); |
| 142 gfx::Image gfx_image = gfx::Image::CreateFrom1xBitmap(bitmap); | 136 gfx::Image gfx_image = gfx::Image::CreateFrom1xBitmap(bitmap); |
| 143 cache.SetGAIAPictureOfProfileAtIndex(profile_index, &gfx_image); | 137 entry->SetGAIAPicture(&gfx_image); |
| 144 } else if (picture_status == ProfileDownloader::PICTURE_DEFAULT) { | 138 } else if (picture_status == ProfileDownloader::PICTURE_DEFAULT) { |
| 145 cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL); | 139 entry->SetGAIAPicture(nullptr); |
| 146 } | 140 } |
| 147 | 141 |
| 148 const base::string16 hosted_domain = downloader->GetProfileHostedDomain(); | 142 const base::string16 hosted_domain = downloader->GetProfileHostedDomain(); |
| 149 profile_->GetPrefs()->SetString(prefs::kGoogleServicesHostedDomain, | 143 profile_->GetPrefs()->SetString(prefs::kGoogleServicesHostedDomain, |
| 150 (hosted_domain.empty() ? Profile::kNoHostedDomainFound : | 144 (hosted_domain.empty() ? Profile::kNoHostedDomainFound : |
| 151 base::UTF16ToUTF8(hosted_domain))); | 145 base::UTF16ToUTF8(hosted_domain))); |
| 152 } | 146 } |
| 153 | 147 |
| 154 void GAIAInfoUpdateService::OnProfileDownloadFailure( | 148 void GAIAInfoUpdateService::OnProfileDownloadFailure( |
| 155 ProfileDownloader* downloader, | 149 ProfileDownloader* downloader, |
| 156 ProfileDownloaderDelegate::FailureReason reason) { | 150 ProfileDownloaderDelegate::FailureReason reason) { |
| 157 profile_image_downloader_.reset(); | 151 profile_image_downloader_.reset(); |
| 158 | 152 |
| 159 // Save the last updated time. | 153 // Save the last updated time. |
| 160 last_updated_ = base::Time::Now(); | 154 last_updated_ = base::Time::Now(); |
| 161 profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime, | 155 profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime, |
| 162 last_updated_.ToInternalValue()); | 156 last_updated_.ToInternalValue()); |
| 163 ScheduleNextUpdate(); | 157 ScheduleNextUpdate(); |
| 164 } | 158 } |
| 165 | 159 |
| 166 void GAIAInfoUpdateService::OnUsernameChanged(const std::string& username) { | 160 void GAIAInfoUpdateService::OnUsernameChanged(const std::string& username) { |
| 167 ProfileInfoCache& cache = | 161 ProfileAttributesEntry* entry; |
| 168 g_browser_process->profile_manager()->GetProfileInfoCache(); | 162 if (!g_browser_process->profile_manager()->GetProfileAttributesStorage(). |
| 169 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | 163 GetProfileAttributesWithPath(profile_->GetPath(), &entry)) { |
| 170 if (profile_index == std::string::npos) | |
| 171 return; | 164 return; |
| 165 } |
| 172 | 166 |
| 173 if (username.empty()) { | 167 if (username.empty()) { |
| 174 // Unset the old user's GAIA info. | 168 // Unset the old user's GAIA info. |
| 175 cache.SetGAIANameOfProfileAtIndex(profile_index, base::string16()); | 169 entry->SetGAIAName(base::string16()); |
| 176 cache.SetGAIAGivenNameOfProfileAtIndex(profile_index, base::string16()); | 170 entry->SetGAIAGivenName(base::string16()); |
| 177 // The profile index may have changed. | 171 entry->SetGAIAPicture(nullptr); |
| 178 profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | |
| 179 if (profile_index == std::string::npos) | |
| 180 return; | |
| 181 cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL); | |
| 182 // Unset the cached URL. | 172 // Unset the cached URL. |
| 183 profile_->GetPrefs()->ClearPref(prefs::kProfileGAIAInfoPictureURL); | 173 profile_->GetPrefs()->ClearPref(prefs::kProfileGAIAInfoPictureURL); |
| 184 } else { | 174 } else { |
| 185 // Update the new user's GAIA info. | 175 // Update the new user's GAIA info. |
| 186 Update(); | 176 Update(); |
| 187 } | 177 } |
| 188 } | 178 } |
| 189 | 179 |
| 190 void GAIAInfoUpdateService::Shutdown() { | 180 void GAIAInfoUpdateService::Shutdown() { |
| 191 timer_.Stop(); | 181 timer_.Stop(); |
| 192 profile_image_downloader_.reset(); | 182 profile_image_downloader_.reset(); |
| 193 SigninManagerBase* signin_manager = | 183 SigninManagerBase* signin_manager = |
| 194 SigninManagerFactory::GetForProfile(profile_); | 184 SigninManagerFactory::GetForProfile(profile_); |
| 195 signin_manager->RemoveObserver(this); | 185 signin_manager->RemoveObserver(this); |
| 196 | 186 |
| 197 // OK to reset |profile_| pointer here because GAIAInfoUpdateService will not | 187 // OK to reset |profile_| pointer here because GAIAInfoUpdateService will not |
| 198 // access it again. This pointer is also used to implement the delegate for | 188 // access it again. This pointer is also used to implement the delegate for |
| 199 // |profile_image_downloader_|. However that object was destroyed above. | 189 // |profile_image_downloader_|. However that object was destroyed above. |
| 200 profile_ = NULL; | 190 profile_ = nullptr; |
| 201 } | 191 } |
| 202 | 192 |
| 203 void GAIAInfoUpdateService::ScheduleNextUpdate() { | 193 void GAIAInfoUpdateService::ScheduleNextUpdate() { |
| 204 if (timer_.IsRunning()) | 194 if (timer_.IsRunning()) |
| 205 return; | 195 return; |
| 206 | 196 |
| 207 const base::TimeDelta desired_delta = | 197 const base::TimeDelta desired_delta = |
| 208 base::TimeDelta::FromHours(kUpdateIntervalHours); | 198 base::TimeDelta::FromHours(kUpdateIntervalHours); |
| 209 const base::TimeDelta update_delta = base::Time::Now() - last_updated_; | 199 const base::TimeDelta update_delta = base::Time::Now() - last_updated_; |
| 210 | 200 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 221 const std::string& account_id, | 211 const std::string& account_id, |
| 222 const std::string& username, | 212 const std::string& username, |
| 223 const std::string& password) { | 213 const std::string& password) { |
| 224 OnUsernameChanged(username); | 214 OnUsernameChanged(username); |
| 225 } | 215 } |
| 226 | 216 |
| 227 void GAIAInfoUpdateService::GoogleSignedOut(const std::string& account_id, | 217 void GAIAInfoUpdateService::GoogleSignedOut(const std::string& account_id, |
| 228 const std::string& username) { | 218 const std::string& username) { |
| 229 OnUsernameChanged(std::string()); | 219 OnUsernameChanged(std::string()); |
| 230 } | 220 } |
| OLD | NEW |