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.
5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_cocoa_controller.h"
7 #include "base/strings/sys_string_conversions.h"
8 #include "chrome/app/chrome_command_ids.h" // IDC_BOOKMARK_MENU
9 #import "chrome/browser/app_controller_mac.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_menu_bridge.h"
15 #import "chrome/browser/ui/cocoa/l10n_util.h"
16 #include "chrome/browser/ui/host_desktop.h"
17 #include "components/bookmarks/browser/bookmark_utils.h"
18 #include "content/public/browser/user_metrics.h"
19 #import "ui/base/cocoa/cocoa_base_utils.h"
20 #import "ui/base/cocoa/menu_controller.h"
22 using base::UserMetricsAction;
23 using bookmarks::BookmarkNode;
24 using content::OpenURLParams;
25 using content::Referrer;
29 // Menus more than this many pixels wide will get trimmed
30 // TODO(jrg): ask UI dudes what a good value is.
31 const NSUInteger kMaximumMenuPixelsWide = 300;
35 @implementation BookmarkMenuCocoaController
37 + (NSString*)menuTitleForNode:(const BookmarkNode*)node {
38 base::string16 title = [MenuController elideMenuTitle:node->GetTitle()
39 toWidth:kMaximumMenuPixelsWide];
40 return base::SysUTF16ToNSString(title);
43 + (NSString*)tooltipForNode:(const BookmarkNode*)node {
44 NSString* title = base::SysUTF16ToNSString(node->GetTitle());
45 std::string urlString = node->url().possibly_invalid_spec();
46 NSString* url = base::SysUTF8ToNSString(urlString);
47 return cocoa_l10n_util::TooltipForURLAndTitle(url, title);
50 - (id)initWithBridge:(BookmarkMenuBridge*)bridge
51 andMenu:(NSMenu*)menu {
52 if ((self = [super init])) {
55 menu_.reset([menu retain]);
56 [[self menu] setDelegate:self];
62 if ([[self menu] delegate] == self)
63 [[self menu] setDelegate:nil];
71 - (BOOL)validateMenuItem:(NSMenuItem*)menuItem {
72 AppController* controller = [NSApp delegate];
73 return ![controller keyWindowIsModal];
76 // NSMenu delegate method: called just before menu is displayed.
77 - (void)menuNeedsUpdate:(NSMenu*)menu {
78 bridge_->UpdateMenu(menu);
81 // Return the a BookmarkNode that has the given id (called
82 // "identifier" here to avoid conflict with objc's concept of "id").
83 - (const BookmarkNode*)nodeForIdentifier:(int)identifier {
84 return bookmarks::GetBookmarkNodeByID(bridge_->GetBookmarkModel(),
88 // Open the URL of the given BookmarkNode in the current tab.
89 - (void)openURLForNode:(const BookmarkNode*)node {
91 chrome::FindTabbedBrowser(bridge_->GetProfile(),
93 chrome::HOST_DESKTOP_TYPE_NATIVE);
95 browser = new Browser(Browser::CreateParams(
96 bridge_->GetProfile(), chrome::HOST_DESKTOP_TYPE_NATIVE));
98 WindowOpenDisposition disposition =
99 ui::WindowOpenDispositionFromNSEvent([NSApp currentEvent]);
100 OpenURLParams params(
101 node->url(), Referrer(), disposition,
102 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false);
103 browser->OpenURL(params);
106 // Open sites under BookmarkNode with the specified disposition.
107 - (void)openAll:(NSInteger)tag
108 withDisposition:(WindowOpenDisposition)disposition {
109 int identifier = tag;
111 const BookmarkNode* node = [self nodeForIdentifier:identifier];
115 chrome::FindTabbedBrowser(bridge_->GetProfile(),
117 chrome::HOST_DESKTOP_TYPE_NATIVE);
119 browser = new Browser(Browser::CreateParams(
120 bridge_->GetProfile(), chrome::HOST_DESKTOP_TYPE_NATIVE));
124 if (!node || !browser)
125 return; // shouldn't be reached
127 chrome::OpenAll(NULL, browser, node, disposition, browser->profile());
129 if (disposition == NEW_FOREGROUND_TAB) {
130 content::RecordAction(UserMetricsAction("OpenAllBookmarks"));
131 } else if (disposition == NEW_WINDOW) {
132 content::RecordAction(UserMetricsAction("OpenAllBookmarksNewWindow"));
134 content::RecordAction(
135 UserMetricsAction("OpenAllBookmarksIncognitoWindow"));
139 - (IBAction)openBookmarkMenuItem:(id)sender {
140 NSInteger tag = [sender tag];
141 int identifier = tag;
142 const BookmarkNode* node = [self nodeForIdentifier:identifier];
145 return; // shouldn't be reached
147 [self openURLForNode:node];
150 - (IBAction)openAllBookmarks:(id)sender {
151 [self openAll:[sender tag] withDisposition:NEW_FOREGROUND_TAB];
154 - (IBAction)openAllBookmarksNewWindow:(id)sender {
155 [self openAll:[sender tag] withDisposition:NEW_WINDOW];
158 - (IBAction)openAllBookmarksIncognitoWindow:(id)sender {
159 [self openAll:[sender tag] withDisposition:OFF_THE_RECORD];
162 @end // BookmarkMenuCocoaController