user_controller.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
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_USER_CONTROLLER_H_ 6#define CHROME_BROWSER_CHROMEOS_LOGIN_USER_CONTROLLER_H_ 7#pragma once 8 9#include <string> 10 11#include "base/string16.h" 12#include "base/task.h" 13#include "chrome/browser/chromeos/login/new_user_view.h" 14#include "chrome/browser/chromeos/login/user_manager.h" 15#include "chrome/browser/chromeos/login/user_view.h" 16#include "chrome/browser/chromeos/wm_ipc.h" 17#include "chrome/common/notification_observer.h" 18#include "chrome/common/notification_registrar.h" 19#include "testing/gtest/include/gtest/gtest_prod.h" 20#include "views/controls/button/button.h" 21#include "views/controls/textfield/textfield.h" 22#include "views/widget/widget_delegate.h" 23namespace views { 24class WidgetGtk; 25} 26 27namespace chromeos { 28 29class ThrobberManager; 30 31// UserController manages the set of windows needed to login a single existing 32// user or first time login for a new user. ExistingUserController creates 33// the nececessary set of UserControllers. 34class UserController : public views::WidgetDelegate, 35 public NewUserView::Delegate, 36 public NotificationObserver, 37 public UserView::Delegate { 38 public: 39 class Delegate { 40 public: 41 virtual void Login(UserController* source, 42 const string16& password) = 0; 43 virtual void LoginOffTheRecord() = 0; 44 virtual void ClearErrors() = 0; 45 virtual void OnUserSelected(UserController* source) = 0; 46 virtual void ActivateWizard(const std::string& screen_name) = 0; 47 virtual void RemoveUser(UserController* source) = 0; 48 virtual void AddStartUrl(const GURL& start_url) = 0; 49 virtual void SetStatusAreaEnabled(bool enable) = 0; 50 51 // Selects user entry with specified |index|. 52 // Does nothing if current user is already selected. 53 virtual void SelectUser(int index) = 0; 54 protected: 55 virtual ~Delegate() {} 56 }; 57 58 // Creates a UserController representing new user or guest login. 59 UserController(Delegate* delegate, bool is_guest); 60 61 // Creates a UserController for the specified user. 62 UserController(Delegate* delegate, const UserManager::User& user); 63 64 ~UserController(); 65 66 // Initializes the UserController, creating the set of windows/controls. 67 // |index| is the index of this user, and |total_user_count| the total 68 // number of users. 69 void Init(int index, int total_user_count, bool need_browse_without_signin); 70 71 // Update border window parameters to notify window manager about new numbers. 72 // |index| of this user and |total_user_count| of users. 73 void UpdateUserCount(int index, int total_user_count); 74 75 int user_index() const { return user_index_; } 76 bool is_user_selected() const { return is_user_selected_; } 77 bool is_new_user() const { return is_new_user_; } 78 bool is_guest() const { return is_guest_; } 79 80 const UserManager::User& user() const { return user_; } 81 82 // Enables or disables tooltip with user's email. 83 void EnableNameTooltip(bool enable); 84 85 // Called when user view is activated (OnUserSelected). 86 void ClearAndEnableFields(); 87 88 // Called when user view is activated (OnUserSelected). 89 void ClearAndEnablePassword(); 90 91 // Get widget that contains all controls. 92 views::WidgetGtk* controls_window() { 93 return controls_window_; 94 } 95 96 // Returns bounds of the main input field in the screen coordinates (e.g. 97 // these bounds could be used to choose positions for the error bubble). 98 gfx::Rect GetMainInputScreenBounds() const; 99 100 // Starts/Stops throbber. 101 void StartThrobber(); 102 void StopThrobber(); 103 104 // views::WidgetDelegate: 105 virtual void IsActiveChanged(bool active); 106 107 // NotificationObserver implementation. 108 virtual void Observe(NotificationType type, 109 const NotificationSource& source, 110 const NotificationDetails& details); 111 112 // NewUserView::Delegate 113 virtual void OnLogin(const std::string& username, 114 const std::string& password); 115 virtual void OnCreateAccount(); 116 virtual void OnLoginOffTheRecord(); 117 virtual void AddStartUrl(const GURL& start_url) { 118 delegate_->AddStartUrl(start_url); 119 } 120 virtual void ClearErrors(); 121 virtual void NavigateAway(); 122 virtual void SetStatusAreaEnabled(bool enable) { 123 delegate_->SetStatusAreaEnabled(enable); 124 } 125 126 // UserView::Delegate implementation: 127 virtual void OnRemoveUser(); 128 129 // Selects user relative to the current user. 130 void SelectUserRelative(int shift); 131 132 // Padding between the user windows. 133 static const int kPadding; 134 135 // Max size needed when an entry is not selected. 136 static const int kUnselectedSize; 137 static const int kNewUserUnselectedSize; 138 139 private: 140 FRIEND_TEST(UserControllerTest, GetNameTooltip); 141 142 // Performs common setup for login windows. 143 void ConfigureLoginWindow(views::WidgetGtk* window, 144 int index, 145 const gfx::Rect& bounds, 146 chromeos::WmIpcWindowType type, 147 views::View* contents_view); 148 views::WidgetGtk* CreateControlsWindow(int index, 149 int* width, int* height, 150 bool need_guest_link); 151 views::WidgetGtk* CreateImageWindow(int index); 152 views::WidgetGtk* CreateLabelWindow(int index, WmIpcWindowType type); 153 void CreateBorderWindow(int index, 154 int total_user_count, 155 int controls_width, int controls_height); 156 157 // Sets specified image on the image window. If image's size is less than 158 // 75% of window size, image size is preserved to avoid blur. Otherwise, 159 // the image is resized to fit window size precisely. Image view repaints 160 // itself. 161 void SetImage(const SkBitmap& image); 162 163 // Returns tooltip text for user name. 164 std::wstring GetNameTooltip() const; 165 166 // User index within all the users. 167 int user_index_; 168 169 // Is this user selected now? 170 bool is_user_selected_; 171 172 // Is this the new user pod? 173 const bool is_new_user_; 174 175 // Is this the guest pod? 176 const bool is_guest_; 177 178 // Is this user the owner? 179 const bool is_owner_; 180 181 // Should we show tooltips above user image and label to help distinguish 182 // users with the same display name. 183 bool show_name_tooltip_; 184 185 // If is_new_user_ and is_guest_ are false, this is the user being shown. 186 UserManager::User user_; 187 188 Delegate* delegate_; 189 190 // A window is used to represent the individual chunks. 191 views::WidgetGtk* controls_window_; 192 views::WidgetGtk* image_window_; 193 views::WidgetGtk* border_window_; 194 views::WidgetGtk* label_window_; 195 views::WidgetGtk* unselected_label_window_; 196 197 // View that shows user image on image window. 198 UserView* user_view_; 199 200 // Views that show display name of the user. 201 views::Label* label_view_; 202 views::Label* unselected_label_view_; 203 204 // Input controls which are used for username and password. 205 UserInput* user_input_; 206 207 // Throbber host that can show a throbber. 208 ThrobberHostView* throbber_host_; 209 210 NotificationRegistrar registrar_; 211 212 ScopedRunnableMethodFactory<UserController> method_factory_; 213 214 DISALLOW_COPY_AND_ASSIGN(UserController); 215}; 216 217} // namespace chromeos 218 219#endif // CHROME_BROWSER_CHROMEOS_LOGIN_USER_CONTROLLER_H_ 220