Home | History | Annotate | Download | only in sync
      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_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"
     20 
     21 class Browser;
     22 class ProfileSyncService;
     23 
     24 namespace content {
     25 class WebContents;
     26 }  // namespace content
     27 
     28 // Waits for successful singin notification from the signin manager and then
     29 // starts the sync machine.  Instances of this class delete themselves once
     30 // the job is done.
     31 class OneClickSigninSyncStarter : public SigninTracker::Observer,
     32                                   public chrome::BrowserListObserver,
     33                                   public content::WebContentsObserver {
     34  public:
     35   enum StartSyncMode {
     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,
     39 
     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.
     43     CONFIGURE_SYNC_FIRST,
     44 
     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,
     49 
     50     // The process should be aborted because the undo button has been pressed.
     51     UNDO_SYNC
     52   };
     53 
     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).
     57     NO_CONFIRMATION,
     58 
     59     // Signin flow redirected outside of trusted domains, so ask the user to
     60     // confirm before signing in.
     61     CONFIRM_UNTRUSTED_SIGNIN,
     62 
     63     // Display a confirmation after signing in.
     64     CONFIRM_AFTER_SIGNIN
     65   };
     66 
     67   // Result of the sync setup.
     68   enum SyncSetupResult {
     69     SYNC_SETUP_SUCCESS,
     70     SYNC_SETUP_FAILURE
     71   };
     72 
     73   typedef base::Callback<void(SyncSetupResult)> Callback;
     74 
     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.
     82   // It can be empty.
     83   OneClickSigninSyncStarter(Profile* profile,
     84                             Browser* browser,
     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,
     92                             Callback callback);
     93 
     94   // chrome::BrowserListObserver override.
     95   virtual void OnBrowserRemoved(Browser* browser) OVERRIDE;
     96 
     97  private:
     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,
    104                            CallbackNull);
    105 
    106   virtual ~OneClickSigninSyncStarter();
    107 
    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);
    111 
    112   // SigninTracker::Observer override.
    113   virtual void SigninFailed(const GoogleServiceAuthError& error) OVERRIDE;
    114   virtual void SigninSuccess() OVERRIDE;
    115 
    116 #if defined(ENABLE_CONFIGURATION_POLICY)
    117   // User input handler for the signin confirmation dialog.
    118   class SigninDialogDelegate
    119     : public ui::ProfileSigninConfirmationDelegate {
    120    public:
    121     SigninDialogDelegate(
    122         base::WeakPtr<OneClickSigninSyncStarter> sync_starter);
    123     virtual ~SigninDialogDelegate();
    124     virtual void OnCancelSignin() OVERRIDE;
    125     virtual void OnContinueSignin() OVERRIDE;
    126     virtual void OnSigninWithNewProfile() OVERRIDE;
    127    private:
    128     base::WeakPtr<OneClickSigninSyncStarter> sync_starter_;
    129   };
    130   friend class SigninDialogDelegate;
    131 
    132   // Callback invoked once policy registration is complete. If registration
    133   // fails, |dm_token| and |client_id| will be empty.
    134   void OnRegisteredForPolicy(const std::string& dm_token,
    135                              const std::string& client_id);
    136 
    137   // Callback invoked when a policy fetch request has completed. |success| is
    138   // true if policy was successfully fetched.
    139   void OnPolicyFetchComplete(bool success);
    140 
    141   // Called to create a new profile, which is then signed in with the
    142   // in-progress auth credentials currently stored in this object.
    143   void CreateNewSignedInProfile();
    144 
    145   // Helper function that loads policy with the cached |dm_token_| and
    146   // |client_id|, then completes the signin process.
    147   void LoadPolicyWithCachedCredentials();
    148 
    149   // Callback invoked once a profile is created, so we can complete the
    150   // credentials transfer, load policy, and open the first window.
    151   void CompleteInitForNewProfile(chrome::HostDesktopType desktop_type,
    152                                  Profile* profile,
    153                                  Profile::CreateStatus status);
    154 
    155 #endif  // defined(ENABLE_CONFIGURATION_POLICY)
    156 
    157   // Cancels the in-progress signin for this profile.
    158   void CancelSigninAndDelete();
    159 
    160   // Callback invoked to check whether the user needs policy or if a
    161   // confirmation is required (in which case we have to prompt the user first).
    162   void ConfirmSignin(const std::string& oauth_token);
    163 
    164   // Displays confirmation UI to the user if confirmation_required_ ==
    165   // CONFIRM_UNTRUSTED_SIGNIN, otherwise completes the pending signin process.
    166   void ConfirmAndSignin();
    167 
    168   // Callback invoked once the user has responded to the signin confirmation UI.
    169   // If response == UNDO_SYNC, the signin is cancelled, otherwise the pending
    170   // signin is completed.
    171   void UntrustedSigninConfirmed(StartSyncMode response);
    172 
    173   // GetProfileSyncService returns non-NULL pointer if sync is enabled.
    174   // There is a scenario when when ProfileSyncService discovers that sync is
    175   // disabled during setup. In this case GetProfileSyncService will return NULL,
    176   // but we still need to call PSS::SetSetupInProgress(false). For this purpose
    177   // call FinishProfileSyncServiceSetup() function.
    178   ProfileSyncService* GetProfileSyncService();
    179 
    180   void FinishProfileSyncServiceSetup();
    181 
    182   // Displays the settings UI and brings up the advanced sync settings
    183   // dialog if |configure_sync| is true. The web contents provided to the
    184   // constructor is used if it's showing a blank page and not about to be
    185   // closed. Otherwise, a new tab or an existing settings tab is used.
    186   void ShowSettingsPage(bool configure_sync);
    187 
    188   // Displays a settings page in the provided web contents. |sub_page| can be
    189   // empty to show the main settings page.
    190   void ShowSettingsPageInWebContents(content::WebContents* contents,
    191                                      const std::string& sub_page);
    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 base::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 
    210   // Callback executed when sync setup succeeds or fails.
    211   Callback sync_setup_completed_callback_;
    212 
    213 #if defined(ENABLE_CONFIGURATION_POLICY)
    214   // Policy credentials we keep while determining whether to create
    215   // a new profile for an enterprise user or not.
    216   std::string dm_token_;
    217   std::string client_id_;
    218 #endif
    219 
    220   base::WeakPtrFactory<OneClickSigninSyncStarter> weak_pointer_factory_;
    221 
    222   DISALLOW_COPY_AND_ASSIGN(OneClickSigninSyncStarter);
    223 };
    224 
    225 
    226 #endif  // CHROME_BROWSER_UI_SYNC_ONE_CLICK_SIGNIN_SYNC_STARTER_H_
    227