Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / options / sync_setup_handler.h
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_OPTIONS_SYNC_SETUP_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_OPTIONS_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
15 class LoginUIService;
16 class ProfileManager;
17 class ProfileSyncService;
18 class SigninManagerBase;
19
20 namespace content {
21 class WebContents;
22 }
23
24 class 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   ~SyncSetupHandler() override;
31
32   // OptionsPageUIHandler implementation.
33   void GetLocalizedValues(base::DictionaryValue* localized_strings) override;
34   void RegisterMessages() override;
35
36   // SyncStartupTracker::Observer implementation;
37   void SyncStartupCompleted() override;
38   void SyncStartupFailed() override;
39
40   // LoginUIService::LoginUI implementation.
41   void FocusUI() override;
42   void CloseUI() override;
43
44   static void GetStaticLocalizedValues(
45       base::DictionaryValue* localized_strings,
46       content::WebUI* web_ui);
47
48   // Initializes the sync setup flow and shows the setup UI.
49   void OpenSyncSetup();
50
51   // Shows advanced configuration dialog without going through sign in dialog.
52   // Kicks the sync backend if necessary with showing spinner dialog until it
53   // gets ready.
54   void OpenConfigureSync();
55
56   // Terminates the sync setup flow.
57   void CloseSyncSetup();
58
59  protected:
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);
84
85   bool is_configuring_sync() const { return configuring_sync_; }
86
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);
94
95   // Called when configuring sync is done to close the dialog and start syncing.
96   void ConfigureSyncDone();
97
98   // Helper routine that gets the ProfileSyncService associated with the parent
99   // profile.
100   ProfileSyncService* GetSyncService() const;
101
102   // Returns the LoginUIService for the parent profile.
103   LoginUIService* GetLoginUIService() const;
104
105  private:
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();
119
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();
123 #endif
124
125   // Helper routine that gets the Profile associated with this object (virtual
126   // so tests can override).
127   virtual Profile* GetProfile() const;
128
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();
132
133   // Displays spinner-only UI indicating that something is going on in the
134   // background.
135   // TODO(kochi): better to show some message that the user can understand what
136   // is running in the background.
137   void DisplaySpinner();
138
139   // Displays an error dialog which shows timeout of starting the sync backend.
140   void DisplayTimeout();
141
142   // Returns true if this object is the active login object.
143   bool IsActiveLogin() const;
144
145   // If a wizard already exists, return true. Otherwise, return false.
146   bool IsExistingWizardPresent();
147
148   // If a wizard already exists, focus it and return true.
149   bool FocusExistingWizardIfPresent();
150
151   // Helper object used to wait for the sync backend to startup.
152   scoped_ptr<SyncStartupTracker> sync_startup_tracker_;
153
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_;
158
159   // Weak reference to the profile manager.
160   ProfileManager* const profile_manager_;
161
162   // The OneShotTimer object used to timeout of starting the sync backend
163   // service.
164   scoped_ptr<base::OneShotTimer<SyncSetupHandler> > backend_start_timer_;
165
166   DISALLOW_COPY_AND_ASSIGN(SyncSetupHandler);
167 };
168
169 #endif  // CHROME_BROWSER_UI_WEBUI_OPTIONS_SYNC_SETUP_HANDLER_H_