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