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

Side by Side Diff: chrome/browser/chromeos/login/login_utils.cc

Issue 8499021: UserPolicyCache only becomes ready after policy has been fetched. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix pointer typo Created 9 years, 1 month 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 | Annotate | Revision Log
OLDNEW
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/chromeos/login/login_utils.h" 5 #include "chrome/browser/chromeos/login/login_utils.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 const char kServiceScopeChromeOS[] = 95 const char kServiceScopeChromeOS[] =
96 "https://www.googleapis.com/auth/chromesync"; 96 "https://www.googleapis.com/auth/chromesync";
97 97
98 const char kServiceScopeChromeOSDeviceManagement[] = 98 const char kServiceScopeChromeOSDeviceManagement[] =
99 "https://www.googleapis.com/auth/chromeosdevicemanagement"; 99 "https://www.googleapis.com/auth/chromeosdevicemanagement";
100 } // namespace 100 } // namespace
101 101
102 // Task for fetching tokens from UI thread. 102 // Task for fetching tokens from UI thread.
103 class StartSyncOnUIThreadTask : public Task { 103 class StartSyncOnUIThreadTask : public Task {
104 public: 104 public:
105 StartSyncOnUIThreadTask( 105 explicit StartSyncOnUIThreadTask(
106 const GaiaAuthConsumer::ClientLoginResult& credentials) 106 const GaiaAuthConsumer::ClientLoginResult& credentials)
107 : credentials_(credentials) {} 107 : credentials_(credentials) {}
108 virtual ~StartSyncOnUIThreadTask() {} 108 virtual ~StartSyncOnUIThreadTask() {}
109 109
110 // Task override. 110 // Task override.
111 virtual void Run() { 111 virtual void Run() OVERRIDE {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
113 LoginUtils::Get()->FetchCookies(ProfileManager::GetDefaultProfile(), 113 LoginUtils::Get()->FetchCookies(ProfileManager::GetDefaultProfile(),
114 credentials_); 114 credentials_);
115 LoginUtils::Get()->StartSync(ProfileManager::GetDefaultProfile(), 115 LoginUtils::Get()->StartSync(ProfileManager::GetDefaultProfile(),
116 credentials_); 116 credentials_);
117 } 117 }
118 118
119 private: 119 private:
120 GaiaAuthConsumer::ClientLoginResult credentials_; 120 GaiaAuthConsumer::ClientLoginResult credentials_;
121
122 DISALLOW_COPY_AND_ASSIGN(StartSyncOnUIThreadTask);
121 }; 123 };
122 124
123 // Transfers initial set of Profile cookies from the default profile. 125 // Transfers initial set of Profile cookies from the default profile.
124 class TransferDefaultCookiesOnIOThreadTask : public Task { 126 class TransferDefaultCookiesOnIOThreadTask : public Task {
125 public: 127 public:
126 TransferDefaultCookiesOnIOThreadTask( 128 TransferDefaultCookiesOnIOThreadTask(
127 net::URLRequestContextGetter* auth_context, 129 net::URLRequestContextGetter* auth_context,
128 net::URLRequestContextGetter* new_context) 130 net::URLRequestContextGetter* new_context)
129 : auth_context_(auth_context), 131 : auth_context_(auth_context),
130 new_context_(new_context) {} 132 new_context_(new_context) {}
131 virtual ~TransferDefaultCookiesOnIOThreadTask() {} 133 virtual ~TransferDefaultCookiesOnIOThreadTask() {}
132 134
133 // Task override. 135 // Task override.
134 virtual void Run() { 136 virtual void Run() OVERRIDE {
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
136 net::CookieStore* default_store = 138 net::CookieStore* default_store =
137 auth_context_->GetURLRequestContext()->cookie_store(); 139 auth_context_->GetURLRequestContext()->cookie_store();
138 net::CookieMonster* default_monster = default_store->GetCookieMonster(); 140 net::CookieMonster* default_monster = default_store->GetCookieMonster();
139 default_monster->SetKeepExpiredCookies(); 141 default_monster->SetKeepExpiredCookies();
140 default_monster->GetAllCookiesAsync( 142 default_monster->GetAllCookiesAsync(
141 base::Bind( 143 base::Bind(
142 &TransferDefaultCookiesOnIOThreadTask::InitializeCookieMonster, 144 &TransferDefaultCookiesOnIOThreadTask::InitializeCookieMonster,
143 base::Unretained(this))); 145 base::Unretained(this)));
144 } 146 }
145 147
146 void InitializeCookieMonster(const net::CookieList& cookies) { 148 void InitializeCookieMonster(const net::CookieList& cookies) {
147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
148 net::CookieStore* new_store = 150 net::CookieStore* new_store =
149 new_context_->GetURLRequestContext()->cookie_store(); 151 new_context_->GetURLRequestContext()->cookie_store();
150 net::CookieMonster* new_monster = new_store->GetCookieMonster(); 152 net::CookieMonster* new_monster = new_store->GetCookieMonster();
151 153
152 if (!new_monster->InitializeFrom(cookies)) { 154 if (!new_monster->InitializeFrom(cookies)) {
153 LOG(WARNING) << "Failed initial cookie transfer."; 155 LOG(WARNING) << "Failed initial cookie transfer.";
154 } 156 }
155 } 157 }
156 158
157 private: 159 private:
158 net::URLRequestContextGetter* auth_context_; 160 net::URLRequestContextGetter* auth_context_;
159 net::URLRequestContextGetter* new_context_; 161 net::URLRequestContextGetter* new_context_;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 GaiaConstants::kPicasaService, 218 GaiaConstants::kPicasaService,
217 oauth1_token_, 219 oauth1_token_,
218 oauth1_secret_); 220 oauth1_secret_);
219 } 221 }
220 } 222 }
221 223
222 // GaiaOAuthConsumer implementation: 224 // GaiaOAuthConsumer implementation:
223 virtual void OnOAuthLoginSuccess(const std::string& sid, 225 virtual void OnOAuthLoginSuccess(const std::string& sid,
224 const std::string& lsid, 226 const std::string& lsid,
225 const std::string& auth) OVERRIDE { 227 const std::string& auth) OVERRIDE {
226 GaiaAuthConsumer::ClientLoginResult credentials(sid, 228 GaiaAuthConsumer::ClientLoginResult credentials(
227 lsid, auth, std::string()); 229 sid, lsid, auth, std::string());
228 UserManager::Get()->set_offline_login(false); 230 UserManager::Get()->set_offline_login(false);
229 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 231 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
230 new StartSyncOnUIThreadTask(credentials)); 232 new StartSyncOnUIThreadTask(credentials));
231 } 233 }
232 234
233 virtual void OnOAuthLoginFailure( 235 virtual void OnOAuthLoginFailure(
234 const GoogleServiceAuthError& error) OVERRIDE { 236 const GoogleServiceAuthError& error) OVERRIDE {
235 LOG(WARNING) << "Failed to verify OAuth1 access tokens," 237 LOG(WARNING) << "Failed to verify OAuth1 access tokens, error: "
236 << " error.state=" << error.state(); 238 << error.state();
237 239
238 // Mark this account's OAuth token state as invalid if the failure is not 240 // Mark this account's OAuth token state as invalid if the failure is not
239 // caused by network error. 241 // caused by network error.
240 if (error.state() != GoogleServiceAuthError::CONNECTION_FAILED) { 242 if (error.state() != GoogleServiceAuthError::CONNECTION_FAILED) {
241 UserManager::Get()->SaveUserOAuthStatus(username_, 243 UserManager::Get()->SaveUserOAuthStatus(username_,
242 User::OAUTH_TOKEN_STATUS_INVALID); 244 User::OAUTH_TOKEN_STATUS_INVALID);
243 } else { 245 } else {
244 UserManager::Get()->set_offline_login(true); 246 UserManager::Get()->set_offline_login(true);
245 } 247 }
246 } 248 }
(...skipping 23 matching lines...) Expand all
270 } 272 }
271 273
272 // GaiaAuthConsumer overrides. 274 // GaiaAuthConsumer overrides.
273 virtual void OnIssueAuthTokenSuccess(const std::string& service, 275 virtual void OnIssueAuthTokenSuccess(const std::string& service,
274 const std::string& auth_token) OVERRIDE { 276 const std::string& auth_token) OVERRIDE {
275 gaia_fetcher_.StartMergeSession(auth_token); 277 gaia_fetcher_.StartMergeSession(auth_token);
276 } 278 }
277 279
278 virtual void OnIssueAuthTokenFailure(const std::string& service, 280 virtual void OnIssueAuthTokenFailure(const std::string& service,
279 const GoogleServiceAuthError& error) OVERRIDE { 281 const GoogleServiceAuthError& error) OVERRIDE {
280 LOG(WARNING) << "Failed IssueAuthToken request," 282 LOG(WARNING) << "Failed IssueAuthToken request, error: " << error.state();
281 << " error.state=" << error.state();
282 HandlerGaiaAuthError(error); 283 HandlerGaiaAuthError(error);
283 delete this; 284 delete this;
284 } 285 }
285 286
286 virtual void OnMergeSessionSuccess(const std::string& data) OVERRIDE { 287 virtual void OnMergeSessionSuccess(const std::string& data) OVERRIDE {
287 VLOG(1) << "MergeSession successful."; 288 VLOG(1) << "MergeSession successful.";
288 delete this; 289 delete this;
289 } 290 }
290 291
291 virtual void OnMergeSessionFailure( 292 virtual void OnMergeSessionFailure(
292 const GoogleServiceAuthError& error) OVERRIDE { 293 const GoogleServiceAuthError& error) OVERRIDE {
293 LOG(WARNING) << "Failed MergeSession request," 294 LOG(WARNING) << "Failed MergeSession request, error: " << error.state();
294 << " error.state=" << error.state();
295 HandlerGaiaAuthError(error); 295 HandlerGaiaAuthError(error);
296 delete this; 296 delete this;
297 } 297 }
298 298
299 private: 299 private:
300 void HandlerGaiaAuthError(const GoogleServiceAuthError& error) { 300 void HandlerGaiaAuthError(const GoogleServiceAuthError& error) {
301 // Mark this account's login state as offline if we encountered a network 301 // Mark this account's login state as offline if we encountered a network
302 // error. That will make us verify user OAuth token and try to fetch session 302 // error. That will make us verify user OAuth token and try to fetch session
303 // cookies again once we detect that the machine comes online. 303 // cookies again once we detect that the machine comes online.
304 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) 304 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED)
305 UserManager::Get()->set_offline_login(true); 305 UserManager::Get()->set_offline_login(true);
306 } 306 }
307 307
308 GaiaAuthFetcher gaia_fetcher_; 308 GaiaAuthFetcher gaia_fetcher_;
309 DISALLOW_COPY_AND_ASSIGN(UserSessionCookieFetcher); 309 DISALLOW_COPY_AND_ASSIGN(UserSessionCookieFetcher);
310 }; 310 };
311 311
312 312 // Fetches the oauth token for the device management service. Since Profile
313 // Fetches an OAuth token and initializes user policy with it. 313 // creation might be blocking on a user policy fetch, this fetcher must always
314 // send a (possibly empty) token to the BrowserPolicyConnector, which will then
315 // let the policy subsystem proceed and resume Profile creation.
316 // Sending the token even when no Profile is pending is also OK.
314 class PolicyOAuthFetcher : public GaiaOAuthConsumer { 317 class PolicyOAuthFetcher : public GaiaOAuthConsumer {
315 public: 318 public:
319 // Fetches the device management service's oauth token using |oauth1_token|
320 // and |oauth1_secret| as access tokens.
316 PolicyOAuthFetcher(Profile* profile, 321 PolicyOAuthFetcher(Profile* profile,
317 const std::string& oauth1_token, 322 const std::string& oauth1_token,
318 const std::string& oauth1_secret) 323 const std::string& oauth1_secret)
319 : oauth_fetcher_(this, 324 : oauth_fetcher_(this,
320 profile->GetRequestContext(), 325 profile->GetRequestContext(),
321 profile, 326 profile,
322 kServiceScopeChromeOSDeviceManagement), 327 kServiceScopeChromeOSDeviceManagement),
323 oauth1_token_(oauth1_token), 328 oauth1_token_(oauth1_token),
324 oauth1_secret_(oauth1_secret) { 329 oauth1_secret_(oauth1_secret) {
325 oauth_fetcher_.SetAutoFetchLimit(
326 GaiaOAuthFetcher::OAUTH2_SERVICE_ACCESS_TOKEN);
327 } 330 }
331
332 // Fetches the device management service's oauth token, after also retrieving
333 // the access tokens.
334 explicit PolicyOAuthFetcher(Profile* profile)
335 : oauth_fetcher_(this,
336 profile->GetRequestContext(),
337 profile,
338 kServiceScopeChromeOSDeviceManagement) {
339 }
340
328 virtual ~PolicyOAuthFetcher() {} 341 virtual ~PolicyOAuthFetcher() {}
329 342
330 void Start() { 343 void Start() {
331 oauth_fetcher_.StartOAuthWrapBridge( 344 oauth_fetcher_.SetAutoFetchLimit(
332 oauth1_token_, oauth1_secret_, GaiaConstants::kGaiaOAuthDuration, 345 GaiaOAuthFetcher::OAUTH2_SERVICE_ACCESS_TOKEN);
333 std::string(kServiceScopeChromeOSDeviceManagement)); 346
347 if (oauth1_token_.empty()) {
348 oauth_fetcher_.StartGetOAuthTokenRequest();
349 } else {
350 oauth_fetcher_.StartOAuthWrapBridge(
351 oauth1_token_, oauth1_secret_, GaiaConstants::kGaiaOAuthDuration,
352 std::string(kServiceScopeChromeOSDeviceManagement));
353 }
334 } 354 }
335 355
336 // GaiaOAuthConsumer implementation: 356 const std::string& oauth1_token() const { return oauth1_token_; }
357 const std::string& oauth1_secret() const { return oauth1_secret_; }
358 const std::string& policy_token() const { return policy_token_; }
359
360 private:
361 virtual void OnGetOAuthTokenSuccess(const std::string& oauth_token) OVERRIDE {
362 VLOG(1) << "Got OAuth request token";
363 }
364
365 virtual void OnGetOAuthTokenFailure(
366 const GoogleServiceAuthError& error) OVERRIDE {
367 LOG(WARNING) << "Failed to get OAuth request token, error: "
368 << error.state();
369 SetPolicyToken("");
370 }
371
372 virtual void OnOAuthGetAccessTokenSuccess(
373 const std::string& token,
374 const std::string& secret) OVERRIDE {
375 VLOG(1) << "Got OAuth access token";
376 oauth1_token_ = token;
377 oauth1_secret_ = secret;
378 }
379
380 virtual void OnOAuthGetAccessTokenFailure(
381 const GoogleServiceAuthError& error) OVERRIDE {
382 LOG(WARNING) << "Failed to get OAuth access token, error: "
383 << error.state();
384 SetPolicyToken("");
385 }
386
337 virtual void OnOAuthWrapBridgeSuccess( 387 virtual void OnOAuthWrapBridgeSuccess(
338 const std::string& service_name, 388 const std::string& service_name,
339 const std::string& token, 389 const std::string& token,
340 const std::string& expires_in) OVERRIDE { 390 const std::string& expires_in) OVERRIDE {
341 policy::BrowserPolicyConnector* browser_policy_connector = 391 VLOG(1) << "Got OAuth access token for " << service_name;
342 g_browser_process->browser_policy_connector(); 392 SetPolicyToken(token);
343 browser_policy_connector->RegisterForUserPolicy(token);
344 } 393 }
345 394
346 virtual void OnOAuthWrapBridgeFailure( 395 virtual void OnOAuthWrapBridgeFailure(
347 const std::string& service_name, 396 const std::string& service_name,
348 const GoogleServiceAuthError& error) OVERRIDE { 397 const GoogleServiceAuthError& error) OVERRIDE {
349 LOG(WARNING) << "Failed to get OAuth access token for " << service_name; 398 LOG(WARNING) << "Failed to get OAuth access token for " << service_name
399 << ", error: " << error.state();
400 SetPolicyToken("");
350 } 401 }
351 402
352 private: 403 void SetPolicyToken(const std::string& token) {
404 policy_token_ = token;
405 g_browser_process->browser_policy_connector()->RegisterForUserPolicy(token);
406 }
407
353 GaiaOAuthFetcher oauth_fetcher_; 408 GaiaOAuthFetcher oauth_fetcher_;
354 std::string oauth1_token_; 409 std::string oauth1_token_;
355 std::string oauth1_secret_; 410 std::string oauth1_secret_;
411 std::string policy_token_;
356 412
357 DISALLOW_COPY_AND_ASSIGN(PolicyOAuthFetcher); 413 DISALLOW_COPY_AND_ASSIGN(PolicyOAuthFetcher);
358 }; 414 };
359 415
360 // Used to request a restart to switch to the guest mode. 416 // Used to request a restart to switch to the guest mode.
361 class JobRestartRequest 417 class JobRestartRequest
362 : public base::RefCountedThreadSafe<JobRestartRequest> { 418 : public base::RefCountedThreadSafe<JobRestartRequest> {
363 public: 419 public:
364 JobRestartRequest(int pid, const std::string& command_line) 420 JobRestartRequest(int pid, const std::string& command_line)
365 : pid_(pid), 421 : pid_(pid),
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 Profile* new_profile) OVERRIDE; 508 Profile* new_profile) OVERRIDE;
453 virtual void TransferDefaultAuthCache(Profile* default_profile, 509 virtual void TransferDefaultAuthCache(Profile* default_profile,
454 Profile* new_profile) OVERRIDE; 510 Profile* new_profile) OVERRIDE;
455 511
456 // ProfileManagerObserver implementation: 512 // ProfileManagerObserver implementation:
457 virtual void OnProfileCreated(Profile* profile, Status status) OVERRIDE; 513 virtual void OnProfileCreated(Profile* profile, Status status) OVERRIDE;
458 514
459 // GaiaOAuthConsumer overrides. 515 // GaiaOAuthConsumer overrides.
460 virtual void OnGetOAuthTokenSuccess(const std::string& oauth_token) OVERRIDE; 516 virtual void OnGetOAuthTokenSuccess(const std::string& oauth_token) OVERRIDE;
461 virtual void OnGetOAuthTokenFailure( 517 virtual void OnGetOAuthTokenFailure(
462 const GoogleServiceAuthError& error) OVERRIDE; 518 const GoogleServiceAuthError& error) OVERRIDE;
463 virtual void OnOAuthGetAccessTokenSuccess(const std::string& token, 519 virtual void OnOAuthGetAccessTokenSuccess(const std::string& token,
464 const std::string& secret) OVERRIDE; 520 const std::string& secret) OVERRIDE;
465 virtual void OnOAuthGetAccessTokenFailure( 521 virtual void OnOAuthGetAccessTokenFailure(
466 const GoogleServiceAuthError& error) OVERRIDE; 522 const GoogleServiceAuthError& error) OVERRIDE;
467 523
468 // net::NetworkChangeNotifier::OnlineStateObserver overrides. 524 // net::NetworkChangeNotifier::OnlineStateObserver overrides.
469 virtual void OnOnlineStateChanged(bool online) OVERRIDE; 525 virtual void OnOnlineStateChanged(bool online) OVERRIDE;
470 526
471 // Given the authenticated credentials from the cookie jar, try to exchange 527 // Given the authenticated credentials from the cookie jar, try to exchange
472 // fetch OAuth request, v1 and v2 tokens. 528 // fetch OAuth request, v1 and v2 tokens.
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 649
594 username_ = username; 650 username_ = username;
595 password_ = password; 651 password_ = password;
596 652
597 credentials_ = credentials; 653 credentials_ = credentials;
598 pending_requests_ = pending_requests; 654 pending_requests_ = pending_requests;
599 using_oauth_ = using_oauth; 655 using_oauth_ = using_oauth;
600 has_cookies_ = has_cookies; 656 has_cookies_ = has_cookies;
601 delegate_ = delegate; 657 delegate_ = delegate;
602 658
659 policy::BrowserPolicyConnector* connector =
660 g_browser_process->browser_policy_connector();
661
662 // If this is an enterprise device and the user belongs to the enterprise
663 // domain, then wait for a policy fetch before logging the user in. This
664 // will delay Profile creation until the policy is fetched, so that features
665 // controlled by policy (e.g. Sync, Startup tabs) only start after the
666 // PrefService has the right values.
667 // Profile creation is also resumed if the fetch attempt fails.
668 bool wait_for_policy_fetch =
669 using_oauth_ &&
670 authenticator_.get() &&
671 (connector->GetUserAffiliation(username) ==
672 policy::CloudPolicyDataStore::USER_AFFILIATION_MANAGED);
673
603 // Initialize user policy before the profile is created so the profile 674 // Initialize user policy before the profile is created so the profile
604 // initialization code sees the policy settings. 675 // initialization code sees the cached policy settings.
605 g_browser_process->browser_policy_connector()->InitializeUserPolicy(username); 676 connector->InitializeUserPolicy(username, wait_for_policy_fetch);
677
678 if (wait_for_policy_fetch) {
679 // Profile creation will block until user policy is fetched, which
680 // requires the DeviceManagement token. Try to fetch it now.
681 VLOG(1) << "Profile creation requires policy token, fetching now";
682 policy_oauth_fetcher_.reset(
683 new PolicyOAuthFetcher(authenticator_->authentication_profile()));
684 policy_oauth_fetcher_->Start();
685 }
606 686
607 // The default profile will have been changed because the ProfileManager 687 // The default profile will have been changed because the ProfileManager
608 // will process the notification that the UserManager sends out. 688 // will process the notification that the UserManager sends out.
609 ProfileManager::CreateDefaultProfileAsync(this); 689 ProfileManager::CreateDefaultProfileAsync(this);
610 } 690 }
611 691
612 void LoginUtilsImpl::DelegateDeleted(Delegate* delegate) { 692 void LoginUtilsImpl::DelegateDeleted(Delegate* delegate) {
613 if (delegate_ == delegate) 693 if (delegate_ == delegate)
614 delegate_ = NULL; 694 delegate_ = NULL;
615 } 695 }
616 696
617 void LoginUtilsImpl::OnProfileCreated(Profile* user_profile, Status status) { 697 void LoginUtilsImpl::OnProfileCreated(Profile* user_profile, Status status) {
618 CHECK(user_profile); 698 CHECK(user_profile);
619 switch (status) { 699 switch (status) {
620 case STATUS_INITIALIZED: 700 case STATUS_INITIALIZED:
621 break; 701 break;
622 case STATUS_CREATED: 702 case STATUS_CREATED:
623 if (UserManager::Get()->current_user_is_new()) 703 if (UserManager::Get()->current_user_is_new())
624 SetFirstLoginPrefs(user_profile->GetPrefs()); 704 SetFirstLoginPrefs(user_profile->GetPrefs());
625 RespectLocalePreference(user_profile); 705 RespectLocalePreference(user_profile);
626 return; 706 return;
627 case STATUS_FAIL: 707 case STATUS_FAIL:
628 default: 708 default:
629 NOTREACHED(); 709 NOTREACHED();
630 return; 710 return;
631 } 711 }
632 712
633 // Initialize the user-policy backend. 713 // Initialize the user-policy backend.
634 policy::BrowserPolicyConnector* browser_policy_connector =
635 g_browser_process->browser_policy_connector();
636
637 if (!using_oauth_) { 714 if (!using_oauth_) {
638 browser_policy_connector->SetUserPolicyTokenService( 715 g_browser_process->browser_policy_connector()->
639 user_profile->GetTokenService()); 716 SetUserPolicyTokenService(user_profile->GetTokenService());
640 } 717 }
641 718
642 // We suck. This is a hack since we do not have the enterprise feature 719 // We suck. This is a hack since we do not have the enterprise feature
643 // done yet to pull down policies from the domain admin. We'll take this 720 // done yet to pull down policies from the domain admin. We'll take this
644 // out when we get that done properly. 721 // out when we get that done properly.
645 // TODO(xiyuan): Remove this once enterprise feature is ready. 722 // TODO(xiyuan): Remove this once enterprise feature is ready.
646 if (EndsWith(username_, "@google.com", true)) { 723 if (EndsWith(username_, "@google.com", true)) {
647 PrefService* pref_service = user_profile->GetPrefs(); 724 PrefService* pref_service = user_profile->GetPrefs();
648 pref_service->SetBoolean(prefs::kEnableScreenLock, true); 725 pref_service->SetBoolean(prefs::kEnableScreenLock, true);
649 } 726 }
650 727
651 BootTimesLoader* btl = BootTimesLoader::Get(); 728 BootTimesLoader* btl = BootTimesLoader::Get();
652 btl->AddLoginTimeMarker("UserProfileGotten", false); 729 btl->AddLoginTimeMarker("UserProfileGotten", false);
653 730
654 if (using_oauth_) { 731 if (using_oauth_) {
732 // Reuse the access token fetched by the PolicyOAuthFetcher, if it was
733 // used to fetch policies before Profile creation.
734 if (policy_oauth_fetcher_.get() &&
735 !policy_oauth_fetcher_->oauth1_token().empty()) {
736 VLOG(1) << "Resuming profile creation after fetching policy token";
737 StoreOAuth1AccessToken(user_profile,
738 policy_oauth_fetcher_->oauth1_token(),
739 policy_oauth_fetcher_->oauth1_secret());
740 }
741
655 // Transfer cookies when user signs in using extension. 742 // Transfer cookies when user signs in using extension.
656 if (has_cookies_) { 743 if (has_cookies_) {
657 // Transfer cookies from the profile that was used for authentication. 744 // Transfer cookies from the profile that was used for authentication.
658 // This profile contains cookies that auth extension should have already 745 // This profile contains cookies that auth extension should have already
659 // put in place that will ensure that the newly created session is 746 // put in place that will ensure that the newly created session is
660 // authenticated for the websites that work with the used authentication 747 // authenticated for the websites that work with the used authentication
661 // schema. 748 // schema.
662 TransferDefaultCookies(authenticator_->authentication_profile(), 749 TransferDefaultCookies(authenticator_->authentication_profile(),
663 user_profile); 750 user_profile);
664 } 751 }
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 // TODO(altimofeev): Need to sanitize memory used to store password. 825 // TODO(altimofeev): Need to sanitize memory used to store password.
739 credentials_ = GaiaAuthConsumer::ClientLoginResult(); 826 credentials_ = GaiaAuthConsumer::ClientLoginResult();
740 } 827 }
741 828
742 void LoginUtilsImpl::FetchOAuth1AccessToken(Profile* auth_profile) { 829 void LoginUtilsImpl::FetchOAuth1AccessToken(Profile* auth_profile) {
743 oauth_fetcher_.reset(new GaiaOAuthFetcher(this, 830 oauth_fetcher_.reset(new GaiaOAuthFetcher(this,
744 auth_profile->GetRequestContext(), 831 auth_profile->GetRequestContext(),
745 auth_profile, 832 auth_profile,
746 kServiceScopeChromeOS)); 833 kServiceScopeChromeOS));
747 // Let's first get the Oauth request token and OAuth1 token+secret. 834 // Let's first get the Oauth request token and OAuth1 token+secret.
748 // One we get that, we will kick off individial requests for OAuth2 tokens for 835 // Once we get that, we will kick off individual requests for OAuth2 tokens
749 // all our services. 836 // for all our services.
750 oauth_fetcher_->SetAutoFetchLimit(GaiaOAuthFetcher::OAUTH1_ALL_ACCESS_TOKEN); 837 oauth_fetcher_->SetAutoFetchLimit(GaiaOAuthFetcher::OAUTH1_ALL_ACCESS_TOKEN);
751 oauth_fetcher_->StartGetOAuthTokenRequest(); 838 oauth_fetcher_->StartGetOAuthTokenRequest();
752 } 839 }
753 840
754 void LoginUtilsImpl::FetchCookies(Profile* user_profile, 841 void LoginUtilsImpl::FetchCookies(Profile* user_profile,
755 const GaiaAuthConsumer::ClientLoginResult& credentials) { 842 const GaiaAuthConsumer::ClientLoginResult& credentials) {
756 if (!using_oauth_) { 843 if (!using_oauth_) {
757 // Take the credentials passed in and try to exchange them for 844 // Take the credentials passed in and try to exchange them for
758 // full-fledged Google authentication cookies. This is 845 // full-fledged Google authentication cookies. This is
759 // best-effort; it's possible that we'll fail due to network 846 // best-effort; it's possible that we'll fail due to network
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
792 password_, false); 879 password_, false);
793 username_ = ""; 880 username_ = "";
794 password_ = ""; 881 password_ = "";
795 882
796 token_service->Initialize(GaiaConstants::kChromeOSSource, user_profile); 883 token_service->Initialize(GaiaConstants::kChromeOSSource, user_profile);
797 token_service->LoadTokensFromDB(); 884 token_service->LoadTokensFromDB();
798 } 885 }
799 token_service->UpdateCredentials(credentials); 886 token_service->UpdateCredentials(credentials);
800 if (token_service->AreCredentialsValid()) 887 if (token_service->AreCredentialsValid())
801 token_service->StartFetchingTokens(); 888 token_service->StartFetchingTokens();
802
803 } 889 }
804 890
805 void LoginUtilsImpl::RespectLocalePreference(Profile* profile) { 891 void LoginUtilsImpl::RespectLocalePreference(Profile* profile) {
806 DCHECK(profile != NULL); 892 DCHECK(profile != NULL);
807 PrefService* prefs = profile->GetPrefs(); 893 PrefService* prefs = profile->GetPrefs();
808 DCHECK(prefs != NULL); 894 DCHECK(prefs != NULL);
809 if (g_browser_process == NULL) 895 if (g_browser_process == NULL)
810 return; 896 return;
811 897
812 std::string pref_locale = prefs->GetString(prefs::kApplicationLocale); 898 std::string pref_locale = prefs->GetString(prefs::kApplicationLocale);
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 profile->GetRequestContext())); 1129 profile->GetRequestContext()));
1044 } 1130 }
1045 1131
1046 void LoginUtilsImpl::OnGetOAuthTokenSuccess(const std::string& oauth_token) { 1132 void LoginUtilsImpl::OnGetOAuthTokenSuccess(const std::string& oauth_token) {
1047 VLOG(1) << "Got OAuth request token!"; 1133 VLOG(1) << "Got OAuth request token!";
1048 } 1134 }
1049 1135
1050 void LoginUtilsImpl::OnGetOAuthTokenFailure( 1136 void LoginUtilsImpl::OnGetOAuthTokenFailure(
1051 const GoogleServiceAuthError& error) { 1137 const GoogleServiceAuthError& error) {
1052 // TODO(zelidrag): Pop up sync setup UI here? 1138 // TODO(zelidrag): Pop up sync setup UI here?
1053 LOG(WARNING) << "Failed fetching OAuth request token"; 1139 LOG(WARNING) << "Failed fetching OAuth request token, error: "
1140 << error.state();
1054 } 1141 }
1055 1142
1056 void LoginUtilsImpl::OnOAuthGetAccessTokenSuccess(const std::string& token, 1143 void LoginUtilsImpl::OnOAuthGetAccessTokenSuccess(const std::string& token,
1057 const std::string& secret) { 1144 const std::string& secret) {
1058 VLOG(1) << "Got OAuth v1 token!"; 1145 VLOG(1) << "Got OAuth v1 token!";
1059 Profile* user_profile = ProfileManager::GetDefaultProfile(); 1146 Profile* user_profile = ProfileManager::GetDefaultProfile();
1060 StoreOAuth1AccessToken(user_profile, token, secret); 1147 StoreOAuth1AccessToken(user_profile, token, secret);
1061 1148
1062 // Verify OAuth1 token by doing OAuthLogin and fetching credentials. 1149 // Verify OAuth1 token by doing OAuthLogin and fetching credentials.
1063 VerifyOAuth1AccessToken(user_profile, token, secret); 1150 VerifyOAuth1AccessToken(user_profile, token, secret);
1064 } 1151 }
1065 1152
1153 void LoginUtilsImpl::OnOAuthGetAccessTokenFailure(
1154 const GoogleServiceAuthError& error) {
1155 // TODO(zelidrag): Pop up sync setup UI here?
1156 LOG(WARNING) << "Failed fetching OAuth request token, error: "
1157 << error.state();
1158 }
1159
1066 void LoginUtilsImpl::FetchSecondaryTokens(Profile* offrecord_profile, 1160 void LoginUtilsImpl::FetchSecondaryTokens(Profile* offrecord_profile,
1067 const std::string& token, 1161 const std::string& token,
1068 const std::string& secret) { 1162 const std::string& secret) {
1069 FetchPolicyToken(offrecord_profile, token, secret); 1163 FetchPolicyToken(offrecord_profile, token, secret);
1070 // TODO(rickcam, zelidrag): Wire TokenService there when it becomes 1164 // TODO(rickcam, zelidrag): Wire TokenService there when it becomes
1071 // capable of handling OAuth1 tokens directly. 1165 // capable of handling OAuth1 tokens directly.
1072 } 1166 }
1073 1167
1074 bool LoginUtilsImpl::ReadOAuth1AccessToken(Profile* user_profile, 1168 bool LoginUtilsImpl::ReadOAuth1AccessToken(Profile* user_profile,
1075 std::string* token, 1169 std::string* token,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1130 token, 1224 token,
1131 secret, 1225 secret,
1132 username_)); 1226 username_));
1133 oauth_login_verifier_->Start(); 1227 oauth_login_verifier_->Start();
1134 } 1228 }
1135 1229
1136 1230
1137 void LoginUtilsImpl::FetchPolicyToken(Profile* offrecord_profile, 1231 void LoginUtilsImpl::FetchPolicyToken(Profile* offrecord_profile,
1138 const std::string& token, 1232 const std::string& token,
1139 const std::string& secret) { 1233 const std::string& secret) {
1140 // Trigger oauth token fetch for user policy. 1234 // Fetch dm service token now, if it hasn't been fetched yet.
1141 policy_oauth_fetcher_.reset(new PolicyOAuthFetcher(offrecord_profile, 1235 if (!policy_oauth_fetcher_.get() ||
1142 token, 1236 policy_oauth_fetcher_->policy_token().empty()) {
Nikita (slow) 2011/11/15 12:09:23 Could we distinguish these cases: 1. Policy token
Nikita (slow) 2011/11/15 12:16:20 should NOT restart it.
Joao da Silva 2011/11/15 14:35:56 Done. This method makes a new fetch if it hasn't b
1143 secret)); 1237 // Trigger oauth token fetch for user policy.
1144 policy_oauth_fetcher_->Start(); 1238 policy_oauth_fetcher_.reset(new PolicyOAuthFetcher(offrecord_profile,
1239 token,
1240 secret));
1241 policy_oauth_fetcher_->Start();
1242 }
1145 1243
1146 // TODO(zelidrag): We should add initialization of other services somewhere 1244 // TODO(zelidrag): We should add initialization of other services somewhere
1147 // here as well. This could be handled with TokenService class once it is 1245 // here as well. This could be handled with TokenService class once it is
1148 // ready to handle OAuth tokens. 1246 // ready to handle OAuth tokens.
1149 1247
1150 // We don't need authenticator instance any more, reset it so that 1248 // We don't need authenticator instance any more, reset it so that
1151 // ScreenLocker would create a separate instance. 1249 // ScreenLocker would create a separate instance.
1152 // TODO(nkostylev): There's a potential race if SL would be created before 1250 // TODO(nkostylev): There's a potential race if SL would be created before
1153 // OAuth tokens are fetched. It would use incorrect Authenticator instance. 1251 // OAuth tokens are fetched. It would use incorrect Authenticator instance.
1154 authenticator_ = NULL; 1252 authenticator_ = NULL;
1155 } 1253 }
1156 1254
1157 void LoginUtilsImpl::OnOAuthGetAccessTokenFailure(
1158 const GoogleServiceAuthError& error) {
1159 // TODO(zelidrag): Pop up sync setup UI here?
1160 LOG(WARNING) << "Failed fetching OAuth v1 token, error: " << error.state();
1161 }
1162
1163 void LoginUtilsImpl::OnOnlineStateChanged(bool online) { 1255 void LoginUtilsImpl::OnOnlineStateChanged(bool online) {
1164 // If we come online for the first time after successful offline login, 1256 // If we come online for the first time after successful offline login,
1165 // we need to kick of OAuth token verification process again. 1257 // we need to kick of OAuth token verification process again.
1166 if (UserManager::Get()->user_is_logged_in() && 1258 if (UserManager::Get()->user_is_logged_in() &&
1167 UserManager::Get()->offline_login() && online) { 1259 UserManager::Get()->offline_login() && online) {
1168 if (!authenticator_.get()) 1260 if (!authenticator_.get())
1169 CreateAuthenticator(NULL); 1261 CreateAuthenticator(NULL);
1170 std::string oauth1_token; 1262 std::string oauth1_token;
1171 std::string oauth1_secret; 1263 std::string oauth1_secret;
1172 Profile* user_profile = ProfileManager::GetDefaultProfile(); 1264 Profile* user_profile = ProfileManager::GetDefaultProfile();
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 // Mark login host for deletion after browser starts. This 1298 // Mark login host for deletion after browser starts. This
1207 // guarantees that the message loop will be referenced by the 1299 // guarantees that the message loop will be referenced by the
1208 // browser before it is dereferenced by the login host. 1300 // browser before it is dereferenced by the login host.
1209 if (login_host) { 1301 if (login_host) {
1210 login_host->OnSessionStart(); 1302 login_host->OnSessionStart();
1211 login_host = NULL; 1303 login_host = NULL;
1212 } 1304 }
1213 } 1305 }
1214 1306
1215 } // namespace chromeos 1307 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/login/login_browsertest.cc ('k') | chrome/browser/chromeos/login/login_utils_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698