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

Side by Side Diff: chrome/browser/ui/sync/one_click_signin_sync_starter.cc

Issue 2594703002: Create a new profile and sign in the user. (Closed)
Patch Set: Rebase Created 3 years, 12 months 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
OLDNEW
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
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
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);
143 skip_sync_confirm_ = false;
145 } 144 }
146 145
147 void OneClickSigninSyncStarter::ConfirmSignin(const std::string& oauth_token) { 146 void OneClickSigninSyncStarter::ConfirmSignin(ProfileMode profile_mode,
147 const std::string& oauth_token) {
148 DCHECK(!oauth_token.empty()); 148 DCHECK(!oauth_token.empty());
149 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); 149 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_);
150 // If this is a new signin (no account authenticated yet) try loading 150 if (signin->IsAuthenticated()) {
151 // policy for this user now, before any signed in services are initialized.
152 if (!signin->IsAuthenticated()) {
153 policy::UserPolicySigninService* policy_service =
154 policy::UserPolicySigninServiceFactory::GetForProfile(profile_);
155 policy_service->RegisterForPolicy(
156 signin->GetUsernameForAuthInProgress(),
157 oauth_token,
158 base::Bind(&OneClickSigninSyncStarter::OnRegisteredForPolicy,
159 weak_pointer_factory_.GetWeakPtr()));
160 return;
161 } else {
162 // The user is already signed in - just tell SigninManager to continue 151 // The user is already signed in - just tell SigninManager to continue
163 // with its re-auth flow. 152 // with its re-auth flow.
153 DCHECK_EQ(CURRENT_PROFILE, profile_mode);
164 signin->CompletePendingSignin(); 154 signin->CompletePendingSignin();
155 return;
156 }
157
158 switch (profile_mode) {
159 case CURRENT_PROFILE: {
160 // If this is a new signin (no account authenticated yet) try loading
161 // policy for this user now, before any signed in services are
162 // initialized.
163 policy::UserPolicySigninService* policy_service =
164 policy::UserPolicySigninServiceFactory::GetForProfile(profile_);
165 policy_service->RegisterForPolicy(
166 signin->GetUsernameForAuthInProgress(), oauth_token,
167 base::Bind(&OneClickSigninSyncStarter::OnRegisteredForPolicy,
168 weak_pointer_factory_.GetWeakPtr()));
169 break;
170 }
171 case NEW_PROFILE:
172 // If this is a new signin (no account authenticated yet) in a new
173 // profile, then just create the new signed-in profile and skip loading
174 // the policy as there is no need to ask the user again if they should be
175 // signed in to a new profile. Note that in this case the policy will be
176 // applied after the new profile is signed in.
177 CreateNewSignedInProfile();
178 break;
165 } 179 }
166 } 180 }
167 181
168 OneClickSigninSyncStarter::SigninDialogDelegate::SigninDialogDelegate( 182 OneClickSigninSyncStarter::SigninDialogDelegate::SigninDialogDelegate(
169 base::WeakPtr<OneClickSigninSyncStarter> sync_starter) 183 base::WeakPtr<OneClickSigninSyncStarter> sync_starter)
170 : sync_starter_(sync_starter) { 184 : sync_starter_(sync_starter) {
171 } 185 }
172 186
173 OneClickSigninSyncStarter::SigninDialogDelegate::~SigninDialogDelegate() { 187 OneClickSigninSyncStarter::SigninDialogDelegate::~SigninDialogDelegate() {
174 } 188 }
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 // we ever want to change this behavior, we could call 270 // we ever want to change this behavior, we could call
257 // SigninManager::SignOut() here instead. 271 // SigninManager::SignOut() here instead.
258 DLOG_IF(ERROR, !success) << "Error fetching policy for user"; 272 DLOG_IF(ERROR, !success) << "Error fetching policy for user";
259 DVLOG_IF(1, success) << "Policy fetch successful - completing signin"; 273 DVLOG_IF(1, success) << "Policy fetch successful - completing signin";
260 SigninManagerFactory::GetForProfile(profile_)->CompletePendingSignin(); 274 SigninManagerFactory::GetForProfile(profile_)->CompletePendingSignin();
261 } 275 }
262 276
263 void OneClickSigninSyncStarter::CreateNewSignedInProfile() { 277 void OneClickSigninSyncStarter::CreateNewSignedInProfile() {
264 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_); 278 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_);
265 DCHECK(!signin->GetUsernameForAuthInProgress().empty()); 279 DCHECK(!signin->GetUsernameForAuthInProgress().empty());
266 DCHECK(!dm_token_.empty()); 280
267 DCHECK(!client_id_.empty());
268 // Create a new profile and have it call back when done so we can inject our 281 // Create a new profile and have it call back when done so we can inject our
269 // signin credentials. 282 // signin credentials.
270 size_t icon_index = g_browser_process->profile_manager()-> 283 size_t icon_index = g_browser_process->profile_manager()->
271 GetProfileAttributesStorage().ChooseAvatarIconIndexForNewProfile(); 284 GetProfileAttributesStorage().ChooseAvatarIconIndexForNewProfile();
272 ProfileManager::CreateMultiProfileAsync( 285 ProfileManager::CreateMultiProfileAsync(
273 base::UTF8ToUTF16(signin->GetUsernameForAuthInProgress()), 286 base::UTF8ToUTF16(signin->GetUsernameForAuthInProgress()),
274 profiles::GetDefaultAvatarIconUrl(icon_index), 287 profiles::GetDefaultAvatarIconUrl(icon_index),
275 base::Bind(&OneClickSigninSyncStarter::CompleteInitForNewProfile, 288 base::Bind(&OneClickSigninSyncStarter::CompleteInitForNewProfile,
276 weak_pointer_factory_.GetWeakPtr()), 289 weak_pointer_factory_.GetWeakPtr()),
277 std::string()); 290 std::string());
(...skipping 17 matching lines...) Expand all
295 } 308 }
296 case Profile::CREATE_STATUS_INITIALIZED: { 309 case Profile::CREATE_STATUS_INITIALIZED: {
297 // Wait until the profile is initialized before we transfer credentials. 310 // Wait until the profile is initialized before we transfer credentials.
298 SigninManager* old_signin_manager = 311 SigninManager* old_signin_manager =
299 SigninManagerFactory::GetForProfile(profile_); 312 SigninManagerFactory::GetForProfile(profile_);
300 SigninManager* new_signin_manager = 313 SigninManager* new_signin_manager =
301 SigninManagerFactory::GetForProfile(new_profile); 314 SigninManagerFactory::GetForProfile(new_profile);
302 DCHECK(!old_signin_manager->GetUsernameForAuthInProgress().empty()); 315 DCHECK(!old_signin_manager->GetUsernameForAuthInProgress().empty());
303 DCHECK(!old_signin_manager->IsAuthenticated()); 316 DCHECK(!old_signin_manager->IsAuthenticated());
304 DCHECK(!new_signin_manager->IsAuthenticated()); 317 DCHECK(!new_signin_manager->IsAuthenticated());
305 DCHECK(!dm_token_.empty());
306 DCHECK(!client_id_.empty());
307 318
308 // Copy credentials from the old profile to the just-created profile, 319 // Copy credentials from the old profile to the just-created profile,
309 // and switch over to tracking that profile. 320 // and switch over to tracking that profile.
310 new_signin_manager->CopyCredentialsFrom(*old_signin_manager); 321 new_signin_manager->CopyCredentialsFrom(*old_signin_manager);
311 FinishProfileSyncServiceSetup(); 322 FinishProfileSyncServiceSetup();
312 Initialize(new_profile, nullptr); 323 Initialize(new_profile, nullptr);
313 DCHECK_EQ(profile_, new_profile); 324 DCHECK_EQ(profile_, new_profile);
325 skip_sync_confirm_ = true;
314 326
315 // We've transferred our credentials to the new profile - notify that 327 // We've transferred our credentials to the new profile - notify that
316 // the signin for the original profile was cancelled (must do this after 328 // the signin for the original profile was cancelled (must do this after
317 // we have called Initialize() with the new profile, as otherwise this 329 // we have called Initialize() with the new profile, as otherwise this
318 // object will get freed when the signin on the old profile is cancelled. 330 // object will get freed when the signin on the old profile is cancelled.
319 old_signin_manager->SignOut(signin_metrics::TRANSFER_CREDENTIALS, 331 old_signin_manager->SignOut(signin_metrics::TRANSFER_CREDENTIALS,
320 signin_metrics::SignoutDelete::IGNORE_METRIC); 332 signin_metrics::SignoutDelete::IGNORE_METRIC);
321 333
322 // Load policy for the just-created profile - once policy has finished 334 if (!dm_token_.empty()) {
323 // loading the signin process will complete. 335 // Load policy for the just-created profile - once policy has finished
324 LoadPolicyWithCachedCredentials(); 336 // loading the signin process will complete.
337 DCHECK(!client_id_.empty());
338 LoadPolicyWithCachedCredentials();
339 } else {
340 // No policy to load - simply complete the signin process.
341 SigninManagerFactory::GetForProfile(profile_)->CompletePendingSignin();
342 }
325 343
326 // Unlock the new profile. 344 // Unlock the new profile.
327 ProfileAttributesEntry* entry; 345 ProfileAttributesEntry* entry;
328 bool has_entry = 346 bool has_entry =
329 g_browser_process->profile_manager() 347 g_browser_process->profile_manager()
330 ->GetProfileAttributesStorage() 348 ->GetProfileAttributesStorage()
331 .GetProfileAttributesWithPath(new_profile->GetPath(), &entry); 349 .GetProfileAttributesWithPath(new_profile->GetPath(), &entry);
332 DCHECK(has_entry); 350 DCHECK(has_entry);
333 entry->SetIsSigninRequired(false); 351 entry->SetIsSigninRequired(false);
334 352
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
469 void OneClickSigninSyncStarter::AccountAddedToCookie( 487 void OneClickSigninSyncStarter::AccountAddedToCookie(
470 const GoogleServiceAuthError& error) { 488 const GoogleServiceAuthError& error) {
471 if (first_account_added_to_cookie_) 489 if (first_account_added_to_cookie_)
472 return; 490 return;
473 491
474 first_account_added_to_cookie_ = true; 492 first_account_added_to_cookie_ = true;
475 493
476 // Regardless of whether the account was successfully added or not, 494 // Regardless of whether the account was successfully added or not,
477 // continue with sync starting. 495 // continue with sync starting.
478 496
497 // TODO(zmin): Remove this hack once the https://crbug.com/657924 fixed.
498 // Skip the Sync confirmation dialog if user choose to create a new profile
499 // for the corp signin. This is because the dialog doesn't work properly
500 // after the corp signin.
501 if (skip_sync_confirm_) {
502 OnSyncConfirmationUIClosed(LoginUIService::ABORT_SIGNIN);
503 return;
504 }
479 505
480 if (switches::UsePasswordSeparatedSigninFlow()) { 506 if (switches::UsePasswordSeparatedSigninFlow()) {
481 // Under the new signin flow, the sync confirmation dialog should always be 507 // Under the new signin flow, the sync confirmation dialog should always be
482 // shown regardless of |start_mode_|. |sync_setup_completed_callback_| will 508 // shown regardless of |start_mode_|. |sync_setup_completed_callback_| will
483 // be run after the modal is closed. 509 // be run after the modal is closed.
484 DisplayModalSyncConfirmationWindow(); 510 DisplayModalSyncConfirmationWindow();
485 return; 511 return;
486 } 512 }
487 513
488 if (!sync_setup_completed_callback_.is_null()) 514 if (!sync_setup_completed_callback_.is_null())
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 false /* user_gesture */); 679 false /* user_gesture */);
654 } 680 }
655 681
656 void OneClickSigninSyncStarter::LoadContinueUrl() { 682 void OneClickSigninSyncStarter::LoadContinueUrl() {
657 web_contents()->GetController().LoadURL( 683 web_contents()->GetController().LoadURL(
658 continue_url_, 684 continue_url_,
659 content::Referrer(), 685 content::Referrer(),
660 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, 686 ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
661 std::string()); 687 std::string());
662 } 688 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698