1 // Copyright 2013 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.
5 #ifndef CHROME_BROWSER_UI_VIEWS_PROFILE_CHOOSER_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_PROFILE_CHOOSER_VIEW_H_
11 #include "chrome/browser/profiles/avatar_menu.h"
12 #include "chrome/browser/profiles/avatar_menu_observer.h"
13 #include "google_apis/gaia/oauth2_token_service.h"
14 #include "ui/views/bubble/bubble_delegate.h"
15 #include "ui/views/controls/button/button.h"
16 #include "ui/views/controls/button/menu_button_listener.h"
17 #include "ui/views/controls/link_listener.h"
18 #include "ui/views/controls/styled_label_listener.h"
19 #include "ui/views/controls/textfield/textfield_controller.h"
21 class EditableProfilePhoto;
22 class EditableProfileName;
36 // This bubble view is displayed when the user clicks on the avatar button.
37 // It displays a list of profiles and allows users to switch between profiles.
38 class ProfileChooserView : public views::BubbleDelegateView,
39 public views::ButtonListener,
40 public views::LinkListener,
41 public views::MenuButtonListener,
42 public views::TextfieldController,
43 public AvatarMenuObserver,
44 public OAuth2TokenService::Observer {
46 // Shows the bubble if one is not already showing. This allows us to easily
47 // make a button toggle the bubble on and off when clicked: we unconditionally
48 // call this function when the button is clicked and if the bubble isn't
49 // showing it will appear while if it is showing, nothing will happen here and
50 // the existing bubble will auto-close due to focus loss.
51 static void ShowBubble(views::View* anchor_view,
52 views::BubbleBorder::Arrow arrow,
53 views::BubbleBorder::BubbleAlignment border_alignment,
54 const gfx::Rect& anchor_rect,
56 static bool IsShowing();
59 // We normally close the bubble any time it becomes inactive but this can lead
60 // to flaky tests where unexpected UI events are triggering this behavior.
61 // Tests should call this with "false" for more consistent operation.
62 static void clear_close_on_deactivate_for_testing() {
63 close_on_deactivate_for_testing_ = false;
67 friend class NewAvatarMenuButtonTest;
68 FRIEND_TEST_ALL_PREFIXES(NewAvatarMenuButtonTest, SignOut);
70 typedef std::vector<size_t> Indexes;
71 typedef std::map<views::Button*, int> ButtonIndexes;
72 typedef std::map<views::View*, std::string> AccountButtonIndexes;
74 // Different views that can be displayed in the bubble.
76 PROFILE_CHOOSER_VIEW, // Shows a "fast profile switcher" view.
77 ACCOUNT_MANAGEMENT_VIEW, // Shows a list of accounts for the active user.
78 GAIA_SIGNIN_VIEW, // Shows a web view for primary sign in.
79 GAIA_ADD_ACCOUNT_VIEW // Shows a web view for adding secondary accounts.
82 ProfileChooserView(views::View* anchor_view,
83 views::BubbleBorder::Arrow arrow,
84 const gfx::Rect& anchor_rect,
86 virtual ~ProfileChooserView();
88 // views::BubbleDelegateView:
89 virtual void Init() OVERRIDE;
90 virtual void WindowClosing() OVERRIDE;
92 // views::ButtonListener:
93 virtual void ButtonPressed(views::Button* sender,
94 const ui::Event& event) OVERRIDE;
96 // views::LinkListener:
97 virtual void LinkClicked(views::Link* sender, int event_flags) OVERRIDE;
99 // views::MenuButtonListener:
100 virtual void OnMenuButtonClicked(views::View* source,
101 const gfx::Point& point) OVERRIDE;
102 // views::TextfieldController:
103 virtual bool HandleKeyEvent(views::Textfield* sender,
104 const ui::KeyEvent& key_event) OVERRIDE;
106 // AvatarMenuObserver:
107 virtual void OnAvatarMenuChanged(AvatarMenu* avatar_menu) OVERRIDE;
109 // OAuth2TokenService::Observer overrides.
110 virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
111 virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE;
113 static ProfileChooserView* profile_bubble_;
114 static bool close_on_deactivate_for_testing_;
118 // Shows either the profile chooser or the account management views.
119 void ShowView(BubbleViewMode view_to_display,
120 AvatarMenu* avatar_menu);
122 // Creates the main profile card for the profile |avatar_item|. |is_guest|
123 // is used to determine whether to show any Sign in/Sign out/Manage accounts
125 views::View* CreateCurrentProfileView(
126 const AvatarMenu::Item& avatar_item,
128 views::View* CreateGuestProfileView();
129 views::View* CreateOtherProfilesView(const Indexes& avatars_to_show);
130 views::View* CreateOptionsView(bool is_guest_view);
132 // Account Management view for the profile |avatar_item|.
133 views::View* CreateCurrentProfileEditableView(
134 const AvatarMenu::Item& avatar_item);
135 views::View* CreateCurrentProfileAccountsView(
136 const AvatarMenu::Item& avatar_item);
137 void CreateAccountButton(views::GridLayout* layout,
138 const std::string& account,
139 bool is_primary_account,
142 scoped_ptr<AvatarMenu> avatar_menu_;
145 // Other profiles used in the "fast profile switcher" view.
146 ButtonIndexes open_other_profile_indexes_map_;
148 // Accounts associated with the current profile.
149 AccountButtonIndexes current_profile_accounts_map_;
151 // Links displayed in the active profile card.
152 views::Link* manage_accounts_link_;
153 views::Link* signout_current_profile_link_;
154 views::Link* signin_current_profile_link_;
156 // The profile name and photo in the active profile card. Owned by the
158 EditableProfilePhoto* current_profile_photo_;
159 EditableProfileName* current_profile_name_;
162 views::LabelButton* guest_button_;
163 views::LabelButton* end_guest_button_;
164 views::LabelButton* add_user_button_;
165 views::LabelButton* users_button_;
166 views::LabelButton* add_account_button_;
169 BubbleViewMode view_mode_;
171 DISALLOW_COPY_AND_ASSIGN(ProfileChooserView);
174 #endif // CHROME_BROWSER_UI_VIEWS_PROFILE_CHOOSER_VIEW_H_