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

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: Nits from Mattias 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
359 private:
360 virtual void OnGetOAuthTokenSuccess(const std::string& oauth_token) OVERRIDE {
361 VLOG(1) << "Got OAuth request token";
362 }
363
364 virtual void OnGetOAuthTokenFailure(
365 const GoogleServiceAuthError& error) OVERRIDE {
366 LOG(WARNING) << "Failed to get OAuth request token, error: "
367 << error.state();
368 SetPolicyToken("");
369 }
370
371 virtual void OnOAuthGetAccessTokenSuccess(
372 const std::string& token,
373 const std::string& secret) OVERRIDE {
374 VLOG(1) << "Got OAuth access token";
375 oauth1_token_ = token;
376 oauth1_secret_ = secret;
377 }
378
379 virtual void OnOAuthGetAccessTokenFailure(
380 const GoogleServiceAuthError& error) OVERRIDE {
381 LOG(WARNING) << "Failed to get OAuth access token, error: "
382 << error.state();
383 SetPolicyToken("");
384 }
385
337 virtual void OnOAuthWrapBridgeSuccess( 386 virtual void OnOAuthWrapBridgeSuccess(
338 const std::string& service_name, 387 const std::string& service_name,
339 const std::string& token, 388 const std::string& token,
340 const std::string& expires_in) OVERRIDE { 389 const std::string& expires_in) OVERRIDE {
341 policy::BrowserPolicyConnector* browser_policy_connector = 390 VLOG(1) << "Got OAuth access token for " << service_name;
342 g_browser_process->browser_policy_connector(); 391 SetPolicyToken(token);
343 browser_policy_connector->RegisterForUserPolicy(token);
344 } 392 }
345 393
346 virtual void OnOAuthWrapBridgeFailure( 394 virtual void OnOAuthWrapBridgeFailure(
347 const std::string& service_name, 395 const std::string& service_name,
348 const GoogleServiceAuthError& error) OVERRIDE { 396 const GoogleServiceAuthError& error) OVERRIDE {
349 LOG(WARNING) << "Failed to get OAuth access token for " << service_name; 397 LOG(WARNING) << "Failed to get OAuth access token for " << service_name
398 << ", error: " << error.state();
399 SetPolicyToken("");
350 } 400 }
351 401
352 private: 402 void SetPolicyToken(const std::string& token) {
403 g_browser_process->browser_policy_connector()->RegisterForUserPolicy(token);
404 }
405
353 GaiaOAuthFetcher oauth_fetcher_; 406 GaiaOAuthFetcher oauth_fetcher_;
354 std::string oauth1_token_; 407 std::string oauth1_token_;
355 std::string oauth1_secret_; 408 std::string oauth1_secret_;
356 409
357 DISALLOW_COPY_AND_ASSIGN(PolicyOAuthFetcher); 410 DISALLOW_COPY_AND_ASSIGN(PolicyOAuthFetcher);
358 }; 411 };
359 412
360 // Used to request a restart to switch to the guest mode. 413 // Used to request a restart to switch to the guest mode.
361 class JobRestartRequest 414 class JobRestartRequest
362 : public base::RefCountedThreadSafe<JobRestartRequest> { 415 : public base::RefCountedThreadSafe<JobRestartRequest> {
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 Profile* new_profile) OVERRIDE; 505 Profile* new_profile) OVERRIDE;
453 virtual void TransferDefaultAuthCache(Profile* default_profile, 506 virtual void TransferDefaultAuthCache(Profile* default_profile,
454 Profile* new_profile) OVERRIDE; 507 Profile* new_profile) OVERRIDE;
455 508
456 // ProfileManagerObserver implementation: 509 // ProfileManagerObserver implementation:
457 virtual void OnProfileCreated(Profile* profile, Status status) OVERRIDE; 510 virtual void OnProfileCreated(Profile* profile, Status status) OVERRIDE;
458 511
459 // GaiaOAuthConsumer overrides. 512 // GaiaOAuthConsumer overrides.
460 virtual void OnGetOAuthTokenSuccess(const std::string& oauth_token) OVERRIDE; 513 virtual void OnGetOAuthTokenSuccess(const std::string& oauth_token) OVERRIDE;
461 virtual void OnGetOAuthTokenFailure( 514 virtual void OnGetOAuthTokenFailure(
462 const GoogleServiceAuthError& error) OVERRIDE; 515 const GoogleServiceAuthError& error) OVERRIDE;
463 virtual void OnOAuthGetAccessTokenSuccess(const std::string& token, 516 virtual void OnOAuthGetAccessTokenSuccess(const std::string& token,
464 const std::string& secret) OVERRIDE; 517 const std::string& secret) OVERRIDE;
465 virtual void OnOAuthGetAccessTokenFailure( 518 virtual void OnOAuthGetAccessTokenFailure(
466 const GoogleServiceAuthError& error) OVERRIDE; 519 const GoogleServiceAuthError& error) OVERRIDE;
467 520
468 // net::NetworkChangeNotifier::OnlineStateObserver overrides. 521 // net::NetworkChangeNotifier::OnlineStateObserver overrides.
469 virtual void OnOnlineStateChanged(bool online) OVERRIDE; 522 virtual void OnOnlineStateChanged(bool online) OVERRIDE;
470 523
471 // Given the authenticated credentials from the cookie jar, try to exchange 524 // Given the authenticated credentials from the cookie jar, try to exchange
472 // fetch OAuth request, v1 and v2 tokens. 525 // fetch OAuth request, v1 and v2 tokens.
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 646
594 username_ = username; 647 username_ = username;
595 password_ = password; 648 password_ = password;
596 649
597 credentials_ = credentials; 650 credentials_ = credentials;
598 pending_requests_ = pending_requests; 651 pending_requests_ = pending_requests;
599 using_oauth_ = using_oauth; 652 using_oauth_ = using_oauth;
600 has_cookies_ = has_cookies; 653 has_cookies_ = has_cookies;
601 delegate_ = delegate; 654 delegate_ = delegate;
602 655
656 policy::BrowserPolicyConnector* connector =
657 g_browser_process->browser_policy_connector();
658
659 // If this is an enterprise device and the user belongs to the enterprise
Nikita (slow) 2011/11/14 16:21:28 This code path is used when we log in with the exi
Joao da Silva 2011/11/14 17:19:09 It will try to fetch the dmservice token again, bu
660 // domain, then wait for a policy fetch before logging the user in. This
661 // will delay Profile creation until the policy is fetched, so that features
662 // controlled by policy (e.g. Sync, Startup tabs) only start after the
663 // PrefService has the right values.
664 // Profile creation is also resumed if the fetch attempt fails.
665 bool wait_for_policy_fetch =
666 using_oauth_ &&
667 authenticator_.get() &&
668 (connector->GetUserAffiliation(username) ==
669 policy::CloudPolicyDataStore::USER_AFFILIATION_MANAGED);
670
603 // Initialize user policy before the profile is created so the profile 671 // Initialize user policy before the profile is created so the profile
604 // initialization code sees the policy settings. 672 // initialization code sees the cached policy settings.
605 g_browser_process->browser_policy_connector()->InitializeUserPolicy(username); 673 connector->InitializeUserPolicy(username, wait_for_policy_fetch);
674
675 if (wait_for_policy_fetch) {
676 // Profile creation will block until user policy is fetched, which
677 // requires the DeviceManagement token. Try to fetch it now.
678 VLOG(1) << "Profile creation requires policy token, fetching now";
679 policy_oauth_fetcher_.reset(
680 new PolicyOAuthFetcher(authenticator_->authentication_profile()));
681 policy_oauth_fetcher_->Start();
Nikita (slow) 2011/11/14 16:21:28 PolicyOAuthFetcher::Start() will fetch in async mo
Joao da Silva 2011/11/14 17:19:09 Profile creation blocks on its PrefService becomin
682 }
606 683
607 // The default profile will have been changed because the ProfileManager 684 // The default profile will have been changed because the ProfileManager
608 // will process the notification that the UserManager sends out. 685 // will process the notification that the UserManager sends out.
609 ProfileManager::CreateDefaultProfileAsync(this); 686 ProfileManager::CreateDefaultProfileAsync(this);
610 } 687 }
611 688
612 void LoginUtilsImpl::DelegateDeleted(Delegate* delegate) { 689 void LoginUtilsImpl::DelegateDeleted(Delegate* delegate) {
613 if (delegate_ == delegate) 690 if (delegate_ == delegate)
614 delegate_ = NULL; 691 delegate_ = NULL;
615 } 692 }
616 693
617 void LoginUtilsImpl::OnProfileCreated(Profile* user_profile, Status status) { 694 void LoginUtilsImpl::OnProfileCreated(Profile* user_profile, Status status) {
618 CHECK(user_profile); 695 CHECK(user_profile);
619 switch (status) { 696 switch (status) {
620 case STATUS_INITIALIZED: 697 case STATUS_INITIALIZED:
621 break; 698 break;
622 case STATUS_CREATED: 699 case STATUS_CREATED:
623 if (UserManager::Get()->current_user_is_new()) 700 if (UserManager::Get()->current_user_is_new())
624 SetFirstLoginPrefs(user_profile->GetPrefs()); 701 SetFirstLoginPrefs(user_profile->GetPrefs());
625 RespectLocalePreference(user_profile); 702 RespectLocalePreference(user_profile);
626 return; 703 return;
627 case STATUS_FAIL: 704 case STATUS_FAIL:
628 default: 705 default:
629 NOTREACHED(); 706 NOTREACHED();
630 return; 707 return;
631 } 708 }
632 709
633 // Initialize the user-policy backend. 710 // Initialize the user-policy backend.
634 policy::BrowserPolicyConnector* browser_policy_connector =
635 g_browser_process->browser_policy_connector();
636
637 if (!using_oauth_) { 711 if (!using_oauth_) {
638 browser_policy_connector->SetUserPolicyTokenService( 712 g_browser_process->browser_policy_connector()->
639 user_profile->GetTokenService()); 713 SetUserPolicyTokenService(user_profile->GetTokenService());
640 } 714 }
641 715
642 // We suck. This is a hack since we do not have the enterprise feature 716 // 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 717 // done yet to pull down policies from the domain admin. We'll take this
644 // out when we get that done properly. 718 // out when we get that done properly.
645 // TODO(xiyuan): Remove this once enterprise feature is ready. 719 // TODO(xiyuan): Remove this once enterprise feature is ready.
646 if (EndsWith(username_, "@google.com", true)) { 720 if (EndsWith(username_, "@google.com", true)) {
647 PrefService* pref_service = user_profile->GetPrefs(); 721 PrefService* pref_service = user_profile->GetPrefs();
648 pref_service->SetBoolean(prefs::kEnableScreenLock, true); 722 pref_service->SetBoolean(prefs::kEnableScreenLock, true);
649 } 723 }
650 724
651 BootTimesLoader* btl = BootTimesLoader::Get(); 725 BootTimesLoader* btl = BootTimesLoader::Get();
652 btl->AddLoginTimeMarker("UserProfileGotten", false); 726 btl->AddLoginTimeMarker("UserProfileGotten", false);
653 727
654 if (using_oauth_) { 728 if (using_oauth_) {
729 // Reuse the access token fetched by the PolicyOAuthFetcher, if it was
730 // used to fetch policies before Profile creation.
731 if (policy_oauth_fetcher_.get() &&
732 !policy_oauth_fetcher_->oauth1_token().empty()) {
733 VLOG(1) << "Resuming profile creation after fetching policy token";
734 StoreOAuth1AccessToken(user_profile,
735 policy_oauth_fetcher_->oauth1_token(),
736 policy_oauth_fetcher_->oauth1_secret());
737 }
738
655 // Transfer cookies when user signs in using extension. 739 // Transfer cookies when user signs in using extension.
656 if (has_cookies_) { 740 if (has_cookies_) {
657 // Transfer cookies from the profile that was used for authentication. 741 // Transfer cookies from the profile that was used for authentication.
658 // This profile contains cookies that auth extension should have already 742 // This profile contains cookies that auth extension should have already
659 // put in place that will ensure that the newly created session is 743 // put in place that will ensure that the newly created session is
660 // authenticated for the websites that work with the used authentication 744 // authenticated for the websites that work with the used authentication
661 // schema. 745 // schema.
662 TransferDefaultCookies(authenticator_->authentication_profile(), 746 TransferDefaultCookies(authenticator_->authentication_profile(),
663 user_profile); 747 user_profile);
664 } 748 }
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 // TODO(altimofeev): Need to sanitize memory used to store password. 822 // TODO(altimofeev): Need to sanitize memory used to store password.
739 credentials_ = GaiaAuthConsumer::ClientLoginResult(); 823 credentials_ = GaiaAuthConsumer::ClientLoginResult();
740 } 824 }
741 825
742 void LoginUtilsImpl::FetchOAuth1AccessToken(Profile* auth_profile) { 826 void LoginUtilsImpl::FetchOAuth1AccessToken(Profile* auth_profile) {
743 oauth_fetcher_.reset(new GaiaOAuthFetcher(this, 827 oauth_fetcher_.reset(new GaiaOAuthFetcher(this,
744 auth_profile->GetRequestContext(), 828 auth_profile->GetRequestContext(),
745 auth_profile, 829 auth_profile,
746 kServiceScopeChromeOS)); 830 kServiceScopeChromeOS));
747 // Let's first get the Oauth request token and OAuth1 token+secret. 831 // 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 832 // Once we get that, we will kick off individual requests for OAuth2 tokens
749 // all our services. 833 // for all our services.
750 oauth_fetcher_->SetAutoFetchLimit(GaiaOAuthFetcher::OAUTH1_ALL_ACCESS_TOKEN); 834 oauth_fetcher_->SetAutoFetchLimit(GaiaOAuthFetcher::OAUTH1_ALL_ACCESS_TOKEN);
751 oauth_fetcher_->StartGetOAuthTokenRequest(); 835 oauth_fetcher_->StartGetOAuthTokenRequest();
752 } 836 }
753 837
754 void LoginUtilsImpl::FetchCookies(Profile* user_profile, 838 void LoginUtilsImpl::FetchCookies(Profile* user_profile,
755 const GaiaAuthConsumer::ClientLoginResult& credentials) { 839 const GaiaAuthConsumer::ClientLoginResult& credentials) {
756 if (!using_oauth_) { 840 if (!using_oauth_) {
757 // Take the credentials passed in and try to exchange them for 841 // Take the credentials passed in and try to exchange them for
758 // full-fledged Google authentication cookies. This is 842 // full-fledged Google authentication cookies. This is
759 // best-effort; it's possible that we'll fail due to network 843 // 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); 876 password_, false);
793 username_ = ""; 877 username_ = "";
794 password_ = ""; 878 password_ = "";
795 879
796 token_service->Initialize(GaiaConstants::kChromeOSSource, user_profile); 880 token_service->Initialize(GaiaConstants::kChromeOSSource, user_profile);
797 token_service->LoadTokensFromDB(); 881 token_service->LoadTokensFromDB();
798 } 882 }
799 token_service->UpdateCredentials(credentials); 883 token_service->UpdateCredentials(credentials);
800 if (token_service->AreCredentialsValid()) 884 if (token_service->AreCredentialsValid())
801 token_service->StartFetchingTokens(); 885 token_service->StartFetchingTokens();
802
803 } 886 }
804 887
805 void LoginUtilsImpl::RespectLocalePreference(Profile* profile) { 888 void LoginUtilsImpl::RespectLocalePreference(Profile* profile) {
806 DCHECK(profile != NULL); 889 DCHECK(profile != NULL);
807 PrefService* prefs = profile->GetPrefs(); 890 PrefService* prefs = profile->GetPrefs();
808 DCHECK(prefs != NULL); 891 DCHECK(prefs != NULL);
809 if (g_browser_process == NULL) 892 if (g_browser_process == NULL)
810 return; 893 return;
811 894
812 std::string pref_locale = prefs->GetString(prefs::kApplicationLocale); 895 std::string pref_locale = prefs->GetString(prefs::kApplicationLocale);
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 profile->GetRequestContext())); 1126 profile->GetRequestContext()));
1044 } 1127 }
1045 1128
1046 void LoginUtilsImpl::OnGetOAuthTokenSuccess(const std::string& oauth_token) { 1129 void LoginUtilsImpl::OnGetOAuthTokenSuccess(const std::string& oauth_token) {
1047 VLOG(1) << "Got OAuth request token!"; 1130 VLOG(1) << "Got OAuth request token!";
1048 } 1131 }
1049 1132
1050 void LoginUtilsImpl::OnGetOAuthTokenFailure( 1133 void LoginUtilsImpl::OnGetOAuthTokenFailure(
1051 const GoogleServiceAuthError& error) { 1134 const GoogleServiceAuthError& error) {
1052 // TODO(zelidrag): Pop up sync setup UI here? 1135 // TODO(zelidrag): Pop up sync setup UI here?
1053 LOG(WARNING) << "Failed fetching OAuth request token"; 1136 LOG(WARNING) << "Failed fetching OAuth request token, error: "
1137 << error.state();
1054 } 1138 }
1055 1139
1056 void LoginUtilsImpl::OnOAuthGetAccessTokenSuccess(const std::string& token, 1140 void LoginUtilsImpl::OnOAuthGetAccessTokenSuccess(const std::string& token,
1057 const std::string& secret) { 1141 const std::string& secret) {
1058 VLOG(1) << "Got OAuth v1 token!"; 1142 VLOG(1) << "Got OAuth v1 token!";
1059 Profile* user_profile = ProfileManager::GetDefaultProfile(); 1143 Profile* user_profile = ProfileManager::GetDefaultProfile();
1060 StoreOAuth1AccessToken(user_profile, token, secret); 1144 StoreOAuth1AccessToken(user_profile, token, secret);
1061 1145
1062 // Verify OAuth1 token by doing OAuthLogin and fetching credentials. 1146 // Verify OAuth1 token by doing OAuthLogin and fetching credentials.
1063 VerifyOAuth1AccessToken(user_profile, token, secret); 1147 VerifyOAuth1AccessToken(user_profile, token, secret);
1064 } 1148 }
1065 1149
1150 void LoginUtilsImpl::OnOAuthGetAccessTokenFailure(
1151 const GoogleServiceAuthError& error) {
1152 // TODO(zelidrag): Pop up sync setup UI here?
1153 LOG(WARNING) << "Failed fetching OAuth request token, error: "
1154 << error.state();
1155 }
1156
1066 void LoginUtilsImpl::FetchSecondaryTokens(Profile* offrecord_profile, 1157 void LoginUtilsImpl::FetchSecondaryTokens(Profile* offrecord_profile,
1067 const std::string& token, 1158 const std::string& token,
1068 const std::string& secret) { 1159 const std::string& secret) {
1069 FetchPolicyToken(offrecord_profile, token, secret); 1160 FetchPolicyToken(offrecord_profile, token, secret);
1070 // TODO(rickcam, zelidrag): Wire TokenService there when it becomes 1161 // TODO(rickcam, zelidrag): Wire TokenService there when it becomes
1071 // capable of handling OAuth1 tokens directly. 1162 // capable of handling OAuth1 tokens directly.
1072 } 1163 }
1073 1164
1074 bool LoginUtilsImpl::ReadOAuth1AccessToken(Profile* user_profile, 1165 bool LoginUtilsImpl::ReadOAuth1AccessToken(Profile* user_profile,
1075 std::string* token, 1166 std::string* token,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1130 token, 1221 token,
1131 secret, 1222 secret,
1132 username_)); 1223 username_));
1133 oauth_login_verifier_->Start(); 1224 oauth_login_verifier_->Start();
1134 } 1225 }
1135 1226
1136 1227
1137 void LoginUtilsImpl::FetchPolicyToken(Profile* offrecord_profile, 1228 void LoginUtilsImpl::FetchPolicyToken(Profile* offrecord_profile,
1138 const std::string& token, 1229 const std::string& token,
1139 const std::string& secret) { 1230 const std::string& secret) {
1140 // Trigger oauth token fetch for user policy. 1231 // Fetch dm service token now, if it hasn't been fetched yet.
1141 policy_oauth_fetcher_.reset(new PolicyOAuthFetcher(offrecord_profile, 1232 if (!policy_oauth_fetcher_.get()) {
1142 token, 1233 // Trigger oauth token fetch for user policy.
1143 secret)); 1234 policy_oauth_fetcher_.reset(new PolicyOAuthFetcher(offrecord_profile,
1144 policy_oauth_fetcher_->Start(); 1235 token,
1236 secret));
1237 policy_oauth_fetcher_->Start();
1238 }
1145 1239
1146 // TODO(zelidrag): We should add initialization of other services somewhere 1240 // TODO(zelidrag): We should add initialization of other services somewhere
1147 // here as well. This could be handled with TokenService class once it is 1241 // here as well. This could be handled with TokenService class once it is
1148 // ready to handle OAuth tokens. 1242 // ready to handle OAuth tokens.
1149 1243
1150 // We don't need authenticator instance any more, reset it so that 1244 // We don't need authenticator instance any more, reset it so that
1151 // ScreenLocker would create a separate instance. 1245 // ScreenLocker would create a separate instance.
1152 // TODO(nkostylev): There's a potential race if SL would be created before 1246 // TODO(nkostylev): There's a potential race if SL would be created before
1153 // OAuth tokens are fetched. It would use incorrect Authenticator instance. 1247 // OAuth tokens are fetched. It would use incorrect Authenticator instance.
1154 authenticator_ = NULL; 1248 authenticator_ = NULL;
1155 } 1249 }
1156 1250
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) { 1251 void LoginUtilsImpl::OnOnlineStateChanged(bool online) {
1164 // If we come online for the first time after successful offline login, 1252 // If we come online for the first time after successful offline login,
Nikita (slow) 2011/11/14 16:21:28 Should you refetch policy tokens here too? Applies
Joao da Silva 2011/11/14 17:19:09 Probably a good idea. The token only has to be fet
Nikita (slow) 2011/11/15 12:09:23 Ok, it will also be called on crash. http://codere
1165 // we need to kick of OAuth token verification process again. 1253 // we need to kick of OAuth token verification process again.
1166 if (UserManager::Get()->user_is_logged_in() && 1254 if (UserManager::Get()->user_is_logged_in() &&
1167 UserManager::Get()->offline_login() && online) { 1255 UserManager::Get()->offline_login() && online) {
1168 if (!authenticator_.get()) 1256 if (!authenticator_.get())
1169 CreateAuthenticator(NULL); 1257 CreateAuthenticator(NULL);
1170 std::string oauth1_token; 1258 std::string oauth1_token;
1171 std::string oauth1_secret; 1259 std::string oauth1_secret;
1172 Profile* user_profile = ProfileManager::GetDefaultProfile(); 1260 Profile* user_profile = ProfileManager::GetDefaultProfile();
1173 if (ReadOAuth1AccessToken(user_profile, &oauth1_token, &oauth1_secret)) 1261 if (ReadOAuth1AccessToken(user_profile, &oauth1_token, &oauth1_secret))
1174 VerifyOAuth1AccessToken(user_profile, oauth1_token, oauth1_secret); 1262 VerifyOAuth1AccessToken(user_profile, oauth1_token, oauth1_secret);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 // Mark login host for deletion after browser starts. This 1294 // Mark login host for deletion after browser starts. This
1207 // guarantees that the message loop will be referenced by the 1295 // guarantees that the message loop will be referenced by the
1208 // browser before it is dereferenced by the login host. 1296 // browser before it is dereferenced by the login host.
1209 if (login_host) { 1297 if (login_host) {
1210 login_host->OnSessionStart(); 1298 login_host->OnSessionStart();
1211 login_host = NULL; 1299 login_host = NULL;
1212 } 1300 }
1213 } 1301 }
1214 1302
1215 } // namespace chromeos 1303 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/login/login_browsertest.cc ('k') | chrome/browser/chromeos/login/login_utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698