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 #import "chrome/browser/ui/cocoa/profile_menu_controller.h" 6 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_model.h" 11 #include "chrome/browser/profiles/avatar_menu_model_observer.h" 12 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile_info_cache.h" 14 #include "chrome/browser/profiles/profile_info_interface.h" 15 #include "chrome/browser/profiles/profile_manager.h" 16 #include "chrome/browser/profiles/profile_metrics.h" 17 #include "chrome/browser/ui/browser.h" 18 #include "chrome/browser/ui/browser_list.h" 19 #include "chrome/browser/ui/browser_list_observer.h" 20 #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h" 21 #include "grit/generated_resources.h" 22 #include "ui/base/l10n/l10n_util_mac.h" 23 #include "ui/gfx/image/image.h" 24 25 @interface ProfileMenuController (Private) 26 - (void)initializeMenu; 27 @end 28 29 namespace ProfileMenuControllerInternal { 30 31 class Observer : public chrome::BrowserListObserver, 32 public AvatarMenuModelObserver { 33 public: 34 Observer(ProfileMenuController* controller) : controller_(controller) { 35 BrowserList::AddObserver(this); 36 } 37 38 virtual ~Observer() { 39 BrowserList::RemoveObserver(this); 40 } 41 42 // chrome::BrowserListObserver: 43 virtual void OnBrowserAdded(Browser* browser) OVERRIDE {} 44 virtual void OnBrowserRemoved(Browser* browser) OVERRIDE { 45 [controller_ activeBrowserChangedTo:chrome::GetLastActiveBrowser()]; 46 } 47 virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE { 48 [controller_ activeBrowserChangedTo:browser]; 49 } 50 51 // AvatarMenuModelObserver: 52 virtual void OnAvatarMenuModelChanged(AvatarMenuModel* model) OVERRIDE { 53 [controller_ rebuildMenu]; 54 } 55 56 private: 57 ProfileMenuController* controller_; // Weak; owns this. 58 }; 59 60 } // namespace ProfileMenuControllerInternal 61 62 //////////////////////////////////////////////////////////////////////////////// 63 64 @implementation ProfileMenuController 65 66 - (id)initWithMainMenuItem:(NSMenuItem*)item { 67 if ((self = [super init])) { 68 mainMenuItem_ = item; 69 70 base::scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle: 71 l10n_util::GetNSStringWithFixup(IDS_PROFILES_OPTIONS_GROUP_NAME)]); 72 [mainMenuItem_ setSubmenu:menu]; 73 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) 78 withObject:nil 79 afterDelay:0]; 80 } 81 return self; 82 } 83 84 - (IBAction)switchToProfileFromMenu:(id)sender { 85 model_->SwitchToProfile([sender tag], false); 86 ProfileMetrics::LogProfileSwitchUser(ProfileMetrics::SWITCH_PROFILE_MENU); 87 } 88 89 - (IBAction)switchToProfileFromDock:(id)sender { 90 // Explicitly bring to the foreground when taking action from the dock. 91 [NSApp activateIgnoringOtherApps:YES]; 92 model_->SwitchToProfile([sender tag], false); 93 ProfileMetrics::LogProfileSwitchUser(ProfileMetrics::SWITCH_PROFILE_DOCK); 94 } 95 96 - (IBAction)editProfile:(id)sender { 97 model_->EditProfile(model_->GetActiveProfileIndex()); 98 } 99 100 - (IBAction)newProfile:(id)sender { 101 model_->AddNewProfile(ProfileMetrics::ADD_NEW_USER_MENU); 102 } 103 104 - (BOOL)insertItemsIntoMenu:(NSMenu*)menu 105 atOffset:(NSInteger)offset 106 fromDock:(BOOL)dock { 107 if (!model_ || !model_->ShouldShowAvatarMenu()) 108 return NO; 109 110 if (dock) { 111 NSString* headerName = 112 l10n_util::GetNSStringWithFixup(IDS_PROFILES_OPTIONS_GROUP_NAME); 113 base::scoped_nsobject<NSMenuItem> header( 114 [[NSMenuItem alloc] initWithTitle:headerName 115 action:NULL 116 keyEquivalent:@""]); 117 [header setEnabled:NO]; 118 [menu insertItem:header atIndex:offset++]; 119 } 120 121 for (size_t i = 0; i < model_->GetNumberOfItems(); ++i) { 122 const AvatarMenuModel::Item& itemData = model_->GetItemAt(i); 123 NSString* name = base::SysUTF16ToNSString(itemData.name); 124 SEL action = dock ? @selector(switchToProfileFromDock:) 125 : @selector(switchToProfileFromMenu:); 126 NSMenuItem* item = [self createItemWithTitle:name 127 action:action]; 128 [item setTag:itemData.model_index]; 129 if (dock) { 130 [item setIndentationLevel:1]; 131 } else { 132 [item setImage:itemData.icon.ToNSImage()]; 133 [item setState:itemData.active ? NSOnState : NSOffState]; 134 } 135 [menu insertItem:item atIndex:i + offset]; 136 } 137 138 return YES; 139 } 140 141 - (BOOL)validateMenuItem:(NSMenuItem*)menuItem { 142 size_t activeProfileIndex = model_->GetActiveProfileIndex(); 143 ProfileInfoCache* cache = 144 &g_browser_process->profile_manager()->GetProfileInfoCache(); 145 BOOL profileIsManaged = cache->ProfileIsManagedAtIndex(activeProfileIndex); 146 if ([menuItem action] == @selector(switchToProfileFromDock:) || 147 [menuItem action] == @selector(switchToProfileFromMenu:)) { 148 if (!profileIsManaged) 149 return YES; 150 151 return [menuItem tag] == static_cast<NSInteger>(activeProfileIndex); 152 } 153 154 if ([menuItem action] == @selector(newProfile:)) 155 return !profileIsManaged; 156 157 return YES; 158 } 159 160 // Private ///////////////////////////////////////////////////////////////////// 161 162 - (NSMenu*)menu { 163 return [mainMenuItem_ submenu]; 164 } 165 166 - (void)initializeMenu { 167 observer_.reset(new ProfileMenuControllerInternal::Observer(self)); 168 model_.reset(new AvatarMenuModel( 169 &g_browser_process->profile_manager()->GetProfileInfoCache(), 170 observer_.get(), 171 NULL)); 172 173 [[self menu] addItem:[NSMenuItem separatorItem]]; 174 175 NSMenuItem* item = [self createItemWithTitle: 176 l10n_util::GetNSStringWithFixup(IDS_PROFILES_CUSTOMIZE_PROFILE) 177 action:@selector(editProfile:)]; 178 [[self menu] addItem:item]; 179 180 [[self menu] addItem:[NSMenuItem separatorItem]]; 181 item = [self createItemWithTitle:l10n_util::GetNSStringWithFixup( 182 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION) 183 action:@selector(newProfile:)]; 184 [[self menu] addItem:item]; 185 186 [self rebuildMenu]; 187 } 188 189 // Notifies the controller that the active browser has changed and that the 190 // menu item and menu need to be updated to reflect that. 191 - (void)activeBrowserChangedTo:(Browser*)browser { 192 // Tell the model that the browser has changed. 193 model_->set_browser(browser); 194 195 // If |browser| is NULL, it may be because the current profile was deleted 196 // and there are no other loaded profiles. In this case, calling 197 // |model_->GetActiveProfileIndex()| may result in a profile being loaded, 198 // which is inappropriate to do on the UI thread. 199 // 200 // An early return provides the desired behavior: 201 // a) If the profile was deleted, the menu would have been rebuilt and no 202 // profile will have a check mark. 203 // b) If the profile was not deleted, but there is no active browser, then 204 // the previous profile will remain checked. 205 if (!browser) 206 return; 207 208 size_t active_profile_index = model_->GetActiveProfileIndex(); 209 210 // Update the state for the menu items. 211 for (size_t i = 0; i < model_->GetNumberOfItems(); ++i) { 212 size_t tag = model_->GetItemAt(i).model_index; 213 [[[self menu] itemWithTag:tag] 214 setState:active_profile_index == tag ? NSOnState 215 : NSOffState]; 216 } 217 } 218 219 - (void)rebuildMenu { 220 NSMenu* menu = [self menu]; 221 222 for (NSMenuItem* item = [menu itemAtIndex:0]; 223 ![item isSeparatorItem]; 224 item = [menu itemAtIndex:0]) { 225 [menu removeItemAtIndex:0]; 226 } 227 228 BOOL hasContent = [self insertItemsIntoMenu:menu atOffset:0 fromDock:NO]; 229 230 [mainMenuItem_ setHidden:!hasContent]; 231 } 232 233 - (NSMenuItem*)createItemWithTitle:(NSString*)title action:(SEL)sel { 234 base::scoped_nsobject<NSMenuItem> item( 235 [[NSMenuItem alloc] initWithTitle:title action:sel keyEquivalent:@""]); 236 [item setTarget:self]; 237 return [item.release() autorelease]; 238 } 239 240 @end 241