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/grit/generated_resources.h"
8#include "ui/base/l10n/l10n_util.h"
9
10PermissionMenuModel::PermissionMenuModel(
11    const GURL& url,
12    const WebsiteSettingsUI::PermissionInfo& info,
13    const ChangeCallback& callback)
14    : ui::SimpleMenuModel(this), permission_(info), callback_(callback) {
15  DCHECK(!callback_.is_null());
16  base::string16 label;
17  switch (permission_.default_setting) {
18    case CONTENT_SETTING_ALLOW:
19      label = l10n_util::GetStringUTF16(
20          IDS_WEBSITE_SETTINGS_MENU_ITEM_DEFAULT_ALLOW);
21      break;
22    case CONTENT_SETTING_BLOCK:
23      label = l10n_util::GetStringUTF16(
24          IDS_WEBSITE_SETTINGS_MENU_ITEM_DEFAULT_BLOCK);
25      break;
26    case CONTENT_SETTING_ASK:
27      label = l10n_util::GetStringUTF16(
28          IDS_WEBSITE_SETTINGS_MENU_ITEM_DEFAULT_ASK);
29      break;
30    case CONTENT_SETTING_NUM_SETTINGS:
31      NOTREACHED();
32    default:
33      break;
34  }
35  AddCheckItem(CONTENT_SETTING_DEFAULT, label);
36
37  // Media only support CONTENTE_SETTTING_ALLOW for https.
38  if (permission_.type != CONTENT_SETTINGS_TYPE_MEDIASTREAM ||
39      url.SchemeIsSecure()) {
40    label = l10n_util::GetStringUTF16(
41        IDS_WEBSITE_SETTINGS_MENU_ITEM_ALLOW);
42    AddCheckItem(CONTENT_SETTING_ALLOW, label);
43  }
44
45  if (permission_.type != CONTENT_SETTINGS_TYPE_FULLSCREEN) {
46    label = l10n_util::GetStringUTF16(
47        IDS_WEBSITE_SETTINGS_MENU_ITEM_BLOCK);
48    AddCheckItem(CONTENT_SETTING_BLOCK, label);
49  }
50}
51
52PermissionMenuModel::PermissionMenuModel(const GURL& url,
53                                         ContentSetting setting,
54                                         const ChangeCallback& callback)
55    : ui::SimpleMenuModel(this), callback_(callback) {
56  DCHECK(setting == CONTENT_SETTING_ALLOW || setting == CONTENT_SETTING_BLOCK);
57  permission_.type = CONTENT_SETTINGS_TYPE_DEFAULT;
58  permission_.setting = setting;
59  permission_.default_setting = CONTENT_SETTING_NUM_SETTINGS;
60  AddCheckItem(CONTENT_SETTING_ALLOW,
61               l10n_util::GetStringUTF16(IDS_PERMISSION_ALLOW));
62  AddCheckItem(CONTENT_SETTING_BLOCK,
63               l10n_util::GetStringUTF16(IDS_PERMISSION_DENY));
64}
65
66PermissionMenuModel::~PermissionMenuModel() {}
67
68bool PermissionMenuModel::IsCommandIdChecked(int command_id) const {
69  return permission_.setting == command_id;
70}
71
72bool PermissionMenuModel::IsCommandIdEnabled(int command_id) const {
73  return true;
74}
75
76bool PermissionMenuModel::GetAcceleratorForCommandId(
77    int command_id,
78    ui::Accelerator* accelerator) {
79  // Accelerators are not supported.
80  return false;
81}
82
83void PermissionMenuModel::ExecuteCommand(int command_id, int event_flags) {
84  permission_.setting = static_cast<ContentSetting>(command_id);
85  callback_.Run(permission_);
86}
87