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

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: Rebased 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 bool failed() const {
359 return !oauth_fetcher_.HasPendingFetch() && policy_token_.empty();
360 }
361
362 private:
363 virtual void OnGetOAuthTokenSuccess(const std::string& oauth_token) OVERRIDE {
364 VLOG(1) << "Got OAuth request token";
365 }
366
367 virtual void OnGetOAuthTokenFailure(
368 const GoogleServiceAuthError& error) OVERRIDE {
369 LOG(WARNING) << "Failed to get OAuth request token, error: "
370 << error.state();
371 SetPolicyToken("");
372 }
373
374 virtual void OnOAuthGetAccessTokenSuccess(
375 const std::string& token,
376 const std::string& secret) OVERRIDE {
377 VLOG(1) << "Got OAuth access token";
378 oauth1_token_ = token;
379 oauth1_secret_ = secret;
380 }
381
382 virtual void OnOAuthGetAccessTokenFailure(
383 const GoogleServiceAuthError& error) OVERRIDE {
384 LOG(WARNING) << "Failed to get OAuth access token, error: "
385 << error.state();
386 SetPolicyToken("");
387 }
388
337 virtual void OnOAuthWrapBridgeSuccess( 389 virtual void OnOAuthWrapBridgeSuccess(
338 const std::string& service_name, 390 const std::string& service_name,
339 const std::string& token, 391 const std::string& token,
340 const std::string& expires_in) OVERRIDE { 392 const std::string& expires_in) OVERRIDE {
341 policy::BrowserPolicyConnector* browser_policy_connector = 393 VLOG(1) << "Got OAuth access token for " << service_name;
342 g_browser_process->browser_policy_connector(); 394 SetPolicyToken(token);
343 browser_policy_connector->RegisterForUserPolicy(token);
344 } 395 }
345 396
346 virtual void OnOAuthWrapBridgeFailure( 397 virtual void OnOAuthWrapBridgeFailure(
347 const std::string& service_name, 398 const std::string& service_name,
348 const GoogleServiceAuthError& error) OVERRIDE { 399 const GoogleServiceAuthError& error) OVERRIDE {
349 LOG(WARNING) << "Failed to get OAuth access token for " << service_name; 400 LOG(WARNING) << "Failed to get OAuth access token for " << service_name
401 << ", error: " << error.state();
402 SetPolicyToken("");
350 } 403 }
351 404
352 private: 405 void SetPolicyToken(const std::string& token) {
406 policy_token_ = token;
407 g_browser_process->browser_policy_connector()->RegisterForUserPolicy(token);
408 }
409
353 GaiaOAuthFetcher oauth_fetcher_; 410 GaiaOAuthFetcher oauth_fetcher_;
354 std::string oauth1_token_; 411 std::string oauth1_token_;
355 std::string oauth1_secret_; 412 std::string oauth1_secret_;
413 std::string policy_token_;
356 414
357 DISALLOW_COPY_AND_ASSIGN(PolicyOAuthFetcher); 415 DISALLOW_COPY_AND_ASSIGN(PolicyOAuthFetcher);
358 }; 416 };
359 417
360 // Used to request a restart to switch to the guest mode. 418 // Used to request a restart to switch to the guest mode.
361 class JobRestartRequest 419 class JobRestartRequest
362 : public base::RefCountedThreadSafe<JobRestartRequest> { 420 : public base::RefCountedThreadSafe<JobRestartRequest> {
363 public: 421 public:
364 JobRestartRequest(int pid, const std::string& command_line) 422 JobRestartRequest(int pid, const std::string& command_line)
365 : pid_(pid), 423 : pid_(pid),
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 Profile* new_profile) OVERRIDE; 512 Profile* new_profile) OVERRIDE;
455 virtual void TransferDefaultAuthCache(Profile* default_profile, 513 virtual void TransferDefaultAuthCache(Profile* default_profile,
456 Profile* new_profile) OVERRIDE; 514 Profile* new_profile) OVERRIDE;
457 515
458 // ProfileManagerObserver implementation: 516 // ProfileManagerObserver implementation:
459 virtual void OnProfileCreated(Profile* profile, Status status) OVERRIDE; 517 virtual void OnProfileCreated(Profile* profile, Status status) OVERRIDE;
460 518
461 // GaiaOAuthConsumer overrides. 519 // GaiaOAuthConsumer overrides.
462 virtual void OnGetOAuthTokenSuccess(const std::string& oauth_token) OVERRIDE; 520 virtual void OnGetOAuthTokenSuccess(const std::string& oauth_token) OVERRIDE;
463 virtual void OnGetOAuthTokenFailure( 521 virtual void OnGetOAuthTokenFailure(
464 const GoogleServiceAuthError& error) OVERRIDE; 522 const GoogleServiceAuthError& error) OVERRIDE;
465 virtual void OnOAuthGetAccessTokenSuccess(const std::string& token, 523 virtual void OnOAuthGetAccessTokenSuccess(const std::string& token,
466 const std::string& secret) OVERRIDE; 524 const std::string& secret) OVERRIDE;
467 virtual void OnOAuthGetAccessTokenFailure( 525 virtual void OnOAuthGetAccessTokenFailure(
468 const GoogleServiceAuthError& error) OVERRIDE; 526 const GoogleServiceAuthError& error) OVERRIDE;
469 527
470 // net::NetworkChangeNotifier::OnlineStateObserver overrides. 528 // net::NetworkChangeNotifier::OnlineStateObserver overrides.
471 virtual void OnOnlineStateChanged(bool online) OVERRIDE; 529 virtual void OnOnlineStateChanged(bool online) OVERRIDE;
472 530
473 // Given the authenticated credentials from the cookie jar, try to exchange 531 // Given the authenticated credentials from the cookie jar, try to exchange
474 // fetch OAuth request, v1 and v2 tokens. 532 // fetch OAuth request, v1 and v2 tokens.
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 656
599 username_ = username; 657 username_ = username;
600 password_ = password; 658 password_ = password;
601 659
602 credentials_ = credentials; 660 credentials_ = credentials;
603 pending_requests_ = pending_requests; 661 pending_requests_ = pending_requests;
604 using_oauth_ = using_oauth; 662 using_oauth_ = using_oauth;
605 has_cookies_ = has_cookies; 663 has_cookies_ = has_cookies;
606 delegate_ = delegate; 664 delegate_ = delegate;
607 665
666 policy::BrowserPolicyConnector* connector =
667 g_browser_process->browser_policy_connector();
668
669 // If this is an enterprise device and the user belongs to the enterprise
670 // domain, then wait for a policy fetch before logging the user in. This
671 // will delay Profile creation until the policy is fetched, so that features
672 // controlled by policy (e.g. Sync, Startup tabs) only start after the
673 // PrefService has the right values.
674 // Profile creation is also resumed if the fetch attempt fails.
675 bool wait_for_policy_fetch =
676 using_oauth_ &&
677 authenticator_.get() &&
678 (connector->GetUserAffiliation(username) ==
679 policy::CloudPolicyDataStore::USER_AFFILIATION_MANAGED);
680
608 // Initialize user policy before the profile is created so the profile 681 // Initialize user policy before the profile is created so the profile
609 // initialization code sees the policy settings. 682 // initialization code sees the cached policy settings.
610 g_browser_process->browser_policy_connector()->InitializeUserPolicy(username); 683 connector->InitializeUserPolicy(username, wait_for_policy_fetch);
684
685 if (wait_for_policy_fetch) {
686 // Profile creation will block until user policy is fetched, which
687 // requires the DeviceManagement token. Try to fetch it now.
688 VLOG(1) << "Profile creation requires policy token, fetching now";
689 policy_oauth_fetcher_.reset(
690 new PolicyOAuthFetcher(authenticator_->authentication_profile()));
691 policy_oauth_fetcher_->Start();
692 }
611 693
612 // The default profile will have been changed because the ProfileManager 694 // The default profile will have been changed because the ProfileManager
613 // will process the notification that the UserManager sends out. 695 // will process the notification that the UserManager sends out.
614 ProfileManager::CreateDefaultProfileAsync(this); 696 ProfileManager::CreateDefaultProfileAsync(this);
615 } 697 }
616 698
617 void LoginUtilsImpl::DelegateDeleted(Delegate* delegate) { 699 void LoginUtilsImpl::DelegateDeleted(Delegate* delegate) {
618 if (delegate_ == delegate) 700 if (delegate_ == delegate)
619 delegate_ = NULL; 701 delegate_ = NULL;
620 } 702 }
621 703
622 void LoginUtilsImpl::OnProfileCreated(Profile* user_profile, Status status) { 704 void LoginUtilsImpl::OnProfileCreated(Profile* user_profile, Status status) {
623 CHECK(user_profile); 705 CHECK(user_profile);
624 switch (status) { 706 switch (status) {
625 case STATUS_INITIALIZED: 707 case STATUS_INITIALIZED:
626 break; 708 break;
627 case STATUS_CREATED: 709 case STATUS_CREATED:
628 if (UserManager::Get()->current_user_is_new()) 710 if (UserManager::Get()->current_user_is_new())
629 SetFirstLoginPrefs(user_profile->GetPrefs()); 711 SetFirstLoginPrefs(user_profile->GetPrefs());
630 RespectLocalePreference(user_profile); 712 RespectLocalePreference(user_profile);
631 return; 713 return;
632 case STATUS_FAIL: 714 case STATUS_FAIL:
633 default: 715 default:
634 NOTREACHED(); 716 NOTREACHED();
635 return; 717 return;
636 } 718 }
637 719
638 // Initialize the user-policy backend. 720 // Initialize the user-policy backend.
639 policy::BrowserPolicyConnector* browser_policy_connector =
640 g_browser_process->browser_policy_connector();
641
642 if (!using_oauth_) { 721 if (!using_oauth_) {
643 browser_policy_connector->SetUserPolicyTokenService( 722 g_browser_process->browser_policy_connector()->
644 user_profile->GetTokenService()); 723 SetUserPolicyTokenService(user_profile->GetTokenService());
645 } 724 }
646 725
647 // We suck. This is a hack since we do not have the enterprise feature 726 // We suck. This is a hack since we do not have the enterprise feature
648 // done yet to pull down policies from the domain admin. We'll take this 727 // done yet to pull down policies from the domain admin. We'll take this
649 // out when we get that done properly. 728 // out when we get that done properly.
650 // TODO(xiyuan): Remove this once enterprise feature is ready. 729 // TODO(xiyuan): Remove this once enterprise feature is ready.
651 if (EndsWith(username_, "@google.com", true)) { 730 if (EndsWith(username_, "@google.com", true)) {
652 PrefService* pref_service = user_profile->GetPrefs(); 731 PrefService* pref_service = user_profile->GetPrefs();
653 pref_service->SetBoolean(prefs::kEnableScreenLock, true); 732 pref_service->SetBoolean(prefs::kEnableScreenLock, true);
654 } 733 }
655 734
656 BootTimesLoader* btl = BootTimesLoader::Get(); 735 BootTimesLoader* btl = BootTimesLoader::Get();
657 btl->AddLoginTimeMarker("UserProfileGotten", false); 736 btl->AddLoginTimeMarker("UserProfileGotten", false);
658 737
659 if (using_oauth_) { 738 if (using_oauth_) {
739 // Reuse the access token fetched by the PolicyOAuthFetcher, if it was
740 // used to fetch policies before Profile creation.
741 if (policy_oauth_fetcher_.get() &&
742 !policy_oauth_fetcher_->oauth1_token().empty()) {
743 VLOG(1) << "Resuming profile creation after fetching policy token";
744 StoreOAuth1AccessToken(user_profile,
745 policy_oauth_fetcher_->oauth1_token(),
746 policy_oauth_fetcher_->oauth1_secret());
747 }
748
660 // Transfer cookies when user signs in using extension. 749 // Transfer cookies when user signs in using extension.
661 if (has_cookies_) { 750 if (has_cookies_) {
662 // Transfer cookies from the profile that was used for authentication. 751 // Transfer cookies from the profile that was used for authentication.
663 // This profile contains cookies that auth extension should have already 752 // This profile contains cookies that auth extension should have already
664 // put in place that will ensure that the newly created session is 753 // put in place that will ensure that the newly created session is
665 // authenticated for the websites that work with the used authentication 754 // authenticated for the websites that work with the used authentication
666 // schema. 755 // schema.
667 TransferDefaultCookies(authenticator_->authentication_profile(), 756 TransferDefaultCookies(authenticator_->authentication_profile(),
668 user_profile); 757 user_profile);
669 } 758 }
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
743 // TODO(altimofeev): Need to sanitize memory used to store password. 832 // TODO(altimofeev): Need to sanitize memory used to store password.
744 credentials_ = GaiaAuthConsumer::ClientLoginResult(); 833 credentials_ = GaiaAuthConsumer::ClientLoginResult();
745 } 834 }
746 835
747 void LoginUtilsImpl::FetchOAuth1AccessToken(Profile* auth_profile) { 836 void LoginUtilsImpl::FetchOAuth1AccessToken(Profile* auth_profile) {
748 oauth_fetcher_.reset(new GaiaOAuthFetcher(this, 837 oauth_fetcher_.reset(new GaiaOAuthFetcher(this,
749 auth_profile->GetRequestContext(), 838 auth_profile->GetRequestContext(),
750 auth_profile, 839 auth_profile,
751 kServiceScopeChromeOS)); 840 kServiceScopeChromeOS));
752 // Let's first get the Oauth request token and OAuth1 token+secret. 841 // Let's first get the Oauth request token and OAuth1 token+secret.
753 // One we get that, we will kick off individial requests for OAuth2 tokens for 842 // Once we get that, we will kick off individual requests for OAuth2 tokens
754 // all our services. 843 // for all our services.
755 oauth_fetcher_->SetAutoFetchLimit(GaiaOAuthFetcher::OAUTH1_ALL_ACCESS_TOKEN); 844 oauth_fetcher_->SetAutoFetchLimit(GaiaOAuthFetcher::OAUTH1_ALL_ACCESS_TOKEN);
756 oauth_fetcher_->StartGetOAuthTokenRequest(); 845 oauth_fetcher_->StartGetOAuthTokenRequest();
757 } 846 }
758 847
759 void LoginUtilsImpl::FetchCookies(Profile* user_profile, 848 void LoginUtilsImpl::FetchCookies(Profile* user_profile,
760 const GaiaAuthConsumer::ClientLoginResult& credentials) { 849 const GaiaAuthConsumer::ClientLoginResult& credentials) {
761 if (!using_oauth_) { 850 if (!using_oauth_) {
762 // Take the credentials passed in and try to exchange them for 851 // Take the credentials passed in and try to exchange them for
763 // full-fledged Google authentication cookies. This is 852 // full-fledged Google authentication cookies. This is
764 // best-effort; it's possible that we'll fail due to network 853 // best-effort; it's possible that we'll fail due to network
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
797 password_, false); 886 password_, false);
798 username_ = ""; 887 username_ = "";
799 password_ = ""; 888 password_ = "";
800 889
801 token_service->Initialize(GaiaConstants::kChromeOSSource, user_profile); 890 token_service->Initialize(GaiaConstants::kChromeOSSource, user_profile);
802 token_service->LoadTokensFromDB(); 891 token_service->LoadTokensFromDB();
803 } 892 }
804 token_service->UpdateCredentials(credentials); 893 token_service->UpdateCredentials(credentials);
805 if (token_service->AreCredentialsValid()) 894 if (token_service->AreCredentialsValid())
806 token_service->StartFetchingTokens(); 895 token_service->StartFetchingTokens();
807
808 } 896 }
809 897
810 void LoginUtilsImpl::RespectLocalePreference(Profile* profile) { 898 void LoginUtilsImpl::RespectLocalePreference(Profile* profile) {
811 DCHECK(profile != NULL); 899 DCHECK(profile != NULL);
812 PrefService* prefs = profile->GetPrefs(); 900 PrefService* prefs = profile->GetPrefs();
813 DCHECK(prefs != NULL); 901 DCHECK(prefs != NULL);
814 if (g_browser_process == NULL) 902 if (g_browser_process == NULL)
815 return; 903 return;
816 904
817 std::string pref_locale = prefs->GetString(prefs::kApplicationLocale); 905 std::string pref_locale = prefs->GetString(prefs::kApplicationLocale);
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
1065 profile->GetRequestContext())); 1153 profile->GetRequestContext()));
1066 } 1154 }
1067 1155
1068 void LoginUtilsImpl::OnGetOAuthTokenSuccess(const std::string& oauth_token) { 1156 void LoginUtilsImpl::OnGetOAuthTokenSuccess(const std::string& oauth_token) {
1069 VLOG(1) << "Got OAuth request token!"; 1157 VLOG(1) << "Got OAuth request token!";
1070 } 1158 }
1071 1159
1072 void LoginUtilsImpl::OnGetOAuthTokenFailure( 1160 void LoginUtilsImpl::OnGetOAuthTokenFailure(
1073 const GoogleServiceAuthError& error) { 1161 const GoogleServiceAuthError& error) {
1074 // TODO(zelidrag): Pop up sync setup UI here? 1162 // TODO(zelidrag): Pop up sync setup UI here?
1075 LOG(WARNING) << "Failed fetching OAuth request token"; 1163 LOG(WARNING) << "Failed fetching OAuth request token, error: "
1164 << error.state();
1076 } 1165 }
1077 1166
1078 void LoginUtilsImpl::OnOAuthGetAccessTokenSuccess(const std::string& token, 1167 void LoginUtilsImpl::OnOAuthGetAccessTokenSuccess(const std::string& token,
1079 const std::string& secret) { 1168 const std::string& secret) {
1080 VLOG(1) << "Got OAuth v1 token!"; 1169 VLOG(1) << "Got OAuth v1 token!";
1081 Profile* user_profile = ProfileManager::GetDefaultProfile(); 1170 Profile* user_profile = ProfileManager::GetDefaultProfile();
1082 StoreOAuth1AccessToken(user_profile, token, secret); 1171 StoreOAuth1AccessToken(user_profile, token, secret);
1083 1172
1084 // Verify OAuth1 token by doing OAuthLogin and fetching credentials. 1173 // Verify OAuth1 token by doing OAuthLogin and fetching credentials.
1085 VerifyOAuth1AccessToken(user_profile, token, secret); 1174 VerifyOAuth1AccessToken(user_profile, token, secret);
1086 } 1175 }
1087 1176
1177 void LoginUtilsImpl::OnOAuthGetAccessTokenFailure(
1178 const GoogleServiceAuthError& error) {
1179 // TODO(zelidrag): Pop up sync setup UI here?
1180 LOG(WARNING) << "Failed fetching OAuth request token, error: "
1181 << error.state();
1182 }
1183
1088 void LoginUtilsImpl::FetchSecondaryTokens(Profile* offrecord_profile, 1184 void LoginUtilsImpl::FetchSecondaryTokens(Profile* offrecord_profile,
1089 const std::string& token, 1185 const std::string& token,
1090 const std::string& secret) { 1186 const std::string& secret) {
1091 FetchPolicyToken(offrecord_profile, token, secret); 1187 FetchPolicyToken(offrecord_profile, token, secret);
1092 // TODO(rickcam, zelidrag): Wire TokenService there when it becomes 1188 // TODO(rickcam, zelidrag): Wire TokenService there when it becomes
1093 // capable of handling OAuth1 tokens directly. 1189 // capable of handling OAuth1 tokens directly.
1094 } 1190 }
1095 1191
1096 bool LoginUtilsImpl::ReadOAuth1AccessToken(Profile* user_profile, 1192 bool LoginUtilsImpl::ReadOAuth1AccessToken(Profile* user_profile,
1097 std::string* token, 1193 std::string* token,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1152 token, 1248 token,
1153 secret, 1249 secret,
1154 username_)); 1250 username_));
1155 oauth_login_verifier_->Start(); 1251 oauth_login_verifier_->Start();
1156 } 1252 }
1157 1253
1158 1254
1159 void LoginUtilsImpl::FetchPolicyToken(Profile* offrecord_profile, 1255 void LoginUtilsImpl::FetchPolicyToken(Profile* offrecord_profile,
1160 const std::string& token, 1256 const std::string& token,
1161 const std::string& secret) { 1257 const std::string& secret) {
1162 // Trigger oauth token fetch for user policy. 1258 // Fetch dm service token now, if it hasn't been fetched yet.
1163 policy_oauth_fetcher_.reset(new PolicyOAuthFetcher(offrecord_profile, 1259 if (!policy_oauth_fetcher_.get() || policy_oauth_fetcher_->failed()) {
1164 token, 1260 // Trigger oauth token fetch for user policy.
1165 secret)); 1261 policy_oauth_fetcher_.reset(new PolicyOAuthFetcher(offrecord_profile,
1166 policy_oauth_fetcher_->Start(); 1262 token,
1263 secret));
1264 policy_oauth_fetcher_->Start();
1265 }
1167 1266
1168 // TODO(zelidrag): We should add initialization of other services somewhere 1267 // TODO(zelidrag): We should add initialization of other services somewhere
1169 // here as well. This could be handled with TokenService class once it is 1268 // here as well. This could be handled with TokenService class once it is
1170 // ready to handle OAuth tokens. 1269 // ready to handle OAuth tokens.
1171 1270
1172 // We don't need authenticator instance any more, reset it so that 1271 // We don't need authenticator instance any more, reset it so that
1173 // ScreenLocker would create a separate instance. 1272 // ScreenLocker would create a separate instance.
1174 // TODO(nkostylev): There's a potential race if SL would be created before 1273 // TODO(nkostylev): There's a potential race if SL would be created before
1175 // OAuth tokens are fetched. It would use incorrect Authenticator instance. 1274 // OAuth tokens are fetched. It would use incorrect Authenticator instance.
1176 authenticator_ = NULL; 1275 authenticator_ = NULL;
1177 } 1276 }
1178 1277
1179 void LoginUtilsImpl::OnOAuthGetAccessTokenFailure(
1180 const GoogleServiceAuthError& error) {
1181 // TODO(zelidrag): Pop up sync setup UI here?
1182 LOG(WARNING) << "Failed fetching OAuth v1 token, error: " << error.state();
1183 }
1184
1185 void LoginUtilsImpl::OnOnlineStateChanged(bool online) { 1278 void LoginUtilsImpl::OnOnlineStateChanged(bool online) {
1186 // If we come online for the first time after successful offline login, 1279 // If we come online for the first time after successful offline login,
1187 // we need to kick of OAuth token verification process again. 1280 // we need to kick of OAuth token verification process again.
1188 if (UserManager::Get()->user_is_logged_in() && 1281 if (UserManager::Get()->user_is_logged_in() &&
1189 UserManager::Get()->offline_login() && online) { 1282 UserManager::Get()->offline_login() && online) {
1190 KickStartAuthentication(ProfileManager::GetDefaultProfile()); 1283 KickStartAuthentication(ProfileManager::GetDefaultProfile());
1191 } 1284 }
1192 } 1285 }
1193 1286
1194 LoginUtils* LoginUtils::Get() { 1287 LoginUtils* LoginUtils::Get() {
(...skipping 26 matching lines...) Expand all
1221 // Mark login host for deletion after browser starts. This 1314 // Mark login host for deletion after browser starts. This
1222 // guarantees that the message loop will be referenced by the 1315 // guarantees that the message loop will be referenced by the
1223 // browser before it is dereferenced by the login host. 1316 // browser before it is dereferenced by the login host.
1224 if (login_host) { 1317 if (login_host) {
1225 login_host->OnSessionStart(); 1318 login_host->OnSessionStart();
1226 login_host = NULL; 1319 login_host = NULL;
1227 } 1320 }
1228 } 1321 }
1229 1322
1230 } // namespace chromeos 1323 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698