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_WEBUI_OPTIONS_SYNC_SETUP_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_OPTIONS_SYNC_SETUP_HANDLER_H_
8 #include "base/gtest_prod_util.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/timer/timer.h"
11 #include "chrome/browser/sync/sync_startup_tracker.h"
12 #include "chrome/browser/ui/webui/options/options_ui.h"
13 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
17 class ProfileSyncService;
18 class SigninManagerBase;
24 class SyncSetupHandler : public options::OptionsPageUIHandler,
25 public SyncStartupTracker::Observer,
26 public LoginUIService::LoginUI {
28 // Constructs a new SyncSetupHandler. |profile_manager| may be NULL.
29 explicit SyncSetupHandler(ProfileManager* profile_manager);
30 ~SyncSetupHandler() override;
32 // OptionsPageUIHandler implementation.
33 void GetLocalizedValues(base::DictionaryValue* localized_strings) override;
34 void RegisterMessages() override;
36 // SyncStartupTracker::Observer implementation;
37 void SyncStartupCompleted() override;
38 void SyncStartupFailed() override;
40 // LoginUIService::LoginUI implementation.
41 void FocusUI() override;
42 void CloseUI() override;
44 static void GetStaticLocalizedValues(
45 base::DictionaryValue* localized_strings,
46 content::WebUI* web_ui);
48 // Initializes the sync setup flow and shows the setup UI.
51 // Shows advanced configuration dialog without going through sign in dialog.
52 // Kicks the sync backend if necessary with showing spinner dialog until it
54 void OpenConfigureSync();
56 // Terminates the sync setup flow.
57 void CloseSyncSetup();
60 friend class SyncSetupHandlerTest;
61 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerTest,
62 DisplayConfigureWithBackendDisabledAndCancel);
63 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerTest, HandleSetupUIWhenSyncDisabled);
64 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerTest, SelectCustomEncryption);
65 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerTest, ShowSyncSetupWhenNotSignedIn);
66 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerTest, SuccessfullySetPassphrase);
67 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerTest, TestSyncEverything);
68 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerTest, TestSyncNothing);
69 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerTest, TestSyncAllManually);
70 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerTest, TestPassphraseStillRequired);
71 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerTest, TestSyncIndividualTypes);
72 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerTest, TurnOnEncryptAll);
73 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerTest, TurnOnEncryptAllDisallowed);
74 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerTest, UnsuccessfullySetPassphrase);
75 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerNonCrosTest,
76 UnrecoverableErrorInitializingSync);
77 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerNonCrosTest,
78 GaiaErrorInitializingSync);
79 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerNonCrosTest, HandleCaptcha);
80 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerNonCrosTest, HandleGaiaAuthFailure);
81 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerNonCrosTest,
82 SubmitAuthWithInvalidUsername);
83 FRIEND_TEST_ALL_PREFIXES(SyncSetupHandlerFirstSigninTest, DisplayBasicLogin);
85 bool is_configuring_sync() const { return configuring_sync_; }
87 // Display the configure sync UI. If |show_advanced| is true, skip directly
88 // to the "advanced settings" dialog, otherwise give the user the simpler
89 // "Sync Everything" dialog. Overridden by subclasses to allow them to skip
90 // the sync setup dialog if desired.
91 // If |passphrase_failed| is true, then the user previously tried to enter an
92 // invalid passphrase.
93 virtual void DisplayConfigureSync(bool show_advanced, bool passphrase_failed);
95 // Called when configuring sync is done to close the dialog and start syncing.
96 void ConfigureSyncDone();
98 // Helper routine that gets the ProfileSyncService associated with the parent
100 ProfileSyncService* GetSyncService() const;
102 // Returns the LoginUIService for the parent profile.
103 LoginUIService* GetLoginUIService() const;
106 // Callbacks from the page.
107 void OnDidClosePage(const base::ListValue* args);
108 void HandleConfigure(const base::ListValue* args);
109 void HandlePassphraseEntry(const base::ListValue* args);
110 void HandlePassphraseCancel(const base::ListValue* args);
111 void HandleShowSetupUI(const base::ListValue* args);
112 void HandleDoSignOutOnAuthError(const base::ListValue* args);
113 void HandleStartSignin(const base::ListValue* args);
114 void HandleStopSyncing(const base::ListValue* args);
115 void HandleCloseTimeout(const base::ListValue* args);
116 #if !defined(OS_CHROMEOS)
117 // Displays the GAIA login form.
118 void DisplayGaiaLogin();
120 // When web-flow is enabled, displays the Gaia login form in a new tab.
121 // This function is virtual so that tests can override.
122 virtual void DisplayGaiaLoginInNewTabOrWindow();
125 // Helper routine that gets the Profile associated with this object (virtual
126 // so tests can override).
127 virtual Profile* GetProfile() const;
129 // A utility function to call before actually showing setup dialog. Makes sure
130 // that a new dialog can be shown and sets flag that setup is in progress.
131 bool PrepareSyncSetup();
133 // Displays spinner-only UI indicating that something is going on in the
135 // TODO(kochi): better to show some message that the user can understand what
136 // is running in the background.
137 void DisplaySpinner();
139 // Displays an error dialog which shows timeout of starting the sync backend.
140 void DisplayTimeout();
142 // Returns true if this object is the active login object.
143 bool IsActiveLogin() const;
145 // If a wizard already exists, return true. Otherwise, return false.
146 bool IsExistingWizardPresent();
148 // If a wizard already exists, focus it and return true.
149 bool FocusExistingWizardIfPresent();
151 // Helper object used to wait for the sync backend to startup.
152 scoped_ptr<SyncStartupTracker> sync_startup_tracker_;
154 // Set to true whenever the sync configure UI is visible. This is used to tell
155 // what stage of the setup wizard the user was in and to update the UMA
156 // histograms in the case that the user cancels out.
157 bool configuring_sync_;
159 // Weak reference to the profile manager.
160 ProfileManager* const profile_manager_;
162 // The OneShotTimer object used to timeout of starting the sync backend
164 scoped_ptr<base::OneShotTimer<SyncSetupHandler> > backend_start_timer_;
166 DISALLOW_COPY_AND_ASSIGN(SyncSetupHandler);
169 #endif // CHROME_BROWSER_UI_WEBUI_OPTIONS_SYNC_SETUP_HANDLER_H_