Upstream version 5.34.104.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 LabelButton;
32 }
33
34 class Browser;
35
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 {
45  public:
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,
55                          Browser* browser);
56   static bool IsShowing();
57   static void Hide();
58
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;
64   }
65
66  private:
67   friend class NewAvatarMenuButtonTest;
68   FRIEND_TEST_ALL_PREFIXES(NewAvatarMenuButtonTest, SignOut);
69
70   typedef std::vector<size_t> Indexes;
71   typedef std::map<views::Button*, int> ButtonIndexes;
72   typedef std::map<views::View*, std::string> AccountButtonIndexes;
73
74   // Different views that can be displayed in the bubble.
75   enum BubbleViewMode {
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.
80   };
81
82   ProfileChooserView(views::View* anchor_view,
83                      views::BubbleBorder::Arrow arrow,
84                      const gfx::Rect& anchor_rect,
85                      Browser* browser);
86   virtual ~ProfileChooserView();
87
88   // views::BubbleDelegateView:
89   virtual void Init() OVERRIDE;
90   virtual void WindowClosing() OVERRIDE;
91
92   // views::ButtonListener:
93   virtual void ButtonPressed(views::Button* sender,
94                              const ui::Event& event) OVERRIDE;
95
96   // views::LinkListener:
97   virtual void LinkClicked(views::Link* sender, int event_flags) OVERRIDE;
98
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;
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 either the profile chooser or the account management views.
119   void ShowView(BubbleViewMode view_to_display,
120                 AvatarMenu* avatar_menu);
121
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
124   // links.
125   views::View* CreateCurrentProfileView(
126       const AvatarMenu::Item& avatar_item,
127       bool is_guest);
128   views::View* CreateGuestProfileView();
129   views::View* CreateOtherProfilesView(const Indexes& avatars_to_show);
130   views::View* CreateOptionsView(bool is_guest_view);
131
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,
140                            int width);
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::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_;
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_