Fix race condition between account picker and GAIA sign-in screen
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / gaia_screen_handler.h
bloba7f08836c45b19b3a9de7d9b2331ec52e919c8d8
1 // Copyright 2013 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_CHROMEOS_LOGIN_GAIA_SCREEN_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_GAIA_SCREEN_HANDLER_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/command_line.h"
12 #include "base/memory/ref_counted.h"
13 #include "chrome/browser/chromeos/login/screens/core_oobe_actor.h"
14 #include "chrome/browser/ui/webui/chromeos/login/network_state_informer.h"
15 #include "net/base/net_errors.h"
17 namespace policy {
18 class ConsumerManagementService;
21 namespace chromeos {
23 class SigninScreenHandler;
25 // A class that's used to specify the way how Gaia should be loaded.
26 struct GaiaContext {
27 GaiaContext();
29 // Forces Gaia to reload.
30 bool force_reload;
32 // Whether local verison of Gaia is used.
33 bool is_local;
35 // True if password was changed for the current user.
36 bool password_changed;
38 // True if user pods can be displyed.
39 bool show_users;
41 // Whether Gaia should be loaded in offline mode.
42 bool use_offline;
44 // True if user list is non-empty.
45 bool has_users;
47 // Email of current user.
48 std::string email;
50 // Whether consumer management enrollment is in progress.
51 bool is_enrolling_consumer_management;
53 // True if embedded_signin is enabled.
54 bool embedded_signin_enabled;
57 // A class that handles WebUI hooks in Gaia screen.
58 class GaiaScreenHandler : public BaseScreenHandler {
59 public:
60 enum FrameState {
61 FRAME_STATE_UNKNOWN = 0,
62 FRAME_STATE_LOADING,
63 FRAME_STATE_LOADED,
64 FRAME_STATE_ERROR
67 GaiaScreenHandler(
68 CoreOobeActor* core_oobe_actor,
69 const scoped_refptr<NetworkStateInformer>& network_state_informer,
70 policy::ConsumerManagementService* consumer_management);
71 virtual ~GaiaScreenHandler();
73 void LoadGaia(const GaiaContext& context);
74 void UpdateGaia(const GaiaContext& context);
76 // Sends request to reload Gaia. If |force_reload| is true, request
77 // will be sent in any case, otherwise it will be sent only when Gaia is
78 // not loading right now.
79 void ReloadGaia(bool force_reload);
81 // Reload gaia with embedded signin frame.
82 void SwitchToEmbeddedSignin();
84 // Cancel embedded signin for the next load.
85 void CancelEmbeddedSignin();
87 FrameState frame_state() const { return frame_state_; }
88 net::Error frame_error() const { return frame_error_; }
90 private:
91 // TODO (ygorshenin@): remove this dependency.
92 friend class SigninScreenHandler;
94 // BaseScreenHandler implementation:
95 virtual void DeclareLocalizedValues(LocalizedValuesBuilder* builder) override;
96 virtual void Initialize() override;
98 // WebUIMessageHandler implementation:
99 virtual void RegisterMessages() override;
101 // WebUI message handlers.
102 void HandleFrameLoadingCompleted(int status);
103 void HandleCompleteAuthentication(const std::string& gaia_id,
104 const std::string& email,
105 const std::string& password,
106 const std::string& auth_code);
107 void HandleCompleteLogin(const std::string& gaia_id,
108 const std::string& typed_email,
109 const std::string& password,
110 bool using_saml);
112 void HandleUsingSAMLAPI();
113 void HandleScrapedPasswordCount(int password_count);
114 void HandleScrapedPasswordVerificationFailed();
116 void HandleGaiaUIReady();
118 // This is called when ConsumerManagementService::SetOwner() returns.
119 void OnSetOwnerDone(const std::string& gaia_id,
120 const std::string& typed_email,
121 const std::string& password,
122 bool using_saml,
123 bool success);
125 // Really handles the complete login message.
126 void DoCompleteLogin(const std::string& gaia_id,
127 const std::string& typed_email,
128 const std::string& password,
129 bool using_saml);
131 // Fill GAIA user name.
132 void PopulateEmail(const std::string& user_id);
134 // Mark user as having password changed:
135 void PasswordChangedFor(const std::string& user_id);
137 // Kick off cookie / local storage cleanup.
138 void StartClearingCookies(const base::Closure& on_clear_callback);
139 void OnCookiesCleared(const base::Closure& on_clear_callback);
141 // Kick off DNS cache flushing.
142 void StartClearingDnsCache();
143 void OnDnsCleared();
145 // Show sign-in screen for the given credentials.
146 virtual void ShowSigninScreenForCreds(const std::string& username,
147 const std::string& password);
148 // Attempts login for test.
149 void SubmitLoginFormForTest();
151 // Updates the member variable and UMA histogram indicating whether the
152 // principals API was used during SAML login.
153 void SetSAMLPrincipalsAPIUsed(bool api_used);
155 // Show the sign-in screen. Depending on internal state, the screen will
156 // either be shown immediately or after an asynchronous clean-up process that
157 // cleans DNS cache and cookies. In the latter case, the request to show the
158 // screen can be canceled by calling CancelShowGaiaAsync() while the clean-up
159 // is in progress.
160 void ShowGaiaAsync(bool is_enrolling_consumer_management);
162 // Cancels the request to show the sign-in screen while the asynchronous
163 // clean-up process that precedes the screen showing is in progress.
164 void CancelShowGaiaAsync();
166 // Shows signin screen after dns cache and cookie cleanup operations finish.
167 void ShowGaiaScreenIfReady();
169 // Decides whether an auth extension should be pre-loaded. If it should,
170 // pre-loads it.
171 void MaybePreloadAuthExtension();
173 // Tells webui to load authentication extension. |force| is used to force the
174 // extension reloading, if it has already been loaded. |silent_load| is true
175 // for cases when extension should be loaded in the background and it
176 // shouldn't grab the focus. |offline| is true when offline version of the
177 // extension should be used.
178 void LoadAuthExtension(bool force, bool silent_load, bool offline);
180 // TODO (ygorshenin@): GaiaScreenHandler should implement
181 // NetworkStateInformer::Observer.
182 void UpdateState(ErrorScreenActor::ErrorReason reason);
184 // TODO (ygorshenin@): remove this dependency.
185 void SetSigninScreenHandler(SigninScreenHandler* handler);
187 SigninScreenHandlerDelegate* Delegate();
189 // Current state of Gaia frame.
190 FrameState frame_state_;
192 // Latest Gaia frame error.
193 net::Error frame_error_;
195 // Network state informer used to keep signin screen up.
196 scoped_refptr<NetworkStateInformer> network_state_informer_;
198 // Consumer management service for checking if enrollment is in progress.
199 policy::ConsumerManagementService* consumer_management_;
201 CoreOobeActor* core_oobe_actor_;
203 // Email to pre-populate with.
204 std::string populated_email_;
206 // Emails of the users, whose passwords have recently been changed.
207 std::set<std::string> password_changed_for_;
209 // True if dns cache cleanup is done.
210 bool dns_cleared_;
212 // True if DNS cache task is already running.
213 bool dns_clear_task_running_;
215 // True if cookie jar cleanup is done.
216 bool cookies_cleared_;
218 // If true, the sign-in screen will be shown when DNS cache and cookie
219 // clean-up finish.
220 bool show_when_dns_and_cookies_cleared_;
222 // Is focus still stolen from Gaia page?
223 bool focus_stolen_;
225 // Has Gaia page silent load been started for the current sign-in attempt?
226 bool gaia_silent_load_;
228 // The active network at the moment when Gaia page was preloaded.
229 std::string gaia_silent_load_network_;
231 // If the user authenticated via SAML, this indicates whether the principals
232 // API was used.
233 bool using_saml_api_;
235 // Whether consumer management enrollment is in progress.
236 bool is_enrolling_consumer_management_;
238 // Test credentials.
239 std::string test_user_;
240 std::string test_pass_;
241 bool test_expects_complete_login_;
243 // True if user pressed shortcut to enable embedded signin.
244 bool embedded_signin_enabled_by_shortcut_;
246 // Non-owning ptr to SigninScreenHandler instance. Should not be used
247 // in dtor.
248 // TODO (ygorshenin@): GaiaScreenHandler shouldn't communicate with
249 // signin_screen_handler directly.
250 SigninScreenHandler* signin_screen_handler_;
252 base::WeakPtrFactory<GaiaScreenHandler> weak_factory_;
254 DISALLOW_COPY_AND_ASSIGN(GaiaScreenHandler);
257 } // namespace chromeos
259 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_GAIA_SCREEN_HANDLER_H_