Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / profile_chooser_view.h
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.
4
5 #ifndef CHROME_BROWSER_UI_VIEWS_PROFILE_CHOOSER_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_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 "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"
20
21 class EditableProfilePhoto;
22 class EditableProfileName;
23
24 namespace gfx {
25 class Image;
26 }
27
28 namespace views {
29 class GridLayout;
30 class Link;
31 class TextButton;
32 class LabelButton;
33 }
34
35 class Browser;
36
37 // This bubble view is displayed when the user clicks on the avatar button.
38 // It displays a list of profiles and allows users to switch between profiles.
39 class ProfileChooserView : public views::BubbleDelegateView,
40                            public views::ButtonListener,
41                            public views::LinkListener,
42                            public views::MenuButtonListener,
43                            public views::TextfieldController,
44                            public AvatarMenuObserver,
45                            public OAuth2TokenService::Observer {
46  public:
47   // Shows the bubble if one is not already showing.  This allows us to easily
48   // make a button toggle the bubble on and off when clicked: we unconditionally
49   // call this function when the button is clicked and if the bubble isn't
50   // showing it will appear while if it is showing, nothing will happen here and
51   // the existing bubble will auto-close due to focus loss.
52   static void ShowBubble(views::View* anchor_view,
53                          views::BubbleBorder::Arrow arrow,
54                          views::BubbleBorder::BubbleAlignment border_alignment,
55                          const gfx::Rect& anchor_rect,
56                          Browser* browser);
57   static bool IsShowing();
58   static void Hide();
59
60   // We normally close the bubble any time it becomes inactive but this can lead
61   // to flaky tests where unexpected UI events are triggering this behavior.
62   // Tests should call this with "false" for more consistent operation.
63   static void clear_close_on_deactivate_for_testing() {
64     close_on_deactivate_for_testing_ = false;
65   }
66
67  private:
68   friend class NewAvatarMenuButtonTest;
69   FRIEND_TEST_ALL_PREFIXES(NewAvatarMenuButtonTest, SignOut);
70
71   typedef std::vector<size_t> Indexes;
72   typedef std::map<views::Button*, int> ButtonIndexes;
73   typedef std::map<views::View*, std::string> AccountButtonIndexes;
74
75   // Different views that can be displayed in the bubble.
76   enum BubbleViewMode {
77     PROFILE_CHOOSER_VIEW,     // Shows a "fast profile switcher" view.
78     ACCOUNT_MANAGEMENT_VIEW,  // Shows a list of accounts for the active user.
79     GAIA_SIGNIN_VIEW,         // Shows a web view for primary sign in.
80     GAIA_ADD_ACCOUNT_VIEW     // Shows a web view for adding secondary accounts.
81   };
82
83   ProfileChooserView(views::View* anchor_view,
84                      views::BubbleBorder::Arrow arrow,
85                      const gfx::Rect& anchor_rect,
86                      Browser* browser);
87   virtual ~ProfileChooserView();
88
89   // views::BubbleDelegateView:
90   virtual void Init() OVERRIDE;
91   virtual void WindowClosing() OVERRIDE;
92
93   // views::ButtonListener:
94   virtual void ButtonPressed(views::Button* sender,
95                              const ui::Event& event) OVERRIDE;
96
97   // views::LinkListener:
98   virtual void LinkClicked(views::Link* sender, int event_flags) OVERRIDE;
99
100   // views::MenuButtonListener:
101   virtual void OnMenuButtonClicked(views::View* source,
102                                    const gfx::Point& point) OVERRIDE;
103   // views::TextfieldController:
104   virtual bool HandleKeyEvent(views::Textfield* sender,
105                               const ui::KeyEvent& key_event) OVERRIDE;
106
107   // AvatarMenuObserver:
108   virtual void OnAvatarMenuChanged(AvatarMenu* avatar_menu) OVERRIDE;
109
110   // OAuth2TokenService::Observer overrides.
111   virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
112   virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE;
113
114   static ProfileChooserView* profile_bubble_;
115   static bool close_on_deactivate_for_testing_;
116
117   void ResetView();
118
119   // Shows either the profile chooser or the account management views.
120   void ShowView(BubbleViewMode view_to_display,
121                 AvatarMenu* avatar_menu);
122
123   // Creates the main profile card for the profile |avatar_item|. |is_guest|
124   // is used to determine whether to show any Sign in/Sign out/Manage accounts
125   // links.
126   views::View* CreateCurrentProfileView(
127       const AvatarMenu::Item& avatar_item,
128       bool is_guest);
129   views::View* CreateGuestProfileView();
130   views::View* CreateOtherProfilesView(const Indexes& avatars_to_show);
131   views::View* CreateOptionsView(bool is_guest_view);
132
133   // Account Management view for the profile |avatar_item|.
134   views::View* CreateCurrentProfileEditableView(
135       const AvatarMenu::Item& avatar_item);
136   views::View* CreateCurrentProfileAccountsView(
137       const AvatarMenu::Item& avatar_item);
138   void CreateAccountButton(views::GridLayout* layout,
139                            const std::string& account,
140                            bool is_primary_account);
141
142   scoped_ptr<AvatarMenu> avatar_menu_;
143   Browser* browser_;
144
145   // Other profiles used in the "fast profile switcher" view.
146   ButtonIndexes open_other_profile_indexes_map_;
147
148   // Accounts associated with the current profile.
149   AccountButtonIndexes current_profile_accounts_map_;
150
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_;
155
156   // The profile name and photo in the active profile card. Owned by the
157   // views hierarchy.
158   EditableProfilePhoto* current_profile_photo_;
159   EditableProfileName* current_profile_name_;
160
161   // Action buttons.
162   views::TextButton* guest_button_;
163   views::TextButton* end_guest_button_;
164   views::TextButton* add_user_button_;
165   views::TextButton* users_button_;
166   views::LabelButton* add_account_button_;
167
168   // Active view mode.
169   BubbleViewMode view_mode_;
170
171   DISALLOW_COPY_AND_ASSIGN(ProfileChooserView);
172 };
173
174 #endif  // CHROME_BROWSER_UI_VIEWS_PROFILE_CHOOSER_VIEW_H_