OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/sync/signin_manager.h" | 5 #include "chrome/browser/sync/signin_manager.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "chrome/browser/net/gaia/token_service.h" | 9 #include "chrome/browser/net/gaia/token_service.h" |
10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/sync/profile_sync_service.h" | 12 #include "chrome/browser/sync/profile_sync_service.h" |
13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
14 #include "chrome/common/net/gaia/gaia_constants.h" | 14 #include "chrome/common/net/gaia/gaia_constants.h" |
15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
16 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
17 #include "content/common/notification_service.h" | 17 #include "content/common/notification_service.h" |
18 | 18 |
19 const char kGetInfoEmailKey[] = "email"; | 19 const char kGetInfoEmailKey[] = "email"; |
20 | 20 |
21 namespace { | |
22 bool IsUsingOAuth() { | |
23 return CommandLine::ForCurrentProcess()->HasSwitch( | |
24 switches::kEnableSyncOAuth); | |
25 } | |
26 } | |
akalin
2011/08/11 18:03:37
} // namespace
Rick Campbell
2011/08/11 21:04:08
Moved IsUsingOAuth() to util/
| |
27 | |
21 SigninManager::SigninManager() | 28 SigninManager::SigninManager() |
22 : profile_(NULL), had_two_factor_error_(false) {} | 29 : profile_(NULL), had_two_factor_error_(false) {} |
23 | 30 |
24 SigninManager::~SigninManager() {} | 31 SigninManager::~SigninManager() {} |
25 | 32 |
26 // static | 33 // static |
27 void SigninManager::RegisterUserPrefs(PrefService* user_prefs) { | 34 void SigninManager::RegisterUserPrefs(PrefService* user_prefs) { |
28 user_prefs->RegisterStringPref(prefs::kGoogleServicesUsername, | 35 user_prefs->RegisterStringPref(prefs::kGoogleServicesUsername, |
29 "", | 36 "", |
30 PrefService::UNSYNCABLE_PREF); | 37 PrefService::UNSYNCABLE_PREF); |
31 user_prefs->RegisterBooleanPref(prefs::kAutologinEnabled, | 38 user_prefs->RegisterBooleanPref(prefs::kAutologinEnabled, |
32 true, | 39 true, |
33 PrefService::UNSYNCABLE_PREF); | 40 PrefService::UNSYNCABLE_PREF); |
34 } | 41 } |
35 | 42 |
36 void SigninManager::Initialize(Profile* profile) { | 43 void SigninManager::Initialize(Profile* profile) { |
37 profile_ = profile; | 44 profile_ = profile; |
38 username_ = profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername); | 45 SetUsername(profile_->GetPrefs()->GetString(prefs::kGoogleServicesUsername)); |
39 profile_->GetTokenService()->Initialize( | 46 profile_->GetTokenService()->Initialize( |
40 GaiaConstants::kChromeSource, profile_); | 47 GaiaConstants::kChromeSource, profile_); |
41 if (!username_.empty()) { | 48 if (!GetUsername().empty()) { |
42 profile_->GetTokenService()->LoadTokensFromDB(); | 49 profile_->GetTokenService()->LoadTokensFromDB(); |
43 } | 50 } |
44 } | 51 } |
45 | 52 |
46 bool SigninManager::IsInitialized() const { | 53 bool SigninManager::IsInitialized() const { |
47 return profile_ != NULL; | 54 return profile_ != NULL; |
48 } | 55 } |
49 | 56 |
50 void SigninManager::CleanupNotificationRegistration() { | 57 void SigninManager::CleanupNotificationRegistration() { |
51 #if !defined(OS_CHROMEOS) | 58 #if !defined(OS_CHROMEOS) |
52 Source<TokenService> token_service(profile_->GetTokenService()); | 59 Source<TokenService> token_service(profile_->GetTokenService()); |
53 if (registrar_.IsRegistered(this, | 60 if (registrar_.IsRegistered(this, |
54 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 61 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
55 token_service)) { | 62 token_service)) { |
56 registrar_.Remove(this, | 63 registrar_.Remove(this, |
57 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 64 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
58 token_service); | 65 token_service); |
59 } | 66 } |
60 #endif | 67 #endif |
61 } | 68 } |
62 | 69 |
63 // If a username already exists, the user is logged in. | 70 // If a username already exists, the user is logged in. |
64 const std::string& SigninManager::GetUsername() { | 71 const std::string& SigninManager::GetUsername() { |
72 if (IsUsingOAuth()) | |
akalin
2011/08/11 18:03:37
Consider:
return IsUsingOAuth() ? oauth_username_
Rick Campbell
2011/08/11 21:04:08
Done.
| |
73 return oauth_username_; | |
65 return username_; | 74 return username_; |
66 } | 75 } |
67 | 76 |
68 void SigninManager::SetUsername(const std::string& username) { | 77 void SigninManager::SetUsername(const std::string& username) { |
69 username_ = username; | 78 if (IsUsingOAuth()) |
akalin
2011/08/11 18:03:37
Consider:
(IsUsingOAuth() ? oauth_username_ : use
Rick Campbell
2011/08/11 21:04:08
As we discussed out-of-band, I'm going to leave th
| |
79 oauth_username_ = username; | |
80 else | |
81 username_ = username; | |
70 } | 82 } |
71 | 83 |
72 // static | 84 // static |
73 void SigninManager::PrepareForSignin() { | 85 void SigninManager::PrepareForSignin() { |
74 DCHECK(username_.empty()); | 86 DCHECK(username_.empty()); |
75 #if !defined(OS_CHROMEOS) | 87 #if !defined(OS_CHROMEOS) |
76 // The Sign out should clear the token service credentials. | 88 // The Sign out should clear the token service credentials. |
77 // Note: In CHROMEOS we might have valid credentials but still need to | 89 // Note: In CHROMEOS we might have valid credentials but still need to |
78 // set up 2-factor authentication. | 90 // set up 2-factor authentication. |
79 DCHECK(!profile_->GetTokenService()->AreCredentialsValid()); | 91 DCHECK(!profile_->GetTokenService()->AreCredentialsValid()); |
80 #endif | 92 #endif |
81 } | 93 } |
82 | 94 |
95 // static | |
96 void SigninManager::PrepareForOAuthSignin() { | |
97 DCHECK(oauth_username_.empty()); | |
98 #if !defined(OS_CHROMEOS) | |
99 // The Sign out should clear the token service credentials. | |
100 // Note: In CHROMEOS we might have valid credentials but still need to | |
101 // set up 2-factor authentication. | |
102 DCHECK(!profile_->GetTokenService()->AreOAuthCredentialsValid()); | |
103 #endif | |
104 } | |
105 | |
83 // Users must always sign out before they sign in again. | 106 // Users must always sign out before they sign in again. |
84 void SigninManager::StartOAuthSignIn() { | 107 void SigninManager::StartOAuthSignIn() { |
85 PrepareForSignin(); | 108 PrepareForOAuthSignin(); |
86 oauth_login_.reset(new GaiaOAuthFetcher(this, | 109 oauth_login_.reset(new GaiaOAuthFetcher(this, |
87 profile_->GetRequestContext(), | 110 profile_->GetRequestContext(), |
88 profile_, | 111 profile_, |
89 GaiaConstants::kSyncServiceOAuth)); | 112 GaiaConstants::kSyncServiceOAuth)); |
90 oauth_login_->StartGetOAuthToken(); | 113 oauth_login_->StartGetOAuthToken(); |
114 // TODO(rogerta?): Bug 92325: Expand Autologin to include OAuth signin | |
91 } | 115 } |
92 | 116 |
93 // Users must always sign out before they sign in again. | 117 // Users must always sign out before they sign in again. |
94 void SigninManager::StartSignIn(const std::string& username, | 118 void SigninManager::StartSignIn(const std::string& username, |
95 const std::string& password, | 119 const std::string& password, |
96 const std::string& login_token, | 120 const std::string& login_token, |
97 const std::string& login_captcha) { | 121 const std::string& login_captcha) { |
98 PrepareForSignin(); | 122 PrepareForSignin(); |
99 username_.assign(username); | 123 username_.assign(username); |
100 password_.assign(password); | 124 password_.assign(password); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
139 | 163 |
140 void SigninManager::SignOut() { | 164 void SigninManager::SignOut() { |
141 if (!profile_) | 165 if (!profile_) |
142 return; | 166 return; |
143 | 167 |
144 CleanupNotificationRegistration(); | 168 CleanupNotificationRegistration(); |
145 | 169 |
146 client_login_.reset(); | 170 client_login_.reset(); |
147 last_result_ = ClientLoginResult(); | 171 last_result_ = ClientLoginResult(); |
148 username_.clear(); | 172 username_.clear(); |
173 oauth_username_.clear(); | |
149 password_.clear(); | 174 password_.clear(); |
150 had_two_factor_error_ = false; | 175 had_two_factor_error_ = false; |
151 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, username_); | 176 profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesUsername); |
177 profile_->GetPrefs()->ClearPref(prefs::kSyncUsingOAuth); | |
152 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); | 178 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); |
153 profile_->GetTokenService()->ResetCredentialsInMemory(); | 179 profile_->GetTokenService()->ResetCredentialsInMemory(); |
154 profile_->GetTokenService()->EraseTokensFromDB(); | 180 profile_->GetTokenService()->EraseTokensFromDB(); |
155 } | 181 } |
156 | 182 |
157 void SigninManager::OnClientLoginSuccess(const ClientLoginResult& result) { | 183 void SigninManager::OnClientLoginSuccess(const ClientLoginResult& result) { |
158 last_result_ = result; | 184 last_result_ = result; |
159 // Make a request for the canonical email address. | 185 // Make a request for the canonical email address. |
160 client_login_->StartGetUserInfo(result.lsid, kGetInfoEmailKey); | 186 client_login_->StartGetUserInfo(result.lsid, kGetInfoEmailKey); |
161 } | 187 } |
162 | 188 |
189 // NOTE: GetUserInfo is a ClientLogin request similar to OAuth's userinfo | |
163 void SigninManager::OnGetUserInfoSuccess(const std::string& key, | 190 void SigninManager::OnGetUserInfoSuccess(const std::string& key, |
164 const std::string& value) { | 191 const std::string& value) { |
165 DCHECK(key == kGetInfoEmailKey); | 192 DCHECK(key == kGetInfoEmailKey); |
166 | 193 |
167 username_ = value; | 194 username_ = value; |
168 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, username_); | 195 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, username_); |
196 profile_->GetPrefs()->SetBoolean(prefs::kSyncUsingOAuth, false); | |
169 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); | 197 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); |
170 | 198 |
171 GoogleServiceSigninSuccessDetails details(username_, password_); | 199 GoogleServiceSigninSuccessDetails details(username_, password_); |
172 NotificationService::current()->Notify( | 200 NotificationService::current()->Notify( |
173 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 201 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
174 Source<Profile>(profile_), | 202 Source<Profile>(profile_), |
175 Details<const GoogleServiceSigninSuccessDetails>(&details)); | 203 Details<const GoogleServiceSigninSuccessDetails>(&details)); |
176 | 204 |
177 password_.clear(); // Don't need it anymore. | 205 password_.clear(); // Don't need it anymore. |
178 | 206 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 } | 247 } |
220 | 248 |
221 SignOut(); | 249 SignOut(); |
222 } | 250 } |
223 | 251 |
224 void SigninManager::OnGetOAuthTokenSuccess(const std::string& oauth_token) { | 252 void SigninManager::OnGetOAuthTokenSuccess(const std::string& oauth_token) { |
225 VLOG(1) << "SigninManager::SigninManager::OnGetOAuthTokenSuccess"; | 253 VLOG(1) << "SigninManager::SigninManager::OnGetOAuthTokenSuccess"; |
226 } | 254 } |
227 | 255 |
228 void SigninManager::OnGetOAuthTokenFailure() { | 256 void SigninManager::OnGetOAuthTokenFailure() { |
229 VLOG(1) << "SigninManager::OnGetOAuthTokenFailure"; | 257 LOG(WARNING) << "SigninManager::OnGetOAuthTokenFailure"; |
230 } | 258 } |
231 | 259 |
232 void SigninManager::OnOAuthGetAccessTokenSuccess(const std::string& token, | 260 void SigninManager::OnOAuthGetAccessTokenSuccess(const std::string& token, |
233 const std::string& secret) { | 261 const std::string& secret) { |
234 VLOG(1) << "SigninManager::OnOAuthGetAccessTokenSuccess"; | 262 VLOG(1) << "SigninManager::OnOAuthGetAccessTokenSuccess"; |
263 profile_->GetTokenService()->UpdateOAuthCredentials(token, secret); | |
235 } | 264 } |
236 | 265 |
237 void SigninManager::OnOAuthGetAccessTokenFailure( | 266 void SigninManager::OnOAuthGetAccessTokenFailure( |
238 const GoogleServiceAuthError& error) { | 267 const GoogleServiceAuthError& error) { |
239 VLOG(1) << "SigninManager::OnOAuthGetAccessTokenFailure"; | 268 LOG(WARNING) << "SigninManager::OnOAuthGetAccessTokenFailure"; |
240 } | 269 } |
241 | 270 |
242 void SigninManager::OnOAuthWrapBridgeSuccess(const std::string& service_name, | 271 void SigninManager::OnOAuthWrapBridgeSuccess(const std::string& service_name, |
243 const std::string& token, | 272 const std::string& token, |
244 const std::string& expires_in) { | 273 const std::string& expires_in) { |
245 VLOG(1) << "SigninManager::OnOAuthWrapBridgeSuccess"; | 274 VLOG(1) << "SigninManager::OnOAuthWrapBridgeSuccess"; |
246 } | 275 } |
247 | 276 |
248 void SigninManager::OnOAuthWrapBridgeFailure( | 277 void SigninManager::OnOAuthWrapBridgeFailure( |
249 const std::string& service_scope, | 278 const std::string& service_scope, |
250 const GoogleServiceAuthError& error) { | 279 const GoogleServiceAuthError& error) { |
251 VLOG(1) << "SigninManager::OnOAuthWrapBridgeFailure"; | 280 LOG(WARNING) << "SigninManager::OnOAuthWrapBridgeFailure"; |
252 } | 281 } |
253 | 282 |
283 // NOTE: userinfo is an OAuth request similar to ClientLogin's GetUserInfo | |
254 void SigninManager::OnUserInfoSuccess(const std::string& email) { | 284 void SigninManager::OnUserInfoSuccess(const std::string& email) { |
255 VLOG(1) << "SigninManager::OnUserInfoSuccess(\"" << email << "\")"; | 285 VLOG(1) << "Sync signin for " << email << " is complete."; |
akalin
2011/08/11 18:03:37
DCHECK(IsUsingOAuth()) ?
Rick Campbell
2011/08/11 21:04:08
Done. Added similar DCHECKs for SigninManager::On
| |
286 oauth_username_ = email; | |
287 profile_->GetPrefs()->SetString( | |
288 prefs::kGoogleServicesUsername, oauth_username_); | |
289 profile_->GetPrefs()->SetBoolean(prefs::kSyncUsingOAuth, true); | |
290 profile_->GetPrefs()->ScheduleSavePersistentPrefs(); | |
291 | |
292 DCHECK(password_.empty()); | |
293 GoogleServiceSigninSuccessDetails details(oauth_username_, ""); | |
294 NotificationService::current()->Notify( | |
295 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | |
296 Source<Profile>(profile_), | |
297 Details<const GoogleServiceSigninSuccessDetails>(&details)); | |
298 | |
299 DCHECK(profile_->GetTokenService()->AreOAuthCredentialsValid()); | |
300 profile_->GetTokenService()->StartFetchingOAuthTokens(); | |
256 } | 301 } |
257 | 302 |
258 void SigninManager::OnUserInfoFailure(const GoogleServiceAuthError& error) { | 303 void SigninManager::OnUserInfoFailure(const GoogleServiceAuthError& error) { |
259 VLOG(1) << "SigninManager::OnUserInfoFailure"; | 304 LOG(WARNING) << "SigninManager::OnUserInfoFailure"; |
260 } | 305 } |
261 | 306 |
262 void SigninManager::Observe(int type, | 307 void SigninManager::Observe(int type, |
263 const NotificationSource& source, | 308 const NotificationSource& source, |
264 const NotificationDetails& details) { | 309 const NotificationDetails& details) { |
265 #if !defined(OS_CHROMEOS) | 310 #if !defined(OS_CHROMEOS) |
266 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE); | 311 DCHECK(type == chrome::NOTIFICATION_TOKEN_AVAILABLE); |
267 TokenService::TokenAvailableDetails* tok_details = | 312 TokenService::TokenAvailableDetails* tok_details = |
268 Details<TokenService::TokenAvailableDetails>(details).ptr(); | 313 Details<TokenService::TokenAvailableDetails>(details).ptr(); |
269 | 314 |
270 // If a GAIA service token has become available, use it to pre-login the | 315 // If a GAIA service token has become available, use it to pre-login the |
271 // user to other services that depend on GAIA credentials. | 316 // user to other services that depend on GAIA credentials. |
272 if (tok_details->service() == GaiaConstants::kGaiaService) { | 317 if (tok_details->service() == GaiaConstants::kGaiaService) { |
273 if (client_login_.get() == NULL) { | 318 if (client_login_.get() == NULL) { |
274 client_login_.reset(new GaiaAuthFetcher(this, | 319 client_login_.reset(new GaiaAuthFetcher(this, |
275 GaiaConstants::kChromeSource, | 320 GaiaConstants::kChromeSource, |
276 profile_->GetRequestContext())); | 321 profile_->GetRequestContext())); |
277 } | 322 } |
278 | 323 |
279 client_login_->StartTokenAuth(tok_details->token()); | 324 client_login_->StartTokenAuth(tok_details->token()); |
280 | 325 |
281 // We only want to do this once per sign-in. | 326 // We only want to do this once per sign-in. |
282 CleanupNotificationRegistration(); | 327 CleanupNotificationRegistration(); |
283 } | 328 } |
284 #endif | 329 #endif |
285 } | 330 } |
OLD | NEW |