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