Home | History | Annotate | Download | only in profiles
      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 
     23 class EditableProfilePhoto;
     24 class EditableProfileName;
     25 
     26 namespace gfx {
     27 class Image;
     28 }
     29 
     30 namespace views {
     31 class GridLayout;
     32 class ImageButton;
     33 class Link;
     34 class LabelButton;
     35 }
     36 
     37 class 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.
     41 class 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(
     55       profiles::BubbleViewMode view_mode,
     56       const signin::ManageAccountsParams& manage_accounts_params,
     57       views::View* anchor_view,
     58       views::BubbleBorder::Arrow arrow,
     59       views::BubbleBorder::BubbleAlignment border_alignment,
     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                      Browser* browser,
     82                      profiles::BubbleViewMode view_mode,
     83                      signin::GAIAServiceType service_type);
     84   virtual ~ProfileChooserView();
     85 
     86   // views::BubbleDelegateView:
     87   virtual void Init() OVERRIDE;
     88   virtual void WindowClosing() OVERRIDE;
     89 
     90   // views::ButtonListener:
     91   virtual void ButtonPressed(views::Button* sender,
     92                              const ui::Event& event) OVERRIDE;
     93 
     94   // views::LinkListener:
     95   virtual void LinkClicked(views::Link* sender, int event_flags) OVERRIDE;
     96 
     97   // views::StyledLabelListener implementation.
     98   virtual void StyledLabelLinkClicked(
     99       const gfx::Range& range, int event_flags) OVERRIDE;
    100 
    101   // views::TextfieldController:
    102   virtual bool HandleKeyEvent(views::Textfield* sender,
    103                               const ui::KeyEvent& key_event) OVERRIDE;
    104 
    105   // AvatarMenuObserver:
    106   virtual void OnAvatarMenuChanged(AvatarMenu* avatar_menu) OVERRIDE;
    107 
    108   // OAuth2TokenService::Observer overrides.
    109   virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
    110   virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE;
    111 
    112   static ProfileChooserView* profile_bubble_;
    113   static bool close_on_deactivate_for_testing_;
    114 
    115   void ResetView();
    116 
    117   // Shows the bubble with the |view_to_display|.
    118   void ShowView(profiles::BubbleViewMode view_to_display,
    119                 AvatarMenu* avatar_menu);
    120 
    121   // Creates the profile chooser view. |tutorial_shown| indicates if the "mirror
    122   // enabled" tutorial was shown or not in the last active view.
    123   views::View* CreateProfileChooserView(AvatarMenu* avatar_menu,
    124       profiles::TutorialMode last_tutorial_mode);
    125 
    126   // Creates the main profile card for the profile |avatar_item|. |is_guest|
    127   // is used to determine whether to show any Sign in/Sign out/Manage accounts
    128   // links.
    129   views::View* CreateCurrentProfileView(
    130       const AvatarMenu::Item& avatar_item,
    131       bool is_guest);
    132   views::View* CreateGuestProfileView();
    133   views::View* CreateOtherProfilesView(const Indexes& avatars_to_show);
    134   views::View* CreateOptionsView(bool enable_lock);
    135   views::View* CreateSupervisedUserDisclaimerView();
    136 
    137   // Account Management view for the profile |avatar_item|.
    138   views::View* CreateCurrentProfileAccountsView(
    139       const AvatarMenu::Item& avatar_item);
    140   void CreateAccountButton(views::GridLayout* layout,
    141                            const std::string& account,
    142                            bool is_primary_account,
    143                            bool reauth_required,
    144                            int width);
    145 
    146   // Creates a webview showing the gaia signin page.
    147   views::View* CreateGaiaSigninView();
    148 
    149   // Creates a view to confirm account removal for |account_id_to_remove_|.
    150   views::View* CreateAccountRemovalView();
    151 
    152   // Removes the currently selected account and attempts to restart Chrome.
    153   void RemoveAccount();
    154 
    155   // Creates a a tutorial card at the top prompting the user to try out the new
    156   // profile management UI.
    157   views::View* CreateNewProfileManagementPreviewView();
    158 
    159   // Creates a tutorial card shown when new profile management preview is
    160   // enabled. |current_avatar_item| indicates the current profile.
    161   // |tutorial_shown| indicates if the tutorial card is already shown in the
    162   // last active view.
    163   views::View* CreatePreviewEnabledTutorialView(
    164       const AvatarMenu::Item& current_avatar_item, bool tutorial_shown);
    165 
    166   // Creates a a tutorial card at the top prompting the user to send feedback
    167   // about the new profile management preview and/or to end preview.
    168   views::View* CreateSendPreviewFeedbackView();
    169 
    170   // Creates a tutorial card with the specified |title_text|, |context_text|,
    171   // and a bottom row with a right-aligned link using the specified |link_text|,
    172   // and a left aligned button using the specified |button_text|. The method
    173   // sets |link| to point to the newly created link, |button| to the newly
    174   // created button, and |tutorial_mode_| to the given |tutorial_mode|.
    175   views::View* CreateTutorialView(
    176       profiles::TutorialMode tutorial_mode,
    177       const base::string16& title_text,
    178       const base::string16& content_text,
    179       const base::string16& link_text,
    180       const base::string16& button_text,
    181       views::Link** link,
    182       views::LabelButton** button);
    183 
    184   views::View* CreateEndPreviewView();
    185 
    186   // Clean-up done after an action was performed in the ProfileChooser.
    187   void PostActionPerformed(ProfileMetrics::ProfileDesktopMenu action_performed);
    188 
    189   scoped_ptr<AvatarMenu> avatar_menu_;
    190   Browser* browser_;
    191 
    192   // Other profiles used in the "fast profile switcher" view.
    193   ButtonIndexes open_other_profile_indexes_map_;
    194 
    195   // Buttons associated with the current profile.
    196   AccountButtonIndexes delete_account_button_map_;
    197   AccountButtonIndexes reauth_account_button_map_;
    198 
    199   // Links and buttons displayed in the tutorial card.
    200   views::Link* tutorial_learn_more_link_;
    201   views::LabelButton* tutorial_ok_button_;
    202   views::LabelButton* tutorial_enable_new_profile_management_button_;
    203   views::Link* tutorial_end_preview_link_;
    204   views::LabelButton* tutorial_send_feedback_button_;
    205 
    206   // Links and buttons displayed in the active profile card.
    207   views::Link* manage_accounts_link_;
    208   views::LabelButton* signin_current_profile_link_;
    209   views::ImageButton* question_mark_button_;
    210 
    211   // The profile name and photo in the active profile card. Owned by the
    212   // views hierarchy.
    213   EditableProfilePhoto* current_profile_photo_;
    214   EditableProfileName* current_profile_name_;
    215 
    216   // Action buttons.
    217   views::LabelButton* users_button_;
    218   views::LabelButton* lock_button_;
    219   views::Link* add_account_link_;
    220 
    221   // Buttons displayed in the gaia signin view.
    222   views::ImageButton* gaia_signin_cancel_button_;
    223 
    224   // Links and buttons displayed in the account removal view.
    225   views::LabelButton* remove_account_button_;
    226   views::ImageButton* account_removal_cancel_button_;
    227 
    228   // Links and buttons displayed in the end-preview view.
    229   views::LabelButton* end_preview_and_relaunch_button_;
    230   views::ImageButton* end_preview_cancel_button_;
    231 
    232   // Records the account id to remove.
    233   std::string account_id_to_remove_;
    234 
    235   // Active view mode.
    236   profiles::BubbleViewMode view_mode_;
    237 
    238   // The current tutorial mode.
    239   profiles::TutorialMode tutorial_mode_;
    240 
    241   // The GAIA service type provided in the response header.
    242   signin::GAIAServiceType gaia_service_type_;
    243 
    244   DISALLOW_COPY_AND_ASSIGN(ProfileChooserView);
    245 };
    246 
    247 #endif  // CHROME_BROWSER_UI_VIEWS_PROFILES_PROFILE_CHOOSER_VIEW_H_
    248