[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / website_settings / permission_selector_button.mm
blob38448359848197eb9e5be0be759739f009abe21f
1 // Copyright 2014 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/website_settings/permission_selector_button.h"
7 #include "base/strings/sys_string_conversions.h"
8 #include "chrome/browser/ui/cocoa/website_settings/website_settings_utils_cocoa.h"
9 #include "chrome/browser/ui/website_settings/website_settings_ui.h"
10 #include "chrome/browser/ui/website_settings/website_settings_utils.h"
11 #import "ui/base/cocoa/menu_controller.h"
13 @implementation PermissionSelectorButton
15 - (id)initWithPermissionInfo:
16           (const WebsiteSettingsUI::PermissionInfo&)permissionInfo
17                       forURL:(const GURL&)url
18                 withCallback:(PermissionMenuModel::ChangeCallback)callback {
19   if (self = [super initWithFrame:NSMakeRect(0, 0, 1, 1) pullsDown:NO]) {
20     [self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
21     [self setBordered:NO];
22     [[self cell] setControlSize:NSSmallControlSize];
24     menuModel_.reset(new PermissionMenuModel(url, permissionInfo, callback));
26     menuController_.reset([[MenuController alloc] initWithModel:menuModel_.get()
27                                          useWithPopUpButtonCell:NO]);
28     [self setMenu:[menuController_ menu]];
29     [self selectItemWithTag:permissionInfo.setting];
31     // Set the button title.
32     base::scoped_nsobject<NSMenuItem> titleItem([[NSMenuItem alloc] init]);
33     base::string16 buttonTitle = WebsiteSettingsUI::PermissionActionToUIString(
34         permissionInfo.setting,
35         permissionInfo.default_setting,
36         permissionInfo.source);
37     [titleItem setTitle:base::SysUTF16ToNSString(buttonTitle)];
38     [[self cell] setUsesItemFromMenu:NO];
39     [[self cell] setMenuItem:titleItem.get()];
40     // Although the frame is reset, below, this sizes the cell properly.
41     [self sizeToFit];
43     // Size the button to just fit the visible title - not all of its items.
44     [self setFrameSize:SizeForWebsiteSettingsButtonTitle(self, [self title])];
45   }
46   return self;
49 - (CGFloat)maxTitleWidthWithDefaultSetting:(ContentSetting)defaultSetting {
50   // Determine the largest possible size for this button.
51   CGFloat maxTitleWidth = 0;
52   for (NSMenuItem* item in [self itemArray]) {
53     NSString* title =
54         base::SysUTF16ToNSString(WebsiteSettingsUI::PermissionActionToUIString(
55             static_cast<ContentSetting>([item tag]),
56             defaultSetting,
57             content_settings::SETTING_SOURCE_USER));
58     NSSize size = SizeForWebsiteSettingsButtonTitle(self, title);
59     maxTitleWidth = std::max(maxTitleWidth, size.width);
60   }
61   return maxTitleWidth;
64 // Accessor function for testing only.
65 - (NSMenu*)permissionMenu {
66   return [menuController_ menu];
69 @end