new_user_view.h revision 201ade2fbba22bfb27ae029f4d23fca6ded109a0
1// Copyright (c) 2010 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_CHROMEOS_LOGIN_NEW_USER_VIEW_H_
6#define CHROME_BROWSER_CHROMEOS_LOGIN_NEW_USER_VIEW_H_
7#pragma once
8
9#include <string>
10
11#include "base/task.h"
12#include "chrome/browser/chromeos/login/language_switch_menu.h"
13#include "views/accelerator.h"
14#include "views/controls/button/button.h"
15#include "views/controls/button/menu_button.h"
16#include "views/controls/link.h"
17#include "views/controls/textfield/textfield.h"
18#include "views/view.h"
19
20namespace views {
21class Label;
22class NativeButton;
23class Throbber;
24}  // namespace views
25
26namespace chromeos {
27
28// View that is used for new user login. It asks for username and password,
29// allows to specify language preferences or initiate new account creation.
30class NewUserView : public views::View,
31                    public views::Textfield::Controller,
32                    public views::LinkController,
33                    public views::ButtonListener {
34 public:
35  // Delegate class to get notifications from the view.
36  class Delegate {
37  public:
38    virtual ~Delegate() {}
39
40    // User provided |username|, |password| and initiated login.
41    virtual void OnLogin(const std::string& username,
42                         const std::string& password) = 0;
43
44    // Initiates off the record (incognito) login.
45    virtual void OnLoginOffTheRecord() = 0;
46
47    // User initiated new account creation.
48    virtual void OnCreateAccount() = 0;
49
50    // Adds start URL that will be opened after login.
51    virtual void AddStartUrl(const GURL& start_url) = 0;
52
53    // User started typing so clear all error messages.
54    virtual void ClearErrors() = 0;
55
56    // User tries to navigate away from NewUserView pod.
57    virtual void NavigateAway() = 0;
58
59    // Enables/disables raw of controls at status area.
60    virtual void SetStatusAreaEnabled(bool enable) = 0;
61  };
62
63  // If |need_border| is true, RoundedRect border and background are required.
64  NewUserView(Delegate* delegate,
65              bool need_border,
66              bool need_guest_link);
67
68  virtual ~NewUserView();
69
70  // Initialize view layout.
71  void Init();
72
73  // Update strings from the resources. Executed on language change.
74  void UpdateLocalizedStrings();
75
76  // Resets password text and sets the enabled state of the password.
77  void ClearAndEnablePassword();
78
79  // Resets password and username text and focuses on username.
80  void ClearAndEnableFields();
81
82  // Starts throbber shown during login.
83  void StartThrobber();
84
85  // Stops throbber shown during login.
86  void StopThrobber();
87
88  // Returns bounds of password field in screen coordinates.
89  gfx::Rect GetPasswordBounds() const;
90
91  // Returns bounds of username field in screen coordinates.
92  gfx::Rect GetUsernameBounds() const;
93
94  // Overridden from views::View:
95  virtual gfx::Size GetPreferredSize();
96  virtual void Layout();
97  virtual void RequestFocus();
98
99  // Setters for textfields.
100  void SetUsername(const std::string& username);
101  void SetPassword(const std::string& password);
102
103  // Attempt to login with the current field values.
104  void Login();
105
106  // Overridden from views::Textfield::Controller
107  // Not thread-safe, by virtue of using SetupSession().
108  virtual bool HandleKeystroke(views::Textfield* sender,
109                               const views::Textfield::Keystroke& keystroke);
110  virtual void ContentsChanged(views::Textfield* sender,
111                               const string16& new_contents);
112
113  // Overridden from views::ButtonListener.
114  virtual void ButtonPressed(views::Button* sender, const views::Event& event);
115
116  // Overridden from views::LinkController.
117  virtual void LinkActivated(views::Link* source, int event_flags);
118
119  virtual bool AcceleratorPressed(const views::Accelerator& accelerator);
120
121 protected:
122  // views::View overrides:
123  virtual void ViewHierarchyChanged(bool is_add,
124                                    views::View *parent,
125                                    views::View *child);
126  virtual void NativeViewHierarchyChanged(bool attached,
127                                          gfx::NativeView native_view,
128                                          views::RootView* root_view);
129  virtual void OnLocaleChanged();
130  void AddChildView(View* view);
131
132 private:
133  // Enables/disables input controls (textfields, buttons).
134  void EnableInputControls(bool enabled);
135  void FocusFirstField();
136
137  // Creates Link control and adds it as a child.
138  void InitLink(views::Link** link);
139
140  // Delete and recreate native controls that fail to update preferred size
141  // after text/locale update.
142  void RecreatePeculiarControls();
143
144  // Enable or disable the |sign_in_button_| based on the contents of the
145  // |username_field_| and |password_field_|. If there is text in both the
146  // button is enabled, otherwise it's disabled.
147  void UpdateSignInButtonState();
148
149  // Create view with specified solid background and add it as  child.
150  views::View* CreateSplitter(SkColor color);
151
152  // Screen controls.
153  // NOTE: sign_in_button_ and languages_menubutton_ are handled with
154  // special care: they are recreated on any text/locale change
155  // because they are not resized properly.
156  views::Textfield* username_field_;
157  views::Textfield* password_field_;
158  views::Label* title_label_;
159  views::Label* title_hint_label_;
160  views::View* splitter_up1_;
161  views::View* splitter_up2_;
162  views::View* splitter_down1_;
163  views::View* splitter_down2_;
164  views::NativeButton* sign_in_button_;
165  views::Link* create_account_link_;
166  views::Link* guest_link_;
167  views::MenuButton* languages_menubutton_;
168  views::Throbber* throbber_;
169
170  views::Accelerator accel_focus_pass_;
171  views::Accelerator accel_focus_user_;
172  views::Accelerator accel_login_off_the_record_;
173  views::Accelerator accel_enable_accessibility_;
174
175  // Notifications receiver.
176  Delegate* delegate_;
177
178  ScopedRunnableMethodFactory<NewUserView> focus_grabber_factory_;
179
180  LanguageSwitchMenu language_switch_menu_;
181
182  // Indicates that this view was created when focus manager was unavailable
183  // (on the hidden tab, for example).
184  bool focus_delayed_;
185
186  // True when login is in process.
187  bool login_in_process_;
188
189  // If true, this view needs RoundedRect border and background.
190  bool need_border_;
191
192  // Whether Guest Mode link is needed.
193  bool need_guest_link_;
194
195  // Whether create account link is needed. Set to false for now but we may
196  // need it back in near future.
197  bool need_create_account_;
198
199  // Ordinal position of controls inside view layout.
200  int languages_menubutton_order_;
201  int sign_in_button_order_;
202
203  FRIEND_TEST_ALL_PREFIXES(LoginScreenTest, IncognitoLogin);
204  friend class LoginScreenTest;
205
206  DISALLOW_COPY_AND_ASSIGN(NewUserView);
207};
208
209}  // namespace chromeos
210
211#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_NEW_USER_VIEW_H_
212