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 "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 107 |
108 string16 full_name = downloader->GetProfileFullName(); | 108 string16 full_name = downloader->GetProfileFullName(); |
109 string16 given_name = downloader->GetProfileGivenName(); | 109 string16 given_name = downloader->GetProfileGivenName(); |
110 SkBitmap bitmap = downloader->GetProfilePicture(); | 110 SkBitmap bitmap = downloader->GetProfilePicture(); |
111 ProfileDownloader::PictureStatus picture_status = | 111 ProfileDownloader::PictureStatus picture_status = |
112 downloader->GetProfilePictureStatus(); | 112 downloader->GetProfilePictureStatus(); |
113 std::string picture_url = downloader->GetProfilePictureURL(); | 113 std::string picture_url = downloader->GetProfilePictureURL(); |
114 | 114 |
115 ProfileInfoCache& cache = | 115 ProfileInfoCache& cache = |
116 g_browser_process->profile_manager()->GetProfileInfoCache(); | 116 g_browser_process->profile_manager()->GetProfileInfoCache(); |
117 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | 117 |
118 if (profile_index == std::string::npos) | 118 ProfileInfoEntry entry; |
| 119 if (!cache.GetInfoForProfile(profile_->GetPath(), &entry)) |
119 return; | 120 return; |
120 | 121 |
121 cache.SetGAIANameOfProfileAtIndex(profile_index, full_name); | 122 entry.set_GAIA_full_name(full_name); |
122 cache.SetGAIAGivenNameOfProfileAtIndex(profile_index, given_name); | 123 entry.set_GAIA_given_name(given_name); |
123 | 124 |
124 // The profile index may have changed. | |
125 profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | |
126 if (profile_index == std::string::npos) | |
127 return; | |
128 if (picture_status == ProfileDownloader::PICTURE_SUCCESS) { | 125 if (picture_status == ProfileDownloader::PICTURE_SUCCESS) { |
129 profile_->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL, | 126 profile_->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL, |
130 picture_url); | 127 picture_url); |
131 gfx::Image gfx_image = gfx::Image::CreateFrom1xBitmap(bitmap); | 128 gfx::Image gfx_image = gfx::Image::CreateFrom1xBitmap(bitmap); |
132 cache.SetGAIAPictureOfProfileAtIndex(profile_index, &gfx_image); | 129 cache.SetGAIAPictureOfProfile(entry.path(), &gfx_image); |
133 } else if (picture_status == ProfileDownloader::PICTURE_DEFAULT) { | 130 } else if (picture_status == ProfileDownloader::PICTURE_DEFAULT) { |
134 cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL); | 131 cache.SetGAIAPictureOfProfile(entry.path(), NULL); |
135 } | 132 } |
136 | 133 |
137 // If this profile hasn't switched to using GAIA information for the profile | 134 // If this profile hasn't switched to using GAIA information for the profile |
138 // name and picture then switch it now. Once the profile has switched this | 135 // name and picture then switch it now. Once the profile has switched this |
139 // preference guards against clobbering the user's custom settings. | 136 // preference guards against clobbering the user's custom settings. |
140 if (!cache.GetHasMigratedToGAIAInfoOfProfileAtIndex(profile_index)) { | 137 if (!entry.has_migrated_to_GAIA_info_of_profile()) { |
141 cache.SetHasMigratedToGAIAInfoOfProfileAtIndex(profile_index, true); | 138 entry.set_has_migrated_to_GAIA_info_of_profile(true); |
142 // Order matters here for shortcut management, like in | 139 // Order matters here for shortcut management, like in |
143 // ProfileShortcutManagerWin::OnProfileAdded, as the picture update does not | 140 // ProfileShortcutManagerWin::OnProfileAdded, as the picture update does not |
144 // allow us to change the target, so we have to apply any renaming first. We | 141 // allow us to change the target, so we have to apply any renaming first. We |
145 // also need to re-fetch the index, as SetIsUsingGAIANameOfProfileAtIndex | 142 // also need to re-fetch the index, as SetIsUsingGAIANameOfProfileAtIndex |
146 // may alter it. | 143 // may alter it. |
147 cache.SetIsUsingGAIANameOfProfileAtIndex(profile_index, true); | 144 entry.set_is_using_GAIA_name(true); |
148 profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | 145 entry.set_is_using_GAIA_picture(true); |
149 cache.SetIsUsingGAIAPictureOfProfileAtIndex(profile_index, true); | |
150 } | 146 } |
| 147 |
| 148 cache.SetInfoForProfile(entry); |
151 } | 149 } |
152 | 150 |
153 void GAIAInfoUpdateService::OnProfileDownloadFailure( | 151 void GAIAInfoUpdateService::OnProfileDownloadFailure( |
154 ProfileDownloader* downloader, | 152 ProfileDownloader* downloader, |
155 ProfileDownloaderDelegate::FailureReason reason) { | 153 ProfileDownloaderDelegate::FailureReason reason) { |
156 profile_image_downloader_.reset(); | 154 profile_image_downloader_.reset(); |
157 | 155 |
158 // Save the last updated time. | 156 // Save the last updated time. |
159 last_updated_ = base::Time::Now(); | 157 last_updated_ = base::Time::Now(); |
160 profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime, | 158 profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime, |
161 last_updated_.ToInternalValue()); | 159 last_updated_.ToInternalValue()); |
162 ScheduleNextUpdate(); | 160 ScheduleNextUpdate(); |
163 } | 161 } |
164 | 162 |
165 void GAIAInfoUpdateService::OnUsernameChanged() { | 163 void GAIAInfoUpdateService::OnUsernameChanged() { |
166 ProfileInfoCache& cache = | 164 ProfileInfoCache& cache = |
167 g_browser_process->profile_manager()->GetProfileInfoCache(); | 165 g_browser_process->profile_manager()->GetProfileInfoCache(); |
168 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | 166 |
169 if (profile_index == std::string::npos) | 167 ProfileInfoEntry entry; |
| 168 if (!cache.GetInfoForProfile(profile_->GetPath(), &entry)) |
170 return; | 169 return; |
171 | 170 |
172 std::string username = profile_->GetPrefs()->GetString( | 171 std::string username = profile_->GetPrefs()->GetString( |
173 prefs::kGoogleServicesUsername); | 172 prefs::kGoogleServicesUsername); |
174 if (username.empty()) { | 173 if (username.empty()) { |
175 // Unset the old user's GAIA info. | 174 // Unset the old user's GAIA info. |
176 cache.SetGAIANameOfProfileAtIndex(profile_index, string16()); | 175 entry.set_GAIA_full_name(string16()); |
177 // The profile index may have changed. | 176 cache.SetGAIAPictureOfProfile(entry.path(), NULL); |
178 profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | 177 cache.SetInfoForProfile(entry); |
179 if (profile_index == std::string::npos) | |
180 return; | |
181 cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL); | |
182 // Unset the cached URL. | 178 // Unset the cached URL. |
183 profile_->GetPrefs()->ClearPref(prefs::kProfileGAIAInfoPictureURL); | 179 profile_->GetPrefs()->ClearPref(prefs::kProfileGAIAInfoPictureURL); |
184 } else { | 180 } else { |
185 // Update the new user's GAIA info. | 181 // Update the new user's GAIA info. |
186 Update(); | 182 Update(); |
187 } | 183 } |
188 } | 184 } |
189 | 185 |
190 void GAIAInfoUpdateService::ScheduleNextUpdate() { | 186 void GAIAInfoUpdateService::ScheduleNextUpdate() { |
191 if (timer_.IsRunning()) | 187 if (timer_.IsRunning()) |
192 return; | 188 return; |
193 | 189 |
194 const base::TimeDelta desired_delta = | 190 const base::TimeDelta desired_delta = |
195 base::TimeDelta::FromHours(kUpdateIntervalHours); | 191 base::TimeDelta::FromHours(kUpdateIntervalHours); |
196 const base::TimeDelta update_delta = base::Time::Now() - last_updated_; | 192 const base::TimeDelta update_delta = base::Time::Now() - last_updated_; |
197 | 193 |
198 base::TimeDelta delta; | 194 base::TimeDelta delta; |
199 if (update_delta < base::TimeDelta() || update_delta > desired_delta) | 195 if (update_delta < base::TimeDelta() || update_delta > desired_delta) |
200 delta = base::TimeDelta::FromSeconds(kMinUpdateIntervalSeconds); | 196 delta = base::TimeDelta::FromSeconds(kMinUpdateIntervalSeconds); |
201 else | 197 else |
202 delta = desired_delta - update_delta; | 198 delta = desired_delta - update_delta; |
203 | 199 |
204 timer_.Start(FROM_HERE, delta, this, &GAIAInfoUpdateService::Update); | 200 timer_.Start(FROM_HERE, delta, this, &GAIAInfoUpdateService::Update); |
205 } | 201 } |
OLD | NEW |