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.
5 #import "chrome/browser/ui/cocoa/profiles/profile_menu_controller.h"
7 #include "base/mac/scoped_nsobject.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profiles/avatar_menu.h"
11 #include "chrome/browser/profiles/avatar_menu_observer.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
14 #include "chrome/browser/profiles/profile_info_cache.h"
15 #include "chrome/browser/profiles/profile_info_interface.h"
16 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/browser/profiles/profile_metrics.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/browser_list.h"
20 #include "chrome/browser/ui/browser_list_observer.h"
21 #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "components/signin/core/common/profile_management_switches.h"
24 #include "ui/base/l10n/l10n_util_mac.h"
25 #include "ui/gfx/image/image.h"
27 @interface ProfileMenuController (Private)
28 - (void)initializeMenu;
31 namespace ProfileMenuControllerInternal {
33 class Observer : public chrome::BrowserListObserver,
34 public AvatarMenuObserver {
36 Observer(ProfileMenuController* controller) : controller_(controller) {
37 BrowserList::AddObserver(this);
40 ~Observer() override { BrowserList::RemoveObserver(this); }
42 // chrome::BrowserListObserver:
43 void OnBrowserAdded(Browser* browser) override {}
44 void OnBrowserRemoved(Browser* browser) override {
45 [controller_ activeBrowserChangedTo:chrome::GetLastActiveBrowser()];
47 void OnBrowserSetLastActive(Browser* browser) override {
48 [controller_ activeBrowserChangedTo:browser];
51 // AvatarMenuObserver:
52 void OnAvatarMenuChanged(AvatarMenu* menu) override {
53 [controller_ rebuildMenu];
57 ProfileMenuController* controller_; // Weak; owns this.
60 } // namespace ProfileMenuControllerInternal
62 ////////////////////////////////////////////////////////////////////////////////
64 @implementation ProfileMenuController
66 - (id)initWithMainMenuItem:(NSMenuItem*)item {
67 if ((self = [super init])) {
70 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:
71 l10n_util::GetNSStringWithFixup(IDS_PROFILES_OPTIONS_GROUP_NAME)]);
72 [mainMenuItem_ setSubmenu:menu];
74 // This object will be constructed as part of nib loading, which happens
75 // before the message loop starts and g_browser_process is available.
76 // Schedule this on the loop to do work when the browser is ready.
77 [self performSelector:@selector(initializeMenu)
84 - (IBAction)switchToProfileFromMenu:(id)sender {
85 avatarMenu_->SwitchToProfile([sender tag], false,
86 ProfileMetrics::SWITCH_PROFILE_MENU);
89 - (IBAction)switchToProfileFromDock:(id)sender {
90 // Explicitly bring to the foreground when taking action from the dock.
91 [NSApp activateIgnoringOtherApps:YES];
92 avatarMenu_->SwitchToProfile([sender tag], false,
93 ProfileMetrics::SWITCH_PROFILE_DOCK);
96 - (IBAction)editProfile:(id)sender {
97 avatarMenu_->EditProfile(avatarMenu_->GetActiveProfileIndex());
100 - (IBAction)newProfile:(id)sender {
101 avatarMenu_->AddNewProfile(ProfileMetrics::ADD_NEW_USER_MENU);
104 - (BOOL)insertItemsIntoMenu:(NSMenu*)menu
105 atOffset:(NSInteger)offset
106 fromDock:(BOOL)dock {
107 if (!avatarMenu_ || !avatarMenu_->ShouldShowAvatarMenu())
111 NSString* headerName =
112 l10n_util::GetNSStringWithFixup(IDS_PROFILES_OPTIONS_GROUP_NAME);
113 base::scoped_nsobject<NSMenuItem> header(
114 [[NSMenuItem alloc] initWithTitle:headerName
117 [header setEnabled:NO];
118 [menu insertItem:header atIndex:offset++];
121 for (size_t i = 0; i < avatarMenu_->GetNumberOfItems(); ++i) {
122 const AvatarMenu::Item& itemData = avatarMenu_->GetItemAt(i);
123 NSString* name = base::SysUTF16ToNSString(itemData.name);
124 SEL action = dock ? @selector(switchToProfileFromDock:)
125 : @selector(switchToProfileFromMenu:);
126 NSMenuItem* item = [self createItemWithTitle:name
128 [item setTag:itemData.menu_index];
130 [item setIndentationLevel:1];
132 gfx::Image itemIcon = itemData.icon;
133 // The image might be too large and need to be resized (i.e. if this is
134 // a signed-in user using the GAIA profile photo).
135 if (itemIcon.Width() > profiles::kAvatarIconWidth ||
136 itemIcon.Height() > profiles::kAvatarIconHeight) {
137 itemIcon = profiles::GetAvatarIconForWebUI(itemIcon, true);
139 DCHECK(itemIcon.Width() <= profiles::kAvatarIconWidth);
140 DCHECK(itemIcon.Height() <= profiles::kAvatarIconHeight);
141 [item setImage:itemIcon.ToNSImage()];
142 [item setState:itemData.active ? NSOnState : NSOffState];
144 [menu insertItem:item atIndex:i + offset];
150 - (BOOL)validateMenuItem:(NSMenuItem*)menuItem {
151 // In guest mode, chrome://settings isn't available, so disallow creating
152 // or editing a profile.
153 Profile* activeProfile = ProfileManager::GetLastUsedProfile();
154 if (activeProfile->IsGuestSession()) {
155 return [menuItem action] != @selector(newProfile:) &&
156 [menuItem action] != @selector(editProfile:);
159 const AvatarMenu::Item& itemData = avatarMenu_->GetItemAt(
160 avatarMenu_->GetActiveProfileIndex());
161 if ([menuItem action] == @selector(switchToProfileFromDock:) ||
162 [menuItem action] == @selector(switchToProfileFromMenu:)) {
163 if (!itemData.supervised)
166 return [menuItem tag] == static_cast<NSInteger>(itemData.menu_index);
169 if ([menuItem action] == @selector(newProfile:))
170 return !itemData.supervised;
175 // Private /////////////////////////////////////////////////////////////////////
178 return [mainMenuItem_ submenu];
181 - (void)initializeMenu {
182 observer_.reset(new ProfileMenuControllerInternal::Observer(self));
183 avatarMenu_.reset(new AvatarMenu(
184 &g_browser_process->profile_manager()->GetProfileInfoCache(),
187 avatarMenu_->RebuildMenu();
189 [[self menu] addItem:[NSMenuItem separatorItem]];
191 NSMenuItem* item = [self createItemWithTitle:
192 l10n_util::GetNSStringWithFixup(IDS_PROFILES_MANAGE_BUTTON_LABEL)
193 action:@selector(editProfile:)];
194 [[self menu] addItem:item];
196 [[self menu] addItem:[NSMenuItem separatorItem]];
197 item = [self createItemWithTitle:l10n_util::GetNSStringWithFixup(
198 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION)
199 action:@selector(newProfile:)];
200 [[self menu] addItem:item];
205 // Notifies the controller that the active browser has changed and that the
206 // menu item and menu need to be updated to reflect that.
207 - (void)activeBrowserChangedTo:(Browser*)browser {
208 // Tell the menu that the browser has changed.
209 avatarMenu_->ActiveBrowserChanged(browser);
211 // If |browser| is NULL, it may be because the current profile was deleted
212 // and there are no other loaded profiles. In this case, calling
213 // |avatarMenu_->GetActiveProfileIndex()| may result in a profile being
214 // loaded, which is inappropriate to do on the UI thread.
216 // An early return provides the desired behavior:
217 // a) If the profile was deleted, the menu would have been rebuilt and no
218 // profile will have a check mark.
219 // b) If the profile was not deleted, but there is no active browser, then
220 // the previous profile will remain checked.
224 // Update the avatar menu to get the active item states. Don't call
225 // avatarMenu_->GetActiveProfileIndex() as the index might be
226 // incorrect if -activeBrowserChangedTo: is called while we deleting the
227 // active profile and closing all its browser windows.
228 avatarMenu_->RebuildMenu();
230 // Update the state for the menu items.
231 for (size_t i = 0; i < avatarMenu_->GetNumberOfItems(); ++i) {
232 const AvatarMenu::Item& itemData = avatarMenu_->GetItemAt(i);
233 [[[self menu] itemWithTag:itemData.menu_index]
234 setState:itemData.active ? NSOnState : NSOffState];
238 - (void)rebuildMenu {
239 NSMenu* menu = [self menu];
241 for (NSMenuItem* item = [menu itemAtIndex:0];
242 ![item isSeparatorItem];
243 item = [menu itemAtIndex:0]) {
244 [menu removeItemAtIndex:0];
247 BOOL hasContent = [self insertItemsIntoMenu:menu atOffset:0 fromDock:NO];
249 [mainMenuItem_ setHidden:!hasContent];
252 - (NSMenuItem*)createItemWithTitle:(NSString*)title action:(SEL)sel {
253 base::scoped_nsobject<NSMenuItem> item(
254 [[NSMenuItem alloc] initWithTitle:title action:sel keyEquivalent:@""]);
255 [item setTarget:self];
256 return [item.release() autorelease];