- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / profile_menu_controller.mm
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.h"
11 #include "chrome/browser/profiles/avatar_menu_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 AvatarMenuObserver {
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   // AvatarMenuObserver:
52   virtual void OnAvatarMenuChanged(AvatarMenu* menu) 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   menu_->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   menu_->SwitchToProfile([sender tag], false);
93   ProfileMetrics::LogProfileSwitchUser(ProfileMetrics::SWITCH_PROFILE_DOCK);
94 }
95
96 - (IBAction)editProfile:(id)sender {
97   menu_->EditProfile(menu_->GetActiveProfileIndex());
98 }
99
100 - (IBAction)newProfile:(id)sender {
101   menu_->AddNewProfile(ProfileMetrics::ADD_NEW_USER_MENU);
102 }
103
104 - (BOOL)insertItemsIntoMenu:(NSMenu*)menu
105                    atOffset:(NSInteger)offset
106                    fromDock:(BOOL)dock {
107   if (!menu_ || !menu_->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 < menu_->GetNumberOfItems(); ++i) {
122     const AvatarMenu::Item& itemData = menu_->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.menu_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   const AvatarMenu::Item& itemData = menu_->GetItemAt(
143       menu_->GetActiveProfileIndex());
144   if ([menuItem action] == @selector(switchToProfileFromDock:) ||
145       [menuItem action] == @selector(switchToProfileFromMenu:)) {
146     if (!itemData.managed)
147       return YES;
148
149     return [menuItem tag] == static_cast<NSInteger>(itemData.menu_index);
150   }
151
152   if ([menuItem action] == @selector(newProfile:))
153     return !itemData.managed;
154
155   return YES;
156 }
157
158 // Private /////////////////////////////////////////////////////////////////////
159
160 - (NSMenu*)menu {
161   return [mainMenuItem_ submenu];
162 }
163
164 - (void)initializeMenu {
165   observer_.reset(new ProfileMenuControllerInternal::Observer(self));
166   menu_.reset(new AvatarMenu(
167       &g_browser_process->profile_manager()->GetProfileInfoCache(),
168       observer_.get(),
169       NULL));
170   menu_->RebuildMenu();
171
172   [[self menu] addItem:[NSMenuItem separatorItem]];
173
174   NSMenuItem* item = [self createItemWithTitle:
175           l10n_util::GetNSStringWithFixup(IDS_PROFILES_CUSTOMIZE_PROFILE)
176                                         action:@selector(editProfile:)];
177   [[self menu] addItem:item];
178
179   [[self menu] addItem:[NSMenuItem separatorItem]];
180   item = [self createItemWithTitle:l10n_util::GetNSStringWithFixup(
181                                        IDS_PROFILES_CREATE_NEW_PROFILE_OPTION)
182                             action:@selector(newProfile:)];
183   [[self menu] addItem:item];
184
185   [self rebuildMenu];
186 }
187
188 // Notifies the controller that the active browser has changed and that the
189 // menu item and menu need to be updated to reflect that.
190 - (void)activeBrowserChangedTo:(Browser*)browser {
191   // Tell the menu that the browser has changed.
192   menu_->ActiveBrowserChanged(browser);
193
194   // If |browser| is NULL, it may be because the current profile was deleted
195   // and there are no other loaded profiles. In this case, calling
196   // |menu_->GetActiveProfileIndex()| may result in a profile being loaded,
197   // which is inappropriate to do on the UI thread.
198   //
199   // An early return provides the desired behavior:
200   //   a) If the profile was deleted, the menu would have been rebuilt and no
201   //      profile will have a check mark.
202   //   b) If the profile was not deleted, but there is no active browser, then
203   //      the previous profile will remain checked.
204   if (!browser)
205     return;
206
207   size_t active_profile_index = menu_->GetActiveProfileIndex();
208
209   // Update the state for the menu items.
210   for (size_t i = 0; i < menu_->GetNumberOfItems(); ++i) {
211     size_t tag = menu_->GetItemAt(i).menu_index;
212     [[[self menu] itemWithTag:tag]
213         setState:active_profile_index == tag ? NSOnState
214                                              : NSOffState];
215   }
216 }
217
218 - (void)rebuildMenu {
219   NSMenu* menu = [self menu];
220
221   for (NSMenuItem* item = [menu itemAtIndex:0];
222        ![item isSeparatorItem];
223        item = [menu itemAtIndex:0]) {
224     [menu removeItemAtIndex:0];
225   }
226
227   BOOL hasContent = [self insertItemsIntoMenu:menu atOffset:0 fromDock:NO];
228
229   [mainMenuItem_ setHidden:!hasContent];
230 }
231
232 - (NSMenuItem*)createItemWithTitle:(NSString*)title action:(SEL)sel {
233   base::scoped_nsobject<NSMenuItem> item(
234       [[NSMenuItem alloc] initWithTitle:title action:sel keyEquivalent:@""]);
235   [item setTarget:self];
236   return [item.release() autorelease];
237 }
238
239 @end