OLD | NEW |
| (Empty) |
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 #include "chrome/browser/ui/website_settings/permission_menu_model.h" | |
6 | |
7 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | |
8 #include "chrome/browser/plugins/plugin_utils.h" | |
9 #include "chrome/browser/plugins/plugins_field_trial.h" | |
10 #include "chrome/common/chrome_features.h" | |
11 #include "chrome/grit/generated_resources.h" | |
12 #include "content/public/common/origin_util.h" | |
13 #include "ppapi/features/features.h" | |
14 #include "ui/base/l10n/l10n_util.h" | |
15 #include "ui/base/material_design/material_design_controller.h" | |
16 | |
17 PermissionMenuModel::PermissionMenuModel( | |
18 Profile* profile, | |
19 const GURL& url, | |
20 const WebsiteSettingsUI::PermissionInfo& info, | |
21 const ChangeCallback& callback) | |
22 : ui::SimpleMenuModel(this), | |
23 host_content_settings_map_( | |
24 HostContentSettingsMapFactory::GetForProfile(profile)), | |
25 permission_(info), | |
26 callback_(callback) { | |
27 DCHECK(!callback_.is_null()); | |
28 base::string16 label; | |
29 | |
30 // Retrieve the string to show for the default setting for this permission. | |
31 ContentSetting effective_default_setting = permission_.default_setting; | |
32 | |
33 #if BUILDFLAG(ENABLE_PLUGINS) | |
34 effective_default_setting = PluginsFieldTrial::EffectiveContentSetting( | |
35 host_content_settings_map_, permission_.type, | |
36 permission_.default_setting); | |
37 #endif // BUILDFLAG(ENABLE_PLUGINS) | |
38 | |
39 switch (effective_default_setting) { | |
40 case CONTENT_SETTING_ALLOW: | |
41 label = l10n_util::GetStringUTF16( | |
42 IDS_WEBSITE_SETTINGS_MENU_ITEM_DEFAULT_ALLOW); | |
43 break; | |
44 case CONTENT_SETTING_BLOCK: | |
45 label = l10n_util::GetStringUTF16( | |
46 IDS_WEBSITE_SETTINGS_MENU_ITEM_DEFAULT_BLOCK); | |
47 break; | |
48 case CONTENT_SETTING_ASK: | |
49 label = | |
50 l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_MENU_ITEM_DEFAULT_ASK); | |
51 break; | |
52 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT: | |
53 // TODO(tommycli): We display the ASK string for DETECT because with | |
54 // HTML5 by Default, Chrome will ask before running Flash on most sites. | |
55 // Once the feature flag is gone, migrate the actual setting to ASK. | |
56 label = l10n_util::GetStringUTF16( | |
57 PluginUtils::ShouldPreferHtmlOverPlugins(host_content_settings_map_) | |
58 ? IDS_WEBSITE_SETTINGS_MENU_ITEM_DEFAULT_ASK | |
59 : IDS_WEBSITE_SETTINGS_MENU_ITEM_DEFAULT_DETECT_IMPORTANT_CONTENT)
; | |
60 break; | |
61 case CONTENT_SETTING_NUM_SETTINGS: | |
62 NOTREACHED(); | |
63 default: | |
64 break; | |
65 } | |
66 | |
67 // The Material UI for site settings uses comboboxes instead of menubuttons, | |
68 // which means the elements of the menu themselves have to be shorter, instead | |
69 // of simply setting a shorter label on the menubutton. | |
70 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { | |
71 label = WebsiteSettingsUI::PermissionActionToUIString( | |
72 profile, permission_.type, CONTENT_SETTING_DEFAULT, | |
73 effective_default_setting, permission_.source); | |
74 } | |
75 | |
76 AddCheckItem(CONTENT_SETTING_DEFAULT, label); | |
77 | |
78 // Retrieve the string to show for allowing the permission. | |
79 // Notifications does not support CONTENT_SETTING_ALLOW in incognito. | |
80 bool allow_disabled_for_notifications = | |
81 permission_.is_incognito && | |
82 permission_.type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS; | |
83 // Media only supports CONTENT_SETTTING_ALLOW for secure origins. | |
84 bool is_media_permission = | |
85 permission_.type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC || | |
86 permission_.type == CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA; | |
87 if (!allow_disabled_for_notifications && | |
88 (!is_media_permission || content::IsOriginSecure(url))) { | |
89 label = l10n_util::GetStringUTF16( | |
90 IDS_WEBSITE_SETTINGS_MENU_ITEM_ALLOW); | |
91 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { | |
92 label = WebsiteSettingsUI::PermissionActionToUIString( | |
93 profile, permission_.type, CONTENT_SETTING_ALLOW, | |
94 effective_default_setting, permission_.source); | |
95 } | |
96 AddCheckItem(CONTENT_SETTING_ALLOW, label); | |
97 } | |
98 | |
99 // TODO(tommycli): With the HTML5 by Default feature, Flash is treated the | |
100 // same as any other permission with ASK, i.e. there is no ASK exception. | |
101 // Once the feature flag is gone, remove this block of code entirely. | |
102 if (permission_.type == CONTENT_SETTINGS_TYPE_PLUGINS && | |
103 !PluginUtils::ShouldPreferHtmlOverPlugins(host_content_settings_map_)) { | |
104 label = l10n_util::GetStringUTF16( | |
105 IDS_WEBSITE_SETTINGS_MENU_ITEM_DETECT_IMPORTANT_CONTENT); | |
106 AddCheckItem(CONTENT_SETTING_DETECT_IMPORTANT_CONTENT, label); | |
107 } | |
108 | |
109 // Retrieve the string to show for blocking the permission. | |
110 label = l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_MENU_ITEM_BLOCK); | |
111 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { | |
112 label = WebsiteSettingsUI::PermissionActionToUIString( | |
113 profile, info.type, CONTENT_SETTING_BLOCK, effective_default_setting, | |
114 info.source); | |
115 } | |
116 AddCheckItem(CONTENT_SETTING_BLOCK, label); | |
117 } | |
118 | |
119 PermissionMenuModel::PermissionMenuModel(Profile* profile, | |
120 const GURL& url, | |
121 ContentSetting setting, | |
122 const ChangeCallback& callback) | |
123 : ui::SimpleMenuModel(this), | |
124 host_content_settings_map_( | |
125 HostContentSettingsMapFactory::GetForProfile(profile)), | |
126 callback_(callback) { | |
127 DCHECK(setting == CONTENT_SETTING_ALLOW || setting == CONTENT_SETTING_BLOCK); | |
128 permission_.type = CONTENT_SETTINGS_TYPE_DEFAULT; | |
129 permission_.setting = setting; | |
130 permission_.default_setting = CONTENT_SETTING_NUM_SETTINGS; | |
131 AddCheckItem(CONTENT_SETTING_ALLOW, | |
132 l10n_util::GetStringUTF16(IDS_PERMISSION_ALLOW)); | |
133 AddCheckItem(CONTENT_SETTING_BLOCK, | |
134 l10n_util::GetStringUTF16(IDS_PERMISSION_DENY)); | |
135 } | |
136 | |
137 PermissionMenuModel::~PermissionMenuModel() {} | |
138 | |
139 bool PermissionMenuModel::IsCommandIdChecked(int command_id) const { | |
140 ContentSetting setting = permission_.setting; | |
141 | |
142 #if BUILDFLAG(ENABLE_PLUGINS) | |
143 setting = PluginsFieldTrial::EffectiveContentSetting( | |
144 host_content_settings_map_, permission_.type, permission_.setting); | |
145 #endif // BUILDFLAG(ENABLE_PLUGINS) | |
146 | |
147 return setting == command_id; | |
148 } | |
149 | |
150 bool PermissionMenuModel::IsCommandIdEnabled(int command_id) const { | |
151 return true; | |
152 } | |
153 | |
154 void PermissionMenuModel::ExecuteCommand(int command_id, int event_flags) { | |
155 permission_.setting = static_cast<ContentSetting>(command_id); | |
156 callback_.Run(permission_); | |
157 } | |
OLD | NEW |