profile_chooser_view.h revision 0529e5d033099cbfc42635f6f6183833b09dff6e
1// Copyright 2014 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_VIEWS_PROFILES_PROFILE_CHOOSER_VIEW_H_ 6#define CHROME_BROWSER_UI_VIEWS_PROFILES_PROFILE_CHOOSER_VIEW_H_ 7 8#include <map> 9#include <vector> 10 11#include "chrome/browser/profiles/avatar_menu.h" 12#include "chrome/browser/profiles/avatar_menu_observer.h" 13#include "chrome/browser/ui/browser_window.h" 14#include "google_apis/gaia/oauth2_token_service.h" 15#include "ui/views/bubble/bubble_delegate.h" 16#include "ui/views/controls/button/button.h" 17#include "ui/views/controls/button/menu_button_listener.h" 18#include "ui/views/controls/link_listener.h" 19#include "ui/views/controls/styled_label_listener.h" 20#include "ui/views/controls/textfield/textfield_controller.h" 21 22class EditableProfilePhoto; 23class EditableProfileName; 24 25namespace gfx { 26class Image; 27} 28 29namespace views { 30class GridLayout; 31class ImageButton; 32class Link; 33class LabelButton; 34} 35 36class Browser; 37 38// This bubble view is displayed when the user clicks on the avatar button. 39// It displays a list of profiles and allows users to switch between profiles. 40class ProfileChooserView : public views::BubbleDelegateView, 41 public views::ButtonListener, 42 public views::LinkListener, 43 public views::MenuButtonListener, 44 public views::StyledLabelListener, 45 public views::TextfieldController, 46 public AvatarMenuObserver, 47 public OAuth2TokenService::Observer { 48 public: 49 // Different views that can be displayed in the bubble. 50 enum BubbleViewMode { 51 // Shows a "fast profile switcher" view. 52 BUBBLE_VIEW_MODE_PROFILE_CHOOSER, 53 // Shows a list of accounts for the active user. 54 BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT, 55 // Shows a web view for primary sign in. 56 BUBBLE_VIEW_MODE_GAIA_SIGNIN, 57 // Shows a web view for adding secondary accounts. 58 BUBBLE_VIEW_MODE_GAIA_ADD_ACCOUNT, 59 // Shows a view for confirming account removal. 60 BUBBLE_VIEW_MODE_ACCOUNT_REMOVAL 61 }; 62 63 // Shows the bubble if one is not already showing. This allows us to easily 64 // make a button toggle the bubble on and off when clicked: we unconditionally 65 // call this function when the button is clicked and if the bubble isn't 66 // showing it will appear while if it is showing, nothing will happen here and 67 // the existing bubble will auto-close due to focus loss. 68 static void ShowBubble(BubbleViewMode view_mode, 69 views::View* anchor_view, 70 views::BubbleBorder::Arrow arrow, 71 views::BubbleBorder::BubbleAlignment border_alignment, 72 const gfx::Rect& anchor_rect, 73 Browser* browser); 74 static bool IsShowing(); 75 static void Hide(); 76 77 // We normally close the bubble any time it becomes inactive but this can lead 78 // to flaky tests where unexpected UI events are triggering this behavior. 79 // Tests should call this with "false" for more consistent operation. 80 static void clear_close_on_deactivate_for_testing() { 81 close_on_deactivate_for_testing_ = false; 82 } 83 84 private: 85 friend class NewAvatarMenuButtonTest; 86 FRIEND_TEST_ALL_PREFIXES(NewAvatarMenuButtonTest, SignOut); 87 88 typedef std::vector<size_t> Indexes; 89 typedef std::map<views::Button*, int> ButtonIndexes; 90 typedef std::map<views::View*, std::string> AccountButtonIndexes; 91 92 ProfileChooserView(views::View* anchor_view, 93 views::BubbleBorder::Arrow arrow, 94 const gfx::Rect& anchor_rect, 95 Browser* browser, 96 BubbleViewMode view_mode); 97 virtual ~ProfileChooserView(); 98 99 // views::BubbleDelegateView: 100 virtual void Init() OVERRIDE; 101 virtual void WindowClosing() OVERRIDE; 102 103 // views::ButtonListener: 104 virtual void ButtonPressed(views::Button* sender, 105 const ui::Event& event) OVERRIDE; 106 107 // views::LinkListener: 108 virtual void LinkClicked(views::Link* sender, int event_flags) OVERRIDE; 109 110 // views::MenuButtonListener: 111 virtual void OnMenuButtonClicked(views::View* source, 112 const gfx::Point& point) OVERRIDE; 113 114 // views::StyledLabelListener implementation. 115 virtual void StyledLabelLinkClicked( 116 const gfx::Range& range, int event_flags) OVERRIDE; 117 118 // views::TextfieldController: 119 virtual bool HandleKeyEvent(views::Textfield* sender, 120 const ui::KeyEvent& key_event) OVERRIDE; 121 122 // AvatarMenuObserver: 123 virtual void OnAvatarMenuChanged(AvatarMenu* avatar_menu) OVERRIDE; 124 125 // OAuth2TokenService::Observer overrides. 126 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE; 127 virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE; 128 129 static ProfileChooserView* profile_bubble_; 130 static bool close_on_deactivate_for_testing_; 131 132 void ResetView(); 133 134 // Shows the bubble with the |view_to_display|. 135 void ShowView(BubbleViewMode view_to_display, 136 AvatarMenu* avatar_menu); 137 138 // Creates the profile chooser view. |tutorial_shown| indicates if the "mirror 139 // enabled" tutorial was shown or not in the last active view. 140 views::View* CreateProfileChooserView(AvatarMenu* avatar_menu, 141 bool tutorial_shown); 142 143 // Creates the main profile card for the profile |avatar_item|. |is_guest| 144 // is used to determine whether to show any Sign in/Sign out/Manage accounts 145 // links. 146 views::View* CreateCurrentProfileView( 147 const AvatarMenu::Item& avatar_item, 148 bool is_guest); 149 views::View* CreateGuestProfileView(); 150 views::View* CreateOtherProfilesView(const Indexes& avatars_to_show); 151 views::View* CreateOptionsView(bool enable_lock); 152 153 // Account Management view for the profile |avatar_item|. 154 views::View* CreateCurrentProfileEditableView( 155 const AvatarMenu::Item& avatar_item); 156 views::View* CreateCurrentProfileAccountsView( 157 const AvatarMenu::Item& avatar_item); 158 void CreateAccountButton(views::GridLayout* layout, 159 const std::string& account, 160 bool is_primary_account, 161 int width); 162 163 // Creates a webview showing the gaia signin page. 164 views::View* CreateGaiaSigninView(bool add_secondary_account); 165 166 // Creates a view to confirm account removal for |account_id_to_remove_|. 167 views::View* CreateAccountRemovalView(); 168 169 // Removes the currently selected account and attempts to restart Chrome. 170 void RemoveAccount(); 171 172 // Creates a a tutorial card at the top prompting the user to try out the new 173 // profile management UI. 174 views::View* CreateNewProfileManagementPreviewView(); 175 176 // Creates a tutorial card shown when new profile management preview is 177 // enabled. |current_avatar_item| indicates the current profile. 178 // |tutorial_shown| indicates if the tutorial card is already shown in the 179 // last active view. 180 views::View* CreatePreviewEnabledTutorialView( 181 const AvatarMenu::Item& current_avatar_item, bool tutorial_shown); 182 183 // Creates a tutorial card with the specified |title_text|, |context_text|, 184 // and a bottom row with a right-aligned link using the specified |link_text|, 185 // and a left aligned button using the specified |button_text|. The method 186 // sets |link| to point to the newly created link, and |button| to the newly 187 // created button. 188 views::View* CreateTutorialView( 189 const base::string16& title_text, 190 const base::string16& content_text, 191 const base::string16& link_text, 192 const base::string16& button_text, 193 views::Link** link, 194 views::LabelButton** button); 195 196 scoped_ptr<AvatarMenu> avatar_menu_; 197 Browser* browser_; 198 199 // Other profiles used in the "fast profile switcher" view. 200 ButtonIndexes open_other_profile_indexes_map_; 201 202 // Accounts associated with the current profile. 203 AccountButtonIndexes current_profile_accounts_map_; 204 205 // Links and buttons displayed in the tutorial card. 206 views::Link* tutorial_learn_more_link_; 207 views::LabelButton* tutorial_ok_button_; 208 views::LabelButton* tutorial_enable_new_profile_management_button_; 209 210 // Links displayed in the active profile card. 211 views::Link* manage_accounts_link_; 212 views::Link* signin_current_profile_link_; 213 214 // The profile name and photo in the active profile card. Owned by the 215 // views hierarchy. 216 EditableProfilePhoto* current_profile_photo_; 217 EditableProfileName* current_profile_name_; 218 219 // Action buttons. 220 views::LabelButton* users_button_; 221 views::LabelButton* lock_button_; 222 views::LabelButton* add_account_button_; 223 224 // Buttons displayed in the gaia signin view. 225 views::ImageButton* gaia_signin_cancel_button_; 226 227 // Links and buttons displayed in the account removal view. 228 views::LabelButton* remove_account_and_relaunch_button_; 229 views::ImageButton* account_removal_cancel_button_; 230 231 // Records the account id to remove. 232 std::string account_id_to_remove_; 233 234 // Active view mode. 235 BubbleViewMode view_mode_; 236 237 // Whether the tutorial is currently shown. 238 bool tutorial_showing_; 239 240 DISALLOW_COPY_AND_ASSIGN(ProfileChooserView); 241}; 242 243#endif // CHROME_BROWSER_UI_VIEWS_PROFILES_PROFILE_CHOOSER_VIEW_H_ 244