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 #include "chrome/browser/ui/website_settings/permission_menu_model.h"
7 #include "chrome/grit/generated_resources.h"
8 #include "components/content_settings/core/browser/plugins_field_trial.h"
9 #include "content/public/common/origin_util.h"
10 #include "ui/base/l10n/l10n_util.h"
12 PermissionMenuModel::PermissionMenuModel(
14 const WebsiteSettingsUI::PermissionInfo
& info
,
15 const ChangeCallback
& callback
)
16 : ui::SimpleMenuModel(this), permission_(info
), callback_(callback
) {
17 DCHECK(!callback_
.is_null());
20 ContentSetting effective_default_setting
= permission_
.default_setting
;
22 #if defined(ENABLE_PLUGINS)
23 effective_default_setting
=
24 content_settings::PluginsFieldTrial::EffectiveContentSetting(
25 permission_
.type
, permission_
.default_setting
);
26 #endif // defined(ENABLE_PLUGINS)
28 switch (effective_default_setting
) {
29 case CONTENT_SETTING_ALLOW
:
30 label
= l10n_util::GetStringUTF16(
31 IDS_WEBSITE_SETTINGS_MENU_ITEM_DEFAULT_ALLOW
);
33 case CONTENT_SETTING_BLOCK
:
34 label
= l10n_util::GetStringUTF16(
35 IDS_WEBSITE_SETTINGS_MENU_ITEM_DEFAULT_BLOCK
);
37 case CONTENT_SETTING_ASK
:
39 l10n_util::GetStringUTF16(IDS_WEBSITE_SETTINGS_MENU_ITEM_DEFAULT_ASK
);
41 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT
:
42 label
= l10n_util::GetStringUTF16(
43 IDS_WEBSITE_SETTINGS_MENU_ITEM_DEFAULT_DETECT_IMPORTANT_CONTENT
);
45 case CONTENT_SETTING_NUM_SETTINGS
:
50 AddCheckItem(CONTENT_SETTING_DEFAULT
, label
);
52 // CONTENT_SETTING_ALLOW and CONTENT_SETTING_BLOCK are not allowed for
53 // fullscreen or mouse lock on file:// URLs, because there wouldn't be
54 // a reasonable origin with which to associate the preference.
55 // TODO(estark): Revisit this when crbug.com/455882 is fixed.
56 bool is_exclusive_access_on_file
=
57 (permission_
.type
== CONTENT_SETTINGS_TYPE_FULLSCREEN
||
58 permission_
.type
== CONTENT_SETTINGS_TYPE_MOUSELOCK
) &&
61 // Media only supports CONTENT_SETTTING_ALLOW for secure origins.
62 if ((permission_
.type
!= CONTENT_SETTINGS_TYPE_MEDIASTREAM
||
63 content::IsOriginSecure(url
)) &&
64 !is_exclusive_access_on_file
) {
65 label
= l10n_util::GetStringUTF16(
66 IDS_WEBSITE_SETTINGS_MENU_ITEM_ALLOW
);
67 AddCheckItem(CONTENT_SETTING_ALLOW
, label
);
70 if (permission_
.type
== CONTENT_SETTINGS_TYPE_PLUGINS
) {
71 label
= l10n_util::GetStringUTF16(
72 IDS_WEBSITE_SETTINGS_MENU_ITEM_DETECT_IMPORTANT_CONTENT
);
73 AddCheckItem(CONTENT_SETTING_DETECT_IMPORTANT_CONTENT
, label
);
76 if (permission_
.type
!= CONTENT_SETTINGS_TYPE_FULLSCREEN
&&
77 !is_exclusive_access_on_file
) {
78 label
= l10n_util::GetStringUTF16(
79 IDS_WEBSITE_SETTINGS_MENU_ITEM_BLOCK
);
80 AddCheckItem(CONTENT_SETTING_BLOCK
, label
);
84 PermissionMenuModel::PermissionMenuModel(const GURL
& url
,
85 ContentSetting setting
,
86 const ChangeCallback
& callback
)
87 : ui::SimpleMenuModel(this), callback_(callback
) {
88 DCHECK(setting
== CONTENT_SETTING_ALLOW
|| setting
== CONTENT_SETTING_BLOCK
);
89 permission_
.type
= CONTENT_SETTINGS_TYPE_DEFAULT
;
90 permission_
.setting
= setting
;
91 permission_
.default_setting
= CONTENT_SETTING_NUM_SETTINGS
;
92 AddCheckItem(CONTENT_SETTING_ALLOW
,
93 l10n_util::GetStringUTF16(IDS_PERMISSION_ALLOW
));
94 AddCheckItem(CONTENT_SETTING_BLOCK
,
95 l10n_util::GetStringUTF16(IDS_PERMISSION_DENY
));
98 PermissionMenuModel::~PermissionMenuModel() {}
100 bool PermissionMenuModel::IsCommandIdChecked(int command_id
) const {
101 ContentSetting setting
= permission_
.setting
;
103 #if defined(ENABLE_PLUGINS)
104 setting
= content_settings::PluginsFieldTrial::EffectiveContentSetting(
105 permission_
.type
, permission_
.setting
);
106 #endif // defined(ENABLE_PLUGINS)
108 return setting
== command_id
;
111 bool PermissionMenuModel::IsCommandIdEnabled(int command_id
) const {
115 bool PermissionMenuModel::GetAcceleratorForCommandId(
117 ui::Accelerator
* accelerator
) {
118 // Accelerators are not supported.
122 void PermissionMenuModel::ExecuteCommand(int command_id
, int event_flags
) {
123 permission_
.setting
= static_cast<ContentSetting
>(command_id
);
124 callback_
.Run(permission_
);