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