Home | History | Annotate | Download | only in profiles
      1 // Copyright (c) 2012 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_PROFILES_AVATAR_MENU_H_
      6 #define CHROME_BROWSER_PROFILES_AVATAR_MENU_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/strings/string16.h"
     14 #include "chrome/browser/profiles/profile_metrics.h"
     15 #include "chrome/browser/ui/host_desktop.h"
     16 #include "content/public/browser/notification_observer.h"
     17 #include "content/public/browser/notification_registrar.h"
     18 #include "content/public/browser/web_contents.h"
     19 #include "content/public/browser/web_contents_observer.h"
     20 #include "ui/gfx/image/image.h"
     21 
     22 class AvatarMenuObserver;
     23 class Browser;
     24 class Profile;
     25 class ProfileInfoInterface;
     26 class ProfileList;
     27 class AvatarMenuActions;
     28 
     29 // This class represents the menu-like interface used to select profiles,
     30 // such as the bubble that appears when the avatar icon is clicked in the
     31 // browser window frame. This class will notify its observer when the backend
     32 // data changes, and the view for this model should forward actions
     33 // back to it in response to user events.
     34 class AvatarMenu : public content::NotificationObserver {
     35  public:
     36   // Represents an item in the menu.
     37   struct Item {
     38     Item(size_t menu_index, size_t profile_index, const gfx::Image& icon);
     39     ~Item();
     40 
     41     // The icon to be displayed next to the item.
     42     gfx::Image icon;
     43 
     44     // Whether or not the current browser is using this profile.
     45     bool active;
     46 
     47     // The name of this profile.
     48     base::string16 name;
     49 
     50     // A string representing the sync state of the profile.
     51     base::string16 sync_state;
     52 
     53     // Whether or not the current profile is signed in. If true, |sync_state| is
     54     // expected to be the email of the signed in user.
     55     bool signed_in;
     56 
     57     // Whether or not the current profile requires sign-in before use.
     58     bool signin_required;
     59 
     60     // Whether or not the current profile is a supervised user
     61     // (see ManagedUserService).
     62     bool supervised;
     63 
     64     // The index in the menu of this profile, used by views to refer to
     65     // profiles.
     66     size_t menu_index;
     67 
     68     // The index in the |profile_cache| for this profile.
     69     size_t profile_index;
     70 
     71     // The path of this profile.
     72     base::FilePath profile_path;
     73   };
     74 
     75   // Constructor. |observer| can be NULL. |browser| can be NULL and a new one
     76   // will be created if an action requires it.
     77   AvatarMenu(ProfileInfoInterface* profile_cache,
     78              AvatarMenuObserver* observer,
     79              Browser* browser);
     80   virtual ~AvatarMenu();
     81 
     82   // True if avatar menu should be displayed.
     83   static bool ShouldShowAvatarMenu();
     84 
     85   // Sets |image| to the image corresponding to the given profile, and
     86   // sets |is_rectangle| to true unless |image| is a built-in profile avatar.
     87   static void GetImageForMenuButton(Profile* profile,
     88                                     gfx::Image* image,
     89                                     bool* is_rectangle);
     90 
     91   // Compare items by name.
     92   static bool CompareItems(const Item* item1, const Item* item2);
     93 
     94   // Opens a Browser with the specified profile in response to the user
     95   // selecting an item. If |always_create| is true then a new window is created
     96   // even if a window for that profile already exists.
     97   void SwitchToProfile(size_t index,
     98                        bool always_create,
     99                        ProfileMetrics::ProfileOpen metric);
    100 
    101   // Creates a new profile.
    102   void AddNewProfile(ProfileMetrics::ProfileAdd type);
    103 
    104   // Opens the profile settings in response to clicking the edit button next to
    105   // an item.
    106   void EditProfile(size_t index);
    107 
    108   // Rebuilds the menu from the cache.
    109   void RebuildMenu();
    110 
    111   // Gets the number of profiles.
    112   size_t GetNumberOfItems() const;
    113 
    114   // Gets the Item at the specified index.
    115   const Item& GetItemAt(size_t index) const;
    116 
    117   // Returns the index of the active profile.
    118   size_t GetActiveProfileIndex();
    119 
    120   // Returns information about a supervised user which will be displayed in the
    121   // avatar menu. If the profile does not belong to a supervised user, an empty
    122   // string will be returned.
    123   base::string16 GetSupervisedUserInformation() const;
    124 
    125   // Returns the icon for the supervised user which will be displayed in the
    126   // avatar menu.
    127   const gfx::Image& GetSupervisedUserIcon() const;
    128 
    129   // This menu is also used for the always-present Mac system menubar. If the
    130   // last active browser changes, the menu will need to reference that browser.
    131   void ActiveBrowserChanged(Browser* browser);
    132 
    133   // Returns true if the add profile link should be shown.
    134   bool ShouldShowAddNewProfileLink() const;
    135 
    136   // Returns true if the edit profile link should be shown.
    137   bool ShouldShowEditProfileLink() const;
    138 
    139   // content::NotificationObserver:
    140   virtual void Observe(int type,
    141                        const content::NotificationSource& source,
    142                        const content::NotificationDetails& details) OVERRIDE;
    143 
    144  private:
    145   // The model that provides the list of menu items.
    146   scoped_ptr<ProfileList> profile_list_;
    147 
    148   // The controller for avatar menu actions.
    149   scoped_ptr<AvatarMenuActions> menu_actions_;
    150 
    151   // The cache that provides the profile information. Weak.
    152   ProfileInfoInterface* profile_info_;
    153 
    154   // The observer of this model, which is notified of changes. Weak.
    155   AvatarMenuObserver* observer_;
    156 
    157   // Browser in which this avatar menu resides. Weak.
    158   Browser* browser_;
    159 
    160   // Listens for notifications from the ProfileInfoCache.
    161   content::NotificationRegistrar registrar_;
    162 
    163   DISALLOW_COPY_AND_ASSIGN(AvatarMenu);
    164 };
    165 
    166 #endif  // CHROME_BROWSER_PROFILES_AVATAR_MENU_H_
    167