Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / profiles / profile_menu_controller.mm
index 7392ae0..3838232 100644 (file)
@@ -37,21 +37,19 @@ class Observer : public chrome::BrowserListObserver,
     BrowserList::AddObserver(this);
   }
 
-  virtual ~Observer() {
-    BrowserList::RemoveObserver(this);
-  }
+  ~Observer() override { BrowserList::RemoveObserver(this); }
 
   // chrome::BrowserListObserver:
-  virtual void OnBrowserAdded(Browser* browser) OVERRIDE {}
-  virtual void OnBrowserRemoved(Browser* browser) OVERRIDE {
+  void OnBrowserAdded(Browser* browser) override {}
+  void OnBrowserRemoved(Browser* browser) override {
     [controller_ activeBrowserChangedTo:chrome::GetLastActiveBrowser()];
   }
-  virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE {
+  void OnBrowserSetLastActive(Browser* browser) override {
     [controller_ activeBrowserChangedTo:browser];
   }
 
   // AvatarMenuObserver:
-  virtual void OnAvatarMenuChanged(AvatarMenu* menu) OVERRIDE {
+  void OnAvatarMenuChanged(AvatarMenu* menu) override {
     [controller_ rebuildMenu];
   }
 
@@ -84,29 +82,33 @@ class Observer : public chrome::BrowserListObserver,
 }
 
 - (IBAction)switchToProfileFromMenu:(id)sender {
-  menu_->SwitchToProfile([sender tag], false,
-                         ProfileMetrics::SWITCH_PROFILE_MENU);
+  avatarMenu_->SwitchToProfile([sender tag], false,
+                               ProfileMetrics::SWITCH_PROFILE_MENU);
 }
 
 - (IBAction)switchToProfileFromDock:(id)sender {
   // Explicitly bring to the foreground when taking action from the dock.
   [NSApp activateIgnoringOtherApps:YES];
-  menu_->SwitchToProfile([sender tag], false,
-                         ProfileMetrics::SWITCH_PROFILE_DOCK);
+  avatarMenu_->SwitchToProfile([sender tag], false,
+                               ProfileMetrics::SWITCH_PROFILE_DOCK);
 }
 
 - (IBAction)editProfile:(id)sender {
-  menu_->EditProfile(menu_->GetActiveProfileIndex());
+  avatarMenu_->EditProfile(avatarMenu_->GetActiveProfileIndex());
 }
 
 - (IBAction)newProfile:(id)sender {
-  menu_->AddNewProfile(ProfileMetrics::ADD_NEW_USER_MENU);
+  avatarMenu_->AddNewProfile(ProfileMetrics::ADD_NEW_USER_MENU);
 }
 
 - (BOOL)insertItemsIntoMenu:(NSMenu*)menu
                    atOffset:(NSInteger)offset
                    fromDock:(BOOL)dock {
-  if (!menu_ || !menu_->ShouldShowAvatarMenu())
+  if (!avatarMenu_ || !avatarMenu_->ShouldShowAvatarMenu())
+    return NO;
+
+  // Don't show the list of profiles in the dock if only one profile exists.
+  if (dock && avatarMenu_->GetNumberOfItems() <= 1)
     return NO;
 
   if (dock) {
@@ -120,8 +122,8 @@ class Observer : public chrome::BrowserListObserver,
     [menu insertItem:header atIndex:offset++];
   }
 
-  for (size_t i = 0; i < menu_->GetNumberOfItems(); ++i) {
-    const AvatarMenu::Item& itemData = menu_->GetItemAt(i);
+  for (size_t i = 0; i < avatarMenu_->GetNumberOfItems(); ++i) {
+    const AvatarMenu::Item& itemData = avatarMenu_->GetItemAt(i);
     NSString* name = base::SysUTF16ToNSString(itemData.name);
     SEL action = dock ? @selector(switchToProfileFromDock:)
                       : @selector(switchToProfileFromMenu:);
@@ -158,8 +160,8 @@ class Observer : public chrome::BrowserListObserver,
            [menuItem action] != @selector(editProfile:);
   }
 
-  const AvatarMenu::Item& itemData = menu_->GetItemAt(
-      menu_->GetActiveProfileIndex());
+  const AvatarMenu::Item& itemData = avatarMenu_->GetItemAt(
+      avatarMenu_->GetActiveProfileIndex());
   if ([menuItem action] == @selector(switchToProfileFromDock:) ||
       [menuItem action] == @selector(switchToProfileFromMenu:)) {
     if (!itemData.supervised)
@@ -182,11 +184,11 @@ class Observer : public chrome::BrowserListObserver,
 
 - (void)initializeMenu {
   observer_.reset(new ProfileMenuControllerInternal::Observer(self));
-  menu_.reset(new AvatarMenu(
+  avatarMenu_.reset(new AvatarMenu(
       &g_browser_process->profile_manager()->GetProfileInfoCache(),
       observer_.get(),
       NULL));
-  menu_->RebuildMenu();
+  avatarMenu_->RebuildMenu();
 
   [[self menu] addItem:[NSMenuItem separatorItem]];
 
@@ -208,12 +210,12 @@ class Observer : public chrome::BrowserListObserver,
 // menu item and menu need to be updated to reflect that.
 - (void)activeBrowserChangedTo:(Browser*)browser {
   // Tell the menu that the browser has changed.
-  menu_->ActiveBrowserChanged(browser);
+  avatarMenu_->ActiveBrowserChanged(browser);
 
   // If |browser| is NULL, it may be because the current profile was deleted
   // and there are no other loaded profiles. In this case, calling
-  // |menu_->GetActiveProfileIndex()| may result in a profile being loaded,
-  // which is inappropriate to do on the UI thread.
+  // |avatarMenu_->GetActiveProfileIndex()| may result in a profile being
+  // loaded, which is inappropriate to do on the UI thread.
   //
   // An early return provides the desired behavior:
   //   a) If the profile was deleted, the menu would have been rebuilt and no
@@ -223,15 +225,17 @@ class Observer : public chrome::BrowserListObserver,
   if (!browser)
     return;
 
-  // In guest mode, there is no active menu item.
-  size_t activeProfileIndex = browser->profile()->IsGuestSession() ?
-      std::string::npos : menu_->GetActiveProfileIndex();
+  // Update the avatar menu to get the active item states. Don't call
+  // avatarMenu_->GetActiveProfileIndex() as the index might be
+  // incorrect if -activeBrowserChangedTo: is called while we deleting the
+  // active profile and closing all its browser windows.
+  avatarMenu_->RebuildMenu();
 
   // Update the state for the menu items.
-  for (size_t i = 0; i < menu_->GetNumberOfItems(); ++i) {
-    size_t tag = menu_->GetItemAt(i).menu_index;
-    [[[self menu] itemWithTag:tag]
-        setState:activeProfileIndex == tag ? NSOnState : NSOffState];
+  for (size_t i = 0; i < avatarMenu_->GetNumberOfItems(); ++i) {
+    const AvatarMenu::Item& itemData = avatarMenu_->GetItemAt(i);
+    [[[self menu] itemWithTag:itemData.menu_index]
+        setState:itemData.active ? NSOnState : NSOffState];
   }
 }