Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / website_settings / permission_selector_button.mm
blob6cc2466abe1c78cb8ee47f0eac7ffed6b9b49306
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.type, permissionInfo.setting,
35         permissionInfo.default_setting, permissionInfo.source);
36     [titleItem setTitle:base::SysUTF16ToNSString(buttonTitle)];
37     [[self cell] setUsesItemFromMenu:NO];
38     [[self cell] setMenuItem:titleItem.get()];
39     // Although the frame is reset, below, this sizes the cell properly.
40     [self sizeToFit];
42     // Size the button to just fit the visible title - not all of its items.
43     [self setFrameSize:SizeForWebsiteSettingsButtonTitle(self, [self title])];
44   }
45   return self;
48 - (CGFloat)maxTitleWidthForContentSettingsType:(ContentSettingsType)type
49                             withDefaultSetting:(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             type, static_cast<ContentSetting>([item tag]), defaultSetting,
56             content_settings::SETTING_SOURCE_USER));
57     NSSize size = SizeForWebsiteSettingsButtonTitle(self, title);
58     maxTitleWidth = std::max(maxTitleWidth, size.width);
59   }
60   return maxTitleWidth;
63 // Accessor function for testing only.
64 - (NSMenu*)permissionMenu {
65   return [menuController_ menu];
68 @end