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_tracker.h"
16 #include "chrome/browser/ui/browser_list_observer.h"
17 #include "chrome/browser/ui/host_desktop.h"
18 #include "chrome/browser/ui/sync/profile_signin_confirmation_helper.h"
19 #include "content/public/browser/web_contents_observer.h"
22 class ProfileSyncService
;
26 } // namespace content
28 // Waits for successful singin notification from the signin manager and then
29 // starts the sync machine. Instances of this class delete themselves once
31 class OneClickSigninSyncStarter
: public SigninTracker::Observer
,
32 public chrome::BrowserListObserver
,
33 public content::WebContentsObserver
{
36 // Starts the process of signing the user in with the SigninManager, and
37 // once completed automatically starts sync with all data types enabled.
38 SYNC_WITH_DEFAULT_SETTINGS
,
40 // Starts the process of signing the user in with the SigninManager, and
41 // once completed redirects the user to the settings page to allow them
42 // to configure which data types to sync before sync is enabled.
45 // Starts the process of re-authenticating the user via SigninManager,
46 // and once completed, redirects the user to the settings page, but doesn't
47 // display the configure sync UI.
48 SHOW_SETTINGS_WITHOUT_CONFIGURE
,
50 // The process should be aborted because the undo button has been pressed.
54 enum ConfirmationRequired
{
55 // No need to display a "post-signin" confirmation bubble (for example, if
56 // the user was doing a re-auth flow).
59 // Signin flow redirected outside of trusted domains, so ask the user to
60 // confirm before signing in.
61 CONFIRM_UNTRUSTED_SIGNIN
,
63 // Display a confirmation after signing in.
67 // Result of the sync setup.
68 enum SyncSetupResult
{
73 typedef base::Callback
<void(SyncSetupResult
)> Callback
;
75 // |profile| must not be NULL, however |browser| can be. When using the
76 // OneClickSigninSyncStarter from a browser, provide both.
77 // If |display_confirmation| is true, the user will be prompted to confirm the
78 // signin before signin completes.
79 // |web_contents| is used to show the sync UI if it's showing a blank page
80 // and not about to be closed. It can be NULL.
81 // |callback| is always executed before OneClickSigninSyncStarter is deleted.
83 OneClickSigninSyncStarter(Profile
* profile
,
85 const std::string
& session_index
,
86 const std::string
& email
,
87 const std::string
& password
,
88 const std::string
& oauth_code
,
89 StartSyncMode start_mode
,
90 content::WebContents
* web_contents
,
91 ConfirmationRequired display_confirmation
,
94 // chrome::BrowserListObserver override.
95 virtual void OnBrowserRemoved(Browser
* browser
) OVERRIDE
;
98 friend class OneClickSigninSyncStarterTest
;
99 FRIEND_TEST_ALL_PREFIXES(OneClickSigninSyncStarterTest
,
100 CallbackSigninFailed
);
101 FRIEND_TEST_ALL_PREFIXES(OneClickSigninSyncStarterTest
,
102 CallbackSigninSucceeded
);
103 FRIEND_TEST_ALL_PREFIXES(OneClickSigninSyncStarterTest
,
106 virtual ~OneClickSigninSyncStarter();
108 // Initializes the internals of the OneClickSigninSyncStarter object. Can also
109 // be used to re-initialize the object to refer to a newly created profile.
110 void Initialize(Profile
* profile
, Browser
* browser
);
112 // SigninTracker::Observer override.
113 virtual void SigninFailed(const GoogleServiceAuthError
& error
) OVERRIDE
;
114 virtual void SigninSuccess() OVERRIDE
;
115 virtual void MergeSessionComplete(
116 const GoogleServiceAuthError
& error
) OVERRIDE
;
118 #if defined(ENABLE_CONFIGURATION_POLICY)
119 // User input handler for the signin confirmation dialog.
120 class SigninDialogDelegate
121 : public ui::ProfileSigninConfirmationDelegate
{
123 SigninDialogDelegate(
124 base::WeakPtr
<OneClickSigninSyncStarter
> sync_starter
);
125 virtual ~SigninDialogDelegate();
126 virtual void OnCancelSignin() OVERRIDE
;
127 virtual void OnContinueSignin() OVERRIDE
;
128 virtual void OnSigninWithNewProfile() OVERRIDE
;
130 base::WeakPtr
<OneClickSigninSyncStarter
> sync_starter_
;
132 friend class SigninDialogDelegate
;
134 // Callback invoked once policy registration is complete. If registration
135 // fails, |dm_token| and |client_id| will be empty.
136 void OnRegisteredForPolicy(const std::string
& dm_token
,
137 const std::string
& client_id
);
139 // Callback invoked when a policy fetch request has completed. |success| is
140 // true if policy was successfully fetched.
141 void OnPolicyFetchComplete(bool success
);
143 // Called to create a new profile, which is then signed in with the
144 // in-progress auth credentials currently stored in this object.
145 void CreateNewSignedInProfile();
147 // Helper function that loads policy with the cached |dm_token_| and
148 // |client_id|, then completes the signin process.
149 void LoadPolicyWithCachedCredentials();
151 // Callback invoked once a profile is created, so we can complete the
152 // credentials transfer, load policy, and open the first window.
153 void CompleteInitForNewProfile(chrome::HostDesktopType desktop_type
,
155 Profile::CreateStatus status
);
157 #endif // defined(ENABLE_CONFIGURATION_POLICY)
159 // Cancels the in-progress signin for this profile.
160 void CancelSigninAndDelete();
162 // Callback invoked to check whether the user needs policy or if a
163 // confirmation is required (in which case we have to prompt the user first).
164 void ConfirmSignin(const std::string
& oauth_token
);
166 // Displays confirmation UI to the user if confirmation_required_ ==
167 // CONFIRM_UNTRUSTED_SIGNIN, otherwise completes the pending signin process.
168 void ConfirmAndSignin();
170 // Callback invoked once the user has responded to the signin confirmation UI.
171 // If response == UNDO_SYNC, the signin is cancelled, otherwise the pending
172 // signin is completed.
173 void UntrustedSigninConfirmed(StartSyncMode response
);
175 // GetProfileSyncService returns non-NULL pointer if sync is enabled.
176 // There is a scenario when when ProfileSyncService discovers that sync is
177 // disabled during setup. In this case GetProfileSyncService will return NULL,
178 // but we still need to call PSS::SetSetupInProgress(false). For this purpose
179 // call FinishProfileSyncServiceSetup() function.
180 ProfileSyncService
* GetProfileSyncService();
182 void FinishProfileSyncServiceSetup();
184 // Displays the settings UI and brings up the advanced sync settings
185 // dialog if |configure_sync| is true. The web contents provided to the
186 // constructor is used if it's showing a blank page and not about to be
187 // closed. Otherwise, a new tab or an existing settings tab is used.
188 void ShowSettingsPage(bool configure_sync
);
190 // Displays a settings page in the provided web contents. |sub_page| can be
191 // empty to show the main settings page.
192 void ShowSettingsPageInWebContents(content::WebContents
* contents
,
193 const std::string
& sub_page
);
195 // Shows the post-signin confirmation bubble. If |custom_message| is empty,
196 // the default "You are signed in" message is displayed.
197 void DisplayFinalConfirmationBubble(const base::string16
& custom_message
);
199 // Makes sure browser_ points to a valid browser (opens a new browser if
200 // necessary). Useful in the case where the user has created a new Profile as
201 // part of the signin process.
202 void EnsureBrowser();
206 scoped_ptr
<SigninTracker
> signin_tracker_
;
207 StartSyncMode start_mode_
;
208 chrome::HostDesktopType desktop_type_
;
209 bool force_same_tab_navigation_
;
210 ConfirmationRequired confirmation_required_
;
212 // Callback executed when sync setup succeeds or fails.
213 Callback sync_setup_completed_callback_
;
215 #if defined(ENABLE_CONFIGURATION_POLICY)
216 // Policy credentials we keep while determining whether to create
217 // a new profile for an enterprise user or not.
218 std::string dm_token_
;
219 std::string client_id_
;
222 base::WeakPtrFactory
<OneClickSigninSyncStarter
> weak_pointer_factory_
;
224 DISALLOW_COPY_AND_ASSIGN(OneClickSigninSyncStarter
);
228 #endif // CHROME_BROWSER_UI_SYNC_ONE_CLICK_SIGNIN_SYNC_STARTER_H_