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