1// Copyright (c) 2011 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/helper.h"
13#include "chrome/browser/chromeos/login/language_switch_menu.h"
14#include "chrome/browser/chromeos/login/user_input.h"
15#include "views/accelerator.h"
16#include "views/controls/button/button.h"
17#include "views/controls/link.h"
18#include "views/controls/textfield/textfield_controller.h"
19#include "views/view.h"
20
21namespace views {
22class Label;
23class MenuButton;
24class NativeButton;
25}  // namespace views
26
27namespace chromeos {
28
29// View that is used for new user login. It asks for username and password,
30// allows to specify language preferences or initiate new account creation.
31class NewUserView : public ThrobberHostView,
32                    public UserInput,
33                    public views::TextfieldController,
34                    public views::LinkController,
35                    public views::ButtonListener {
36 public:
37  // Delegate class to get notifications from the view.
38  class Delegate {
39  public:
40    virtual ~Delegate() {}
41
42    // User provided |username|, |password| and initiated login.
43    virtual void OnLogin(const std::string& username,
44                         const std::string& password) = 0;
45
46    // Initiates incognito login.
47    virtual void OnLoginAsGuest() = 0;
48
49    // User initiated new account creation.
50    virtual void OnCreateAccount() = 0;
51
52    // User requested enterprise enrollment.
53    virtual void OnStartEnterpriseEnrollment() = 0;
54
55    // User started typing so clear all error messages.
56    virtual void ClearErrors() = 0;
57
58    // User tries to navigate away from NewUserView pod.
59    virtual void NavigateAway() = 0;
60  };
61
62  // If |need_border| is true, RoundedRect border and background are required.
63  NewUserView(Delegate* delegate,
64              bool need_border,
65              bool need_guest_link);
66
67  virtual ~NewUserView();
68
69  // Initialize view layout.
70  void Init();
71
72  // Update strings from the resources. Executed on language change.
73  void UpdateLocalizedStringsAndFonts();
74
75  // Returns bounds of password field in screen coordinates.
76  gfx::Rect GetPasswordBounds() const;
77
78  // Returns bounds of username field in screen coordinates.
79  gfx::Rect GetUsernameBounds() const;
80
81  // views::View:
82  virtual gfx::Size GetPreferredSize();
83  virtual void Layout();
84  virtual void RequestFocus();
85
86  // Setters for textfields.
87  void SetUsername(const std::string& username);
88  void SetPassword(const std::string& password);
89
90  // Attempt to login with the current field values.
91  void Login();
92
93  // views::TextfieldController:
94  // Not thread-safe, by virtue of using SetupSession().
95  virtual bool HandleKeyEvent(views::Textfield* sender,
96                              const views::KeyEvent& keystroke);
97  virtual void ContentsChanged(views::Textfield* sender,
98                               const string16& new_contents);
99
100  // views::ButtonListener:
101  virtual void ButtonPressed(views::Button* sender, const views::Event& event);
102
103  // views::LinkController:
104  virtual void LinkActivated(views::Link* source, int event_flags);
105  virtual bool AcceleratorPressed(const views::Accelerator& accelerator);
106
107  // ThrobberHostView:
108  virtual gfx::Rect CalculateThrobberBounds(views::Throbber* throbber);
109
110  // UserInput:
111  virtual void EnableInputControls(bool enabled);
112  virtual void ClearAndFocusControls();
113  virtual void ClearAndFocusPassword();
114  virtual gfx::Rect GetMainInputScreenBounds() const;
115
116  // Navigates "away" to other user pods if allowed.
117  // Returns true if event has been processed.
118  bool NavigateAway();
119
120 protected:
121  // views::View:
122  virtual void ViewHierarchyChanged(bool is_add,
123                                    views::View *parent,
124                                    views::View *child);
125  virtual void OnLocaleChanged();
126  void AddChildView(View* view);
127
128 private:
129  // Creates Link control and adds it as a child.
130  void InitLink(views::Link** link);
131
132  // Delete and recreate native controls that fail to update preferred size
133  // after text/locale update.
134  void RecreatePeculiarControls();
135
136  // Enable or disable the |sign_in_button_| based on the contents of the
137  // |username_field_| and |password_field_|. If there is text in both the
138  // button is enabled, otherwise it's disabled.
139  void UpdateSignInButtonState();
140
141  // Create view with specified solid background and add it as  child.
142  views::View* CreateSplitter(SkColor color);
143
144  // Screen controls.
145  // NOTE: sign_in_button_ and languages_menubutton_ are handled with
146  // special care: they are recreated on any text/locale change
147  // because they are not resized properly.
148  views::Textfield* username_field_;
149  views::Textfield* password_field_;
150  views::Label* title_label_;
151  views::Label* title_hint_label_;
152  views::View* splitter_up1_;
153  views::View* splitter_up2_;
154  views::View* splitter_down1_;
155  views::View* splitter_down2_;
156  views::NativeButton* sign_in_button_;
157  views::Link* guest_link_;
158  views::Link* create_account_link_;
159  views::MenuButton* languages_menubutton_;
160
161  views::Accelerator accel_focus_pass_;
162  views::Accelerator accel_focus_user_;
163  views::Accelerator accel_enterprise_enrollment_;
164  views::Accelerator accel_login_off_the_record_;
165  views::Accelerator accel_toggle_accessibility_;
166
167  // Notifications receiver.
168  Delegate* delegate_;
169
170  ScopedRunnableMethodFactory<NewUserView> focus_grabber_factory_;
171
172  LanguageSwitchMenu language_switch_menu_;
173
174  // True when login is in process.
175  bool login_in_process_;
176
177  // If true, this view needs RoundedRect border and background.
178  bool need_border_;
179
180  // Whether Guest Mode link is needed.
181  bool need_guest_link_;
182
183  // Whether create account link is needed. Set to false for now but we may
184  // need it back in near future.
185  bool need_create_account_;
186
187  // Ordinal position of controls inside view layout.
188  int languages_menubutton_order_;
189  int sign_in_button_order_;
190
191  DISALLOW_COPY_AND_ASSIGN(NewUserView);
192};
193
194}  // namespace chromeos
195
196#endif  // CHROME_BROWSER_CHROMEOS_LOGIN_NEW_USER_VIEW_H_
197