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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_UI_SYNC_ONE_CLICK_SIGNIN_SYNC_STARTER_H_ 6 #define CHROME_BROWSER_UI_SYNC_ONE_CLICK_SIGNIN_SYNC_STARTER_H_ 7 8 #include <string> 9 10 #include "base/callback_forward.h" 11 #include "base/gtest_prod_util.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/weak_ptr.h" 14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/signin/signin_promo.h" 16 #include "chrome/browser/signin/signin_tracker.h" 17 #include "chrome/browser/ui/browser_list_observer.h" 18 #include "chrome/browser/ui/host_desktop.h" 19 #include "chrome/browser/ui/sync/profile_signin_confirmation_helper.h" 20 #include "content/public/browser/web_contents_observer.h" 21 22 class Browser; 23 class ProfileSyncService; 24 25 namespace content { 26 class WebContents; 27 } // namespace content 28 29 namespace policy { 30 class CloudPolicyClient; 31 } 32 33 // Waits for successful singin notification from the signin manager and then 34 // starts the sync machine. Instances of this class delete themselves once 35 // the job is done. 36 class OneClickSigninSyncStarter : public SigninTracker::Observer, 37 public chrome::BrowserListObserver, 38 public content::WebContentsObserver { 39 public: 40 enum StartSyncMode { 41 // Starts the process of signing the user in with the SigninManager, and 42 // once completed automatically starts sync with all data types enabled. 43 SYNC_WITH_DEFAULT_SETTINGS, 44 45 // Starts the process of signing the user in with the SigninManager, and 46 // once completed redirects the user to the settings page to allow them 47 // to configure which data types to sync before sync is enabled. 48 CONFIGURE_SYNC_FIRST, 49 50 // Starts the process of re-authenticating the user via SigninManager, 51 // and once completed, redirects the user to the settings page, but doesn't 52 // display the configure sync UI. 53 SHOW_SETTINGS_WITHOUT_CONFIGURE, 54 55 // The process should be aborted because the undo button has been pressed. 56 UNDO_SYNC 57 }; 58 59 enum ConfirmationRequired { 60 // No need to display a "post-signin" confirmation bubble (for example, if 61 // the user was doing a re-auth flow). 62 NO_CONFIRMATION, 63 64 // Signin flow redirected outside of trusted domains, so ask the user to 65 // confirm before signing in. 66 CONFIRM_UNTRUSTED_SIGNIN, 67 68 // Display a confirmation after signing in. 69 CONFIRM_AFTER_SIGNIN 70 }; 71 72 // Result of the sync setup. 73 enum SyncSetupResult { 74 SYNC_SETUP_SUCCESS, 75 SYNC_SETUP_FAILURE 76 }; 77 78 typedef base::Callback<void(SyncSetupResult)> Callback; 79 80 // |profile| must not be NULL, however |browser| can be. When using the 81 // OneClickSigninSyncStarter from a browser, provide both. 82 // If |display_confirmation| is true, the user will be prompted to confirm the 83 // signin before signin completes. 84 // |web_contents| is used to show the sync setup page, if necessary. If NULL, 85 // the sync setup page will be loaded in either a new tab or a tab that is 86 // already showing it. 87 // |callback| is always executed before OneClickSigninSyncStarter is deleted. 88 // It can be empty. 89 OneClickSigninSyncStarter(Profile* profile, 90 Browser* browser, 91 const std::string& session_index, 92 const std::string& email, 93 const std::string& password, 94 StartSyncMode start_mode, 95 content::WebContents* web_contents, 96 ConfirmationRequired display_confirmation, 97 signin::Source source, 98 Callback callback); 99 100 // chrome::BrowserListObserver override. 101 virtual void OnBrowserRemoved(Browser* browser) OVERRIDE; 102 103 private: 104 friend class OneClickSigninSyncStarterTest; 105 FRIEND_TEST_ALL_PREFIXES(OneClickSigninSyncStarterTest, 106 CallbackSigninFailed); 107 FRIEND_TEST_ALL_PREFIXES(OneClickSigninSyncStarterTest, 108 CallbackSigninSucceeded); 109 FRIEND_TEST_ALL_PREFIXES(OneClickSigninSyncStarterTest, 110 CallbackNull); 111 112 virtual ~OneClickSigninSyncStarter(); 113 114 // Initializes the internals of the OneClickSigninSyncStarter object. Can also 115 // be used to re-initialize the object to refer to a newly created profile. 116 void Initialize(Profile* profile, Browser* browser); 117 118 // SigninTracker::Observer override. 119 virtual void SigninFailed(const GoogleServiceAuthError& error) OVERRIDE; 120 virtual void SigninSuccess() OVERRIDE; 121 122 #if defined(ENABLE_CONFIGURATION_POLICY) 123 // User input handler for the signin confirmation dialog. 124 class SigninDialogDelegate 125 : public ui::ProfileSigninConfirmationDelegate { 126 public: 127 SigninDialogDelegate( 128 base::WeakPtr<OneClickSigninSyncStarter> sync_starter); 129 virtual ~SigninDialogDelegate(); 130 virtual void OnCancelSignin() OVERRIDE; 131 virtual void OnContinueSignin() OVERRIDE; 132 virtual void OnSigninWithNewProfile() OVERRIDE; 133 private: 134 base::WeakPtr<OneClickSigninSyncStarter> sync_starter_; 135 }; 136 friend class SigninDialogDelegate; 137 138 // Callback invoked once policy registration is complete. If registration 139 // fails, |client| will be null. 140 void OnRegisteredForPolicy(scoped_ptr<policy::CloudPolicyClient> client); 141 142 // Callback invoked when a policy fetch request has completed. |success| is 143 // true if policy was successfully fetched. 144 void OnPolicyFetchComplete(bool success); 145 146 // Called to create a new profile, which is then signed in with the 147 // in-progress auth credentials currently stored in this object. 148 void CreateNewSignedInProfile(); 149 150 // Helper function that loads policy with the passed CloudPolicyClient, then 151 // completes the signin process. 152 void LoadPolicyWithCachedClient(); 153 154 // Callback invoked once a profile is created, so we can complete the 155 // credentials transfer, load policy, and open the first window. 156 void CompleteInitForNewProfile(chrome::HostDesktopType desktop_type, 157 Profile* profile, 158 Profile::CreateStatus status); 159 160 // Cancels the in-progress signin for this profile. 161 void CancelSigninAndDelete(); 162 #endif // defined(ENABLE_CONFIGURATION_POLICY) 163 164 // Callback invoked to check whether the user needs policy or if a 165 // confirmation is required (in which case we have to prompt the user first). 166 void ConfirmSignin(const std::string& oauth_token); 167 168 // Displays confirmation UI to the user if confirmation_required_ == 169 // CONFIRM_UNTRUSTED_SIGNIN, otherwise completes the pending signin process. 170 void ConfirmAndSignin(); 171 172 // Callback invoked once the user has responded to the signin confirmation UI. 173 // If response == UNDO_SYNC, the signin is cancelled, otherwise the pending 174 // signin is completed. 175 void UntrustedSigninConfirmed(StartSyncMode response); 176 177 // GetProfileSyncService returns non-NULL pointer if sync is enabled. 178 // There is a scenario when when ProfileSyncService discovers that sync is 179 // disabled during setup. In this case GetProfileSyncService will return NULL, 180 // but we still need to call PSS::SetSetupInProgress(false). For this purpose 181 // call FinishProfileSyncServiceSetup() function. 182 ProfileSyncService* GetProfileSyncService(); 183 184 void FinishProfileSyncServiceSetup(); 185 186 // Displays the settings UI in a new tab. Brings up the advanced sync settings 187 // dialog if |configure_sync| is true. 188 void ShowSettingsPageInNewTab(bool configure_sync); 189 190 // Displays the sync configuration UI in the provided web contents. 191 void ShowSyncSettingsPageInWebContents(content::WebContents* contents); 192 193 // Shows the post-signin confirmation bubble. If |custom_message| is empty, 194 // the default "You are signed in" message is displayed. 195 void DisplayFinalConfirmationBubble(const string16& custom_message); 196 197 // Makes sure browser_ points to a valid browser (opens a new browser if 198 // necessary). Useful in the case where the user has created a new Profile as 199 // part of the signin process. 200 void EnsureBrowser(); 201 202 Profile* profile_; 203 Browser* browser_; 204 scoped_ptr<SigninTracker> signin_tracker_; 205 StartSyncMode start_mode_; 206 chrome::HostDesktopType desktop_type_; 207 bool force_same_tab_navigation_; 208 ConfirmationRequired confirmation_required_; 209 signin::Source source_; 210 211 // Callback executed when sync setup succeeds or fails. 212 Callback sync_setup_completed_callback_; 213 214 base::WeakPtrFactory<OneClickSigninSyncStarter> weak_pointer_factory_; 215 216 #if defined(ENABLE_CONFIGURATION_POLICY) 217 // CloudPolicyClient reference we keep while determining whether to create 218 // a new profile for an enterprise user or not. 219 scoped_ptr<policy::CloudPolicyClient> policy_client_; 220 #endif 221 222 DISALLOW_COPY_AND_ASSIGN(OneClickSigninSyncStarter); 223 }; 224 225 226 #endif // CHROME_BROWSER_UI_SYNC_ONE_CLICK_SIGNIN_SYNC_STARTER_H_ 227