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.
5 #ifndef CHROME_BROWSER_UI_SYNC_ONE_CLICK_SIGNIN_SYNC_STARTER_H_
6 #define CHROME_BROWSER_UI_SYNC_ONE_CLICK_SIGNIN_SYNC_STARTER_H_
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"
23 class ProfileSyncService
;
27 } // namespace content
30 class CloudPolicyClient
;
33 // Waits for successful singin notification from the signin manager and then
34 // starts the sync machine. Instances of this class delete themselves once
36 class OneClickSigninSyncStarter
: public SigninTracker::Observer
,
37 public chrome::BrowserListObserver
,
38 public content::WebContentsObserver
{
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
,
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.
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
,
55 // The process should be aborted because the undo button has been pressed.
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).
64 // Signin flow redirected outside of trusted domains, so ask the user to
65 // confirm before signing in.
66 CONFIRM_UNTRUSTED_SIGNIN
,
68 // Display a confirmation after signing in.
72 // Result of the sync setup.
73 enum SyncSetupResult
{
78 typedef base::Callback
<void(SyncSetupResult
)> Callback
;
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 UI if it's showing a blank page
85 // and not about to be closed. It can be NULL.
86 // |callback| is always executed before OneClickSigninSyncStarter is deleted.
88 OneClickSigninSyncStarter(Profile
* profile
,
90 const std::string
& session_index
,
91 const std::string
& email
,
92 const std::string
& password
,
93 StartSyncMode start_mode
,
94 content::WebContents
* web_contents
,
95 ConfirmationRequired display_confirmation
,
96 signin::Source source
,
99 // chrome::BrowserListObserver override.
100 virtual void OnBrowserRemoved(Browser
* browser
) OVERRIDE
;
103 friend class OneClickSigninSyncStarterTest
;
104 FRIEND_TEST_ALL_PREFIXES(OneClickSigninSyncStarterTest
,
105 CallbackSigninFailed
);
106 FRIEND_TEST_ALL_PREFIXES(OneClickSigninSyncStarterTest
,
107 CallbackSigninSucceeded
);
108 FRIEND_TEST_ALL_PREFIXES(OneClickSigninSyncStarterTest
,
111 virtual ~OneClickSigninSyncStarter();
113 // Initializes the internals of the OneClickSigninSyncStarter object. Can also
114 // be used to re-initialize the object to refer to a newly created profile.
115 void Initialize(Profile
* profile
, Browser
* browser
);
117 // SigninTracker::Observer override.
118 virtual void SigninFailed(const GoogleServiceAuthError
& error
) OVERRIDE
;
119 virtual void SigninSuccess() OVERRIDE
;
121 #if defined(ENABLE_CONFIGURATION_POLICY)
122 // User input handler for the signin confirmation dialog.
123 class SigninDialogDelegate
124 : public ui::ProfileSigninConfirmationDelegate
{
126 SigninDialogDelegate(
127 base::WeakPtr
<OneClickSigninSyncStarter
> sync_starter
);
128 virtual ~SigninDialogDelegate();
129 virtual void OnCancelSignin() OVERRIDE
;
130 virtual void OnContinueSignin() OVERRIDE
;
131 virtual void OnSigninWithNewProfile() OVERRIDE
;
133 base::WeakPtr
<OneClickSigninSyncStarter
> sync_starter_
;
135 friend class SigninDialogDelegate
;
137 // Callback invoked once policy registration is complete. If registration
138 // fails, |client| will be null.
139 void OnRegisteredForPolicy(scoped_ptr
<policy::CloudPolicyClient
> client
);
141 // Callback invoked when a policy fetch request has completed. |success| is
142 // true if policy was successfully fetched.
143 void OnPolicyFetchComplete(bool success
);
145 // Called to create a new profile, which is then signed in with the
146 // in-progress auth credentials currently stored in this object.
147 void CreateNewSignedInProfile();
149 // Helper function that loads policy with the passed CloudPolicyClient, then
150 // completes the signin process.
151 void LoadPolicyWithCachedClient();
153 // Callback invoked once a profile is created, so we can complete the
154 // credentials transfer, load policy, and open the first window.
155 void CompleteInitForNewProfile(chrome::HostDesktopType desktop_type
,
157 Profile::CreateStatus status
);
159 // Cancels the in-progress signin for this profile.
160 void CancelSigninAndDelete();
161 #endif // defined(ENABLE_CONFIGURATION_POLICY)
163 // Callback invoked to check whether the user needs policy or if a
164 // confirmation is required (in which case we have to prompt the user first).
165 void ConfirmSignin(const std::string
& oauth_token
);
167 // Displays confirmation UI to the user if confirmation_required_ ==
168 // CONFIRM_UNTRUSTED_SIGNIN, otherwise completes the pending signin process.
169 void ConfirmAndSignin();
171 // Callback invoked once the user has responded to the signin confirmation UI.
172 // If response == UNDO_SYNC, the signin is cancelled, otherwise the pending
173 // signin is completed.
174 void UntrustedSigninConfirmed(StartSyncMode response
);
176 // GetProfileSyncService returns non-NULL pointer if sync is enabled.
177 // There is a scenario when when ProfileSyncService discovers that sync is
178 // disabled during setup. In this case GetProfileSyncService will return NULL,
179 // but we still need to call PSS::SetSetupInProgress(false). For this purpose
180 // call FinishProfileSyncServiceSetup() function.
181 ProfileSyncService
* GetProfileSyncService();
183 void FinishProfileSyncServiceSetup();
185 // Displays the settings UI and brings up the advanced sync settings
186 // dialog if |configure_sync| is true. The web contents provided to the
187 // constructor is used if it's showing a blank page and not about to be
188 // closed. Otherwise, a new tab or an existing settings tab is used.
189 void ShowSettingsPage(bool configure_sync
);
191 // Displays a settings page in the provided web contents. |sub_page| can be
192 // empty to show the main settings page.
193 void ShowSettingsPageInWebContents(content::WebContents
* contents
,
194 const std::string
& sub_page
);
196 // Shows the post-signin confirmation bubble. If |custom_message| is empty,
197 // the default "You are signed in" message is displayed.
198 void DisplayFinalConfirmationBubble(const string16
& custom_message
);
200 // Makes sure browser_ points to a valid browser (opens a new browser if
201 // necessary). Useful in the case where the user has created a new Profile as
202 // part of the signin process.
203 void EnsureBrowser();
207 scoped_ptr
<SigninTracker
> signin_tracker_
;
208 StartSyncMode start_mode_
;
209 chrome::HostDesktopType desktop_type_
;
210 bool force_same_tab_navigation_
;
211 ConfirmationRequired confirmation_required_
;
212 signin::Source source_
;
214 // Callback executed when sync setup succeeds or fails.
215 Callback sync_setup_completed_callback_
;
217 base::WeakPtrFactory
<OneClickSigninSyncStarter
> weak_pointer_factory_
;
219 #if defined(ENABLE_CONFIGURATION_POLICY)
220 // CloudPolicyClient reference we keep while determining whether to create
221 // a new profile for an enterprise user or not.
222 scoped_ptr
<policy::CloudPolicyClient
> policy_client_
;
225 DISALLOW_COPY_AND_ASSIGN(OneClickSigninSyncStarter
);
229 #endif // CHROME_BROWSER_UI_SYNC_ONE_CLICK_SIGNIN_SYNC_STARTER_H_