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/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/profiles/profile_info_cache.h" | 11 #include "chrome/browser/profiles/profile_info_cache.h" |
12 #include "chrome/browser/profiles/profile_manager.h" | 12 #include "chrome/browser/profiles/profile_manager.h" |
| 13 #include "chrome/browser/signin/signin_manager_factory.h" |
13 #include "chrome/browser/sync/profile_sync_service.h" | 14 #include "chrome/browser/sync/profile_sync_service.h" |
14 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
15 #include "chrome/common/profile_management_switches.h" | 16 #include "chrome/common/profile_management_switches.h" |
16 #include "content/public/browser/notification_details.h" | 17 #include "content/public/browser/notification_details.h" |
17 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
18 #include "ui/gfx/image/image.h" | 19 #include "ui/gfx/image/image.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 // Update the user's GAIA info every 24 hours. | 23 // Update the user's GAIA info every 24 hours. |
23 const int kUpdateIntervalHours = 24; | 24 const int kUpdateIntervalHours = 24; |
24 | 25 |
25 // If the users's GAIA info is very out of date then wait at least this long | 26 // 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 // before starting an update. This avoids slowdown during startup. |
27 const int kMinUpdateIntervalSeconds = 5; | 28 const int kMinUpdateIntervalSeconds = 5; |
28 | 29 |
29 } // namespace | 30 } // namespace |
30 | 31 |
31 GAIAInfoUpdateService::GAIAInfoUpdateService(Profile* profile) | 32 GAIAInfoUpdateService::GAIAInfoUpdateService(Profile* profile) |
32 : profile_(profile) { | 33 : profile_(profile) { |
| 34 SigninManagerBase* signin_manager = |
| 35 SigninManagerFactory::GetForProfile(profile_); |
| 36 signin_manager->AddObserver(this); |
| 37 |
33 PrefService* prefs = profile_->GetPrefs(); | 38 PrefService* prefs = profile_->GetPrefs(); |
34 username_pref_.Init(prefs::kGoogleServicesUsername, prefs, | |
35 base::Bind(&GAIAInfoUpdateService::OnUsernameChanged, | |
36 base::Unretained(this))); | |
37 | |
38 last_updated_ = base::Time::FromInternalValue( | 39 last_updated_ = base::Time::FromInternalValue( |
39 prefs->GetInt64(prefs::kProfileGAIAInfoUpdateTime)); | 40 prefs->GetInt64(prefs::kProfileGAIAInfoUpdateTime)); |
40 ScheduleNextUpdate(); | 41 ScheduleNextUpdate(); |
41 } | 42 } |
42 | 43 |
43 GAIAInfoUpdateService::~GAIAInfoUpdateService() { | 44 GAIAInfoUpdateService::~GAIAInfoUpdateService() { |
| 45 DCHECK(!profile_) << "Shutdown not called before dtor"; |
44 } | 46 } |
45 | 47 |
46 void GAIAInfoUpdateService::Update() { | 48 void GAIAInfoUpdateService::Update() { |
47 // The user must be logged in. | 49 // The user must be logged in. |
48 std::string username = profile_->GetPrefs()->GetString( | 50 SigninManagerBase* signin_manager = |
49 prefs::kGoogleServicesUsername); | 51 SigninManagerFactory::GetForProfile(profile_); |
50 if (username.empty()) | 52 if (signin_manager->GetAuthenticatedAccountId().empty()) |
51 return; | 53 return; |
52 | 54 |
53 if (profile_image_downloader_) | 55 if (profile_image_downloader_) |
54 return; | 56 return; |
55 profile_image_downloader_.reset(new ProfileDownloader(this)); | 57 profile_image_downloader_.reset(new ProfileDownloader(this)); |
56 profile_image_downloader_->Start(); | 58 profile_image_downloader_->Start(); |
57 } | 59 } |
58 | 60 |
59 // static | 61 // static |
60 bool GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(Profile* profile) { | 62 bool GAIAInfoUpdateService::ShouldUseGAIAProfileInfo(Profile* profile) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 ProfileDownloaderDelegate::FailureReason reason) { | 146 ProfileDownloaderDelegate::FailureReason reason) { |
145 profile_image_downloader_.reset(); | 147 profile_image_downloader_.reset(); |
146 | 148 |
147 // Save the last updated time. | 149 // Save the last updated time. |
148 last_updated_ = base::Time::Now(); | 150 last_updated_ = base::Time::Now(); |
149 profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime, | 151 profile_->GetPrefs()->SetInt64(prefs::kProfileGAIAInfoUpdateTime, |
150 last_updated_.ToInternalValue()); | 152 last_updated_.ToInternalValue()); |
151 ScheduleNextUpdate(); | 153 ScheduleNextUpdate(); |
152 } | 154 } |
153 | 155 |
154 void GAIAInfoUpdateService::OnUsernameChanged() { | 156 void GAIAInfoUpdateService::OnUsernameChanged(const std::string& username) { |
155 ProfileInfoCache& cache = | 157 ProfileInfoCache& cache = |
156 g_browser_process->profile_manager()->GetProfileInfoCache(); | 158 g_browser_process->profile_manager()->GetProfileInfoCache(); |
157 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | 159 size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); |
158 if (profile_index == std::string::npos) | 160 if (profile_index == std::string::npos) |
159 return; | 161 return; |
160 | 162 |
161 std::string username = profile_->GetPrefs()->GetString( | |
162 prefs::kGoogleServicesUsername); | |
163 if (username.empty()) { | 163 if (username.empty()) { |
164 // Unset the old user's GAIA info. | 164 // Unset the old user's GAIA info. |
165 cache.SetGAIANameOfProfileAtIndex(profile_index, base::string16()); | 165 cache.SetGAIANameOfProfileAtIndex(profile_index, base::string16()); |
166 // The profile index may have changed. | 166 // The profile index may have changed. |
167 profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); | 167 profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath()); |
168 if (profile_index == std::string::npos) | 168 if (profile_index == std::string::npos) |
169 return; | 169 return; |
170 cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL); | 170 cache.SetGAIAPictureOfProfileAtIndex(profile_index, NULL); |
171 // Unset the cached URL. | 171 // Unset the cached URL. |
172 profile_->GetPrefs()->ClearPref(prefs::kProfileGAIAInfoPictureURL); | 172 profile_->GetPrefs()->ClearPref(prefs::kProfileGAIAInfoPictureURL); |
173 } else { | 173 } else { |
174 // Update the new user's GAIA info. | 174 // Update the new user's GAIA info. |
175 Update(); | 175 Update(); |
176 } | 176 } |
177 } | 177 } |
178 | 178 |
| 179 void GAIAInfoUpdateService::Shutdown() { |
| 180 timer_.Stop(); |
| 181 profile_image_downloader_.reset(); |
| 182 SigninManagerBase* signin_manager = |
| 183 SigninManagerFactory::GetForProfile(profile_); |
| 184 signin_manager->RemoveObserver(this); |
| 185 |
| 186 // OK to reset |profile_| pointer here because GAIAInfoUpdateService will not |
| 187 // access it again. This pointer is also used to implement the delegate for |
| 188 // |profile_image_downloader_|. However that object was destroyed above. |
| 189 profile_ = NULL; |
| 190 } |
| 191 |
179 void GAIAInfoUpdateService::ScheduleNextUpdate() { | 192 void GAIAInfoUpdateService::ScheduleNextUpdate() { |
180 if (timer_.IsRunning()) | 193 if (timer_.IsRunning()) |
181 return; | 194 return; |
182 | 195 |
183 const base::TimeDelta desired_delta = | 196 const base::TimeDelta desired_delta = |
184 base::TimeDelta::FromHours(kUpdateIntervalHours); | 197 base::TimeDelta::FromHours(kUpdateIntervalHours); |
185 const base::TimeDelta update_delta = base::Time::Now() - last_updated_; | 198 const base::TimeDelta update_delta = base::Time::Now() - last_updated_; |
186 | 199 |
187 base::TimeDelta delta; | 200 base::TimeDelta delta; |
188 if (update_delta < base::TimeDelta() || update_delta > desired_delta) | 201 if (update_delta < base::TimeDelta() || update_delta > desired_delta) |
189 delta = base::TimeDelta::FromSeconds(kMinUpdateIntervalSeconds); | 202 delta = base::TimeDelta::FromSeconds(kMinUpdateIntervalSeconds); |
190 else | 203 else |
191 delta = desired_delta - update_delta; | 204 delta = desired_delta - update_delta; |
192 | 205 |
193 timer_.Start(FROM_HERE, delta, this, &GAIAInfoUpdateService::Update); | 206 timer_.Start(FROM_HERE, delta, this, &GAIAInfoUpdateService::Update); |
194 } | 207 } |
| 208 |
| 209 void GAIAInfoUpdateService::GoogleSigninSucceeded( |
| 210 const std::string& username, |
| 211 const std::string& password) { |
| 212 OnUsernameChanged(username); |
| 213 } |
| 214 |
| 215 void GAIAInfoUpdateService::GoogleSignedOut(const std::string& username) { |
| 216 OnUsernameChanged(std::string()); |
| 217 } |
OLD | NEW |