Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/sync/one_click_signin_sync_starter.h" | 5 #include "chrome/browser/ui/sync/one_click_signin_sync_starter.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 | 73 |
| 74 } // namespace | 74 } // namespace |
| 75 | 75 |
| 76 OneClickSigninSyncStarter::OneClickSigninSyncStarter( | 76 OneClickSigninSyncStarter::OneClickSigninSyncStarter( |
| 77 Profile* profile, | 77 Profile* profile, |
| 78 Browser* browser, | 78 Browser* browser, |
| 79 const std::string& gaia_id, | 79 const std::string& gaia_id, |
| 80 const std::string& email, | 80 const std::string& email, |
| 81 const std::string& password, | 81 const std::string& password, |
| 82 const std::string& refresh_token, | 82 const std::string& refresh_token, |
| 83 ProfileMode profile_mode, | |
| 83 StartSyncMode start_mode, | 84 StartSyncMode start_mode, |
| 84 content::WebContents* web_contents, | 85 content::WebContents* web_contents, |
| 85 ConfirmationRequired confirmation_required, | 86 ConfirmationRequired confirmation_required, |
| 86 const GURL& current_url, | 87 const GURL& current_url, |
| 87 const GURL& continue_url, | 88 const GURL& continue_url, |
| 88 Callback sync_setup_completed_callback) | 89 Callback sync_setup_completed_callback) |
| 89 : content::WebContentsObserver(web_contents), | 90 : content::WebContentsObserver(web_contents), |
| 90 profile_(nullptr), | 91 profile_(nullptr), |
| 91 start_mode_(start_mode), | 92 start_mode_(start_mode), |
| 92 confirmation_required_(confirmation_required), | 93 confirmation_required_(confirmation_required), |
| 93 current_url_(current_url), | 94 current_url_(current_url), |
| 94 continue_url_(continue_url), | 95 continue_url_(continue_url), |
| 95 sync_setup_completed_callback_(sync_setup_completed_callback), | 96 sync_setup_completed_callback_(sync_setup_completed_callback), |
| 96 first_account_added_to_cookie_(false), | 97 first_account_added_to_cookie_(false), |
| 97 weak_pointer_factory_(this) { | 98 weak_pointer_factory_(this) { |
| 98 DCHECK(profile); | 99 DCHECK(profile); |
| 99 DCHECK(web_contents || continue_url.is_empty()); | 100 DCHECK(web_contents || continue_url.is_empty()); |
| 100 BrowserList::AddObserver(this); | 101 BrowserList::AddObserver(this); |
| 101 Initialize(profile, browser); | 102 Initialize(profile, browser); |
| 102 | 103 |
| 103 // Policy is enabled, so pass in a callback to do extra policy-related UI | 104 SigninManagerFactory::GetForProfile(profile_)->StartSignInWithRefreshToken( |
| 104 // before signin completes. | 105 refresh_token, gaia_id, email, password, |
| 105 SigninManagerFactory::GetForProfile(profile_)-> | 106 base::Bind(&OneClickSigninSyncStarter::ConfirmSignin, |
| 106 StartSignInWithRefreshToken( | 107 weak_pointer_factory_.GetWeakPtr(), profile_mode)); |
| 107 refresh_token, gaia_id, email, password, | |
| 108 base::Bind(&OneClickSigninSyncStarter::ConfirmSignin, | |
| 109 weak_pointer_factory_.GetWeakPtr())); | |
| 110 } | 108 } |
| 111 | 109 |
| 112 void OneClickSigninSyncStarter::OnBrowserRemoved(Browser* browser) { | 110 void OneClickSigninSyncStarter::OnBrowserRemoved(Browser* browser) { |
| 113 if (browser == browser_) | 111 if (browser == browser_) |
| 114 browser_ = nullptr; | 112 browser_ = nullptr; |
| 115 } | 113 } |
| 116 | 114 |
| 117 OneClickSigninSyncStarter::~OneClickSigninSyncStarter() { | 115 OneClickSigninSyncStarter::~OneClickSigninSyncStarter() { |
| 118 BrowserList::RemoveObserver(this); | 116 BrowserList::RemoveObserver(this); |
| 119 LoginUIServiceFactory::GetForProfile(profile_)->RemoveObserver(this); | 117 LoginUIServiceFactory::GetForProfile(profile_)->RemoveObserver(this); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 135 // Let the sync service know that setup is in progress so it doesn't start | 133 // Let the sync service know that setup is in progress so it doesn't start |
| 136 // syncing until the user has finished any configuration. | 134 // syncing until the user has finished any configuration. |
| 137 ProfileSyncService* profile_sync_service = GetProfileSyncService(); | 135 ProfileSyncService* profile_sync_service = GetProfileSyncService(); |
| 138 if (profile_sync_service) | 136 if (profile_sync_service) |
| 139 sync_blocker_ = profile_sync_service->GetSetupInProgressHandle(); | 137 sync_blocker_ = profile_sync_service->GetSetupInProgressHandle(); |
| 140 | 138 |
| 141 // Make sure the syncing is requested, otherwise the SigninManager | 139 // Make sure the syncing is requested, otherwise the SigninManager |
| 142 // will not be able to complete successfully. | 140 // will not be able to complete successfully. |
| 143 syncer::SyncPrefs sync_prefs(profile_->GetPrefs()); | 141 syncer::SyncPrefs sync_prefs(profile_->GetPrefs()); |
| 144 sync_prefs.SetSyncRequested(true); | 142 sync_prefs.SetSyncRequested(true); |
| 145 skip_sync_confirm_ = false; | |
| 146 } | 143 } |
| 147 | 144 |
| 148 void OneClickSigninSyncStarter::ConfirmSignin(const std::string& oauth_token) { | 145 void OneClickSigninSyncStarter::ConfirmSignin(ProfileMode profile_mode, |
| 146 const std::string& oauth_token) { | |
| 149 DCHECK(!oauth_token.empty()); | 147 DCHECK(!oauth_token.empty()); |
| 150 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); | 148 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); |
| 151 // If this is a new signin (no account authenticated yet) try loading | 149 if (signin->IsAuthenticated()) { |
| 152 // policy for this user now, before any signed in services are initialized. | |
| 153 if (!signin->IsAuthenticated()) { | |
| 154 policy::UserPolicySigninService* policy_service = | |
| 155 policy::UserPolicySigninServiceFactory::GetForProfile(profile_); | |
| 156 policy_service->RegisterForPolicy( | |
| 157 signin->GetUsernameForAuthInProgress(), | |
| 158 oauth_token, | |
| 159 base::Bind(&OneClickSigninSyncStarter::OnRegisteredForPolicy, | |
| 160 weak_pointer_factory_.GetWeakPtr())); | |
| 161 return; | |
| 162 } else { | |
| 163 // The user is already signed in - just tell SigninManager to continue | 150 // The user is already signed in - just tell SigninManager to continue |
| 164 // with its re-auth flow. | 151 // with its re-auth flow. |
| 152 DCHECK_EQ(CURRENT_PROFILE, profile_mode); | |
| 165 signin->CompletePendingSignin(); | 153 signin->CompletePendingSignin(); |
| 154 return; | |
| 155 } | |
| 156 | |
| 157 switch (profile_mode) { | |
| 158 case CURRENT_PROFILE: { | |
| 159 // If this is a new signin (no account authenticated yet) try loading | |
| 160 // policy for this user now, before any signed in services are | |
| 161 // initialized. | |
| 162 policy::UserPolicySigninService* policy_service = | |
| 163 policy::UserPolicySigninServiceFactory::GetForProfile(profile_); | |
| 164 policy_service->RegisterForPolicy( | |
| 165 signin->GetUsernameForAuthInProgress(), oauth_token, | |
| 166 base::Bind(&OneClickSigninSyncStarter::OnRegisteredForPolicy, | |
| 167 weak_pointer_factory_.GetWeakPtr())); | |
| 168 break; | |
| 169 } | |
| 170 case NEW_PROFILE: | |
| 171 // If this is a new signin (no account authenticated yet) in a new | |
| 172 // profile, then just create the new signed-in profile and skip loading | |
| 173 // the policy as there is no need to ask the user again if they should be | |
| 174 // signed in to a new profile. Note that in this case the policy will be | |
| 175 // applied after the new profile is signed in. | |
| 176 CreateNewSignedInProfile(); | |
| 177 break; | |
| 166 } | 178 } |
| 167 } | 179 } |
| 168 | 180 |
| 169 OneClickSigninSyncStarter::SigninDialogDelegate::SigninDialogDelegate( | 181 OneClickSigninSyncStarter::SigninDialogDelegate::SigninDialogDelegate( |
| 170 base::WeakPtr<OneClickSigninSyncStarter> sync_starter) | 182 base::WeakPtr<OneClickSigninSyncStarter> sync_starter) |
| 171 : sync_starter_(sync_starter) { | 183 : sync_starter_(sync_starter) { |
| 172 } | 184 } |
| 173 | 185 |
| 174 OneClickSigninSyncStarter::SigninDialogDelegate::~SigninDialogDelegate() { | 186 OneClickSigninSyncStarter::SigninDialogDelegate::~SigninDialogDelegate() { |
| 175 } | 187 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 // we ever want to change this behavior, we could call | 269 // we ever want to change this behavior, we could call |
| 258 // SigninManager::SignOut() here instead. | 270 // SigninManager::SignOut() here instead. |
| 259 DLOG_IF(ERROR, !success) << "Error fetching policy for user"; | 271 DLOG_IF(ERROR, !success) << "Error fetching policy for user"; |
| 260 DVLOG_IF(1, success) << "Policy fetch successful - completing signin"; | 272 DVLOG_IF(1, success) << "Policy fetch successful - completing signin"; |
| 261 SigninManagerFactory::GetForProfile(profile_)->CompletePendingSignin(); | 273 SigninManagerFactory::GetForProfile(profile_)->CompletePendingSignin(); |
| 262 } | 274 } |
| 263 | 275 |
| 264 void OneClickSigninSyncStarter::CreateNewSignedInProfile() { | 276 void OneClickSigninSyncStarter::CreateNewSignedInProfile() { |
| 265 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); | 277 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); |
| 266 DCHECK(!signin->GetUsernameForAuthInProgress().empty()); | 278 DCHECK(!signin->GetUsernameForAuthInProgress().empty()); |
| 267 DCHECK(!dm_token_.empty()); | 279 |
| 268 DCHECK(!client_id_.empty()); | |
| 269 // Create a new profile and have it call back when done so we can inject our | 280 // Create a new profile and have it call back when done so we can inject our |
| 270 // signin credentials. | 281 // signin credentials. |
| 271 size_t icon_index = g_browser_process->profile_manager()-> | 282 size_t icon_index = g_browser_process->profile_manager()-> |
| 272 GetProfileAttributesStorage().ChooseAvatarIconIndexForNewProfile(); | 283 GetProfileAttributesStorage().ChooseAvatarIconIndexForNewProfile(); |
| 273 ProfileManager::CreateMultiProfileAsync( | 284 ProfileManager::CreateMultiProfileAsync( |
| 274 base::UTF8ToUTF16(signin->GetUsernameForAuthInProgress()), | 285 base::UTF8ToUTF16(signin->GetUsernameForAuthInProgress()), |
| 275 profiles::GetDefaultAvatarIconUrl(icon_index), | 286 profiles::GetDefaultAvatarIconUrl(icon_index), |
| 276 base::Bind(&OneClickSigninSyncStarter::CompleteInitForNewProfile, | 287 base::Bind(&OneClickSigninSyncStarter::CompleteInitForNewProfile, |
| 277 weak_pointer_factory_.GetWeakPtr()), | 288 weak_pointer_factory_.GetWeakPtr()), |
| 278 std::string()); | 289 std::string()); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 296 } | 307 } |
| 297 case Profile::CREATE_STATUS_INITIALIZED: { | 308 case Profile::CREATE_STATUS_INITIALIZED: { |
| 298 // Wait until the profile is initialized before we transfer credentials. | 309 // Wait until the profile is initialized before we transfer credentials. |
| 299 SigninManager* old_signin_manager = | 310 SigninManager* old_signin_manager = |
| 300 SigninManagerFactory::GetForProfile(profile_); | 311 SigninManagerFactory::GetForProfile(profile_); |
| 301 SigninManager* new_signin_manager = | 312 SigninManager* new_signin_manager = |
| 302 SigninManagerFactory::GetForProfile(new_profile); | 313 SigninManagerFactory::GetForProfile(new_profile); |
| 303 DCHECK(!old_signin_manager->GetUsernameForAuthInProgress().empty()); | 314 DCHECK(!old_signin_manager->GetUsernameForAuthInProgress().empty()); |
| 304 DCHECK(!old_signin_manager->IsAuthenticated()); | 315 DCHECK(!old_signin_manager->IsAuthenticated()); |
| 305 DCHECK(!new_signin_manager->IsAuthenticated()); | 316 DCHECK(!new_signin_manager->IsAuthenticated()); |
| 306 DCHECK(!dm_token_.empty()); | |
| 307 DCHECK(!client_id_.empty()); | |
| 308 | 317 |
| 309 // Copy credentials from the old profile to the just-created profile, | 318 // Copy credentials from the old profile to the just-created profile, |
| 310 // and switch over to tracking that profile. | 319 // and switch over to tracking that profile. |
| 311 new_signin_manager->CopyCredentialsFrom(*old_signin_manager); | 320 new_signin_manager->CopyCredentialsFrom(*old_signin_manager); |
| 312 FinishProfileSyncServiceSetup(); | 321 FinishProfileSyncServiceSetup(); |
| 313 Initialize(new_profile, nullptr); | 322 Initialize(new_profile, nullptr); |
| 314 DCHECK_EQ(profile_, new_profile); | 323 DCHECK_EQ(profile_, new_profile); |
| 315 skip_sync_confirm_ = true; | |
| 316 | 324 |
| 317 // We've transferred our credentials to the new profile - notify that | 325 // We've transferred our credentials to the new profile - notify that |
| 318 // the signin for the original profile was cancelled (must do this after | 326 // the signin for the original profile was cancelled (must do this after |
| 319 // we have called Initialize() with the new profile, as otherwise this | 327 // we have called Initialize() with the new profile, as otherwise this |
| 320 // object will get freed when the signin on the old profile is cancelled. | 328 // object will get freed when the signin on the old profile is cancelled. |
| 321 old_signin_manager->SignOut(signin_metrics::TRANSFER_CREDENTIALS, | 329 old_signin_manager->SignOut(signin_metrics::TRANSFER_CREDENTIALS, |
| 322 signin_metrics::SignoutDelete::IGNORE_METRIC); | 330 signin_metrics::SignoutDelete::IGNORE_METRIC); |
| 323 | 331 |
| 324 // Load policy for the just-created profile - once policy has finished | 332 if (!dm_token_.empty()) { |
| 325 // loading the signin process will complete. | 333 // Load policy for the just-created profile - once policy has finished |
| 326 LoadPolicyWithCachedCredentials(); | 334 // loading the signin process will complete. |
| 335 DCHECK(!client_id_.empty()); | |
| 336 LoadPolicyWithCachedCredentials(); | |
| 337 } else { | |
| 338 // No policy to load - simply complete the signin process. | |
| 339 SigninManagerFactory::GetForProfile(profile_)->CompletePendingSignin(); | |
| 340 } | |
| 327 | 341 |
| 328 // Unlock the new profile. | 342 // Unlock the new profile. |
| 329 ProfileAttributesEntry* entry; | 343 ProfileAttributesEntry* entry; |
| 330 bool has_entry = | 344 bool has_entry = |
| 331 g_browser_process->profile_manager() | 345 g_browser_process->profile_manager() |
| 332 ->GetProfileAttributesStorage() | 346 ->GetProfileAttributesStorage() |
| 333 .GetProfileAttributesWithPath(new_profile->GetPath(), &entry); | 347 .GetProfileAttributesWithPath(new_profile->GetPath(), &entry); |
| 334 DCHECK(has_entry); | 348 DCHECK(has_entry); |
| 335 entry->SetIsSigninRequired(false); | 349 entry->SetIsSigninRequired(false); |
| 336 | 350 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 471 void OneClickSigninSyncStarter::AccountAddedToCookie( | 485 void OneClickSigninSyncStarter::AccountAddedToCookie( |
| 472 const GoogleServiceAuthError& error) { | 486 const GoogleServiceAuthError& error) { |
| 473 if (first_account_added_to_cookie_) | 487 if (first_account_added_to_cookie_) |
| 474 return; | 488 return; |
| 475 | 489 |
| 476 first_account_added_to_cookie_ = true; | 490 first_account_added_to_cookie_ = true; |
| 477 | 491 |
| 478 // Regardless of whether the account was successfully added or not, | 492 // Regardless of whether the account was successfully added or not, |
| 479 // continue with sync starting. | 493 // continue with sync starting. |
| 480 | 494 |
| 481 // TODO(zmin): Remove this hack once the https://crbug.com/657924 fixed. | |
|
anthonyvd
2016/12/21 19:09:28
Are you sure you can now remove this?
msarda
2016/12/22 11:58:05
I have moved this change to its own CL https://cod
| |
| 482 // Skip the Sync confirmation dialog if user choose to create a new profile | |
| 483 // for the corp signin. This is because the dialog doesn't work properly | |
| 484 // after the corp signin. | |
| 485 if (skip_sync_confirm_) { | |
| 486 OnSyncConfirmationUIClosed(LoginUIService::ABORT_SIGNIN); | |
| 487 return; | |
| 488 } | |
| 489 | |
| 490 if (switches::UsePasswordSeparatedSigninFlow()) { | 495 if (switches::UsePasswordSeparatedSigninFlow()) { |
| 491 // Under the new signin flow, the sync confirmation dialog should always be | 496 // Under the new signin flow, the sync confirmation dialog should always be |
| 492 // shown regardless of |start_mode_|. |sync_setup_completed_callback_| will | 497 // shown regardless of |start_mode_|. |sync_setup_completed_callback_| will |
| 493 // be run after the modal is closed. | 498 // be run after the modal is closed. |
| 494 DisplayModalSyncConfirmationWindow(); | 499 DisplayModalSyncConfirmationWindow(); |
| 495 return; | 500 return; |
| 496 } | 501 } |
| 497 | 502 |
| 498 if (!sync_setup_completed_callback_.is_null()) | 503 if (!sync_setup_completed_callback_.is_null()) |
| 499 sync_setup_completed_callback_.Run(SYNC_SETUP_SUCCESS); | 504 sync_setup_completed_callback_.Run(SYNC_SETUP_SUCCESS); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 663 false /* user_gesture */); | 668 false /* user_gesture */); |
| 664 } | 669 } |
| 665 | 670 |
| 666 void OneClickSigninSyncStarter::LoadContinueUrl() { | 671 void OneClickSigninSyncStarter::LoadContinueUrl() { |
| 667 web_contents()->GetController().LoadURL( | 672 web_contents()->GetController().LoadURL( |
| 668 continue_url_, | 673 continue_url_, |
| 669 content::Referrer(), | 674 content::Referrer(), |
| 670 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | 675 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 671 std::string()); | 676 std::string()); |
| 672 } | 677 } |
| OLD | NEW |