Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: chrome/browser/profiles/gaia_info_update_service.cc

Issue 1794353003: Refactor ProfileInfoCache in c/b/profiles (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Replaced LOG(WARNING) by UMA histograms Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 last_updated_.ToInternalValue()); 111 last_updated_.ToInternalValue());
111 ScheduleNextUpdate(); 112 ScheduleNextUpdate();
112 113
113 base::string16 full_name = downloader->GetProfileFullName(); 114 base::string16 full_name = downloader->GetProfileFullName();
114 base::string16 given_name = downloader->GetProfileGivenName(); 115 base::string16 given_name = downloader->GetProfileGivenName();
115 SkBitmap bitmap = downloader->GetProfilePicture(); 116 SkBitmap bitmap = downloader->GetProfilePicture();
116 ProfileDownloader::PictureStatus picture_status = 117 ProfileDownloader::PictureStatus picture_status =
117 downloader->GetProfilePictureStatus(); 118 downloader->GetProfilePictureStatus();
118 std::string picture_url = downloader->GetProfilePictureURL(); 119 std::string picture_url = downloader->GetProfilePictureURL();
119 120
120 ProfileInfoCache& cache = 121 ProfileAttributesEntry* entry;
121 g_browser_process->profile_manager()->GetProfileInfoCache(); 122 if (!g_browser_process->profile_manager()->GetProfileAttributesStorage().
122 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); 123 GetProfileAttributesWithPath(profile_->GetPath(), &entry)) {
123 if (profile_index == std::string::npos)
124 return; 124 return;
125 }
125 126
126 cache.SetGAIANameOfProfileAtIndex(profile_index, full_name); 127 entry->SetGAIAName(full_name);
127 // The profile index may have changed. 128 entry->SetGAIAGivenName(given_name);
128 profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
129 DCHECK_NE(profile_index, std::string::npos);
130
131 cache.SetGAIAGivenNameOfProfileAtIndex(profile_index, given_name);
132 // The profile index may have changed.
133 profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
134 DCHECK_NE(profile_index, std::string::npos);
135 129
136 if (picture_status == ProfileDownloader::PICTURE_SUCCESS) { 130 if (picture_status == ProfileDownloader::PICTURE_SUCCESS) {
137 profile_->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL, 131 profile_->GetPrefs()->SetString(prefs::kProfileGAIAInfoPictureURL,
138 picture_url); 132 picture_url);
139 gfx::Image gfx_image = gfx::Image::CreateFrom1xBitmap(bitmap); 133 gfx::Image gfx_image = gfx::Image::CreateFrom1xBitmap(bitmap);
140 cache.SetGAIAPictureOfProfileAtIndex(profile_index, &gfx_image); 134 entry->SetGAIAPicture(&gfx_image);
141 } else if (picture_status == ProfileDownloader::PICTURE_DEFAULT) { 135 } else if (picture_status == ProfileDownloader::PICTURE_DEFAULT) {
142 cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL); 136 entry->SetGAIAPicture(nullptr);
143 } 137 }
144 138
145 const base::string16 hosted_domain = downloader->GetProfileHostedDomain(); 139 const base::string16 hosted_domain = downloader->GetProfileHostedDomain();
146 profile_->GetPrefs()->SetString(prefs::kGoogleServicesHostedDomain, 140 profile_->GetPrefs()->SetString(prefs::kGoogleServicesHostedDomain,
147 (hosted_domain.empty() ? Profile::kNoHostedDomainFound : 141 (hosted_domain.empty() ? Profile::kNoHostedDomainFound :
148 base::UTF16ToUTF8(hosted_domain))); 142 base::UTF16ToUTF8(hosted_domain)));
149 } 143 }
150 144
151 void GAIAInfoUpdateService::OnProfileDownloadFailure( 145 void GAIAInfoUpdateService::OnProfileDownloadFailure(
152 ProfileDownloader* downloader, 146 ProfileDownloader* downloader,
153 ProfileDownloaderDelegate::FailureReason reason) { 147 ProfileDownloaderDelegate::FailureReason reason) {
154 profile_image_downloader_.reset(); 148 profile_image_downloader_.reset();
155 149
156 // Save the last updated time. 150 // Save the last updated time.
157 last_updated_ = base::Time::Now(); 151 last_updated_ = base::Time::Now();
158 profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime, 152 profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime,
159 last_updated_.ToInternalValue()); 153 last_updated_.ToInternalValue());
160 ScheduleNextUpdate(); 154 ScheduleNextUpdate();
161 } 155 }
162 156
163 void GAIAInfoUpdateService::OnUsernameChanged(const std::string& username) { 157 void GAIAInfoUpdateService::OnUsernameChanged(const std::string& username) {
164 ProfileInfoCache& cache = 158 ProfileAttributesEntry* entry;
165 g_browser_process->profile_manager()->GetProfileInfoCache(); 159 if (!g_browser_process->profile_manager()->GetProfileAttributesStorage().
166 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); 160 GetProfileAttributesWithPath(profile_->GetPath(), &entry)) {
167 if (profile_index == std::string::npos)
168 return; 161 return;
162 }
169 163
170 if (username.empty()) { 164 if (username.empty()) {
171 // Unset the old user's GAIA info. 165 // Unset the old user's GAIA info.
172 cache.SetGAIANameOfProfileAtIndex(profile_index, base::string16()); 166 entry->SetGAIAName(base::string16());
173 cache.SetGAIAGivenNameOfProfileAtIndex(profile_index, base::string16()); 167 entry->SetGAIAGivenName(base::string16());
174 // The profile index may have changed. 168 entry->SetGAIAPicture(nullptr);
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. 169 // Unset the cached URL.
180 profile_->GetPrefs()->ClearPref(prefs::kProfileGAIAInfoPictureURL); 170 profile_->GetPrefs()->ClearPref(prefs::kProfileGAIAInfoPictureURL);
181 } else { 171 } else {
182 // Update the new user's GAIA info. 172 // Update the new user's GAIA info.
183 Update(); 173 Update();
184 } 174 }
185 } 175 }
186 176
187 void GAIAInfoUpdateService::Shutdown() { 177 void GAIAInfoUpdateService::Shutdown() {
188 timer_.Stop(); 178 timer_.Stop();
189 profile_image_downloader_.reset(); 179 profile_image_downloader_.reset();
190 SigninManagerBase* signin_manager = 180 SigninManagerBase* signin_manager =
191 SigninManagerFactory::GetForProfile(profile_); 181 SigninManagerFactory::GetForProfile(profile_);
192 signin_manager->RemoveObserver(this); 182 signin_manager->RemoveObserver(this);
193 183
194 // OK to reset |profile_| pointer here because GAIAInfoUpdateService will not 184 // OK to reset |profile_| pointer here because GAIAInfoUpdateService will not
195 // access it again. This pointer is also used to implement the delegate for 185 // access it again. This pointer is also used to implement the delegate for
196 // |profile_image_downloader_|. However that object was destroyed above. 186 // |profile_image_downloader_|. However that object was destroyed above.
197 profile_ = NULL; 187 profile_ = nullptr;
198 } 188 }
199 189
200 void GAIAInfoUpdateService::ScheduleNextUpdate() { 190 void GAIAInfoUpdateService::ScheduleNextUpdate() {
201 if (timer_.IsRunning()) 191 if (timer_.IsRunning())
202 return; 192 return;
203 193
204 const base::TimeDelta desired_delta = 194 const base::TimeDelta desired_delta =
205 base::TimeDelta::FromHours(kUpdateIntervalHours); 195 base::TimeDelta::FromHours(kUpdateIntervalHours);
206 const base::TimeDelta update_delta = base::Time::Now() - last_updated_; 196 const base::TimeDelta update_delta = base::Time::Now() - last_updated_;
207 197
208 base::TimeDelta delta; 198 base::TimeDelta delta;
209 if (update_delta < base::TimeDelta() || update_delta > desired_delta) 199 if (update_delta < base::TimeDelta() || update_delta > desired_delta)
210 delta = base::TimeDelta::FromSeconds(kMinUpdateIntervalSeconds); 200 delta = base::TimeDelta::FromSeconds(kMinUpdateIntervalSeconds);
211 else 201 else
212 delta = desired_delta - update_delta; 202 delta = desired_delta - update_delta;
213 203
214 // UMA Profile Metrics should be logged regularly. Logging is not performed 204 // UMA Profile Metrics should be logged regularly. Logging is not performed
215 // in Update() because it is a public method and may be called at any time. 205 // in Update() because it is a public method and may be called at any time.
216 // These metrics should logged only on this schedule. 206 // These metrics should logged only on this schedule.
Mike Lerman 2016/04/13 15:07:41 Put this back... we do really want to log them at
lwchkg 2016/04/13 16:50:57 See https://chromium.googlesource.com/chromium/src
217 // 207 //
218 // In mac perf tests, the browser process pointer may be null. 208 // In mac perf tests, the browser process pointer may be null.
219 if (g_browser_process) 209 if (g_browser_process)
220 ProfileMetrics::LogNumberOfProfiles(g_browser_process->profile_manager()); 210 ProfileMetrics::LogNumberOfProfiles(g_browser_process->profile_manager());
221 211
222 timer_.Start(FROM_HERE, delta, this, &GAIAInfoUpdateService::Update); 212 timer_.Start(FROM_HERE, delta, this, &GAIAInfoUpdateService::Update);
223 } 213 }
224 214
225 void GAIAInfoUpdateService::GoogleSigninSucceeded( 215 void GAIAInfoUpdateService::GoogleSigninSucceeded(
226 const std::string& account_id, 216 const std::string& account_id,
227 const std::string& username, 217 const std::string& username,
228 const std::string& password) { 218 const std::string& password) {
229 OnUsernameChanged(username); 219 OnUsernameChanged(username);
230 } 220 }
231 221
232 void GAIAInfoUpdateService::GoogleSignedOut(const std::string& account_id, 222 void GAIAInfoUpdateService::GoogleSignedOut(const std::string& account_id,
233 const std::string& username) { 223 const std::string& username) {
234 OnUsernameChanged(std::string()); 224 OnUsernameChanged(std::string());
235 } 225 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698