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 managed user
     61     // (see ManagedUserService).
     62     bool managed;
     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, bool always_create);
     98 
     99   // Creates a new profile.
    100   void AddNewProfile(ProfileMetrics::ProfileAdd type);
    101 
    102   // Opens the profile settings in response to clicking the edit button next to
    103   // an item.
    104   void EditProfile(size_t index);
    105 
    106   // Rebuilds the menu from the cache.
    107   void RebuildMenu();
    108 
    109   // Gets the number of profiles.
    110   size_t GetNumberOfItems() const;
    111 
    112   // Gets the Item at the specified index.
    113   const Item& GetItemAt(size_t index) const;
    114 
    115   // Returns the index of the active profile.
    116   size_t GetActiveProfileIndex();
    117 
    118   // Returns information about a managed user which will be displayed in the
    119   // avatar menu. If the profile does not belong to a managed user, an empty
    120   // string will be returned.
    121   base::string16 GetManagedUserInformation() const;
    122 
    123   // Returns the icon for the managed user which will be displayed in the
    124   // avatar menu.
    125   const gfx::Image& GetManagedUserIcon() const;
    126 
    127   // This menu is also used for the always-present Mac system menubar. If the
    128   // last active browser changes, the menu will need to reference that browser.
    129   void ActiveBrowserChanged(Browser* browser);
    130 
    131   // Returns true if the add profile link should be shown.
    132   bool ShouldShowAddNewProfileLink() const;
    133 
    134   // Returns true if the edit profile link should be shown.
    135   bool ShouldShowEditProfileLink() const;
    136 
    137   // content::NotificationObserver:
    138   virtual void Observe(int type,
    139                        const content::NotificationSource& source,
    140                        const content::NotificationDetails& details) OVERRIDE;
    141 
    142  private:
    143   // The model that provides the list of menu items.
    144   scoped_ptr<ProfileList> profile_list_;
    145 
    146   // The controller for avatar menu actions.
    147   scoped_ptr<AvatarMenuActions> menu_actions_;
    148 
    149   // The cache that provides the profile information. Weak.
    150   ProfileInfoInterface* profile_info_;
    151 
    152   // The observer of this model, which is notified of changes. Weak.
    153   AvatarMenuObserver* observer_;
    154 
    155   // Browser in which this avatar menu resides. Weak.
    156   Browser* browser_;
    157 
    158   // Listens for notifications from the ProfileInfoCache.
    159   content::NotificationRegistrar registrar_;
    160 
    161   DISALLOW_COPY_AND_ASSIGN(AvatarMenu);
    162 };
    163 
    164 #endif  // CHROME_BROWSER_PROFILES_AVATAR_MENU_H_
    165