Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: chrome/browser/ui/cocoa/renderer_context_menu/share_menu_controller.mm

Issue 1105143005: Issue 465302:System wide share options on Mac Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added unit tests, honored Cocoa memory management issues Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #import "chrome/browser/ui/cocoa/renderer_context_menu/share_menu_controller.h"
Avi (use Gerrit) 2015/05/15 16:13:08 Where is your copyright header? Also, same questi
2
3 #include "base/strings/sys_string_conversions.h"
4 #include "base/strings/utf_string_conversions.h"
5 #include "chrome/app/chrome_command_ids.h"
6 #import "chrome/browser/app_controller_mac.h"
7 #include "chrome/browser/ui/browser_commands.h"
8 #include "chrome/browser/ui/browser_finder.h"
9 #include "chrome/browser/ui/browser_list.h"
10 #include "chrome/browser/ui/browser_list_observer.h"
11 #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h"
12 #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/l10n/l10n_util_mac.h"
17 #include "net/base/escape.h"
18
19 @interface ShareMenuController() <NSSharingServiceDelegate>
20 - (void)buildShareItemsFor:(Browser* )browser;
21 - (NSMenuItem*)createItemWithTitle:(NSString*)title action:(SEL)sel;
22 - (NSString *)currentPageUrlFor:(Browser* )browser;
23 - (void)enableShareMenuItemsFor:(Browser *)browser;
24 - (NSArray*)shareMenuItemsFor:(NSArray*)items;
Avi (use Gerrit) 2015/05/15 16:13:09 Lots of bad formatting. For types, it's always (N
25 @end
26
27 namespace ShareMenuControllerInternal {
28
29 class Observer : public chrome::BrowserListObserver {
30 public:
31 Observer(ShareMenuController* controller) : controller_(controller) {
32 BrowserList::AddObserver(this);
33 }
34
35 ~Observer() override { BrowserList::RemoveObserver(this); }
36
37 // Wait for a browser tab to become active before building the share menu.
38 void OnBrowserSetLastActive(Browser* browser) override {
39 if (browser && ![controller_ menuItemsLoaded]) {
40 [controller_ initShareMenuItem];
41 [controller_ buildShareItemsFor:browser];
42 }
43 [controller_ enableShareMenuItemsFor:browser];
44 }
45
46 private:
47 ShareMenuController* controller_; // Weak; owns this.
48 };
49
50 } // namespace ShareMenuControllerInternal
51
52 ////////////////////////////////////////////////////////////////////////////////
53
54 @implementation ShareMenuController
55
56 - (id)initWithMainMenu:(NSMenu*)menu {
57 if (self = [super init]) {
58 mainMenu_ = menu;
59 shareItems_.reset([[NSMutableArray alloc] init]);
60 menuItemsLoaded_ = NO;
61 observer_.reset(new ShareMenuControllerInternal::Observer(self));
62 }
63 return self;
64 }
65
66 - (BOOL) menuItemsLoaded {
Avi (use Gerrit) 2015/05/15 16:13:08 No space after )
67 return menuItemsLoaded_;
68 }
69
70 // Create a submenu for displaying various system wide sharing options.
Avi (use Gerrit) 2015/05/15 16:13:08 Comments go in the header file, not the .mm file.
71 - (void)initShareMenuItem {
72 shareSubMenu_.reset([[NSMenu alloc] initWithTitle:@"ShareSubMenu"]);
73 fileMenuItem_ = [mainMenu_ itemWithTag:IDC_FILE_MENU];
74 NSString* shareMenuName =
75 l10n_util::GetNSStringWithFixup(IDS_SHARE_MENU_MAC);
76 shareMenuItem_ = [self createItemWithTitle:shareMenuName action:nil];
77 [shareMenuItem_ setTag:IDC_SHARE_MENU];
78 }
79
80 - (NSMenu*)shareSubMenu {
81 return shareSubMenu_;
82 }
83
84 - (NSMenuItem*)shareSubMenuItem {
85 return shareMenuItem_;
86 }
87
88 - (void)buildShareItemsFor:(Browser *)browser {
Avi (use Gerrit) 2015/05/15 16:13:09 No space before *.
89 if (!shareSubMenu_)
90 return;
91
92 [shareItems_ removeAllObjects];
93 [shareItems_ addObject:[self currentPageUrlFor:browser]];
94
95 // If the sharemenu had already been built, remove all items and rebuild.
96 if ([shareSubMenu_ numberOfItems] > 0) {
97 [shareSubMenu_ removeAllItems];
98 }
99
100 NSArray* menuItems = [self shareMenuItemsFor:shareItems_];
101 for (NSMenuItem* item in menuItems) {
102 [shareSubMenu_ addItem:item];
103 }
104
105 // Make sure the Share menu item is created only once.
106 if (![[fileMenuItem_ submenu] itemWithTag:IDC_SHARE_MENU]) {
107 NSMenuItem* savePageItem = [[fileMenuItem_ submenu]
108 itemWithTag:IDC_SAVE_PAGE];
109 NSInteger idxSavePage = [[fileMenuItem_ submenu] indexOfItem:savePageItem];
110 [[fileMenuItem_ submenu] insertItem:shareMenuItem_ atIndex:idxSavePage + 2];
111 }
Avi (use Gerrit) 2015/05/15 16:13:09 ? Why do you have to do this? This code looks lik
sarka 2015/05/15 16:36:55 Were you referring to the line containing '[[fileM
Avi (use Gerrit) 2015/05/15 16:40:33 I mean this whole "if" block. You are modifying t
sarka 2015/05/15 16:45:47 You are right. The Share menu and its submenu shou
112
113 [[fileMenuItem_ submenu] setSubmenu:shareSubMenu_ forItem:shareMenuItem_];
114 menuItemsLoaded_ = YES;
115 }
116
117 - (NSMenuItem*)createItemWithTitle:(NSString*)title action:(SEL)sel {
118 NSMenuItem* item = [[[NSMenuItem alloc] initWithTitle:title action:sel
119 keyEquivalent:@""]
120 autorelease];
Avi (use Gerrit) 2015/05/15 16:13:09 Align on :
121 [item setTarget:self];
122 return item;
123 }
124
125 - (NSString*)currentPageUrlFor:(Browser*)browser {
126 TabStripModel* tabStrip = browser->tab_strip_model();
127 content::WebContents* wc = tabStrip->GetActiveWebContents();
128 std::string pageUrl = net::EscapeQueryParamValue(wc->GetVisibleURL().spec(),
129 false);
130 return base::SysUTF8ToNSString(pageUrl);
131 }
132
133 - (void)enableShareMenuItemsFor:(Browser *)browser {
Avi (use Gerrit) 2015/05/15 16:13:09 No space before *
134 [shareSubMenu_ setAutoenablesItems:NO];
135 for (NSMenuItem* item in [shareSubMenu_ itemArray]) {
136 BOOL enabled = chrome::CanEmailPageLocation(browser) ?
137 [self validateMenuItem:item] : NO;
Avi (use Gerrit) 2015/05/15 16:13:09 indent
138 [item setEnabled:enabled];
139 }
140 }
141
142 // Retrieve the list of possible sharing options and create menu item for each.
Avi (use Gerrit) 2015/05/15 16:13:09 comments go in the header file
143 - (NSArray*)shareMenuItemsFor:(NSArray*)items {
144 base::scoped_nsobject<NSMutableArray>
145 menuItems([[NSMutableArray alloc] init]);
Avi (use Gerrit) 2015/05/15 16:13:08 indent!
146 NSArray *sharingServices = [NSSharingService sharingServicesForItems:items];
Avi (use Gerrit) 2015/05/15 16:13:08 space after the *, not before
147 for (NSSharingService* currentService in sharingServices) {
148 NSString* titleText = nil;
149 if ([currentService isEqualTo:[NSSharingService sharingServiceNamed
150 :NSSharingServiceNameComposeEmail]]) {
Avi (use Gerrit) 2015/05/15 16:13:08 Do not break there! You're breaking a method name
151 titleText = l10n_util::GetNSString(IDS_EMAIL_PAGE_LOCATION_MAC);
152 } else {
153 titleText = currentService.title;
154 }
155 NSMenuItem* item = [self createItemWithTitle:titleText
156 action:@selector(
157 systemShareService:)];
Avi (use Gerrit) 2015/05/15 16:13:08 One line. Don't align ALL colons, just the ones th
158 item.image = currentService.image;
159 item.representedObject = currentService;
160 [item setTarget:self];
161 currentService.delegate = self;
162 [menuItems addObject:item];
163 }
164 return [menuItems copy];
Avi (use Gerrit) 2015/05/15 16:13:09 1) You're leaking. 2) Why do you need to copy this
165 }
166
167 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
Avi (use Gerrit) 2015/05/15 16:13:08 no space before *
168 return ([menuItem action] == @selector(systemShareService:));
169 }
170
171 - (void)systemShareService:(id)sender {
172 NSMenuItem* currentMenuItem = (NSMenuItem*)sender;
173 [currentMenuItem.representedObject performWithItems:shareItems_];
174 }
175
176 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698